Skip to content

Commit

Permalink
Remove globals in RzAsm plugins (#4114)
Browse files Browse the repository at this point in the history
* Remove globals in RzAsm plugins

* Fix breakage due small mistakes
  • Loading branch information
wargio authored Jan 17, 2024
1 parent 5b36c7c commit 0b54b57
Show file tree
Hide file tree
Showing 25 changed files with 589 additions and 562 deletions.
11 changes: 7 additions & 4 deletions librz/asm/p/asm_arm_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,17 @@ static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
rz_str_cpy(insn->mnemonic, tmpstr);
free(tmpstr);
}
char *buf_asm = sdb_fmt("%s%s%s",
char *buf_asm = rz_str_newf("%s%s%s",
insn->mnemonic,
insn->op_str[0] ? " " : "",
insn->op_str);
if (!disp_hash) {
rz_str_replace_char(buf_asm, '#', 0);
if (buf_asm) {
if (!disp_hash) {
rz_str_replace_char(buf_asm, '#', 0);
}
rz_strbuf_set(&op->buf_asm, buf_asm);
free(buf_asm);
}
rz_strbuf_set(&op->buf_asm, buf_asm);
}
cs_free(insn, n);
beach:
Expand Down
99 changes: 52 additions & 47 deletions librz/asm/p/asm_chip8.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,84 +4,89 @@
#include <rz_asm.h>
#include <rz_lib.h>

static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *b, int l) {
static int chip8_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *b, int l) {
ut16 opcode = rz_read_be16(b);
uint8_t x = (opcode >> 8) & 0x0F;
uint8_t y = (opcode >> 4) & 0x0F;
uint8_t nibble = opcode & 0x0F;
uint16_t nnn = opcode & 0x0FFF;
uint8_t kk = opcode & 0xFF;
const char *buf_asm = "invalid";
char *buf_asm = NULL;
switch (opcode & 0xF000) {
case 0x0000:
if (opcode == 0x00E0) {
buf_asm = "cls";
buf_asm = strdup("cls");
} else if (opcode == 0x00EE) {
buf_asm = "ret";
buf_asm = strdup("ret");
} else if ((opcode & 0xFFF0) == 0x00C0) {
buf_asm = sdb_fmt("scd 0x%01x", nibble);
buf_asm = rz_str_newf("scd 0x%01x", nibble);
} else if (opcode == 0x00FB) {
buf_asm = "scr";
buf_asm = strdup("scr");
} else if (opcode == 0x00FC) {
buf_asm = "scl";
buf_asm = strdup("scl");
} else if (opcode == 0x00FD) {
buf_asm = "exit";
buf_asm = strdup("exit");
} else if (opcode == 0x00FE) {
buf_asm = "low";
buf_asm = strdup("low");
} else if (opcode == 0x00FF) {
buf_asm = "high";
buf_asm = strdup("high");
}
break;
case 0x1000: buf_asm = sdb_fmt("jp 0x%03x", nnn); break;
case 0x2000: buf_asm = sdb_fmt("call 0x%03x", nnn); break;
case 0x3000: buf_asm = sdb_fmt("se v%1x, 0x%02x", x, kk); break;
case 0x4000: buf_asm = sdb_fmt("sne v%1x, 0x%02x", x, kk); break;
case 0x5000: buf_asm = sdb_fmt("se v%1x, v%1x", x, y); break;
case 0x6000: buf_asm = sdb_fmt("ld v%1x, 0x%02x", x, kk); break;
case 0x7000: buf_asm = sdb_fmt("add v%1x, 0x%02x", x, kk); break;
case 0x1000: buf_asm = rz_str_newf("jp 0x%03x", nnn); break;
case 0x2000: buf_asm = rz_str_newf("call 0x%03x", nnn); break;
case 0x3000: buf_asm = rz_str_newf("se v%1x, 0x%02x", x, kk); break;
case 0x4000: buf_asm = rz_str_newf("sne v%1x, 0x%02x", x, kk); break;
case 0x5000: buf_asm = rz_str_newf("se v%1x, v%1x", x, y); break;
case 0x6000: buf_asm = rz_str_newf("ld v%1x, 0x%02x", x, kk); break;
case 0x7000: buf_asm = rz_str_newf("add v%1x, 0x%02x", x, kk); break;
case 0x8000: {
switch (nibble) {
case 0x0: buf_asm = sdb_fmt("ld v%1x, v%1x", x, y); break;
case 0x1: buf_asm = sdb_fmt("or v%1x, v%1x", x, y); break;
case 0x2: buf_asm = sdb_fmt("and v%1x, v%1x", x, y); break;
case 0x3: buf_asm = sdb_fmt("xor v%1x, v%1x", x, y); break;
case 0x4: buf_asm = sdb_fmt("add v%1x, v%1x", x, y); break;
case 0x5: buf_asm = sdb_fmt("sub v%1x, v%1x", x, y); break;
case 0x6: buf_asm = sdb_fmt("shr v%1x, v%1x", x, y); break;
case 0x7: buf_asm = sdb_fmt("subn v%1x, v%1x", x, y); break;
case 0xE: buf_asm = sdb_fmt("shl v%1x, v%1x", x, y); break;
case 0x0: buf_asm = rz_str_newf("ld v%1x, v%1x", x, y); break;
case 0x1: buf_asm = rz_str_newf("or v%1x, v%1x", x, y); break;
case 0x2: buf_asm = rz_str_newf("and v%1x, v%1x", x, y); break;
case 0x3: buf_asm = rz_str_newf("xor v%1x, v%1x", x, y); break;
case 0x4: buf_asm = rz_str_newf("add v%1x, v%1x", x, y); break;
case 0x5: buf_asm = rz_str_newf("sub v%1x, v%1x", x, y); break;
case 0x6: buf_asm = rz_str_newf("shr v%1x, v%1x", x, y); break;
case 0x7: buf_asm = rz_str_newf("subn v%1x, v%1x", x, y); break;
case 0xE: buf_asm = rz_str_newf("shl v%1x, v%1x", x, y); break;
}
} break;
case 0x9000: buf_asm = sdb_fmt("sne v%1x, v%1x", x, y); break;
case 0xA000: buf_asm = sdb_fmt("ld i, 0x%03x", nnn); break;
case 0xB000: buf_asm = sdb_fmt("jp v0, 0x%03x", nnn); break;
case 0xC000: buf_asm = sdb_fmt("rnd v%1x, 0x%02x", x, kk); break;
case 0xD000: buf_asm = sdb_fmt("drw v%1x, v%1x, 0x%01x", x, y, nibble); break;
case 0x9000: buf_asm = rz_str_newf("sne v%1x, v%1x", x, y); break;
case 0xA000: buf_asm = rz_str_newf("ld i, 0x%03x", nnn); break;
case 0xB000: buf_asm = rz_str_newf("jp v0, 0x%03x", nnn); break;
case 0xC000: buf_asm = rz_str_newf("rnd v%1x, 0x%02x", x, kk); break;
case 0xD000: buf_asm = rz_str_newf("drw v%1x, v%1x, 0x%01x", x, y, nibble); break;
case 0xE000: {
if (kk == 0x9E) {
buf_asm = sdb_fmt("skp v%1x", x);
buf_asm = rz_str_newf("skp v%1x", x);
} else if (kk == 0xA1) {
buf_asm = sdb_fmt("sknp v%1x", x);
buf_asm = rz_str_newf("sknp v%1x", x);
}
} break;
case 0xF000: {
switch (kk) {
case 0x07: buf_asm = sdb_fmt("ld v%1x, dt", x); break;
case 0x0A: buf_asm = sdb_fmt("ld v%1x, k", x); break;
case 0x15: buf_asm = sdb_fmt("ld dt, v%1x", x); break;
case 0x18: buf_asm = sdb_fmt("ld st, v%1x", x); break;
case 0x1E: buf_asm = sdb_fmt("add i, v%1x", x); break;
case 0x29: buf_asm = sdb_fmt("ld f, v%1x", x); break;
case 0x33: buf_asm = sdb_fmt("ld b, v%1x", x); break;
case 0x55: buf_asm = sdb_fmt("ld [i], v%1x", x); break;
case 0x65: buf_asm = sdb_fmt("ld v%1x, [i]", x); break;
case 0x30: buf_asm = sdb_fmt("ld hf, v%1x", x); break;
case 0x75: buf_asm = sdb_fmt("ld r, v%1x", x); break;
case 0x85: buf_asm = sdb_fmt("ld v%1x, r", x); break;
case 0x07: buf_asm = rz_str_newf("ld v%1x, dt", x); break;
case 0x0A: buf_asm = rz_str_newf("ld v%1x, k", x); break;
case 0x15: buf_asm = rz_str_newf("ld dt, v%1x", x); break;
case 0x18: buf_asm = rz_str_newf("ld st, v%1x", x); break;
case 0x1E: buf_asm = rz_str_newf("add i, v%1x", x); break;
case 0x29: buf_asm = rz_str_newf("ld f, v%1x", x); break;
case 0x33: buf_asm = rz_str_newf("ld b, v%1x", x); break;
case 0x55: buf_asm = rz_str_newf("ld [i], v%1x", x); break;
case 0x65: buf_asm = rz_str_newf("ld v%1x, [i]", x); break;
case 0x30: buf_asm = rz_str_newf("ld hf, v%1x", x); break;
case 0x75: buf_asm = rz_str_newf("ld r, v%1x", x); break;
case 0x85: buf_asm = rz_str_newf("ld v%1x, r", x); break;
}
} break;
}
rz_strbuf_set(&op->buf_asm, buf_asm);
if (!buf_asm) {
rz_strbuf_set(&op->buf_asm, "invalid");
} else {
rz_strbuf_set(&op->buf_asm, buf_asm);
free(buf_asm);
}
op->size = 2;
return op->size;
}
Expand All @@ -92,7 +97,7 @@ RzAsmPlugin rz_asm_plugin_chip8 = {
.license = "LGPL3",
.bits = 32,
.desc = "Chip8 disassembler",
.disassemble = &disassemble,
.disassemble = &chip8_disassemble,
};

