diff --git a/tests/unit/test_x86.c b/tests/unit/test_x86.c index 38fef5f9..d05b38db 100644 --- a/tests/unit/test_x86.c +++ b/tests/unit/test_x86.c @@ -981,6 +981,91 @@ static void test_x86_nested_uc_emu_start_exits(void) OK(uc_close(uc)); } +static void test_x86_correct_address_in_small_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data) +{ + // Check registers + uint64_t r_rax = 0x0; + uint64_t r_rip = 0x0; + OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax)); + OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip)); + TEST_CHECK(r_rax == 0x7F00); + TEST_CHECK(r_rip == 0x7F00); + + // Check address + // printf("%lx\n", address); + TEST_CHECK(address == 0x7F00); +} + +static void test_x86_correct_address_in_small_jump_hook(void) +{ + uc_engine *uc; + // movabs $0x7FFFFFFFFEFBEC9C, %rax + // jmp *%rax + char code[] = "\x48\xb8\x00\x7F\x00\x00\x00\x00\x00\x00\xff\xe0"; + + uint64_t r_rax = 0x0; + uint64_t r_rip = 0x0; + uc_hook hook; + + uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_64, code, sizeof(code) - 1); + OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_x86_correct_address_in_small_jump_hook_callback, NULL, 1, 0)); + + + uc_assert_err( + UC_ERR_FETCH_UNMAPPED, + uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0)); + + OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax)); + OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip)); + TEST_CHECK(r_rax == 0x7F00); + TEST_CHECK(r_rip == 0x7F00); + + OK(uc_close(uc)); +} + +static void test_x86_correct_address_in_long_jump_hook_callback(uc_engine *uc, int type, uint64_t address, int size, int64_t value, void *user_data) +{ + // Check registers + uint64_t r_rax = 0x0; + uint64_t r_rip = 0x0; + OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax)); + OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip)); + TEST_CHECK(r_rax == 0x7FFFFFFFFFFFFF00); + TEST_CHECK(r_rip == 0x7FFFFFFFFFFFFF00); + + // Check address + // printf("%lx\n", address); + TEST_CHECK(address == 0x7FFFFFFFFFFFFF00); +} + +static void test_x86_correct_address_in_long_jump_hook(void) +{ + uc_engine *uc; + // movabs $0x7FFFFFFFFEFBEC9C, %rax + // jmp *%rax + char code[] = "\x48\xb8\x00\xff\xff\xff\xff\xff\xff\x7f\xff\xe0"; + + uint64_t r_rax = 0x0; + uint64_t r_rip = 0x0; + uc_hook hook; + + uc_common_setup(&uc, UC_ARCH_X86, UC_MODE_64, code, sizeof(code) - 1); + OK(uc_hook_add(uc, &hook, UC_HOOK_MEM_UNMAPPED, test_x86_correct_address_in_long_jump_hook_callback, NULL, 1, 0)); + + + uc_assert_err( + UC_ERR_FETCH_UNMAPPED, + uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0)); + + OK(uc_reg_read(uc, UC_X86_REG_RAX, &r_rax)); + OK(uc_reg_read(uc, UC_X86_REG_RIP, &r_rip)); + TEST_CHECK(r_rax == 0x7FFFFFFFFFFFFF00); + TEST_CHECK(r_rip == 0x7FFFFFFFFFFFFF00); + + OK(uc_close(uc)); +} + + TEST_LIST = { {"test_x86_in", test_x86_in}, {"test_x86_out", test_x86_out}, @@ -1013,4 +1098,6 @@ TEST_LIST = { {"test_x86_64_nested_emu_start_error", test_x86_64_nested_emu_start_error}, {"test_x86_eflags_reserved_bit", test_x86_eflags_reserved_bit}, {"test_x86_nested_uc_emu_start_exits", test_x86_nested_uc_emu_start_exits}, + {"test_x86_correct_address_in_small_jump_hook", test_x86_correct_address_in_small_jump_hook}, + {"test_x86_correct_address_in_long_jump_hook", test_x86_correct_address_in_long_jump_hook}, {NULL, NULL}};