diff options
| author | ARKALI <31196273+arkali@users.noreply.github.com> | 2026-11-23 22:32:01 +0100 |
|---|---|---|
| committer | ARKALI <31196273+arkali@users.noreply.github.com> | 2026-11-23 22:32:01 +0100 |
| commit | e063e5ead623a6a713bf1c8804b0518c33b121a5 (patch) | |
| tree | 370623b94b53dc405aa98194c92c78f6b832175f /runtime/src/code_hooks.h | |
| parent | 5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff) | |
code_hooks: unwind data for the thunks, so exceptions and stack walks pass through them
Diffstat (limited to 'runtime/src/code_hooks.h')
| -rw-r--r-- | runtime/src/code_hooks.h | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/runtime/src/code_hooks.h b/runtime/src/code_hooks.h index 147a84830..c87f1b2b1 100644 --- a/runtime/src/code_hooks.h +++ b/runtime/src/code_hooks.h | |||
| @@ -42,6 +42,41 @@ inline uint8_t* alloc_near(const void* close_to, std::size_t size) { | |||
| 42 | return nullptr; | 42 | return nullptr; |
| 43 | } | 43 | } |
| 44 | 44 | ||
| 45 | // A thunk's page: its code, then the unwind data Windows needs to walk the stack through it. Without that data the | ||
| 46 | // unwinder takes the thunk for a leaf function and reads the return address from the wrong place, so an exception | ||
| 47 | // raised below it (in the hooked function or the callback) finds no handler and ends the process, and a stack trace | ||
| 48 | // stops there. | ||
| 49 | constexpr DWORD kThunkUnwindOffset = 0x200; | ||
| 50 | constexpr DWORD kThunkFunctionOffset = 0x240; | ||
| 51 | |||
| 52 | // Seals a thunk of `code_size` bytes: writes its UNWIND_INFO (`unwind`: version, prolog size, code count and codes) and | ||
| 53 | // RUNTIME_FUNCTION, makes the page executable and registers the function table. Frees the page and returns null on | ||
| 54 | // failure. | ||
| 55 | inline uint8_t* finish_thunk(uint8_t* t, std::size_t code_size, std::initializer_list<uint8_t> unwind) { | ||
| 56 | std::size_t i = 0; | ||
| 57 | for (uint8_t b : unwind) t[kThunkUnwindOffset + i++] = b; | ||
| 58 | auto* function = reinterpret_cast<RUNTIME_FUNCTION*>(t + kThunkFunctionOffset); | ||
| 59 | function->BeginAddress = 0; | ||
| 60 | function->EndAddress = static_cast<DWORD>(code_size); | ||
| 61 | function->UnwindData = kThunkUnwindOffset; | ||
| 62 | DWORD old = 0; | ||
| 63 | if (!VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old) || | ||
| 64 | !RtlAddFunctionTable(function, 1, reinterpret_cast<DWORD64>(t))) { | ||
| 65 | VirtualFree(t, 0, MEM_RELEASE); | ||
| 66 | return nullptr; | ||
| 67 | } | ||
| 68 | FlushInstructionCache(GetCurrentProcess(), t, code_size); | ||
| 69 | return t; | ||
| 70 | } | ||
| 71 | |||
| 72 | // Frees a thunk that nothing points at. Its function table goes first: a table left behind would send every later | ||
| 73 | // unwind through freed memory. | ||
| 74 | inline void free_thunk(uint8_t* thunk) { | ||
| 75 | if (!thunk) return; | ||
| 76 | RtlDeleteFunctionTable(reinterpret_cast<RUNTIME_FUNCTION*>(thunk + kThunkFunctionOffset)); | ||
| 77 | VirtualFree(thunk, 0, MEM_RELEASE); | ||
| 78 | } | ||
| 79 | |||
| 45 | // The thunk: argument `arg` (0 = rcx, 1 = rdx; the low 32 bits) goes through `fn`, then the original runs with every | 80 | // The thunk: argument `arg` (0 = rcx, 1 = rdx; the low 32 bits) goes through `fn`, then the original runs with every |
| 46 | // other argument register as the caller left it. Stack: the caller's call leaves rsp = 8 mod 16; four pushes and | 81 | // other argument register as the caller left it. Stack: the caller's call leaves rsp = 8 mod 16; four pushes and |
| 47 | // 0x68 bytes make it 16-aligned for the call to `fn`, with its 32-byte shadow space and room for xmm0-3. | 82 | // 0x68 bytes make it 16-aligned for the call to `fn`, with its 32-byte shadow space and room for xmm0-3. |
| @@ -70,13 +105,9 @@ inline uint8_t* make_remap_thunk(const void* close_to, int arg, RemapFn fn, cons | |||
| 70 | put({0x89, static_cast<uint8_t>(arg == 0 ? 0xC1 : 0xC2)}); // mov ecx|edx, eax (the remapped value) | 105 | put({0x89, static_cast<uint8_t>(arg == 0 ? 0xC1 : 0xC2)}); // mov ecx|edx, eax (the remapped value) |
| 71 | put({0x48, 0xB8}); put64(reinterpret_cast<uint64_t>(original)); // mov rax, original | 106 | put({0x48, 0xB8}); put64(reinterpret_cast<uint64_t>(original)); // mov rax, original |
| 72 | put({0xFF, 0xE0}); // jmp rax | 107 | put({0xFF, 0xE0}); // jmp rax |
| 73 | DWORD old = 0; | 108 | // UNWIND_INFO version 1, 10-byte prolog, five UWOP_ALLOC_SMALL codes (size / 8 - 1 in the high nibble), the last |
| 74 | if (!VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old)) { | 109 | // prolog instruction first: sub rsp,0x68 at 10, then 8 bytes for each push (the pushed registers are volatile). |
| 75 | VirtualFree(t, 0, MEM_RELEASE); | 110 | return finish_thunk(t, p - t, {0x01, 10, 5, 0, 10, 0xC2, 6, 0x02, 4, 0x02, 2, 0x02, 1, 0x02, 0, 0}); |
| 76 | return nullptr; | ||
| 77 | } | ||
| 78 | FlushInstructionCache(GetCurrentProcess(), t, p - t); | ||
| 79 | return t; | ||
| 80 | } | 111 | } |
| 81 | 112 | ||
| 82 | using ResultFn = uint32_t (*)(uint32_t result, uint32_t arg0); | 113 | using ResultFn = uint32_t (*)(uint32_t result, uint32_t arg0); |
| @@ -102,13 +133,8 @@ inline uint8_t* make_result_thunk(const void* close_to, ResultFn fn, const void* | |||
| 102 | put({0xFF, 0xD0}); // call rax | 133 | put({0xFF, 0xD0}); // call rax |
| 103 | put({0x48, 0x83, 0xC4, 0x20}); // add rsp, 0x20 | 134 | put({0x48, 0x83, 0xC4, 0x20}); // add rsp, 0x20 |
| 104 | put({0x59, 0xC3}); // pop rcx ; ret | 135 | put({0x59, 0xC3}); // pop rcx ; ret |
| 105 | DWORD old = 0; | 136 | // As above: 5-byte prolog, sub rsp,0x20 at 5, push rcx at 1. |
| 106 | if (!VirtualProtect(t, 4096, PAGE_EXECUTE_READ, &old)) { | 137 | return finish_thunk(t, p - t, {0x01, 5, 2, 0, 5, 0x32, 1, 0x02}); |
| 107 | VirtualFree(t, 0, MEM_RELEASE); | ||
| 108 | return nullptr; | ||
| 109 | } | ||
| 110 | FlushInstructionCache(GetCurrentProcess(), t, p - t); | ||
| 111 | return t; | ||
| 112 | } | 138 | } |
| 113 | 139 | ||
| 114 | // Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. | 140 | // Points every E8/E9 rel32 in [begin, begin + size) whose target is `target` at `thunk`. The range must be writable. |
| @@ -194,7 +220,7 @@ inline HookResult redirect_image_calls(uint8_t* base, uint32_t target_rva, MakeT | |||
| 194 | const std::size_t size = static_cast<std::size_t>(len); | 220 | const std::size_t size = static_cast<std::size_t>(len); |
| 195 | DWORD old = 0; | 221 | DWORD old = 0; |
| 196 | if (!VirtualProtect(begin, size, PAGE_EXECUTE_READWRITE, &old)) { | 222 | if (!VirtualProtect(begin, size, PAGE_EXECUTE_READWRITE, &old)) { |
| 197 | if (r.redirected == 0) VirtualFree(thunk, 0, MEM_RELEASE); | 223 | if (r.redirected == 0) free_thunk(thunk); |
| 198 | return r.error = "VirtualProtect failed", r; | 224 | return r.error = "VirtualProtect failed", r; |
| 199 | } | 225 | } |
| 200 | r.redirected += redirect_direct_branches(begin, size, target, thunk); | 226 | r.redirected += redirect_direct_branches(begin, size, target, thunk); |