upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/code_hooks_test.cpp
diff options
context:
space:
mode:
authorARKALI <31196273+arkali@users.noreply.github.com>2026-11-23 22:32:01 +0100
committerARKALI <31196273+arkali@users.noreply.github.com>2026-11-23 22:32:01 +0100
commite063e5ead623a6a713bf1c8804b0518c33b121a5 (patch)
tree370623b94b53dc405aa98194c92c78f6b832175f /runtime/test/code_hooks_test.cpp
parent5d74c272363f2703265d44e6aaab1a1df8ddfaf3 (diff)
code_hooks: unwind data for the thunks, so exceptions and stack walks pass through them
Diffstat (limited to 'runtime/test/code_hooks_test.cpp')
-rw-r--r--runtime/test/code_hooks_test.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp
index cb8ce1727..39efb9be1 100644
--- a/runtime/test/code_hooks_test.cpp
+++ b/runtime/test/code_hooks_test.cpp
@@ -5,6 +5,7 @@
5 5
6#include <cstdio> 6#include <cstdio>
7#include <cstring> 7#include <cstring>
8#include <stdexcept>
8#include <vector> 9#include <vector>
9 10
10namespace { 11namespace {
@@ -56,6 +57,59 @@ void init_pe64(uint8_t* img, uint32_t size_of_image, const std::vector<IMAGE_SEC
56 for (std::size_t i = 0; i < sections.size(); ++i) sec[i] = sections[i]; 57 for (std::size_t i = 0; i < sections.size(); ++i) sec[i] = sections[i];
57} 58}
58 59
60// Real functions for the unwinding checks: an exception leaves the hooked function, or the callback.
61uint32_t throw_on_seven(uint32_t value) {
62 if (value == 7) throw std::runtime_error("seven");
63 return value + 1;
64}
65
66uint32_t throw_on_seven_result(uint32_t result, uint32_t argument) {
67 if (argument == 8) throw std::runtime_error("eight");
68 return result * 100 + argument;
69}
70
71// Leaves no return address of an earlier call where an unwinder that reads the wrong slot could find one.
72void dirty_stack() {
73 volatile unsigned char bytes[8192];
74 for (auto& b : bytes) b = 0xAB;
75}
76
77bool caught_through(const uint8_t* thunk, uint32_t argument) {
78 dirty_stack();
79 try {
80 reinterpret_cast<uint32_t (*)(uint32_t)>(const_cast<uint8_t*>(thunk))(argument);
81 } catch (const std::runtime_error&) {
82 return true;
83 }
84 return false;
85}
86
87// Unwinds one frame from inside `thunk`, where its call returns to (`return_offset`, after a prolog that moved rsp by
88// `frame` bytes), and says whether the caller's rip and rsp come out.
89bool unwinds_to_caller(uint8_t* thunk, std::size_t return_offset, std::size_t frame) {
90 alignas(16) uint64_t stack[64] = {};
91 const uint64_t caller_rip = 0x1234567890ull;
92 uint64_t* at_entry = stack + 40; // rsp when the thunk is entered: the return address
93 *at_entry = caller_rip;
94 CONTEXT context{};
95 context.ContextFlags = CONTEXT_CONTROL | CONTEXT_INTEGER;
96 context.Rip = reinterpret_cast<DWORD64>(thunk + return_offset);
97 context.Rsp = reinterpret_cast<DWORD64>(at_entry) - frame;
98 DWORD64 base = 0, establisher = 0;
99 PRUNTIME_FUNCTION function = RtlLookupFunctionEntry(context.Rip, &base, nullptr);
100 if (!function || base != reinterpret_cast<DWORD64>(thunk)) return false;
101 void* handler_data = nullptr;
102 RtlVirtualUnwind(UNW_FLAG_NHANDLER, base, context.Rip, function, &context, &handler_data, &establisher, nullptr);
103 return context.Rip == caller_rip && context.Rsp == reinterpret_cast<DWORD64>(at_entry + 1);
104}
105
106// The offset just after the first `call rax` (FF D0) of a thunk.
107std::size_t after_first_call(const uint8_t* thunk) {
108 for (std::size_t i = 0; i + 1 < 256; ++i)
109 if (thunk[i] == 0xFF && thunk[i + 1] == 0xD0) return i + 2;
110 return 0;
111}
112
59} // namespace 113} // namespace
60 114
61int main() { 115int main() {
@@ -291,6 +345,29 @@ int main() {
291 "redirect_direct_branches returns 0 for null begin"); 345 "redirect_direct_branches returns 0 for null begin");
292 VirtualFree(mem, 0, MEM_RELEASE); 346 VirtualFree(mem, 0, MEM_RELEASE);
293 347
348 // Stack walks and exceptions pass through both thunks.
349 const auto* throwing = reinterpret_cast<const void*>(&throw_on_seven);
350 uint8_t* unwind_result = make_result_thunk(throwing, &throw_on_seven_result, throwing);
351 uint8_t* unwind_remap = make_remap_thunk(throwing, 0, &throw_on_seven, throwing);
352 check(unwind_result && unwind_remap, "thunks for the unwinding checks");
353 if (unwind_result && unwind_remap) {
354 check(reinterpret_cast<Fn1>(unwind_result)(1) == 201, "the result thunk still works");
355 check(reinterpret_cast<Fn1>(unwind_remap)(1) == 3, "the remap thunk still works");
356 check(unwinds_to_caller(unwind_result, after_first_call(unwind_result), 0x28),
357 "a stack walk from the hooked function reaches the result thunk's caller");
358 check(unwinds_to_caller(unwind_remap, after_first_call(unwind_remap), 0x88),
359 "a stack walk from the callback reaches the remap thunk's caller");
360 check(caught_through(unwind_result, 7), "an exception leaves the hooked function through the result thunk");
361 check(caught_through(unwind_result, 8), "an exception leaves the callback through the result thunk");
362 check(caught_through(unwind_remap, 7), "an exception leaves the callback through the remap thunk");
363 const DWORD64 freed = reinterpret_cast<DWORD64>(unwind_result) + 1;
364 DWORD64 base = 0;
365 free_thunk(unwind_result);
366 check(RtlLookupFunctionEntry(freed, &base, nullptr) == nullptr, "a freed thunk leaves no function table");
367 free_thunk(unwind_remap);
368 }
369 free_thunk(nullptr);
370
294 std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); 371 std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures);
295 return g_failures == 0 ? 0 : 1; 372 return g_failures == 0 ? 0 : 1;
296} 373}