1 : // Copyright 2014 Google Inc. All Rights Reserved.
2 : //
3 : // Licensed under the Apache License, Version 2.0 (the "License");
4 : // you may not use this file except in compliance with the License.
5 : // You may obtain a copy of the License at
6 : //
7 : // http://www.apache.org/licenses/LICENSE-2.0
8 : //
9 : // Unless required by applicable law or agreed to in writing, software
10 : // distributed under the License is distributed on an "AS IS" BASIS,
11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 : // See the License for the specific language governing permissions and
13 : // limitations under the License.
14 : //
15 : // Utility functions used by the Asan check functions..
16 : #ifndef SYZYGY_AGENT_ASAN_RTL_UTILS_H_
17 : #define SYZYGY_AGENT_ASAN_RTL_UTILS_H_
18 :
19 : #include <windows.h>
20 :
21 : #include "syzygy/agent/asan/error_info.h"
22 :
23 : namespace agent {
24 : namespace asan {
25 :
26 : // Forward declarations.
27 : class AsanRuntime;
28 : class Shadow;
29 :
30 : // Contents of the registers before calling the Asan memory check function.
31 : // Note: the order of fields is significant!
32 : #pragma pack(push, 1)
33 : struct AsanContext {
34 : #ifdef _WIN64
35 : // TODO(loskutov): add more x64 registers or eliminate this piece of code.
36 : size_t original_rdi;
37 : size_t original_rsi;
38 : size_t original_rbp;
39 : size_t original_rsp;
40 : size_t original_rbx;
41 : size_t original_rdx;
42 : size_t original_rcx;
43 : size_t original_rax;
44 : DWORD original_eflags;
45 : size_t original_rip;
46 : #else
47 : DWORD original_edi;
48 : DWORD original_esi;
49 : DWORD original_ebp;
50 : DWORD original_esp;
51 : DWORD original_ebx;
52 : DWORD original_edx;
53 : DWORD original_ecx;
54 : DWORD original_eax;
55 : DWORD original_eflags;
56 : DWORD original_eip;
57 : #endif
58 : };
59 : #pragma pack(pop)
60 :
61 : // Set the AsanRuntime instance that should be used to report the crash.
62 : // @param runtime The runtime instance to use.
63 : void SetAsanRuntimeInstance(AsanRuntime* runtime);
64 :
65 : // Convert a CONTEXT struct to an Asan context.
66 : // @param context The context to convert.
67 : // @param asan_context Receives the Asan context.
68 : void ContextToAsanContext(const CONTEXT& context, AsanContext* asan_context);
69 :
70 : // Report a bad access to the memory.
71 : // @param location The memory address of the access.
72 : // @param access_mode The mode of the access.
73 : // @param access_size The size of the access.
74 : // @param asan_context The context of the access.
75 : void ReportBadMemoryAccess(const void* location,
76 : AccessMode access_mode,
77 : size_t access_size,
78 : const AsanContext& asan_context);
79 :
80 : // Report an invalid access to @p location.
81 : // @param location The memory address of the access.
82 : // @param access_mode The mode of the access.
83 : void ReportBadAccess(const void* location, AccessMode access_mode);
84 :
85 : // Test that a memory range is accessible. Report an error if it's not.
86 : // @param shadow The shadow memory to use.
87 : // @param memory The pointer to the beginning of the memory range that we want
88 : // to check.
89 : // @param size The size of the memory range that we want to check.
90 : // @param access_mode The access mode.
91 : void TestMemoryRange(Shadow* shadow,
92 : const uint8_t* memory,
93 : size_t size,
94 : AccessMode access_mode);
95 :
96 : // Helper function to test if the memory range of a given structure is
97 : // accessible.
98 : // @tparam T the type of the structure to be tested.
99 : // @param shadow The shadow memory to use.
100 : // @param structure A pointer to this structure.
101 : // @param access mode The access mode.
102 : template <typename T>
103 E : void TestStructure(Shadow* shadow, const T* structure, AccessMode access_mode) {
104 E : TestMemoryRange(shadow, reinterpret_cast<const uint8_t*>(structure),
105 : sizeof(T), access_mode);
106 E : }
107 :
108 : } // namespace asan
109 : } // namespace agent
110 :
111 :
112 : #endif // SYZYGY_AGENT_ASAN_RTL_UTILS_H_
|