]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/interception/interception_win.cc
Merge compiler-rt trunk r338150, and resolve conflicts.
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / interception / interception_win.cc
1 //===-- interception_linux.cc -----------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file is a part of AddressSanitizer, an address sanity checker.
11 //
12 // Windows-specific interception methods.
13 //
14 // This file is implementing several hooking techniques to intercept calls
15 // to functions. The hooks are dynamically installed by modifying the assembly
16 // code.
17 //
18 // The hooking techniques are making assumptions on the way the code is
19 // generated and are safe under these assumptions.
20 //
21 // On 64-bit architecture, there is no direct 64-bit jump instruction. To allow
22 // arbitrary branching on the whole memory space, the notion of trampoline
23 // region is used. A trampoline region is a memory space withing 2G boundary
24 // where it is safe to add custom assembly code to build 64-bit jumps.
25 //
26 // Hooking techniques
27 // ==================
28 //
29 // 1) Detour
30 //
31 //    The Detour hooking technique is assuming the presence of an header with
32 //    padding and an overridable 2-bytes nop instruction (mov edi, edi). The
33 //    nop instruction can safely be replaced by a 2-bytes jump without any need
34 //    to save the instruction. A jump to the target is encoded in the function
35 //    header and the nop instruction is replaced by a short jump to the header.
36 //
37 //        head:  5 x nop                 head:  jmp <hook>
38 //        func:  mov edi, edi    -->     func:  jmp short <head>
39 //               [...]                   real:  [...]
40 //
41 //    This technique is only implemented on 32-bit architecture.
42 //    Most of the time, Windows API are hookable with the detour technique.
43 //
44 // 2) Redirect Jump
45 //
46 //    The redirect jump is applicable when the first instruction is a direct
47 //    jump. The instruction is replaced by jump to the hook.
48 //
49 //        func:  jmp <label>     -->     func:  jmp <hook>
50 //
51 //    On an 64-bit architecture, a trampoline is inserted.
52 //
53 //        func:  jmp <label>     -->     func:  jmp <tramp>
54 //                                              [...]
55 //
56 //                                   [trampoline]
57 //                                      tramp:  jmp QWORD [addr]
58 //                                       addr:  .bytes <hook>
59 //
60 //    Note: <real> is equilavent to <label>.
61 //
62 // 3) HotPatch
63 //
64 //    The HotPatch hooking is assuming the presence of an header with padding
65 //    and a first instruction with at least 2-bytes.
66 //
67 //    The reason to enforce the 2-bytes limitation is to provide the minimal
68 //    space to encode a short jump. HotPatch technique is only rewriting one
69 //    instruction to avoid breaking a sequence of instructions containing a
70 //    branching target.
71 //
72 //    Assumptions are enforced by MSVC compiler by using the /HOTPATCH flag.
73 //      see: https://msdn.microsoft.com/en-us/library/ms173507.aspx
74 //    Default padding length is 5 bytes in 32-bits and 6 bytes in 64-bits.
75 //
76 //        head:   5 x nop                head:  jmp <hook>
77 //        func:   <instr>        -->     func:  jmp short <head>
78 //                [...]                  body:  [...]
79 //
80 //                                   [trampoline]
81 //                                       real:  <instr>
82 //                                              jmp <body>
83 //
84 //    On an 64-bit architecture:
85 //
86 //        head:   6 x nop                head:  jmp QWORD [addr1]
87 //        func:   <instr>        -->     func:  jmp short <head>
88 //                [...]                  body:  [...]
89 //
90 //                                   [trampoline]
91 //                                      addr1:  .bytes <hook>
92 //                                       real:  <instr>
93 //                                              jmp QWORD [addr2]
94 //                                      addr2:  .bytes <body>
95 //
96 // 4) Trampoline
97 //
98 //    The Trampoline hooking technique is the most aggressive one. It is
99 //    assuming that there is a sequence of instructions that can be safely
100 //    replaced by a jump (enough room and no incoming branches).
101 //
102 //    Unfortunately, these assumptions can't be safely presumed and code may
103 //    be broken after hooking.
104 //
105 //        func:   <instr>        -->     func:  jmp <hook>
106 //                <instr>
107 //                [...]                  body:  [...]
108 //
109 //                                   [trampoline]
110 //                                       real:  <instr>
111 //                                              <instr>
112 //                                              jmp <body>
113 //
114 //    On an 64-bit architecture:
115 //
116 //        func:   <instr>        -->     func:  jmp QWORD [addr1]
117 //                <instr>
118 //                [...]                  body:  [...]
119 //
120 //                                   [trampoline]
121 //                                      addr1:  .bytes <hook>
122 //                                       real:  <instr>
123 //                                              <instr>
124 //                                              jmp QWORD [addr2]
125 //                                      addr2:  .bytes <body>
126 //===----------------------------------------------------------------------===//
127
128 #include "interception.h"
129
130 #if SANITIZER_WINDOWS
131 #include "sanitizer_common/sanitizer_platform.h"
132 #define WIN32_LEAN_AND_MEAN
133 #include <windows.h>
134
135 namespace __interception {
136
137 static const int kAddressLength = FIRST_32_SECOND_64(4, 8);
138 static const int kJumpInstructionLength = 5;
139 static const int kShortJumpInstructionLength = 2;
140 static const int kIndirectJumpInstructionLength = 6;
141 static const int kBranchLength =
142     FIRST_32_SECOND_64(kJumpInstructionLength, kIndirectJumpInstructionLength);
143 static const int kDirectBranchLength = kBranchLength + kAddressLength;
144
145 static void InterceptionFailed() {
146   // Do we have a good way to abort with an error message here?
147   __debugbreak();
148 }
149
150 static bool DistanceIsWithin2Gig(uptr from, uptr target) {
151 #if SANITIZER_WINDOWS64
152   if (from < target)
153     return target - from <= (uptr)0x7FFFFFFFU;
154   else
155     return from - target <= (uptr)0x80000000U;
156 #else
157   // In a 32-bit address space, the address calculation will wrap, so this check
158   // is unnecessary.
159   return true;
160 #endif
161 }
162
163 static uptr GetMmapGranularity() {
164   SYSTEM_INFO si;
165   GetSystemInfo(&si);
166   return si.dwAllocationGranularity;
167 }
168
169 static uptr RoundUpTo(uptr size, uptr boundary) {
170   return (size + boundary - 1) & ~(boundary - 1);
171 }
172
173 // FIXME: internal_str* and internal_mem* functions should be moved from the
174 // ASan sources into interception/.
175
176 static size_t _strlen(const char *str) {
177   const char* p = str;
178   while (*p != '\0') ++p;
179   return p - str;
180 }
181
182 static char* _strchr(char* str, char c) {
183   while (*str) {
184     if (*str == c)
185       return str;
186     ++str;
187   }
188   return nullptr;
189 }
190
191 static void _memset(void *p, int value, size_t sz) {
192   for (size_t i = 0; i < sz; ++i)
193     ((char*)p)[i] = (char)value;
194 }
195
196 static void _memcpy(void *dst, void *src, size_t sz) {
197   char *dst_c = (char*)dst,
198        *src_c = (char*)src;
199   for (size_t i = 0; i < sz; ++i)
200     dst_c[i] = src_c[i];
201 }
202
203 static bool ChangeMemoryProtection(
204     uptr address, uptr size, DWORD *old_protection) {
205   return ::VirtualProtect((void*)address, size,
206                           PAGE_EXECUTE_READWRITE,
207                           old_protection) != FALSE;
208 }
209
210 static bool RestoreMemoryProtection(
211     uptr address, uptr size, DWORD old_protection) {
212   DWORD unused;
213   return ::VirtualProtect((void*)address, size,
214                           old_protection,
215                           &unused) != FALSE;
216 }
217
218 static bool IsMemoryPadding(uptr address, uptr size) {
219   u8* function = (u8*)address;
220   for (size_t i = 0; i < size; ++i)
221     if (function[i] != 0x90 && function[i] != 0xCC)
222       return false;
223   return true;
224 }
225
226 static const u8 kHintNop9Bytes[] = {
227   0x66, 0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
228 };
229
230 template<class T>
231 static bool FunctionHasPrefix(uptr address, const T &pattern) {
232   u8* function = (u8*)address - sizeof(pattern);
233   for (size_t i = 0; i < sizeof(pattern); ++i)
234     if (function[i] != pattern[i])
235       return false;
236   return true;
237 }
238
239 static bool FunctionHasPadding(uptr address, uptr size) {
240   if (IsMemoryPadding(address - size, size))
241     return true;
242   if (size <= sizeof(kHintNop9Bytes) &&
243       FunctionHasPrefix(address, kHintNop9Bytes))
244     return true;
245   return false;
246 }
247
248 static void WritePadding(uptr from, uptr size) {
249   _memset((void*)from, 0xCC, (size_t)size);
250 }
251
252 static void WriteJumpInstruction(uptr from, uptr target) {
253   if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target))
254     InterceptionFailed();
255   ptrdiff_t offset = target - from - kJumpInstructionLength;
256   *(u8*)from = 0xE9;
257   *(u32*)(from + 1) = offset;
258 }
259
260 static void WriteShortJumpInstruction(uptr from, uptr target) {
261   sptr offset = target - from - kShortJumpInstructionLength;
262   if (offset < -128 || offset > 127)
263     InterceptionFailed();
264   *(u8*)from = 0xEB;
265   *(u8*)(from + 1) = (u8)offset;
266 }
267
268 #if SANITIZER_WINDOWS64
269 static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) {
270   // jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative
271   // offset.
272   // The offset is the distance from then end of the jump instruction to the
273   // memory location containing the targeted address. The displacement is still
274   // 32-bit in x64, so indirect_target must be located within +/- 2GB range.
275   int offset = indirect_target - from - kIndirectJumpInstructionLength;
276   if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength,
277                             indirect_target)) {
278     InterceptionFailed();
279   }
280   *(u16*)from = 0x25FF;
281   *(u32*)(from + 2) = offset;
282 }
283 #endif
284
285 static void WriteBranch(
286     uptr from, uptr indirect_target, uptr target) {
287 #if SANITIZER_WINDOWS64
288   WriteIndirectJumpInstruction(from, indirect_target);
289   *(u64*)indirect_target = target;
290 #else
291   (void)indirect_target;
292   WriteJumpInstruction(from, target);
293 #endif
294 }
295
296 static void WriteDirectBranch(uptr from, uptr target) {
297 #if SANITIZER_WINDOWS64
298   // Emit an indirect jump through immediately following bytes:
299   //   jmp [rip + kBranchLength]
300   //   .quad <target>
301   WriteBranch(from, from + kBranchLength, target);
302 #else
303   WriteJumpInstruction(from, target);
304 #endif
305 }
306
307 struct TrampolineMemoryRegion {
308   uptr content;
309   uptr allocated_size;
310   uptr max_size;
311 };
312
313 static const uptr kTrampolineScanLimitRange = 1 << 31;  // 2 gig
314 static const int kMaxTrampolineRegion = 1024;
315 static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion];
316
317 static void *AllocateTrampolineRegion(uptr image_address, size_t granularity) {
318 #if SANITIZER_WINDOWS64
319   uptr address = image_address;
320   uptr scanned = 0;
321   while (scanned < kTrampolineScanLimitRange) {
322     MEMORY_BASIC_INFORMATION info;
323     if (!::VirtualQuery((void*)address, &info, sizeof(info)))
324       return nullptr;
325
326     // Check whether a region can be allocated at |address|.
327     if (info.State == MEM_FREE && info.RegionSize >= granularity) {
328       void *page = ::VirtualAlloc((void*)RoundUpTo(address, granularity),
329                                   granularity,
330                                   MEM_RESERVE | MEM_COMMIT,
331                                   PAGE_EXECUTE_READWRITE);
332       return page;
333     }
334
335     // Move to the next region.
336     address = (uptr)info.BaseAddress + info.RegionSize;
337     scanned += info.RegionSize;
338   }
339   return nullptr;
340 #else
341   return ::VirtualAlloc(nullptr,
342                         granularity,
343                         MEM_RESERVE | MEM_COMMIT,
344                         PAGE_EXECUTE_READWRITE);
345 #endif
346 }
347
348 // Used by unittests to release mapped memory space.
349 void TestOnlyReleaseTrampolineRegions() {
350   for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
351     TrampolineMemoryRegion *current = &TrampolineRegions[bucket];
352     if (current->content == 0)
353       return;
354     ::VirtualFree((void*)current->content, 0, MEM_RELEASE);
355     current->content = 0;
356   }
357 }
358
359 static uptr AllocateMemoryForTrampoline(uptr image_address, size_t size) {
360   // Find a region within 2G with enough space to allocate |size| bytes.
361   TrampolineMemoryRegion *region = nullptr;
362   for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
363     TrampolineMemoryRegion* current = &TrampolineRegions[bucket];
364     if (current->content == 0) {
365       // No valid region found, allocate a new region.
366       size_t bucket_size = GetMmapGranularity();
367       void *content = AllocateTrampolineRegion(image_address, bucket_size);
368       if (content == nullptr)
369         return 0U;
370
371       current->content = (uptr)content;
372       current->allocated_size = 0;
373       current->max_size = bucket_size;
374       region = current;
375       break;
376     } else if (current->max_size - current->allocated_size > size) {
377 #if SANITIZER_WINDOWS64
378         // In 64-bits, the memory space must be allocated within 2G boundary.
379         uptr next_address = current->content + current->allocated_size;
380         if (next_address < image_address ||
381             next_address - image_address >= 0x7FFF0000)
382           continue;
383 #endif
384       // The space can be allocated in the current region.
385       region = current;
386       break;
387     }
388   }
389
390   // Failed to find a region.
391   if (region == nullptr)
392     return 0U;
393
394   // Allocate the space in the current region.
395   uptr allocated_space = region->content + region->allocated_size;
396   region->allocated_size += size;
397   WritePadding(allocated_space, size);
398
399   return allocated_space;
400 }
401
402 // Returns 0 on error.
403 static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
404   switch (*(u64*)address) {
405     case 0x90909090909006EB:  // stub: jmp over 6 x nop.
406       return 8;
407   }
408
409   switch (*(u8*)address) {
410     case 0x90:  // 90 : nop
411       return 1;
412
413     case 0x50:  // push eax / rax
414     case 0x51:  // push ecx / rcx
415     case 0x52:  // push edx / rdx
416     case 0x53:  // push ebx / rbx
417     case 0x54:  // push esp / rsp
418     case 0x55:  // push ebp / rbp
419     case 0x56:  // push esi / rsi
420     case 0x57:  // push edi / rdi
421     case 0x5D:  // pop ebp / rbp
422       return 1;
423
424     case 0x6A:  // 6A XX = push XX
425       return 2;
426
427     case 0xb8:  // b8 XX XX XX XX : mov eax, XX XX XX XX
428     case 0xB9:  // b9 XX XX XX XX : mov ecx, XX XX XX XX
429       return 5;
430
431     // Cannot overwrite control-instruction. Return 0 to indicate failure.
432     case 0xE9:  // E9 XX XX XX XX : jmp <label>
433     case 0xE8:  // E8 XX XX XX XX : call <func>
434     case 0xC3:  // C3 : ret
435     case 0xEB:  // EB XX : jmp XX (short jump)
436     case 0x70:  // 7Y YY : jy XX (short conditional jump)
437     case 0x71:
438     case 0x72:
439     case 0x73:
440     case 0x74:
441     case 0x75:
442     case 0x76:
443     case 0x77:
444     case 0x78:
445     case 0x79:
446     case 0x7A:
447     case 0x7B:
448     case 0x7C:
449     case 0x7D:
450     case 0x7E:
451     case 0x7F:
452       return 0;
453   }
454
455   switch (*(u16*)(address)) {
456     case 0x018A:  // 8A 01 : mov al, byte ptr [ecx]
457     case 0xFF8B:  // 8B FF : mov edi, edi
458     case 0xEC8B:  // 8B EC : mov ebp, esp
459     case 0xc889:  // 89 C8 : mov eax, ecx
460     case 0xC18B:  // 8B C1 : mov eax, ecx
461     case 0xC033:  // 33 C0 : xor eax, eax
462     case 0xC933:  // 33 C9 : xor ecx, ecx
463     case 0xD233:  // 33 D2 : xor edx, edx
464       return 2;
465
466     // Cannot overwrite control-instruction. Return 0 to indicate failure.
467     case 0x25FF:  // FF 25 XX XX XX XX : jmp [XXXXXXXX]
468       return 0;
469   }
470
471   switch (0x00FFFFFF & *(u32*)address) {
472     case 0x24A48D:  // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX]
473       return 7;
474   }
475
476 #if SANITIZER_WINDOWS64
477   switch (*(u8*)address) {
478     case 0xA1:  // A1 XX XX XX XX XX XX XX XX :
479                 //   movabs eax, dword ptr ds:[XXXXXXXX]
480       return 9;
481   }
482
483   switch (*(u16*)address) {
484     case 0x5040:  // push rax
485     case 0x5140:  // push rcx
486     case 0x5240:  // push rdx
487     case 0x5340:  // push rbx
488     case 0x5440:  // push rsp
489     case 0x5540:  // push rbp
490     case 0x5640:  // push rsi
491     case 0x5740:  // push rdi
492     case 0x5441:  // push r12
493     case 0x5541:  // push r13
494     case 0x5641:  // push r14
495     case 0x5741:  // push r15
496     case 0x9066:  // Two-byte NOP
497       return 2;
498
499     case 0x058B:  // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX]
500       if (rel_offset)
501         *rel_offset = 2;
502       return 6;
503   }
504
505   switch (0x00FFFFFF & *(u32*)address) {
506     case 0xe58948:    // 48 8b c4 : mov rbp, rsp
507     case 0xc18b48:    // 48 8b c1 : mov rax, rcx
508     case 0xc48b48:    // 48 8b c4 : mov rax, rsp
509     case 0xd9f748:    // 48 f7 d9 : neg rcx
510     case 0xd12b48:    // 48 2b d1 : sub rdx, rcx
511     case 0x07c1f6:    // f6 c1 07 : test cl, 0x7
512     case 0xc98548:    // 48 85 C9 : test rcx, rcx
513     case 0xc0854d:    // 4d 85 c0 : test r8, r8
514     case 0xc2b60f:    // 0f b6 c2 : movzx eax, dl
515     case 0xc03345:    // 45 33 c0 : xor r8d, r8d
516     case 0xdb3345:    // 45 33 DB : xor r11d, r11d
517     case 0xd98b4c:    // 4c 8b d9 : mov r11, rcx
518     case 0xd28b4c:    // 4c 8b d2 : mov r10, rdx
519     case 0xc98b4c:    // 4C 8B C9 : mov r9, rcx
520     case 0xd2b60f:    // 0f b6 d2 : movzx edx, dl
521     case 0xca2b48:    // 48 2b ca : sub rcx, rdx
522     case 0x10b70f:    // 0f b7 10 : movzx edx, WORD PTR [rax]
523     case 0xc00b4d:    // 3d 0b c0 : or r8, r8
524     case 0xd18b48:    // 48 8b d1 : mov rdx, rcx
525     case 0xdc8b4c:    // 4c 8b dc : mov r11, rsp
526     case 0xd18b4c:    // 4c 8b d1 : mov r10, rcx
527       return 3;
528
529     case 0xec8348:    // 48 83 ec XX : sub rsp, XX
530     case 0xf88349:    // 49 83 f8 XX : cmp r8, XX
531     case 0x588948:    // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx
532       return 4;
533
534     case 0xec8148:    // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX
535       return 7;
536
537     case 0x058b48:    // 48 8b 05 XX XX XX XX :
538                       //   mov rax, QWORD PTR [rip + XXXXXXXX]
539     case 0x25ff48:    // 48 ff 25 XX XX XX XX :
540                       //   rex.W jmp QWORD PTR [rip + XXXXXXXX]
541
542       // Instructions having offset relative to 'rip' need offset adjustment.
543       if (rel_offset)
544         *rel_offset = 3;
545       return 7;
546
547     case 0x2444c7:    // C7 44 24 XX YY YY YY YY
548                       //   mov dword ptr [rsp + XX], YYYYYYYY
549       return 8;
550   }
551
552   switch (*(u32*)(address)) {
553     case 0x24448b48:  // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX]
554     case 0x246c8948:  // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp
555     case 0x245c8948:  // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx
556     case 0x24748948:  // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi
557     case 0x244C8948:  // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx
558       return 5;
559     case 0x24648348:  // 48 83 64 24 XX : and QWORD PTR [rsp + XX], YY
560       return 6;
561   }
562
563 #else
564
565   switch (*(u8*)address) {
566     case 0xA1:  // A1 XX XX XX XX :  mov eax, dword ptr ds:[XXXXXXXX]
567       return 5;
568   }
569   switch (*(u16*)address) {
570     case 0x458B:  // 8B 45 XX : mov eax, dword ptr [ebp + XX]
571     case 0x5D8B:  // 8B 5D XX : mov ebx, dword ptr [ebp + XX]
572     case 0x7D8B:  // 8B 7D XX : mov edi, dword ptr [ebp + XX]
573     case 0xEC83:  // 83 EC XX : sub esp, XX
574     case 0x75FF:  // FF 75 XX : push dword ptr [ebp + XX]
575       return 3;
576     case 0xC1F7:  // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX
577     case 0x25FF:  // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX]
578       return 6;
579     case 0x3D83:  // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX
580       return 7;
581     case 0x7D83:  // 83 7D XX YY : cmp dword ptr [ebp + XX], YY
582       return 4;
583   }
584
585   switch (0x00FFFFFF & *(u32*)address) {
586     case 0x24448A:  // 8A 44 24 XX : mov eal, dword ptr [esp + XX]
587     case 0x24448B:  // 8B 44 24 XX : mov eax, dword ptr [esp + XX]
588     case 0x244C8B:  // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX]
589     case 0x24548B:  // 8B 54 24 XX : mov edx, dword ptr [esp + XX]
590     case 0x24748B:  // 8B 74 24 XX : mov esi, dword ptr [esp + XX]
591     case 0x247C8B:  // 8B 7C 24 XX : mov edi, dword ptr [esp + XX]
592       return 4;
593   }
594
595   switch (*(u32*)address) {
596     case 0x2444B60F:  // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX]
597       return 5;
598   }
599 #endif
600
601   // Unknown instruction!
602   // FIXME: Unknown instruction failures might happen when we add a new
603   // interceptor or a new compiler version. In either case, they should result
604   // in visible and readable error messages. However, merely calling abort()
605   // leads to an infinite recursion in CheckFailed.
606   InterceptionFailed();
607   return 0;
608 }
609
610 // Returns 0 on error.
611 static size_t RoundUpToInstrBoundary(size_t size, uptr address) {
612   size_t cursor = 0;
613   while (cursor < size) {
614     size_t instruction_size = GetInstructionSize(address + cursor);
615     if (!instruction_size)
616       return 0;
617     cursor += instruction_size;
618   }
619   return cursor;
620 }
621
622 static bool CopyInstructions(uptr to, uptr from, size_t size) {
623   size_t cursor = 0;
624   while (cursor != size) {
625     size_t rel_offset = 0;
626     size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset);
627     _memcpy((void*)(to + cursor), (void*)(from + cursor),
628             (size_t)instruction_size);
629     if (rel_offset) {
630       uptr delta = to - from;
631       uptr relocated_offset = *(u32*)(to + cursor + rel_offset) - delta;
632 #if SANITIZER_WINDOWS64
633       if (relocated_offset + 0x80000000U >= 0xFFFFFFFFU)
634         return false;
635 #endif
636       *(u32*)(to + cursor + rel_offset) = relocated_offset;
637     }
638     cursor += instruction_size;
639   }
640   return true;
641 }
642
643
644 #if !SANITIZER_WINDOWS64
645 bool OverrideFunctionWithDetour(
646     uptr old_func, uptr new_func, uptr *orig_old_func) {
647   const int kDetourHeaderLen = 5;
648   const u16 kDetourInstruction = 0xFF8B;
649
650   uptr header = (uptr)old_func - kDetourHeaderLen;
651   uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength;
652
653   // Validate that the function is hookable.
654   if (*(u16*)old_func != kDetourInstruction ||
655       !IsMemoryPadding(header, kDetourHeaderLen))
656     return false;
657
658   // Change memory protection to writable.
659   DWORD protection = 0;
660   if (!ChangeMemoryProtection(header, patch_length, &protection))
661     return false;
662
663   // Write a relative jump to the redirected function.
664   WriteJumpInstruction(header, new_func);
665
666   // Write the short jump to the function prefix.
667   WriteShortJumpInstruction(old_func, header);
668
669   // Restore previous memory protection.
670   if (!RestoreMemoryProtection(header, patch_length, protection))
671     return false;
672
673   if (orig_old_func)
674     *orig_old_func = old_func + kShortJumpInstructionLength;
675
676   return true;
677 }
678 #endif
679
680 bool OverrideFunctionWithRedirectJump(
681     uptr old_func, uptr new_func, uptr *orig_old_func) {
682   // Check whether the first instruction is a relative jump.
683   if (*(u8*)old_func != 0xE9)
684     return false;
685
686   if (orig_old_func) {
687     uptr relative_offset = *(u32*)(old_func + 1);
688     uptr absolute_target = old_func + relative_offset + kJumpInstructionLength;
689     *orig_old_func = absolute_target;
690   }
691
692 #if SANITIZER_WINDOWS64
693   // If needed, get memory space for a trampoline jump.
694   uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength);
695   if (!trampoline)
696     return false;
697   WriteDirectBranch(trampoline, new_func);
698 #endif
699
700   // Change memory protection to writable.
701   DWORD protection = 0;
702   if (!ChangeMemoryProtection(old_func, kJumpInstructionLength, &protection))
703     return false;
704
705   // Write a relative jump to the redirected function.
706   WriteJumpInstruction(old_func, FIRST_32_SECOND_64(new_func, trampoline));
707
708   // Restore previous memory protection.
709   if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection))
710     return false;
711
712   return true;
713 }
714
715 bool OverrideFunctionWithHotPatch(
716     uptr old_func, uptr new_func, uptr *orig_old_func) {
717   const int kHotPatchHeaderLen = kBranchLength;
718
719   uptr header = (uptr)old_func - kHotPatchHeaderLen;
720   uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength;
721
722   // Validate that the function is hot patchable.
723   size_t instruction_size = GetInstructionSize(old_func);
724   if (instruction_size < kShortJumpInstructionLength ||
725       !FunctionHasPadding(old_func, kHotPatchHeaderLen))
726     return false;
727
728   if (orig_old_func) {
729     // Put the needed instructions into the trampoline bytes.
730     uptr trampoline_length = instruction_size + kDirectBranchLength;
731     uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
732     if (!trampoline)
733       return false;
734     if (!CopyInstructions(trampoline, old_func, instruction_size))
735       return false;
736     WriteDirectBranch(trampoline + instruction_size,
737                       old_func + instruction_size);
738     *orig_old_func = trampoline;
739   }
740
741   // If needed, get memory space for indirect address.
742   uptr indirect_address = 0;
743 #if SANITIZER_WINDOWS64
744   indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
745   if (!indirect_address)
746     return false;
747 #endif
748
749   // Change memory protection to writable.
750   DWORD protection = 0;
751   if (!ChangeMemoryProtection(header, patch_length, &protection))
752     return false;
753
754   // Write jumps to the redirected function.
755   WriteBranch(header, indirect_address, new_func);
756   WriteShortJumpInstruction(old_func, header);
757
758   // Restore previous memory protection.
759   if (!RestoreMemoryProtection(header, patch_length, protection))
760     return false;
761
762   return true;
763 }
764
765 bool OverrideFunctionWithTrampoline(
766     uptr old_func, uptr new_func, uptr *orig_old_func) {
767
768   size_t instructions_length = kBranchLength;
769   size_t padding_length = 0;
770   uptr indirect_address = 0;
771
772   if (orig_old_func) {
773     // Find out the number of bytes of the instructions we need to copy
774     // to the trampoline.
775     instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func);
776     if (!instructions_length)
777       return false;
778
779     // Put the needed instructions into the trampoline bytes.
780     uptr trampoline_length = instructions_length + kDirectBranchLength;
781     uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
782     if (!trampoline)
783       return false;
784     if (!CopyInstructions(trampoline, old_func, instructions_length))
785       return false;
786     WriteDirectBranch(trampoline + instructions_length,
787                       old_func + instructions_length);
788     *orig_old_func = trampoline;
789   }
790
791 #if SANITIZER_WINDOWS64
792   // Check if the targeted address can be encoded in the function padding.
793   // Otherwise, allocate it in the trampoline region.
794   if (IsMemoryPadding(old_func - kAddressLength, kAddressLength)) {
795     indirect_address = old_func - kAddressLength;
796     padding_length = kAddressLength;
797   } else {
798     indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
799     if (!indirect_address)
800       return false;
801   }
802 #endif
803
804   // Change memory protection to writable.
805   uptr patch_address = old_func - padding_length;
806   uptr patch_length = instructions_length + padding_length;
807   DWORD protection = 0;
808   if (!ChangeMemoryProtection(patch_address, patch_length, &protection))
809     return false;
810
811   // Patch the original function.
812   WriteBranch(old_func, indirect_address, new_func);
813
814   // Restore previous memory protection.
815   if (!RestoreMemoryProtection(patch_address, patch_length, protection))
816     return false;
817
818   return true;
819 }
820
821 bool OverrideFunction(
822     uptr old_func, uptr new_func, uptr *orig_old_func) {
823 #if !SANITIZER_WINDOWS64
824   if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func))
825     return true;
826 #endif
827   if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func))
828     return true;
829   if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func))
830     return true;
831   if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func))
832     return true;
833   return false;
834 }
835
836 static void **InterestingDLLsAvailable() {
837   static const char *InterestingDLLs[] = {
838       "kernel32.dll",
839       "msvcr100.dll",      // VS2010
840       "msvcr110.dll",      // VS2012
841       "msvcr120.dll",      // VS2013
842       "vcruntime140.dll",  // VS2015
843       "ucrtbase.dll",      // Universal CRT
844       // NTDLL should go last as it exports some functions that we should
845       // override in the CRT [presumably only used internally].
846       "ntdll.dll", NULL};
847   static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };
848   if (!result[0]) {
849     for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {
850       if (HMODULE h = GetModuleHandleA(InterestingDLLs[i]))
851         result[j++] = (void *)h;
852     }
853   }
854   return &result[0];
855 }
856
857 namespace {
858 // Utility for reading loaded PE images.
859 template <typename T> class RVAPtr {
860  public:
861   RVAPtr(void *module, uptr rva)
862       : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {}
863   operator T *() { return ptr_; }
864   T *operator->() { return ptr_; }
865   T *operator++() { return ++ptr_; }
866
867  private:
868   T *ptr_;
869 };
870 } // namespace
871
872 // Internal implementation of GetProcAddress. At least since Windows 8,
873 // GetProcAddress appears to initialize DLLs before returning function pointers
874 // into them. This is problematic for the sanitizers, because they typically
875 // want to intercept malloc *before* MSVCRT initializes. Our internal
876 // implementation walks the export list manually without doing initialization.
877 uptr InternalGetProcAddress(void *module, const char *func_name) {
878   // Check that the module header is full and present.
879   RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
880   RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
881   if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ"
882       headers->Signature != IMAGE_NT_SIGNATURE ||           // "PE\0\0"
883       headers->FileHeader.SizeOfOptionalHeader <
884           sizeof(IMAGE_OPTIONAL_HEADER)) {
885     return 0;
886   }
887
888   IMAGE_DATA_DIRECTORY *export_directory =
889       &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
890   if (export_directory->Size == 0)
891     return 0;
892   RVAPtr<IMAGE_EXPORT_DIRECTORY> exports(module,
893                                          export_directory->VirtualAddress);
894   RVAPtr<DWORD> functions(module, exports->AddressOfFunctions);
895   RVAPtr<DWORD> names(module, exports->AddressOfNames);
896   RVAPtr<WORD> ordinals(module, exports->AddressOfNameOrdinals);
897
898   for (DWORD i = 0; i < exports->NumberOfNames; i++) {
899     RVAPtr<char> name(module, names[i]);
900     if (!strcmp(func_name, name)) {
901       DWORD index = ordinals[i];
902       RVAPtr<char> func(module, functions[index]);
903
904       // Handle forwarded functions.
905       DWORD offset = functions[index];
906       if (offset >= export_directory->VirtualAddress &&
907           offset < export_directory->VirtualAddress + export_directory->Size) {
908         // An entry for a forwarded function is a string with the following
909         // format: "<module> . <function_name>" that is stored into the
910         // exported directory.
911         char function_name[256];
912         size_t funtion_name_length = _strlen(func);
913         if (funtion_name_length >= sizeof(function_name) - 1)
914           InterceptionFailed();
915
916         _memcpy(function_name, func, funtion_name_length);
917         function_name[funtion_name_length] = '\0';
918         char* separator = _strchr(function_name, '.');
919         if (!separator)
920           InterceptionFailed();
921         *separator = '\0';
922
923         void* redirected_module = GetModuleHandleA(function_name);
924         if (!redirected_module)
925           InterceptionFailed();
926         return InternalGetProcAddress(redirected_module, separator + 1);
927       }
928
929       return (uptr)(char *)func;
930     }
931   }
932
933   return 0;
934 }
935
936 bool OverrideFunction(
937     const char *func_name, uptr new_func, uptr *orig_old_func) {
938   bool hooked = false;
939   void **DLLs = InterestingDLLsAvailable();
940   for (size_t i = 0; DLLs[i]; ++i) {
941     uptr func_addr = InternalGetProcAddress(DLLs[i], func_name);
942     if (func_addr &&
943         OverrideFunction(func_addr, new_func, orig_old_func)) {
944       hooked = true;
945     }
946   }
947   return hooked;
948 }
949
950 bool OverrideImportedFunction(const char *module_to_patch,
951                               const char *imported_module,
952                               const char *function_name, uptr new_function,
953                               uptr *orig_old_func) {
954   HMODULE module = GetModuleHandleA(module_to_patch);
955   if (!module)
956     return false;
957
958   // Check that the module header is full and present.
959   RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
960   RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
961   if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE || // "MZ"
962       headers->Signature != IMAGE_NT_SIGNATURE ||            // "PE\0\0"
963       headers->FileHeader.SizeOfOptionalHeader <
964           sizeof(IMAGE_OPTIONAL_HEADER)) {
965     return false;
966   }
967
968   IMAGE_DATA_DIRECTORY *import_directory =
969       &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
970
971   // Iterate the list of imported DLLs. FirstThunk will be null for the last
972   // entry.
973   RVAPtr<IMAGE_IMPORT_DESCRIPTOR> imports(module,
974                                           import_directory->VirtualAddress);
975   for (; imports->FirstThunk != 0; ++imports) {
976     RVAPtr<const char> modname(module, imports->Name);
977     if (_stricmp(&*modname, imported_module) == 0)
978       break;
979   }
980   if (imports->FirstThunk == 0)
981     return false;
982
983   // We have two parallel arrays: the import address table (IAT) and the table
984   // of names. They start out containing the same data, but the loader rewrites
985   // the IAT to hold imported addresses and leaves the name table in
986   // OriginalFirstThunk alone.
987   RVAPtr<IMAGE_THUNK_DATA> name_table(module, imports->OriginalFirstThunk);
988   RVAPtr<IMAGE_THUNK_DATA> iat(module, imports->FirstThunk);
989   for (; name_table->u1.Ordinal != 0; ++name_table, ++iat) {
990     if (!IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) {
991       RVAPtr<IMAGE_IMPORT_BY_NAME> import_by_name(
992           module, name_table->u1.ForwarderString);
993       const char *funcname = &import_by_name->Name[0];
994       if (strcmp(funcname, function_name) == 0)
995         break;
996     }
997   }
998   if (name_table->u1.Ordinal == 0)
999     return false;
1000
1001   // Now we have the correct IAT entry. Do the swap. We have to make the page
1002   // read/write first.
1003   if (orig_old_func)
1004     *orig_old_func = iat->u1.AddressOfData;
1005   DWORD old_prot, unused_prot;
1006   if (!VirtualProtect(&iat->u1.AddressOfData, 4, PAGE_EXECUTE_READWRITE,
1007                       &old_prot))
1008     return false;
1009   iat->u1.AddressOfData = new_function;
1010   if (!VirtualProtect(&iat->u1.AddressOfData, 4, old_prot, &unused_prot))
1011     return false;  // Not clear if this failure bothers us.
1012   return true;
1013 }
1014
1015 }  // namespace __interception
1016
1017 #endif  // SANITIZER_MAC