//===-- interception_win_test.cc ------------------------------------------===// // // 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 ThreadSanitizer/AddressSanitizer runtime. // Tests for interception_win.h. // //===----------------------------------------------------------------------===// #include "interception/interception.h" #include "gtest/gtest.h" // Too slow for debug build #if !SANITIZER_DEBUG #if SANITIZER_WINDOWS #define WIN32_LEAN_AND_MEAN #include namespace __interception { namespace { enum FunctionPrefixKind { FunctionPrefixNone, FunctionPrefixPadding, FunctionPrefixHotPatch, FunctionPrefixDetour, }; typedef bool (*TestOverrideFunction)(uptr, uptr, uptr*); typedef int (*IdentityFunction)(int); #if SANITIZER_WINDOWS64 const u8 kIdentityCodeWithPrologue[] = { 0x55, // push rbp 0x48, 0x89, 0xE5, // mov rbp,rsp 0x8B, 0xC1, // mov eax,ecx 0x5D, // pop rbp 0xC3, // ret }; const u8 kIdentityCodeWithPushPop[] = { 0x55, // push rbp 0x48, 0x89, 0xE5, // mov rbp,rsp 0x53, // push rbx 0x50, // push rax 0x58, // pop rax 0x8B, 0xC1, // mov rax,rcx 0x5B, // pop rbx 0x5D, // pop rbp 0xC3, // ret }; const u8 kIdentityTwiceOffset = 16; const u8 kIdentityTwice[] = { 0x55, // push rbp 0x48, 0x89, 0xE5, // mov rbp,rsp 0x8B, 0xC1, // mov eax,ecx 0x5D, // pop rbp 0xC3, // ret 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x90, 0x55, // push rbp 0x48, 0x89, 0xE5, // mov rbp,rsp 0x8B, 0xC1, // mov eax,ecx 0x5D, // pop rbp 0xC3, // ret }; const u8 kIdentityCodeWithMov[] = { 0x89, 0xC8, // mov eax, ecx 0xC3, // ret }; const u8 kIdentityCodeWithJump[] = { 0xE9, 0x04, 0x00, 0x00, 0x00, // jmp + 4 0xCC, 0xCC, 0xCC, 0xCC, 0x89, 0xC8, // mov eax, ecx 0xC3, // ret }; #else const u8 kIdentityCodeWithPrologue[] = { 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8] 0x5D, // pop ebp 0xC3, // ret }; const u8 kIdentityCodeWithPushPop[] = { 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x53, // push ebx 0x50, // push eax 0x58, // pop eax 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8] 0x5B, // pop ebx 0x5D, // pop ebp 0xC3, // ret }; const u8 kIdentityTwiceOffset = 8; const u8 kIdentityTwice[] = { 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8] 0x5D, // pop ebp 0xC3, // ret 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x8B, 0x45, 0x08, // mov eax,dword ptr [ebp + 8] 0x5D, // pop ebp 0xC3, // ret }; const u8 kIdentityCodeWithMov[] = { 0x8B, 0x44, 0x24, 0x04, // mov eax,dword ptr [esp + 4] 0xC3, // ret }; const u8 kIdentityCodeWithJump[] = { 0xE9, 0x04, 0x00, 0x00, 0x00, // jmp + 4 0xCC, 0xCC, 0xCC, 0xCC, 0x8B, 0x44, 0x24, 0x04, // mov eax,dword ptr [esp + 4] 0xC3, // ret }; #endif const u8 kPatchableCode1[] = { 0xB8, 0x4B, 0x00, 0x00, 0x00, // mov eax,4B 0x33, 0xC9, // xor ecx,ecx 0xC3, // ret }; const u8 kPatchableCode2[] = { 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x33, 0xC0, // xor eax,eax 0x5D, // pop ebp 0xC3, // ret }; const u8 kPatchableCode3[] = { 0x55, // push ebp 0x8B, 0xEC, // mov ebp,esp 0x6A, 0x00, // push 0 0xE8, 0x3D, 0xFF, 0xFF, 0xFF, // call }; const u8 kPatchableCode4[] = { 0xE9, 0xCC, 0xCC, 0xCC, 0xCC, // jmp