From 24e1ec8e74eca9dc8eaf50cf0c7263bfee3fd9ab Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 22 Jul 2023 13:56:32 -0500 Subject: [PATCH 1/3] Add CPSR to implicit reads for every instruction which has a predicate. --- Mapping.c | 19 +++++++++++++++++++ Mapping.h | 1 + arch/ARM/ARMMapping.c | 1 + 3 files changed, 21 insertions(+) diff --git a/Mapping.c b/Mapping.c index 305e616962..60db35eec6 100644 --- a/Mapping.c +++ b/Mapping.c @@ -85,6 +85,25 @@ void map_add_implicit_write(MCInst *MI, uint32_t Reg) } } +/// Adds a register to the implicit read register list. +/// It will not add the same register twice. +void map_add_implicit_read(MCInst *MI, uint32_t Reg) +{ + if (!MI->flat_insn->detail) + return; + + uint16_t *regs_read = MI->flat_insn->detail->regs_read; + for (int i = 0; i < MAX_IMPL_W_REGS; ++i) { + if (i == MI->flat_insn->detail->regs_read_count) { + regs_read[i] = Reg; + MI->flat_insn->detail->regs_read_count++; + return; + } + if (regs_read[i] == Reg) + return; + } +} + /// Removes a register from the implicit write register list. void map_remove_implicit_write(MCInst *MI, uint32_t Reg) { diff --git a/Mapping.h b/Mapping.h index 87aaa1e4b2..4398d79268 100644 --- a/Mapping.h +++ b/Mapping.h @@ -98,6 +98,7 @@ int name2id(const name_map *map, int max, const char *name); const char *id2name(const name_map *map, int max, const unsigned int id); void map_add_implicit_write(MCInst *MI, uint32_t Reg); +void map_add_implicit_read(MCInst *MI, uint32_t Reg); void map_remove_implicit_write(MCInst *MI, uint32_t Reg); void map_implicit_reads(MCInst *MI, const insn_map *imap); diff --git a/arch/ARM/ARMMapping.c b/arch/ARM/ARMMapping.c index 07d5b408fd..841a8b9167 100644 --- a/arch/ARM/ARMMapping.c +++ b/arch/ARM/ARMMapping.c @@ -788,6 +788,7 @@ static void add_cs_detail_general(MCInst *MI, arm_op_group op_group, return; } ARM_get_detail(MI)->cc = CC; + map_add_implicit_read(MI, ARM_REG_CPSR); break; } case ARM_OP_GROUP_VPTPredicateOperand: { From 9153bb30a54658a23566e58f29657d45ff6f83a9 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 22 Jul 2023 13:56:45 -0500 Subject: [PATCH 2/3] Formatting --- Mapping.c | 3 ++- Mapping.h | 6 +++--- arch/ARM/ARMMapping.c | 3 ++- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Mapping.c b/Mapping.c index 60db35eec6..23a21c0fd1 100644 --- a/Mapping.c +++ b/Mapping.c @@ -179,7 +179,8 @@ void map_implicit_writes(MCInst *MI, const insn_map *imap) } /// Adds a given group to @MI->flat_insn. -void add_group(MCInst *MI, unsigned /* arch_group */ group) { +void add_group(MCInst *MI, unsigned /* arch_group */ group) +{ #ifndef CAPSTONE_DIET if (!MI->flat_insn->detail) return; diff --git a/Mapping.h b/Mapping.h index 4398d79268..7dd7bed04b 100644 --- a/Mapping.h +++ b/Mapping.h @@ -21,9 +21,9 @@ typedef struct insn_map { unsigned short mapid; // The Capstone instruction id #ifndef CAPSTONE_DIET uint16_t regs_use[MAX_IMPL_R_REGS]; ///< list of implicit registers used by - ///< this instruction + ///< this instruction uint16_t regs_mod[MAX_IMPL_W_REGS]; ///< list of implicit registers modified - ///< by this instruction + ///< by this instruction unsigned char groups [MAX_NUM_GROUPS]; ///< list of group this instruction belong to bool branch; // branch instruction? @@ -47,7 +47,7 @@ typedef struct { uint8_t /* cs_ac_type */ access; ///< The access type (read, write) uint8_t /* cs_data_type */ dtypes[MAX_NO_DATA_TYPES]; ///< List of op types. Terminated by - ///< CS_DATA_TYPE_LAST + ///< CS_DATA_TYPE_LAST } mapping_op; #define MAX_NO_INSN_MAP_OPS 16 diff --git a/arch/ARM/ARMMapping.c b/arch/ARM/ARMMapping.c index 841a8b9167..f2eb6a33db 100644 --- a/arch/ARM/ARMMapping.c +++ b/arch/ARM/ARMMapping.c @@ -1043,7 +1043,8 @@ static void add_cs_detail_general(MCInst *MI, arm_op_group op_group, SYSm); if (TheReg) { ARM_set_detail_op_sysreg( - MI, TheReg->sysreg.mclasssysreg, IsOutReg); + MI, TheReg->sysreg.mclasssysreg, + IsOutReg); return; } From 40e5c0867f0ef7283febe5372ce59b9180bb0ae9 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Sat, 22 Jul 2023 14:02:17 -0500 Subject: [PATCH 3/3] Add forgotten check --- arch/ARM/ARMMapping.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/arch/ARM/ARMMapping.c b/arch/ARM/ARMMapping.c index f2eb6a33db..ebb097aa06 100644 --- a/arch/ARM/ARMMapping.c +++ b/arch/ARM/ARMMapping.c @@ -788,7 +788,8 @@ static void add_cs_detail_general(MCInst *MI, arm_op_group op_group, return; } ARM_get_detail(MI)->cc = CC; - map_add_implicit_read(MI, ARM_REG_CPSR); + if (CC != ARMCC_AL) + map_add_implicit_read(MI, ARM_REG_CPSR); break; } case ARM_OP_GROUP_VPTPredicateOperand: {