Skip to content

Commit 1ba25de

Browse files
authored
Fix UC_HOOK_MEM on arm32 (#2091)
1 parent d7c0497 commit 1ba25de

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

qemu/tcg/arm/tcg-target.inc.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -1601,7 +1601,11 @@ static void tcg_out_qemu_ld(TCGContext *s, const TCGArg *args, bool is64)
16011601
/* This a conditional BL only to load a pointer within this opcode into LR
16021602
for the slow path. We will not be using the value for a tail call. */
16031603
label_ptr = s->code_ptr;
1604-
tcg_out_bl(s, COND_NE, 0);
1604+
if (!tcg_uc_has_hookmem(s)) {
1605+
tcg_out_bl(s, COND_NE, 0);
1606+
} else {
1607+
tcg_out_bl(s, COND_AL, 0);
1608+
}
16051609

16061610
tcg_out_qemu_ld_index(s, opc, datalo, datahi, addrlo, addend);
16071611

@@ -1733,7 +1737,11 @@ static void tcg_out_qemu_st(TCGContext *s, const TCGArg *args, bool is64)
17331737

17341738
/* The conditional call must come last, as we're going to return here. */
17351739
label_ptr = s->code_ptr;
1736-
tcg_out_bl(s, COND_NE, 0);
1740+
if (!tcg_uc_has_hookmem(s)) {
1741+
tcg_out_bl(s, COND_NE, 0);
1742+
} else {
1743+
tcg_out_bl(s, COND_AL, 0);
1744+
}
17371745

17381746
add_qemu_ldst_label(s, false, oi, datalo, datahi, addrlo, addrhi,
17391747
s->code_ptr, label_ptr);

tests/unit/test_arm.c

+48-1
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,52 @@ static void test_armeb_be32_thumb2(void)
815815
OK(uc_close(uc));
816816
}
817817

818+
static bool test_arm_mem_read_write_cb(uc_engine *uc, int type,
819+
uint64_t address, int size,
820+
int64_t value, void *user_data)
821+
{
822+
uint64_t *count = (uint64_t *)user_data;
823+
switch (type) {
824+
case UC_MEM_READ:
825+
count[0]++;
826+
break;
827+
case UC_MEM_WRITE:
828+
count[1]++;
829+
break;
830+
}
831+
832+
return 0;
833+
}
834+
static void test_arm_mem_hook_read_write(void)
835+
{
836+
uc_engine *uc;
837+
// ldr r1, [sp]
838+
// str r1, [sp, #4]
839+
// ldr r2, [sp, #4]
840+
// str r2, [sp]
841+
const char code[] = "\x00\x10\x9d\xe5\x04\x10\x8d\xe5\x04\x20\x9d\xe5\x00\x20\x8d\xe5";
842+
uint32_t r_sp;
843+
r_sp = 0x9000;
844+
uc_hook hk;
845+
uint64_t counter[2] = {0, 0};
846+
847+
uc_common_setup(&uc, UC_ARCH_ARM, UC_MODE_ARM, code, sizeof(code) - 1,
848+
UC_CPU_ARM_CORTEX_A15);
849+
850+
uc_reg_write(uc, UC_ARM_REG_SP, &r_sp);
851+
uc_mem_map(uc, 0x8000, 1024 * 16, UC_PROT_ALL);
852+
853+
OK(uc_hook_add(uc, &hk, UC_HOOK_MEM_READ, test_arm_mem_read_write_cb,
854+
counter, 1, 0));
855+
OK(uc_hook_add(uc, &hk, UC_HOOK_MEM_WRITE, test_arm_mem_read_write_cb,
856+
counter, 1, 0));
857+
858+
OK(uc_emu_start(uc, code_start, code_start + sizeof(code) - 1, 0, 0));
859+
860+
TEST_CHECK(counter[0] == 2 && counter[1] == 2);
861+
OK(uc_close(uc));
862+
}
863+
818864
TEST_LIST = {{"test_arm_nop", test_arm_nop},
819865
{"test_arm_thumb_sub", test_arm_thumb_sub},
820866
{"test_armeb_sub", test_armeb_sub},
@@ -840,4 +886,5 @@ TEST_LIST = {{"test_arm_nop", test_arm_nop},
840886
{"test_arm_context_save", test_arm_context_save},
841887
{"test_arm_thumb2", test_arm_thumb2},
842888
{"test_armeb_be32_thumb2", test_armeb_be32_thumb2},
843-
{NULL, NULL}};
889+
{"test_arm_mem_hook_read_write", test_arm_mem_hook_read_write},
890+
{NULL, NULL}};

0 commit comments

Comments
 (0)