xref: /llvm-project/compiler-rt/lib/interception/interception_win.cpp (revision bbf377060adc8607e1187952388c7eeea7cf4933)
12385cf66SAlexandre Ganea //===-- interception_win.cpp ------------------------------------*- C++ -*-===//
2ebbce04cSNico Weber //
3ebbce04cSNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4ebbce04cSNico Weber // See https://llvm.org/LICENSE.txt for license information.
5ebbce04cSNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebbce04cSNico Weber //
7ebbce04cSNico Weber //===----------------------------------------------------------------------===//
8ebbce04cSNico Weber //
9ebbce04cSNico Weber // This file is a part of AddressSanitizer, an address sanity checker.
10ebbce04cSNico Weber //
11ebbce04cSNico Weber // Windows-specific interception methods.
12ebbce04cSNico Weber //
13ebbce04cSNico Weber // This file is implementing several hooking techniques to intercept calls
14ebbce04cSNico Weber // to functions. The hooks are dynamically installed by modifying the assembly
15ebbce04cSNico Weber // code.
16ebbce04cSNico Weber //
17ebbce04cSNico Weber // The hooking techniques are making assumptions on the way the code is
18ebbce04cSNico Weber // generated and are safe under these assumptions.
19ebbce04cSNico Weber //
20ebbce04cSNico Weber // On 64-bit architecture, there is no direct 64-bit jump instruction. To allow
21ebbce04cSNico Weber // arbitrary branching on the whole memory space, the notion of trampoline
22ebbce04cSNico Weber // region is used. A trampoline region is a memory space withing 2G boundary
23ebbce04cSNico Weber // where it is safe to add custom assembly code to build 64-bit jumps.
24ebbce04cSNico Weber //
25ebbce04cSNico Weber // Hooking techniques
26ebbce04cSNico Weber // ==================
27ebbce04cSNico Weber //
28ebbce04cSNico Weber // 1) Detour
29ebbce04cSNico Weber //
3004ccbe6eSHans Wennborg //    The Detour hooking technique is assuming the presence of a header with
31ebbce04cSNico Weber //    padding and an overridable 2-bytes nop instruction (mov edi, edi). The
32ebbce04cSNico Weber //    nop instruction can safely be replaced by a 2-bytes jump without any need
33ebbce04cSNico Weber //    to save the instruction. A jump to the target is encoded in the function
34ebbce04cSNico Weber //    header and the nop instruction is replaced by a short jump to the header.
35ebbce04cSNico Weber //
36ebbce04cSNico Weber //        head:  5 x nop                 head:  jmp <hook>
37ebbce04cSNico Weber //        func:  mov edi, edi    -->     func:  jmp short <head>
38ebbce04cSNico Weber //               [...]                   real:  [...]
39ebbce04cSNico Weber //
40ebbce04cSNico Weber //    This technique is only implemented on 32-bit architecture.
41ebbce04cSNico Weber //    Most of the time, Windows API are hookable with the detour technique.
42ebbce04cSNico Weber //
43ebbce04cSNico Weber // 2) Redirect Jump
44ebbce04cSNico Weber //
45ebbce04cSNico Weber //    The redirect jump is applicable when the first instruction is a direct
46ebbce04cSNico Weber //    jump. The instruction is replaced by jump to the hook.
47ebbce04cSNico Weber //
48ebbce04cSNico Weber //        func:  jmp <label>     -->     func:  jmp <hook>
49ebbce04cSNico Weber //
5004ccbe6eSHans Wennborg //    On a 64-bit architecture, a trampoline is inserted.
51ebbce04cSNico Weber //
52ebbce04cSNico Weber //        func:  jmp <label>     -->     func:  jmp <tramp>
53ebbce04cSNico Weber //                                              [...]
54ebbce04cSNico Weber //
55ebbce04cSNico Weber //                                   [trampoline]
56ebbce04cSNico Weber //                                      tramp:  jmp QWORD [addr]
57ebbce04cSNico Weber //                                       addr:  .bytes <hook>
58ebbce04cSNico Weber //
59a1e7e401SKazuaki Ishizaki //    Note: <real> is equivalent to <label>.
60ebbce04cSNico Weber //
61ebbce04cSNico Weber // 3) HotPatch
62ebbce04cSNico Weber //
6304ccbe6eSHans Wennborg //    The HotPatch hooking is assuming the presence of a header with padding
64ebbce04cSNico Weber //    and a first instruction with at least 2-bytes.
65ebbce04cSNico Weber //
66ebbce04cSNico Weber //    The reason to enforce the 2-bytes limitation is to provide the minimal
67ebbce04cSNico Weber //    space to encode a short jump. HotPatch technique is only rewriting one
68ebbce04cSNico Weber //    instruction to avoid breaking a sequence of instructions containing a
69ebbce04cSNico Weber //    branching target.
70ebbce04cSNico Weber //
71ebbce04cSNico Weber //    Assumptions are enforced by MSVC compiler by using the /HOTPATCH flag.
72ebbce04cSNico Weber //      see: https://msdn.microsoft.com/en-us/library/ms173507.aspx
73ebbce04cSNico Weber //    Default padding length is 5 bytes in 32-bits and 6 bytes in 64-bits.
74ebbce04cSNico Weber //
75ebbce04cSNico Weber //        head:   5 x nop                head:  jmp <hook>
76ebbce04cSNico Weber //        func:   <instr>        -->     func:  jmp short <head>
77ebbce04cSNico Weber //                [...]                  body:  [...]
78ebbce04cSNico Weber //
79ebbce04cSNico Weber //                                   [trampoline]
80ebbce04cSNico Weber //                                       real:  <instr>
81ebbce04cSNico Weber //                                              jmp <body>
82ebbce04cSNico Weber //
8304ccbe6eSHans Wennborg //    On a 64-bit architecture:
84ebbce04cSNico Weber //
85ebbce04cSNico Weber //        head:   6 x nop                head:  jmp QWORD [addr1]
86ebbce04cSNico Weber //        func:   <instr>        -->     func:  jmp short <head>
87ebbce04cSNico Weber //                [...]                  body:  [...]
88ebbce04cSNico Weber //
89ebbce04cSNico Weber //                                   [trampoline]
90ebbce04cSNico Weber //                                      addr1:  .bytes <hook>
91ebbce04cSNico Weber //                                       real:  <instr>
92ebbce04cSNico Weber //                                              jmp QWORD [addr2]
93ebbce04cSNico Weber //                                      addr2:  .bytes <body>
94ebbce04cSNico Weber //
95ebbce04cSNico Weber // 4) Trampoline
96ebbce04cSNico Weber //
97ebbce04cSNico Weber //    The Trampoline hooking technique is the most aggressive one. It is
98ebbce04cSNico Weber //    assuming that there is a sequence of instructions that can be safely
99ebbce04cSNico Weber //    replaced by a jump (enough room and no incoming branches).
100ebbce04cSNico Weber //
101ebbce04cSNico Weber //    Unfortunately, these assumptions can't be safely presumed and code may
102ebbce04cSNico Weber //    be broken after hooking.
103ebbce04cSNico Weber //
104ebbce04cSNico Weber //        func:   <instr>        -->     func:  jmp <hook>
105ebbce04cSNico Weber //                <instr>
106ebbce04cSNico Weber //                [...]                  body:  [...]
107ebbce04cSNico Weber //
108ebbce04cSNico Weber //                                   [trampoline]
109ebbce04cSNico Weber //                                       real:  <instr>
110ebbce04cSNico Weber //                                              <instr>
111ebbce04cSNico Weber //                                              jmp <body>
112ebbce04cSNico Weber //
11304ccbe6eSHans Wennborg //    On a 64-bit architecture:
114ebbce04cSNico Weber //
115ebbce04cSNico Weber //        func:   <instr>        -->     func:  jmp QWORD [addr1]
116ebbce04cSNico Weber //                <instr>
117ebbce04cSNico Weber //                [...]                  body:  [...]
118ebbce04cSNico Weber //
119ebbce04cSNico Weber //                                   [trampoline]
120ebbce04cSNico Weber //                                      addr1:  .bytes <hook>
121ebbce04cSNico Weber //                                       real:  <instr>
122ebbce04cSNico Weber //                                              <instr>
123ebbce04cSNico Weber //                                              jmp QWORD [addr2]
124ebbce04cSNico Weber //                                      addr2:  .bytes <body>
125ebbce04cSNico Weber //===----------------------------------------------------------------------===//
126ebbce04cSNico Weber 
127ebbce04cSNico Weber #include "interception.h"
128ebbce04cSNico Weber 
129ebbce04cSNico Weber #if SANITIZER_WINDOWS
130ebbce04cSNico Weber #include "sanitizer_common/sanitizer_platform.h"
131ebbce04cSNico Weber #define WIN32_LEAN_AND_MEAN
132ebbce04cSNico Weber #include <windows.h>
1333d2925b9SHans #include <psapi.h>
134ebbce04cSNico Weber 
135ebbce04cSNico Weber namespace __interception {
136ebbce04cSNico Weber 
137ebbce04cSNico Weber static const int kAddressLength = FIRST_32_SECOND_64(4, 8);
138ebbce04cSNico Weber static const int kJumpInstructionLength = 5;
139ebbce04cSNico Weber static const int kShortJumpInstructionLength = 2;
140979c38ccSMartin Storsjö UNUSED static const int kIndirectJumpInstructionLength = 6;
141ebbce04cSNico Weber static const int kBranchLength =
142ebbce04cSNico Weber     FIRST_32_SECOND_64(kJumpInstructionLength, kIndirectJumpInstructionLength);
143ebbce04cSNico Weber static const int kDirectBranchLength = kBranchLength + kAddressLength;
144ebbce04cSNico Weber 
1450716888dSAlvin Wong #  if defined(_MSC_VER)
1460716888dSAlvin Wong #    define INTERCEPTION_FORMAT(f, a)
1470716888dSAlvin Wong #  else
1480716888dSAlvin Wong #    define INTERCEPTION_FORMAT(f, a) __attribute__((format(printf, f, a)))
1490716888dSAlvin Wong #  endif
1500716888dSAlvin Wong 
1510716888dSAlvin Wong static void (*ErrorReportCallback)(const char *format, ...)
1520716888dSAlvin Wong     INTERCEPTION_FORMAT(1, 2);
1530716888dSAlvin Wong 
1540716888dSAlvin Wong void SetErrorReportCallback(void (*callback)(const char *format, ...)) {
1550716888dSAlvin Wong   ErrorReportCallback = callback;
1560716888dSAlvin Wong }
1570716888dSAlvin Wong 
1580716888dSAlvin Wong #  define ReportError(...)                \
1590716888dSAlvin Wong     do {                                  \
1600716888dSAlvin Wong       if (ErrorReportCallback)            \
1610716888dSAlvin Wong         ErrorReportCallback(__VA_ARGS__); \
1620716888dSAlvin Wong     } while (0)
1630716888dSAlvin Wong 
164ebbce04cSNico Weber static void InterceptionFailed() {
1650716888dSAlvin Wong   ReportError("interception_win: failed due to an unrecoverable error.\n");
1667b5571f3SAlvin Wong   // This acts like an abort when no debugger is attached. According to an old
1677b5571f3SAlvin Wong   // comment, calling abort() leads to an infinite recursion in CheckFailed.
168ebbce04cSNico Weber   __debugbreak();
169ebbce04cSNico Weber }
170ebbce04cSNico Weber 
171ebbce04cSNico Weber static bool DistanceIsWithin2Gig(uptr from, uptr target) {
172ebbce04cSNico Weber #if SANITIZER_WINDOWS64
173ebbce04cSNico Weber   if (from < target)
174ebbce04cSNico Weber     return target - from <= (uptr)0x7FFFFFFFU;
175ebbce04cSNico Weber   else
176ebbce04cSNico Weber     return from - target <= (uptr)0x80000000U;
177ebbce04cSNico Weber #else
178ebbce04cSNico Weber   // In a 32-bit address space, the address calculation will wrap, so this check
179ebbce04cSNico Weber   // is unnecessary.
180ebbce04cSNico Weber   return true;
181ebbce04cSNico Weber #endif
182ebbce04cSNico Weber }
183ebbce04cSNico Weber 
184ebbce04cSNico Weber static uptr GetMmapGranularity() {
185ebbce04cSNico Weber   SYSTEM_INFO si;
186ebbce04cSNico Weber   GetSystemInfo(&si);
187ebbce04cSNico Weber   return si.dwAllocationGranularity;
188ebbce04cSNico Weber }
189ebbce04cSNico Weber 
190cdfd4cffSHans UNUSED static uptr RoundDownTo(uptr size, uptr boundary) {
191cdfd4cffSHans   return size & ~(boundary - 1);
192cdfd4cffSHans }
193cdfd4cffSHans 
19446fe36a4SAlex Richardson UNUSED static uptr RoundUpTo(uptr size, uptr boundary) {
195cdfd4cffSHans   return RoundDownTo(size + boundary - 1, boundary);
19646fe36a4SAlex Richardson }
19746fe36a4SAlex Richardson 
19846fe36a4SAlex Richardson // FIXME: internal_str* and internal_mem* functions should be moved from the
19946fe36a4SAlex Richardson // ASan sources into interception/.
20046fe36a4SAlex Richardson 
20146fe36a4SAlex Richardson static size_t _strlen(const char *str) {
20246fe36a4SAlex Richardson   const char* p = str;
20346fe36a4SAlex Richardson   while (*p != '\0') ++p;
20446fe36a4SAlex Richardson   return p - str;
20546fe36a4SAlex Richardson }
20646fe36a4SAlex Richardson 
20746fe36a4SAlex Richardson static char* _strchr(char* str, char c) {
20846fe36a4SAlex Richardson   while (*str) {
20946fe36a4SAlex Richardson     if (*str == c)
21046fe36a4SAlex Richardson       return str;
21146fe36a4SAlex Richardson     ++str;
21246fe36a4SAlex Richardson   }
21346fe36a4SAlex Richardson   return nullptr;
21446fe36a4SAlex Richardson }
21546fe36a4SAlex Richardson 
21669ebac7aSHans Wennborg static int _strcmp(const char *s1, const char *s2) {
21769ebac7aSHans Wennborg   while (true) {
21869ebac7aSHans Wennborg     unsigned c1 = *s1;
21969ebac7aSHans Wennborg     unsigned c2 = *s2;
22069ebac7aSHans Wennborg     if (c1 != c2) return (c1 < c2) ? -1 : 1;
22169ebac7aSHans Wennborg     if (c1 == 0) break;
22269ebac7aSHans Wennborg     s1++;
22369ebac7aSHans Wennborg     s2++;
22469ebac7aSHans Wennborg   }
22569ebac7aSHans Wennborg   return 0;
22669ebac7aSHans Wennborg }
22769ebac7aSHans Wennborg 
22846fe36a4SAlex Richardson static void _memset(void *p, int value, size_t sz) {
22946fe36a4SAlex Richardson   for (size_t i = 0; i < sz; ++i)
23046fe36a4SAlex Richardson     ((char*)p)[i] = (char)value;
23146fe36a4SAlex Richardson }
23246fe36a4SAlex Richardson 
23346fe36a4SAlex Richardson static void _memcpy(void *dst, void *src, size_t sz) {
23446fe36a4SAlex Richardson   char *dst_c = (char*)dst,
23546fe36a4SAlex Richardson        *src_c = (char*)src;
23646fe36a4SAlex Richardson   for (size_t i = 0; i < sz; ++i)
23746fe36a4SAlex Richardson     dst_c[i] = src_c[i];
23846fe36a4SAlex Richardson }
23946fe36a4SAlex Richardson 
240ebbce04cSNico Weber static bool ChangeMemoryProtection(
241ebbce04cSNico Weber     uptr address, uptr size, DWORD *old_protection) {
242ebbce04cSNico Weber   return ::VirtualProtect((void*)address, size,
243ebbce04cSNico Weber                           PAGE_EXECUTE_READWRITE,
244ebbce04cSNico Weber                           old_protection) != FALSE;
245ebbce04cSNico Weber }
246ebbce04cSNico Weber 
247ebbce04cSNico Weber static bool RestoreMemoryProtection(
248ebbce04cSNico Weber     uptr address, uptr size, DWORD old_protection) {
249ebbce04cSNico Weber   DWORD unused;
250ebbce04cSNico Weber   return ::VirtualProtect((void*)address, size,
251ebbce04cSNico Weber                           old_protection,
252ebbce04cSNico Weber                           &unused) != FALSE;
253ebbce04cSNico Weber }
254ebbce04cSNico Weber 
255ebbce04cSNico Weber static bool IsMemoryPadding(uptr address, uptr size) {
256ebbce04cSNico Weber   u8* function = (u8*)address;
257ebbce04cSNico Weber   for (size_t i = 0; i < size; ++i)
258ebbce04cSNico Weber     if (function[i] != 0x90 && function[i] != 0xCC)
259ebbce04cSNico Weber       return false;
260ebbce04cSNico Weber   return true;
261ebbce04cSNico Weber }
262ebbce04cSNico Weber 
263ebbce04cSNico Weber static const u8 kHintNop8Bytes[] = {
264ebbce04cSNico Weber   0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00
265ebbce04cSNico Weber };
266ebbce04cSNico Weber 
267ebbce04cSNico Weber template<class T>
268ebbce04cSNico Weber static bool FunctionHasPrefix(uptr address, const T &pattern) {
269ebbce04cSNico Weber   u8* function = (u8*)address - sizeof(pattern);
270ebbce04cSNico Weber   for (size_t i = 0; i < sizeof(pattern); ++i)
271ebbce04cSNico Weber     if (function[i] != pattern[i])
272ebbce04cSNico Weber       return false;
273ebbce04cSNico Weber   return true;
274ebbce04cSNico Weber }
275ebbce04cSNico Weber 
276ebbce04cSNico Weber static bool FunctionHasPadding(uptr address, uptr size) {
277ebbce04cSNico Weber   if (IsMemoryPadding(address - size, size))
278ebbce04cSNico Weber     return true;
279ebbce04cSNico Weber   if (size <= sizeof(kHintNop8Bytes) &&
280ebbce04cSNico Weber       FunctionHasPrefix(address, kHintNop8Bytes))
281ebbce04cSNico Weber     return true;
282ebbce04cSNico Weber   return false;
283ebbce04cSNico Weber }
284ebbce04cSNico Weber 
285ebbce04cSNico Weber static void WritePadding(uptr from, uptr size) {
28646fe36a4SAlex Richardson   _memset((void*)from, 0xCC, (size_t)size);
287ebbce04cSNico Weber }
288ebbce04cSNico Weber 
289ebbce04cSNico Weber static void WriteJumpInstruction(uptr from, uptr target) {
2901a7a00bdSAlvin Wong   if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target)) {
2911a7a00bdSAlvin Wong     ReportError(
2921a7a00bdSAlvin Wong         "interception_win: cannot write jmp further than 2GB away, from %p to "
2931a7a00bdSAlvin Wong         "%p.\n",
2941a7a00bdSAlvin Wong         (void *)from, (void *)target);
295ebbce04cSNico Weber     InterceptionFailed();
2961a7a00bdSAlvin Wong   }
297ebbce04cSNico Weber   ptrdiff_t offset = target - from - kJumpInstructionLength;
298ebbce04cSNico Weber   *(u8*)from = 0xE9;
299ebbce04cSNico Weber   *(u32*)(from + 1) = offset;
300ebbce04cSNico Weber }
301ebbce04cSNico Weber 
302ebbce04cSNico Weber static void WriteShortJumpInstruction(uptr from, uptr target) {
303ebbce04cSNico Weber   sptr offset = target - from - kShortJumpInstructionLength;
304cdfd4cffSHans   if (offset < -128 || offset > 127) {
305cdfd4cffSHans     ReportError("interception_win: cannot write short jmp from %p to %p\n",
306cdfd4cffSHans                 (void *)from, (void *)target);
307ebbce04cSNico Weber     InterceptionFailed();
308cdfd4cffSHans   }
309ebbce04cSNico Weber   *(u8*)from = 0xEB;
310ebbce04cSNico Weber   *(u8*)(from + 1) = (u8)offset;
311ebbce04cSNico Weber }
312ebbce04cSNico Weber 
313ebbce04cSNico Weber #if SANITIZER_WINDOWS64
314ebbce04cSNico Weber static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) {
315ebbce04cSNico Weber   // jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative
316ebbce04cSNico Weber   // offset.
317ebbce04cSNico Weber   // The offset is the distance from then end of the jump instruction to the
318ebbce04cSNico Weber   // memory location containing the targeted address. The displacement is still
319ebbce04cSNico Weber   // 32-bit in x64, so indirect_target must be located within +/- 2GB range.
320ebbce04cSNico Weber   int offset = indirect_target - from - kIndirectJumpInstructionLength;
321ebbce04cSNico Weber   if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength,
322ebbce04cSNico Weber                             indirect_target)) {
3231a7a00bdSAlvin Wong     ReportError(
3241a7a00bdSAlvin Wong         "interception_win: cannot write indirect jmp with target further than "
3251a7a00bdSAlvin Wong         "2GB away, from %p to %p.\n",
3261a7a00bdSAlvin Wong         (void *)from, (void *)indirect_target);
327ebbce04cSNico Weber     InterceptionFailed();
328ebbce04cSNico Weber   }
329ebbce04cSNico Weber   *(u16*)from = 0x25FF;
330ebbce04cSNico Weber   *(u32*)(from + 2) = offset;
331ebbce04cSNico Weber }
332ebbce04cSNico Weber #endif
333ebbce04cSNico Weber 
334ebbce04cSNico Weber static void WriteBranch(
335ebbce04cSNico Weber     uptr from, uptr indirect_target, uptr target) {
336ebbce04cSNico Weber #if SANITIZER_WINDOWS64
337ebbce04cSNico Weber   WriteIndirectJumpInstruction(from, indirect_target);
338ebbce04cSNico Weber   *(u64*)indirect_target = target;
339ebbce04cSNico Weber #else
340ebbce04cSNico Weber   (void)indirect_target;
341ebbce04cSNico Weber   WriteJumpInstruction(from, target);
342ebbce04cSNico Weber #endif
343ebbce04cSNico Weber }
344ebbce04cSNico Weber 
345ebbce04cSNico Weber static void WriteDirectBranch(uptr from, uptr target) {
346ebbce04cSNico Weber #if SANITIZER_WINDOWS64
347ebbce04cSNico Weber   // Emit an indirect jump through immediately following bytes:
348ebbce04cSNico Weber   //   jmp [rip + kBranchLength]
349ebbce04cSNico Weber   //   .quad <target>
350ebbce04cSNico Weber   WriteBranch(from, from + kBranchLength, target);
351ebbce04cSNico Weber #else
352ebbce04cSNico Weber   WriteJumpInstruction(from, target);
353ebbce04cSNico Weber #endif
354ebbce04cSNico Weber }
355ebbce04cSNico Weber 
356ebbce04cSNico Weber struct TrampolineMemoryRegion {
357ebbce04cSNico Weber   uptr content;
358ebbce04cSNico Weber   uptr allocated_size;
359ebbce04cSNico Weber   uptr max_size;
360ebbce04cSNico Weber };
361ebbce04cSNico Weber 
362cdfd4cffSHans UNUSED static const uptr kTrampolineRangeLimit = 1ull << 31;  // 2 gig
363ebbce04cSNico Weber static const int kMaxTrampolineRegion = 1024;
364ebbce04cSNico Weber static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion];
365ebbce04cSNico Weber 
366cdfd4cffSHans static void *AllocateTrampolineRegion(uptr min_addr, uptr max_addr,
367cdfd4cffSHans                                       uptr func_addr, size_t granularity) {
368ebbce04cSNico Weber #  if SANITIZER_WINDOWS64
369cdfd4cffSHans   // Clamp {min,max}_addr to the accessible address space.
370cdfd4cffSHans   SYSTEM_INFO system_info;
371cdfd4cffSHans   ::GetSystemInfo(&system_info);
372cdfd4cffSHans   uptr min_virtual_addr =
373cdfd4cffSHans       RoundUpTo((uptr)system_info.lpMinimumApplicationAddress, granularity);
374cdfd4cffSHans   uptr max_virtual_addr =
375cdfd4cffSHans       RoundDownTo((uptr)system_info.lpMaximumApplicationAddress, granularity);
376cdfd4cffSHans   if (min_addr < min_virtual_addr)
377cdfd4cffSHans     min_addr = min_virtual_addr;
378cdfd4cffSHans   if (max_addr > max_virtual_addr)
379cdfd4cffSHans     max_addr = max_virtual_addr;
380ebbce04cSNico Weber 
381cdfd4cffSHans   // This loop probes the virtual address space to find free memory in the
382cdfd4cffSHans   // [min_addr, max_addr] interval. The search starts from func_addr and
383cdfd4cffSHans   // proceeds "outwards" towards the interval bounds using two probes, lo_addr
384cdfd4cffSHans   // and hi_addr, for addresses lower/higher than func_addr. At each step, it
385cdfd4cffSHans   // considers the probe closest to func_addr. If that address is not free, the
386cdfd4cffSHans   // probe is advanced (lower or higher depending on the probe) to the next
387cdfd4cffSHans   // memory block and the search continues.
388cdfd4cffSHans   uptr lo_addr = RoundDownTo(func_addr, granularity);
389cdfd4cffSHans   uptr hi_addr = RoundUpTo(func_addr, granularity);
390cdfd4cffSHans   while (lo_addr >= min_addr || hi_addr <= max_addr) {
391cdfd4cffSHans     // Consider the in-range address closest to func_addr.
392cdfd4cffSHans     uptr addr;
393cdfd4cffSHans     if (lo_addr < min_addr)
394cdfd4cffSHans       addr = hi_addr;
395cdfd4cffSHans     else if (hi_addr > max_addr)
396cdfd4cffSHans       addr = lo_addr;
397cdfd4cffSHans     else
398cdfd4cffSHans       addr = (hi_addr - func_addr < func_addr - lo_addr) ? hi_addr : lo_addr;
399cdfd4cffSHans 
400cdfd4cffSHans     MEMORY_BASIC_INFORMATION info;
401cdfd4cffSHans     if (!::VirtualQuery((void *)addr, &info, sizeof(info))) {
402cdfd4cffSHans       ReportError(
403cdfd4cffSHans           "interception_win: VirtualQuery in AllocateTrampolineRegion failed "
404cdfd4cffSHans           "for %p\n",
405cdfd4cffSHans           (void *)addr);
406cdfd4cffSHans       return nullptr;
407cdfd4cffSHans     }
408cdfd4cffSHans 
409cdfd4cffSHans     // Check whether a region can be allocated at |addr|.
410ebbce04cSNico Weber     if (info.State == MEM_FREE && info.RegionSize >= granularity) {
411cdfd4cffSHans       void *page =
412cdfd4cffSHans           ::VirtualAlloc((void *)addr, granularity, MEM_RESERVE | MEM_COMMIT,
413ebbce04cSNico Weber                          PAGE_EXECUTE_READWRITE);
414cdfd4cffSHans       if (page == nullptr)
415cdfd4cffSHans         ReportError(
416cdfd4cffSHans             "interception_win: VirtualAlloc in AllocateTrampolineRegion failed "
417cdfd4cffSHans             "for %p\n",
418cdfd4cffSHans             (void *)addr);
419ebbce04cSNico Weber       return page;
420ebbce04cSNico Weber     }
421ebbce04cSNico Weber 
422cdfd4cffSHans     if (addr == lo_addr)
423cdfd4cffSHans       lo_addr =
424cdfd4cffSHans           RoundDownTo((uptr)info.AllocationBase - granularity, granularity);
425cdfd4cffSHans     if (addr == hi_addr)
426cdfd4cffSHans       hi_addr =
427cdfd4cffSHans           RoundUpTo((uptr)info.BaseAddress + info.RegionSize, granularity);
428ebbce04cSNico Weber   }
429cdfd4cffSHans 
430cdfd4cffSHans   ReportError(
431cdfd4cffSHans       "interception_win: AllocateTrampolineRegion failed to find free memory; "
432cdfd4cffSHans       "min_addr: %p, max_addr: %p, func_addr: %p, granularity: %zu\n",
433055f1a77Sbernhardu       (void *)min_addr, (void *)max_addr, (void *)func_addr, granularity);
434ebbce04cSNico Weber   return nullptr;
435ebbce04cSNico Weber #else
436ebbce04cSNico Weber   return ::VirtualAlloc(nullptr,
437ebbce04cSNico Weber                         granularity,
438ebbce04cSNico Weber                         MEM_RESERVE | MEM_COMMIT,
439ebbce04cSNico Weber                         PAGE_EXECUTE_READWRITE);
440ebbce04cSNico Weber #endif
441ebbce04cSNico Weber }
442ebbce04cSNico Weber 
443ebbce04cSNico Weber // Used by unittests to release mapped memory space.
444ebbce04cSNico Weber void TestOnlyReleaseTrampolineRegions() {
445ebbce04cSNico Weber   for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
446ebbce04cSNico Weber     TrampolineMemoryRegion *current = &TrampolineRegions[bucket];
447ebbce04cSNico Weber     if (current->content == 0)
448ebbce04cSNico Weber       return;
449ebbce04cSNico Weber     ::VirtualFree((void*)current->content, 0, MEM_RELEASE);
450ebbce04cSNico Weber     current->content = 0;
451ebbce04cSNico Weber   }
452ebbce04cSNico Weber }
453ebbce04cSNico Weber 
4543d2925b9SHans static uptr AllocateMemoryForTrampoline(uptr func_address, size_t size) {
4553d2925b9SHans #  if SANITIZER_WINDOWS64
456cdfd4cffSHans   uptr min_addr = func_address - kTrampolineRangeLimit;
457cdfd4cffSHans   uptr max_addr = func_address + kTrampolineRangeLimit - size;
458cdfd4cffSHans 
459cdfd4cffSHans   // Allocate memory within 2GB of the module (DLL or EXE file) so that any
460cdfd4cffSHans   // address within the module can be referenced with PC-relative operands.
4613d2925b9SHans   // This allows us to not just jump to the trampoline with a PC-relative
4623d2925b9SHans   // offset, but to relocate any instructions that we copy to the trampoline
4633d2925b9SHans   // which have references to the original module. If we can't find the base
4643d2925b9SHans   // address of the module (e.g. if func_address is in mmap'ed memory), just
465cdfd4cffSHans   // stay within 2GB of func_address.
4663d2925b9SHans   HMODULE module;
4673d2925b9SHans   if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
4683d2925b9SHans                            GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
4693d2925b9SHans                            (LPCWSTR)func_address, &module)) {
4703d2925b9SHans     MODULEINFO module_info;
4713d2925b9SHans     if (::GetModuleInformation(::GetCurrentProcess(), module,
4723d2925b9SHans                                 &module_info, sizeof(module_info))) {
473cdfd4cffSHans       min_addr = (uptr)module_info.lpBaseOfDll + module_info.SizeOfImage -
474cdfd4cffSHans                  kTrampolineRangeLimit;
475cdfd4cffSHans       max_addr = (uptr)module_info.lpBaseOfDll + kTrampolineRangeLimit - size;
4763d2925b9SHans     }
4773d2925b9SHans   }
478cdfd4cffSHans 
479cdfd4cffSHans   // Check for overflow.
480cdfd4cffSHans   if (min_addr > func_address)
481cdfd4cffSHans     min_addr = 0;
482cdfd4cffSHans   if (max_addr < func_address)
483cdfd4cffSHans     max_addr = ~(uptr)0;
484cdfd4cffSHans #  else
485cdfd4cffSHans   uptr min_addr = 0;
486cdfd4cffSHans   uptr max_addr = ~min_addr;
4873d2925b9SHans #  endif
4883d2925b9SHans 
489cdfd4cffSHans   // Find a region within [min_addr,max_addr] with enough space to allocate
490cdfd4cffSHans   // |size| bytes.
491ebbce04cSNico Weber   TrampolineMemoryRegion *region = nullptr;
492ebbce04cSNico Weber   for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {
493ebbce04cSNico Weber     TrampolineMemoryRegion* current = &TrampolineRegions[bucket];
494ebbce04cSNico Weber     if (current->content == 0) {
495ebbce04cSNico Weber       // No valid region found, allocate a new region.
496ebbce04cSNico Weber       size_t bucket_size = GetMmapGranularity();
497cdfd4cffSHans       void *content = AllocateTrampolineRegion(min_addr, max_addr, func_address,
498cdfd4cffSHans                                                bucket_size);
499ebbce04cSNico Weber       if (content == nullptr)
500ebbce04cSNico Weber         return 0U;
501ebbce04cSNico Weber 
502ebbce04cSNico Weber       current->content = (uptr)content;
503ebbce04cSNico Weber       current->allocated_size = 0;
504ebbce04cSNico Weber       current->max_size = bucket_size;
505ebbce04cSNico Weber       region = current;
506ebbce04cSNico Weber       break;
507ebbce04cSNico Weber     } else if (current->max_size - current->allocated_size > size) {
508ebbce04cSNico Weber       uptr next_address = current->content + current->allocated_size;
509cdfd4cffSHans       if (next_address < min_addr || next_address > max_addr)
510ebbce04cSNico Weber         continue;
511ebbce04cSNico Weber       // The space can be allocated in the current region.
512ebbce04cSNico Weber       region = current;
513ebbce04cSNico Weber       break;
514ebbce04cSNico Weber     }
515ebbce04cSNico Weber   }
516ebbce04cSNico Weber 
517ebbce04cSNico Weber   // Failed to find a region.
518ebbce04cSNico Weber   if (region == nullptr)
519ebbce04cSNico Weber     return 0U;
520ebbce04cSNico Weber 
521ebbce04cSNico Weber   // Allocate the space in the current region.
522ebbce04cSNico Weber   uptr allocated_space = region->content + region->allocated_size;
523ebbce04cSNico Weber   region->allocated_size += size;
524ebbce04cSNico Weber   WritePadding(allocated_space, size);
525ebbce04cSNico Weber 
526ebbce04cSNico Weber   return allocated_space;
527ebbce04cSNico Weber }
528ebbce04cSNico Weber 
52922ea0ceaSToshihito Kikuchi // The following prologues cannot be patched because of the short jump
53022ea0ceaSToshihito Kikuchi // jumping to the patching region.
53122ea0ceaSToshihito Kikuchi 
532d79aee9fSFarzon Lotfi // Short jump patterns  below are only for x86_64.
533d79aee9fSFarzon Lotfi #  if SANITIZER_WINDOWS_x64
53422ea0ceaSToshihito Kikuchi // ntdll!wcslen in Win11
53522ea0ceaSToshihito Kikuchi //   488bc1          mov     rax,rcx
53622ea0ceaSToshihito Kikuchi //   0fb710          movzx   edx,word ptr [rax]
53722ea0ceaSToshihito Kikuchi //   4883c002        add     rax,2
53822ea0ceaSToshihito Kikuchi //   6685d2          test    dx,dx
53922ea0ceaSToshihito Kikuchi //   75f4            jne     -12
54022ea0ceaSToshihito Kikuchi static const u8 kPrologueWithShortJump1[] = {
54122ea0ceaSToshihito Kikuchi     0x48, 0x8b, 0xc1, 0x0f, 0xb7, 0x10, 0x48, 0x83,
54222ea0ceaSToshihito Kikuchi     0xc0, 0x02, 0x66, 0x85, 0xd2, 0x75, 0xf4,
54322ea0ceaSToshihito Kikuchi };
54422ea0ceaSToshihito Kikuchi 
54522ea0ceaSToshihito Kikuchi // ntdll!strrchr in Win11
54622ea0ceaSToshihito Kikuchi //   4c8bc1          mov     r8,rcx
54722ea0ceaSToshihito Kikuchi //   8a01            mov     al,byte ptr [rcx]
54822ea0ceaSToshihito Kikuchi //   48ffc1          inc     rcx
54922ea0ceaSToshihito Kikuchi //   84c0            test    al,al
55022ea0ceaSToshihito Kikuchi //   75f7            jne     -9
55122ea0ceaSToshihito Kikuchi static const u8 kPrologueWithShortJump2[] = {
55222ea0ceaSToshihito Kikuchi     0x4c, 0x8b, 0xc1, 0x8a, 0x01, 0x48, 0xff, 0xc1,
55322ea0ceaSToshihito Kikuchi     0x84, 0xc0, 0x75, 0xf7,
55422ea0ceaSToshihito Kikuchi };
555aa45fc41SMartin Storsjö #endif
55622ea0ceaSToshihito Kikuchi 
557ebbce04cSNico Weber // Returns 0 on error.
558ebbce04cSNico Weber static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {
55936b1811dSbernhardu   if (rel_offset) {
56036b1811dSbernhardu     *rel_offset = 0;
56136b1811dSbernhardu   }
56236b1811dSbernhardu 
5635a48a824SFarzon Lotfi #if SANITIZER_ARM64
5645a48a824SFarzon Lotfi   // An ARM64 instruction is 4 bytes long.
5655a48a824SFarzon Lotfi   return 4;
5665a48a824SFarzon Lotfi #endif
5675a48a824SFarzon Lotfi 
568d79aee9fSFarzon Lotfi #  if SANITIZER_WINDOWS_x64
56922ea0ceaSToshihito Kikuchi   if (memcmp((u8*)address, kPrologueWithShortJump1,
57022ea0ceaSToshihito Kikuchi              sizeof(kPrologueWithShortJump1)) == 0 ||
57122ea0ceaSToshihito Kikuchi       memcmp((u8*)address, kPrologueWithShortJump2,
57222ea0ceaSToshihito Kikuchi              sizeof(kPrologueWithShortJump2)) == 0) {
57322ea0ceaSToshihito Kikuchi     return 0;
57422ea0ceaSToshihito Kikuchi   }
57522ea0ceaSToshihito Kikuchi #endif
57622ea0ceaSToshihito Kikuchi 
577ebbce04cSNico Weber   switch (*(u64*)address) {
578ebbce04cSNico Weber     case 0x90909090909006EB:  // stub: jmp over 6 x nop.
579ebbce04cSNico Weber       return 8;
580ebbce04cSNico Weber   }
581ebbce04cSNico Weber 
582ebbce04cSNico Weber   switch (*(u8*)address) {
583ebbce04cSNico Weber     case 0x90:  // 90 : nop
58442383242SCharlie Barto     case 0xC3:  // C3 : ret   (for small/empty function interception
58542383242SCharlie Barto     case 0xCC:  // CC : int 3  i.e. registering weak functions)
586ebbce04cSNico Weber       return 1;
587ebbce04cSNico Weber 
588ebbce04cSNico Weber     case 0x50:  // push eax / rax
589ebbce04cSNico Weber     case 0x51:  // push ecx / rcx
590ebbce04cSNico Weber     case 0x52:  // push edx / rdx
591ebbce04cSNico Weber     case 0x53:  // push ebx / rbx
592ebbce04cSNico Weber     case 0x54:  // push esp / rsp
593ebbce04cSNico Weber     case 0x55:  // push ebp / rbp
594ebbce04cSNico Weber     case 0x56:  // push esi / rsi
595ebbce04cSNico Weber     case 0x57:  // push edi / rdi
596ebbce04cSNico Weber     case 0x5D:  // pop ebp / rbp
597ebbce04cSNico Weber       return 1;
598ebbce04cSNico Weber 
599ebbce04cSNico Weber     case 0x6A:  // 6A XX = push XX
600ebbce04cSNico Weber       return 2;
601ebbce04cSNico Weber 
6023bd8f4e0SCharlie Barto     // This instruction can be encoded with a 16-bit immediate but that is
6033bd8f4e0SCharlie Barto     // incredibly unlikely.
6043bd8f4e0SCharlie Barto     case 0x68:  // 68 XX XX XX XX : push imm32
6053bd8f4e0SCharlie Barto       return 5;
6063bd8f4e0SCharlie Barto 
607ebbce04cSNico Weber     case 0xb8:  // b8 XX XX XX XX : mov eax, XX XX XX XX
608ebbce04cSNico Weber     case 0xB9:  // b9 XX XX XX XX : mov ecx, XX XX XX XX
6095f405707Sbernhardu     case 0xBA:  // ba XX XX XX XX : mov edx, XX XX XX XX
610ebbce04cSNico Weber       return 5;
611ebbce04cSNico Weber 
612ebbce04cSNico Weber     // Cannot overwrite control-instruction. Return 0 to indicate failure.
613ebbce04cSNico Weber     case 0xE9:  // E9 XX XX XX XX : jmp <label>
614ebbce04cSNico Weber     case 0xE8:  // E8 XX XX XX XX : call <func>
615ebbce04cSNico Weber     case 0xEB:  // EB XX : jmp XX (short jump)
616ebbce04cSNico Weber     case 0x70:  // 7Y YY : jy XX (short conditional jump)
617ebbce04cSNico Weber     case 0x71:
618ebbce04cSNico Weber     case 0x72:
619ebbce04cSNico Weber     case 0x73:
620ebbce04cSNico Weber     case 0x74:
621ebbce04cSNico Weber     case 0x75:
622ebbce04cSNico Weber     case 0x76:
623ebbce04cSNico Weber     case 0x77:
624ebbce04cSNico Weber     case 0x78:
625ebbce04cSNico Weber     case 0x79:
626ebbce04cSNico Weber     case 0x7A:
627ebbce04cSNico Weber     case 0x7B:
628ebbce04cSNico Weber     case 0x7C:
629ebbce04cSNico Weber     case 0x7D:
630ebbce04cSNico Weber     case 0x7E:
631ebbce04cSNico Weber     case 0x7F:
632ebbce04cSNico Weber       return 0;
633ebbce04cSNico Weber   }
634ebbce04cSNico Weber 
635ebbce04cSNico Weber   switch (*(u16*)(address)) {
636ebbce04cSNico Weber     case 0x018A:  // 8A 01 : mov al, byte ptr [ecx]
637ebbce04cSNico Weber     case 0xFF8B:  // 8B FF : mov edi, edi
638ebbce04cSNico Weber     case 0xEC8B:  // 8B EC : mov ebp, esp
639ebbce04cSNico Weber     case 0xc889:  // 89 C8 : mov eax, ecx
64099612a3aSbernhardu     case 0xD189:  // 89 D1 : mov ecx, edx
641ca40985eSAlvin Wong     case 0xE589:  // 89 E5 : mov ebp, esp
642ebbce04cSNico Weber     case 0xC18B:  // 8B C1 : mov eax, ecx
64399612a3aSbernhardu     case 0xC031:  // 31 C0 : xor eax, eax
64499612a3aSbernhardu     case 0xC931:  // 31 C9 : xor ecx, ecx
64599612a3aSbernhardu     case 0xD231:  // 31 D2 : xor edx, edx
646ebbce04cSNico Weber     case 0xC033:  // 33 C0 : xor eax, eax
647ebbce04cSNico Weber     case 0xC933:  // 33 C9 : xor ecx, ecx
648ebbce04cSNico Weber     case 0xD233:  // 33 D2 : xor edx, edx
6493bd8f4e0SCharlie Barto     case 0xDB84:  // 84 DB : test bl,bl
65099612a3aSbernhardu     case 0xC084:  // 84 C0 : test al,al
6513bd8f4e0SCharlie Barto     case 0xC984:  // 84 C9 : test cl,cl
6523bd8f4e0SCharlie Barto     case 0xD284:  // 84 D2 : test dl,dl
653ebbce04cSNico Weber       return 2;
654ebbce04cSNico Weber 
65557466db7Sbernhardu     case 0x3980:  // 80 39 XX : cmp BYTE PTR [rcx], XX
65657466db7Sbernhardu     case 0x4D8B:  // 8B 4D XX : mov XX(%ebp), ecx
65757466db7Sbernhardu     case 0x558B:  // 8B 55 XX : mov XX(%ebp), edx
65857466db7Sbernhardu     case 0x758B:  // 8B 75 XX : mov XX(%ebp), esp
659f85579fbSbernhardu     case 0xE483:  // 83 E4 XX : and esp, XX
660854ea0cfSbernhardu     case 0xEC83:  // 83 EC XX : sub esp, XX
661bf6f1ca2Sbernhardu     case 0xC1F6:  // F6 C1 XX : test cl, XX
662bf6f1ca2Sbernhardu       return 3;
663bf6f1ca2Sbernhardu 
664351ee305Sbernhardu     case 0x89FF:  // FF 89 XX XX XX XX : dec dword ptr [ecx + XX XX XX XX]
665351ee305Sbernhardu     case 0xEC81:  // 81 EC XX XX XX XX : sub esp, XX XX XX XX
666351ee305Sbernhardu       return 6;
667351ee305Sbernhardu 
668ebbce04cSNico Weber     // Cannot overwrite control-instruction. Return 0 to indicate failure.
669a943922cSbernhardu     case 0x25FF:  // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX]
670ebbce04cSNico Weber       return 0;
671ebbce04cSNico Weber   }
672ebbce04cSNico Weber 
673ebbce04cSNico Weber   switch (0x00FFFFFF & *(u32 *)address) {
67456592a81Sbernhardu     case 0x244C8D:  // 8D 4C 24 XX : lea ecx, [esp + XX]
67556592a81Sbernhardu     case 0x2474FF:  // FF 74 24 XX : push qword ptr [rsp + XX]
67656592a81Sbernhardu       return 4;
677ebbce04cSNico Weber     case 0x24A48D:  // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX]
678ebbce04cSNico Weber       return 7;
679ebbce04cSNico Weber   }
680ebbce04cSNico Weber 
68142383242SCharlie Barto   switch (0x000000FF & *(u32 *)address) {
68242383242SCharlie Barto     case 0xc2:  // C2 XX XX : ret XX (needed for registering weak functions)
68342383242SCharlie Barto       return 3;
68442383242SCharlie Barto   }
68542383242SCharlie Barto 
686d79aee9fSFarzon Lotfi #  if SANITIZER_WINDOWS_x64
687ebbce04cSNico Weber   switch (*(u8*)address) {
688ebbce04cSNico Weber     case 0xA1:  // A1 XX XX XX XX XX XX XX XX :
689ebbce04cSNico Weber                 //   movabs eax, dword ptr ds:[XXXXXXXX]
690ebbce04cSNico Weber       return 9;
6913bd8f4e0SCharlie Barto     case 0xF2:
6923bd8f4e0SCharlie Barto       switch (*(u32 *)(address + 1)) {
6933bd8f4e0SCharlie Barto           case 0x2444110f:  //  f2 0f 11 44 24 XX       movsd  QWORD PTR
6943bd8f4e0SCharlie Barto                             //  [rsp + XX], xmm0
6953bd8f4e0SCharlie Barto           case 0x244c110f:  //  f2 0f 11 4c 24 XX       movsd  QWORD PTR
6963bd8f4e0SCharlie Barto                             //  [rsp + XX], xmm1
6973bd8f4e0SCharlie Barto           case 0x2454110f:  //  f2 0f 11 54 24 XX       movsd  QWORD PTR
6983bd8f4e0SCharlie Barto                             //  [rsp + XX], xmm2
6993bd8f4e0SCharlie Barto           case 0x245c110f:  //  f2 0f 11 5c 24 XX       movsd  QWORD PTR
7003bd8f4e0SCharlie Barto                             //  [rsp + XX], xmm3
7013bd8f4e0SCharlie Barto           case 0x2464110f:  //  f2 0f 11 64 24 XX       movsd  QWORD PTR
7023bd8f4e0SCharlie Barto                             //  [rsp + XX], xmm4
7033bd8f4e0SCharlie Barto             return 6;
7043bd8f4e0SCharlie Barto       }
7053bd8f4e0SCharlie Barto       break;
70622ea0ceaSToshihito Kikuchi 
70722ea0ceaSToshihito Kikuchi     case 0x83:
70822ea0ceaSToshihito Kikuchi       const u8 next_byte = *(u8*)(address + 1);
70922ea0ceaSToshihito Kikuchi       const u8 mod = next_byte >> 6;
71022ea0ceaSToshihito Kikuchi       const u8 rm = next_byte & 7;
71122ea0ceaSToshihito Kikuchi       if (mod == 1 && rm == 4)
71222ea0ceaSToshihito Kikuchi         return 5;  // 83 ModR/M SIB Disp8 Imm8
71322ea0ceaSToshihito Kikuchi                    //   add|or|adc|sbb|and|sub|xor|cmp [r+disp8], imm8
714ebbce04cSNico Weber   }
715ebbce04cSNico Weber 
716ebbce04cSNico Weber   switch (*(u16*)address) {
717ebbce04cSNico Weber     case 0x5040:  // push rax
718ebbce04cSNico Weber     case 0x5140:  // push rcx
719ebbce04cSNico Weber     case 0x5240:  // push rdx
720ebbce04cSNico Weber     case 0x5340:  // push rbx
721ebbce04cSNico Weber     case 0x5440:  // push rsp
722ebbce04cSNico Weber     case 0x5540:  // push rbp
723ebbce04cSNico Weber     case 0x5640:  // push rsi
724ebbce04cSNico Weber     case 0x5740:  // push rdi
725ebbce04cSNico Weber     case 0x5441:  // push r12
726ebbce04cSNico Weber     case 0x5541:  // push r13
727ebbce04cSNico Weber     case 0x5641:  // push r14
728ebbce04cSNico Weber     case 0x5741:  // push r15
729ebbce04cSNico Weber     case 0x9066:  // Two-byte NOP
73022ea0ceaSToshihito Kikuchi     case 0xc084:  // test al, al
73122ea0ceaSToshihito Kikuchi     case 0x018a:  // mov al, byte ptr [rcx]
732ebbce04cSNico Weber       return 2;
733ebbce04cSNico Weber 
7343bd8f4e0SCharlie Barto     case 0x7E80:  // 80 7E YY XX  cmp BYTE PTR [rsi+YY], XX
7353bd8f4e0SCharlie Barto     case 0x7D80:  // 80 7D YY XX  cmp BYTE PTR [rbp+YY], XX
7363bd8f4e0SCharlie Barto     case 0x7A80:  // 80 7A YY XX  cmp BYTE PTR [rdx+YY], XX
7373bd8f4e0SCharlie Barto     case 0x7880:  // 80 78 YY XX  cmp BYTE PTR [rax+YY], XX
7383bd8f4e0SCharlie Barto     case 0x7B80:  // 80 7B YY XX  cmp BYTE PTR [rbx+YY], XX
7393bd8f4e0SCharlie Barto     case 0x7980:  // 80 79 YY XX  cmp BYTE ptr [rcx+YY], XX
7403bd8f4e0SCharlie Barto       return 4;
7413bd8f4e0SCharlie Barto 
7426c52a18aSbernhardu     case 0x058A:  // 8A 05 XX XX XX XX : mov al, byte ptr [XX XX XX XX]
743ebbce04cSNico Weber     case 0x058B:  // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX]
744ebbce04cSNico Weber       if (rel_offset)
745ebbce04cSNico Weber         *rel_offset = 2;
746351ee305Sbernhardu     case 0xB841:  // 41 B8 XX XX XX XX : mov r8d, XX XX XX XX
747ebbce04cSNico Weber       return 6;
7483bd8f4e0SCharlie Barto 
7493bd8f4e0SCharlie Barto     case 0x7E81:  // 81 7E YY XX XX XX XX  cmp DWORD PTR [rsi+YY], XX XX XX XX
7503bd8f4e0SCharlie Barto     case 0x7D81:  // 81 7D YY XX XX XX XX  cmp DWORD PTR [rbp+YY], XX XX XX XX
7513bd8f4e0SCharlie Barto     case 0x7A81:  // 81 7A YY XX XX XX XX  cmp DWORD PTR [rdx+YY], XX XX XX XX
7523bd8f4e0SCharlie Barto     case 0x7881:  // 81 78 YY XX XX XX XX  cmp DWORD PTR [rax+YY], XX XX XX XX
7533bd8f4e0SCharlie Barto     case 0x7B81:  // 81 7B YY XX XX XX XX  cmp DWORD PTR [rbx+YY], XX XX XX XX
7543bd8f4e0SCharlie Barto     case 0x7981:  // 81 79 YY XX XX XX XX  cmp dword ptr [rcx+YY], XX XX XX XX
7553bd8f4e0SCharlie Barto       return 7;
756ebbce04cSNico Weber   }
757ebbce04cSNico Weber 
758ebbce04cSNico Weber   switch (0x00FFFFFF & *(u32 *)address) {
7593bd8f4e0SCharlie Barto     case 0x10b70f:    // 0f b7 10 : movzx edx, WORD PTR [rax]
7603bd8f4e0SCharlie Barto     case 0xc00b4d:    // 4d 0b c0 : or r8, r8
761ebbce04cSNico Weber     case 0xc03345:    // 45 33 c0 : xor r8d, r8d
7623bd8f4e0SCharlie Barto     case 0xc08548:    // 48 85 c0 : test rax, rax
7633bd8f4e0SCharlie Barto     case 0xc0854d:    // 4d 85 c0 : test r8, r8
7643bd8f4e0SCharlie Barto     case 0xc08b41:    // 41 8b c0 : mov eax, r8d
7653bd8f4e0SCharlie Barto     case 0xc0ff48:    // 48 ff c0 : inc rax
7663bd8f4e0SCharlie Barto     case 0xc0ff49:    // 49 ff c0 : inc r8
7673bd8f4e0SCharlie Barto     case 0xc18b41:    // 41 8b c1 : mov eax, r9d
7683bd8f4e0SCharlie Barto     case 0xc18b48:    // 48 8b c1 : mov rax, rcx
7693bd8f4e0SCharlie Barto     case 0xc18b4c:    // 4c 8b c1 : mov r8, rcx
7703bd8f4e0SCharlie Barto     case 0xc1ff48:    // 48 ff c1 : inc rcx
7713bd8f4e0SCharlie Barto     case 0xc1ff49:    // 49 ff c1 : inc r9
7723bd8f4e0SCharlie Barto     case 0xc28b41:    // 41 8b c2 : mov eax, r10d
77357466db7Sbernhardu     case 0x01b60f:    // 0f b6 01 : movzx eax, BYTE PTR [rcx]
77457466db7Sbernhardu     case 0x09b60f:    // 0f b6 09 : movzx ecx, BYTE PTR [rcx]
77557466db7Sbernhardu     case 0x11b60f:    // 0f b6 11 : movzx edx, BYTE PTR [rcx]
7763bd8f4e0SCharlie Barto     case 0xc2b60f:    // 0f b6 c2 : movzx eax, dl
7773bd8f4e0SCharlie Barto     case 0xc2ff48:    // 48 ff c2 : inc rdx
7783bd8f4e0SCharlie Barto     case 0xc2ff49:    // 49 ff c2 : inc r10
7793bd8f4e0SCharlie Barto     case 0xc38b41:    // 41 8b c3 : mov eax, r11d
7803bd8f4e0SCharlie Barto     case 0xc3ff48:    // 48 ff c3 : inc rbx
7813bd8f4e0SCharlie Barto     case 0xc3ff49:    // 49 ff c3 : inc r11
7823bd8f4e0SCharlie Barto     case 0xc48b41:    // 41 8b c4 : mov eax, r12d
7833bd8f4e0SCharlie Barto     case 0xc48b48:    // 48 8b c4 : mov rax, rsp
7843bd8f4e0SCharlie Barto     case 0xc4ff49:    // 49 ff c4 : inc r12
7853bd8f4e0SCharlie Barto     case 0xc5ff49:    // 49 ff c5 : inc r13
7863bd8f4e0SCharlie Barto     case 0xc6ff48:    // 48 ff c6 : inc rsi
7873bd8f4e0SCharlie Barto     case 0xc6ff49:    // 49 ff c6 : inc r14
7883bd8f4e0SCharlie Barto     case 0xc7ff48:    // 48 ff c7 : inc rdi
7893bd8f4e0SCharlie Barto     case 0xc7ff49:    // 49 ff c7 : inc r15
790ebbce04cSNico Weber     case 0xc93345:    // 45 33 c9 : xor r9d, r9d
7913bd8f4e0SCharlie Barto     case 0xc98548:    // 48 85 c9 : test rcx, rcx
7923bd8f4e0SCharlie Barto     case 0xc9854d:    // 4d 85 c9 : test r9, r9
7933bd8f4e0SCharlie Barto     case 0xc98b4c:    // 4c 8b c9 : mov r9, rcx
79457466db7Sbernhardu     case 0xd12948:    // 48 29 d1 : sub rcx, rdx
795ebbce04cSNico Weber     case 0xca2b48:    // 48 2b ca : sub rcx, rdx
79642383242SCharlie Barto     case 0xca3b48:    // 48 3b ca : cmp rcx, rdx
7973bd8f4e0SCharlie Barto     case 0xd12b48:    // 48 2b d1 : sub rdx, rcx
798ebbce04cSNico Weber     case 0xd18b48:    // 48 8b d1 : mov rdx, rcx
799ebbce04cSNico Weber     case 0xd18b4c:    // 4c 8b d1 : mov r10, rcx
8003bd8f4e0SCharlie Barto     case 0xd28548:    // 48 85 d2 : test rdx, rdx
8013bd8f4e0SCharlie Barto     case 0xd2854d:    // 4d 85 d2 : test r10, r10
8023bd8f4e0SCharlie Barto     case 0xd28b4c:    // 4c 8b d2 : mov r10, rdx
8033bd8f4e0SCharlie Barto     case 0xd2b60f:    // 0f b6 d2 : movzx edx, dl
80457466db7Sbernhardu     case 0xd2be0f:    // 0f be d2 : movsx edx, dl
8053bd8f4e0SCharlie Barto     case 0xd98b4c:    // 4c 8b d9 : mov r11, rcx
8063bd8f4e0SCharlie Barto     case 0xd9f748:    // 48 f7 d9 : neg rcx
80757466db7Sbernhardu     case 0xc03145:    // 45 31 c0 : xor r8d,r8d
80857466db7Sbernhardu     case 0xc93145:    // 45 31 c9 : xor r9d,r9d
8093bd8f4e0SCharlie Barto     case 0xdb3345:    // 45 33 db : xor r11d, r11d
81057466db7Sbernhardu     case 0xc08445:    // 45 84 c0 : test r8b,r8b
81157466db7Sbernhardu     case 0xd28445:    // 45 84 d2 : test r10b,r10b
8123bd8f4e0SCharlie Barto     case 0xdb8548:    // 48 85 db : test rbx, rbx
8133bd8f4e0SCharlie Barto     case 0xdb854d:    // 4d 85 db : test r11, r11
8143bd8f4e0SCharlie Barto     case 0xdc8b4c:    // 4c 8b dc : mov r11, rsp
8153bd8f4e0SCharlie Barto     case 0xe48548:    // 48 85 e4 : test rsp, rsp
8163bd8f4e0SCharlie Barto     case 0xe4854d:    // 4d 85 e4 : test r12, r12
81757466db7Sbernhardu     case 0xc88948:    // 48 89 c8 : mov rax,rcx
81857466db7Sbernhardu     case 0xcb8948:    // 48 89 cb : mov rbx,rcx
81957466db7Sbernhardu     case 0xd08948:    // 48 89 d0 : mov rax,rdx
82057466db7Sbernhardu     case 0xd18948:    // 48 89 d1 : mov rcx,rdx
82157466db7Sbernhardu     case 0xd38948:    // 48 89 d3 : mov rbx,rdx
8223bd8f4e0SCharlie Barto     case 0xe58948:    // 48 89 e5 : mov rbp, rsp
8233bd8f4e0SCharlie Barto     case 0xed8548:    // 48 85 ed : test rbp, rbp
82457466db7Sbernhardu     case 0xc88949:    // 49 89 c8 : mov r8, rcx
82557466db7Sbernhardu     case 0xc98949:    // 49 89 c9 : mov r9, rcx
82657466db7Sbernhardu     case 0xca8949:    // 49 89 ca : mov r10,rcx
82757466db7Sbernhardu     case 0xd08949:    // 49 89 d0 : mov r8, rdx
82857466db7Sbernhardu     case 0xd18949:    // 49 89 d1 : mov r9, rdx
82957466db7Sbernhardu     case 0xd28949:    // 49 89 d2 : mov r10, rdx
83057466db7Sbernhardu     case 0xd38949:    // 49 89 d3 : mov r11, rdx
8313bd8f4e0SCharlie Barto     case 0xed854d:    // 4d 85 ed : test r13, r13
8323bd8f4e0SCharlie Barto     case 0xf6854d:    // 4d 85 f6 : test r14, r14
8333bd8f4e0SCharlie Barto     case 0xff854d:    // 4d 85 ff : test r15, r15
834ebbce04cSNico Weber       return 3;
835ebbce04cSNico Weber 
8363bd8f4e0SCharlie Barto     case 0x245489:    // 89 54 24 XX : mov DWORD PTR[rsp + XX], edx
8373bd8f4e0SCharlie Barto     case 0x428d44:    // 44 8d 42 XX : lea r8d , [rdx + XX]
8383bd8f4e0SCharlie Barto     case 0x588948:    // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx
839ebbce04cSNico Weber     case 0xec8348:    // 48 83 ec XX : sub rsp, XX
840ebbce04cSNico Weber     case 0xf88349:    // 49 83 f8 XX : cmp r8, XX
84156592a81Sbernhardu     case 0x488d49:    // 49 8d 48 XX : lea rcx, [...]
84256592a81Sbernhardu     case 0x048d4c:    // 4c 8d 04 XX : lea r8, [...]
84356592a81Sbernhardu     case 0x148d4e:    // 4e 8d 14 XX : lea r10, [...]
84456592a81Sbernhardu     case 0x398366:    // 66 83 39 XX : cmp WORD PTR [rcx], XX
845ebbce04cSNico Weber       return 4;
846ebbce04cSNico Weber 
8475f405707Sbernhardu     case 0x441F0F:  // 0F 1F 44 XX XX :   nop DWORD PTR [...]
8483bd8f4e0SCharlie Barto     case 0x246483:  // 83 64 24 XX YY :   and    DWORD PTR [rsp+XX], YY
8493bd8f4e0SCharlie Barto       return 5;
8503bd8f4e0SCharlie Barto 
8513bd8f4e0SCharlie Barto     case 0x788166:  // 66 81 78 XX YY YY  cmp WORD PTR [rax+XX], YY YY
8523bd8f4e0SCharlie Barto     case 0x798166:  // 66 81 79 XX YY YY  cmp WORD PTR [rcx+XX], YY YY
8533bd8f4e0SCharlie Barto     case 0x7a8166:  // 66 81 7a XX YY YY  cmp WORD PTR [rdx+XX], YY YY
8543bd8f4e0SCharlie Barto     case 0x7b8166:  // 66 81 7b XX YY YY  cmp WORD PTR [rbx+XX], YY YY
8553bd8f4e0SCharlie Barto     case 0x7e8166:  // 66 81 7e XX YY YY  cmp WORD PTR [rsi+XX], YY YY
8563bd8f4e0SCharlie Barto     case 0x7f8166:  // 66 81 7f XX YY YY  cmp WORD PTR [rdi+XX], YY YY
8573bd8f4e0SCharlie Barto       return 6;
8583bd8f4e0SCharlie Barto 
859ebbce04cSNico Weber     case 0xec8148:    // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX
860*bbf37706Sbernhardu     case 0xc0c748:    // 48 C7 C0 XX XX XX XX : mov rax, XX XX XX XX
861ebbce04cSNico Weber       return 7;
862ebbce04cSNico Weber 
8633bd8f4e0SCharlie Barto     // clang-format off
8643bd8f4e0SCharlie Barto     case 0x788141:  // 41 81 78 XX YY YY YY YY : cmp DWORD PTR [r8+YY], XX XX XX XX
8653bd8f4e0SCharlie Barto     case 0x798141:  // 41 81 79 XX YY YY YY YY : cmp DWORD PTR [r9+YY], XX XX XX XX
8663bd8f4e0SCharlie Barto     case 0x7a8141:  // 41 81 7a XX YY YY YY YY : cmp DWORD PTR [r10+YY], XX XX XX XX
8673bd8f4e0SCharlie Barto     case 0x7b8141:  // 41 81 7b XX YY YY YY YY : cmp DWORD PTR [r11+YY], XX XX XX XX
8683bd8f4e0SCharlie Barto     case 0x7d8141:  // 41 81 7d XX YY YY YY YY : cmp DWORD PTR [r13+YY], XX XX XX XX
8693bd8f4e0SCharlie Barto     case 0x7e8141:  // 41 81 7e XX YY YY YY YY : cmp DWORD PTR [r14+YY], XX XX XX XX
8703bd8f4e0SCharlie Barto     case 0x7f8141:  // 41 81 7f YY XX XX XX XX : cmp DWORD PTR [r15+YY], XX XX XX XX
8713bd8f4e0SCharlie Barto     case 0x247c81:  // 81 7c 24 YY XX XX XX XX : cmp DWORD PTR [rsp+YY], XX XX XX XX
8723bd8f4e0SCharlie Barto       return 8;
8733bd8f4e0SCharlie Barto       // clang-format on
8743bd8f4e0SCharlie Barto 
875ebbce04cSNico Weber     case 0x058b48:    // 48 8b 05 XX XX XX XX :
876ebbce04cSNico Weber                       //   mov rax, QWORD PTR [rip + XXXXXXXX]
87742383242SCharlie Barto     case 0x058d48:    // 48 8d 05 XX XX XX XX :
87842383242SCharlie Barto                       //   lea rax, QWORD PTR [rip + XXXXXXXX]
87955f5d68cSHans     case 0x0d8948:    // 48 89 0d XX XX XX XX :
88055f5d68cSHans                       //   mov QWORD PTR [rip + XXXXXXXX], rcx
88155f5d68cSHans     case 0x158948:    // 48 89 15 XX XX XX XX :
88255f5d68cSHans                       //   mov QWORD PTR [rip + XXXXXXXX], rdx
883ebbce04cSNico Weber     case 0x25ff48:    // 48 ff 25 XX XX XX XX :
884ebbce04cSNico Weber                       //   rex.W jmp QWORD PTR [rip + XXXXXXXX]
885160e8eb4Snicole mazzuca     case 0x158D4C:    // 4c 8d 15 XX XX XX XX : lea r10, [rip + XX]
886ebbce04cSNico Weber       // Instructions having offset relative to 'rip' need offset adjustment.
887ebbce04cSNico Weber       if (rel_offset)
888ebbce04cSNico Weber         *rel_offset = 3;
889ebbce04cSNico Weber       return 7;
890ebbce04cSNico Weber 
891ebbce04cSNico Weber     case 0x2444c7:    // C7 44 24 XX YY YY YY YY
892ebbce04cSNico Weber                       //   mov dword ptr [rsp + XX], YYYYYYYY
893ebbce04cSNico Weber       return 8;
894213c90d3Sbernhardu 
895213c90d3Sbernhardu     case 0x7c8141:  // 41 81 7c ZZ YY XX XX XX XX
896213c90d3Sbernhardu                     // cmp DWORD PTR [reg+reg*n+YY], XX XX XX XX
897213c90d3Sbernhardu       return 9;
898ebbce04cSNico Weber   }
899ebbce04cSNico Weber 
900ebbce04cSNico Weber   switch (*(u32*)(address)) {
90156592a81Sbernhardu     case 0x01b60f44:  // 44 0f b6 01 : movzx r8d, BYTE PTR [rcx]
90256592a81Sbernhardu     case 0x09b60f44:  // 44 0f b6 09 : movzx r9d, BYTE PTR [rcx]
90356592a81Sbernhardu     case 0x0ab60f44:  // 44 0f b6 0a : movzx r8d, BYTE PTR [rdx]
90456592a81Sbernhardu     case 0x11b60f44:  // 44 0f b6 11 : movzx r10d, BYTE PTR [rcx]
905ce4618a9SMike Hommey     case 0x1ab60f44:  // 44 0f b6 1a : movzx r11d, BYTE PTR [rdx]
9068417f6afSHans Wennborg       return 4;
907ebbce04cSNico Weber     case 0x24448b48:  // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX]
908ebbce04cSNico Weber     case 0x246c8948:  // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp
909ebbce04cSNico Weber     case 0x245c8948:  // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx
910ebbce04cSNico Weber     case 0x24748948:  // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi
9117cd109b9SAlexandre Ganea     case 0x247c8948:  // 48 89 7c 24 XX : mov QWORD PTR [rsp + XX], rdi
912ebbce04cSNico Weber     case 0x244C8948:  // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx
913ebbce04cSNico Weber     case 0x24548948:  // 48 89 54 24 XX : mov QWORD PTR [rsp + XX], rdx
914ebbce04cSNico Weber     case 0x244c894c:  // 4c 89 4c 24 XX : mov QWORD PTR [rsp + XX], r9
915ebbce04cSNico Weber     case 0x2444894c:  // 4c 89 44 24 XX : mov QWORD PTR [rsp + XX], r8
9163bd8f4e0SCharlie Barto     case 0x244c8944:  // 44 89 4c 24 XX   mov DWORD PTR [rsp + XX], r9d
9173bd8f4e0SCharlie Barto     case 0x24448944:  // 44 89 44 24 XX   mov DWORD PTR [rsp + XX], r8d
9183bd8f4e0SCharlie Barto     case 0x246c8d48:  // 48 8d 6c 24 XX : lea rbp, [rsp + XX]
919ebbce04cSNico Weber       return 5;
9203bd8f4e0SCharlie Barto     case 0x24648348:  // 48 83 64 24 XX YY : and QWORD PTR [rsp + XX], YY
921ebbce04cSNico Weber       return 6;
922*bbf37706Sbernhardu     case 0x24A48D48:  // 48 8D A4 24 XX XX XX XX : lea rsp, [rsp + XX XX XX XX]
923*bbf37706Sbernhardu       return 8;
924ebbce04cSNico Weber   }
925ebbce04cSNico Weber 
9265f405707Sbernhardu   switch (0xFFFFFFFFFFULL & *(u64 *)(address)) {
9275f405707Sbernhardu     case 0xC07E0F4866:  // 66 48 0F 7E C0 : movq rax, xmm0
9285f405707Sbernhardu       return 5;
9295f405707Sbernhardu   }
9305f405707Sbernhardu 
931ebbce04cSNico Weber #else
932ebbce04cSNico Weber 
933ebbce04cSNico Weber   switch (*(u8*)address) {
934ebbce04cSNico Weber     case 0xA1:  // A1 XX XX XX XX :  mov eax, dword ptr ds:[XXXXXXXX]
935ebbce04cSNico Weber       return 5;
936ebbce04cSNico Weber   }
937ebbce04cSNico Weber   switch (*(u16*)address) {
938ebbce04cSNico Weber     case 0x458B:  // 8B 45 XX : mov eax, dword ptr [ebp + XX]
939ebbce04cSNico Weber     case 0x5D8B:  // 8B 5D XX : mov ebx, dword ptr [ebp + XX]
940ebbce04cSNico Weber     case 0x7D8B:  // 8B 7D XX : mov edi, dword ptr [ebp + XX]
9413bd8f4e0SCharlie Barto     case 0x758B:  // 8B 75 XX : mov esi, dword ptr [ebp + XX]
942ebbce04cSNico Weber     case 0x75FF:  // FF 75 XX : push dword ptr [ebp + XX]
943ebbce04cSNico Weber       return 3;
944ebbce04cSNico Weber     case 0xC1F7:  // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX
945ebbce04cSNico Weber       return 6;
946ebbce04cSNico Weber     case 0x3D83:  // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX
947ebbce04cSNico Weber       return 7;
948ebbce04cSNico Weber     case 0x7D83:  // 83 7D XX YY : cmp dword ptr [ebp + XX], YY
949ebbce04cSNico Weber       return 4;
950ebbce04cSNico Weber   }
951ebbce04cSNico Weber 
952ebbce04cSNico Weber   switch (0x00FFFFFF & *(u32*)address) {
953ebbce04cSNico Weber     case 0x24448A:  // 8A 44 24 XX : mov eal, dword ptr [esp + XX]
954ebbce04cSNico Weber     case 0x24448B:  // 8B 44 24 XX : mov eax, dword ptr [esp + XX]
955ebbce04cSNico Weber     case 0x244C8B:  // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX]
956ebbce04cSNico Weber     case 0x24548B:  // 8B 54 24 XX : mov edx, dword ptr [esp + XX]
957ca40985eSAlvin Wong     case 0x245C8B:  // 8B 5C 24 XX : mov ebx, dword ptr [esp + XX]
958ca40985eSAlvin Wong     case 0x246C8B:  // 8B 6C 24 XX : mov ebp, dword ptr [esp + XX]
959ebbce04cSNico Weber     case 0x24748B:  // 8B 74 24 XX : mov esi, dword ptr [esp + XX]
960ebbce04cSNico Weber     case 0x247C8B:  // 8B 7C 24 XX : mov edi, dword ptr [esp + XX]
961ebbce04cSNico Weber       return 4;
962ebbce04cSNico Weber   }
963ebbce04cSNico Weber 
964ebbce04cSNico Weber   switch (*(u32*)address) {
965ebbce04cSNico Weber     case 0x2444B60F:  // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX]
966ebbce04cSNico Weber       return 5;
967ebbce04cSNico Weber   }
968ebbce04cSNico Weber #endif
969ebbce04cSNico Weber 
9700716888dSAlvin Wong   // Unknown instruction! This might happen when we add a new interceptor, use
9710716888dSAlvin Wong   // a new compiler version, or if Windows changed how some functions are
9720716888dSAlvin Wong   // compiled. In either case, we print the address and 8 bytes of instructions
9730716888dSAlvin Wong   // to notify the user about the error and to help identify the unknown
9740716888dSAlvin Wong   // instruction. Don't treat this as a fatal error, though we can break the
9750716888dSAlvin Wong   // debugger if one has been attached.
9760716888dSAlvin Wong   u8 *bytes = (u8 *)address;
9770716888dSAlvin Wong   ReportError(
9780716888dSAlvin Wong       "interception_win: unhandled instruction at %p: %02x %02x %02x %02x %02x "
9790716888dSAlvin Wong       "%02x %02x %02x\n",
9800716888dSAlvin Wong       (void *)address, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
9810716888dSAlvin Wong       bytes[5], bytes[6], bytes[7]);
9827b5571f3SAlvin Wong   if (::IsDebuggerPresent())
9837b5571f3SAlvin Wong     __debugbreak();
984ebbce04cSNico Weber   return 0;
985ebbce04cSNico Weber }
986ebbce04cSNico Weber 
98736b1811dSbernhardu size_t TestOnlyGetInstructionSize(uptr address, size_t *rel_offset) {
98836b1811dSbernhardu   return GetInstructionSize(address, rel_offset);
98936b1811dSbernhardu }
99036b1811dSbernhardu 
991ebbce04cSNico Weber // Returns 0 on error.
992ebbce04cSNico Weber static size_t RoundUpToInstrBoundary(size_t size, uptr address) {
993ebbce04cSNico Weber   size_t cursor = 0;
994ebbce04cSNico Weber   while (cursor < size) {
995ebbce04cSNico Weber     size_t instruction_size = GetInstructionSize(address + cursor);
996ebbce04cSNico Weber     if (!instruction_size)
997ebbce04cSNico Weber       return 0;
998ebbce04cSNico Weber     cursor += instruction_size;
999ebbce04cSNico Weber   }
1000ebbce04cSNico Weber   return cursor;
1001ebbce04cSNico Weber }
1002ebbce04cSNico Weber 
1003ebbce04cSNico Weber static bool CopyInstructions(uptr to, uptr from, size_t size) {
1004ebbce04cSNico Weber   size_t cursor = 0;
1005ebbce04cSNico Weber   while (cursor != size) {
1006ebbce04cSNico Weber     size_t rel_offset = 0;
1007ebbce04cSNico Weber     size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset);
10087b5571f3SAlvin Wong     if (!instruction_size)
10097b5571f3SAlvin Wong       return false;
101046fe36a4SAlex Richardson     _memcpy((void *)(to + cursor), (void *)(from + cursor),
1011ebbce04cSNico Weber             (size_t)instruction_size);
1012ebbce04cSNico Weber     if (rel_offset) {
1013ebbce04cSNico Weber #  if SANITIZER_WINDOWS64
1014bc34a833Snicole mazzuca       // we want to make sure that the new relative offset still fits in 32-bits
1015bc34a833Snicole mazzuca       // this will be untrue if relocated_offset \notin [-2**31, 2**31)
1016bc34a833Snicole mazzuca       s64 delta = to - from;
1017bc34a833Snicole mazzuca       s64 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;
1018cdfd4cffSHans       if (-0x8000'0000ll > relocated_offset ||
1019cdfd4cffSHans           relocated_offset > 0x7FFF'FFFFll) {
1020cdfd4cffSHans         ReportError(
1021cdfd4cffSHans             "interception_win: CopyInstructions relocated_offset %lld outside "
1022cdfd4cffSHans             "32-bit range\n",
1023cdfd4cffSHans             (long long)relocated_offset);
1024ebbce04cSNico Weber         return false;
1025cdfd4cffSHans       }
1026bc34a833Snicole mazzuca #  else
1027bc34a833Snicole mazzuca       // on 32-bit, the relative offset will always be correct
1028bc34a833Snicole mazzuca       s32 delta = to - from;
1029bc34a833Snicole mazzuca       s32 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;
1030ebbce04cSNico Weber #  endif
1031bc34a833Snicole mazzuca       *(s32 *)(to + cursor + rel_offset) = relocated_offset;
1032ebbce04cSNico Weber     }
1033ebbce04cSNico Weber     cursor += instruction_size;
1034ebbce04cSNico Weber   }
1035ebbce04cSNico Weber   return true;
1036ebbce04cSNico Weber }
1037ebbce04cSNico Weber 
1038ebbce04cSNico Weber 
1039ebbce04cSNico Weber #if !SANITIZER_WINDOWS64
1040ebbce04cSNico Weber bool OverrideFunctionWithDetour(
1041ebbce04cSNico Weber     uptr old_func, uptr new_func, uptr *orig_old_func) {
1042ebbce04cSNico Weber   const int kDetourHeaderLen = 5;
1043ebbce04cSNico Weber   const u16 kDetourInstruction = 0xFF8B;
1044ebbce04cSNico Weber 
1045ebbce04cSNico Weber   uptr header = (uptr)old_func - kDetourHeaderLen;
1046ebbce04cSNico Weber   uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength;
1047ebbce04cSNico Weber 
1048ebbce04cSNico Weber   // Validate that the function is hookable.
1049ebbce04cSNico Weber   if (*(u16*)old_func != kDetourInstruction ||
1050ebbce04cSNico Weber       !IsMemoryPadding(header, kDetourHeaderLen))
1051ebbce04cSNico Weber     return false;
1052ebbce04cSNico Weber 
1053ebbce04cSNico Weber   // Change memory protection to writable.
1054ebbce04cSNico Weber   DWORD protection = 0;
1055ebbce04cSNico Weber   if (!ChangeMemoryProtection(header, patch_length, &protection))
1056ebbce04cSNico Weber     return false;
1057ebbce04cSNico Weber 
1058ebbce04cSNico Weber   // Write a relative jump to the redirected function.
1059ebbce04cSNico Weber   WriteJumpInstruction(header, new_func);
1060ebbce04cSNico Weber 
1061ebbce04cSNico Weber   // Write the short jump to the function prefix.
1062ebbce04cSNico Weber   WriteShortJumpInstruction(old_func, header);
1063ebbce04cSNico Weber 
1064ebbce04cSNico Weber   // Restore previous memory protection.
1065ebbce04cSNico Weber   if (!RestoreMemoryProtection(header, patch_length, protection))
1066ebbce04cSNico Weber     return false;
1067ebbce04cSNico Weber 
1068ebbce04cSNico Weber   if (orig_old_func)
1069ebbce04cSNico Weber     *orig_old_func = old_func + kShortJumpInstructionLength;
1070ebbce04cSNico Weber 
1071ebbce04cSNico Weber   return true;
1072ebbce04cSNico Weber }
1073ebbce04cSNico Weber #endif
1074ebbce04cSNico Weber 
1075ebbce04cSNico Weber bool OverrideFunctionWithRedirectJump(
1076ebbce04cSNico Weber     uptr old_func, uptr new_func, uptr *orig_old_func) {
1077ebbce04cSNico Weber   // Check whether the first instruction is a relative jump.
1078ebbce04cSNico Weber   if (*(u8*)old_func != 0xE9)
1079ebbce04cSNico Weber     return false;
1080ebbce04cSNico Weber 
1081ebbce04cSNico Weber   if (orig_old_func) {
108278c033b5SMarkus Böck     sptr relative_offset = *(s32 *)(old_func + 1);
1083ebbce04cSNico Weber     uptr absolute_target = old_func + relative_offset + kJumpInstructionLength;
1084ebbce04cSNico Weber     *orig_old_func = absolute_target;
1085ebbce04cSNico Weber   }
1086ebbce04cSNico Weber 
1087ebbce04cSNico Weber #if SANITIZER_WINDOWS64
1088ebbce04cSNico Weber   // If needed, get memory space for a trampoline jump.
1089ebbce04cSNico Weber   uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength);
1090ebbce04cSNico Weber   if (!trampoline)
1091ebbce04cSNico Weber     return false;
1092ebbce04cSNico Weber   WriteDirectBranch(trampoline, new_func);
1093ebbce04cSNico Weber #endif
1094ebbce04cSNico Weber 
1095ebbce04cSNico Weber   // Change memory protection to writable.
1096ebbce04cSNico Weber   DWORD protection = 0;
1097ebbce04cSNico Weber   if (!ChangeMemoryProtection(old_func, kJumpInstructionLength, &protection))
1098ebbce04cSNico Weber     return false;
1099ebbce04cSNico Weber 
1100ebbce04cSNico Weber   // Write a relative jump to the redirected function.
1101ebbce04cSNico Weber   WriteJumpInstruction(old_func, FIRST_32_SECOND_64(new_func, trampoline));
1102ebbce04cSNico Weber 
1103ebbce04cSNico Weber   // Restore previous memory protection.
1104ebbce04cSNico Weber   if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection))
1105ebbce04cSNico Weber     return false;
1106ebbce04cSNico Weber 
1107ebbce04cSNico Weber   return true;
1108ebbce04cSNico Weber }
1109ebbce04cSNico Weber 
1110ebbce04cSNico Weber bool OverrideFunctionWithHotPatch(
1111ebbce04cSNico Weber     uptr old_func, uptr new_func, uptr *orig_old_func) {
1112ebbce04cSNico Weber   const int kHotPatchHeaderLen = kBranchLength;
1113ebbce04cSNico Weber 
1114ebbce04cSNico Weber   uptr header = (uptr)old_func - kHotPatchHeaderLen;
1115ebbce04cSNico Weber   uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength;
1116ebbce04cSNico Weber 
1117ebbce04cSNico Weber   // Validate that the function is hot patchable.
1118ebbce04cSNico Weber   size_t instruction_size = GetInstructionSize(old_func);
1119ebbce04cSNico Weber   if (instruction_size < kShortJumpInstructionLength ||
1120ebbce04cSNico Weber       !FunctionHasPadding(old_func, kHotPatchHeaderLen))
1121ebbce04cSNico Weber     return false;
1122ebbce04cSNico Weber 
1123ebbce04cSNico Weber   if (orig_old_func) {
1124ebbce04cSNico Weber     // Put the needed instructions into the trampoline bytes.
1125ebbce04cSNico Weber     uptr trampoline_length = instruction_size + kDirectBranchLength;
1126ebbce04cSNico Weber     uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
1127ebbce04cSNico Weber     if (!trampoline)
1128ebbce04cSNico Weber       return false;
1129ebbce04cSNico Weber     if (!CopyInstructions(trampoline, old_func, instruction_size))
1130ebbce04cSNico Weber       return false;
1131ebbce04cSNico Weber     WriteDirectBranch(trampoline + instruction_size,
1132ebbce04cSNico Weber                       old_func + instruction_size);
1133ebbce04cSNico Weber     *orig_old_func = trampoline;
1134ebbce04cSNico Weber   }
1135ebbce04cSNico Weber 
1136ebbce04cSNico Weber   // If needed, get memory space for indirect address.
1137ebbce04cSNico Weber   uptr indirect_address = 0;
1138ebbce04cSNico Weber #if SANITIZER_WINDOWS64
1139ebbce04cSNico Weber   indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
1140ebbce04cSNico Weber   if (!indirect_address)
1141ebbce04cSNico Weber     return false;
1142ebbce04cSNico Weber #endif
1143ebbce04cSNico Weber 
1144ebbce04cSNico Weber   // Change memory protection to writable.
1145ebbce04cSNico Weber   DWORD protection = 0;
1146ebbce04cSNico Weber   if (!ChangeMemoryProtection(header, patch_length, &protection))
1147ebbce04cSNico Weber     return false;
1148ebbce04cSNico Weber 
1149ebbce04cSNico Weber   // Write jumps to the redirected function.
1150ebbce04cSNico Weber   WriteBranch(header, indirect_address, new_func);
1151ebbce04cSNico Weber   WriteShortJumpInstruction(old_func, header);
1152ebbce04cSNico Weber 
1153ebbce04cSNico Weber   // Restore previous memory protection.
1154ebbce04cSNico Weber   if (!RestoreMemoryProtection(header, patch_length, protection))
1155ebbce04cSNico Weber     return false;
1156ebbce04cSNico Weber 
1157ebbce04cSNico Weber   return true;
1158ebbce04cSNico Weber }
1159ebbce04cSNico Weber 
1160ebbce04cSNico Weber bool OverrideFunctionWithTrampoline(
1161ebbce04cSNico Weber     uptr old_func, uptr new_func, uptr *orig_old_func) {
1162ebbce04cSNico Weber 
1163ebbce04cSNico Weber   size_t instructions_length = kBranchLength;
1164ebbce04cSNico Weber   size_t padding_length = 0;
1165ebbce04cSNico Weber   uptr indirect_address = 0;
1166ebbce04cSNico Weber 
1167ebbce04cSNico Weber   if (orig_old_func) {
1168ebbce04cSNico Weber     // Find out the number of bytes of the instructions we need to copy
1169ebbce04cSNico Weber     // to the trampoline.
1170ebbce04cSNico Weber     instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func);
1171ebbce04cSNico Weber     if (!instructions_length)
1172ebbce04cSNico Weber       return false;
1173ebbce04cSNico Weber 
1174ebbce04cSNico Weber     // Put the needed instructions into the trampoline bytes.
1175ebbce04cSNico Weber     uptr trampoline_length = instructions_length + kDirectBranchLength;
1176ebbce04cSNico Weber     uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);
1177ebbce04cSNico Weber     if (!trampoline)
1178ebbce04cSNico Weber       return false;
1179ebbce04cSNico Weber     if (!CopyInstructions(trampoline, old_func, instructions_length))
1180ebbce04cSNico Weber       return false;
1181ebbce04cSNico Weber     WriteDirectBranch(trampoline + instructions_length,
1182ebbce04cSNico Weber                       old_func + instructions_length);
1183ebbce04cSNico Weber     *orig_old_func = trampoline;
1184ebbce04cSNico Weber   }
1185ebbce04cSNico Weber 
1186ebbce04cSNico Weber #if SANITIZER_WINDOWS64
1187ebbce04cSNico Weber   // Check if the targeted address can be encoded in the function padding.
1188ebbce04cSNico Weber   // Otherwise, allocate it in the trampoline region.
1189ebbce04cSNico Weber   if (IsMemoryPadding(old_func - kAddressLength, kAddressLength)) {
1190ebbce04cSNico Weber     indirect_address = old_func - kAddressLength;
1191ebbce04cSNico Weber     padding_length = kAddressLength;
1192ebbce04cSNico Weber   } else {
1193ebbce04cSNico Weber     indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);
1194ebbce04cSNico Weber     if (!indirect_address)
1195ebbce04cSNico Weber       return false;
1196ebbce04cSNico Weber   }
1197ebbce04cSNico Weber #endif
1198ebbce04cSNico Weber 
1199ebbce04cSNico Weber   // Change memory protection to writable.
1200ebbce04cSNico Weber   uptr patch_address = old_func - padding_length;
1201ebbce04cSNico Weber   uptr patch_length = instructions_length + padding_length;
1202ebbce04cSNico Weber   DWORD protection = 0;
1203ebbce04cSNico Weber   if (!ChangeMemoryProtection(patch_address, patch_length, &protection))
1204ebbce04cSNico Weber     return false;
1205ebbce04cSNico Weber 
1206ebbce04cSNico Weber   // Patch the original function.
1207ebbce04cSNico Weber   WriteBranch(old_func, indirect_address, new_func);
1208ebbce04cSNico Weber 
1209ebbce04cSNico Weber   // Restore previous memory protection.
1210ebbce04cSNico Weber   if (!RestoreMemoryProtection(patch_address, patch_length, protection))
1211ebbce04cSNico Weber     return false;
1212ebbce04cSNico Weber 
1213ebbce04cSNico Weber   return true;
1214ebbce04cSNico Weber }
1215ebbce04cSNico Weber 
1216ebbce04cSNico Weber bool OverrideFunction(
1217ebbce04cSNico Weber     uptr old_func, uptr new_func, uptr *orig_old_func) {
1218ebbce04cSNico Weber #if !SANITIZER_WINDOWS64
1219ebbce04cSNico Weber   if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func))
1220ebbce04cSNico Weber     return true;
1221ebbce04cSNico Weber #endif
1222ebbce04cSNico Weber   if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func))
1223ebbce04cSNico Weber     return true;
1224ebbce04cSNico Weber   if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func))
1225ebbce04cSNico Weber     return true;
1226ebbce04cSNico Weber   if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func))
1227ebbce04cSNico Weber     return true;
1228ebbce04cSNico Weber   return false;
1229ebbce04cSNico Weber }
1230ebbce04cSNico Weber 
1231ebbce04cSNico Weber static void **InterestingDLLsAvailable() {
1232ebbce04cSNico Weber   static const char *InterestingDLLs[] = {
1233ebbce04cSNico Weber     "kernel32.dll",
123441b09bbeSCharlie Barto     "msvcr100d.dll",      // VS2010
123541b09bbeSCharlie Barto     "msvcr110d.dll",      // VS2012
123641b09bbeSCharlie Barto     "msvcr120d.dll",      // VS2013
123741b09bbeSCharlie Barto     "vcruntime140d.dll",  // VS2015
123841b09bbeSCharlie Barto     "ucrtbased.dll",      // Universal CRT
1239ebbce04cSNico Weber     "msvcr100.dll",       // VS2010
1240ebbce04cSNico Weber     "msvcr110.dll",       // VS2012
1241ebbce04cSNico Weber     "msvcr120.dll",       // VS2013
1242ebbce04cSNico Weber     "vcruntime140.dll",   // VS2015
1243ebbce04cSNico Weber     "ucrtbase.dll",       // Universal CRT
1244814a75dcSAlvin Wong #  if (defined(__MINGW32__) && defined(__i386__))
1245814a75dcSAlvin Wong     "libc++.dll",     // libc++
1246814a75dcSAlvin Wong     "libunwind.dll",  // libunwind
1247814a75dcSAlvin Wong #  endif
124869ebac7aSHans Wennborg     // NTDLL must go last as it gets special treatment in OverrideFunction.
124941b09bbeSCharlie Barto     "ntdll.dll",
125041b09bbeSCharlie Barto     NULL
125141b09bbeSCharlie Barto   };
1252ebbce04cSNico Weber   static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };
1253ebbce04cSNico Weber   if (!result[0]) {
1254ebbce04cSNico Weber     for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {
1255ebbce04cSNico Weber       if (HMODULE h = GetModuleHandleA(InterestingDLLs[i]))
1256ebbce04cSNico Weber         result[j++] = (void *)h;
1257ebbce04cSNico Weber     }
1258ebbce04cSNico Weber   }
1259ebbce04cSNico Weber   return &result[0];
1260ebbce04cSNico Weber }
1261ebbce04cSNico Weber 
1262ebbce04cSNico Weber namespace {
1263ebbce04cSNico Weber // Utility for reading loaded PE images.
1264ebbce04cSNico Weber template <typename T> class RVAPtr {
1265ebbce04cSNico Weber  public:
1266ebbce04cSNico Weber   RVAPtr(void *module, uptr rva)
1267ebbce04cSNico Weber       : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {}
1268ebbce04cSNico Weber   operator T *() { return ptr_; }
1269ebbce04cSNico Weber   T *operator->() { return ptr_; }
1270ebbce04cSNico Weber   T *operator++() { return ++ptr_; }
1271ebbce04cSNico Weber 
1272ebbce04cSNico Weber  private:
1273ebbce04cSNico Weber   T *ptr_;
1274ebbce04cSNico Weber };
1275ebbce04cSNico Weber } // namespace
1276ebbce04cSNico Weber 
1277ebbce04cSNico Weber // Internal implementation of GetProcAddress. At least since Windows 8,
1278ebbce04cSNico Weber // GetProcAddress appears to initialize DLLs before returning function pointers
1279ebbce04cSNico Weber // into them. This is problematic for the sanitizers, because they typically
1280ebbce04cSNico Weber // want to intercept malloc *before* MSVCRT initializes. Our internal
1281ebbce04cSNico Weber // implementation walks the export list manually without doing initialization.
1282ebbce04cSNico Weber uptr InternalGetProcAddress(void *module, const char *func_name) {
1283ebbce04cSNico Weber   // Check that the module header is full and present.
1284ebbce04cSNico Weber   RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
1285ebbce04cSNico Weber   RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
1286ebbce04cSNico Weber   if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE ||  // "MZ"
1287ebbce04cSNico Weber       headers->Signature != IMAGE_NT_SIGNATURE ||             // "PE\0\0"
1288ebbce04cSNico Weber       headers->FileHeader.SizeOfOptionalHeader <
1289ebbce04cSNico Weber           sizeof(IMAGE_OPTIONAL_HEADER)) {
1290ebbce04cSNico Weber     return 0;
1291ebbce04cSNico Weber   }
1292ebbce04cSNico Weber 
1293ebbce04cSNico Weber   IMAGE_DATA_DIRECTORY *export_directory =
1294ebbce04cSNico Weber       &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
1295ebbce04cSNico Weber   if (export_directory->Size == 0)
1296ebbce04cSNico Weber     return 0;
1297ebbce04cSNico Weber   RVAPtr<IMAGE_EXPORT_DIRECTORY> exports(module,
1298ebbce04cSNico Weber                                          export_directory->VirtualAddress);
1299ebbce04cSNico Weber   RVAPtr<DWORD> functions(module, exports->AddressOfFunctions);
1300ebbce04cSNico Weber   RVAPtr<DWORD> names(module, exports->AddressOfNames);
1301ebbce04cSNico Weber   RVAPtr<WORD> ordinals(module, exports->AddressOfNameOrdinals);
1302ebbce04cSNico Weber 
1303ebbce04cSNico Weber   for (DWORD i = 0; i < exports->NumberOfNames; i++) {
1304ebbce04cSNico Weber     RVAPtr<char> name(module, names[i]);
130569ebac7aSHans Wennborg     if (!_strcmp(func_name, name)) {
1306ebbce04cSNico Weber       DWORD index = ordinals[i];
1307ebbce04cSNico Weber       RVAPtr<char> func(module, functions[index]);
1308ebbce04cSNico Weber 
1309ebbce04cSNico Weber       // Handle forwarded functions.
1310ebbce04cSNico Weber       DWORD offset = functions[index];
1311ebbce04cSNico Weber       if (offset >= export_directory->VirtualAddress &&
1312ebbce04cSNico Weber           offset < export_directory->VirtualAddress + export_directory->Size) {
1313ebbce04cSNico Weber         // An entry for a forwarded function is a string with the following
1314ebbce04cSNico Weber         // format: "<module> . <function_name>" that is stored into the
1315ebbce04cSNico Weber         // exported directory.
1316ebbce04cSNico Weber         char function_name[256];
131746fe36a4SAlex Richardson         size_t funtion_name_length = _strlen(func);
1318cdfd4cffSHans         if (funtion_name_length >= sizeof(function_name) - 1) {
1319055f1a77Sbernhardu           ReportError("interception_win: func too long: '%s'\n", (char *)func);
1320ebbce04cSNico Weber           InterceptionFailed();
1321cdfd4cffSHans         }
1322ebbce04cSNico Weber 
132346fe36a4SAlex Richardson         _memcpy(function_name, func, funtion_name_length);
1324ebbce04cSNico Weber         function_name[funtion_name_length] = '\0';
132546fe36a4SAlex Richardson         char* separator = _strchr(function_name, '.');
1326cdfd4cffSHans         if (!separator) {
1327cdfd4cffSHans           ReportError("interception_win: no separator in '%s'\n",
1328cdfd4cffSHans                       function_name);
1329ebbce04cSNico Weber           InterceptionFailed();
1330cdfd4cffSHans         }
1331ebbce04cSNico Weber         *separator = '\0';
1332ebbce04cSNico Weber 
1333ebbce04cSNico Weber         void* redirected_module = GetModuleHandleA(function_name);
1334cdfd4cffSHans         if (!redirected_module) {
1335cdfd4cffSHans           ReportError("interception_win: GetModuleHandleA failed for '%s'\n",
1336cdfd4cffSHans                       function_name);
1337ebbce04cSNico Weber           InterceptionFailed();
1338cdfd4cffSHans         }
1339ebbce04cSNico Weber         return InternalGetProcAddress(redirected_module, separator + 1);
1340ebbce04cSNico Weber       }
1341ebbce04cSNico Weber 
1342ebbce04cSNico Weber       return (uptr)(char *)func;
1343ebbce04cSNico Weber     }
1344ebbce04cSNico Weber   }
1345ebbce04cSNico Weber 
1346ebbce04cSNico Weber   return 0;
1347ebbce04cSNico Weber }
1348ebbce04cSNico Weber 
1349ebbce04cSNico Weber bool OverrideFunction(
1350ebbce04cSNico Weber     const char *func_name, uptr new_func, uptr *orig_old_func) {
135169ebac7aSHans Wennborg   static const char *kNtDllIgnore[] = {
135269ebac7aSHans Wennborg     "memcmp", "memcpy", "memmove", "memset"
135369ebac7aSHans Wennborg   };
135469ebac7aSHans Wennborg 
1355ebbce04cSNico Weber   bool hooked = false;
1356ebbce04cSNico Weber   void **DLLs = InterestingDLLsAvailable();
1357ebbce04cSNico Weber   for (size_t i = 0; DLLs[i]; ++i) {
135869ebac7aSHans Wennborg     if (DLLs[i + 1] == nullptr) {
135969ebac7aSHans Wennborg       // This is the last DLL, i.e. NTDLL. It exports some functions that
136069ebac7aSHans Wennborg       // we only want to override in the CRT.
136169ebac7aSHans Wennborg       for (const char *ignored : kNtDllIgnore) {
136269ebac7aSHans Wennborg         if (_strcmp(func_name, ignored) == 0)
136369ebac7aSHans Wennborg           return hooked;
136469ebac7aSHans Wennborg       }
136569ebac7aSHans Wennborg     }
136669ebac7aSHans Wennborg 
1367ebbce04cSNico Weber     uptr func_addr = InternalGetProcAddress(DLLs[i], func_name);
1368ebbce04cSNico Weber     if (func_addr &&
1369ebbce04cSNico Weber         OverrideFunction(func_addr, new_func, orig_old_func)) {
1370ebbce04cSNico Weber       hooked = true;
1371ebbce04cSNico Weber     }
1372ebbce04cSNico Weber   }
1373ebbce04cSNico Weber   return hooked;
1374ebbce04cSNico Weber }
1375ebbce04cSNico Weber 
1376ebbce04cSNico Weber bool OverrideImportedFunction(const char *module_to_patch,
1377ebbce04cSNico Weber                               const char *imported_module,
1378ebbce04cSNico Weber                               const char *function_name, uptr new_function,
1379ebbce04cSNico Weber                               uptr *orig_old_func) {
1380ebbce04cSNico Weber   HMODULE module = GetModuleHandleA(module_to_patch);
1381ebbce04cSNico Weber   if (!module)
1382ebbce04cSNico Weber     return false;
1383ebbce04cSNico Weber 
1384ebbce04cSNico Weber   // Check that the module header is full and present.
1385ebbce04cSNico Weber   RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);
1386ebbce04cSNico Weber   RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);
1387ebbce04cSNico Weber   if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE ||  // "MZ"
1388ebbce04cSNico Weber       headers->Signature != IMAGE_NT_SIGNATURE ||             // "PE\0\0"
1389ebbce04cSNico Weber       headers->FileHeader.SizeOfOptionalHeader <
1390ebbce04cSNico Weber           sizeof(IMAGE_OPTIONAL_HEADER)) {
1391ebbce04cSNico Weber     return false;
1392ebbce04cSNico Weber   }
1393ebbce04cSNico Weber 
1394ebbce04cSNico Weber   IMAGE_DATA_DIRECTORY *import_directory =
1395ebbce04cSNico Weber       &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];
1396ebbce04cSNico Weber 
1397ebbce04cSNico Weber   // Iterate the list of imported DLLs. FirstThunk will be null for the last
1398ebbce04cSNico Weber   // entry.
1399ebbce04cSNico Weber   RVAPtr<IMAGE_IMPORT_DESCRIPTOR> imports(module,
1400ebbce04cSNico Weber                                           import_directory->VirtualAddress);
1401ebbce04cSNico Weber   for (; imports->FirstThunk != 0; ++imports) {
1402ebbce04cSNico Weber     RVAPtr<const char> modname(module, imports->Name);
1403ebbce04cSNico Weber     if (_stricmp(&*modname, imported_module) == 0)
1404ebbce04cSNico Weber       break;
1405ebbce04cSNico Weber   }
1406ebbce04cSNico Weber   if (imports->FirstThunk == 0)
1407ebbce04cSNico Weber     return false;
1408ebbce04cSNico Weber 
1409ebbce04cSNico Weber   // We have two parallel arrays: the import address table (IAT) and the table
1410ebbce04cSNico Weber   // of names. They start out containing the same data, but the loader rewrites
1411ebbce04cSNico Weber   // the IAT to hold imported addresses and leaves the name table in
1412ebbce04cSNico Weber   // OriginalFirstThunk alone.
1413ebbce04cSNico Weber   RVAPtr<IMAGE_THUNK_DATA> name_table(module, imports->OriginalFirstThunk);
1414ebbce04cSNico Weber   RVAPtr<IMAGE_THUNK_DATA> iat(module, imports->FirstThunk);
1415ebbce04cSNico Weber   for (; name_table->u1.Ordinal != 0; ++name_table, ++iat) {
1416ebbce04cSNico Weber     if (!IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) {
1417ebbce04cSNico Weber       RVAPtr<IMAGE_IMPORT_BY_NAME> import_by_name(
1418ebbce04cSNico Weber           module, name_table->u1.ForwarderString);
1419ebbce04cSNico Weber       const char *funcname = &import_by_name->Name[0];
142069ebac7aSHans Wennborg       if (_strcmp(funcname, function_name) == 0)
1421ebbce04cSNico Weber         break;
1422ebbce04cSNico Weber     }
1423ebbce04cSNico Weber   }
1424ebbce04cSNico Weber   if (name_table->u1.Ordinal == 0)
1425ebbce04cSNico Weber     return false;
1426ebbce04cSNico Weber 
1427ebbce04cSNico Weber   // Now we have the correct IAT entry. Do the swap. We have to make the page
1428ebbce04cSNico Weber   // read/write first.
1429ebbce04cSNico Weber   if (orig_old_func)
1430ebbce04cSNico Weber     *orig_old_func = iat->u1.AddressOfData;
1431ebbce04cSNico Weber   DWORD old_prot, unused_prot;
1432ebbce04cSNico Weber   if (!VirtualProtect(&iat->u1.AddressOfData, 4, PAGE_EXECUTE_READWRITE,
1433ebbce04cSNico Weber                       &old_prot))
1434ebbce04cSNico Weber     return false;
1435ebbce04cSNico Weber   iat->u1.AddressOfData = new_function;
1436ebbce04cSNico Weber   if (!VirtualProtect(&iat->u1.AddressOfData, 4, old_prot, &unused_prot))
1437ebbce04cSNico Weber     return false;  // Not clear if this failure bothers us.
1438ebbce04cSNico Weber   return true;
1439ebbce04cSNico Weber }
1440ebbce04cSNico Weber 
1441ebbce04cSNico Weber }  // namespace __interception
1442ebbce04cSNico Weber 
144304ccbe6eSHans Wennborg #endif  // SANITIZER_WINDOWS
1444