diff options
| author | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:36:26 -0300 |
|---|---|---|
| committer | Rafael <rafaelalvesp2005@gmail.com> | 2026-11-23 15:36:26 -0300 |
| commit | 63f0e0dea3a9d44201db408c4e2c3f2f5b2b94d6 (patch) | |
| tree | 86f0dcad8070e2a3a693ef4897aa83950cfeb224 /runtime/test/code_hooks_test.cpp | |
| parent | f78f7d6a2a9364cb128c03ac539fe540d5c70df1 (diff) | |
| parent | 6dda0630bd6fc0514baadb94fb98e36bd7d64c0b (diff) | |
Merge main into fix/walk-bink_names
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/code_hooks_test.cpp')
| -rw-r--r-- | runtime/test/code_hooks_test.cpp | 168 |
1 files changed, 168 insertions, 0 deletions
diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp index cb5e266da..cb8ce1727 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 <vector> | ||
| 8 | 9 | ||
| 9 | namespace { | 10 | namespace { |
| 10 | 11 | ||
| @@ -40,10 +41,26 @@ void caller(uint8_t* at, const uint8_t* to) { | |||
| 40 | put(at + 9, {0x48, 0x83, 0xC4, 0x28, 0xC3}); | 41 | put(at + 9, {0x48, 0x83, 0xC4, 0x28, 0xC3}); |
| 41 | } | 42 | } |
| 42 | 43 | ||
| 44 | void init_pe64(uint8_t* img, uint32_t size_of_image, const std::vector<IMAGE_SECTION_HEADER>& sections) { | ||
| 45 | auto* dos = reinterpret_cast<IMAGE_DOS_HEADER*>(img); | ||
| 46 | dos->e_magic = IMAGE_DOS_SIGNATURE; | ||
| 47 | dos->e_lfanew = 0x80; | ||
| 48 | auto* nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(img + dos->e_lfanew); | ||
| 49 | nt->Signature = IMAGE_NT_SIGNATURE; | ||
| 50 | nt->FileHeader.Machine = IMAGE_FILE_MACHINE_AMD64; | ||
| 51 | nt->FileHeader.NumberOfSections = static_cast<WORD>(sections.size()); | ||
| 52 | nt->FileHeader.SizeOfOptionalHeader = sizeof(IMAGE_OPTIONAL_HEADER64); | ||
| 53 | nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR64_MAGIC; | ||
| 54 | nt->OptionalHeader.SizeOfImage = size_of_image; | ||
| 55 | IMAGE_SECTION_HEADER* sec = IMAGE_FIRST_SECTION(nt); | ||
| 56 | for (std::size_t i = 0; i < sections.size(); ++i) sec[i] = sections[i]; | ||
| 57 | } | ||
| 58 | |||
| 43 | } // namespace | 59 | } // namespace |
| 44 | 60 | ||
| 45 | int main() { | 61 | int main() { |
| 46 | using namespace leonida::hooks; | 62 | using namespace leonida::hooks; |
| 63 | std::setvbuf(stdout, nullptr, _IONBF, 0); | ||
| 47 | uint8_t* mem = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 4096); | 64 | uint8_t* mem = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 4096); |
| 48 | check(mem != nullptr, "executable memory near the module"); | 65 | check(mem != nullptr, "executable memory near the module"); |
| 49 | if (!mem) return 1; | 66 | if (!mem) return 1; |
| @@ -123,6 +140,157 @@ int main() { | |||
| 123 | } | 140 | } |
| 124 | check(!hook_image_results(mem, 0, first_person_on_foot).ok, "hook_image_results refuses memory without a PE"); | 141 | check(!hook_image_results(mem, 0, first_person_on_foot).ok, "hook_image_results refuses memory without a PE"); |
| 125 | 142 | ||
| 143 | // Test A: PE64 executable section with Misc.VirtualSize == 0 falls back to SizeOfRawData (matching pe_loader.cpp). | ||
| 144 | uint8_t* img_a = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 0x2000); | ||
| 145 | check(img_a != nullptr, "synthetic PE64 image A allocated near module"); | ||
| 146 | if (img_a) { | ||
| 147 | std::memset(img_a, 0, 0x2000); | ||
| 148 | IMAGE_SECTION_HEADER sec_a{}; | ||
| 149 | std::memcpy(sec_a.Name, ".text", 5); | ||
| 150 | sec_a.Misc.VirtualSize = 0; | ||
| 151 | sec_a.VirtualAddress = 0x1000; | ||
| 152 | sec_a.SizeOfRawData = 0x200; | ||
| 153 | sec_a.Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE; | ||
| 154 | init_pe64(img_a, 0x2000, {sec_a}); | ||
| 155 | put(img_a + 0x1000, {0x89, 0xC8, 0xC3}); // mov eax, ecx ; ret | ||
| 156 | caller(img_a + 0x1020, img_a + 0x1000); | ||
| 157 | DWORD old_a = 0; | ||
| 158 | check(VirtualProtect(img_a + 0x1000, 0x1000, PAGE_EXECUTE_READ, &old_a) != FALSE, | ||
| 159 | "image A code page marked PAGE_EXECUTE_READ"); | ||
| 160 | FlushInstructionCache(GetCurrentProcess(), img_a + 0x1000, 0x1000); | ||
| 161 | |||
| 162 | const HookResult ra = hook_image_calls(img_a, 0x1000, &remap_lucia, 0); | ||
| 163 | check(ra.ok && ra.redirected == 1, | ||
| 164 | "VirtualSize == 0 executable section uses SizeOfRawData and redirects its caller"); | ||
| 165 | if (ra.ok) { | ||
| 166 | check(reinterpret_cast<Fn1>(img_a + 0x1020)(lucia) == mandy, | ||
| 167 | "hooked caller in VirtualSize == 0 section remaps Lucia to Mandy"); | ||
| 168 | check(reinterpret_cast<Fn1>(img_a + 0x1020)(1234) == 1234, | ||
| 169 | "hooked caller in VirtualSize == 0 section passes other hashes through"); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | |||
| 173 | // Test B: a 0-byte executable section (VirtualSize == 0 && SizeOfRawData == 0) is skipped rather than failing VirtualProtect. | ||
| 174 | uint8_t* img_b = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 0x2000); | ||
| 175 | check(img_b != nullptr, "synthetic PE64 image B allocated near module"); | ||
| 176 | if (img_b) { | ||
| 177 | std::memset(img_b, 0, 0x2000); | ||
| 178 | IMAGE_SECTION_HEADER sec_empty{}; | ||
| 179 | std::memcpy(sec_empty.Name, ".empty", 6); | ||
| 180 | sec_empty.Misc.VirtualSize = 0; | ||
| 181 | sec_empty.VirtualAddress = 0x1000; | ||
| 182 | sec_empty.SizeOfRawData = 0; | ||
| 183 | sec_empty.Characteristics = IMAGE_SCN_MEM_EXECUTE; | ||
| 184 | IMAGE_SECTION_HEADER sec_text{}; | ||
| 185 | std::memcpy(sec_text.Name, ".text", 5); | ||
| 186 | sec_text.Misc.VirtualSize = 0x200; | ||
| 187 | sec_text.VirtualAddress = 0x1000; | ||
| 188 | sec_text.SizeOfRawData = 0x200; | ||
| 189 | sec_text.Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE; | ||
| 190 | init_pe64(img_b, 0x2000, {sec_empty, sec_text}); | ||
| 191 | put(img_b + 0x1040, {0xB8, 0x02, 0x00, 0x00, 0x00, 0xC3}); // mov eax, 2 ; ret | ||
| 192 | caller(img_b + 0x1060, img_b + 0x1040); | ||
| 193 | DWORD old_b = 0; | ||
| 194 | check(VirtualProtect(img_b + 0x1000, 0x1000, PAGE_EXECUTE_READ, &old_b) != FALSE, | ||
| 195 | "image B code page marked PAGE_EXECUTE_READ"); | ||
| 196 | FlushInstructionCache(GetCurrentProcess(), img_b + 0x1000, 0x1000); | ||
| 197 | |||
| 198 | const int before_calls = original_calls; | ||
| 199 | const HookResult rb = hook_image_results(img_b, 0x1040, first_person_on_foot); | ||
| 200 | check(rb.ok && rb.redirected == 1, | ||
| 201 | "0-byte executable section is skipped and the non-empty section is hooked"); | ||
| 202 | if (rb.ok) { | ||
| 203 | using Ctx = uint32_t (*)(uint32_t); | ||
| 204 | check(reinterpret_cast<Ctx>(img_b + 0x1060)(0) == 4, | ||
| 205 | "result hook in PE64 image overrides context 0"); | ||
| 206 | check(reinterpret_cast<Ctx>(img_b + 0x1060)(3) == 2, | ||
| 207 | "result hook in PE64 image preserves non-overridden contexts"); | ||
| 208 | check(original_calls == before_calls + 2, | ||
| 209 | "result hook invokes the original getter on each call"); | ||
| 210 | } | ||
| 211 | } | ||
| 212 | |||
| 213 | // Test C: sections extending past SizeOfImage are clamped and 32-bit VirtualAddress + VirtualSize wraparound is safe. | ||
| 214 | uint8_t* img_c = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 0x2000); | ||
| 215 | check(img_c != nullptr, "synthetic PE64 image C allocated near module"); | ||
| 216 | if (img_c) { | ||
| 217 | std::memset(img_c, 0, 0x2000); | ||
| 218 | IMAGE_SECTION_HEADER sec_wrap{}; | ||
| 219 | std::memcpy(sec_wrap.Name, ".wrap", 5); | ||
| 220 | sec_wrap.Misc.VirtualSize = 0x200u; | ||
| 221 | sec_wrap.VirtualAddress = 0xFFFFFF00u; | ||
| 222 | sec_wrap.Characteristics = IMAGE_SCN_MEM_EXECUTE; | ||
| 223 | IMAGE_SECTION_HEADER sec_over{}; | ||
| 224 | std::memcpy(sec_over.Name, ".text", 5); | ||
| 225 | sec_over.Misc.VirtualSize = 0x1800u; // spans 0x0800..0x2000, past SizeOfImage = 0x1000 | ||
| 226 | sec_over.VirtualAddress = 0x0800u; | ||
| 227 | sec_over.Characteristics = IMAGE_SCN_MEM_EXECUTE | IMAGE_SCN_CNT_CODE; | ||
| 228 | init_pe64(img_c, 0x1000, {sec_wrap, sec_over}); | ||
| 229 | put(img_c + 0x0800, {0x89, 0xC8, 0xC3}); // mov eax, ecx ; ret | ||
| 230 | caller(img_c + 0x0820, img_c + 0x0800); // in-image caller | ||
| 231 | caller(img_c + 0x1020, img_c + 0x0800); // out-of-image bytes past SizeOfImage | ||
| 232 | uint8_t out_before[16]; | ||
| 233 | std::memcpy(out_before, img_c + 0x1020, sizeof(out_before)); | ||
| 234 | FlushInstructionCache(GetCurrentProcess(), img_c, 0x2000); | ||
| 235 | |||
| 236 | const HookResult rc = hook_image_calls(img_c, 0x0800, &remap_lucia, 0); | ||
| 237 | check(rc.ok && rc.redirected == 1, | ||
| 238 | "section past SizeOfImage is clamped and wraparound section is ignored"); | ||
| 239 | if (rc.ok) { | ||
| 240 | check(reinterpret_cast<Fn1>(img_c + 0x0820)(lucia) == mandy, | ||
| 241 | "in-image caller inside clamped section is remapped"); | ||
| 242 | check(std::memcmp(img_c + 0x1020, out_before, sizeof(out_before)) == 0, | ||
| 243 | "bytes beyond SizeOfImage are left untouched"); | ||
| 244 | } | ||
| 245 | } | ||
| 246 | |||
| 247 | // Test D: malformed headers, PE32 optional header, out-of-image section table, and null arguments/callbacks. | ||
| 248 | uint8_t* bad = static_cast<uint8_t*>(VirtualAlloc(nullptr, 0x1000, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE)); | ||
| 249 | check(bad != nullptr, "scratch buffer for malformed PE checks allocated"); | ||
| 250 | if (bad) { | ||
| 251 | std::memset(bad, 0, 0x1000); | ||
| 252 | auto* bad_dos = reinterpret_cast<IMAGE_DOS_HEADER*>(bad); | ||
| 253 | bad_dos->e_magic = IMAGE_DOS_SIGNATURE; | ||
| 254 | bad_dos->e_lfanew = 0; | ||
| 255 | check(!hook_image_calls(bad, 0x200, &remap_lucia, 0).ok, "MZ with e_lfanew == 0 is refused"); | ||
| 256 | bad_dos->e_lfanew = -16; | ||
| 257 | check(!hook_image_calls(bad, 0x200, &remap_lucia, 0).ok, "MZ with negative e_lfanew is refused"); | ||
| 258 | |||
| 259 | std::memset(bad, 0, 0x1000); | ||
| 260 | init_pe64(bad, 0x1000, {}); | ||
| 261 | auto* bad_nt = reinterpret_cast<IMAGE_NT_HEADERS64*>(bad + 0x80); | ||
| 262 | bad_nt->OptionalHeader.Magic = IMAGE_NT_OPTIONAL_HDR32_MAGIC; | ||
| 263 | check(!hook_image_calls(bad, 0x200, &remap_lucia, 0).ok, "PE32 optional header magic is refused"); | ||
| 264 | |||
| 265 | std::memset(bad, 0, 0x1000); | ||
| 266 | init_pe64(bad, 0x200, {}); | ||
| 267 | bad_nt->FileHeader.NumberOfSections = 100; | ||
| 268 | check(!hook_image_calls(bad, 0x100, &remap_lucia, 0).ok, | ||
| 269 | "section table extending past SizeOfImage is refused"); | ||
| 270 | VirtualFree(bad, 0, MEM_RELEASE); | ||
| 271 | } | ||
| 272 | |||
| 273 | if (img_a) { | ||
| 274 | check(!hook_image_calls(img_a, 0x1000, nullptr, 0).ok, "hook_image_calls refuses null callback"); | ||
| 275 | VirtualFree(img_a, 0, MEM_RELEASE); | ||
| 276 | } | ||
| 277 | if (img_b) { | ||
| 278 | check(!hook_image_results(img_b, 0x1040, nullptr).ok, "hook_image_results refuses null callback"); | ||
| 279 | VirtualFree(img_b, 0, MEM_RELEASE); | ||
| 280 | } | ||
| 281 | if (img_c) VirtualFree(img_c, 0, MEM_RELEASE); | ||
| 282 | |||
| 283 | check(!hook_image_calls(nullptr, 0x1000, &remap_lucia, 0).ok, "hook_image_calls refuses null base"); | ||
| 284 | check(!hook_image_results(nullptr, 0x1000, first_person_on_foot).ok, "hook_image_results refuses null base"); | ||
| 285 | check(make_remap_thunk(target0, 0, nullptr, target0) == nullptr, "make_remap_thunk refuses null callback"); | ||
| 286 | check(make_remap_thunk(nullptr, 0, &remap_lucia, target0) == nullptr, "make_remap_thunk refuses null close_to"); | ||
| 287 | check(make_result_thunk(getter, nullptr, getter) == nullptr, "make_result_thunk refuses null callback"); | ||
| 288 | check(make_result_thunk(getter, first_person_on_foot, nullptr) == nullptr, | ||
| 289 | "make_result_thunk refuses null original"); | ||
| 290 | check(redirect_direct_branches(nullptr, 16, target0, thunk0) == 0, | ||
| 291 | "redirect_direct_branches returns 0 for null begin"); | ||
| 292 | VirtualFree(mem, 0, MEM_RELEASE); | ||
| 293 | |||
| 126 | std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); | 294 | std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); |
| 127 | return g_failures == 0 ? 0 : 1; | 295 | return g_failures == 0 ? 0 : 1; |
| 128 | } | 296 | } |