blob: 0eee1fa1515e5291e5ce0a4215af9b9920798afc (
plain)
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
72
73
74
75
|
#include <iostream>
#include <subhook.h>
typedef void (*foo_func_t)();
#ifdef SUBHOOK_X86
#if defined SUBHOOK_WINDOWS
#define FOO_CALL __cdecl
#elif defined SUBHOOK_UNIX
#define FOO_CALL __attribute__((cdecl))
#endif
#else
#define FOO_CALL
#endif
extern "C" void FOO_CALL foo();
foo_func_t foo_tr = nullptr;
void foo_hooked() {
std::cout << "foo_hooked() called" << std::endl;;
}
void foo_hooked_tr() {
std::cout << "foo_hooked_tr() called" << std::endl;
foo_tr();
}
int main() {
std::cout << "Testing initial install" << std::endl;
subhook::Hook foo_hook((void *)foo,
(void *)foo_hooked,
subhook::HookFlag64BitOffset);
if (!foo_hook.Install()) {
std::cout << "Install failed" << std::endl;
return EXIT_FAILURE;
}
foo();
if (!foo_hook.Remove()) {
std::cout << "Remove failed" << std::endl;
return EXIT_FAILURE;
}
foo();
std::cout << "Testing re-install" << std::endl;
if (!foo_hook.Install()) {
std::cout << "Install failed" << std::endl;
return EXIT_FAILURE;
}
foo();
if (!foo_hook.Remove()) {
std::cout << "Remove failed" << std::endl;
return EXIT_FAILURE;
}
foo();
std::cout << "Testing trampoline" << std::endl;
subhook::Hook foo_hook_tr((void *)foo,
(void *)foo_hooked_tr,
subhook::HookFlag64BitOffset);
if (!foo_hook_tr.Install()) {
std::cout << "Install failed" << std::endl;
return EXIT_FAILURE;
}
foo_tr = (foo_func_t)foo_hook_tr.GetTrampoline();
if (foo_tr == nullptr) {
std::cout << "Failed to build trampoline" << std::endl;
return EXIT_FAILURE;
}
foo();
return EXIT_SUCCESS;
}
|