summaryrefslogtreecommitdiff
path: root/test/hook.test.c
blob: 625fdbfb2e728f3a98c56ef51f224b0949a21cd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* This file is dedicated to the public domain. */

{.desc = "inline function hooking"};

#ifdef _WIN32

#include "../src/x86.c"
#include "../src/hook.c"
#include "../src/os.c"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

// stub
void con_warn(const char *msg, ...) {
	va_list l;
	va_start(l, msg);
	vfprintf(stderr, msg, l);
	va_end(l);
}

typedef int (*testfunc)(int, int);

__attribute__((noinline)) static int func1(int a, int b) { return a + b; }
static int (*orig_func1)(int, int);
static int hook1(int a, int b) { return orig_func1(a, b) + 5; }

__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)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)test_hook_inline((void *)&func1, (void *)&hook1);
	if (!orig_func1) return false;
	unhook_inline((void *)orig_func1);
	return func1(5, 5) == 10;
}

TEST("Multiple functions should be able to be inline-hooked at once") {
	if (!hook_init()) return false;
	orig_func1 = (testfunc)test_hook_inline((void *)&func1, (void *)&hook1);
	if (!orig_func1) return false;
	orig_func2 = (testfunc)test_hook_inline((void *)&func2, (void *)&hook2);
	if (!orig_func2) return false;
	return func2(5, 5) == 5;
}

#endif

// vi: sw=4 ts=4 noet tw=80 cc=80