Please use GitHub pull requests for new patches. Phabricator shutdown timeline
Changeset View
Changeset View
Standalone View
Standalone View
compiler-rt/lib/interception/interception_win.cpp
Show First 20 Lines • Show All 167 Lines • ▼ Show 20 Lines | |||||
static uptr RoundUpTo(uptr size, uptr boundary) { | static uptr RoundUpTo(uptr size, uptr boundary) { | ||||
return (size + boundary - 1) & ~(boundary - 1); | return (size + boundary - 1) & ~(boundary - 1); | ||||
} | } | ||||
// FIXME: internal_str* and internal_mem* functions should be moved from the | // FIXME: internal_str* and internal_mem* functions should be moved from the | ||||
// ASan sources into interception/. | // ASan sources into interception/. | ||||
static size_t _strlen(const char *str) { | static size_t _strlen(const char *str) { | ||||
const char* p = str; | const char *p = str; | ||||
while (*p != '\0') ++p; | while (*p != '\0') ++p; | ||||
return p - str; | return p - str; | ||||
} | } | ||||
static char* _strchr(char* str, char c) { | static char *_strchr(char *str, char c) { | ||||
while (*str) { | while (*str) { | ||||
if (*str == c) | if (*str == c) | ||||
return str; | return str; | ||||
++str; | ++str; | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
} | } | ||||
static void _memset(void *p, int value, size_t sz) { | static void _memset(void *p, int value, size_t sz) { | ||||
for (size_t i = 0; i < sz; ++i) | for (size_t i = 0; i < sz; ++i) ((char *)p)[i] = (char)value; | ||||
((char*)p)[i] = (char)value; | |||||
} | } | ||||
static void _memcpy(void *dst, void *src, size_t sz) { | static void _memcpy(void *dst, void *src, size_t sz) { | ||||
char *dst_c = (char*)dst, | char *dst_c = (char *)dst, *src_c = (char *)src; | ||||
*src_c = (char*)src; | for (size_t i = 0; i < sz; ++i) dst_c[i] = src_c[i]; | ||||
for (size_t i = 0; i < sz; ++i) | |||||
dst_c[i] = src_c[i]; | |||||
} | } | ||||
static bool ChangeMemoryProtection( | static bool ChangeMemoryProtection(uptr address, uptr size, | ||||
uptr address, uptr size, DWORD *old_protection) { | DWORD *old_protection) { | ||||
return ::VirtualProtect((void*)address, size, | return ::VirtualProtect((void *)address, size, PAGE_EXECUTE_READWRITE, | ||||
PAGE_EXECUTE_READWRITE, | |||||
old_protection) != FALSE; | old_protection) != FALSE; | ||||
} | } | ||||
static bool RestoreMemoryProtection( | static bool RestoreMemoryProtection(uptr address, uptr size, | ||||
uptr address, uptr size, DWORD old_protection) { | DWORD old_protection) { | ||||
DWORD unused; | DWORD unused; | ||||
return ::VirtualProtect((void*)address, size, | return ::VirtualProtect((void *)address, size, old_protection, &unused) != | ||||
old_protection, | FALSE; | ||||
&unused) != FALSE; | |||||
} | } | ||||
static bool IsMemoryPadding(uptr address, uptr size) { | static bool IsMemoryPadding(uptr address, uptr size) { | ||||
u8* function = (u8*)address; | u8 *function = (u8 *)address; | ||||
for (size_t i = 0; i < size; ++i) | for (size_t i = 0; i < size; ++i) | ||||
if (function[i] != 0x90 && function[i] != 0xCC) | if (function[i] != 0x90 && function[i] != 0xCC) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
static const u8 kHintNop8Bytes[] = { | static const u8 kHintNop8Bytes[] = {0x0F, 0x1F, 0x84, 0x00, | ||||
0x0F, 0x1F, 0x84, 0x00, 0x00, 0x00, 0x00, 0x00 | 0x00, 0x00, 0x00, 0x00}; | ||||
}; | |||||
template<class T> | template <class T> | ||||
static bool FunctionHasPrefix(uptr address, const T &pattern) { | static bool FunctionHasPrefix(uptr address, const T &pattern) { | ||||
u8* function = (u8*)address - sizeof(pattern); | u8 *function = (u8 *)address - sizeof(pattern); | ||||
for (size_t i = 0; i < sizeof(pattern); ++i) | for (size_t i = 0; i < sizeof(pattern); ++i) | ||||
if (function[i] != pattern[i]) | if (function[i] != pattern[i]) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
static bool FunctionHasPadding(uptr address, uptr size) { | static bool FunctionHasPadding(uptr address, uptr size) { | ||||
if (IsMemoryPadding(address - size, size)) | if (IsMemoryPadding(address - size, size)) | ||||
return true; | return true; | ||||
if (size <= sizeof(kHintNop8Bytes) && | if (size <= sizeof(kHintNop8Bytes) && | ||||
FunctionHasPrefix(address, kHintNop8Bytes)) | FunctionHasPrefix(address, kHintNop8Bytes)) | ||||
return true; | return true; | ||||
return false; | return false; | ||||
} | } | ||||
static void WritePadding(uptr from, uptr size) { | static void WritePadding(uptr from, uptr size) { | ||||
_memset((void*)from, 0xCC, (size_t)size); | _memset((void *)from, 0xCC, (size_t)size); | ||||
} | } | ||||
static void WriteJumpInstruction(uptr from, uptr target) { | static void WriteJumpInstruction(uptr from, uptr target) { | ||||
if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target)) | if (!DistanceIsWithin2Gig(from + kJumpInstructionLength, target)) | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
ptrdiff_t offset = target - from - kJumpInstructionLength; | ptrdiff_t offset = target - from - kJumpInstructionLength; | ||||
*(u8*)from = 0xE9; | *(u8 *)from = 0xE9; | ||||
*(u32*)(from + 1) = offset; | *(u32 *)(from + 1) = offset; | ||||
} | } | ||||
static void WriteShortJumpInstruction(uptr from, uptr target) { | static void WriteShortJumpInstruction(uptr from, uptr target) { | ||||
sptr offset = target - from - kShortJumpInstructionLength; | sptr offset = target - from - kShortJumpInstructionLength; | ||||
if (offset < -128 || offset > 127) | if (offset < -128 || offset > 127) | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
*(u8*)from = 0xEB; | *(u8 *)from = 0xEB; | ||||
*(u8*)(from + 1) = (u8)offset; | *(u8 *)(from + 1) = (u8)offset; | ||||
} | } | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) { | static void WriteIndirectJumpInstruction(uptr from, uptr indirect_target) { | ||||
// jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative | // jmp [rip + <offset>] = FF 25 <offset> where <offset> is a relative | ||||
// offset. | // offset. | ||||
// The offset is the distance from then end of the jump instruction to the | // The offset is the distance from then end of the jump instruction to the | ||||
// memory location containing the targeted address. The displacement is still | // memory location containing the targeted address. The displacement is still | ||||
// 32-bit in x64, so indirect_target must be located within +/- 2GB range. | // 32-bit in x64, so indirect_target must be located within +/- 2GB range. | ||||
int offset = indirect_target - from - kIndirectJumpInstructionLength; | int offset = indirect_target - from - kIndirectJumpInstructionLength; | ||||
if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength, | if (!DistanceIsWithin2Gig(from + kIndirectJumpInstructionLength, | ||||
indirect_target)) { | indirect_target)) { | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
} | } | ||||
*(u16*)from = 0x25FF; | *(u16 *)from = 0x25FF; | ||||
*(u32*)(from + 2) = offset; | *(u32 *)(from + 2) = offset; | ||||
} | } | ||||
#endif | #endif | ||||
static void WriteBranch( | static void WriteBranch(uptr from, uptr indirect_target, uptr target) { | ||||
uptr from, uptr indirect_target, uptr target) { | |||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
WriteIndirectJumpInstruction(from, indirect_target); | WriteIndirectJumpInstruction(from, indirect_target); | ||||
*(u64*)indirect_target = target; | *(u64 *)indirect_target = target; | ||||
#else | #else | ||||
(void)indirect_target; | (void)indirect_target; | ||||
WriteJumpInstruction(from, target); | WriteJumpInstruction(from, target); | ||||
#endif | #endif | ||||
} | } | ||||
static void WriteDirectBranch(uptr from, uptr target) { | static void WriteDirectBranch(uptr from, uptr target) { | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
Show All 17 Lines | |||||
static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion]; | static TrampolineMemoryRegion TrampolineRegions[kMaxTrampolineRegion]; | ||||
static void *AllocateTrampolineRegion(uptr image_address, size_t granularity) { | static void *AllocateTrampolineRegion(uptr image_address, size_t granularity) { | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
uptr address = image_address; | uptr address = image_address; | ||||
uptr scanned = 0; | uptr scanned = 0; | ||||
while (scanned < kTrampolineScanLimitRange) { | while (scanned < kTrampolineScanLimitRange) { | ||||
MEMORY_BASIC_INFORMATION info; | MEMORY_BASIC_INFORMATION info; | ||||
if (!::VirtualQuery((void*)address, &info, sizeof(info))) | if (!::VirtualQuery((void *)address, &info, sizeof(info))) | ||||
return nullptr; | return nullptr; | ||||
// Check whether a region can be allocated at |address|. | // Check whether a region can be allocated at |address|. | ||||
if (info.State == MEM_FREE && info.RegionSize >= granularity) { | if (info.State == MEM_FREE && info.RegionSize >= granularity) { | ||||
void *page = ::VirtualAlloc((void*)RoundUpTo(address, granularity), | void *page = | ||||
granularity, | ::VirtualAlloc((void *)RoundUpTo(address, granularity), granularity, | ||||
MEM_RESERVE | MEM_COMMIT, | MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); | ||||
PAGE_EXECUTE_READWRITE); | |||||
return page; | return page; | ||||
} | } | ||||
// Move to the next region. | // Move to the next region. | ||||
address = (uptr)info.BaseAddress + info.RegionSize; | address = (uptr)info.BaseAddress + info.RegionSize; | ||||
scanned += info.RegionSize; | scanned += info.RegionSize; | ||||
} | } | ||||
return nullptr; | return nullptr; | ||||
#else | #else | ||||
return ::VirtualAlloc(nullptr, | return ::VirtualAlloc(nullptr, granularity, MEM_RESERVE | MEM_COMMIT, | ||||
granularity, | |||||
MEM_RESERVE | MEM_COMMIT, | |||||
PAGE_EXECUTE_READWRITE); | PAGE_EXECUTE_READWRITE); | ||||
#endif | #endif | ||||
} | } | ||||
// Used by unittests to release mapped memory space. | // Used by unittests to release mapped memory space. | ||||
void TestOnlyReleaseTrampolineRegions() { | void TestOnlyReleaseTrampolineRegions() { | ||||
for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) { | for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) { | ||||
TrampolineMemoryRegion *current = &TrampolineRegions[bucket]; | TrampolineMemoryRegion *current = &TrampolineRegions[bucket]; | ||||
if (current->content == 0) | if (current->content == 0) | ||||
return; | return; | ||||
::VirtualFree((void*)current->content, 0, MEM_RELEASE); | ::VirtualFree((void *)current->content, 0, MEM_RELEASE); | ||||
current->content = 0; | current->content = 0; | ||||
} | } | ||||
} | } | ||||
static uptr AllocateMemoryForTrampoline(uptr image_address, size_t size) { | static uptr AllocateMemoryForTrampoline(uptr image_address, size_t size) { | ||||
// Find a region within 2G with enough space to allocate |size| bytes. | // Find a region within 2G with enough space to allocate |size| bytes. | ||||
TrampolineMemoryRegion *region = nullptr; | TrampolineMemoryRegion *region = nullptr; | ||||
for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) { | for (size_t bucket = 0; bucket < kMaxTrampolineRegion; ++bucket) { | ||||
TrampolineMemoryRegion* current = &TrampolineRegions[bucket]; | TrampolineMemoryRegion *current = &TrampolineRegions[bucket]; | ||||
if (current->content == 0) { | if (current->content == 0) { | ||||
// No valid region found, allocate a new region. | // No valid region found, allocate a new region. | ||||
size_t bucket_size = GetMmapGranularity(); | size_t bucket_size = GetMmapGranularity(); | ||||
void *content = AllocateTrampolineRegion(image_address, bucket_size); | void *content = AllocateTrampolineRegion(image_address, bucket_size); | ||||
if (content == nullptr) | if (content == nullptr) | ||||
return 0U; | return 0U; | ||||
current->content = (uptr)content; | current->content = (uptr)content; | ||||
Show All 23 Lines | #endif | ||||
uptr allocated_space = region->content + region->allocated_size; | uptr allocated_space = region->content + region->allocated_size; | ||||
region->allocated_size += size; | region->allocated_size += size; | ||||
WritePadding(allocated_space, size); | WritePadding(allocated_space, size); | ||||
return allocated_space; | return allocated_space; | ||||
} | } | ||||
// Returns 0 on error. | // Returns 0 on error. | ||||
static size_t GetInstructionSize(uptr address, size_t* rel_offset = nullptr) { | static size_t GetInstructionSize(uptr address, size_t *rel_offset = nullptr) { | ||||
switch (*(u64*)address) { | switch (*(u64 *)address) { | ||||
case 0x90909090909006EB: // stub: jmp over 6 x nop. | case 0x90909090909006EB: // stub: jmp over 6 x nop. | ||||
return 8; | return 8; | ||||
} | } | ||||
switch (*(u8*)address) { | switch (*(u8 *)address) { | ||||
case 0x90: // 90 : nop | case 0x90: // 90 : nop | ||||
return 1; | return 1; | ||||
case 0x50: // push eax / rax | case 0x50: // push eax / rax | ||||
case 0x51: // push ecx / rcx | case 0x51: // push ecx / rcx | ||||
case 0x52: // push edx / rdx | case 0x52: // push edx / rdx | ||||
case 0x53: // push ebx / rbx | case 0x53: // push ebx / rbx | ||||
case 0x54: // push esp / rsp | case 0x54: // push esp / rsp | ||||
Show All 29 Lines | switch (*(u8 *)address) { | ||||
case 0x7B: | case 0x7B: | ||||
case 0x7C: | case 0x7C: | ||||
case 0x7D: | case 0x7D: | ||||
case 0x7E: | case 0x7E: | ||||
case 0x7F: | case 0x7F: | ||||
return 0; | return 0; | ||||
} | } | ||||
switch (*(u16*)(address)) { | switch (*(u16 *)(address)) { | ||||
case 0x018A: // 8A 01 : mov al, byte ptr [ecx] | case 0x018A: // 8A 01 : mov al, byte ptr [ecx] | ||||
case 0xFF8B: // 8B FF : mov edi, edi | case 0xFF8B: // 8B FF : mov edi, edi | ||||
case 0xEC8B: // 8B EC : mov ebp, esp | case 0xEC8B: // 8B EC : mov ebp, esp | ||||
case 0xc889: // 89 C8 : mov eax, ecx | case 0xc889: // 89 C8 : mov eax, ecx | ||||
case 0xC18B: // 8B C1 : mov eax, ecx | case 0xC18B: // 8B C1 : mov eax, ecx | ||||
case 0xC033: // 33 C0 : xor eax, eax | case 0xC033: // 33 C0 : xor eax, eax | ||||
case 0xC933: // 33 C9 : xor ecx, ecx | case 0xC933: // 33 C9 : xor ecx, ecx | ||||
case 0xD233: // 33 D2 : xor edx, edx | case 0xD233: // 33 D2 : xor edx, edx | ||||
return 2; | return 2; | ||||
// Cannot overwrite control-instruction. Return 0 to indicate failure. | // Cannot overwrite control-instruction. Return 0 to indicate failure. | ||||
case 0x25FF: // FF 25 XX XX XX XX : jmp [XXXXXXXX] | case 0x25FF: // FF 25 XX XX XX XX : jmp [XXXXXXXX] | ||||
return 0; | return 0; | ||||
} | } | ||||
switch (0x00FFFFFF & *(u32*)address) { | switch (0x00FFFFFF & *(u32 *)address) { | ||||
case 0x24A48D: // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX] | case 0x24A48D: // 8D A4 24 XX XX XX XX : lea esp, [esp + XX XX XX XX] | ||||
return 7; | return 7; | ||||
} | } | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
switch (*(u8*)address) { | switch (*(u8 *)address) { | ||||
case 0xA1: // A1 XX XX XX XX XX XX XX XX : | case 0xA1: // A1 XX XX XX XX XX XX XX XX : | ||||
// movabs eax, dword ptr ds:[XXXXXXXX] | // movabs eax, dword ptr ds:[XXXXXXXX] | ||||
return 9; | return 9; | ||||
} | } | ||||
switch (*(u16*)address) { | switch (*(u16 *)address) { | ||||
case 0x5040: // push rax | case 0x5040: // push rax | ||||
case 0x5140: // push rcx | case 0x5140: // push rcx | ||||
case 0x5240: // push rdx | case 0x5240: // push rdx | ||||
case 0x5340: // push rbx | case 0x5340: // push rbx | ||||
case 0x5440: // push rsp | case 0x5440: // push rsp | ||||
case 0x5540: // push rbp | case 0x5540: // push rbp | ||||
case 0x5640: // push rsi | case 0x5640: // push rsi | ||||
case 0x5740: // push rdi | case 0x5740: // push rdi | ||||
case 0x5441: // push r12 | case 0x5441: // push r12 | ||||
case 0x5541: // push r13 | case 0x5541: // push r13 | ||||
case 0x5641: // push r14 | case 0x5641: // push r14 | ||||
case 0x5741: // push r15 | case 0x5741: // push r15 | ||||
case 0x9066: // Two-byte NOP | case 0x9066: // Two-byte NOP | ||||
return 2; | return 2; | ||||
case 0x058B: // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX] | case 0x058B: // 8B 05 XX XX XX XX : mov eax, dword ptr [XX XX XX XX] | ||||
if (rel_offset) | if (rel_offset) | ||||
*rel_offset = 2; | *rel_offset = 2; | ||||
return 6; | return 6; | ||||
} | } | ||||
switch (0x00FFFFFF & *(u32*)address) { | switch (0x00FFFFFF & *(u32 *)address) { | ||||
case 0xe58948: // 48 8b c4 : mov rbp, rsp | case 0xe58948: // 48 8b c4 : mov rbp, rsp | ||||
case 0xc18b48: // 48 8b c1 : mov rax, rcx | case 0xc18b48: // 48 8b c1 : mov rax, rcx | ||||
case 0xc48b48: // 48 8b c4 : mov rax, rsp | case 0xc48b48: // 48 8b c4 : mov rax, rsp | ||||
case 0xd9f748: // 48 f7 d9 : neg rcx | case 0xd9f748: // 48 f7 d9 : neg rcx | ||||
case 0xd12b48: // 48 2b d1 : sub rdx, rcx | case 0xd12b48: // 48 2b d1 : sub rdx, rcx | ||||
case 0x07c1f6: // f6 c1 07 : test cl, 0x7 | case 0x07c1f6: // f6 c1 07 : test cl, 0x7 | ||||
case 0xc98548: // 48 85 C9 : test rcx, rcx | case 0xc98548: // 48 85 C9 : test rcx, rcx | ||||
case 0xc0854d: // 4d 85 c0 : test r8, r8 | case 0xc0854d: // 4d 85 c0 : test r8, r8 | ||||
Show All 13 Lines | switch (0x00FFFFFF & *(u32 *)address) { | ||||
case 0xdc8b4c: // 4c 8b dc : mov r11, rsp | case 0xdc8b4c: // 4c 8b dc : mov r11, rsp | ||||
case 0xd18b4c: // 4c 8b d1 : mov r10, rcx | case 0xd18b4c: // 4c 8b d1 : mov r10, rcx | ||||
case 0xE0E483: // 83 E4 E0 : and esp, 0xFFFFFFE0 | case 0xE0E483: // 83 E4 E0 : and esp, 0xFFFFFFE0 | ||||
return 3; | return 3; | ||||
case 0xec8348: // 48 83 ec XX : sub rsp, XX | case 0xec8348: // 48 83 ec XX : sub rsp, XX | ||||
case 0xf88349: // 49 83 f8 XX : cmp r8, XX | case 0xf88349: // 49 83 f8 XX : cmp r8, XX | ||||
case 0x588948: // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx | case 0x588948: // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx | ||||
case 0x245489: // 89 54 24 XX : mov DWORD PTR[rsp + XX], edx | |||||
rnk: So far this seems like the only functional change in this file. | |||||
return 4; | return 4; | ||||
case 0xec8148: // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX | case 0xec8148: // 48 81 EC XX XX XX XX : sub rsp, XXXXXXXX | ||||
return 7; | return 7; | ||||
case 0x058b48: // 48 8b 05 XX XX XX XX : | case 0x058b48: // 48 8b 05 XX XX XX XX : | ||||
// mov rax, QWORD PTR [rip + XXXXXXXX] | // mov rax, QWORD PTR [rip + XXXXXXXX] | ||||
case 0x25ff48: // 48 ff 25 XX XX XX XX : | case 0x25ff48: // 48 ff 25 XX XX XX XX : | ||||
// rex.W jmp QWORD PTR [rip + XXXXXXXX] | // rex.W jmp QWORD PTR [rip + XXXXXXXX] | ||||
// Instructions having offset relative to 'rip' need offset adjustment. | // Instructions having offset relative to 'rip' need offset adjustment. | ||||
if (rel_offset) | if (rel_offset) | ||||
*rel_offset = 3; | *rel_offset = 3; | ||||
return 7; | return 7; | ||||
case 0x2444c7: // C7 44 24 XX YY YY YY YY | case 0x2444c7: // C7 44 24 XX YY YY YY YY | ||||
// mov dword ptr [rsp + XX], YYYYYYYY | // mov dword ptr [rsp + XX], YYYYYYYY | ||||
return 8; | return 8; | ||||
} | } | ||||
switch (*(u32*)(address)) { | switch (*(u32 *)(address)) { | ||||
case 0x24448b48: // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX] | case 0x24448b48: // 48 8b 44 24 XX : mov rax, QWORD ptr [rsp + XX] | ||||
case 0x246c8948: // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp | case 0x246c8948: // 48 89 6C 24 XX : mov QWORD ptr [rsp + XX], rbp | ||||
case 0x245c8948: // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx | case 0x245c8948: // 48 89 5c 24 XX : mov QWORD PTR [rsp + XX], rbx | ||||
case 0x24748948: // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi | case 0x24748948: // 48 89 74 24 XX : mov QWORD PTR [rsp + XX], rsi | ||||
case 0x244C8948: // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx | case 0x244C8948: // 48 89 4C 24 XX : mov QWORD PTR [rsp + XX], rcx | ||||
case 0x24548948: // 48 89 54 24 XX : mov QWORD PTR [rsp + XX], rdx | case 0x24548948: // 48 89 54 24 XX : mov QWORD PTR [rsp + XX], rdx | ||||
case 0x244c894c: // 4c 89 4c 24 XX : mov QWORD PTR [rsp + XX], r9 | case 0x244c894c: // 4c 89 4c 24 XX : mov QWORD PTR [rsp + XX], r9 | ||||
case 0x2444894c: // 4c 89 44 24 XX : mov QWORD PTR [rsp + XX], r8 | case 0x2444894c: // 4c 89 44 24 XX : mov QWORD PTR [rsp + XX], r8 | ||||
return 5; | return 5; | ||||
case 0x24648348: // 48 83 64 24 XX : and QWORD PTR [rsp + XX], YY | case 0x24648348: // 48 83 64 24 XX : and QWORD PTR [rsp + XX], YY | ||||
return 6; | return 6; | ||||
} | } | ||||
#else | #else | ||||
switch (*(u8*)address) { | switch (*(u8 *)address) { | ||||
case 0xA1: // A1 XX XX XX XX : mov eax, dword ptr ds:[XXXXXXXX] | case 0xA1: // A1 XX XX XX XX : mov eax, dword ptr ds:[XXXXXXXX] | ||||
return 5; | return 5; | ||||
} | } | ||||
switch (*(u16*)address) { | switch (*(u16 *)address) { | ||||
case 0x458B: // 8B 45 XX : mov eax, dword ptr [ebp + XX] | case 0x458B: // 8B 45 XX : mov eax, dword ptr [ebp + XX] | ||||
case 0x5D8B: // 8B 5D XX : mov ebx, dword ptr [ebp + XX] | case 0x5D8B: // 8B 5D XX : mov ebx, dword ptr [ebp + XX] | ||||
case 0x7D8B: // 8B 7D XX : mov edi, dword ptr [ebp + XX] | case 0x7D8B: // 8B 7D XX : mov edi, dword ptr [ebp + XX] | ||||
case 0xEC83: // 83 EC XX : sub esp, XX | case 0xEC83: // 83 EC XX : sub esp, XX | ||||
case 0x75FF: // FF 75 XX : push dword ptr [ebp + XX] | case 0x75FF: // FF 75 XX : push dword ptr [ebp + XX] | ||||
return 3; | return 3; | ||||
case 0xC1F7: // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX | case 0xC1F7: // F7 C1 XX YY ZZ WW : test ecx, WWZZYYXX | ||||
case 0x25FF: // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX] | case 0x25FF: // FF 25 XX YY ZZ WW : jmp dword ptr ds:[WWZZYYXX] | ||||
return 6; | return 6; | ||||
case 0x3D83: // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX | case 0x3D83: // 83 3D XX YY ZZ WW TT : cmp TT, WWZZYYXX | ||||
return 7; | return 7; | ||||
case 0x7D83: // 83 7D XX YY : cmp dword ptr [ebp + XX], YY | case 0x7D83: // 83 7D XX YY : cmp dword ptr [ebp + XX], YY | ||||
return 4; | return 4; | ||||
} | } | ||||
switch (0x00FFFFFF & *(u32*)address) { | switch (0x00FFFFFF & *(u32 *)address) { | ||||
case 0x24448A: // 8A 44 24 XX : mov eal, dword ptr [esp + XX] | case 0x24448A: // 8A 44 24 XX : mov eal, dword ptr [esp + XX] | ||||
case 0x24448B: // 8B 44 24 XX : mov eax, dword ptr [esp + XX] | case 0x24448B: // 8B 44 24 XX : mov eax, dword ptr [esp + XX] | ||||
case 0x244C8B: // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX] | case 0x244C8B: // 8B 4C 24 XX : mov ecx, dword ptr [esp + XX] | ||||
case 0x24548B: // 8B 54 24 XX : mov edx, dword ptr [esp + XX] | case 0x24548B: // 8B 54 24 XX : mov edx, dword ptr [esp + XX] | ||||
case 0x24748B: // 8B 74 24 XX : mov esi, dword ptr [esp + XX] | case 0x24748B: // 8B 74 24 XX : mov esi, dword ptr [esp + XX] | ||||
case 0x247C8B: // 8B 7C 24 XX : mov edi, dword ptr [esp + XX] | case 0x247C8B: // 8B 7C 24 XX : mov edi, dword ptr [esp + XX] | ||||
return 4; | return 4; | ||||
} | } | ||||
switch (*(u32*)address) { | switch (*(u32 *)address) { | ||||
case 0x2444B60F: // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX] | case 0x2444B60F: // 0F B6 44 24 XX : movzx eax, byte ptr [esp + XX] | ||||
return 5; | return 5; | ||||
} | } | ||||
#endif | #endif | ||||
// Unknown instruction! | // Unknown instruction! | ||||
// FIXME: Unknown instruction failures might happen when we add a new | // FIXME: Unknown instruction failures might happen when we add a new | ||||
// interceptor or a new compiler version. In either case, they should result | // interceptor or a new compiler version. In either case, they should result | ||||
Show All 15 Lines | static size_t RoundUpToInstrBoundary(size_t size, uptr address) { | ||||
return cursor; | return cursor; | ||||
} | } | ||||
static bool CopyInstructions(uptr to, uptr from, size_t size) { | static bool CopyInstructions(uptr to, uptr from, size_t size) { | ||||
size_t cursor = 0; | size_t cursor = 0; | ||||
while (cursor != size) { | while (cursor != size) { | ||||
size_t rel_offset = 0; | size_t rel_offset = 0; | ||||
size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset); | size_t instruction_size = GetInstructionSize(from + cursor, &rel_offset); | ||||
_memcpy((void*)(to + cursor), (void*)(from + cursor), | _memcpy((void *)(to + cursor), (void *)(from + cursor), | ||||
(size_t)instruction_size); | (size_t)instruction_size); | ||||
if (rel_offset) { | if (rel_offset) { | ||||
uptr delta = to - from; | uptr delta = to - from; | ||||
uptr relocated_offset = *(u32*)(to + cursor + rel_offset) - delta; | uptr relocated_offset = *(u32 *)(to + cursor + rel_offset) - delta; | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
if (relocated_offset + 0x80000000U >= 0xFFFFFFFFU) | if (relocated_offset + 0x80000000U >= 0xFFFFFFFFU) | ||||
return false; | return false; | ||||
#endif | #endif | ||||
*(u32*)(to + cursor + rel_offset) = relocated_offset; | *(u32 *)(to + cursor + rel_offset) = relocated_offset; | ||||
} | } | ||||
cursor += instruction_size; | cursor += instruction_size; | ||||
} | } | ||||
return true; | return true; | ||||
} | } | ||||
#if !SANITIZER_WINDOWS64 | #if !SANITIZER_WINDOWS64 | ||||
bool OverrideFunctionWithDetour( | bool OverrideFunctionWithDetour(uptr old_func, uptr new_func, | ||||
uptr old_func, uptr new_func, uptr *orig_old_func) { | uptr *orig_old_func) { | ||||
const int kDetourHeaderLen = 5; | const int kDetourHeaderLen = 5; | ||||
const u16 kDetourInstruction = 0xFF8B; | const u16 kDetourInstruction = 0xFF8B; | ||||
uptr header = (uptr)old_func - kDetourHeaderLen; | uptr header = (uptr)old_func - kDetourHeaderLen; | ||||
uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength; | uptr patch_length = kDetourHeaderLen + kShortJumpInstructionLength; | ||||
// Validate that the function is hookable. | // Validate that the function is hookable. | ||||
if (*(u16*)old_func != kDetourInstruction || | if (*(u16 *)old_func != kDetourInstruction || | ||||
!IsMemoryPadding(header, kDetourHeaderLen)) | !IsMemoryPadding(header, kDetourHeaderLen)) | ||||
return false; | return false; | ||||
// Change memory protection to writable. | // Change memory protection to writable. | ||||
DWORD protection = 0; | DWORD protection = 0; | ||||
if (!ChangeMemoryProtection(header, patch_length, &protection)) | if (!ChangeMemoryProtection(header, patch_length, &protection)) | ||||
return false; | return false; | ||||
Show All 9 Lines | bool OverrideFunctionWithDetour(uptr old_func, uptr new_func, | ||||
if (orig_old_func) | if (orig_old_func) | ||||
*orig_old_func = old_func + kShortJumpInstructionLength; | *orig_old_func = old_func + kShortJumpInstructionLength; | ||||
return true; | return true; | ||||
} | } | ||||
#endif | #endif | ||||
bool OverrideFunctionWithRedirectJump( | bool OverrideFunctionWithRedirectJump(uptr old_func, uptr new_func, | ||||
uptr old_func, uptr new_func, uptr *orig_old_func) { | uptr *orig_old_func) { | ||||
// Check whether the first instruction is a relative jump. | // Check whether the first instruction is a relative jump. | ||||
if (*(u8*)old_func != 0xE9) | if (*(u8 *)old_func != 0xE9) | ||||
return false; | return false; | ||||
if (orig_old_func) { | if (orig_old_func) { | ||||
uptr relative_offset = *(u32*)(old_func + 1); | uptr relative_offset = *(u32 *)(old_func + 1); | ||||
uptr absolute_target = old_func + relative_offset + kJumpInstructionLength; | uptr absolute_target = old_func + relative_offset + kJumpInstructionLength; | ||||
*orig_old_func = absolute_target; | *orig_old_func = absolute_target; | ||||
} | } | ||||
#if SANITIZER_WINDOWS64 | #if SANITIZER_WINDOWS64 | ||||
// If needed, get memory space for a trampoline jump. | // If needed, get memory space for a trampoline jump. | ||||
uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength); | uptr trampoline = AllocateMemoryForTrampoline(old_func, kDirectBranchLength); | ||||
if (!trampoline) | if (!trampoline) | ||||
Show All 11 Lines | #endif | ||||
// Restore previous memory protection. | // Restore previous memory protection. | ||||
if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection)) | if (!RestoreMemoryProtection(old_func, kJumpInstructionLength, protection)) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
bool OverrideFunctionWithHotPatch( | bool OverrideFunctionWithHotPatch(uptr old_func, uptr new_func, | ||||
uptr old_func, uptr new_func, uptr *orig_old_func) { | uptr *orig_old_func) { | ||||
const int kHotPatchHeaderLen = kBranchLength; | const int kHotPatchHeaderLen = kBranchLength; | ||||
uptr header = (uptr)old_func - kHotPatchHeaderLen; | uptr header = (uptr)old_func - kHotPatchHeaderLen; | ||||
uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength; | uptr patch_length = kHotPatchHeaderLen + kShortJumpInstructionLength; | ||||
// Validate that the function is hot patchable. | // Validate that the function is hot patchable. | ||||
size_t instruction_size = GetInstructionSize(old_func); | size_t instruction_size = GetInstructionSize(old_func); | ||||
if (instruction_size < kShortJumpInstructionLength || | if (instruction_size < kShortJumpInstructionLength || | ||||
Show All 32 Lines | #endif | ||||
// Restore previous memory protection. | // Restore previous memory protection. | ||||
if (!RestoreMemoryProtection(header, patch_length, protection)) | if (!RestoreMemoryProtection(header, patch_length, protection)) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
bool OverrideFunctionWithTrampoline( | bool OverrideFunctionWithTrampoline(uptr old_func, uptr new_func, | ||||
uptr old_func, uptr new_func, uptr *orig_old_func) { | uptr *orig_old_func) { | ||||
size_t instructions_length = kBranchLength; | size_t instructions_length = kBranchLength; | ||||
size_t padding_length = 0; | size_t padding_length = 0; | ||||
uptr indirect_address = 0; | uptr indirect_address = 0; | ||||
if (orig_old_func) { | if (orig_old_func) { | ||||
// Find out the number of bytes of the instructions we need to copy | // Find out the number of bytes of the instructions we need to copy | ||||
// to the trampoline. | // to the trampoline. | ||||
instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func); | instructions_length = RoundUpToInstrBoundary(kBranchLength, old_func); | ||||
Show All 37 Lines | #endif | ||||
// Restore previous memory protection. | // Restore previous memory protection. | ||||
if (!RestoreMemoryProtection(patch_address, patch_length, protection)) | if (!RestoreMemoryProtection(patch_address, patch_length, protection)) | ||||
return false; | return false; | ||||
return true; | return true; | ||||
} | } | ||||
bool OverrideFunction( | bool OverrideFunction(uptr old_func, uptr new_func, uptr *orig_old_func) { | ||||
uptr old_func, uptr new_func, uptr *orig_old_func) { | |||||
#if !SANITIZER_WINDOWS64 | #if !SANITIZER_WINDOWS64 | ||||
if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func)) | if (OverrideFunctionWithDetour(old_func, new_func, orig_old_func)) | ||||
return true; | return true; | ||||
#endif | #endif | ||||
if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func)) | if (OverrideFunctionWithRedirectJump(old_func, new_func, orig_old_func)) | ||||
return true; | return true; | ||||
if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func)) | if (OverrideFunctionWithHotPatch(old_func, new_func, orig_old_func)) | ||||
return true; | return true; | ||||
if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func)) | if (OverrideFunctionWithTrampoline(old_func, new_func, orig_old_func)) | ||||
return true; | return true; | ||||
return false; | return false; | ||||
} | } | ||||
static void **InterestingDLLsAvailable() { | static void **InterestingDLLsAvailable() { | ||||
static const char *InterestingDLLs[] = { | static const char *InterestingDLLs[] = { | ||||
"kernel32.dll", | "kernel32.dll", | ||||
"msvcr100.dll", // VS2010 | "msvcr100.dll", // VS2010 | ||||
"msvcr110.dll", // VS2012 | "msvcr110.dll", // VS2012 | ||||
"msvcr120.dll", // VS2013 | "msvcr120.dll", // VS2013 | ||||
"vcruntime140.dll", // VS2015 | "vcruntime140.dll", // VS2015 | ||||
"ucrtbase.dll", // Universal CRT | "ucrtbase.dll", // Universal CRT | ||||
"KERNELBASE.dll", // KernelBase for GlobalAlloc and LocalAlloc (dynamic) | |||||
// NTDLL should go last as it exports some functions that we should | // NTDLL should go last as it exports some functions that we should | ||||
// override in the CRT [presumably only used internally]. | // override in the CRT [presumably only used internally]. | ||||
"ntdll.dll", NULL}; | "ntdll.dll", NULL}; | ||||
static void *result[ARRAY_SIZE(InterestingDLLs)] = { 0 }; | static void *result[ARRAY_SIZE(InterestingDLLs)] = {0}; | ||||
if (!result[0]) { | if (!result[0]) { | ||||
for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) { | for (size_t i = 0, j = 0; InterestingDLLs[i]; ++i) { | ||||
if (HMODULE h = GetModuleHandleA(InterestingDLLs[i])) | if (HMODULE h = GetModuleHandleA(InterestingDLLs[i])) | ||||
result[j++] = (void *)h; | result[j++] = (void *)h; | ||||
} | } | ||||
} | } | ||||
return &result[0]; | return &result[0]; | ||||
} | } | ||||
namespace { | namespace { | ||||
// Utility for reading loaded PE images. | // Utility for reading loaded PE images. | ||||
template <typename T> class RVAPtr { | template <typename T> | ||||
class RVAPtr { | |||||
public: | public: | ||||
RVAPtr(void *module, uptr rva) | RVAPtr(void *module, uptr rva) | ||||
: ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {} | : ptr_(reinterpret_cast<T *>(reinterpret_cast<char *>(module) + rva)) {} | ||||
operator T *() { return ptr_; } | operator T *() { return ptr_; } | ||||
T *operator->() { return ptr_; } | T *operator->() { return ptr_; } | ||||
T *operator++() { return ++ptr_; } | T *operator++() { return ++ptr_; } | ||||
private: | private: | ||||
▲ Show 20 Lines • Show All 42 Lines • ▼ Show 20 Lines | if (!strcmp(func_name, name)) { | ||||
// exported directory. | // exported directory. | ||||
char function_name[256]; | char function_name[256]; | ||||
size_t funtion_name_length = _strlen(func); | size_t funtion_name_length = _strlen(func); | ||||
if (funtion_name_length >= sizeof(function_name) - 1) | if (funtion_name_length >= sizeof(function_name) - 1) | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
_memcpy(function_name, func, funtion_name_length); | _memcpy(function_name, func, funtion_name_length); | ||||
function_name[funtion_name_length] = '\0'; | function_name[funtion_name_length] = '\0'; | ||||
char* separator = _strchr(function_name, '.'); | char *separator = _strchr(function_name, '.'); | ||||
if (!separator) | if (!separator) | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
*separator = '\0'; | *separator = '\0'; | ||||
void* redirected_module = GetModuleHandleA(function_name); | void *redirected_module = GetModuleHandleA(function_name); | ||||
if (!redirected_module) | if (!redirected_module) | ||||
InterceptionFailed(); | InterceptionFailed(); | ||||
return InternalGetProcAddress(redirected_module, separator + 1); | return InternalGetProcAddress(redirected_module, separator + 1); | ||||
} | } | ||||
return (uptr)(char *)func; | return (uptr)(char *)func; | ||||
} | } | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
bool OverrideFunction( | bool OverrideFunction(const char *func_name, uptr new_func, | ||||
const char *func_name, uptr new_func, uptr *orig_old_func) { | uptr *orig_old_func) { | ||||
bool hooked = false; | bool hooked = false; | ||||
void **DLLs = InterestingDLLsAvailable(); | void **DLLs = InterestingDLLsAvailable(); | ||||
for (size_t i = 0; DLLs[i]; ++i) { | for (size_t i = 0; DLLs[i]; ++i) { | ||||
uptr func_addr = InternalGetProcAddress(DLLs[i], func_name); | uptr func_addr = InternalGetProcAddress(DLLs[i], func_name); | ||||
if (func_addr && | if (func_addr && OverrideFunction(func_addr, new_func, orig_old_func)) { | ||||
OverrideFunction(func_addr, new_func, orig_old_func)) { | |||||
hooked = true; | hooked = true; | ||||
} | } | ||||
} | } | ||||
return hooked; | return hooked; | ||||
} | } | ||||
bool OverrideImportedFunction(const char *module_to_patch, | bool OverrideImportedFunction(const char *module_to_patch, | ||||
const char *imported_module, | const char *imported_module, | ||||
▲ Show 20 Lines • Show All 66 Lines • Show Last 20 Lines |
So far this seems like the only functional change in this file.