Skip to content

Commit aace8c9

Browse files
authored
Reduce core->block use, add more precondition guards, and help for aoc
1 parent edf775b commit aace8c9

File tree

6 files changed

+68
-98
lines changed

6 files changed

+68
-98
lines changed

libr/arch/arch_op.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* radare - LGPL - Copyright 2010-2024 - pancake, nibble, condret */
1+
/* radare - LGPL - Copyright 2010-2026 - pancake, nibble, condret */
22

33
#include <r_arch.h>
44

@@ -16,6 +16,7 @@ R_API bool r_anal_op_set_mnemonic(RAnalOp *op, ut64 addr, const char *s) {
1616
}
1717

1818
R_API bool r_anal_op_set_bytes(RAnalOp *op, ut64 addr, const ut8* data, int size) {
19+
R_RETURN_VAL_IF_FAIL (op && data && size > 0, false);
1920
if (op) {
2021
// TODO: use maxopsz from archbits
2122
op->addr = addr;

libr/arch/arch_session.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ static void _arch_session_free(RArchSession *s) {
1616
}
1717

1818
R_API RArchSession *r_arch_session(RArch *arch, RArchConfig *cfg, RArchPlugin *ap) {
19+
R_RETURN_VAL_IF_FAIL (arch && cfg && ap, false);
1920
RArchSession *ai = R_NEW0 (RArchSession);
2021
r_ref_init (ai, _arch_session_free);
2122
ai->arch = arch;
@@ -35,6 +36,7 @@ R_API RArchSession *r_arch_session(RArch *arch, RArchConfig *cfg, RArchPlugin *a
3536
}
3637

3738
R_API bool r_arch_session_decode(RArchSession *ai, RAnalOp *op, RArchDecodeMask mask) {
39+
R_RETURN_VAL_IF_FAIL (ai && op, false);
3840
RArchPluginDecodeCallback decode = R_UNWRAP3 (ai, plugin, decode);
3941
if (decode != NULL) {
4042
return decode (ai, op, mask);
@@ -43,6 +45,7 @@ R_API bool r_arch_session_decode(RArchSession *ai, RAnalOp *op, RArchDecodeMask
4345
}
4446

4547
R_API bool r_arch_session_patch(RArchSession *ai, RAnalOp *op, RArchEncodeMask mask) {
48+
R_RETURN_VAL_IF_FAIL (ai && op, false);
4649
RArchPluginEncodeCallback encode = R_UNWRAP3 (ai, plugin, encode);
4750
if (encode != NULL) {
4851
return encode (ai, op, mask);
@@ -51,6 +54,7 @@ R_API bool r_arch_session_patch(RArchSession *ai, RAnalOp *op, RArchEncodeMask m
5154
}
5255

5356
R_API bool r_arch_session_encode(RArchSession *ai, RAnalOp *op, RArchEncodeMask mask) {
57+
R_RETURN_VAL_IF_FAIL (ai && op, false);
5458
// TODO R2_590 use the encoder if found in the current session ai->encoder->..
5559
RArchPluginEncodeCallback encode = R_UNWRAP3 (ai, plugin, encode);
5660
if (encode != NULL) {
@@ -60,23 +64,19 @@ R_API bool r_arch_session_encode(RArchSession *ai, RAnalOp *op, RArchEncodeMask
6064
}
6165

6266
R_API RList *r_arch_session_preludes(RArchSession *s) {
63-
if (s) {
64-
RArchPluginPreludesCallback preludes = R_UNWRAP3 (s, plugin, preludes);
65-
if (preludes != NULL) {
66-
return preludes (s);
67-
}
67+
R_RETURN_VAL_IF_FAIL (s, false);
68+
RArchPluginPreludesCallback preludes = R_UNWRAP3 (s, plugin, preludes);
69+
if (preludes != NULL) {
70+
return preludes (s);
6871
}
6972
return NULL;
7073
}
7174

7275
R_API int r_arch_session_info(RArchSession *s, int query) {
73-
if (!s) {
74-
return -1;
75-
}
76+
R_RETURN_VAL_IF_FAIL (s, -1);
7677
RArchPluginInfoCallback info = R_UNWRAP3 (s, plugin, info);
7778
if (info != NULL) {
7879
return info (s, query);
7980
}
8081
return -1;
8182
}
82-

libr/core/canal.c

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -973,13 +973,6 @@ static bool set_fcn_name_from_flag(RCore *core, RAnalFunction *fcn, RFlagItem *f
973973
nameChanged = true;
974974
}
975975
}
976-
#if 0
977-
if (!nameChanged) {
978-
char *nn = r_str_newf ("%s.%08" PFMT64x, fcnpfx, fcn->addr);
979-
r_anal_function_rename (fcn, nn);
980-
free (nn);
981-
}
982-
#endif
983976
return nameChanged;
984977
}
985978

@@ -1225,49 +1218,22 @@ static char *get_title(ut64 addr) {
12251218
return r_str_newf ("0x%"PFMT64x, addr);
12261219
}
12271220

1228-
/* decode and return the RAnalOp at the address addr */
12291221
R_API RAnalOp* r_core_anal_op(RCore *core, ut64 addr, int mask) {
1230-
int len;
1231-
ut8 buf[32];
1232-
ut8 *ptr;
1233-
12341222
R_RETURN_VAL_IF_FAIL (core, NULL);
12351223
if (addr == UT64_MAX) {
12361224
return NULL;
12371225
}
1226+
ut8 buf[32];
12381227
RAnalOp *op = R_NEW0 (RAnalOp);
12391228
int maxopsz = r_anal_archinfo (core->anal, R_ARCH_INFO_MAXOP_SIZE);
1240-
if (sizeof (buf) < maxopsz) {
1229+
if (maxopsz > sizeof (buf)) {
12411230
maxopsz = sizeof (buf);
12421231
}
1243-
int delta = (addr - core->addr);
1244-
int minopsz = 8;
1245-
if (delta > 0 && delta + minopsz < core->blocksize && addr >= core->addr && addr + 16 < core->addr + core->blocksize) {
1246-
ptr = core->block + delta;
1247-
len = core->blocksize - delta;
1248-
if (len < 1) {
1249-
goto err_op;
1250-
}
1251-
} else {
1252-
if (!r_io_read_at (core->io, addr, buf, maxopsz)) {
1253-
goto err_op;
1232+
if (r_io_read_at (core->io, addr, buf, maxopsz)) {
1233+
if (r_anal_op (core->anal, op, addr, buf, maxopsz, mask) > 0) {
1234+
return op;
12541235
}
1255-
ptr = buf;
1256-
len = maxopsz;
12571236
}
1258-
if (r_anal_op (core->anal, op, addr, ptr, len, mask) < 1) {
1259-
goto err_op;
1260-
}
1261-
// TODO This code block must be deleted when all the anal plugs support disasm
1262-
if (!op->mnemonic && mask & R_ARCH_OP_MASK_DISASM) {
1263-
r_asm_set_pc (core->rasm, addr);
1264-
if (r_asm_disassemble (core->rasm, op, ptr, len) < 1) {
1265-
free (op->mnemonic);
1266-
op->mnemonic = strdup ("invalid");
1267-
}
1268-
}
1269-
return op;
1270-
err_op:
12711237
r_anal_op_free (op);
12721238
return NULL;
12731239
}
@@ -5088,23 +5054,21 @@ R_API RCoreAnalStats* r_core_anal_get_stats(RCore *core, ut64 from, ut64 to, ut6
50885054
R_API void r_core_anal_stats_free(RCoreAnalStats *s) {
50895055
if (s) {
50905056
free (s->block);
5057+
free (s);
50915058
}
5092-
free (s);
50935059
}
50945060

50955061
R_API RList* r_core_anal_cycles(RCore *core, int ccl) {
5062+
R_RETURN_VAL_IF_FAIL (core, NULL);
50965063
RCons *cons = core->cons;
50975064
const bool verbose = r_config_get_b (core->config, "scr.interactive") && r_config_get_b (core->config, "scr.prompt");
50985065
ut64 addr = core->addr;
50995066
int depth = 0;
51005067
RAnalOp *op = NULL;
5101-
RAnalCycleFrame *prev = NULL, *cf = NULL;
5068+
RAnalCycleFrame *prev = NULL;
51025069
RAnalCycleHook *ch;
51035070
RList *hooks = r_list_new ();
5104-
if (!hooks) {
5105-
return NULL;
5106-
}
5107-
cf = r_anal_cycle_frame_new ();
5071+
RAnalCycleFrame *cf = r_anal_cycle_frame_new ();
51085072
r_cons_break_push (core->cons, NULL, NULL);
51095073
while (cf && !r_cons_is_breaked (cons)) {
51105074
if ((op = r_core_anal_op (core, addr, R_ARCH_OP_MASK_BASIC)) && (op->cycles) && (ccl > 0)) {
@@ -5305,9 +5269,9 @@ struct r_merge_ctx_t {
53055269
/* Tests if functions are touching */
53065270
bool fcn_merge_touch_cb(ut64 addr, struct r_merge_ctx_t *ctx) {
53075271
RAnalBlock *bb = r_anal_get_block_at(ctx->anal, addr);
5308-
5309-
if (!bb)
5272+
if (!bb) {
53105273
return true;
5274+
}
53115275

53125276
RListIter *iter;
53135277
RAnalFunction *fcn;
@@ -5326,9 +5290,8 @@ bool fcn_merge_touch_cb(ut64 addr, struct r_merge_ctx_t *ctx) {
53265290

53275291
// Add it to the touch list
53285292
if (found) {
5329-
r_list_append(&ctx->touch, bb);
5293+
r_list_append (&ctx->touch, bb);
53305294
}
5331-
53325295
return true;
53335296
}
53345297

libr/core/cio.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ R_API int r_core_seek_base(RCore *core, const char *hex) {
6767

6868
R_API bool r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int append) {
6969
ut64 i;
70-
ut8 *buf;
7170
int bs = core->blocksize;
7271
FILE *fd;
7372
if (append) {
@@ -84,7 +83,7 @@ R_API bool r_core_dump(RCore *core, const char *file, ut64 addr, ut64 size, int
8483
if (bs > 4096) {
8584
bs = 4096;
8685
}
87-
buf = malloc (bs);
86+
ut8 *buf = malloc (bs);
8887
if (!buf) {
8988
R_LOG_ERROR ("Cannot alloc %d byte(s)", bs);
9089
fclose (fd);

libr/core/cmd_anal.inc.c

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9739,9 +9739,48 @@ static void cmd_anal_opcode_bits(RCore *core, const char *arg, int mode) {
97399739
}
97409740
}
97419741

9742+
static void cmd_aoc(RCore *core, const char *input) {
9743+
RListIter *iter;
9744+
RAnalCycleHook *hook;
9745+
char ch = input[1];
9746+
if (ch== '?') {
9747+
r_core_cmd_help_contains (core, help_msg_ao, "aoc");
9748+
return;
9749+
}
9750+
if (ch && ch != ' ') {
9751+
r_core_return_invalid_command (core, "aoc", ch);
9752+
return;
9753+
}
9754+
const int ccl = input[1]? r_num_math (core->num, input + 2): 0;
9755+
if (ccl < 0) {
9756+
R_LOG_ERROR ("aoc expects a positive number");
9757+
return;
9758+
}
9759+
RConfigHold *hc = r_config_hold_new (core->config);
9760+
r_config_hold (hc, "asm.cmt.right", "asm.functions", "asm.lines", "asm.xrefs", NULL);
9761+
r_config_set_b (core->config, "asm.cmt.right", true);
9762+
r_config_set_b (core->config, "asm.functions", false);
9763+
r_config_set_b (core->config, "asm.lines", false);
9764+
r_config_set_b (core->config, "asm.xrefs", false);
9765+
9766+
RList *hooks = r_core_anal_cycles (core, ccl);
9767+
r_list_foreach (hooks, iter, hook) {
9768+
char *ins = r_core_disassemble_instr (core, hook->addr, 1);
9769+
if (ins) {
9770+
size_t count = ccl - hook->cycles;
9771+
r_cons_printf (core->cons, "after %i cycles: %s\n", count, ins);
9772+
free (ins);
9773+
}
9774+
}
9775+
r_list_free (hooks);
9776+
9777+
r_config_hold_restore (hc);
9778+
r_config_hold_free (hc);
9779+
}
9780+
97429781
static void cmd_anal_opcode(RCore *core, const char *input) {
9743-
int l, len = core->blocksize;
97449782
ut32 tbs = core->blocksize;
9783+
int l, len = tbs;
97459784
r_core_block_read (core);
97469785
switch (input[0]) {
97479786
case 's': // "aos"
@@ -9857,39 +9896,8 @@ static void cmd_anal_opcode(RCore *core, const char *input) {
98579896
}
98589897
break;
98599898
case 'c': // "aoc"
9860-
{
9861-
RList *hooks;
9862-
RListIter *iter;
9863-
RAnalCycleHook *hook;
9864-
char *instr_tmp = NULL;
9865-
int ccl = input[1]? r_num_math (core->num, &input[2]): 0; //get cycles to look for
9866-
bool cr = r_config_get_b (core->config, "asm.cmt.right");
9867-
bool fu = r_config_get_b (core->config, "asm.functions");
9868-
bool li = r_config_get_b (core->config, "asm.lines");
9869-
bool xr = r_config_get_b (core->config, "asm.xrefs");
9870-
9871-
r_config_set_b (core->config, "asm.cmt.right", true);
9872-
r_config_set_b (core->config, "asm.functions", false);
9873-
r_config_set_b (core->config, "asm.lines", false);
9874-
r_config_set_b (core->config, "asm.xrefs", false);
9875-
9876-
hooks = r_core_anal_cycles (core, ccl); // analyse
9877-
r_list_foreach (hooks, iter, hook) {
9878-
instr_tmp = r_core_disassemble_instr (core, hook->addr, 1);
9879-
if (instr_tmp) {
9880-
r_cons_printf (core->cons, "After %4i cycles:\t%s", (ccl - hook->cycles), instr_tmp);
9881-
r_cons_flush (core->cons);
9882-
free (instr_tmp);
9883-
}
9884-
}
9885-
r_list_free (hooks);
9886-
9887-
r_config_set_b (core->config, "asm.cmt.right", cr); //reset settings
9888-
r_config_set_b (core->config, "asm.functions", fu);
9889-
r_config_set_b (core->config, "asm.lines", li);
9890-
r_config_set_b (core->config, "asm.xrefs", xr);
9891-
}
9892-
break;
9899+
cmd_aoc (core, input);
9900+
break;
98939901
case 'd': // "aod"
98949902
if (input[1] == 'a') { // "aoda"
98959903
// list sdb database

libr/core/cmd_print.inc.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,7 +1176,6 @@ static void cmd_prc_zoom(RCore *core, const char *input) {
11761176
RVecRIORegion *regions = NULL;
11771177
RIORegion *region = NULL;
11781178
size_t region_idx = 0;
1179-
ut8 *block = core->block;
11801179
int len = core->blocksize;
11811180
if (show_unalloc && core->io->va) {
11821181
RInterval itv = { core->addr, (ut64)len };
@@ -1200,7 +1199,7 @@ static void cmd_prc_zoom(RCore *core, const char *input) {
12001199
}
12011200
} else {
12021201
from = core->addr;
1203-
to = from + core->blocksize;
1202+
to = from + len;
12041203
}
12051204
if (list) {
12061205
r_list_free (list);
@@ -1209,7 +1208,7 @@ static void cmd_prc_zoom(RCore *core, const char *input) {
12091208

12101209
core->print->zoom->mode = (input && *input)? input[1]: 'e';
12111210
r_print_zoom_buf (core->print, printzoomcallback, core, from, to, len, len);
1212-
block = core->print->zoom->buf;
1211+
ut8 *block = core->print->zoom->buf;
12131212

12141213
for (i = 0; i < len; i += cols) {
12151214
ut64 ea = core->addr + i;

0 commit comments

Comments
 (0)