2018-04-07 17:55:02 +06:00
|
|
|
#include <stdio.h>
|
2018-08-31 22:54:49 +06:00
|
|
|
#include <stdlib.h>
|
2018-04-07 17:55:02 +06:00
|
|
|
#include <string.h>
|
|
|
|
|
#include <subhook.h>
|
|
|
|
|
|
2018-09-03 20:08:52 +06:00
|
|
|
typedef void (*foo_func_t)(void);
|
2018-04-07 17:55:02 +06:00
|
|
|
|
2018-09-03 20:08:52 +06:00
|
|
|
#ifdef SUBHOOK_X86
|
2018-09-01 16:16:17 +06:00
|
|
|
#if defined SUBHOOK_WINDOWS
|
|
|
|
|
#define FOO_CALL __cdecl
|
|
|
|
|
#elif defined SUBHOOK_UNIX
|
|
|
|
|
#define FOO_CALL __attribute__((cdecl))
|
|
|
|
|
#endif
|
2018-09-07 18:53:56 +06:00
|
|
|
#endif
|
|
|
|
|
#ifndef FOO_CALL
|
2018-09-01 16:16:17 +06:00
|
|
|
#define FOO_CALL
|
|
|
|
|
#endif
|
|
|
|
|
|
2018-09-03 20:08:52 +06:00
|
|
|
extern void FOO_CALL foo(void);
|
2018-04-07 17:55:02 +06:00
|
|
|
foo_func_t foo_tr = NULL;
|
|
|
|
|
|
2018-09-03 20:08:52 +06:00
|
|
|
void foo_hooked(void) {
|
2018-04-07 17:55:02 +06:00
|
|
|
puts("foo_hooked() called");
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-03 20:08:52 +06:00
|
|
|
void foo_hooked_tr(void) {
|
2018-04-07 17:55:02 +06:00
|
|
|
puts("foo_hooked_tr() called");
|
|
|
|
|
foo_tr();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
|
puts("Testing initial install");
|
|
|
|
|
|
2023-02-10 00:00:32 +06:00
|
|
|
subhook_t foo_hook = subhook_new(
|
|
|
|
|
foo, foo_hooked, SUBHOOK_64BIT_OFFSET | SUBHOOK_TRAMPOLINE);
|
2018-04-07 17:55:02 +06:00
|
|
|
if (foo_hook == NULL || subhook_install(foo_hook) < 0) {
|
|
|
|
|
puts("Install failed");
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_FAILURE;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|
|
|
|
|
foo();
|
|
|
|
|
if (subhook_remove(foo_hook) < 0) {
|
|
|
|
|
puts("Remove failed");
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_FAILURE;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|
|
|
|
|
foo();
|
|
|
|
|
|
|
|
|
|
puts("Testing re-install");
|
|
|
|
|
|
|
|
|
|
if (subhook_install(foo_hook) < 0) {
|
|
|
|
|
puts("Install failed");
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_FAILURE;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|
|
|
|
|
foo();
|
|
|
|
|
if (subhook_remove(foo_hook) < 0) {
|
|
|
|
|
puts("Remove failed");
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_FAILURE;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|
|
|
|
|
foo();
|
|
|
|
|
|
2020-11-02 03:18:01 +06:00
|
|
|
subhook_free(foo_hook);
|
|
|
|
|
|
2018-04-07 17:55:02 +06:00
|
|
|
puts("Testing trampoline");
|
|
|
|
|
|
2023-02-10 00:00:32 +06:00
|
|
|
subhook_t foo_hook_tr = subhook_new(
|
|
|
|
|
foo, foo_hooked_tr, SUBHOOK_64BIT_OFFSET | SUBHOOK_TRAMPOLINE);
|
2018-04-07 17:55:02 +06:00
|
|
|
if (subhook_install(foo_hook_tr) < 0) {
|
|
|
|
|
puts("Install failed");
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_FAILURE;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|
|
|
|
|
foo_tr = (foo_func_t)subhook_get_trampoline(foo_hook_tr);
|
2018-08-31 22:54:49 +06:00
|
|
|
if (foo_tr == NULL) {
|
|
|
|
|
puts("Failed to build trampoline");
|
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
|
}
|
2018-04-07 17:55:02 +06:00
|
|
|
foo();
|
2018-08-31 22:54:49 +06:00
|
|
|
|
2020-11-02 03:18:01 +06:00
|
|
|
subhook_free(foo_hook_tr);
|
|
|
|
|
|
2018-08-31 22:54:49 +06:00
|
|
|
return EXIT_SUCCESS;
|
2018-04-07 17:55:02 +06:00
|
|
|
}
|