From 6e1711172d344d8b4295e0fd14792b19f2facf8b Mon Sep 17 00:00:00 2001 From: Jack Gallagher Date: Wed, 17 Jul 2024 14:01:02 +0100 Subject: [PATCH] i#2417 AArch64: Make stolen-reg test more reliable (#6878) The test client prints a message for any load/store that uses the stolen register as the index, assuming only the two instructions in the test app will be found. I have seen in at least one case the test outputs an extra line because it found an additional instruction that used the stolen register as the index. Presumably this instruction came from a system library so this problem is OS dependent. I have made the test more robust by changing the client to look for mov , #0 before the load/store which is present in the test app but unlikely to appear before matching load/stores in system libraries. Issue: #2417 --- .../client-interface/stolen-reg-index.dll.c | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/suite/tests/client-interface/stolen-reg-index.dll.c b/suite/tests/client-interface/stolen-reg-index.dll.c index e5e0b67691d..c43ae16c943 100644 --- a/suite/tests/client-interface/stolen-reg-index.dll.c +++ b/suite/tests/client-interface/stolen-reg-index.dll.c @@ -113,6 +113,15 @@ insert_get_addr(void *drcontext, instrlist_t *ilist, instr_t *instr, opnd_t mref return true; } +static bool +prev_is_mov_0(instr_t *inst, reg_id_t dst_reg) +{ + instr_t *prev = instr_get_prev(inst); + ptr_int_t value = ~0; + return prev != NULL && instr_is_mov_constant(prev, &value) && value == 0 && + opnd_get_reg(instr_get_dst(prev, 0)) == dst_reg; +} + static dr_emit_flags_t event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *inst, bool for_trace, bool translating, void *user_data) @@ -128,9 +137,11 @@ event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *inst if (instr_writes_memory(inst)) { opnd = instr_get_dst(inst, 0); if (opnd_is_memory_reference(opnd) && opnd_is_base_disp(opnd)) { - if (opnd_get_index(opnd) == stolen && opnd_get_base(opnd) == DR_REG_X0) + if (opnd_get_index(opnd) == stolen && opnd_get_base(opnd) == DR_REG_X0 && + prev_is_mov_0(inst, stolen)) dr_printf("store memref with index reg X28\n"); - if (opnd_get_index(opnd) == DR_REG_W28 && opnd_get_base(opnd) == DR_REG_X0) + if (opnd_get_index(opnd) == DR_REG_W28 && opnd_get_base(opnd) == DR_REG_X0 && + prev_is_mov_0(inst, DR_REG_W28)) dr_printf("store memref with index reg W28\n"); if (insert_get_addr(drcontext, bb, inst, instr_get_dst(inst, 0))) return DR_EMIT_DEFAULT; @@ -145,9 +156,11 @@ event_app_instruction(void *drcontext, void *tag, instrlist_t *bb, instr_t *inst if (instr_reads_memory(inst)) { opnd = instr_get_src(inst, 0); if (opnd_is_memory_reference(opnd) && opnd_is_base_disp(opnd)) { - if (opnd_get_index(opnd) == stolen && opnd_get_base(opnd) == DR_REG_X0) + if (opnd_get_index(opnd) == stolen && opnd_get_base(opnd) == DR_REG_X0 && + prev_is_mov_0(inst, stolen)) dr_printf("load memref with index reg X28\n"); - if (opnd_get_index(opnd) == DR_REG_W28 && opnd_get_base(opnd) == DR_REG_X0) + if (opnd_get_index(opnd) == DR_REG_W28 && opnd_get_base(opnd) == DR_REG_X0 && + prev_is_mov_0(inst, DR_REG_W28)) dr_printf("load memref with index reg W28\n"); if (insert_get_addr(drcontext, bb, inst, instr_get_src(inst, 0))) return DR_EMIT_DEFAULT;