diff options
| author | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 22:53:19 -0500 |
|---|---|---|
| committer | ItsNotRexed <300290919+ItsNotRexed@users.noreply.github.com> | 2026-11-22 22:53:19 -0500 |
| commit | 9e6444fadb93c9b800ed403f91fcfee1a88d3560 (patch) | |
| tree | be1cf4a298295b74268c5a652210ca34b5069b7e /runtime/test/code_hooks_test.cpp | |
| parent | 2d3369a4c82e6f36c5190c4c534d3df600e135ec (diff) | |
Mods: the archetype hook, --archetype-lookup RVA[,ARG]
code_hooks.h hooks a function of the mapped game image without patching it: every direct call/jmp rel32 to it in the
image's executable sections is pointed at a thunk that saves rcx/rdx/r8/r9 and xmm0-3, passes the hash (argument 0
or 1) through leonida::mods::active().remap() and jumps on to the original. gta6rt installs it right after mapping,
before any image code runs, and logs how many call sites it redirected, so a --dry-run with a candidate RVA tells
whether it is the archetype lookup. ctest code_hooks runs the thunk on x64 functions written into memory (call and
tail jump remapped, r8 preserved, a branch elsewhere left byte for byte). With the executable's lookup RVA this
applies mods/mandy_prison_intro; docs/modding.md says how to find it.
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 | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/runtime/test/code_hooks_test.cpp b/runtime/test/code_hooks_test.cpp new file mode 100644 index 000000000..6c1fb4fb1 --- /dev/null +++ b/runtime/test/code_hooks_test.cpp | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | // code_hooks.h: a remapping thunk and direct-call redirection, on small x64 functions written into memory. No game | ||
| 2 | // files: the archetype lookup they stand in for takes its model hash in ecx (docs/modding.md). | ||
| 3 | #include "../src/code_hooks.h" | ||
| 4 | #include "../src/mods.h" | ||
| 5 | |||
| 6 | #include <cstdio> | ||
| 7 | #include <cstring> | ||
| 8 | |||
| 9 | namespace { | ||
| 10 | |||
| 11 | int g_checks = 0, g_failures = 0; | ||
| 12 | |||
| 13 | void check(bool ok, const char* what) { | ||
| 14 | ++g_checks; | ||
| 15 | if (!ok) { | ||
| 16 | ++g_failures; | ||
| 17 | std::printf("FAIL %s\n", what); | ||
| 18 | } | ||
| 19 | } | ||
| 20 | |||
| 21 | uint32_t remap_lucia(uint32_t hash) { | ||
| 22 | return hash == leonida::mods::joaat("player_story000_00") ? leonida::mods::joaat("cs_mandy") : hash; | ||
| 23 | } | ||
| 24 | |||
| 25 | void put(uint8_t* at, std::initializer_list<uint8_t> bytes) { | ||
| 26 | for (uint8_t b : bytes) *at++ = b; | ||
| 27 | } | ||
| 28 | |||
| 29 | // `call rel32` (or `jmp` with e9) at `at` towards `to`. | ||
| 30 | void branch(uint8_t* at, uint8_t opcode, const uint8_t* to) { | ||
| 31 | at[0] = opcode; | ||
| 32 | const int32_t rel = static_cast<int32_t>(to - (at + 5)); | ||
| 33 | std::memcpy(at + 1, &rel, 4); | ||
| 34 | } | ||
| 35 | |||
| 36 | // sub rsp,0x28 / call to / add rsp,0x28 / ret | ||
| 37 | void caller(uint8_t* at, const uint8_t* to) { | ||
| 38 | put(at, {0x48, 0x83, 0xEC, 0x28}); | ||
| 39 | branch(at + 4, 0xE8, to); | ||
| 40 | put(at + 9, {0x48, 0x83, 0xC4, 0x28, 0xC3}); | ||
| 41 | } | ||
| 42 | |||
| 43 | } // namespace | ||
| 44 | |||
| 45 | int main() { | ||
| 46 | using namespace leonida::hooks; | ||
| 47 | uint8_t* mem = alloc_near(reinterpret_cast<const void*>(&remap_lucia), 4096); | ||
| 48 | check(mem != nullptr, "executable memory near the module"); | ||
| 49 | if (!mem) return 1; | ||
| 50 | std::memset(mem, 0xCC, 4096); | ||
| 51 | |||
| 52 | uint8_t* target0 = mem; // returns its first argument: mov eax, ecx ; ret | ||
| 53 | put(target0, {0x89, 0xC8, 0xC3}); | ||
| 54 | uint8_t* target1 = mem + 16; // returns its second argument: mov eax, edx ; ret | ||
| 55 | put(target1, {0x89, 0xD0, 0xC3}); | ||
| 56 | uint8_t* target2 = mem + 32; // returns its third argument: mov eax, r8d ; ret | ||
| 57 | put(target2, {0x44, 0x89, 0xC0, 0xC3}); | ||
| 58 | |||
| 59 | uint8_t* call0 = mem + 64; | ||
| 60 | caller(call0, target0); | ||
| 61 | uint8_t* tail0 = mem + 96; // jmp target0 (a tail call) | ||
| 62 | branch(tail0, 0xE9, target0); | ||
| 63 | uint8_t* decoy = mem + 112; // a call to target0 + 1: not the hooked function, must stay as it is | ||
| 64 | caller(decoy, target0 + 1); | ||
| 65 | uint8_t* call1 = mem + 144; | ||
| 66 | caller(call1, target1); | ||
| 67 | uint8_t* call2 = mem + 176; | ||
| 68 | caller(call2, target2); | ||
| 69 | uint8_t decoy_before[16]; | ||
| 70 | std::memcpy(decoy_before, decoy, 16); | ||
| 71 | |||
| 72 | uint8_t* thunk0 = make_remap_thunk(target0, 0, &remap_lucia, target0); | ||
| 73 | uint8_t* thunk1 = make_remap_thunk(target1, 1, &remap_lucia, target1); | ||
| 74 | uint8_t* thunk2 = make_remap_thunk(target2, 0, &remap_lucia, target2); | ||
| 75 | check(thunk0 && thunk1 && thunk2, "thunks are allocated near their targets"); | ||
| 76 | if (!thunk0 || !thunk1 || !thunk2) return 1; | ||
| 77 | |||
| 78 | const std::size_t n0 = redirect_direct_branches(mem + 64, 112, target0, thunk0); | ||
| 79 | const std::size_t n1 = redirect_direct_branches(call1, 16, target1, thunk1); | ||
| 80 | const std::size_t n2 = redirect_direct_branches(call2, 16, target2, thunk2); | ||
| 81 | check(n0 == 2, "the call and the tail jump to the function are redirected, the decoy is not"); | ||
| 82 | check(n1 == 1 && n2 == 1, "one call each in the other callers"); | ||
| 83 | check(std::memcmp(decoy, decoy_before, 16) == 0, "a branch to another address is left byte for byte"); | ||
| 84 | FlushInstructionCache(GetCurrentProcess(), mem, 4096); | ||
| 85 | |||
| 86 | using Fn1 = uint32_t (*)(uint32_t); | ||
| 87 | using Fn2 = uint32_t (*)(uint64_t, uint32_t); | ||
| 88 | using Fn3 = uint32_t (*)(uint32_t, uint32_t, uint32_t); | ||
| 89 | const uint32_t lucia = 0xE472B687u, mandy = 0xD7C0CDFFu; | ||
| 90 | check(reinterpret_cast<Fn1>(call0)(lucia) == mandy, "through the call, Lucia's model id becomes cs_mandy's"); | ||
| 91 | check(reinterpret_cast<Fn1>(call0)(1234) == 1234, "any other id passes through unchanged"); | ||
| 92 | check(reinterpret_cast<Fn1>(tail0)(lucia) == mandy, "a tail jump is remapped as well"); | ||
| 93 | check(reinterpret_cast<Fn2>(call1)(0x1111, lucia) == mandy, "argument 1 (edx) is remapped when asked"); | ||
| 94 | check(reinterpret_cast<Fn3>(call2)(lucia, 2, 777) == 777, "r8 reaches the original unchanged"); | ||
| 95 | |||
| 96 | // hook_image_calls refuses what is not a mapped image or not code | ||
| 97 | check(!hook_image_calls(mem, 0, &remap_lucia, 0).ok, "memory without a PE header is refused"); | ||
| 98 | auto* self = reinterpret_cast<uint8_t*>(GetModuleHandleW(nullptr)); | ||
| 99 | check(!hook_image_calls(self, 0x7FFFFFF0u, &remap_lucia, 0).ok, "an RVA outside the image is refused"); | ||
| 100 | check(!hook_image_calls(self, 0, &remap_lucia, 0).ok, "an RVA in the headers (not code) is refused"); | ||
| 101 | check(!make_remap_thunk(target0, 2, &remap_lucia, target0), "only argument 0 or 1 can be remapped"); | ||
| 102 | |||
| 103 | std::printf("code_hooks_test: %d checks, %d failures\n", g_checks, g_failures); | ||
| 104 | return g_failures == 0 ? 0 : 1; | ||
| 105 | } | ||