Fix #19990 - Fix aoml for non-x86 targets and add tests ##analysis

This commit is contained in:
pancake 2022-04-25 23:41:58 +02:00 committed by pancake
parent cfd66d0d28
commit abf317753c
6 changed files with 112 additions and 17 deletions

View File

@ -193,13 +193,13 @@ static int analop_esil(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len
*str[i] = 0;
ARG (i);
}
switch (insn->id) {
//case RISCV_INS_NOP:
// r_strbuf_setf (&op->esil, ",");
// break;
}
}
switch (insn->id) {
//case RISCV_INS_NOP:
// r_strbuf_setf (&op->esil, ",");
// break;
}
return 0;
}
@ -333,6 +333,9 @@ static int analop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len,
omode = mode;
obits = bits;
}
if (!op) {
return -1;
}
// XXX no arch->cpu ?!?! CS_MODE_MICRO, N64
op->addr = addr;
if (len < 4) {

View File

@ -149,7 +149,12 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
op->size = 4;
r_strbuf_set (&op->buf_asm, "");
}
bool needs_init = cd != 0;
if (!cd || mode != omode) {
if (!needs_init) {
cs_close (&cd);
cd = 0;
}
ret = (bits == 64)?
cs_open (CS_ARCH_ARM64, mode, &cd):
cs_open (CS_ARCH_ARM, mode, &cd);
@ -158,11 +163,13 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
goto beach;
}
}
cs_option (cd, CS_OPT_SYNTAX, (a->config->syntax == R_ASM_SYNTAX_REGNUM)
? CS_OPT_SYNTAX_NOREGNAME
: CS_OPT_SYNTAX_DEFAULT);
cs_option (cd, CS_OPT_DETAIL, R_STR_ISNOTEMPTY (features) ? CS_OPT_ON: CS_OPT_OFF);
cs_option (cd, CS_OPT_DETAIL, CS_OPT_ON);
if (!needs_init) {
cs_option (cd, CS_OPT_SYNTAX, (a->config->syntax == R_ASM_SYNTAX_REGNUM)
? CS_OPT_SYNTAX_NOREGNAME
: CS_OPT_SYNTAX_DEFAULT);
cs_option (cd, CS_OPT_DETAIL, R_STR_ISNOTEMPTY (features) ? CS_OPT_ON: CS_OPT_OFF);
cs_option (cd, CS_OPT_DETAIL, CS_OPT_ON);
}
if (!buf) {
goto beach;
}
@ -214,7 +221,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
}
cs_free (insn, n);
beach:
cs_close (&cd);
// cs_close (&cd);
if (op) {
if (!*r_strbuf_get (&op->buf_asm)) {
r_strbuf_set (&op->buf_asm, "invalid");

View File

@ -1,4 +1,4 @@
/* radare2 - LGPL - Copyright 2019-2021 - pancake */
/* radare2 - LGPL - Copyright 2019-2022 - pancake */
#include <r_asm.h>
#include <r_lib.h>
@ -11,10 +11,11 @@ static csh cd = 0;
static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
cs_insn* insn;
int size = 4;
int mode = (a->config->bits == 64)? CS_MODE_RISCV64 : CS_MODE_RISCV32;
op->size = 4;
if (cd != 0) {
cs_close (&cd);
cd = 0;
}
int ret = cs_open (CS_ARCH_RISCV, mode, &cd);
if (ret) {
@ -28,6 +29,9 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
}
cs_option (cd, CS_OPT_DETAIL, CS_OPT_OFF);
#endif
if (!op) {
goto fin;
}
int n = cs_disasm (cd, (ut8*)buf, len, a->pc, 1, &insn);
if (n < 1) {
r_asm_op_set_asm (op, "invalid");
@ -37,7 +41,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
if (insn->size < 1) {
goto beach;
}
op->size = insn->size;
size = op->size = insn->size;
char *str = r_str_newf ("%s%s%s", insn->mnemonic, insn->op_str[0]? " ": "", insn->op_str);
if (str) {
r_str_replace_char (str, '$', 0);
@ -49,7 +53,7 @@ static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
beach:
// cs_close (&cd);
fin:
return op->size;
return size;
}
RAsmPlugin r_asm_plugin_riscv_cs = {

View File

@ -1,6 +1,6 @@
static char *mnemonics(RAsm *a, int id, bool json) {
int i;
a->cur->disassemble (a, NULL, NULL, -1);
(void) a->cur->disassemble (a, NULL, NULL, -1);
if (id != -1) {
const char *name = cs_insn_name (cd, id);
if (json) {
@ -16,7 +16,7 @@ static char *mnemonics(RAsm *a, int id, bool json) {
} else {
buf = r_strbuf_new ("");
}
for (i = 1; ; i++) {
for (i = 1; i < 8000; i++) {
const char *op = cs_insn_name (cd, i);
if (!op) {
break;

View File

@ -7775,10 +7775,12 @@ static void cmd_anal_opcode(RCore *core, const char *input) {
if (input[1] == ' ' && !IS_DIGIT (input[2])) {
r_cons_printf ("%d\n", r_asm_mnemonics_byname (core->rasm, input + 2));
} else {
// "aoml"
const int id = (input[1] == ' ')
?(int)r_num_math (core->num, input + 2): -1;
char *ops = r_asm_mnemonics (core->rasm, id, input[1] == 'j');
if (ops) {
r_str_trim (ops);
r_cons_println (ops);
free (ops);
}

View File

@ -10,6 +10,85 @@ type: mov
EOF
RUN
NAME=aom-x86
FILE=-
CMDS=<<EOF
e asm.arch=x86
e asm.bits=64
aom
aoml~?
e asm.bits=32
aoml~?
e asm.bits=16
aoml~?
EOF
EXPECT=<<EOF
add
1523
1523
1523
EOF
RUN
NAME=aom-arm
FILE=-
CMDS=<<EOF
e asm.arch=arm
e asm.bits=64
aom
aoml~?
e asm.bits=32
aoml~?
e asm.bits=16
aoml~?
EOF
EXPECT=<<EOF
invalid
949
471
471
EOF
RUN
NAME=aom-mips
FILE=-
CMDS=<<EOF
e asm.arch=mips
e asm.bits=64
aom
aoml~?
e asm.bits=32
aoml~?
e asm.bits=16
aoml~?
EOF
EXPECT=<<EOF
nop
625
625
625
EOF
RUN
NAME=aom-riscv
FILE=-
CMDS=<<EOF
e asm.arch=riscv.cs
e asm.bits=64
aom
aoml~?
e asm.bits=32
aoml~?
e asm.bits=16
aoml~?
EOF
EXPECT=<<EOF
272
272
272
EOF
RUN
NAME=aof
FILE=-
CMDS=<<EOF