Skip to content

Commit

Permalink
Simplify test code slightly.
Browse files Browse the repository at this point in the history
  • Loading branch information
kubo committed Mar 4, 2020
1 parent 234e3cc commit f1f6b5e
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 53 deletions.
10 changes: 8 additions & 2 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ else ()
message(FATAL_ERROR "Unsupported architecture detected!")
endif ()

set(TEST_EXE_SOURCES test_main.c)
if (FUNCHOOK_CPU STREQUAL x86)
set(TEST_EXE_SOURCES ${TEST_EXE_SOURCES} ${FUNCHOOK_CPU}_test.S)
endif ()


if (MSVC)
enable_language(ASM_MASM)
set(FUNCHOOK_ASM_SUFFIX _masm.asm)
Expand All @@ -32,7 +38,7 @@ if (UNIX)
endif ()

if (FUNCHOOK_BUILD_SHARED)
add_executable(funchook_test_shared test_main.c ${FUNCHOOK_CPU}_test.S)
add_executable(funchook_test_shared ${TEST_EXE_SOURCES})
target_link_libraries(funchook_test_shared PRIVATE funchook-shared ${FUNCHOOK_TEST_LIBS})
if (UNIX)
set_target_properties(funchook_test_shared PROPERTIES LINK_FLAGS -rdynamic)
Expand All @@ -44,7 +50,7 @@ if (FUNCHOOK_BUILD_SHARED)
endif ()

if (FUNCHOOK_BUILD_STATIC)
add_executable(funchook_test_static test_main.c ${FUNCHOOK_CPU}_test.S)
add_executable(funchook_test_static ${TEST_EXE_SOURCES})
target_link_libraries(funchook_test_static PRIVATE funchook-static ${FUNCHOOK_TEST_LIBS})
if (UNIX)
set_target_properties(funchook_test_static PROPERTIES LINK_FLAGS -rdynamic)
Expand Down
12 changes: 4 additions & 8 deletions test/libfunchook_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,16 @@
#define DLLEXPORT
#endif

#if defined(WIN32) || defined(__APPLE__)
static int int_val;
static int val_in_dll;

DLLEXPORT void set_int_val(int val)
DLLEXPORT void set_val_in_dll(int val)
{
int_val = val;
val_in_dll = val;
}
#else
extern int int_val;
#endif

DLLEXPORT int get_val_in_dll()
{
return int_val;
return val_in_dll;
}

#define S(suffix) DLLEXPORT int dllfunc_##suffix(int a, int b) { return a * b + suffix; }
Expand Down
47 changes: 21 additions & 26 deletions test/test_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,10 @@

typedef int (*int_func_t)(void);

extern int reset_retval(void);
DLLEXPORT int get_val_in_exe(void);
extern int get_val_in_dll(void);
extern int call_get_val_in_dll(void);
extern int jump_get_val_in_dll(void);
extern int x86_test_jump(void);
extern int x86_test_call_get_pc_thunk_ax(void);
extern int x86_test_call_get_pc_thunk_bx(void);
extern int x86_test_call_get_pc_thunk_cx(void);
Expand All @@ -71,25 +69,18 @@ extern int x86_test_call_and_pop_ebp(void);
extern int x86_test_error_jump1(void);
extern int x86_test_error_jump2(void);

#if defined(WIN32) || defined(__APPLE__)
extern void set_int_val(int val);
#else
#define set_int_val(val) do {} while(0)
#endif
extern void set_val_in_dll(int val);

#ifdef _MSC_VER
int reset_retval()
/* Reset the register for return values.
* %eax for x86.
* %rax for x86_64.
*/
NOINLINE int reset_register()
{
return 0;
}
#endif

#if defined(WIN32)
__declspec(dllexport) int int_val = 0xbaceba11;
#else
int int_val = 0xbaceba11;
#endif


static int test_cnt;
static int error_cnt;
Expand Down Expand Up @@ -169,16 +160,16 @@ void test_funchook_int(volatile int_func_t func, const char *func_name, enum loa
}

