From c4a0813f4aa708f5235ec420c21c335ae2eaa1ae Mon Sep 17 00:00:00 2001 From: Mio Date: Wed, 31 Aug 2022 23:26:01 +0800 Subject: [PATCH] Add a test for infinite loop when sync-ing pc for UC_HOOL_BLOCK #1661 --- tests/unit/test_arm64.c | 43 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_arm64.c b/tests/unit/test_arm64.c index 8afdd0ec..bd64a4c9 100644 --- a/tests/unit/test_arm64.c +++ b/tests/unit/test_arm64.c @@ -1,10 +1,14 @@ +#include "acutest.h" +#include "unicorn/unicorn.h" #include "unicorn_test.h" +#include +#include const uint64_t code_start = 0x1000; const uint64_t code_len = 0x4000; static void uc_common_setup(uc_engine **uc, uc_arch arch, uc_mode mode, - const char *code, uint64_t size, uc_cpu_arm cpu) + const char *code, uint64_t size, uc_cpu_arm64 cpu) { OK(uc_open(arch, mode, uc)); OK(uc_ctl_set_cpu_model(*uc, cpu)); @@ -291,6 +295,42 @@ static void test_arm64_correct_address_in_long_jump_hook(void) OK(uc_close(uc)); } +static void test_arm64_block_sync_pc_cb(uc_engine* uc, uint64_t addr, uint32_t size, void* data) +{ + uint64_t val = code_start; + bool first = *(bool*)data; + if (first) { + OK(uc_reg_write(uc, UC_ARM64_REG_PC, (void*)&val)); + *(bool*)data = false; + } +} + +static void test_arm64_block_sync_pc(void) +{ + uc_engine* uc; + // add x0, x0, #1234;bl t;t:mov x1, #5678; + const char code[] = "\x00\x48\x13\x91\x01\x00\x00\x94\xc1\xc5\x82\xd2"; + uc_hook hk; + uint64_t x0; + bool data = true; + + uc_common_setup(&uc, UC_ARCH_ARM64, UC_MODE_ARM, code, sizeof(code) - 1, + UC_CPU_ARM64_A72); + OK(uc_hook_add(uc, &hk, UC_HOOK_BLOCK, test_arm64_block_sync_pc_cb, (void*)&data, code_start + 8, code_start + 12)); + + x0 = 0; + OK(uc_reg_write(uc, UC_ARM64_REG_X0, (void*)&x0)); + OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0)); + + OK(uc_reg_read(uc, UC_ARM64_REG_X0, (void*)&x0)); + + TEST_CHECK(x0 == (1234 * 2)); + + OK(uc_hook_del(uc, hk)); + OK(uc_close(uc)); +} + + TEST_LIST = {{"test_arm64_until", test_arm64_until}, {"test_arm64_code_patching", test_arm64_code_patching}, {"test_arm64_code_patching_count", test_arm64_code_patching_count}, @@ -301,4 +341,5 @@ TEST_LIST = {{"test_arm64_until", test_arm64_until}, test_arm64_correct_address_in_small_jump_hook}, {"test_arm64_correct_address_in_long_jump_hook", test_arm64_correct_address_in_long_jump_hook}, + {"test_arm64_block_sync_pc", test_arm64_block_sync_pc}, {NULL, NULL}};