brintos

brintos / llvm-project-archived public Read only

0
0
Text · 51.6 KiB · 8568724 Raw
1455 lines · cpp
1//===-- interception_win.cpp ------------------------------------*- C++ -*-===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9// This file is a part of AddressSanitizer, an address sanity checker.10//11// Windows-specific interception methods.12//13// This file is implementing several hooking techniques to intercept calls14// to functions. The hooks are dynamically installed by modifying the assembly15// code.16//17// The hooking techniques are making assumptions on the way the code is18// generated and are safe under these assumptions.19//20// On 64-bit architecture, there is no direct 64-bit jump instruction. To allow21// arbitrary branching on the whole memory space, the notion of trampoline22// region is used. A trampoline region is a memory space withing 2G boundary23// where it is safe to add custom assembly code to build 64-bit jumps.24//25// Hooking techniques26// ==================27//28// 1) Detour29//30//    The Detour hooking technique is assuming the presence of a header with31//    padding and an overridable 2-bytes nop instruction (mov edi, edi). The32//    nop instruction can safely be replaced by a 2-bytes jump without any need33//    to save the instruction. A jump to the target is encoded in the function34//    header and the nop instruction is replaced by a short jump to the header.35//36//        head:  5 x nop                 head:  jmp <hook>37//        func:  mov edi, edi    -->     func:  jmp short <head>38//               [...]                   real:  [...]39//40//    This technique is only implemented on 32-bit architecture.41//    Most of the time, Windows API are hookable with the detour technique.42//43// 2) Redirect Jump44//45//    The redirect jump is applicable when the first instruction is a direct46//    jump. The instruction is replaced by jump to the hook.47//48//        func:  jmp <label>     -->     func:  jmp <hook>49//50//    On a 64-bit architecture, a trampoline is inserted.51//52//        func:  jmp <label>     -->     func:  jmp <tramp>53//                                              [...]54//55//                                   [trampoline]56//                                      tramp:  jmp QWORD [addr]57//                                       addr:  .bytes <hook>58//59//    Note: <real> is equivalent to <label>.60//61// 3) HotPatch62//63//    The HotPatch hooking is assuming the presence of a header with padding64//    and a first instruction with at least 2-bytes.65//66//    The reason to enforce the 2-bytes limitation is to provide the minimal67//    space to encode a short jump. HotPatch technique is only rewriting one68//    instruction to avoid breaking a sequence of instructions containing a69//    branching target.70//71//    Assumptions are enforced by MSVC compiler by using the /HOTPATCH flag.72//      see: https://msdn.microsoft.com/en-us/library/ms173507.aspx73//    Default padding length is 5 bytes in 32-bits and 6 bytes in 64-bits.74//75//        head:   5 x nop                head:  jmp <hook>76//        func:   <instr>        -->     func:  jmp short <head>77//                [...]                  body:  [...]78//79//                                   [trampoline]80//                                       real:  <instr>81//                                              jmp <body>82//83//    On a 64-bit architecture:84//85//        head:   6 x nop                head:  jmp QWORD [addr1]86//        func:   <instr>        -->     func:  jmp short <head>87//                [...]                  body:  [...]88//89//                                   [trampoline]90//                                      addr1:  .bytes <hook>91//                                       real:  <instr>92//                                              jmp QWORD [addr2]93//                                      addr2:  .bytes <body>94//95// 4) Trampoline96//97//    The Trampoline hooking technique is the most aggressive one. It is98//    assuming that there is a sequence of instructions that can be safely99//    replaced by a jump (enough room and no incoming branches).100//101//    Unfortunately, these assumptions can't be safely presumed and code may102//    be broken after hooking.103//104//        func:   <instr>        -->     func:  jmp <hook>105//                <instr>106//                [...]                  body:  [...]107//108//                                   [trampoline]109//                                       real:  <instr>110//                                              <instr>111//                                              jmp <body>112//113//    On a 64-bit architecture:114//115//        func:   <instr>        -->     func:  jmp QWORD [addr1]116//                <instr>117//                [...]                  body:  [...]118//119//                                   [trampoline]120//                                      addr1:  .bytes <hook>121//                                       real:  <instr>122//                                              <instr>123//                                              jmp QWORD [addr2]124//                                      addr2:  .bytes <body>125//===----------------------------------------------------------------------===//126 127#include "interception.h"128 129#if SANITIZER_WINDOWS130#include "sanitizer_common/sanitizer_platform.h"131#define WIN32_LEAN_AND_MEAN132#include <windows.h>133#include <psapi.h>134 135namespace __interception {136 137static const int kAddressLength = FIRST_32_SECOND_64(4, 8);138static const int kJumpInstructionLength = 5;139static const int kShortJumpInstructionLength = 2;140UNUSED static const int kIndirectJumpInstructionLength = 6;141static const int kBranchLength =142    FIRST_32_SECOND_64(kJumpInstructionLength, kIndirectJumpInstructionLength);143static const int kDirectBranchLength = kBranchLength + kAddressLength;144 145#  if defined(_MSC_VER)146#    define INTERCEPTION_FORMAT(f, a)147#  else148#    define INTERCEPTION_FORMAT(f, a) __attribute__((format(printf, f, a)))149#  endif150 151static void (*ErrorReportCallback)(const char *format, ...)152    INTERCEPTION_FORMAT(1, 2);153 154void SetErrorReportCallback(void (*callback)(const char *format, ...)) {155  ErrorReportCallback = callback;156}157 158#  define ReportError(...)                \159    do {                                  \160      if (ErrorReportCallback)            \161        ErrorReportCallback(__VA_ARGS__); \162    } while (0)163 164static void InterceptionFailed() {165  ReportError("interception_win: failed due to an unrecoverable error.\n");166  // This acts like an abort when no debugger is attached. According to an old167  // comment, calling abort() leads to an infinite recursion in CheckFailed.168  __debugbreak();169}170 171static bool DistanceIsWithin2Gig(uptr from, uptr target) {172#if SANITIZER_WINDOWS64173  if (from < target)174    return target - from <= (uptr)0x7FFFFFFFU;175  else176    return from - target <= (uptr)0x80000000U;177#else178  // In a 32-bit address space, the address calculation will wrap, so this check179  // is unnecessary.180  return true;181#endif182}183 184static uptr GetMmapGranularity() {185  SYSTEM_INFO si;186  GetSystemInfo(&si);187  return si.dwAllocationGranularity;188}189 190UNUSED static uptr RoundDownTo(uptr size, uptr boundary) {191  return size & ~(boundary - 1);192}193 194UNUSED static uptr RoundUpTo(uptr size, uptr boundary) {195  return RoundDownTo(size + boundary - 1, boundary);196}197 198// FIXME: internal_str* and internal_mem* functions should be moved from the199// ASan sources into interception/.200 201static size_t _strlen(const char *str) {202  const char* p = str;203  while (*p != '\0') ++p;204  return p - str;205}206 207static char* _strchr(char* str, char c) {208  while (*str) {209    if (*str == c)210      return str;211    ++str;212  }213  return nullptr;214}215 216static int _strcmp(const char *s1, const char *s2) {217  while (true) {218    unsigned c1 = *s1;219    unsigned c2 = *s2;220    if (c1 != c2) return (c1 < c2) ? -1 : 1;221    if (c1 == 0) break;222    s1++;223    s2++;224  }225  return 0;226}227 228static void _memset(void *p, int value, size_t sz) {229  for (size_t i = 0; i < sz; ++i)230    ((char*)p)[i] = (char)value;231}232 233static void _memcpy(void *dst, void *src, size_t sz) {234  char *dst_c = (char*)dst,235       *src_c = (char*)src;236  for (size_t i = 0; i < sz; ++i)237    dst_c[i] = src_c[i];238}239 240static bool ChangeMemoryProtection(241    uptr address, uptr size, DWORD *old_protection) {242  return ::VirtualProtect((void*)address, size,243                          PAGE_EXECUTE_READWRITE,244                          old_protection) != FALSE;245}246 247static bool RestoreMemoryProtection(248    uptr address, uptr size, DWORD old_protection) {249  DWORD unused;250  return ::VirtualProtect((void*)address, size,251                          old_protection,252                          &unused) != FALSE;253}254 255static bool IsMemoryPadding(uptr address, uptr size) {256  u8* function = (u8*)address;257  for (size_t i = 0; i < size; ++i)258    if (function[i] != 0x90 && function[i] != 0xCC)259      return false;260  return true;261}262 263static const u8 kHintNop8Bytes[] = {264  0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00265};266 267template<class T>268static bool FunctionHasPrefix(uptr address, const T &pattern) {269  u8* function = (u8*)address - sizeof(pattern);270  for (size_t i = 0; i < sizeof(pattern); ++i)271    if (function[i] != pattern[i])272      return false;273  return true;274}275 276static bool FunctionHasPadding(uptr address, uptr size) {277  if (IsMemoryPadding(address - size, size))278    return true;279  if (size <= sizeof(kHintNop8Bytes) &&280      FunctionHasPrefix(address, kHintNop8Bytes))281    return true;282  return false;283}284 285static void WritePadding(uptr from, uptr size) {286  _memset((void*)from, 0xCC, (size_t)size);287}288 289static void WriteJumpInstruction(uptr from, uptr target) {290  if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target)) {291    ReportError(292        "interception_win: cannot write jmp further than 2GB away, from %p to "293        "%p.\n",294        (void *)from, (void *)target);295    InterceptionFailed();296  }297  ptrdiff_t offset = target - from - kJumpInstructionLength;298  *(u8*)from = 0xE9;299  *(u32*)(from + 1) = offset;300}301 302static void WriteShortJumpInstruction(uptr from, uptr target) {303  sptr offset = target - from - kShortJumpInstructionLength;304  if (offset < -128 || offset > 127) {305    ReportError("interception_win: cannot write short jmp from %p to %p\n",306                (void *)from, (void *)target);307    InterceptionFailed();308  }309  *(u8*)from = 0xEB;310  *(u8*)(from + 1) = (u8)offset;311}312 313#if SANITIZER_WINDOWS64314static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) {315  // jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative316  // offset.317  // The offset is the distance from then end of the jump instruction to the318  // memory location containing the targeted address. The displacement is still319  // 32-bit in x64, so indirect_target must be located within +/- 2GB range.320  int offset = indirect_target - from - kIndirectJumpInstructionLength;321  if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength,322                            indirect_target)) {323    ReportError(324        "interception_win: cannot write indirect jmp with target further than "325        "2GB away, from %p to %p.\n",326        (void *)from, (void *)indirect_target);327    InterceptionFailed();328  }329  *(u16*)from = 0x25FF;330  *(u32*)(from + 2) = offset;331}332#endif333 334static void WriteBranch(335    uptr from, uptr indirect_target, uptr target) {336#if SANITIZER_WINDOWS64337  WriteIndirectJumpInstruction(from, indirect_target);338  *(u64*)indirect_target = target;339#else340  (void)indirect_target;341  WriteJumpInstruction(from, target);342#endif343}344 345static void WriteDirectBranch(uptr from, uptr target) {346#if SANITIZER_WINDOWS64347  // Emit an indirect jump through immediately following bytes:348  //   jmp [rip + kBranchLength]349  //   .quad <target>350  WriteBranch(from, from + kBranchLength, target);351#else352  WriteJumpInstruction(from, target);353#endif354}355 356struct TrampolineMemoryRegion {357  uptr content;358  uptr allocated_size;359  uptr max_size;360};361 362UNUSED static const uptr kTrampolineRangeLimit = 1ull << 31;  // 2 gig363static const int kMaxTrampolineRegion = 1024;364static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion];365 366static void *AllocateTrampolineRegion(uptr min_addr, uptr max_addr,367                                      uptr func_addr, size_t granularity) {368#  if SANITIZER_WINDOWS64369  // Clamp {min,max}_addr to the accessible address space.370  SYSTEM_INFO system_info;371  ::GetSystemInfo(&system_info);372  uptr min_virtual_addr =373      RoundUpTo((uptr)system_info.lpMinimumApplicationAddress, granularity);374  uptr max_virtual_addr =375      RoundDownTo((uptr)system_info.lpMaximumApplicationAddress, granularity);376  if (min_addr < min_virtual_addr)377    min_addr = min_virtual_addr;378  if (max_addr > max_virtual_addr)379    max_addr = max_virtual_addr;380 381  // This loop probes the virtual address space to find free memory in the382  // [min_addr, max_addr] interval. The search starts from func_addr and383  // proceeds "outwards" towards the interval bounds using two probes, lo_addr384  // and hi_addr, for addresses lower/higher than func_addr. At each step, it385  // considers the probe closest to func_addr. If that address is not free, the386  // probe is advanced (lower or higher depending on the probe) to the next387  // memory block and the search continues.388  uptr lo_addr = RoundDownTo(func_addr, granularity);389  uptr hi_addr = RoundUpTo(func_addr, granularity);390  while (lo_addr >= min_addr || hi_addr <= max_addr) {391    // Consider the in-range address closest to func_addr.392    uptr addr;393    if (lo_addr < min_addr)394      addr = hi_addr;395    else if (hi_addr > max_addr)396      addr = lo_addr;397    else398      addr = (hi_addr - func_addr < func_addr - lo_addr) ? hi_addr : lo_addr;399 400    MEMORY_BASIC_INFORMATION info;401    if (!::VirtualQuery((void *)addr, &info, sizeof(info))) {402      ReportError(403          "interception_win: VirtualQuery in AllocateTrampolineRegion failed "404          "for %p\n",405          (void *)addr);406      return nullptr;407    }408 409    // Check whether a region can be allocated at |addr|.410    if (info.State == MEM_FREE && info.RegionSize >= granularity) {411      void *page =412          ::VirtualAlloc((void *)addr, granularity, MEM_RESERVE | MEM_COMMIT,413                         PAGE_EXECUTE_READWRITE);414      if (page == nullptr)415        ReportError(416            "interception_win: VirtualAlloc in AllocateTrampolineRegion failed "417            "for %p\n",418            (void *)addr);419      return page;420    }421 422    if (addr == lo_addr)423      lo_addr =424          RoundDownTo((uptr)info.AllocationBase - granularity, granularity);425    if (addr == hi_addr)426      hi_addr =427          RoundUpTo((uptr)info.BaseAddress + info.RegionSize, granularity);428  }429 430  ReportError(431      "interception_win: AllocateTrampolineRegion failed to find free memory; "432      "min_addr: %p, max_addr: %p, func_addr: %p, granularity: %zu\n",433      (void *)min_addr, (void *)max_addr, (void *)func_addr, granularity);434  return nullptr;435#else436  return ::VirtualAlloc(nullptr,437                        granularity,438                        MEM_RESERVE | MEM_COMMIT,439                        PAGE_EXECUTE_READWRITE);440#endif441}442 443// Used by unittests to release mapped memory space.444void TestOnlyReleaseTrampolineRegions() {445  for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {446    TrampolineMemoryRegion *current = &TrampolineRegions[bucket];447    if (current->content == 0)448      return;449    ::VirtualFree((void*)current->content, 0, MEM_RELEASE);450    current->content = 0;451  }452}453 454static uptr AllocateMemoryForTrampoline(uptr func_address, size_t size) {455#  if SANITIZER_WINDOWS64456  uptr min_addr = func_address - kTrampolineRangeLimit;457  uptr max_addr = func_address + kTrampolineRangeLimit - size;458 459  // Allocate memory within 2GB of the module (DLL or EXE file) so that any460  // address within the module can be referenced with PC-relative operands.461  // This allows us to not just jump to the trampoline with a PC-relative462  // offset, but to relocate any instructions that we copy to the trampoline463  // which have references to the original module. If we can't find the base464  // address of the module (e.g. if func_address is in mmap'ed memory), just465  // stay within 2GB of func_address.466  HMODULE module;467  if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |468                           GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,469                           (LPCWSTR)func_address, &module)) {470    MODULEINFO module_info;471    if (::GetModuleInformation(::GetCurrentProcess(), module,472                                &module_info, sizeof(module_info))) {473      min_addr = (uptr)module_info.lpBaseOfDll + module_info.SizeOfImage -474                 kTrampolineRangeLimit;475      max_addr = (uptr)module_info.lpBaseOfDll + kTrampolineRangeLimit - size;476    }477  }478 479  // Check for overflow.480  if (min_addr > func_address)481    min_addr = 0;482  if (max_addr < func_address)483    max_addr = ~(uptr)0;484#  else485  uptr min_addr = 0;486  uptr max_addr = ~min_addr;487#  endif488 489  // Find a region within [min_addr,max_addr] with enough space to allocate490  // |size| bytes.491  TrampolineMemoryRegion *region = nullptr;492  for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) {493    TrampolineMemoryRegion* current = &TrampolineRegions[bucket];494    if (current->content == 0) {495      // No valid region found, allocate a new region.496      size_t bucket_size = GetMmapGranularity();497      void *content = AllocateTrampolineRegion(min_addr, max_addr, func_address,498                                               bucket_size);499      if (content == nullptr)500        return 0U;501 502      current->content = (uptr)content;503      current->allocated_size = 0;504      current->max_size = bucket_size;505      region = current;506      break;507    } else if (current->max_size - current->allocated_size > size) {508      uptr next_address = current->content + current->allocated_size;509      if (next_address < min_addr || next_address > max_addr)510        continue;511      // The space can be allocated in the current region.512      region = current;513      break;514    }515  }516 517  // Failed to find a region.518  if (region == nullptr)519    return 0U;520 521  // Allocate the space in the current region.522  uptr allocated_space = region->content + region->allocated_size;523  region->allocated_size += size;524  WritePadding(allocated_space, size);525 526  return allocated_space;527}528 529// The following prologues cannot be patched because of the short jump530// jumping to the patching region.531 532// Short jump patterns  below are only for x86_64.533#  if SANITIZER_WINDOWS_x64534// ntdll!wcslen in Win11535//   488bc1          mov     rax,rcx536//   0fb710          movzx   edx,word ptr [rax]537//   4883c002        add     rax,2538//   6685d2          test    dx,dx539//   75f4            jne     -12540static const u8 kPrologueWithShortJump1[] = {541    0x48, 0x8b, 0xc1, 0x0f, 0xb7, 0x10, 0x48, 0x83,542    0xc0, 0x02, 0x66, 0x85, 0xd2, 0x75, 0xf4,543};544 545// ntdll!strrchr in Win11546//   4c8bc1          mov     r8,rcx547//   8a01            mov     al,byte ptr [rcx]548//   48ffc1          inc     rcx549//   84c0            test    al,al550//   75f7            jne     -9551static const u8 kPrologueWithShortJump2[] = {552    0x4c, 0x8b, 0xc1, 0x8a, 0x01, 0x48, 0xff, 0xc1,553    0x84, 0xc0, 0x75, 0xf7,554};555#endif556 557// Returns 0 on error.558static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) {559  if (rel_offset) {560    *rel_offset = 0;561  }562 563#if SANITIZER_ARM64564  // An ARM64 instruction is 4 bytes long.565  return 4;566#endif567 568#  if SANITIZER_WINDOWS_x64569  if (memcmp((u8*)address, kPrologueWithShortJump1,570             sizeof(kPrologueWithShortJump1)) == 0 ||571      memcmp((u8*)address, kPrologueWithShortJump2,572             sizeof(kPrologueWithShortJump2)) == 0) {573    return 0;574  }575#endif576 577  switch (*(u64*)address) {578    case 0x90909090909006EB:  // stub: jmp over 6 x nop.579      return 8;580  }581 582  switch (*(u8*)address) {583    case 0x90:  // 90 : nop584    case 0xC3:  // C3 : ret   (for small/empty function interception585    case 0xCC:  // CC : int 3  i.e. registering weak functions)586      return 1;587 588    case 0x50:  // push eax / rax589    case 0x51:  // push ecx / rcx590    case 0x52:  // push edx / rdx591    case 0x53:  // push ebx / rbx592    case 0x54:  // push esp / rsp593    case 0x55:  // push ebp / rbp594    case 0x56:  // push esi / rsi595    case 0x57:  // push edi / rdi596    case 0x5D:  // pop ebp / rbp597      return 1;598 599    case 0x6A:  // 6A XX = push XX600      return 2;601 602    // This instruction can be encoded with a 16-bit immediate but that is603    // incredibly unlikely.604    case 0x68:  // 68 XX XX XX XX : push imm32605      return 5;606 607    case 0xb8:  // b8 XX XX XX XX : mov eax, XX XX XX XX608    case 0xB9:  // b9 XX XX XX XX : mov ecx, XX XX XX XX609    case 0xBA:  // ba XX XX XX XX : mov edx, XX XX XX XX610      return 5;611 612    // Cannot overwrite control-instruction. Return 0 to indicate failure.613    case 0xE9:  // E9 XX XX XX XX : jmp <label>614    case 0xE8:  // E8 XX XX XX XX : call <func>615    case 0xEB:  // EB XX : jmp XX (short jump)616    case 0x70:  // 7Y YY : jy XX (short conditional jump)617    case 0x71:618    case 0x72:619    case 0x73:620    case 0x74:621    case 0x75:622    case 0x76:623    case 0x77:624    case 0x78:625    case 0x79:626    case 0x7A:627    case 0x7B:628    case 0x7C:629    case 0x7D:630    case 0x7E:631    case 0x7F:632      return 0;633  }634 635  switch (*(u16*)(address)) {636    case 0x018A:  // 8A 01 : mov al, byte ptr [ecx]637    case 0xFF8B:  // 8B FF : mov edi, edi638    case 0xEC8B:  // 8B EC : mov ebp, esp639    case 0xc889:  // 89 C8 : mov eax, ecx640    case 0xD189:  // 89 D1 : mov ecx, edx641    case 0xE589:  // 89 E5 : mov ebp, esp642    case 0xC18B:  // 8B C1 : mov eax, ecx643    case 0xC031:  // 31 C0 : xor eax, eax644    case 0xC931:  // 31 C9 : xor ecx, ecx645    case 0xD231:  // 31 D2 : xor edx, edx646    case 0xC033:  // 33 C0 : xor eax, eax647    case 0xC933:  // 33 C9 : xor ecx, ecx648    case 0xD233:  // 33 D2 : xor edx, edx649    case 0xFF33:  // 33 FF : xor edi, edi650    case 0x9066:  // 66 90 : xchg %ax,%ax (Two-byte NOP)651    case 0xDB84:  // 84 DB : test bl,bl652    case 0xC084:  // 84 C0 : test al,al653    case 0xC984:  // 84 C9 : test cl,cl654    case 0xD284:  // 84 D2 : test dl,dl655      return 2;656 657    case 0x3980:  // 80 39 XX : cmp BYTE PTR [rcx], XX658    case 0x4D8B:  // 8B 4D XX : mov XX(%ebp), ecx659    case 0x558B:  // 8B 55 XX : mov XX(%ebp), edx660    case 0x758B:  // 8B 75 XX : mov XX(%ebp), esp661    case 0xE483:  // 83 E4 XX : and esp, XX662    case 0xEC83:  // 83 EC XX : sub esp, XX663    case 0xC1F6:  // F6 C1 XX : test cl, XX664      return 3;665 666    case 0x89FF:  // FF 89 XX XX XX XX : dec dword ptr [ecx + XX XX XX XX]667    case 0xEC81:  // 81 EC XX XX XX XX : sub esp, XX XX XX XX668      return 6;669 670    // Cannot overwrite control-instruction. Return 0 to indicate failure.671    case 0x25FF:  // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX]672      return 0;673  }674 675  switch (0x00FFFFFF & *(u32 *)address) {676    case 0x244C8D:  // 8D 4C 24 XX : lea ecx, [esp + XX]677    case 0x2474FF:  // FF 74 24 XX : push qword ptr [rsp + XX]678      return 4;679    case 0x24A48D:  // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX]680      return 7;681  }682 683  switch (0x000000FF & *(u32 *)address) {684    case 0xc2:  // C2 XX XX : ret XX (needed for registering weak functions)685      return 3;686  }687 688#  if SANITIZER_WINDOWS_x64689  switch (*(u8*)address) {690    case 0xA1:  // A1 XX XX XX XX XX XX XX XX :691                //   movabs eax, dword ptr ds:[XXXXXXXX]692      return 9;693    case 0xF2:694      switch (*(u32 *)(address + 1)) {695          case 0x2444110f:  //  f2 0f 11 44 24 XX       movsd  QWORD PTR696                            //  [rsp + XX], xmm0697          case 0x244c110f:  //  f2 0f 11 4c 24 XX       movsd  QWORD PTR698                            //  [rsp + XX], xmm1699          case 0x2454110f:  //  f2 0f 11 54 24 XX       movsd  QWORD PTR700                            //  [rsp + XX], xmm2701          case 0x245c110f:  //  f2 0f 11 5c 24 XX       movsd  QWORD PTR702                            //  [rsp + XX], xmm3703          case 0x2464110f:  //  f2 0f 11 64 24 XX       movsd  QWORD PTR704                            //  [rsp + XX], xmm4705            return 6;706      }707      break;708 709    case 0x83:710      const u8 next_byte = *(u8*)(address + 1);711      const u8 mod = next_byte >> 6;712      const u8 rm = next_byte & 7;713      if (mod == 1 && rm == 4)714        return 5;  // 83 ModR/M SIB Disp8 Imm8715                   //   add|or|adc|sbb|and|sub|xor|cmp [r+disp8], imm8716  }717 718  switch (*(u16*)address) {719    case 0x5040:  // push rax720    case 0x5140:  // push rcx721    case 0x5240:  // push rdx722    case 0x5340:  // push rbx723    case 0x5440:  // push rsp724    case 0x5540:  // push rbp725    case 0x5640:  // push rsi726    case 0x5740:  // push rdi727    case 0x5441:  // push r12728    case 0x5541:  // push r13729    case 0x5641:  // push r14730    case 0x5741:  // push r15731    case 0xc084:  // test al, al732    case 0x018a:  // mov al, byte ptr [rcx]733      return 2;734 735    case 0x7E80:  // 80 7E YY XX  cmp BYTE PTR [rsi+YY], XX736    case 0x7D80:  // 80 7D YY XX  cmp BYTE PTR [rbp+YY], XX737    case 0x7A80:  // 80 7A YY XX  cmp BYTE PTR [rdx+YY], XX738    case 0x7880:  // 80 78 YY XX  cmp BYTE PTR [rax+YY], XX739    case 0x7B80:  // 80 7B YY XX  cmp BYTE PTR [rbx+YY], XX740    case 0x7980:  // 80 79 YY XX  cmp BYTE ptr [rcx+YY], XX741      return 4;742 743    case 0x058A:  // 8A 05 XX XX XX XX : mov al, byte ptr [XX XX XX XX]744    case 0x058B:  // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX]745      if (rel_offset)746        *rel_offset = 2;747      FALLTHROUGH;748    case 0xB841:  // 41 B8 XX XX XX XX : mov r8d, XX XX XX XX749      return 6;750 751    case 0x7E81:  // 81 7E YY XX XX XX XX  cmp DWORD PTR [rsi+YY], XX XX XX XX752    case 0x7D81:  // 81 7D YY XX XX XX XX  cmp DWORD PTR [rbp+YY], XX XX XX XX753    case 0x7A81:  // 81 7A YY XX XX XX XX  cmp DWORD PTR [rdx+YY], XX XX XX XX754    case 0x7881:  // 81 78 YY XX XX XX XX  cmp DWORD PTR [rax+YY], XX XX XX XX755    case 0x7B81:  // 81 7B YY XX XX XX XX  cmp DWORD PTR [rbx+YY], XX XX XX XX756    case 0x7981:  // 81 79 YY XX XX XX XX  cmp dword ptr [rcx+YY], XX XX XX XX757      return 7;758 759    case 0xb848:  // 48 b8 XX XX XX XX XX XX XX XX :760                  //   movabsq XX XX XX XX XX XX XX XX, rax761    case 0xba48:  // 48 ba XX XX XX XX XX XX XX XX :762                  //   movabsq XX XX XX XX XX XX XX XX, rdx763      return 10;764  }765 766  switch (0x00FFFFFF & *(u32 *)address) {767    case 0x10b70f:    // 0f b7 10 : movzx edx, WORD PTR [rax]768    case 0x02b70f:    // 0f b7 02 : movzx eax, WORD PTR [rdx]769    case 0xc00b4d:    // 4d 0b c0 : or r8, r8770    case 0xc03345:    // 45 33 c0 : xor r8d, r8d771    case 0xc08548:    // 48 85 c0 : test rax, rax772    case 0xc0854d:    // 4d 85 c0 : test r8, r8773    case 0xc08b41:    // 41 8b c0 : mov eax, r8d774    case 0xc0ff48:    // 48 ff c0 : inc rax775    case 0xc0ff49:    // 49 ff c0 : inc r8776    case 0xc18b41:    // 41 8b c1 : mov eax, r9d777    case 0xc18b48:    // 48 8b c1 : mov rax, rcx778    case 0xc18b4c:    // 4c 8b c1 : mov r8, rcx779    case 0xc1ff48:    // 48 ff c1 : inc rcx780    case 0xc1ff49:    // 49 ff c1 : inc r9781    case 0xc28b41:    // 41 8b c2 : mov eax, r10d782    case 0x01b60f:    // 0f b6 01 : movzx eax, BYTE PTR [rcx]783    case 0x09b60f:    // 0f b6 09 : movzx ecx, BYTE PTR [rcx]784    case 0x11b60f:    // 0f b6 11 : movzx edx, BYTE PTR [rcx]785    case 0xc2b60f:    // 0f b6 c2 : movzx eax, dl786    case 0xc2ff48:    // 48 ff c2 : inc rdx787    case 0xc2ff49:    // 49 ff c2 : inc r10788    case 0xc38b41:    // 41 8b c3 : mov eax, r11d789    case 0xc3ff48:    // 48 ff c3 : inc rbx790    case 0xc3ff49:    // 49 ff c3 : inc r11791    case 0xc48b41:    // 41 8b c4 : mov eax, r12d792    case 0xc48b48:    // 48 8b c4 : mov rax, rsp793    case 0xc4ff49:    // 49 ff c4 : inc r12794    case 0xc5ff49:    // 49 ff c5 : inc r13795    case 0xc6ff48:    // 48 ff c6 : inc rsi796    case 0xc6ff49:    // 49 ff c6 : inc r14797    case 0xc7ff48:    // 48 ff c7 : inc rdi798    case 0xc7ff49:    // 49 ff c7 : inc r15799    case 0xc93345:    // 45 33 c9 : xor r9d, r9d800    case 0xc98548:    // 48 85 c9 : test rcx, rcx801    case 0xc9854d:    // 4d 85 c9 : test r9, r9802    case 0xc98b4c:    // 4c 8b c9 : mov r9, rcx803    case 0xd12948:    // 48 29 d1 : sub rcx, rdx804    case 0xc22b4c:    // 4c 2b c2 : sub r8, rdx805    case 0xca2b48:    // 48 2b ca : sub rcx, rdx806    case 0xca3b48:    // 48 3b ca : cmp rcx, rdx807    case 0xd12b48:    // 48 2b d1 : sub rdx, rcx808    case 0xd18b48:    // 48 8b d1 : mov rdx, rcx809    case 0xd18b4c:    // 4c 8b d1 : mov r10, rcx810    case 0xd28548:    // 48 85 d2 : test rdx, rdx811    case 0xd2854d:    // 4d 85 d2 : test r10, r10812    case 0xd28b4c:    // 4c 8b d2 : mov r10, rdx813    case 0xd2b60f:    // 0f b6 d2 : movzx edx, dl814    case 0xd2be0f:    // 0f be d2 : movsx edx, dl815    case 0xd98b4c:    // 4c 8b d9 : mov r11, rcx816    case 0xd9f748:    // 48 f7 d9 : neg rcx817    case 0xc03145:    // 45 31 c0 : xor r8d,r8d818    case 0xc93145:    // 45 31 c9 : xor r9d,r9d819    case 0xd23345:    // 45 33 d2 : xor r10d, r10d820    case 0xdb3345:    // 45 33 db : xor r11d, r11d821    case 0xc08445:    // 45 84 c0 : test r8b,r8b822    case 0xd28445:    // 45 84 d2 : test r10b,r10b823    case 0xdb8548:    // 48 85 db : test rbx, rbx824    case 0xdb854d:    // 4d 85 db : test r11, r11825    case 0xdc8b4c:    // 4c 8b dc : mov r11, rsp826    case 0xe48548:    // 48 85 e4 : test rsp, rsp827    case 0xe4854d:    // 4d 85 e4 : test r12, r12828    case 0xc88948:    // 48 89 c8 : mov rax,rcx829    case 0xcb8948:    // 48 89 cb : mov rbx,rcx830    case 0xd08948:    // 48 89 d0 : mov rax,rdx831    case 0xd18948:    // 48 89 d1 : mov rcx,rdx832    case 0xd38948:    // 48 89 d3 : mov rbx,rdx833    case 0xe58948:    // 48 89 e5 : mov rbp, rsp834    case 0xed8548:    // 48 85 ed : test rbp, rbp835    case 0xc88949:    // 49 89 c8 : mov r8, rcx836    case 0xc98949:    // 49 89 c9 : mov r9, rcx837    case 0xca8949:    // 49 89 ca : mov r10,rcx838    case 0xd08949:    // 49 89 d0 : mov r8, rdx839    case 0xd18949:    // 49 89 d1 : mov r9, rdx840    case 0xd28949:    // 49 89 d2 : mov r10, rdx841    case 0xd38949:    // 49 89 d3 : mov r11, rdx842    case 0xed854d:    // 4d 85 ed : test r13, r13843    case 0xf6854d:    // 4d 85 f6 : test r14, r14844    case 0xff854d:    // 4d 85 ff : test r15, r15845      return 3;846 847    case 0x245489:    // 89 54 24 XX : mov DWORD PTR[rsp + XX], edx848    case 0x428d44:    // 44 8d 42 XX : lea r8d , [rdx + XX]849    case 0x588948:    // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx850    case 0xec8348:    // 48 83 ec XX : sub rsp, XX851    case 0xf88349:    // 49 83 f8 XX : cmp r8, XX852    case 0x488d49:    // 49 8d 48 XX : lea rcx, [...]853    case 0x048d4c:    // 4c 8d 04 XX : lea r8, [...]854    case 0x148d4e:    // 4e 8d 14 XX : lea r10, [...]855    case 0x398366:    // 66 83 39 XX : cmp WORD PTR [rcx], XX856      return 4;857 858    case 0x441F0F:  // 0F 1F 44 XX XX :   nop DWORD PTR [...]859    case 0x246483:  // 83 64 24 XX YY :   and    DWORD PTR [rsp+XX], YY860      return 5;861 862    case 0x788166:  // 66 81 78 XX YY YY  cmp WORD PTR [rax+XX], YY YY863    case 0x798166:  // 66 81 79 XX YY YY  cmp WORD PTR [rcx+XX], YY YY864    case 0x7a8166:  // 66 81 7a XX YY YY  cmp WORD PTR [rdx+XX], YY YY865    case 0x7b8166:  // 66 81 7b XX YY YY  cmp WORD PTR [rbx+XX], YY YY866    case 0x7e8166:  // 66 81 7e XX YY YY  cmp WORD PTR [rsi+XX], YY YY867    case 0x7f8166:  // 66 81 7f XX YY YY  cmp WORD PTR [rdi+XX], YY YY868      return 6;869 870    case 0xec8148:    // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX871    case 0xc0c748:    // 48 C7 C0 XX XX XX XX : mov rax, XX XX XX XX872      return 7;873 874    // clang-format off875    case 0x788141:  // 41 81 78 XX YY YY YY YY : cmp DWORD PTR [r8+YY], XX XX XX XX876    case 0x798141:  // 41 81 79 XX YY YY YY YY : cmp DWORD PTR [r9+YY], XX XX XX XX877    case 0x7a8141:  // 41 81 7a XX YY YY YY YY : cmp DWORD PTR [r10+YY], XX XX XX XX878    case 0x7b8141:  // 41 81 7b XX YY YY YY YY : cmp DWORD PTR [r11+YY], XX XX XX XX879    case 0x7d8141:  // 41 81 7d XX YY YY YY YY : cmp DWORD PTR [r13+YY], XX XX XX XX880    case 0x7e8141:  // 41 81 7e XX YY YY YY YY : cmp DWORD PTR [r14+YY], XX XX XX XX881    case 0x7f8141:  // 41 81 7f YY XX XX XX XX : cmp DWORD PTR [r15+YY], XX XX XX XX882    case 0x247c81:  // 81 7c 24 YY XX XX XX XX : cmp DWORD PTR [rsp+YY], XX XX XX XX883      return 8;884      // clang-format on885 886    case 0x058b48:    // 48 8b 05 XX XX XX XX :887                      //   mov rax, QWORD PTR [rip + XXXXXXXX]888    case 0x058d48:    // 48 8d 05 XX XX XX XX :889                      //   lea rax, QWORD PTR [rip + XXXXXXXX]890    case 0x0d8948:    // 48 89 0d XX XX XX XX :891                      //   mov QWORD PTR [rip + XXXXXXXX], rcx892    case 0x158948:    // 48 89 15 XX XX XX XX :893                      //   mov QWORD PTR [rip + XXXXXXXX], rdx894    case 0x25ff48:    // 48 ff 25 XX XX XX XX :895                      //   rex.W jmp QWORD PTR [rip + XXXXXXXX]896    case 0x158D4C:    // 4c 8d 15 XX XX XX XX : lea r10, [rip + XX]897      // Instructions having offset relative to 'rip' need offset adjustment.898      if (rel_offset)899        *rel_offset = 3;900      return 7;901 902    case 0x2444c7:    // C7 44 24 XX YY YY YY YY903                      //   mov dword ptr [rsp + XX], YYYYYYYY904      return 8;905 906    case 0x7c8141:  // 41 81 7c ZZ YY XX XX XX XX907                    // cmp DWORD PTR [reg+reg*n+YY], XX XX XX XX908      return 9;909  }910 911  switch (*(u32*)(address)) {912    case 0x01b60f44:  // 44 0f b6 01 : movzx r8d, BYTE PTR [rcx]913    case 0x09b60f44:  // 44 0f b6 09 : movzx r9d, BYTE PTR [rcx]914    case 0x0ab60f44:  // 44 0f b6 0a : movzx r8d, BYTE PTR [rdx]915    case 0x11b60f44:  // 44 0f b6 11 : movzx r10d, BYTE PTR [rcx]916    case 0x1ab60f44:  // 44 0f b6 1a : movzx r11d, BYTE PTR [rdx]917      return 4;918    case 0x24448b48:  // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX]919    case 0x246c8948:  // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp920    case 0x245c8948:  // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx921    case 0x24748948:  // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi922    case 0x247c8948:  // 48 89 7c 24 XX : mov QWORD PTR [rsp + XX], rdi923    case 0x244C8948:  // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx924    case 0x24548948:  // 48 89 54 24 XX : mov QWORD PTR [rsp + XX], rdx925    case 0x244c894c:  // 4c 89 4c 24 XX : mov QWORD PTR [rsp + XX], r9926    case 0x2444894c:  // 4c 89 44 24 XX : mov QWORD PTR [rsp + XX], r8927    case 0x244c8944:  // 44 89 4c 24 XX   mov DWORD PTR [rsp + XX], r9d928    case 0x24448944:  // 44 89 44 24 XX   mov DWORD PTR [rsp + XX], r8d929    case 0x246c8d48:  // 48 8d 6c 24 XX : lea rbp, [rsp + XX]930      return 5;931    case 0x24648348:  // 48 83 64 24 XX YY : and QWORD PTR [rsp + XX], YY932      return 6;933    case 0x24A48D48:  // 48 8D A4 24 XX XX XX XX : lea rsp, [rsp + XX XX XX XX]934      return 8;935  }936 937  switch (0xFFFFFFFFFFULL & *(u64 *)(address)) {938    case 0xC07E0F4866:  // 66 48 0F 7E C0 : movq rax, xmm0939      return 5;940  }941 942#else943 944  switch (*(u8*)address) {945    case 0xA1:  // A1 XX XX XX XX :  mov eax, dword ptr ds:[XXXXXXXX]946      return 5;947  }948  switch (*(u16*)address) {949    case 0x458B:  // 8B 45 XX : mov eax, dword ptr [ebp + XX]950    case 0x5D8B:  // 8B 5D XX : mov ebx, dword ptr [ebp + XX]951    case 0x7D8B:  // 8B 7D XX : mov edi, dword ptr [ebp + XX]952    case 0x758B:  // 8B 75 XX : mov esi, dword ptr [ebp + XX]953    case 0x75FF:  // FF 75 XX : push dword ptr [ebp + XX]954      return 3;955    case 0xC1F7:  // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX956      return 6;957    case 0x3D83:  // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX958      return 7;959    case 0x7D83:  // 83 7D XX YY : cmp dword ptr [ebp + XX], YY960      return 4;961  }962 963  switch (0x00FFFFFF & *(u32*)address) {964    case 0x24448A:  // 8A 44 24 XX : mov eal, dword ptr [esp + XX]965    case 0x24448B:  // 8B 44 24 XX : mov eax, dword ptr [esp + XX]966    case 0x244C8B:  // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX]967    case 0x24548B:  // 8B 54 24 XX : mov edx, dword ptr [esp + XX]968    case 0x245C8B:  // 8B 5C 24 XX : mov ebx, dword ptr [esp + XX]969    case 0x246C8B:  // 8B 6C 24 XX : mov ebp, dword ptr [esp + XX]970    case 0x24748B:  // 8B 74 24 XX : mov esi, dword ptr [esp + XX]971    case 0x247C8B:  // 8B 7C 24 XX : mov edi, dword ptr [esp + XX]972      return 4;973  }974 975  switch (*(u32*)address) {976    case 0x2444B60F:  // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX]977      return 5;978  }979#endif980 981  // Unknown instruction! This might happen when we add a new interceptor, use982  // a new compiler version, or if Windows changed how some functions are983  // compiled. In either case, we print the address and 8 bytes of instructions984  // to notify the user about the error and to help identify the unknown985  // instruction. Don't treat this as a fatal error, though we can break the986  // debugger if one has been attached.987  u8 *bytes = (u8 *)address;988  ReportError(989      "interception_win: unhandled instruction at %p: %02x %02x %02x %02x %02x "990      "%02x %02x %02x\n",991      (void *)address, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],992      bytes[5], bytes[6], bytes[7]);993  if (::IsDebuggerPresent())994    __debugbreak();995  return 0;996}997 998size_t TestOnlyGetInstructionSize(uptr address, size_t *rel_offset) {999  return GetInstructionSize(address, rel_offset);1000}1001 1002// Returns 0 on error.1003static size_t RoundUpToInstrBoundary(size_t size, uptr address) {1004  size_t cursor = 0;1005  while (cursor < size) {1006    size_t instruction_size = GetInstructionSize(address + cursor);1007    if (!instruction_size)1008      return 0;1009    cursor += instruction_size;1010  }1011  return cursor;1012}1013 1014static bool CopyInstructions(uptr to, uptr from, size_t size) {1015  size_t cursor = 0;1016  while (cursor != size) {1017    size_t rel_offset = 0;1018    size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset);1019    if (!instruction_size)1020      return false;1021    _memcpy((void *)(to + cursor), (void *)(from + cursor),1022            (size_t)instruction_size);1023    if (rel_offset) {1024#  if SANITIZER_WINDOWS641025      // we want to make sure that the new relative offset still fits in 32-bits1026      // this will be untrue if relocated_offset \notin [-2**31, 2**31)1027      s64 delta = to - from;1028      s64 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;1029      if (-0x8000'0000ll > relocated_offset ||1030          relocated_offset > 0x7FFF'FFFFll) {1031        ReportError(1032            "interception_win: CopyInstructions relocated_offset %lld outside "1033            "32-bit range\n",1034            (long long)relocated_offset);1035        return false;1036      }1037#  else1038      // on 32-bit, the relative offset will always be correct1039      s32 delta = to - from;1040      s32 relocated_offset = *(s32 *)(to + cursor + rel_offset) - delta;1041#  endif1042      *(s32 *)(to + cursor + rel_offset) = relocated_offset;1043    }1044    cursor += instruction_size;1045  }1046  return true;1047}1048 1049 1050#if !SANITIZER_WINDOWS641051bool OverrideFunctionWithDetour(1052    uptr old_func, uptr new_func, uptr *orig_old_func) {1053  const int kDetourHeaderLen = 5;1054  const u16 kDetourInstruction = 0xFF8B;1055 1056  uptr header = (uptr)old_func - kDetourHeaderLen;1057  uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength;1058 1059  // Validate that the function is hookable.1060  if (*(u16*)old_func != kDetourInstruction ||1061      !IsMemoryPadding(header, kDetourHeaderLen))1062    return false;1063 1064  // Change memory protection to writable.1065  DWORD protection = 0;1066  if (!ChangeMemoryProtection(header, patch_length, &protection))1067    return false;1068 1069  // Write a relative jump to the redirected function.1070  WriteJumpInstruction(header, new_func);1071 1072  // Write the short jump to the function prefix.1073  WriteShortJumpInstruction(old_func, header);1074 1075  // Restore previous memory protection.1076  if (!RestoreMemoryProtection(header, patch_length, protection))1077    return false;1078 1079  if (orig_old_func)1080    *orig_old_func = old_func + kShortJumpInstructionLength;1081 1082  return true;1083}1084#endif1085 1086bool OverrideFunctionWithRedirectJump(1087    uptr old_func, uptr new_func, uptr *orig_old_func) {1088  // Check whether the first instruction is a relative jump.1089  if (*(u8*)old_func != 0xE9)1090    return false;1091 1092  if (orig_old_func) {1093    sptr relative_offset = *(s32 *)(old_func + 1);1094    uptr absolute_target = old_func + relative_offset + kJumpInstructionLength;1095    *orig_old_func = absolute_target;1096  }1097 1098#if SANITIZER_WINDOWS641099  // If needed, get memory space for a trampoline jump.1100  uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength);1101  if (!trampoline)1102    return false;1103  WriteDirectBranch(trampoline, new_func);1104#endif1105 1106  // Change memory protection to writable.1107  DWORD protection = 0;1108  if (!ChangeMemoryProtection(old_func, kJumpInstructionLength, &protection))1109    return false;1110 1111  // Write a relative jump to the redirected function.1112  WriteJumpInstruction(old_func, FIRST_32_SECOND_64(new_func, trampoline));1113 1114  // Restore previous memory protection.1115  if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection))1116    return false;1117 1118  return true;1119}1120 1121bool OverrideFunctionWithHotPatch(1122    uptr old_func, uptr new_func, uptr *orig_old_func) {1123  const int kHotPatchHeaderLen = kBranchLength;1124 1125  uptr header = (uptr)old_func - kHotPatchHeaderLen;1126  uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength;1127 1128  // Validate that the function is hot patchable.1129  size_t instruction_size = GetInstructionSize(old_func);1130  if (instruction_size < kShortJumpInstructionLength ||1131      !FunctionHasPadding(old_func, kHotPatchHeaderLen))1132    return false;1133 1134  if (orig_old_func) {1135    // Put the needed instructions into the trampoline bytes.1136    uptr trampoline_length = instruction_size + kDirectBranchLength;1137    uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);1138    if (!trampoline)1139      return false;1140    if (!CopyInstructions(trampoline, old_func, instruction_size))1141      return false;1142    WriteDirectBranch(trampoline + instruction_size,1143                      old_func + instruction_size);1144    *orig_old_func = trampoline;1145  }1146 1147  // If needed, get memory space for indirect address.1148  uptr indirect_address = 0;1149#if SANITIZER_WINDOWS641150  indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);1151  if (!indirect_address)1152    return false;1153#endif1154 1155  // Change memory protection to writable.1156  DWORD protection = 0;1157  if (!ChangeMemoryProtection(header, patch_length, &protection))1158    return false;1159 1160  // Write jumps to the redirected function.1161  WriteBranch(header, indirect_address, new_func);1162  WriteShortJumpInstruction(old_func, header);1163 1164  // Restore previous memory protection.1165  if (!RestoreMemoryProtection(header, patch_length, protection))1166    return false;1167 1168  return true;1169}1170 1171bool OverrideFunctionWithTrampoline(1172    uptr old_func, uptr new_func, uptr *orig_old_func) {1173 1174  size_t instructions_length = kBranchLength;1175  size_t padding_length = 0;1176  uptr indirect_address = 0;1177 1178  if (orig_old_func) {1179    // Find out the number of bytes of the instructions we need to copy1180    // to the trampoline.1181    instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func);1182    if (!instructions_length)1183      return false;1184 1185    // Put the needed instructions into the trampoline bytes.1186    uptr trampoline_length = instructions_length + kDirectBranchLength;1187    uptr trampoline = AllocateMemoryForTrampoline(old_func, trampoline_length);1188    if (!trampoline)1189      return false;1190    if (!CopyInstructions(trampoline, old_func, instructions_length))1191      return false;1192    WriteDirectBranch(trampoline + instructions_length,1193                      old_func + instructions_length);1194    *orig_old_func = trampoline;1195  }1196 1197#if SANITIZER_WINDOWS641198  // Check if the targeted address can be encoded in the function padding.1199  // Otherwise, allocate it in the trampoline region.1200  if (IsMemoryPadding(old_func - kAddressLength, kAddressLength)) {1201    indirect_address = old_func - kAddressLength;1202    padding_length = kAddressLength;1203  } else {1204    indirect_address = AllocateMemoryForTrampoline(old_func, kAddressLength);1205    if (!indirect_address)1206      return false;1207  }1208#endif1209 1210  // Change memory protection to writable.1211  uptr patch_address = old_func - padding_length;1212  uptr patch_length = instructions_length + padding_length;1213  DWORD protection = 0;1214  if (!ChangeMemoryProtection(patch_address, patch_length, &protection))1215    return false;1216 1217  // Patch the original function.1218  WriteBranch(old_func, indirect_address, new_func);1219 1220  // Restore previous memory protection.1221  if (!RestoreMemoryProtection(patch_address, patch_length, protection))1222    return false;1223 1224  return true;1225}1226 1227bool OverrideFunction(1228    uptr old_func, uptr new_func, uptr *orig_old_func) {1229#if !SANITIZER_WINDOWS641230  if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func))1231    return true;1232#endif1233  if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func))1234    return true;1235  if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func))1236    return true;1237  if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func))1238    return true;1239  return false;1240}1241 1242static void **InterestingDLLsAvailable() {1243  static const char *InterestingDLLs[] = {1244    "kernel32.dll",1245    "msvcr100d.dll",      // VS20101246    "msvcr110d.dll",      // VS20121247    "msvcr120d.dll",      // VS20131248    "vcruntime140d.dll",  // VS20151249    "ucrtbased.dll",      // Universal CRT1250    "msvcr100.dll",       // VS20101251    "msvcr110.dll",       // VS20121252    "msvcr120.dll",       // VS20131253    "vcruntime140.dll",   // VS20151254    "ucrtbase.dll",       // Universal CRT1255#  if (defined(__MINGW32__) && defined(__i386__))1256    "libc++.dll",     // libc++1257    "libunwind.dll",  // libunwind1258#  endif1259    // NTDLL must go last as it gets special treatment in OverrideFunction.1260    "ntdll.dll",1261    NULL1262  };1263  static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 };1264  if (!result[0]) {1265    for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) {1266      if (HMODULE h = GetModuleHandleA(InterestingDLLs[i]))1267        result[j++] = (void *)h;1268    }1269  }1270  return &result[0];1271}1272 1273namespace {1274// Utility for reading loaded PE images.1275template <typename T> class RVAPtr {1276 public:1277  RVAPtr(void *module, uptr rva)1278      : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {}1279  operator T *() { return ptr_; }1280  T *operator->() { return ptr_; }1281  T *operator++() { return ++ptr_; }1282 1283 private:1284  T *ptr_;1285};1286} // namespace1287 1288// Internal implementation of GetProcAddress. At least since Windows 8,1289// GetProcAddress appears to initialize DLLs before returning function pointers1290// into them. This is problematic for the sanitizers, because they typically1291// want to intercept malloc *before* MSVCRT initializes. Our internal1292// implementation walks the export list manually without doing initialization.1293uptr InternalGetProcAddress(void *module, const char *func_name) {1294  // Check that the module header is full and present.1295  RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);1296  RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);1297  if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE ||  // "MZ"1298      headers->Signature != IMAGE_NT_SIGNATURE ||             // "PE\0\0"1299      headers->FileHeader.SizeOfOptionalHeader <1300          sizeof(IMAGE_OPTIONAL_HEADER)) {1301    return 0;1302  }1303 1304  IMAGE_DATA_DIRECTORY *export_directory =1305      &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];1306  if (export_directory->Size == 0)1307    return 0;1308  RVAPtr<IMAGE_EXPORT_DIRECTORY> exports(module,1309                                         export_directory->VirtualAddress);1310  RVAPtr<DWORD> functions(module, exports->AddressOfFunctions);1311  RVAPtr<DWORD> names(module, exports->AddressOfNames);1312  RVAPtr<WORD> ordinals(module, exports->AddressOfNameOrdinals);1313 1314  for (DWORD i = 0; i < exports->NumberOfNames; i++) {1315    RVAPtr<char> name(module, names[i]);1316    if (!_strcmp(func_name, name)) {1317      DWORD index = ordinals[i];1318      RVAPtr<char> func(module, functions[index]);1319 1320      // Handle forwarded functions.1321      DWORD offset = functions[index];1322      if (offset >= export_directory->VirtualAddress &&1323          offset < export_directory->VirtualAddress + export_directory->Size) {1324        // An entry for a forwarded function is a string with the following1325        // format: "<module> . <function_name>" that is stored into the1326        // exported directory.1327        char function_name[256];1328        size_t funtion_name_length = _strlen(func);1329        if (funtion_name_length >= sizeof(function_name) - 1) {1330          ReportError("interception_win: func too long: '%s'\n", (char *)func);1331          InterceptionFailed();1332        }1333 1334        _memcpy(function_name, func, funtion_name_length);1335        function_name[funtion_name_length] = '\0';1336        char* separator = _strchr(function_name, '.');1337        if (!separator) {1338          ReportError("interception_win: no separator in '%s'\n",1339                      function_name);1340          InterceptionFailed();1341        }1342        *separator = '\0';1343 1344        void* redirected_module = GetModuleHandleA(function_name);1345        if (!redirected_module) {1346          ReportError("interception_win: GetModuleHandleA failed for '%s'\n",1347                      function_name);1348          InterceptionFailed();1349        }1350        return InternalGetProcAddress(redirected_module, separator + 1);1351      }1352 1353      return (uptr)(char *)func;1354    }1355  }1356 1357  return 0;1358}1359 1360bool OverrideFunction(1361    const char *func_name, uptr new_func, uptr *orig_old_func) {1362  static const char *kNtDllIgnore[] = {1363    "memcmp", "memcpy", "memmove", "memset"1364  };1365 1366  bool hooked = false;1367  void **DLLs = InterestingDLLsAvailable();1368  for (size_t i = 0; DLLs[i]; ++i) {1369    if (DLLs[i + 1] == nullptr) {1370      // This is the last DLL, i.e. NTDLL. It exports some functions that1371      // we only want to override in the CRT.1372      for (const char *ignored : kNtDllIgnore) {1373        if (_strcmp(func_name, ignored) == 0)1374          return hooked;1375      }1376    }1377 1378    uptr func_addr = InternalGetProcAddress(DLLs[i], func_name);1379    if (func_addr &&1380        OverrideFunction(func_addr, new_func, orig_old_func)) {1381      hooked = true;1382    }1383  }1384  return hooked;1385}1386 1387bool OverrideImportedFunction(const char *module_to_patch,1388                              const char *imported_module,1389                              const char *function_name, uptr new_function,1390                              uptr *orig_old_func) {1391  HMODULE module = GetModuleHandleA(module_to_patch);1392  if (!module)1393    return false;1394 1395  // Check that the module header is full and present.1396  RVAPtr<IMAGE_DOS_HEADER> dos_stub(module, 0);1397  RVAPtr<IMAGE_NT_HEADERS> headers(module, dos_stub->e_lfanew);1398  if (!module || dos_stub->e_magic != IMAGE_DOS_SIGNATURE ||  // "MZ"1399      headers->Signature != IMAGE_NT_SIGNATURE ||             // "PE\0\0"1400      headers->FileHeader.SizeOfOptionalHeader <1401          sizeof(IMAGE_OPTIONAL_HEADER)) {1402    return false;1403  }1404 1405  IMAGE_DATA_DIRECTORY *import_directory =1406      &headers->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT];1407 1408  // Iterate the list of imported DLLs. FirstThunk will be null for the last1409  // entry.1410  RVAPtr<IMAGE_IMPORT_DESCRIPTOR> imports(module,1411                                          import_directory->VirtualAddress);1412  for (; imports->FirstThunk != 0; ++imports) {1413    RVAPtr<const char> modname(module, imports->Name);1414    if (_stricmp(&*modname, imported_module) == 0)1415      break;1416  }1417  if (imports->FirstThunk == 0)1418    return false;1419 1420  // We have two parallel arrays: the import address table (IAT) and the table1421  // of names. They start out containing the same data, but the loader rewrites1422  // the IAT to hold imported addresses and leaves the name table in1423  // OriginalFirstThunk alone.1424  RVAPtr<IMAGE_THUNK_DATA> name_table(module, imports->OriginalFirstThunk);1425  RVAPtr<IMAGE_THUNK_DATA> iat(module, imports->FirstThunk);1426  for (; name_table->u1.Ordinal != 0; ++name_table, ++iat) {1427    if (!IMAGE_SNAP_BY_ORDINAL(name_table->u1.Ordinal)) {1428      RVAPtr<IMAGE_IMPORT_BY_NAME> import_by_name(1429          module, name_table->u1.ForwarderString);1430      const char *funcname = &import_by_name->Name[0];1431      if (_strcmp(funcname, function_name) == 0)1432        break;1433    }1434  }1435  if (name_table->u1.Ordinal == 0)1436    return false;1437 1438  // Now we have the correct IAT entry. Do the swap. We have to make the page1439  // read/write first.1440  if (orig_old_func)1441    *orig_old_func = iat->u1.AddressOfData;1442  DWORD old_prot, unused_prot;1443  if (!VirtualProtect(&iat->u1.AddressOfData, 4, PAGE_EXECUTE_READWRITE,1444                      &old_prot))1445    return false;1446  iat->u1.AddressOfData = new_function;1447  if (!VirtualProtect(&iat->u1.AddressOfData, 4, old_prot, &unused_prot))1448    return false;  // Not clear if this failure bothers us.1449  return true;1450}1451 1452}  // namespace __interception1453 1454#endif  // SANITIZER_WINDOWS1455