Skip to content

Commit

Permalink
Prepare core_esil trap revert feature
Browse files Browse the repository at this point in the history
  • Loading branch information
condret committed Nov 30, 2024
1 parent 03287a6 commit e58ada5
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
42 changes: 42 additions & 0 deletions libr/core/core_esil.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ static REsilMemInterface core_esil_mem_if = {
.mem_write = core_esil_mem_write
};

static void core_esil_voyeur_trap_revert_reg_write (void *user, const char *name,
ut64 old, ut64 val) {
RCoreEsil *cesil = user;
if (!(cesil->cfg & R_CORE_ESIL_REVERT)) {
return;
}
if (R_UNLIKELY (!r_strbuf_length (&cesil->trap_revert))) {
r_strbuf_setf (&cesil->trap_revert, "0x%"PFMT64x",%s,:=", old, name);
return;
}
r_strbuf_appendf (&cesil->trap_revert, ",0x%"PFMT64x",%s,:=", old, name);
}

static void core_esil_voyeur_trap_revert_mem_write (void *user, ut64 addr,
const ut8 *old, const ut8 *buf, int len) {
RCoreEsil *cesil = user;
if (!(cesil->cfg & R_CORE_ESIL_REVERT)) {
return;
}
int i;
if (R_UNLIKELY (!r_strbuf_length (&cesil->trap_revert))) {
r_strbuf_setf (&cesil->trap_revert, "0x%02x,0x%"PFMT64x",=[1]",
*old, addr);
i = 1;
} else {
i = 0;
}
for (;i < len; i++) {
//TODO: optimize this after breaking
r_strbuf_appendf (&cesil->trap_revert, ",0x%02x,0x%"PFMT64x",=[1]",
old[i], addr + i);
}
}

R_API bool r_core_esil_init(RCore *core) {
R_RETURN_VAL_IF_FAIL (core && core->io, false);
core->esil.reg = r_reg_new ();
Expand All @@ -124,7 +158,12 @@ R_API bool r_core_esil_init(RCore *core) {
0, 0, R_ESIL_OP_TYPE_UNKNOWN)) {
goto op_fail;
}
r_strbuf_init (&core->esil.trap_revert);
core->esil.esil.user = core;
core->esil.tr_reg = r_esil_add_voyeur (&core->esil.esil, &core->esil,
core_esil_voyeur_trap_revert_reg_write, R_ESIL_VOYEUR_REG_WRITE);
core->esil.tr_mem = r_esil_add_voyeur (&core->esil.esil, &core->esil,
core_esil_voyeur_trap_revert_mem_write, R_ESIL_VOYEUR_MEM_WRITE);
return true;
op_fail:
r_esil_fini (&core->esil.esil);
Expand All @@ -135,7 +174,10 @@ R_API bool r_core_esil_init(RCore *core) {

R_API void r_core_esil_fini(RCoreEsil *cesil) {
R_RETURN_IF_FAIL (cesil);
r_esil_del_voyeur (&cesil->esil, cesil->tr_reg);
r_esil_del_voyeur (&cesil->esil, cesil->tr_mem);
r_esil_fini (&cesil->esil);
r_strbuf_fini (&cesil->trap_revert);
if (cesil->reg) {
r_reg_free (cesil->reg);
cesil->reg = NULL;
Expand Down
7 changes: 7 additions & 0 deletions libr/include/r_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ typedef struct {

typedef struct r_core_esil_t {
REsil esil;
union {
RStrBuf trap_revert;
ut64 old_pc;
};
ut32 tr_reg;
ut32 tr_mem;
RReg *reg;
char *cmd_step; // command to run before a step is performed
char *cmd_step_out; // command to run after a step is performed
Expand All @@ -318,6 +324,7 @@ typedef struct r_core_esil_t {

#define R_CORE_ESIL_RO 0x1
#define R_CORE_ESIL_NONULL 0x2
#define R_CORE_ESIL_REVERT 0x4

typedef struct RCorePriv RCorePriv;

Expand Down
2 changes: 1 addition & 1 deletion libr/include/r_esil.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ typedef struct r_esil_register_interface_t {
typedef void (*REsilVoyeurRegRead)(void *user, const char *name, ut64 val);
typedef void (*REsilVoyeurRegWrite)(void *user, const char *name, ut64 old, ut64 val);
typedef void (*REsilVoyeurMemRead)(void *user, ut64 addr, const ut8 *buf, int len);
typedef void (*REsilVoyeurMemWrite)(void *user, ut64 addr, const ut8 *buf, const ut8 *old, int len);
typedef void (*REsilVoyeurMemWrite)(void *user, ut64 addr, const ut8 *old, const ut8 *buf, int len);

typedef struct r_esil_voyeur_t {
void *user;
Expand Down

0 comments on commit e58ada5

Please sign in to comment.