#ifndef RZ_PLUGIN_INCORE
Expand Down
12 changes: 8 additions & 4 deletions librz/asm/p/asm_cr16.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@
#include <rz_asm.h>
#include <cr16_disas.h>

static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
struct cr16_cmd cmd;
static int cr16_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
struct cr16_cmd cmd = { 0 };
int ret = cr16_decode_command(buf, &cmd, len);
rz_strbuf_set(&op->buf_asm, sdb_fmt("%s %s", cmd.instr, cmd.operands));
if (ret > -1) {
rz_strbuf_initf(&op->buf_asm, "%s %s", cmd.instr, cmd.operands);
} else {
rz_strbuf_set(&op->buf_asm, "invalid");
}
return op->size = ret;
}

Expand All @@ -22,7 +26,7 @@ RzAsmPlugin rz_asm_plugin_cr16 = {
.arch = "cr16",
.bits = 16,
.endian = RZ_SYS_ENDIAN_LITTLE,
.disassemble = &disassemble
.disassemble = &cr16_disassemble
};

#ifndef RZ_PLUGIN_INCORE
Expand Down
9 changes: 5 additions & 4 deletions librz/asm/p/asm_dalvik.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ static int dalvik_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
char *flag_str = NULL;
a->dataalign = 2;

const char *buf_asm = NULL;
char *buf_asm = NULL;
if (buf[0] == 0x00) { /* nop */
if (len < 2) {
return -1;
Expand All @@ -35,7 +35,7 @@ static int dalvik_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
{
ut16 array_size = buf[2] | (buf[3] << 8);
int first_key = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
buf_asm = sdb_fmt("packed-switch-payload %d, %d", array_size, first_key);
buf_asm = rz_str_newf("packed-switch-payload %d, %d", array_size, first_key);
size = 8;
payload = 2 * (array_size * 2);
len = 0;
Expand All @@ -47,7 +47,7 @@ static int dalvik_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
// int[size] relative offsets
{
ut16 array_size = buf[2] | (buf[3] << 8);
buf_asm = sdb_fmt("sparse-switch-payload %d", array_size);
buf_asm = rz_str_newf("sparse-switch-payload %d", array_size);
size = 4;
payload = 2 * (array_size * 4);
len = 0;
Expand All @@ -60,7 +60,7 @@ static int dalvik_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
if (len > 7) {
ut16 elem_width = buf[2] | (buf[3] << 8);
ut32 array_size = buf[4] | (buf[5] << 8) | (buf[6] << 16) | (buf[7] << 24);
buf_asm = sdb_fmt("fill-array-data-payload %d, %d", elem_width, array_size);
buf_asm = rz_str_newf("fill-array-data-payload %d, %d", elem_width, array_size);
payload = array_size * elem_width;
}
size = 8;
Expand All @@ -73,6 +73,7 @@ static int dalvik_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
}
if (buf_asm) {
rz_strbuf_set(&op->buf_asm, buf_asm);
RZ_FREE(buf_asm);
}
strasm = NULL;
if (size <= len) {
Expand Down
13 changes: 9 additions & 4 deletions librz/asm/p/asm_ebc.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
ebc_command_t cmd = { { 0 }, { 0 } };
int ret = ebc_decode_command(buf, len, &cmd);
const char *buf_asm = (cmd.operands[0])
? sdb_fmt("%s %s", cmd.instr, cmd.operands)
: cmd.instr;
rz_asm_op_set_asm(op, buf_asm);
if (cmd.operands[0]) {
char *buf_asm = rz_str_newf("%s %s", cmd.instr, cmd.operands);
if (buf_asm) {
rz_asm_op_set_asm(op, buf_asm);
free(buf_asm);
}
} else {
rz_asm_op_set_asm(op, cmd.instr);
}
return op->size = ret;
}

Expand Down
6 changes: 5 additions & 1 deletion librz/asm/p/asm_h8300.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
struct h8300_cmd cmd;
int ret = h8300_decode_command(buf, &cmd);
rz_strbuf_set(&op->buf_asm, sdb_fmt("%s %s", cmd.instr, cmd.operands));
char *buf_asm = rz_str_newf("%s %s", cmd.instr, cmd.operands);
if (buf_asm) {
rz_strbuf_set(&op->buf_asm, buf_asm);
free(buf_asm);
}
return op->size = ret;
}

Expand Down
69 changes: 25 additions & 44 deletions librz/asm/p/asm_m680x_cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

#include <rz_asm.h>
#include <rz_lib.h>
#include <capstone/capstone.h>
#include "cs_helper.h"

static csh cd = 0;
CAPSTONE_DEFINE_PLUGIN_FUNCTIONS(m680x);

static int m680xmode(const char *str) {
static cs_mode m680x_mode(const char *str) {
if (!str) {
return CS_MODE_M680X_6800;
}
Expand Down Expand Up @@ -39,62 +39,42 @@ static int m680xmode(const char *str) {
return CS_MODE_M680X_6800;
}

typedef struct {
int omode;
} M680xContext;
static int m680x_disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
CapstoneContext *ctx = (CapstoneContext *)a->plugin_data;

static bool m680x_init(void **user) {
M680xContext *ctx = RZ_NEW0(M680xContext);
rz_return_val_if_fail(ctx, false);
ctx->omode = 0;
*user = ctx;
return true;
}

static bool the_end(void *p) {
M680xContext *ctx = (M680xContext *)p;
if (cd) {
cs_close(&cd);
cd = 0;
}
if (ctx) {
RZ_FREE(ctx);
}
return true;
}

static int disassemble(RzAsm *a, RzAsmOp *op, const ut8 *buf, int len) {
M680xContext *ctx = (M680xContext *)a->plugin_data;
int omode = ctx->omode;
int mode, n, ret;
int n, ret;
ut64 off = a->pc;
cs_mode mode;
cs_insn *insn = NULL;
mode = m680xmode(a->cpu);
if (cd && mode != omode) {
cs_close(&cd);
cd = 0;
}
mode = m680x_mode(a->cpu);
op->size = 0;
omode = mode;
if (cd == 0) {
ret = cs_open(CS_ARCH_M680X, mode, &cd);

if (ctx->omode != mode) {
cs_close(&ctx->handle);
ctx->omode = -1;
}
if (!ctx->handle) {
ret = cs_open(CS_ARCH_M680X, mode, &ctx->handle);
if (ret) {
return 0;
return -1;
}
cs_option(cd, CS_OPT_DETAIL, CS_OPT_OFF);
ctx->omode = mode;
cs_option(ctx->handle, CS_OPT_DETAIL, CS_OPT_OFF);
}
n = cs_disasm(cd, (const ut8 *)buf, len, off, 1, &insn);

n = cs_disasm(ctx->handle, (const ut8 *)buf, len, off, 1, &insn);
if (n > 0) {
if (insn->size > 0) {
op->size = insn->size;
char *buf_asm = sdb_fmt("%s%s%s",
char *buf_asm = rz_str_newf("%s%s%s",
insn->mnemonic, insn->op_str[0] ? " " : "",
insn->op_str);
char *ptrstr = strstr(buf_asm, "ptr ");
if (ptrstr) {
memmove(ptrstr, ptrstr + 4, strlen(ptrstr + 4) + 1);
}
rz_asm_op_set_asm(op, buf_asm);
free(buf_asm);
}
cs_free(insn, n);
}
Expand All @@ -110,8 +90,9 @@ RzAsmPlugin rz_asm_plugin_m680x_cs = {
.bits = 8 | 32,
.endian = RZ_SYS_ENDIAN_LITTLE,
.init = m680x_init,
.fini = the_end,
.disassemble = &disassemble,
.fini = m680x_fini,
.disassemble = &m680x_disassemble,
.mnemonics = m680x_mnemonics,
};

#ifndef RZ_PLUGIN_INCORE
Expand Down
Loading

0 comments on commit 0b54b57

Please sign in to comment.