Use r_return in RAsm APIs and fix some regressions ##asm

This commit is contained in:
pancake 2019-02-01 23:47:13 -06:00
parent 0daae3d9af
commit f33ebcbd89
9 changed files with 138 additions and 129 deletions

View File

@ -63,17 +63,6 @@ static inline int r_asm_pseudo_org(RAsm *a, char *input) {
return 0;
}
// wtf isnt this the same as r_asm_op_set_hex() ??
static inline int r_asm_pseudo_hex(RAsmOp *op, char *input) {
int len = r_hex_str2bin (input, (ut8*)r_strbuf_get (&op->buf));
if (len < 0) {
eprintf ("Invalid input .hex buffer (odd hexpair string).\n");
return 0;
}
r_asm_op_set_hex (op, r_str_trim_head_tail (input));
return len;
}
static inline int r_asm_pseudo_intN(RAsm *a, RAsmOp *op, char *input, int n) {
short s;
int i;
@ -204,15 +193,15 @@ R_API RAsm *r_asm_new() {
return a;
}
R_API int r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian) {
int ret = 0;
ret |= !r_asm_use (a, arch);
ret |= !r_asm_set_bits (a, bits);
return ret;
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: spagueti
R_API int r_asm_filter_input(RAsm *a, const char *f) {
r_return_val_if_fail (a && f, false);
if (!a->ifilter) {
a->ifilter = r_parse_new ();
}
@ -236,8 +225,10 @@ R_API int r_asm_filter_output(RAsm *a, const char *f) {
return true;
}
R_API RAsm *r_asm_free(RAsm *a) {
if (a) {
R_API void r_asm_free(RAsm *a) {
if (!a) {
return;
}
if (a->cur && a->cur->fini) {
a->cur->fini (a->cur->user);
}
@ -252,8 +243,6 @@ R_API RAsm *r_asm_free(RAsm *a) {
a->pair = NULL;
free (a);
}
return NULL;
}
R_API void r_asm_set_user_ptr(RAsm *a, void *user) {
a->user = user;
@ -315,7 +304,7 @@ R_API bool r_asm_use_assembler(RAsm *a, const char *name) {
}
// TODO: this can be optimized using r_str_hash()
R_API int r_asm_use(RAsm *a, const char *name) {
R_API bool r_asm_use(RAsm *a, const char *name) {
RAsmPlugin *h;
RListIter *iter;
if (!a || !name) {
@ -326,12 +315,14 @@ R_API int r_asm_use(RAsm *a, const char *name) {
if (!a->cur || (a->cur && strcmp (a->cur->arch, h->arch))) {
char *r2prefix = r_str_r2_prefix (R2_SDB_OPCODES);
char *file = r_str_newf ("%s/%s.sdb", r_str_get (r2prefix), h->arch);
if (file) {
r_asm_set_cpu (a, NULL);
sdb_free (a->pair);
a->pair = sdb_new (NULL, file, 0);
free (r2prefix);
free (file);
}
}
a->cur = h;
return true;
}
@ -361,9 +352,7 @@ R_API int r_asm_set_bits(RAsm *a, int bits) {
}
R_API bool r_asm_set_big_endian(RAsm *a, bool b) {
if (!a || !a->cur) {
return false;
}
r_return_val_if_fail (a && a->cur, false);
a->big_endian = false; //little endian by default
switch (a->cur->endian) {
case R_SYS_ENDIAN_NONE:
@ -409,14 +398,11 @@ static bool isInvalid (RAsmOp *op) {
}
R_API int r_asm_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) {
int ret;
if (!a || !buf || !op) {
return -1;
}
r_return_val_if_fail (a && buf && op, -1);
if (len < 1) {
return 0;
}
ret = op->payload = 0;
int ret = op->payload = 0;
op->size = 4;
op->bitsize = 0;
r_asm_op_set_asm (op, "");
@ -567,6 +553,7 @@ R_API void r_asm_list_directives() {
}
R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf) {
r_return_val_if_fail (a && op && buf, 0);
int ret = 0;
char *b = strdup (buf);
if (!b) {
@ -611,6 +598,8 @@ R_API int r_asm_assemble(RAsm *a, RAsmOp *op, const char *buf) {
// TODO: Use RStrBuf api here pls
R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
r_return_val_if_fail (a && buf && len >= 0, NULL);
RStrBuf *buf_asm;
RAsmCode *acode;
ut64 pc = a->pc;
@ -652,18 +641,16 @@ R_API RAsmCode* r_asm_mdisassemble(RAsm *a, const ut8 *buf, int len) {
}
R_API RAsmCode* r_asm_mdisassemble_hexstr(RAsm *a, const char *hexstr) {
RAsmCode *ret;
ut8 *buf;
int len;
if (!(buf = malloc (strlen (hexstr) + 1))) {
ut8 *buf = malloc (strlen (hexstr) + 1);
if (!buf) {
return NULL;
}
len = r_hex_str2bin (hexstr, buf);
int len = r_hex_str2bin (hexstr, buf);
if (len < 1) {
free (buf);
return NULL;
}
ret = r_asm_mdisassemble (a, buf, (ut64)len);
RAsmCode *ret = r_asm_mdisassemble (a, buf, (ut64)len);
if (ret && a->ofilter) {
r_parse_parse (a->ofilter, ret->buf_asm, ret->buf_asm);
}
@ -691,7 +678,7 @@ static void *dup_val(const void *v) {
}
R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
int labels = 0, num, stage, ret, idx, ctr, i, j, linenum = 0;
int num, stage, ret, idx, ctr, i, j, linenum = 0;
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL;
const char *asmcpu = NULL;
RAsmCode *acode = NULL;
@ -761,9 +748,8 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
p = strstr (p + 5, "$sys.");
}
}
if (strchr (lbuf, ':')) {
labels = 1;
}
bool labels = !!strchr (lbuf, ':');
/* Tokenize */
for (tokens[0] = lbuf, ctr = 0;
((ptr = strchr (tokens[ctr], ';')) ||
@ -798,9 +784,11 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
}
inComment = false;
r_asm_set_pc (a, pc);
for (idx = ret = i = j = 0, off = a->pc, acode->buf_hex[0] = '\0';
i <= ctr; i++, idx += ret) {
for (idx = ret = i = j = 0, off = a->pc, acode->buf_hex[0] = '\0'; i <= ctr; i++, idx += ret) {
buf_token = tokens[i];
if (!buf_token) {
continue;
}
if (inComment) {
if (!strncmp (ptr_start, "*/", 2)) {
inComment = false;
@ -809,15 +797,11 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
}
// XXX TODO remove arch-specific hacks
if (!strncmp (a->cur->arch, "avr", 3)) {
for (ptr_start = buf_token; *ptr_start &&
isavrseparator (*ptr_start);
ptr_start++) {
for (ptr_start = buf_token; *ptr_start && isavrseparator (*ptr_start); ptr_start++) {
;
}
} else {
for (ptr_start = buf_token; *ptr_start &&
IS_SEPARATOR (*ptr_start);
ptr_start++) {
for (ptr_start = buf_token; *ptr_start && IS_SEPARATOR (*ptr_start); ptr_start++) {
;
}
}
@ -851,23 +835,25 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
}
if (is_a_label) {
//if (stage != 2) {
if (ptr_start[1] != 0 && ptr_start[1] != ' ') {
if (ptr_start[1] && ptr_start[1] != ' ') {
*ptr = 0;
char *p = strdup (ptr_start);
*ptr = ':';
if (acode->code_align) {
off += (acode->code_align - (off % acode->code_align));
}
char food[64];
snprintf (food, sizeof (food), "0x%"PFMT64x, off);
char *food = r_str_newf ("0x%"PFMT64x, off);
ht_pp_insert (a->flags, ptr_start, food);
// TODO: warning when redefined
r_asm_code_set_equ (acode, ptr_start, food);
r_asm_code_set_equ (acode, p, food);
free (p);
free (food);
}
//}
ptr_start = ptr + 1;
}
ptr = ptr_start;
}
if (*ptr_start == '\0') {
if (!*ptr_start) {
ret = 0;
continue;
}
@ -889,7 +875,9 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
ret = r_asm_pseudo_string (&op, ptr + 8, 1);
} else if (!strncmp (ptr, ".string ", 8)) {
r_str_trim (ptr + 8);
ret = r_asm_pseudo_string (&op, ptr + 8, 1);
char * str = strdup (ptr + 8);
ret = r_asm_pseudo_string (&op, str, 1);
free (str);
} else if (!strncmp (ptr, ".ascii", 6)) {
ret = r_asm_pseudo_string (&op, ptr + 7, 0);
} else if (!strncmp (ptr, ".align", 6)) {
@ -915,7 +903,7 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
} else if (!strncmp (ptr, ".os ", 4)) {
r_syscall_setup (a->syscall, a->cur->arch, a->bits, asmcpu, ptr + 4);
} else if (!strncmp (ptr, ".hex ", 5)) {
ret = r_asm_pseudo_hex (&op, ptr + 5);
ret = r_asm_op_set_hex (&op, ptr + 5);
} else if ((!strncmp (ptr, ".int16 ", 7)) || !strncmp (ptr, ".short ", 7)) {
ret = r_asm_pseudo_int16 (a, &op, ptr + 7);
} else if (!strncmp (ptr, ".int32 ", 7)) {
@ -1016,7 +1004,9 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
}
acode->buf_hex = newbuf;
acode->buf = malloc (4095);
if (acode->buf) {
memcpy (acode->buf + idx, r_strbuf_get (&op.buf), r_strbuf_length (&op.buf)); // ret);
}
// XXX slow. use strbuf pls
strcat (acode->buf_hex, r_strbuf_get (&op.buf_hex));
if (op.buf_inc && r_buf_size (op.buf_inc) > 1) {
@ -1029,7 +1019,6 @@ R_API RAsmCode *r_asm_massemble(RAsm *a, const char *buf) {
free (s);
}
}
}
}
}
@ -1064,6 +1053,7 @@ R_API bool r_asm_set_arch(RAsm *a, const char *name, int bits) {
/* to ease the use of the native bindings (not used in r2) */
R_API char *r_asm_to_string(RAsm *a, ut64 addr, const ut8 *b, int l) {
r_return_val_if_fail (a && b && l >= 0, NULL);
r_asm_set_pc (a, addr);
RAsmCode *code = r_asm_mdisassemble (a, b, l);
if (code) {
@ -1090,6 +1080,7 @@ R_API ut8 *r_asm_from_string(RAsm *a, ut64 addr, const char *b, int *l) {
}
R_API int r_asm_syntax_from_string(const char *name) {
r_return_val_if_fail (name, -1);
if (!strcmp (name, "regnum")) {
return R_ASM_SYNTAX_REGNUM;
}
@ -1109,14 +1100,16 @@ R_API int r_asm_syntax_from_string(const char *name) {
}
R_API char *r_asm_mnemonics(RAsm *a, int id, bool json) {
if (a && a->cur && a->cur->mnemonics) {
r_return_val_if_fail (a && a->cur, NULL);
if (a->cur->mnemonics) {
return a->cur->mnemonics (a, id, json);
}
return NULL;
}
R_API int r_asm_mnemonics_byname(RAsm *a, const char *name) {
if (a && a->cur && a->cur->mnemonics) {
r_return_val_if_fail (a && a->cur, 0);
if (a->cur->mnemonics) {
int i;
for (i = 0; i < 1024; i++) {
char *n = a->cur->mnemonics (a, i, false);
@ -1130,6 +1123,7 @@ R_API int r_asm_mnemonics_byname(RAsm *a, const char *name) {
}
R_API RAsmCode* r_asm_rasm_assemble(RAsm *a, const char *buf, bool use_spp) {
r_return_val_if_fail (a && buf, NULL);
char *lbuf = strdup (buf);
if (!lbuf) {
return NULL;

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2018 - pancake */
/* radare - LGPL - Copyright 2009-2019 - pancake */
#include <stdio.h>
#include <r_asm.h>
@ -19,21 +19,28 @@ R_API void* r_asm_code_free(RAsmCode *acode) {
}
R_API void r_asm_equ_item_free(RAsmEqu *equ) {
if (equ) {
free (equ->key);
free (equ->value);
free (equ);
}
}
static RAsmEqu *__asm_equ_new(const char *key, const char *value) {
RAsmEqu *equ = R_NEW0 (RAsmEqu);
if (equ) {
equ->key = strdup (key);
equ->value = strdup (value);
}
return equ;
}
R_API bool r_asm_code_set_equ (RAsmCode *code, const char *key, const char *value) {
r_return_val_if_fail (code && key && value, false);
if (code->equs) {
RAsmEqu *equ;
RListIter *iter;
if (!code || !key || !value) {
eprintf ("Oops, no key or value defined in r_asm_code_set_equ ()\n");
return false;
}
if (!code->equs) {
code->equs = r_list_newf ((RListFree)r_asm_equ_item_free);
} else {
r_list_foreach (code->equs, iter, equ) {
if (!strcmp (equ->key, key)) {
free (equ->value);
@ -41,15 +48,15 @@ R_API bool r_asm_code_set_equ (RAsmCode *code, const char *key, const char *valu
return true;
}
}
} else {
code->equs = r_list_newf ((RListFree)r_asm_equ_item_free);
}
equ = R_NEW0 (RAsmEqu);
equ->key = strdup (key);
equ->value = strdup (value);
r_list_append (code->equs, equ);
r_list_append (code->equs, __asm_equ_new (key, value));
return true;
}
R_API char *r_asm_code_equ_replace (RAsmCode *code, char *str) {
r_return_val_if_fail (code && str, NULL);
RAsmEqu *equ;
RListIter *iter;
r_list_foreach (code->equs, iter, equ) {

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2018 - pancake */
/* radare - LGPL - Copyright 2018-2019 - pancake */
#include <r_asm.h>
@ -32,22 +32,22 @@ R_API char *r_asm_op_get_asm(RAsmOp *op) {
}
R_API ut8 *r_asm_op_get_buf(RAsmOp *op) {
r_return_val_if_fail (op, NULL);
return (ut8*)r_strbuf_get (&op->buf);
}
R_API int r_asm_op_get_size(RAsmOp *op) {
if (!op) {
return 1;
}
r_return_val_if_fail (op, 1);
const int len = op->size - op->payload;
return R_MAX (1, len);
}
R_API void r_asm_op_set_asm(RAsmOp *op, const char *str) {
r_return_if_fail (op && str);
r_strbuf_set (&op->buf_asm, str);
}
R_API void r_asm_op_set_hex(RAsmOp *op, const char *str) {
R_API int r_asm_op_set_hex(RAsmOp *op, const char *str) {
r_strbuf_set (&op->buf_hex, str);
ut8 *bin = (ut8*)strdup (str);
if (bin) {
@ -56,22 +56,26 @@ R_API void r_asm_op_set_hex(RAsmOp *op, const char *str) {
r_strbuf_setbin (&op->buf, bin, len);
}
free (bin);
return len;
}
return 0;
}
R_API void r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len) {
R_API int r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len) {
r_return_val_if_fail (op && buf && len >= 0, 0);
char *hex = malloc (len * 4 + 1);
if (hex) {
r_hex_bin2str (buf, len, hex);
r_asm_op_set_hex (op, hex);
(void)r_hex_bin2str (buf, len, hex);
int olen = r_asm_op_set_hex (op, hex);
free (hex);
return olen;
}
return 0;
// TODO: update the op->buf too?
}
R_API void r_asm_op_set_buf(RAsmOp *op, const ut8 *buf, int len) {
if (op && buf && len >= 0) {
r_return_if_fail (op && buf && len >= 0);
r_strbuf_setbin (&op->buf, buf, len);
r_asm_op_set_hexbuf (op, buf, len);
}
}

View File

@ -4951,7 +4951,9 @@ static int assemble(RAsm *a, RAsmOp *ao, const char *str) {
break;
}
}
if (retval > 0) {
r_asm_op_set_buf (ao, __data, retval);
}
free (instr.mnemonic);
return retval;
}

View File

@ -1456,10 +1456,9 @@ static int cmd_write(void *data, const char *input) {
}
case 'f': // "waf"
if ((input[2] == ' ' || input[2] == '*')) {
const char *file = input[2]=='*'? input+4: input+3;
RAsmCode *acode;
const char *file = input + ((input[2] == '*')? 4: 3);
r_asm_set_pc (core->assembler, core->offset);
acode = r_asm_assemble_file (core->assembler, file);
RAsmCode *acode = r_asm_assemble_file (core->assembler, file);
if (acode) {
if (input[2] == '*') {
cmd_write_hexpair (core, acode->buf_hex);
@ -1475,8 +1474,12 @@ static int cmd_write(void *data, const char *input) {
r_core_block_read (core);
}
r_asm_code_free (acode);
} else eprintf ("Cannot assemble file\n");
} else eprintf ("Wrong argument\n");
} else {
eprintf ("Cannot assemble file\n");
}
} else {
eprintf ("Wrong argument\n");
}
break;
default:
r_core_cmd_help (core, help_msg_wa);

View File

@ -2549,7 +2549,8 @@ R_API RCore *r_core_fini(RCore *c) {
c->rcmd = r_cmd_free (c->rcmd);
r_list_free (c->cmd_descriptors);
c->anal = r_anal_free (c->anal);
c->assembler = r_asm_free (c->assembler);
r_asm_free (c->assembler);
c->assembler = NULL;
c->print = r_print_free (c->print);
c->bin = r_bin_free (c->bin); // XXX segfaults rabin2 -c
c->lang = r_lang_free (c->lang); // XXX segfaults

View File

@ -217,9 +217,7 @@ R_API int r_egg_raw(REgg *egg, const ut8 *b, int len) {
if (!out) {
return false;
}
int olen = r_hex_bin2str (b, len, out);
eprintf ("OUTLEN (%d) = %d\n", len, olen);
eprintf ("STROUT (%d)\n", strlen (out));
(void)r_hex_bin2str (b, len, out);
r_buf_append_bytes (egg->buf, (const ut8*)".hex ", 5);
r_buf_append_bytes (egg->buf, (const ut8*)out, outlen);
r_buf_append_bytes (egg->buf, (const ut8*)"\n", 1);

View File

@ -145,15 +145,15 @@ typedef struct r_asm_plugin_t {
#ifdef R_API
/* asm.c */
R_API RAsm *r_asm_new(void);
R_API RAsm *r_asm_free(RAsm *a);
R_API void r_asm_free(RAsm *a);
R_API bool r_asm_modify(RAsm *a, ut8 *buf, int field, ut64 val);
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 int r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian);
R_API bool r_asm_setup(RAsm *a, const char *arch, int bits, int big_endian);
R_API int r_asm_is_valid(RAsm *a, const char *name);
R_API int r_asm_use(RAsm *a, const char *name);
R_API bool r_asm_use(RAsm *a, const char *name);
R_API bool r_asm_use_assembler(RAsm *a, const char *name);
R_API bool r_asm_set_arch(RAsm *a, const char *name, int bits);
R_API int r_asm_set_bits(RAsm *a, int bits);
@ -194,8 +194,8 @@ R_API char *r_asm_op_get_hex(RAsmOp *op);
R_API char *r_asm_op_get_asm(RAsmOp *op);
R_API int r_asm_op_get_size(RAsmOp *op);
R_API void r_asm_op_set_asm(RAsmOp *op, const char *str);
R_API void r_asm_op_set_hex(RAsmOp *op, const char *str);
R_API void r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len);
R_API int r_asm_op_set_hex(RAsmOp *op, const char *str);
R_API int r_asm_op_set_hexbuf(RAsmOp *op, const ut8 *buf, int len);
R_API void r_asm_op_set_buf(RAsmOp *op, const ut8 *str, int len);
R_API ut8 *r_asm_op_get_buf(RAsmOp *op);