diff options
author | 2025-04-16 02:13:01 +0100 | |
---|---|---|
committer | 2025-04-16 21:31:20 +0100 | |
commit | 4fddfa831d2a33ab3eee7ceb5f181c82d5aa78d2 (patch) | |
tree | f62a0fc1a0b3d7ffcd7967b98885453636309686 /test/hook.test.c | |
parent | 5c805aac744df43dd96f70bc7e39337d9c3a966a (diff) | |
download | sst-4fddfa831d2a33ab3eee7ceb5f181c82d5aa78d2.tar.gz sst-4fddfa831d2a33ab3eee7ceb5f181c82d5aa78d2.zip |
Rework API for inline hooking
This both simplifies and complicates things, but probably hopefully
maybe simplifies things overall. Certainly in cases like the L4D1 demo
thing where there's 3 inline hooks at once, it seems simpler to be able
to batch the fallible stuff to avoid rollbacks. In cases where you only
need one hook, it's a bit more verbose, but what can you do.
Thanks bill for discussing this with me pretty exhaustively and giving a
lot of good input.
I think both of us still kind of hate it actually.
Diffstat (limited to 'test/hook.test.c')
-rw-r--r-- | test/hook.test.c | 20 |
1 files changed, 16 insertions, 4 deletions
diff --git a/test/hook.test.c b/test/hook.test.c index 9e7cfa9..625fdbf 100644 --- a/test/hook.test.c +++ b/test/hook.test.c @@ -30,16 +30,28 @@ __attribute__((noinline)) static int func2(int a, int b) { return a - b; } static int (*orig_func2)(int, int); static int hook2(int a, int b) { return orig_func2(a, b) + 5; } +// basic reimplementation of old API to support existing test cases. +// XXX: we could probably have tests at the boundaries of the new API too, +// although the current tests are only testing for regressions in x86 jmp logic. +static inline void *test_hook_inline(void *func, void *target) { + void *trampoline; + struct hook_inline_prep_ret prep = hook_inline_prep(func, &trampoline); + if (prep.err) return 0; + if (!hook_inline_mprot(prep.prologue)) return 0; + hook_inline_commit(prep.prologue, target); + return trampoline; +} + TEST("Inline hooks should be able to wrap the original function") { if (!hook_init()) return false; - orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + orig_func1 = (testfunc)test_hook_inline((void *)&func1, (void *)&hook1); if (!orig_func1) return false; return func1(5, 5) == 15; } TEST("Inline hooks should be removable again") { if (!hook_init()) return false; - orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + orig_func1 = (testfunc)test_hook_inline((void *)&func1, (void *)&hook1); if (!orig_func1) return false; unhook_inline((void *)orig_func1); return func1(5, 5) == 10; @@ -47,9 +59,9 @@ TEST("Inline hooks should be removable again") { TEST("Multiple functions should be able to be inline-hooked at once") { if (!hook_init()) return false; - orig_func1 = (testfunc)hook_inline((void *)&func1, (void *)&hook1); + orig_func1 = (testfunc)test_hook_inline((void *)&func1, (void *)&hook1); if (!orig_func1) return false; - orig_func2 = (testfunc)hook_inline((void *)&func2, (void *)&hook2); + orig_func2 = (testfunc)test_hook_inline((void *)&func2, (void *)&hook2); if (!orig_func2) return false; return func2(5, 5) == 5; } |