* config/abi/post/alpha-linux-gnu/baseline_symbols.txt: Update.
[official-gcc.git] / libsanitizer / interception / interception_win.cc
blob9cd717518391f7725d7c15e3875a60bf64c93949
1 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of AddressSanitizer, an address sanity checker.
9 //
10 // Windows-specific interception methods.
11 //===----------------------------------------------------------------------===//
13 #ifdef _WIN32
15 #include "interception.h"
16 #include <windows.h>
18 namespace __interception {
20 bool GetRealFunctionAddress(const char *func_name, uptr *func_addr) {
21 const char *DLLS[] = {
22 "msvcr80.dll",
23 "msvcr90.dll",
24 "kernel32.dll",
25 NULL
27 *func_addr = 0;
28 for (size_t i = 0; *func_addr == 0 && DLLS[i]; ++i) {
29 *func_addr = (uptr)GetProcAddress(GetModuleHandleA(DLLS[i]), func_name);
31 return (*func_addr != 0);
34 // FIXME: internal_str* and internal_mem* functions should be moved from the
35 // ASan sources into interception/.
37 static void _memset(void *p, int value, size_t sz) {
38 for (size_t i = 0; i < sz; ++i)
39 ((char*)p)[i] = (char)value;
42 static void _memcpy(void *dst, void *src, size_t sz) {
43 char *dst_c = (char*)dst,
44 *src_c = (char*)src;
45 for (size_t i = 0; i < sz; ++i)
46 dst_c[i] = src_c[i];
49 static void WriteJumpInstruction(char *jmp_from, char *to) {
50 // jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset fromt jmp_from
51 // to the next instruction to the destination.
52 ptrdiff_t offset = to - jmp_from - 5;
53 *jmp_from = '\xE9';
54 *(ptrdiff_t*)(jmp_from + 1) = offset;
57 static char *GetMemoryForTrampoline(size_t size) {
58 // Trampolines are allocated from a common pool.
59 const int POOL_SIZE = 1024;
60 static char *pool = NULL;
61 static size_t pool_used = 0;
62 if (!pool) {
63 pool = (char *)VirtualAlloc(NULL, POOL_SIZE, MEM_RESERVE | MEM_COMMIT,
64 PAGE_EXECUTE_READWRITE);
65 // FIXME: Might want to apply PAGE_EXECUTE_READ access after all the
66 // interceptors are in place.
67 if (!pool)
68 return NULL;
69 _memset(pool, 0xCC /* int 3 */, POOL_SIZE);
72 if (pool_used + size > POOL_SIZE)
73 return NULL;
75 char *ret = pool + pool_used;
76 pool_used += size;
77 return ret;
80 // Returns 0 on error.
81 static size_t RoundUpToInstrBoundary(size_t size, char *code) {
82 size_t cursor = 0;
83 while (cursor < size) {
84 switch (code[cursor]) {
85 case '\x51': // push ecx
86 case '\x52': // push edx
87 case '\x53': // push ebx
88 case '\x54': // push esp
89 case '\x55': // push ebp
90 case '\x56': // push esi
91 case '\x57': // push edi
92 case '\x5D': // pop ebp
93 cursor++;
94 continue;
95 case '\x6A': // 6A XX = push XX
96 cursor += 2;
97 continue;
98 case '\xE9': // E9 XX YY ZZ WW = jmp WWZZYYXX
99 cursor += 5;
100 continue;
102 switch (*(unsigned short*)(code + cursor)) { // NOLINT
103 case 0xFF8B: // 8B FF = mov edi, edi
104 case 0xEC8B: // 8B EC = mov ebp, esp
105 case 0xC033: // 33 C0 = xor eax, eax
106 cursor += 2;
107 continue;
108 case 0x458B: // 8B 45 XX = mov eax, dword ptr [ebp+XXh]
109 case 0x5D8B: // 8B 5D XX = mov ebx, dword ptr [ebp+XXh]
110 case 0xEC83: // 83 EC XX = sub esp, XX
111 cursor += 3;
112 continue;
113 case 0xC1F7: // F7 C1 XX YY ZZ WW = test ecx, WWZZYYXX
114 cursor += 6;
115 continue;
116 case 0x3D83: // 83 3D XX YY ZZ WW TT = cmp TT, WWZZYYXX
117 cursor += 7;
118 continue;
120 switch (0x00FFFFFF & *(unsigned int*)(code + cursor)) {
121 case 0x24448A: // 8A 44 24 XX = mov eal, dword ptr [esp+XXh]
122 case 0x244C8B: // 8B 4C 24 XX = mov ecx, dword ptr [esp+XXh]
123 case 0x24548B: // 8B 54 24 XX = mov edx, dword ptr [esp+XXh]
124 case 0x24748B: // 8B 74 24 XX = mov esi, dword ptr [esp+XXh]
125 case 0x247C8B: // 8B 7C 24 XX = mov edi, dword ptr [esp+XXh]
126 cursor += 4;
127 continue;
130 // Unknown instruction!
131 // FIXME: Unknown instruction failures might happen when we add a new
132 // interceptor or a new compiler version. In either case, they should result
133 // in visible and readable error messages. However, merely calling abort()
134 // or __debugbreak() leads to an infinite recursion in CheckFailed.
135 // Do we have a good way to abort with an error message here?
136 return 0;
139 return cursor;
142 bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) {
143 #ifdef _WIN64
144 #error OverrideFunction is not yet supported on x64
145 #endif
146 // Function overriding works basically like this:
147 // We write "jmp <new_func>" (5 bytes) at the beginning of the 'old_func'
148 // to override it.
149 // We might want to be able to execute the original 'old_func' from the
150 // wrapper, in this case we need to keep the leading 5+ bytes ('head')
151 // of the original code somewhere with a "jmp <old_func+head>".
152 // We call these 'head'+5 bytes of instructions a "trampoline".
153 char *old_bytes = (char *)old_func;
155 // We'll need at least 5 bytes for a 'jmp'.
156 size_t head = 5;
157 if (orig_old_func) {
158 // Find out the number of bytes of the instructions we need to copy
159 // to the trampoline and store it in 'head'.
160 head = RoundUpToInstrBoundary(head, old_bytes);
161 if (!head)
162 return false;
164 // Put the needed instructions into the trampoline bytes.
165 char *trampoline = GetMemoryForTrampoline(head + 5);
166 if (!trampoline)
167 return false;
168 _memcpy(trampoline, old_bytes, head);
169 WriteJumpInstruction(trampoline + head, old_bytes + head);
170 *orig_old_func = (uptr)trampoline;
173 // Now put the "jmp <new_func>" instruction at the original code location.
174 // We should preserve the EXECUTE flag as some of our own code might be
175 // located in the same page (sic!). FIXME: might consider putting the
176 // __interception code into a separate section or something?
177 DWORD old_prot, unused_prot;
178 if (!VirtualProtect((void *)old_bytes, head, PAGE_EXECUTE_READWRITE,
179 &old_prot))
180 return false;
182 WriteJumpInstruction(old_bytes, (char *)new_func);
183 _memset(old_bytes + 5, 0xCC /* int 3 */, head - 5);
185 // Restore the original permissions.
186 if (!VirtualProtect((void *)old_bytes, head, old_prot, &unused_prot))
187 return false; // not clear if this failure bothers us.
189 return true;
192 } // namespace __interception
194 #endif // _WIN32