Fix asm.acur supporting arch, anal and asm plugins ##arch

* Fix asm.acur supporting arch, anal and asm plugins ##arch

* Fixes the arch/bits combo selection order issue for riscv tests
* Move the riscv arch plugin to the new plugin structure
* Deprecate r_asm_setup ()
This commit is contained in:
pancake 2022-11-26 17:49:10 +01:00 committed by GitHub
parent 6e351d0512
commit 37b279b6f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
12 changed files with 88 additions and 70 deletions

View File

@ -4649,26 +4649,17 @@ static char *arm_mnemonics(RAnal *a, int id, bool json) {
#include "anal_arm_regprofile.inc"
static int archinfo(RAnal *anal, int q) {
if (q == R_ANAL_ARCHINFO_DATA_ALIGN) {
return 4;
}
if (q == R_ANAL_ARCHINFO_ALIGN) {
if (anal && anal->config->bits == 16) {
switch (q) {
case R_ANAL_ARCHINFO_DATA_ALIGN:
case R_ANAL_ARCHINFO_INV_OP_SIZE:
case R_ANAL_ARCHINFO_MAX_OP_SIZE:
break;
case R_ANAL_ARCHINFO_MIN_OP_SIZE:
case R_ANAL_ARCHINFO_ALIGN: // espai de jocs
if (anal->config && anal->config->bits == 16) {
return 2;
}
return 4;
}
if (q == R_ANAL_ARCHINFO_INV_OP_SIZE) {
return 4;
}
if (q == R_ANAL_ARCHINFO_MAX_OP_SIZE) {
return 4;
}
if (q == R_ANAL_ARCHINFO_MIN_OP_SIZE) {
if (anal && anal->config->bits == 16) {
return 2;
}
return 4;
break;
}
return 4; // XXX
}

View File

@ -12,12 +12,12 @@ r_arch_sources = [
'p/arch_jdh8.c',
'p/arch_pickle.c',
'p/arch_rsp.c',
'p/riscv/plugin.c',
'p/rsp/rsp_idec.c',
'p/arch_sh.c',
'p/sh/gnu/sh-dis.c',
'p/arch_v810.c',
'p/v810/v810_disas.c',
'p/arch_riscv.c'
]
# must be deleted when anal refactor is done

View File

@ -1,4 +1,4 @@
OBJ_RISCV=arch_riscv.o
OBJ_RISCV=p/riscv/plugin.o
STATIC_OBJ+=${OBJ_RISCV}
TARGET_RISCV=arch_riscv.${EXT_SO}

View File

@ -3,9 +3,9 @@
#include <r_lib.h>
#include <r_asm.h>
#include <r_arch.h>
#include "riscv/riscv-opc.c"
#include "riscv/riscv.c"
#include "riscv/riscvasm.c"
#include "./riscv-opc.c"
#include "./riscv.c"
#include "./riscvasm.c"
#define RISCVARGSMAX (8)
#define RISCVARGSIZE (64)
#define RISCVARGN(x) ((x)->arg[(x)->num++])
@ -245,7 +245,7 @@ static void get_riscv_args(riscv_args_t *args, const char *d, insn_t l, ut64 pc)
switch (csr) {
#define DECLARE_CSR(name, num) case num: csr_name = #name; break;
#undef RISCV_ENCODING_H
#include "riscv/riscv-opc.h"
#include "./riscv-opc.h"
#undef DECLARE_CSR
}
if (csr_name) {
@ -344,6 +344,8 @@ static bool riscv_decode(RArchSession *s, RAnalOp *op, RArchDecodeMask mask) {
const bool be = R_ARCH_CONFIG_IS_BIG_ENDIAN (s->config);
if (len < 2) {
op->size = 2;
free (op->mnemonic);
op->mnemonic = strdup ("truncated");
return -1;
}
@ -353,9 +355,13 @@ static bool riscv_decode(RArchSession *s, RAnalOp *op, RArchDecodeMask mask) {
word = r_read_ble16 (buf, be);
} else {
word = r_read_ble16 (buf, be);
#if 0
word = r_read_ble32 (buf, be);
op->type = R_ANAL_OP_TYPE_ILL;
free (op->mnemonic);
op->mnemonic = r_str_newf ("truncated %d", len);
return -1;
#endif
}
struct riscv_opcode *o = get_opcode (word);
@ -396,6 +402,8 @@ static bool riscv_decode(RArchSession *s, RAnalOp *op, RArchDecodeMask mask) {
if (!strncmp ("c.", o->name, 2)) {
name += 2;
op->size = 2;
} else {
op->size = 4;
}
#define ARG(x) (arg_n (&args, (x)))
get_riscv_args (&args, o->args, word, addr);

View File

@ -203,12 +203,6 @@ R_API RAsm *r_asm_new(void) {
return a;
}
R_API bool r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian) {
r_return_val_if_fail (a && arch, false);
bool ret = !r_asm_use (a, arch);
return ret | !r_asm_set_bits (a, bits);
}
// TODO must use the internal rparse api when both libraries are merged
R_API bool r_asm_sub_names_input(RAsm *a, const char *f) {
r_return_val_if_fail (a && f, false);
@ -296,7 +290,8 @@ R_API bool r_asm_use_assembler(RAsm *a, const char *name) {
r_list_foreach (a->plugins, iter, h) {
if (h->assemble && !strcmp (h->name, name)) {
a->acur = h;
// a->ecur = r_arch_use (a->arch); // create a new instance for `ecur`
// r_unref (a->ecur);
// a->ecur = a->ecur = r_arch_use (a->arch); // create a new instance for `ecur`
return true;
}
}
@ -340,13 +335,14 @@ static void load_asm_descriptions(RAsm *a, RAsmPlugin *p) {
free (r2prefix);
}
// TODO: this can be optimized using r_str_hash()
R_API bool r_asm_use(RAsm *a, const char *name) {
r_return_val_if_fail (a, false);
if (R_STR_ISEMPTY (name)) {
// that shouldnt be permitted imho, keep for backward compat
return false;
}
r_arch_config_use (a->config, name);
r_asm_use_assembler (a, name);
RAsmPlugin *h;
RListIter *iter;
char *dotname = strdup (name);
@ -369,8 +365,6 @@ R_API bool r_asm_use(RAsm *a, const char *name) {
char *arch = r_str_ndup (name, vv - name);
#if 0
r_arch_config_set_cpu (a->config, arch);
// r_asm_set_cpu (a, arch);
// h->arch = name;
#else
r_asm_set_cpu (a, arch);
#endif
@ -396,10 +390,11 @@ R_API bool r_asm_use(RAsm *a, const char *name) {
if (a->analb.anal) {
if (a->analb.use (a->analb.anal, name)) {
load_asm_descriptions (a, NULL);
// return true;
} else {
R_LOG_ERROR ("Cannot find '%s' asm/arch/anal plugin. See rasm2 -L, -LL or -LLL", name);
a->cur = NULL;
a->acur = NULL;
return true;
}
R_LOG_ERROR ("Cannot find '%s' asm/arch/anal plugin. See rasm2 -L, -LL or -LLL", name);
}
#if 0
// check if its a valid analysis plugin
@ -423,11 +418,15 @@ static bool has_bits(RAsmPlugin *h, int bits) {
}
R_DEPRECATE R_API int r_asm_set_bits(RAsm *a, int bits) {
a->config->bits = bits;
return true;
#if 0
if (has_bits (a->cur, bits)) {
a->config->bits = bits; // TODO : use OR? :)
return true;
}
return false;
#endif
}
R_API bool r_asm_set_big_endian(RAsm *a, bool b) {
@ -573,10 +572,15 @@ static bool assemblerMatches(RAsm *a, RAsmPlugin *h, const char *ends_with) {
static Ase find_assembler(RAsm *a, const char *kw) {
RAsmAssembleCallback aac = R_UNWRAP3 (a, acur, assemble);
if (!aac) {
aac = R_UNWRAP3 (a, cur, assemble);
if (aac) {
return aac;
}
RAsmPlugin *h;
RListIter *iter;
r_list_foreach (a->plugins, iter, h) {
if (assemblerMatches (a, h, kw)) {
a->acur = h;
if (kw) {
if (r_str_endswith (h->name, kw)) {
aac = h->assemble;
@ -670,40 +674,41 @@ R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf) {
}
r_str_case (b, false); // to-lower
r_asm_op_init (op);
if (a->cur) {
Ase ase = R_UNWRAP3 (a, acur, assemble);
Ase ase = find_assembler (a, NULL);
if (!ase) {
ase = find_assembler (a, ".ks");
if (!ase) {
ase = R_UNWRAP3 (a, cur, assemble);
}
if (!ase) {
/* find callback if no assembler support in current plugin */
ase = find_assembler (a, ".ks");
ase = find_assembler (a, ".nz");
#if 0
if (!ase) {
ase = find_assembler (a, ".nz");
if (!ase) {
ase = find_assembler (a, NULL);
}
ase = find_assembler (a, NULL);
}
#endif
}
if (!ase && a->analb.anal) {
// disassemble using the analysis plugin if found
ase = NULL;
RAnalOp aop;
a->analb.opinit (&aop);
ut8 buf[256] = {0};
ret = a->analb.encode (a->analb.anal, a->pc, b, buf, sizeof (buf));
}
if (!ase && a->analb.anal) {
// disassemble using the analysis plugin if found
ase = NULL;
RAnalOp aop;
a->analb.opinit (&aop);
ut8 buf[256] = {0};
ret = a->analb.encode (a->analb.anal, a->pc, b, buf, sizeof (buf));
if (ret > 0) {
r_strbuf_setbin (&op->buf, buf, R_MIN (ret, sizeof (buf)));
a->analb.opfini (&aop);
} else {
ret = ase (a, op, b);
}
} // else fail to assemble
a->analb.opfini (&aop);
} else if (ase) {
/* find callback if no assembler support in current plugin */
ret = ase (a, op, b);
}
// XXX delete this block, the ase thing should be setting asm, buf and hex
if (op && ret > 0) {
op->size = ret; // XXX shouldn't be necessary
r_asm_op_set_asm (op, b); // XXX ase should be updating this already, isn't?
ut8 *opbuf = (ut8*)r_strbuf_get (&op->buf);
r_asm_op_set_buf (op, opbuf, ret);
if (opbuf) {
r_asm_op_set_buf (op, opbuf, ret);
}
}
free (b);
return ret;
@ -717,6 +722,7 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
int ret;
// XXX move from io to archconfig!! and remove the dependency on core!
const size_t addrbytes = a->user? ((RCore *)a->user)->io->addrbytes: 1;
int mininstrsize = 1;
RAsmCode *acode = r_asm_code_new ();
if (!acode) {
@ -730,7 +736,7 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
r_asm_set_pc (a, pc + idx);
ret = r_asm_disassemble (a, &op, buf + idx, len - idx);
if (ret < 1) {
ret = 1;
ret = mininstrsize;
}
ret = op.size;
if (a->ofilter) {
@ -1238,7 +1244,7 @@ R_API int r_asm_syntax_from_string(const char *name) {
}
R_API char *r_asm_mnemonics(RAsm *a, int id, bool json) {
r_return_val_if_fail (a && a->cur, NULL);
r_return_val_if_fail (a, NULL);
// should use rarch instead!.. but for now ranal.mnemonics is calling arch.mnemonics..
if (a->analb.anal && a->analb.mnemonics) {
return a->analb.mnemonics (a->analb.anal, id, json);

View File

@ -119,7 +119,7 @@ RAsmPlugin r_asm_plugin_arm = {
.arch = "arm",
.bits = 16 | 32 | 64,
.endian = R_SYS_ENDIAN_LITTLE | R_SYS_ENDIAN_BIG,
.assemble = &assemble,
.assemble = &assemble, // DEPRECATE
.encode = &encode,
};

View File

@ -3895,8 +3895,14 @@ R_API int r_core_anal_search(RCore *core, ut64 from, ut64 to, ut64 ref, int mode
int arch = -1;
if (core->rasm->config->bits == 64) {
// speedup search
if (!strncmp (core->rasm->cur->name, "arm", 3)) {
arch = R2_ARCH_ARM64;
if (core->rasm->cur) {
if (r_str_startswith (core->rasm->cur->name, "arm")) {
arch = R2_ARCH_ARM64;
}
} else if (core->rasm->config) {
if (r_str_startswith (core->rasm->config->arch, "arm")) {
arch = R2_ARCH_ARM64;
}
}
}
// TODO: get current section range here or gtfo
@ -5011,7 +5017,11 @@ static bool esilbreak_reg_write(REsil *esil, const char *name, ut64 *val) {
}
}
}
if (core->rasm->config->bits == 32 && strstr (core->rasm->cur->name, "arm")) {
if (core->rasm && core->rasm->cur && core->rasm->config && core->rasm->config->bits == 32 && strstr (core->rasm->cur->name, "arm")) {
if ((!(at & 1)) && r_io_is_valid_offset (anal->iob.io, at, 0)) { // !core->anal->opt.noncode)) {
add_string_ref (anal->coreb.core, esil->address, at);
}
} else if (core->anal && core->anal->config && core->anal->config->bits == 32 && strstr (core->anal->cur->name, "arm")) {
if ((!(at & 1)) && r_io_is_valid_offset (anal->iob.io, at, 0)) { // !core->anal->opt.noncode)) {
add_string_ref (anal->coreb.core, esil->address, at);
}

View File

@ -1000,7 +1000,8 @@ static bool cb_asmbits(void *user, void *data) {
if (!ret) {
RAsmPlugin *h = core->rasm->cur;
if (!h) {
R_LOG_ERROR ("e asm.bits: Cannot set value, no plugins defined yet");
// r_asm_use (core->rasm, R_SYS_ARCH);
// R_LOG_ERROR ("e asm.bits: Cannot set value, no plugins defined yet");
ret = true;
}
// else { R_LOG_ERROR ("Cannot set bits %d to '%s'", bits, h->name); }

View File

@ -172,6 +172,7 @@ R_API bool r_arch_add(RArch *arch, RArchPlugin *ap);
R_API bool r_arch_del(RArch *arch, const char *name);
R_API void r_arch_free(RArch *arch);
// deprecate
R_API bool r_arch_set_bits(RArch *arch, ut32 bits);
R_API bool r_arch_set_endian(RArch *arch, ut32 endian);
R_API bool r_arch_set_arch(RArch *arch, char *archname);

View File

@ -107,7 +107,6 @@ R_API char *r_asm_mnemonics(RAsm *a, int id, bool json);
R_API int r_asm_mnemonics_byname(RAsm *a, const char *name);
R_API void r_asm_set_user_ptr(RAsm *a, void *user);
R_API bool r_asm_add(RAsm *a, RAsmPlugin *foo);
R_API bool r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian);
R_API bool r_asm_is_valid(RAsm *a, const char *name);
R_API bool r_asm_use(RAsm *a, const char *name);

View File

@ -8,8 +8,10 @@ EXPECT=<<EOF
EOF
RUN
# XXX this test is broken because asm is no longer enforcing bits configuration
NAME=8051: checking bitness writing (asm.bits)
FILE=malloc://32
BROKEN=1
CMDS=<<EOF
e asm.arch=8051
e asm.bits

View File

@ -1,3 +1,3 @@
ad "nop" 13000000
ad "lui sp, 0x8" 37810000
ad "addi s0, sp, 16" 13040101
# ad "lui sp, 0x8" 37810000
# ad "addi s0, sp, 16" 13040101