Skip to content

Commit 80e9bff

Browse files
committed
wiring up more reg/memifs to esil
1 parent c86e803 commit 80e9bff

File tree

6 files changed

+91
-27
lines changed

6 files changed

+91
-27
lines changed

libr/anal/anal.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ static bool anal_esil_reg_read (void *user, const char *name, ut64 *val) {
117117
if (!ri) {
118118
return false;
119119
}
120-
*val = r_reg_get_value (((RAnal *)user)->reg, ri);
120+
ut64 v = r_reg_get_value (((RAnal *)user)->reg, ri);
121+
if (val) {
122+
*val = v;
123+
}
121124
r_unref (ri);
122125
return true;
123126
}

libr/core/cmd_anal.inc.c

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2452,6 +2452,49 @@ static bool mw(int *ec, ut64 addr, const ut8 *old, const ut8 *buf, int len) {
24522452
return true;
24532453
}
24542454

2455+
static bool anal_rr(void *_reg, const char *name, ut64 *val) {
2456+
RReg *reg = _reg;
2457+
RRegItem *ri = r_reg_get (reg, name, -1);
2458+
if (ri) {
2459+
#if 0
2460+
if (len) {
2461+
*len = ri->size;
2462+
}
2463+
#endif
2464+
if (val) {
2465+
*val = r_reg_get_value (reg, ri);
2466+
}
2467+
return true;
2468+
}
2469+
return false;
2470+
}
2471+
2472+
static bool anal_rw(void *_reg, const char *name, ut64 val) {
2473+
RReg *reg = _reg;
2474+
return r_reg_setv (reg, name, val);
2475+
}
2476+
2477+
static bool anal_mw(void *user, ut64 addr, const ut8 *buf, int len) {
2478+
RCore *core = user;
2479+
r_io_write_at (core->io, addr, buf, len);
2480+
return true;
2481+
}
2482+
2483+
static bool anal_mr(void *user, ut64 addr, ut8 *buf, int len) {
2484+
RCore *core = user;
2485+
(void)r_io_read_at (core->io, addr, (ut8 *)buf, len);
2486+
return true;
2487+
}
2488+
2489+
static bool anal_ir(void *_reg, const char *name) {
2490+
RReg *reg = _reg;
2491+
RRegItem *ri = r_reg_get (reg, name, -1);
2492+
if (ri) {
2493+
return true;
2494+
}
2495+
return false;
2496+
}
2497+
24552498
#if 0
24562499
static bool rw(void *null, const char *regname, ut64 old, ut64 num) {
24572500
return true;
@@ -2544,6 +2587,20 @@ static inline REsil *esil_new_setup(RCore *core) {
25442587
const char *et = r_config_get (core->config, "cmd.esil.trap");
25452588
esil->cmd_trap = R_STR_ISNOTEMPTY (et)? strdup (et): NULL;
25462589
}
2590+
esil->user = core;
2591+
// reg
2592+
esil->reg_if.reg = core->anal->reg;
2593+
esil->reg_if.reg_write = anal_rw;
2594+
esil->reg_if.reg_read = anal_rr;
2595+
esil->reg_if.is_reg = anal_ir;
2596+
// mem
2597+
esil->mem_if.user = core;
2598+
esil->mem_if.mem_read = anal_mr;
2599+
esil->mem_if.mem_write = anal_mw;
2600+
#if 0
2601+
esil->cb.hook_mem_write = anal_mw;
2602+
esil->cb.hook_mem_read = anal_mr;
2603+
#endif
25472604
// run the esilcb from arch
25482605
if (core->anal->arch) {
25492606
r_arch_esilcb (core->anal->arch, R_ARCH_ESIL_ACTION_INIT);
@@ -8046,11 +8103,9 @@ static bool mymemread(REsil *esil, ut64 addr, ut8 *buf, int len) {
80468103
return false;
80478104
}
80488105
n = R_NEW (AeaMemItem);
8049-
if (n) {
8050-
n->addr = addr;
8051-
n->size = len;
8052-
r_list_push (mymemxsr, n);
8053-
}
8106+
n->addr = addr;
8107+
n->size = len;
8108+
r_list_push (mymemxsr, n);
80548109
return true;
80558110
}
80568111

@@ -8415,6 +8470,7 @@ static void cmd_aespc(RCore *core, ut64 addr, ut64 until_addr, int ninstr) {
84158470
break;
84168471
default:
84178472
r_reg_setv (core->anal->reg, "PC", aop.addr + aop.size);
8473+
eprintf ("%p\n", esil->cb.hook_reg_write);
84188474
r_reg_setv (core->dbg->reg, "PC", aop.addr + aop.size);
84198475
const char *e = R_STRBUF_SAFEGET (&aop.esil);
84208476
if (R_STR_ISNOTEMPTY (e)) {

libr/core/core_esil.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,11 @@ R_API bool r_core_esil_single_step(RCore *core) {
434434
//this is like r_list_pop_head + r_list_push,
435435
//but without expensive calls to malloc and free
436436
RListIter *iter = core->esil.stepback.head;
437-
iter->p->n = NULL;
437+
if (iter->p) {
438+
iter->p->n = NULL;
439+
} else {
440+
R_LOG_ERROR ("iter->p shouldnt be null");
441+
}
438442
core->esil.stepback.head = iter->p;
439443
iter->p = NULL;
440444
iter->n = core->esil.stepback.tail;

libr/esil/esil.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* radare - LGPL - Copyright 2014-2024 - pancake, condret */
1+
/* radare - LGPL - Copyright 2014-2025 - pancake, condret */
22

33
#define R_LOG_ORIGIN "esil"
44

@@ -135,7 +135,10 @@ static bool default_reg_read(void *reg, const char *name, ut64 *val) {
135135
if (!ri) {
136136
return false;
137137
}
138-
*val = r_reg_get_value ((RReg *)reg, ri);
138+
ut64 v = r_reg_get_value ((RReg *)reg, ri);
139+
if (val) {
140+
*val = v;
141+
}
139142
r_unref (ri);
140143
return true;
141144
}
@@ -578,7 +581,7 @@ R_API int r_esil_get_parm_type(REsil *esil, const char *str) {
578581
if (r_str_startswith (str, "0x")) {
579582
return R_ESIL_PARM_NUM;
580583
}
581-
if (!((isdigit(str[0])) || str[0] == '-')) {
584+
if (!((isdigit (str[0])) || str[0] == '-')) {
582585
return not_a_number (esil, str);
583586
}
584587
size_t i;
@@ -667,7 +670,7 @@ R_API bool r_esil_reg_read_nocallback(REsil *esil, const char *regname, ut64 *nu
667670

668671
R_API bool r_esil_reg_read(REsil *esil, const char *regname, ut64 *val, ut32 *size) {
669672
#if USE_NEW_ESIL
670-
R_RETURN_VAL_IF_FAIL (esil && regname && val, false);
673+
R_RETURN_VAL_IF_FAIL (esil && regname, false);
671674
if (R_UNLIKELY (!r_esil_reg_read_silent (esil, regname, val, size))) {
672675
return false;
673676
}
@@ -702,7 +705,7 @@ R_API bool r_esil_reg_read(REsil *esil, const char *regname, ut64 *val, ut32 *si
702705
}
703706

704707
R_API bool r_esil_reg_read_silent(REsil *esil, const char *name, ut64 *val, ut32 *size) {
705-
R_RETURN_VAL_IF_FAIL (esil && esil->reg_if.reg_read && name && val, false);
708+
R_RETURN_VAL_IF_FAIL (esil && esil->reg_if.reg_read && name, false);
706709
if (!esil->reg_if.reg_read (esil->reg_if.reg, name, val)) {
707710
return false;
708711
}

libr/esil/esil_ops.c

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* radare - LGPL - Copyright 2024 - pancake, condret */
1+
/* radare - LGPL - Copyright 2024-2025 - pancake, condret */
22

33
#include <r_esil.h>
44
#include <r_anal.h>
@@ -23,6 +23,9 @@
2323
#define OT_FLAG R_ESIL_OP_TYPE_FLAG
2424
#define OT_TRAP R_ESIL_OP_TYPE_TRAP
2525

26+
static bool isreg(REsil *esil, const char* name) {
27+
return r_esil_reg_read (esil, name, NULL, NULL);
28+
}
2629
static ut64 reg_getv(REsil *esil, const char* name) {
2730
ut64 v = UT64_MAX;
2831
if (r_esil_reg_read (esil, name, &v, NULL)) {
@@ -616,8 +619,8 @@ static bool esil_bits(REsil *esil) {
616619
if (popRN (esil, &s)) {
617620
if (esil->anal && esil->anal->coreb.setArchBits) {
618621
esil->anal->coreb.setArchBits (esil->anal->coreb.core, NULL, s);
622+
return true;
619623
}
620-
return true;
621624
}
622625
R_LOG_DEBUG ("esil_bits: missing parameters in stack");
623626
return false;
@@ -642,9 +645,11 @@ static bool esil_syscall(REsil *esil) {
642645
static bool esil_cmd(REsil *esil) {
643646
char *str = r_esil_pop (esil);
644647
if (str) {
645-
if (esil->anal && esil->anal->coreb.setArchBits) {
648+
if (esil->anal && esil->anal->coreb.core) {
646649
esil->anal->coreb.cmd (esil->anal->coreb.core, str);
650+
return true;
647651
}
652+
R_LOG_WARN ("Cannot run RCoreBind.cmd");
648653
}
649654
return false;
650655
}
@@ -678,20 +683,13 @@ static void pushnums(REsil *esil, const char *src, ut64 num2, const char *dst, u
678683
R_RETURN_IF_FAIL (esil);
679684
esil->old = num;
680685
esil->cur = num - num2;
681-
RReg *reg = esil->anal->reg;
682-
RRegItem *ri = r_reg_get (reg, dst, -1);
683-
if (ri) {
686+
if (isreg (esil, dst)) {
684687
esil->lastsz = esil_internal_sizeof_reg (esil, dst);
685-
r_unref (ri);
686-
return;
687-
}
688-
if (ri = r_reg_get (reg, src, -1), ri) {
688+
} else if (isreg (esil, src)) {
689689
esil->lastsz = esil_internal_sizeof_reg (esil, src);
690-
r_unref (ri);
691-
return;
690+
} else {
691+
esil->lastsz = 64;
692692
}
693-
// default size is set to 64 as internally operands are ut64
694-
esil->lastsz = 64;
695693
}
696694

697695
// This function also sets internal vars which is used in flag calculations.

libr/include/r_esil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ typedef struct r_esil_t {
266266
char *cmd_ioer; // r2 (external) command to run when esil fails to IO
267267
char *mdev_range; // string containing the r_str_range to match for read/write accesses
268268
bool (*cmd)(ESIL *esil, const char *name, ut64 a0, ut64 a1);
269-
void *user;
269+
void *user; // RCore *
270270
int stack_fd; // ahem, let's not do this
271271
bool in_cmd_step;
272272
#if 0

0 commit comments

Comments
 (0)