From ec0ed8ee942fecd5168ef92f9076af1eb7fce0d7 Mon Sep 17 00:00:00 2001 From: Nguyen Anh Quynh Date: Mon, 2 Dec 2013 13:55:38 +0800 Subject: [PATCH] semantics: add insn group JUMP, so now we can check if this insn is branching --- arch/AArch64/mapping.c | 11 +++++++++++ arch/ARM/mapping.c | 11 +++++++++++ arch/Mips/mapping.c | 22 ++++++++++++++++++++++ arch/X86/mapping.c | 11 +++++++++++ include/arm.h | 2 ++ include/arm64.h | 1 + include/mips.h | 2 ++ include/x86.h | 2 ++ tests/test_detail.c | 1 + 9 files changed, 63 insertions(+) diff --git a/arch/AArch64/mapping.c b/arch/AArch64/mapping.c index 895d2a29d..e95604428 100644 --- a/arch/AArch64/mapping.c +++ b/arch/AArch64/mapping.c @@ -1884,6 +1884,17 @@ void AArch64_get_insn_id(cs_insn *insn, unsigned int id) // call cs_reg_write() with handle = 1 to pass handle check // we only need to find if this insn modifies ARM64_REG_NZCV insn->arm64.update_flags = cs_reg_write(1, insn, ARM64_REG_NZCV); + + if (insns[i].branch || insns[i].indirect_branch) { + // this insn also belongs to JUMP group + int j; + for (j = 0; j < ARR_SIZE(insns[i].groups); j++) { + if (insn->groups[j] == 0) { + insn->groups[j] = ARM64_GRP_JUMP; + break; + } + } + } } } diff --git a/arch/ARM/mapping.c b/arch/ARM/mapping.c index 164150797..a7b859543 100644 --- a/arch/ARM/mapping.c +++ b/arch/ARM/mapping.c @@ -2306,6 +2306,17 @@ void ARM_get_insn_id(cs_insn *insn, unsigned int id) memcpy(insn->regs_write, insns[i].regs_mod, sizeof(insns[i].regs_mod)); memcpy(insn->groups, insns[i].groups, sizeof(insns[i].groups)); insn->arm.update_flags = cs_reg_write(1, insn, ARM_REG_CPSR); + + if (insns[i].branch || insns[i].indirect_branch) { + // this insn also belongs to JUMP group + int j; + for (j = 0; j < ARR_SIZE(insns[i].groups); j++) { + if (insn->groups[j] == 0) { + insn->groups[j] = ARM_GRP_JUMP; + break; + } + } + } } } diff --git a/arch/Mips/mapping.c b/arch/Mips/mapping.c index fe0872b9a..8127c340e 100644 --- a/arch/Mips/mapping.c +++ b/arch/Mips/mapping.c @@ -1390,6 +1390,17 @@ void Mips_get_insn_id(cs_insn *insn, unsigned int id) memcpy(insn->regs_write, alias_insns[i].regs_mod, sizeof(alias_insns[i].regs_mod)); memcpy(insn->groups, alias_insns[i].groups, sizeof(alias_insns[i].groups)); + if (insns[i].branch || insns[i].indirect_branch) { + // this insn also belongs to JUMP group + int j; + for (j = 0; j < ARR_SIZE(insns[i].groups); j++) { + if (insn->groups[j] == 0) { + insn->groups[j] = MIPS_GRP_JUMP; + break; + } + } + } + return; } } @@ -1400,6 +1411,17 @@ void Mips_get_insn_id(cs_insn *insn, unsigned int id) memcpy(insn->regs_read, insns[i].regs_use, sizeof(insns[i].regs_use)); memcpy(insn->regs_write, insns[i].regs_mod, sizeof(insns[i].regs_mod)); memcpy(insn->groups, insns[i].groups, sizeof(insns[i].groups)); + + if (insns[i].branch || insns[i].indirect_branch) { + // this insn also belongs to JUMP group + int j; + for (j = 0; j < ARR_SIZE(insns[i].groups); j++) { + if (insn->groups[j] == 0) { + insn->groups[j] = MIPS_GRP_JUMP; + break; + } + } + } } } diff --git a/arch/X86/mapping.c b/arch/X86/mapping.c index c6b79465f..e6a3c2b1e 100644 --- a/arch/X86/mapping.c +++ b/arch/X86/mapping.c @@ -6579,6 +6579,17 @@ void X86_get_insn_id(cs_insn *insn, unsigned int id) memcpy(insn->regs_read, insns[i].regs_use, sizeof(insns[i].regs_use)); memcpy(insn->regs_write, insns[i].regs_mod, sizeof(insns[i].regs_mod)); memcpy(insn->groups, insns[i].groups, sizeof(insns[i].groups)); + + if (insns[i].branch || insns[i].indirect_branch) { + // this insn also belongs to JUMP group + int j; + for (j = 0; j < ARR_SIZE(insns[i].groups); j++) { + if (insn->groups[j] == 0) { + insn->groups[j] = X86_GRP_JUMP; + break; + } + } + } } } diff --git a/include/arm.h b/include/arm.h index b18c5658c..3151ec8f8 100644 --- a/include/arm.h +++ b/include/arm.h @@ -670,6 +670,8 @@ typedef enum arm_insn_group { ARM_GRP_CRC, ARM_GRP_DPVFP, ARM_GRP_V6M, + + ARM_GRP_JUMP, // jump insn (including conditional+direct+indirect jumps) ARM_GRP_MAX, } arm_insn_group; diff --git a/include/arm64.h b/include/arm64.h index 877789762..dca5bfe58 100644 --- a/include/arm64.h +++ b/include/arm64.h @@ -706,6 +706,7 @@ typedef enum arm64_insn { ARM64_INS_NGC, ARM64_INS_NEG, + ARM64_GRP_JUMP, // jump insn (including conditional+direct+indirect jumps) ARM64_INS_MAX, } arm64_insn; diff --git a/include/mips.h b/include/mips.h index de65e78f9..4d0509217 100644 --- a/include/mips.h +++ b/include/mips.h @@ -669,6 +669,8 @@ typedef enum mips_insn_group { MIPS_GRP_NONANSFPMATH, MIPS_GRP_NOTFP64BIT, MIPS_GRP_RELOCSTATIC, + + MIPS_GRP_JUMP, // jump insn (including conditional+direct+indirect jumps) MIPS_GRP_MAX, } mips_insn_group; diff --git a/include/x86.h b/include/x86.h index 7cf8cf5de..d397117f1 100644 --- a/include/x86.h +++ b/include/x86.h @@ -1430,6 +1430,8 @@ typedef enum x86_insn_group { X86_GRP_CDI, X86_GRP_ERI, X86_GRP_TBM, + + X86_GRP_JUMP, // jump insn (including conditional+direct+indirect jumps) X86_GRP_MAX } x86_insn_group; diff --git a/tests/test_detail.c b/tests/test_detail.c index fb35bce0b..d1961900d 100644 --- a/tests/test_detail.c +++ b/tests/test_detail.c @@ -47,6 +47,7 @@ static void test() //#define ARM64_CODE "\x20\xfc\x02\x9b" // mneg x0, x1, x2 #define ARM64_CODE "\x21\x7c\x02\x9b\x21\x7c\x00\x53\x00\x40\x21\x4b\xe1\x0b\x40\xb9\x10\x20\x21\x1e" //#define THUMB_CODE "\x0a\xbf" // itet eq +//#define X86_CODE32 "\x77\x04" // ja +6 struct platform platforms[] = { {