//===-- interception_linux.cc -----------------------------------*- C++ -*-===// // // The LLVM Compiler Infrastructure // // This file is distributed under the University of Illinois Open Source // License. See LICENSE.TXT for details. // //===----------------------------------------------------------------------===// // // This file is a part of AddressSanitizer, an address sanity checker. // // Windows-specific interception methods. //===----------------------------------------------------------------------===// #ifdef _WIN32 #include "interception.h" #define WIN32_LEAN_AND_MEAN #include namespace __interception { // FIXME: internal_str* and internal_mem* functions should be moved from the // ASan sources into interception/. static void _memset(void *p, int value, size_t sz) { for (size_t i = 0; i < sz; ++i) ((char*)p)[i] = (char)value; } static void _memcpy(void *dst, void *src, size_t sz) { char *dst_c = (char*)dst, *src_c = (char*)src; for (size_t i = 0; i < sz; ++i) dst_c[i] = src_c[i]; } #if SANITIZER_WINDOWS64 static void WriteIndirectJumpInstruction(char *jmp_from, uptr *indirect_target) { // NOLINT // jmp [rip + XXYYZZWW] = FF 25 WW ZZ YY XX, where // XXYYZZWW is an offset from jmp_from. // The displacement is still 32-bit in x64, so indirect_target must be located // within +/- 2GB range. int offset = (int)(indirect_target - (uptr *)jmp_from); jmp_from[0] = '\xFF'; jmp_from[1] = '\x25'; *(int*)(jmp_from + 2) = offset; } #else static void WriteJumpInstruction(char *jmp_from, char *to) { // jmp XXYYZZWW = E9 WW ZZ YY XX, where XXYYZZWW is an offset from jmp_from // to the next instruction to the destination. ptrdiff_t offset = to - jmp_from - 5; *jmp_from = '\xE9'; *(ptrdiff_t*)(jmp_from + 1) = offset; } #endif static void WriteTrampolineJumpInstruction(char *jmp_from, char *to) { #if SANITIZER_WINDOWS64 // Emit an indirect jump through immediately following bytes: // jmp_from: // jmp [rip + 6] // .quad to // Store the address. uptr *indirect_target = (uptr *)(jmp_from + 6); *indirect_target = (uptr)to; // Write the indirect jump. WriteIndirectJumpInstruction(jmp_from, indirect_target); #else WriteJumpInstruction(jmp_from, to); #endif } static void WriteInterceptorJumpInstruction(char *jmp_from, char *to) { #if SANITIZER_WINDOWS64 // Emit an indirect jump through immediately following bytes: // jmp_from: // jmp [rip - 8] // .quad to // Store the address. uptr *indirect_target = (uptr *)(jmp_from - 8); *indirect_target = (uptr)to; // Write the indirect jump. WriteIndirectJumpInstruction(jmp_from, indirect_target); #else WriteJumpInstruction(jmp_from, to); #endif } static char *GetMemoryForTrampoline(size_t size) { // Trampolines are allocated from a common pool. const int POOL_SIZE = 1024; static char *pool = NULL; static size_t pool_used = 0; if (!pool) { pool = (char *)VirtualAlloc(NULL, POOL_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_EXECUTE_READWRITE); // FIXME: Might want to apply PAGE_EXECUTE_READ access after all the // interceptors are in place. if (!pool) return NULL; _memset(pool, 0xCC /* int 3 */, POOL_SIZE); } if (pool_used + size > POOL_SIZE) return NULL; char *ret = pool + pool_used; pool_used += size; return ret; } // Returns 0 on error. static size_t RoundUpToInstrBoundary(size_t size, char *code) { #if SANITIZER_WINDOWS64 // Win64 RoundUpToInstrBoundary is a work in progress. size_t cursor = 0; while (cursor < size) { switch (code[cursor]) { case '\x57': // 57 : push rdi cursor++; continue; case '\x90': // 90 : nop cursor++; continue; case '\xb8': // b8 XX XX XX XX : mov eax, XX XX XX XX cursor += 5; continue; } switch (*(u16*)(code + cursor)) { // NOLINT case 0x5540: // 40 55 : rex push rbp case 0x5340: // 40 53 : rex push rbx cursor += 2; continue; } switch (0x00FFFFFF & *(u32*)(code + cursor)) { case 0xc18b48: // 48 8b c1 : mov rax, rcx case 0xc48b48: // 48 8b c4 : mov rax, rsp case 0xd9f748: // 48 f7 d9 : neg rcx case 0xd12b48: // 48 2b d1 : sub rdx, rcx case 0x07c1f6: // f6 c1 07 : test cl, 0x7 case 0xc0854d: // 4d 85 c0 : test r8, r8 case 0xc2b60f: // 0f b6 c2 : movzx eax, dl case 0xc03345: // 45 33 c0 : xor r8d, r8d case 0xd98b4c: // 4c 8b d9 : mov r11, rcx case 0xd28b4c: // 4c 8b d2 : mov r10, rdx case 0xd2b60f: // 0f b6 d2 : movzx edx, dl case 0xca2b48: // 48 2b ca : sub rcx, rdx case 0x10b70f: // 0f b7 10 : movzx edx, WORD PTR [rax] case 0xc00b4d: // 3d 0b c0 : or r8, r8 case 0xd18b48: // 48 8b d1 : mov rdx, rcx case 0xdc8b4c: // 4c 8b dc : mov r11,rsp case 0xd18b4c: // 4c 8b d1 : mov r10, rcx cursor += 3; continue; case 0xec8348: // 48 83 ec XX : sub rsp, 0xXX case 0xf88349: // 49 83 f8 XX : cmp r8, XX case 0x588948: // 48 89 58 XX : mov QWORD PTR[rax + XX], rbx cursor += 4; continue; case 0x058b48: // 48 8b 05 XX XX XX XX // = mov rax, QWORD PTR [rip+ 0xXXXXXXXX] case 0x25ff48: // 48 ff 25 XX XX XX XX // = rex.W jmp QWORD PTR [rip + 0xXXXXXXXX] cursor += 7; continue; } switch (*(u32*)(code + cursor)) { case 0x24448b48: // 48 8b 44 24 XX : mov rax, qword ptr [rsp + 0xXX] cursor += 5; continue; } // Check first 5 bytes. switch (0xFFFFFFFFFFull & *(u64*)(code + cursor)) { case 0x08245c8948: // 48 89 5c 24 08 : mov QWORD PTR [rsp+0x8], rbx case 0x1024748948: // 48 89 74 24 10 : mov QWORD PTR [rsp+0x10], rsi cursor += 5; continue; } // Check 8 bytes. switch (*(u64*)(code + cursor)) { case 0x90909090909006EBull: // JMP +6, 6x NOP cursor += 8; continue; } // Unknown instructions! __debugbreak(); } return cursor; #else size_t cursor = 0; while (cursor < size) { switch (code[cursor]) { case '\xE8': // E8 XX XX XX XX = call case '\xE9': // E9 XX XX XX XX = jmp