expected = ++int_val;
set_int_val(int_val);
reset_retval();
set_val_in_dll(int_val);
reset_register();
result = func();
if (expected != result) {
printf("ERROR: %s should return %d but %d before hooking.\n", func_name, expected, result);
error_cnt++;
return;
}
if (func_real != NULL) {
reset_retval();
reset_register();
result = func_real();
if (expected != result) {
printf("ERROR: %s (real) should return %d but %d before hooking.\n", func_name, expected, result);
Expand All @@ -202,8 +193,8 @@ void test_funchook_int(volatile int_func_t func, const char *func_name, enum loa

hook_is_called = 0;
expected = ++int_val;
set_int_val(int_val);
reset_retval();
set_val_in_dll(int_val);
reset_register();
result = func();
if (hook_is_called == 0) {
printf("ERROR: hook_func is not called by %s.\n", func_name);
Expand All @@ -217,7 +208,7 @@ void test_funchook_int(volatile int_func_t func, const char *func_name, enum loa
}
if (func_real != NULL) {
hook_is_called = 0;
reset_retval();
reset_register();
result = func_real();
if (hook_is_called == 0) {
printf("ERROR: hook_func is not called by %s (real).\n", func_name);
Expand All @@ -234,16 +225,16 @@ void test_funchook_int(volatile int_func_t func, const char *func_name, enum loa
funchook_uninstall(funchook, 0);

expected = ++int_val;
set_int_val(int_val);
reset_retval();
set_val_in_dll(int_val);
reset_register();
result = func();
if (expected != result) {
printf("ERROR: %s should return %d but %d after hook is removed.\n", func_name, expected, result);
error_cnt++;
return;
}
if (func_real != NULL) {
reset_retval();
reset_register();
result = func_real();
if (expected != result) {
printf("ERROR: %s (real) should return %d but %d after hook is removed.\n", func_name, expected, result);
Expand Down Expand Up @@ -439,14 +430,18 @@ static void test_hook_many_funcs(void)
#define S(suffix) \
dllfunc_##suffix##_func = dllfunc_##suffix; \
funchook_prepare(funchook, (void**)&dllfunc_##suffix##_func, dllfunc_##suffix##_hook); \
putchar('.'); fflush(stdout);
putchar('.'); fflush(stdout); \
funchook_set_debug_file(NULL); /* disable logging except the first to reduce log size. */
#include "suffix.list"
funchook_set_debug_file("debug.log");
#undef S
#ifndef SKIP_TESTS_CHANGING_EXE
#define S(suffix) \
exefunc_##suffix##_func = exefunc_##suffix; \
funchook_prepare(funchook, (void**)&exefunc_##suffix##_func, exefunc_##suffix##_hook);
funchook_prepare(funchook, (void**)&exefunc_##suffix##_func, exefunc_##suffix##_hook); \
funchook_set_debug_file(NULL); /* disable logging except the first to reduce log size. */
#include "suffix.list"
funchook_set_debug_file("debug.log");
#undef S
#endif
putchar('\n');
Expand Down
10 changes: 0 additions & 10 deletions test/x86_64_test.S

This file was deleted.

7 changes: 0 additions & 7 deletions test/x86_test.S
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#if defined(_WIN32) || defined(__APPLE__)
#define get_val_in_exe _get_val_in_exe
#define int_val _int_val
#define reset_retval _reset_retval
#define x86_test_error_jump1 _x86_test_error_jump1
#define x86_test_error_jump2 _x86_test_error_jump2
#define x86_test_call_get_pc_thunk_ax _x86_test_call_get_pc_thunk_ax
Expand All @@ -21,12 +20,6 @@
#endif
.text

.p2align 4,,15
.globl reset_retval
reset_retval:
xorl %eax, %eax
ret

.p2align 4,,15
.globl x86_test_error_jump1
x86_test_error_jump1:
Expand Down

0 comments on commit f1f6b5e

Please sign in to comment.