Skip to content

Commit 8720632

Browse files
wtdcodesaicao
andcommitted
Add the unit test from saicao
Co-authored-by: Sai Cao <[email protected]>
1 parent 8442eb6 commit 8720632

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

tests/unit/test_arm64.c

+49
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,54 @@ static void test_arm64_mem_prot_regress(void)
599599
OK(uc_close(uc));
600600
}
601601

602+
static bool test_arm64_mem_read_write_cb(uc_engine *uc, int type,
603+
uint64_t address, int size,
604+
int64_t value, void *user_data)
605+
{
606+
uint64_t *count = (uint64_t *)user_data;
607+
switch (type) {
608+
case UC_MEM_READ:
609+
count[0]++;
610+
break;
611+
case UC_MEM_WRITE:
612+
count[1]++;
613+
break;
614+
}
615+
616+
return 0;
617+
}
618+
static void test_arm64_mem_hook_read_write(void)
619+
{
620+
uc_engine *uc;
621+
// ldp x1, x2, [sp]
622+
// stp x1, x2,[sp]
623+
// ldp x1, x2, [sp]
624+
// stp x1, x2,[sp]
625+
const char code[] = {0xe1, 0x0b, 0x40, 0xa9, 0xe1, 0x0b, 0x00, 0xa9,
626+
0xe1, 0x0b, 0x40, 0xa9, 0xe1, 0x0b, 0x00, 0xa9};
627+
uint64_t r_sp;
628+
r_sp = 0x16db6a040;
629+
uc_hook hk;
630+
uint64_t counter[2] = {0, 0};
631+
632+
uc_common_setup(&uc, UC_ARCH_ARM64, UC_MODE_ARM, code, sizeof(code),
633+
UC_CPU_ARM64_A72);
634+
635+
uc_reg_write(uc, UC_ARM64_REG_SP, &r_sp);
636+
uc_mem_map(uc, 0x16db68000, 1024 * 16, UC_PROT_ALL);
637+
638+
OK(uc_hook_add(uc, &hk, UC_HOOK_MEM_READ, test_arm64_mem_read_write_cb,
639+
counter, 1, 0));
640+
OK(uc_hook_add(uc, &hk, UC_HOOK_MEM_WRITE, test_arm64_mem_read_write_cb,
641+
counter, 1, 0));
642+
643+
uc_assert_err(UC_ERR_OK, uc_emu_start(uc, code_start,
644+
code_start + sizeof(code), 0, 0));
645+
646+
TEST_CHECK(counter[0] == 4 && counter[1] == 4);
647+
OK(uc_close(uc));
648+
}
649+
602650
TEST_LIST = {{"test_arm64_until", test_arm64_until},
603651
{"test_arm64_code_patching", test_arm64_code_patching},
604652
{"test_arm64_code_patching_count", test_arm64_code_patching_count},
@@ -615,4 +663,5 @@ TEST_LIST = {{"test_arm64_until", test_arm64_until},
615663
{"test_arm64_mmu", test_arm64_mmu},
616664
{"test_arm64_pc_wrap", test_arm64_pc_wrap},
617665
{"test_arm64_mem_prot_regress", test_arm64_mem_prot_regress},
666+
{"test_arm64_mem_hook_read_write", test_arm64_mem_hook_read_write},
618667
{NULL, NULL}};

0 commit comments

Comments
 (0)