From 99004f3c0b8f554ae1aacb0f168a179ad553d287 Mon Sep 17 00:00:00 2001 From: pancake Date: Tue, 12 Sep 2017 00:25:01 +0200 Subject: [PATCH] Enhance search.in boundaries by removing from/to and always use the list --- libr/core/cmd_anal.c | 85 +++--- libr/core/cmd_debug.c | 4 +- libr/core/cmd_help.c | 20 +- libr/core/cmd_print.c | 10 +- libr/core/cmd_search.c | 578 +++++++++++++++++++++-------------------- libr/core/cmd_zign.c | 16 +- libr/core/file.c | 1 + libr/include/r_core.h | 4 +- libr/io/section.c | 1 - 9 files changed, 380 insertions(+), 339 deletions(-) diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index ef41488e1d..092beeddcf 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -4356,19 +4356,26 @@ static void cmd_anal_calls(RCore *core, const char *input, bool only_print_flag) m->to = addr + len; r_list_append (ranges, m); } else { - ranges = r_core_get_boundaries_prot (core, R_IO_EXEC, "io.sections", NULL, NULL); + ranges = r_core_get_boundaries_prot (core, R_IO_EXEC, "io.sections"); } addr_end = addr + len; } r_cons_break_push (NULL, NULL); if (!binfile || !r_list_length (ranges)) { + RListIter *iter; + RIOMap *map; r_list_free (ranges); const char *search_in = r_config_get (core->config, "search.in"); - ranges = r_core_get_boundaries_prot (core, 0, search_in, &addr, &addr_end); - if (only_print_flag) { - r_cons_printf ("f fcn. 0x%08"PFMT64x" 0 0x%08"PFMT64x"\n", addr, addr); - } else { - _anal_calls (core, addr, addr_end); + ranges = r_core_get_boundaries_prot (core, 0, search_in); + r_list_foreach (ranges, iter, map) { + ut64 addr = map->from; + ut64 addr_end = map->to; + if (only_print_flag) { + r_cons_printf ("f fcn.0x%08"PFMT64x" %d 0x%08"PFMT64x"\n", + addr, addr_end - addr, addr); + } else { + _anal_calls (core, addr, addr_end); + } } } else { RListIter *iter; @@ -4379,7 +4386,8 @@ static void cmd_anal_calls(RCore *core, const char *input, bool only_print_flag) //this normally will happen on fuzzed binaries, dunno if with huge //binaries as well if (only_print_flag) { - r_cons_printf ("f fcn.0x%08"PFMT64x" 0 0x%08"PFMT64x"\n", addr, addr); + r_cons_printf ("f fcn.0x%08"PFMT64x" %d 0x%08"PFMT64x"\n", + addr, (int)addr_end - addr, addr); } else { _anal_calls (core, addr, addr_end); } @@ -5588,52 +5596,69 @@ static void cmd_anal_aav(RCore *core, const char *input) { #define geti(x) r_config_get_i(core->config, x); RIOSection *s = NULL; ut64 o_align = geti ("search.align"); - ut64 from, to, ptr = 0; - ut64 vmin, vmax; + ut64 ptr = 0; bool asterisk = strchr (input, '*');; bool is_debug = r_config_get_i (core->config, "cfg.debug"); - if (is_debug) { - r_list_free (r_core_get_boundaries_prot (core, 0, "dbg.map", &from, &to)); - } else { - s = r_io_section_vget (core->io, core->offset); - if (s) { - from = s->vaddr; - to = s->vaddr + s->size; - } else { - eprintf ("aav: Cannot find section at this address\n"); - // TODO: look in debug maps - return; - } - } + // pre seti ("search.align", 4); char *arg = strchr (input, ' '); if (arg) { ptr = r_num_math (core->num, arg + 1); s = r_io_section_vget (core->io, ptr); } - { + if (false) { RList *ret; if (is_debug) { - ret = r_core_get_boundaries_prot (core, 0, "dbg.map", &vmin, &vmax); + ret = r_core_get_boundaries_prot (core, 0, "dbg.map"); } else { - from = r_config_get_i (core->config, "bin.baddr"); - to = from + ((core->file)? r_io_fd_size (core->io, core->file->fd): 0); + // ut64 from = r_config_get_i (core->config, "bin.baddr"); + //ut64 to = from + ((core->file)? r_io_fd_size (core->io, core->file->fd): 0); if (!s) { eprintf ("aav: Cannot find section at 0x%"PFMT64d"\n", ptr); return; // WTF! } - ret = r_core_get_boundaries_prot (core, 0, "io.sections", &vmin, &vmax); + ret = r_core_get_boundaries_prot (core, 0, "io.sections"); + } + RIOMap *map = r_list_first (ret); + if (map) { + // from = map->from; + // to = map->to; } r_list_free (ret); } - eprintf ("aav: using from to 0x%"PFMT64x" 0x%"PFMT64x"\n", from, to); - eprintf ("Using vmin 0x%"PFMT64x" and vmax 0x%"PFMT64x"\n", vmin, vmax); int vsize = 4; // 32bit dword if (core->assembler->bits == 64) { vsize = 8; } - (void)r_core_search_value_in_range (core, from, to, vmin, vmax, vsize, asterisk, _CbInRangeAav); + + // body + if (is_debug) { + RList *list = r_core_get_boundaries_prot (core, 0, "dbg.map"); + RListIter *iter; + RIOMap *map; + r_list_foreach (list, iter, map) { + eprintf ("aav: from 0x%"PFMT64x" to 0x%"PFMT64x"\n", map->from, map->to); + (void)r_core_search_value_in_range (core, map->from, map->to, + map->from, map->to, map->to - map->from, asterisk, _CbInRangeAav); + } + r_list_free (list); + } else { + s = r_io_section_vget (core->io, core->offset); + if (s) { + ut64 from = s->vaddr; + ut64 to = s->vaddr + s->size; + eprintf ("aav: from 0x%"PFMT64x" to 0x%"PFMT64x"\n", from, to); + (void)r_core_search_value_in_range (core, from, to, + from, to, to - from, asterisk, _CbInRangeAav); + } else { + eprintf ("aav: Cannot find section at this address\n"); + // TODO: look in debug maps + } + } + + // end + seti ("search.align", o_align); } diff --git a/libr/core/cmd_debug.c b/libr/core/cmd_debug.c index ebb877671c..3a7269bc8d 100644 --- a/libr/core/cmd_debug.c +++ b/libr/core/cmd_debug.c @@ -3235,8 +3235,10 @@ static void do_debug_trace_calls(RCore *core, ut64 from, ut64 to, ut64 final_add int n = 0; if (!trace_libs) { - RList *bounds = r_core_get_boundaries (core, "dbg.program", &from, &to); +#if NOOP + RList *bounds = r_core_get_boundaries (core, "dbg.program"); r_list_free (bounds); +#endif } /* set root if not already present */ diff --git a/libr/core/cmd_help.c b/libr/core/cmd_help.c index 19fc4232f8..201939ecee 100644 --- a/libr/core/cmd_help.c +++ b/libr/core/cmd_help.c @@ -232,9 +232,11 @@ R_API void r_core_clippy(const char *msg) { static int cmd_help(void *data, const char *input) { RCore *core = (RCore *)data; + RIOMap *map; const char *k; + RListIter *iter; char *p, out[128] = R_EMPTY; - ut64 n, n2; + ut64 n; int i; RList *tmp; @@ -310,17 +312,19 @@ static int cmd_help(void *data, const char *input) { } break; case 'B': - k = r_str_chop_ro (input+1); - tmp = r_core_get_boundaries (core, k, &n, &n2); - r_cons_printf ("0x%"PFMT64x" 0x%"PFMT64x"\n", n, n2); + k = r_str_chop_ro (input + 1); + tmp = r_core_get_boundaries (core, k); + r_list_foreach (tmp, iter, map) { + r_cons_printf ("0x%"PFMT64x" 0x%"PFMT64x"\n", map->from, map->to); + } r_list_free (tmp); break; case 'd': if (input[1]=='.') { - int cur = R_MAX(core->print->cur, 0); + int cur = R_MAX (core->print->cur, 0); // XXX: we need cmd_xxx.h (cmd_anal.h) - core_anal_bytes(core, core->block + cur, core->blocksize, 1, 'd'); - } else if (input[1]==' '){ + core_anal_bytes (core, core->block + cur, core->blocksize, 1, 'd'); + } else if (input[1] == ' ') { char *d = r_asm_describe (core->assembler, input+2); if (d && *d) { r_cons_println (d); @@ -333,7 +337,7 @@ static int cmd_help(void *data, const char *input) { } break; case 'h': - if (input[1]==' ') { + if (input[1] == ' ') { r_cons_printf ("0x%08x\n", (ut32)r_str_hash (input+2)); } else { eprintf ("Usage: ?h [string-to-hash]\n"); diff --git a/libr/core/cmd_print.c b/libr/core/cmd_print.c index 503b447e37..79f1415f86 100644 --- a/libr/core/cmd_print.c +++ b/libr/core/cmd_print.c @@ -3415,7 +3415,15 @@ static int cmd_print(void *data, const char *input) { r_cons_strcat ("{"); } off = core->offset; - r_list_free (r_core_get_boundaries (core, "file", &from, &to)); + { + RList *list = r_core_get_boundaries (core, "file"); + RIOMap *map = r_list_first (list); + if (map) { + from = map->from; + to = map->to; + } + r_list_free (list); + } piece = R_MAX ((to - from) / w, 1); as = r_core_anal_get_stats (core, from, to, piece); if (!as && mode != '?') { diff --git a/libr/core/cmd_search.c b/libr/core/cmd_search.c index ea5cd869cb..817425adb5 100644 --- a/libr/core/cmd_search.c +++ b/libr/core/cmd_search.c @@ -125,8 +125,10 @@ static unsigned int searchcount = 0; struct search_parameters { RList *boundaries; const char *mode; +/* ut64 from; ut64 to; +*/ bool inverse; bool crypto_search; bool bckwrds; @@ -314,7 +316,7 @@ R_API int r_core_search_preludes(RCore *core) { int cfg_debug = r_config_get_i (core->config, "cfg.debug"); const char *where = cfg_debug? "dbg.map": "io.sections.exec"; - RList *list = r_core_get_boundaries_prot (core, R_IO_EXEC, where, &from, &to); + RList *list = r_core_get_boundaries_prot (core, R_IO_EXEC, where); RListIter *iter; RIOMap *p; @@ -564,55 +566,41 @@ static inline void print_search_progress(ut64 at, ut64 to, int n) { } } -R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char *mode, ut64 *from, ut64 *to) { - RList *list = NULL; - ut64 _from, _to; +static void append_bound(RList *list, RIO *io, ut64 from, ut64 to) { + // TODO: use rinterval + RIOMap *map = R_NEW0 (RIOMap); + if (io && io->desc) { + map->fd = io->desc->fd; + } + map->from = from; + map->to = to; + r_list_append (list, map); +} - if (!from) { - from = &_from; +R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char *mode) { + RList *list = r_list_newf (free); // XXX r_io_map_free); +#if 0 + int fd = -1; + if (core && core->io && core->io->cur) { + fd = core->io->cur->fd; } - if (!to) { - to = &_to; - } - if (!strcmp (mode, "block")) { - *from = core->offset; - *to = core->offset + core->blocksize; +#endif + if (!core->io->va) { + ut64 from = core->offset; + ut64 to = r_io_size (core->io); + append_bound (list, core->io, from, to); + } else if (!strcmp (mode, "block")) { + append_bound (list, core->io, core->offset, core->offset + core->blocksize); } else if (!strcmp (mode, "io.map")) { RIOMap *m = r_io_map_get (core->io, core->offset); if (m) { - *from = m->from; - *to = m->to; - list = r_list_newf ((RListFree)free); - if (!list) { - return NULL; - } - RIOMap *map = R_NEW0 (RIOMap); - *map = *m; - r_list_append (list, map); - } else { - *from = *to = 0; + append_bound (list, core->io, m->from, m->to); } - return list; } else if (!strcmp (mode, "io.maps")) { SdbListIter *iter; RIOMap *m; - *from = *to = 0; - list = r_list_newf ((RListFree)free); - if (!list) { - return NULL; - } ls_foreach (core->io->maps, iter, m) { RIOMap *map = R_NEW0 (RIOMap); - if (!*from) { - *from = map->from; - *to = map->to; - } - if ((m->from < *from) && m->from) { - *from = m->from; - } - if (m->to > *to) { - *to = m->to; - } if (map) { map->fd = m->fd; map->from = m->from; @@ -622,109 +610,81 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char r_list_append (list, map); } } - return list; } else if (!strcmp (mode, "io.section")) { - if (core->io->va) { - SdbListIter *iter; - RIOSection *s; - *from = *to = core->offset; - ls_foreach (core->io->sections, iter, s) { - if (*from >= s->paddr && *from < (s->paddr + s->size)) { - *from = s->vaddr; - *to = s->vaddr + s->vsize - 1; - break; - } - if (*from >= s->vaddr && *from < (s->vaddr + s->vsize)) { - *to = s->vaddr + s->vsize - 1; - break; - } - } - } else { - *from = core->offset; - *to = r_io_size (core->io); + RIOSection *s = r_io_section_get (core->io, core->offset); + if (s) { + append_bound (list, core->io, s->vaddr, s->vaddr + s->vsize); } } else if (!strcmp (mode, "anal.fcn") || !strcmp (mode, "anal.bb")) { - if (core->io->va) { - RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset, - R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM); - if (f) { - *from = f->addr; - *to = f->addr + r_anal_fcn_size (f); + RAnalFunction *f = r_anal_get_fcn_in (core->anal, core->offset, + R_ANAL_FCN_TYPE_FCN | R_ANAL_FCN_TYPE_SYM); + if (f) { + ut64 from = f->addr; + ut64 to = f->addr + r_anal_fcn_size (f); - /* Search only inside the basic block */ - if (!strcmp (mode, "anal.bb")) { - RListIter *iter; - RAnalBlock *bb; + /* Search only inside the basic block */ + if (!strcmp (mode, "anal.bb")) { + RListIter *iter; + RAnalBlock *bb; - r_list_foreach (f->bbs, iter, bb) { - *from = core->offset; - if ((*from >= bb->addr) && (*from < (bb->addr + bb->size))) { - *from = bb->addr; - *to = bb->addr + bb->size; - break; - } - } - } - } else { - eprintf ("WARNING: search.in = ( anal.bb | anal.fcn )"\ - "requires to seek into a valid function\n"); - *from = core->offset; - *to = core->offset + 1; - } - } else { - *from = core->offset; - *to = r_io_size (core->io); - } - } else if (!strncmp (mode, "io.sections", sizeof ("io.sections") - 1)) { - if (core->io->va) { - int mask = 0; - RIOMap *map; - SdbListIter *iter; - RIOSection *s; - - if (!strcmp (mode, "io.sections.exec")) { - mask = R_IO_EXEC; - } - if (!strcmp (mode, "io.sections.write")) { - mask = R_IO_WRITE; - } - - *from = UT64_MAX; - *to = 0; - ls_foreach (core->io->sections, iter, s) { - if (!mask || (s->flags & mask)) { - if (!list) { - list = r_list_newf (free); - maplist = true; - } - map = R_NEW0 (RIOMap); - if (!map) { - eprintf ("RIOMap allocation failed\n"); + r_list_foreach (f->bbs, iter, bb) { + ut64 at = core->offset; + if ((at >= bb->addr) && (at < (bb->addr + bb->size))) { + from = bb->addr; + to = bb->addr + bb->size; break; } - map->fd = s->fd; - map->from = s->vaddr; - map->to = s->vaddr + s->vsize - 1; - if (map->from && map->to) { - if (map->from < *from) { - *from = map->from; - } - if (map->to > *to) { - *to = map->to; - } - } - map->flags = s->flags; - map->delta = 0; - if (!(map->flags & protection)) { - R_FREE (map); - continue; - } - r_list_append (list, map); } } + append_bound (list, core->io, from, to); } else { - *from = core->offset; - *to = r_io_size (core->io); + eprintf ("WARNING: search.in = ( anal.bb | anal.fcn )"\ + "requires to seek into a valid function\n"); + ut64 from = core->offset; + ut64 to = core->offset + 1; + append_bound (list, core->io, from, to); + } + } else if (!strncmp (mode, "io.sections", sizeof ("io.sections") - 1)) { + int mask = 0; + RIOMap *map; + SdbListIter *iter; + RIOSection *s; + + if (!strcmp (mode, "io.sections.exec")) { + mask = R_IO_EXEC; + } + if (!strcmp (mode, "io.sections.write")) { + mask = R_IO_WRITE; + } + + ut64 from = UT64_MAX; + ut64 to = 0; + ls_foreach (core->io->sections, iter, s) { + if (!mask || (s->flags & mask)) { + map = R_NEW0 (RIOMap); + if (!map) { + eprintf ("RIOMap allocation failed\n"); + break; + } + map->fd = s->fd; + map->from = s->vaddr; + map->to = s->vaddr + s->vsize - 1; + if (map->from && map->to) { + if (map->from < from) { + from = map->from; + } + if (map->to > to) { + to = map->to; + } + } + map->flags = s->flags; + map->delta = 0; + if (!(map->flags & protection)) { + R_FREE (map); + continue; + } + r_list_append (list, map); + } } } else if (!strncmp (mode, "dbg.", 4)) { if (core->io->debug) { @@ -741,12 +701,12 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char if (!strcmp (mode, "dbg.map")) { int perm = 0; - *from = *to = core->offset; - list = r_list_newf (free); + ut64 from = core->offset; + ut64 to = core->offset; r_list_foreach (core->dbg->maps, iter, map) { - if (*from >= map->addr && *from < map->addr_end) { - *from = map->addr; - *to = map->addr_end; + if (from >= map->addr && from < map->addr_end) { + from = map->addr; + to = map->addr_end; perm = map->perm; break; } @@ -754,9 +714,9 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char if (perm) { RIOMap *nmap = R_NEW0 (RIOMap); if (nmap) { - nmap->fd = core->io->desc->fd; - nmap->from = *from; - nmap->to = *to; + // nmap->fd = core->io->desc->fd; + nmap->from = from; + nmap->to = to; nmap->flags = perm; nmap->delta = 0; r_list_append (list, nmap); @@ -778,8 +738,8 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char stack = true; } - *from = UT64_MAX; - *to = 0; + ut64 from = UT64_MAX; + ut64 to = 0; r_list_foreach (core->dbg->maps, iter, map) { add = (stack && strstr (map->name, "stack"))? 1: 0; if (!add && (heap && (map->perm & R_IO_WRITE)) && strstr (map->name, "heap")) { @@ -794,15 +754,14 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char if (!nmap) { break; } - nmap->fd = core->io->desc->fd; nmap->from = map->addr; nmap->to = map->addr_end; if (nmap->from && nmap->to) { - if (nmap->from < *from) { - *from = nmap->from; + if (nmap->from < from) { + from = nmap->from; } - if (nmap->to > *to) { - *to = nmap->to; + if (nmap->to > to) { + to = nmap->to; } } nmap->flags = map->perm; @@ -814,40 +773,43 @@ R_API RList *r_core_get_boundaries_prot(RCore *core, int protection, const char } } } - } else { - *from = core->offset; - *to = r_io_size (core->io); } } else { // if (!strcmp (mode, "raw")) { /* obey temporary seek if defined '/x 8080 @ addr:len' */ if (core->tmpseek) { - *from = core->offset; - *to = core->offset + core->blocksize; + ut64 from = core->offset; + ut64 to = core->offset + core->blocksize; + append_bound (list, core->io, from, to); } else { // TODO: repeat last search doesnt works for /a - *from = r_config_get_i (core->config, "search.from"); - if (*from == UT64_MAX) { - *from = core->offset; + ut64 from = r_config_get_i (core->config, "search.from"); + if (from == UT64_MAX) { + from = core->offset; } - *to = r_config_get_i (core->config, "search.to"); - if (*to == UT64_MAX) { + ut64 to = r_config_get_i (core->config, "search.to"); + if (to == UT64_MAX) { if (core->io->va) { /* TODO: section size? */ } else { if (core->file) { - *to = r_io_fd_size (core->io, core->file->fd); + to = r_io_fd_size (core->io, core->file->fd); } } } + append_bound (list, core->io, from, to); } } + if (r_list_empty (list)) { + r_list_free (list); + list = NULL; + } return list; } // XXX: deprecate and use _ok function only -R_API RList *r_core_get_boundaries(RCore *core, const char *mode, ut64 *from, ut64 *to) { - return r_core_get_boundaries_prot (core, R_IO_EXEC | R_IO_WRITE | R_IO_READ, mode, from, to); +R_API RList *r_core_get_boundaries(RCore *core, const char *mode) { + return r_core_get_boundaries_prot (core, R_IO_EXEC | R_IO_WRITE | R_IO_READ, mode); } static bool is_end_gadget(const RAnalOp *aop, const ut8 crop) { @@ -1184,8 +1146,7 @@ R_API RList *r_core_get_boundaries_ok(RCore *core) { if (!strncmp (searchin, "dbg.", 4)\ || !strncmp (searchin, "io.sections", 11)\ || prot & R_IO_EXEC) { /* always true */ - list = r_core_get_boundaries_prot (core, - prot, searchin, &from, &to); + list = r_core_get_boundaries_prot (core, prot, searchin); } else { list = NULL; } @@ -1292,7 +1253,7 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha if (!strncmp (smode, "dbg.", 4)\ || !strncmp (smode, "io.sections", 11)\ || prot & R_IO_EXEC) { - list = r_core_get_boundaries_prot (core, prot, smode, &from, &to); + list = r_core_get_boundaries_prot (core, prot, smode); } else { list = NULL; } @@ -1406,8 +1367,7 @@ static int r_core_search_rop(RCore *core, ut64 from, ut64 to, int opt, const cha r_list_reverse (end_list); // If we have no end gadgets, just skip all of this search nonsense. if (r_list_length (end_list) > 0) { - int prev; - int next, ropdepth; + int prev, next, ropdepth; const int max_inst_size_x86 = 15; // Get the depth of rop search, should just be max_instr // instructions, x86 and friends are weird length instructions, so @@ -1529,7 +1489,13 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c const int hit_combo_limit = r_config_get_i (core->config, "search.esilcombo"); RSearchKeyword kw = R_EMPTY; searchhits = 0; - if (input[1] == ' ') { + if (input[0] == 'E' && input[1] != ' ') { + eprintf ("Usage: /E [esil-expr]\n"); + return; + } + RIOMap *map; + RListIter *iter; + r_list_foreach (param->boundaries, iter, map) { const int kwidx = r_config_get_i (core->config, "search.kwidx"); const int iotrap = r_config_get_i (core->config, "esil.iotrap"); const int stacksize = r_config_get_i (core->config, "esil.stacksize"); @@ -1537,7 +1503,7 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c int hit_happens = 0; int hit_combo = 0; char *res; - ut64 nres, addr = param->from; + ut64 nres, addr = map->from; if (!core->anal->esil) { core->anal->esil = r_anal_esil_new (stacksize, iotrap); } @@ -1553,7 +1519,7 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c core->anal->esil->verbose = 0; r_cons_break_push (NULL, NULL); - for (; addr < param->to; addr++) { + for (; addr < map->to; addr++) { if (core->search->align) { if ((addr % core->search->align)) { continue; @@ -1617,8 +1583,6 @@ static void do_esil_search(RCore *core, struct search_parameters *param, const c } r_config_set_i (core->config, "search.kwidx", kwidx + 1); r_cons_break_pop (); - } else { - eprintf ("Usage: /E [esil-expr]\n"); } r_cons_clear_line (1); } @@ -1634,11 +1598,7 @@ static void do_anal_search(RCore *core, struct search_parameters *param, const c int maxhits, count = 0; bool firstItem = true; - if (!strncmp (param->mode, "dbg.", 4) || !strncmp (param->mode, "io.sections", 11)) { - param->boundaries = r_core_get_boundaries (core, param->mode, ¶m->from, ¶m->to); - } else { - param->boundaries = NULL; - } + param->boundaries = r_core_get_boundaries (core, param->mode); if (*input == 'f') { chk_family = 1; input++; @@ -1681,9 +1641,18 @@ static void do_anal_search(RCore *core, struct search_parameters *param, const c } input = r_str_chop_ro (input); buf = malloc (bsize); + if (!buf) { + eprintf ("Cannot allocate %d bytes\n", bsize); + return; + } maxhits = (int) r_config_get_i (core->config, "search.count"); r_cons_break_push (NULL, NULL); - for (i = 0, at = param->from; at < param->to; at++, i++) { +RIOMap* map; +RListIter *iter; +r_list_foreach (param->boundaries, iter, map) { + ut64 from = map->from; + ut64 to = map->to; + for (i = 0, at = from; at < to; at++, i++) { if (r_cons_is_breaked ()) { break; } @@ -1751,6 +1720,7 @@ static void do_anal_search(RCore *core, struct search_parameters *param, const c } } } +} if (mode == 'j') { r_cons_println ("]\n"); } @@ -1777,15 +1747,16 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch json = 0; } - param->boundaries = r_core_get_boundaries (core, param->mode, ¶m->from, ¶m->to); + r_list_free (param->boundaries); + param->boundaries = r_core_get_boundaries (core, param->mode); maxhits = (int) r_config_get_i (core->config, "search.count"); filter = (int) r_config_get_i (core->config, "asm.filter"); if (!param->boundaries) { map = R_NEW0 (RIOMap); map->fd = core->io->desc->fd; - map->from = param->from; - map->to = param->to; + map->from = r_config_get_i (core->config, "search.from"); + map->to = r_config_get_i (core->config, "search.to"); param->boundaries = r_list_newf (free); r_list_append (param->boundaries, map); maplist = true; @@ -1796,8 +1767,8 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch } r_cons_break_push (NULL, NULL); r_list_foreach (param->boundaries, itermap, map) { - param->from = map->from; - param->to = map->to; + ut64 from = map->from; + ut64 to = map->to; if (r_cons_is_breaked ()) { break; } @@ -1808,7 +1779,7 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch hits = NULL; } else { hits = r_core_asm_strsearch (core, input + 2, - param->from, param->to, maxhits, regexp); + from, to, maxhits, regexp); } if (hits) { const char *cmdhit = r_config_get (core->config, "cmd.hit"); @@ -1904,8 +1875,8 @@ static void do_string_search(RCore *core, struct search_parameters *param) { if (!param->boundaries) { RIOMap *map = R_NEW0 (RIOMap); map->fd = r_io_fd_get_current (core->io); - map->from = param->from; - map->to = param->to; + map->from = r_config_get_i (core->config, "search.from"); + map->to = r_config_get_i (core->config, "search.to"); param->boundaries = r_list_newf (free); r_list_append (param->boundaries, map); maplist = true; @@ -1915,13 +1886,11 @@ static void do_string_search(RCore *core, struct search_parameters *param) { bufsz = core->blocksize; r_cons_break_push (NULL, NULL); r_list_foreach (param->boundaries, iter, map) { - param->from = map->from; - param->to = map->to; searchhits = 0; if (r_cons_is_breaked ()) { break; } - if (param->to < param->from) { + if (map->to < map->from) { eprintf ("invalid from/to values\n"); break; } @@ -1930,36 +1899,36 @@ static void do_string_search(RCore *core, struct search_parameters *param) { if (!json) { RSearchKeyword *kw = r_list_first (core->search->kws); eprintf ("Searching %d bytes in [0x%"PFMT64x "-0x%"PFMT64x "]\n", - kw? kw->keyword_length: 0, param->from, param->to); + kw? kw->keyword_length: 0, map->from, map->to); } if (r_sandbox_enable (0)) { - if ((param->to - param->from) > 1024 * 64) { + if ((map->to - map->from) > 1024 * 64) { eprintf ("Sandbox restricts search range\n"); break; } } if (param->bckwrds) { - if ((param->to - bufsz) <= param->from) { - at = param->from; + if ((map->to - bufsz) <= map->from) { + at = map->from; param->do_bckwrd_srch = false; } else { - at = param->to - bufsz; + at = map->to - bufsz; } } else { - at = param->from; + at = map->from; } /* bckwrds = false -> normal search -> must be at < to bckwrds search -> check later */ - for (; (!param->bckwrds && at < param->to) || param->bckwrds;) { - print_search_progress (at, param->to, searchhits); + for (; (!param->bckwrds && at < map->to) || param->bckwrds;) { + print_search_progress (at, map->to, searchhits); if (r_cons_is_breaked ()) { eprintf ("\n\n"); break; } // avoid searching beyond limits - if ((at + bufsz) > param->to) { - bufsz = param->to - at; + if ((at + bufsz) > map->to) { + bufsz = map->to - at; } if (!r_io_is_valid_offset (core->io, at, 0)) { break; @@ -1988,10 +1957,10 @@ static void do_string_search(RCore *core, struct search_parameters *param) { if (!param->do_bckwrd_srch) { break; } - if ((at - bufsz) < param->from) { + if ((at - bufsz) < map->from) { param->do_bckwrd_srch = false; - bufsz = at - param->from; - at = param->from; + bufsz = at - map->from; + at = map->from; } else { at -= bufsz; } @@ -1999,7 +1968,7 @@ static void do_string_search(RCore *core, struct search_parameters *param) { at += bufsz; } } - print_search_progress (at, param->to, searchhits); + print_search_progress (at, map->to, searchhits); r_cons_clear_line (1); core->num->value = searchhits; if (searchflags && (searchcount > 0) && !json) { @@ -2174,24 +2143,18 @@ static void search_similar_pattern_in(RCore *core, int count, ut64 from, ut64 to static void search_similar_pattern(RCore *core, int count) { RIOMap *p; - ut64 from, to; RListIter *iter; const char *where = r_config_get (core->config, "search.in"); r_cons_break_push (NULL, NULL); - RList *list = r_core_get_boundaries_prot (core, R_IO_EXEC, where, &from, &to); - if (list) { - r_list_foreach (list, iter, p) { - search_similar_pattern_in (core, count, p->from, p->to); - } - r_list_free (list); - } else { - search_similar_pattern_in (core, count, from, to); + RList *list = r_core_get_boundaries_prot (core, R_IO_EXEC, where); + r_list_foreach (list, iter, p) { + search_similar_pattern_in (core, count, p->from, p->to); } + r_list_free (list); r_cons_break_pop (); } - static bool isArm(RCore *core) { RAsm *as = core ? core->assembler : NULL; if (as && as->cur && as->cur->arch) { @@ -2252,7 +2215,8 @@ static int cmd_search(void *data, const char *input) { core->in_search = true; r_flag_space_push (core->flags, "searches"); - param.from = param.to = UT64_MAX; + ut64 from = UT64_MAX; + ut64 to = UT64_MAX; param.inverse = false; param.crypto_search = false; param.bckwrds = false; @@ -2271,14 +2235,13 @@ static int cmd_search(void *data, const char *input) { searchshow = r_config_get_i (core->config, "search.show"); param.mode = r_config_get (core->config, "search.in"); - param.boundaries = r_core_get_boundaries (core, param.mode, - ¶m.from, ¶m.to); + param.boundaries = r_core_get_boundaries (core, param.mode); if (__from != UT64_MAX) { - param.from = __from; + from = __from; } if (__to != UT64_MAX) { - param.to = __to; + to = __to; } /* this introduces a bug until we implement backwards search @@ -2308,25 +2271,24 @@ static int cmd_search(void *data, const char *input) { // TODO: get ranges from current IO section // XXX: Think how to get the section ranges here - if (param.from == UT64_MAX) { - param.from = core->offset; + if (from == UT64_MAX) { + from = core->offset; } - /* we don't really care what's bigger bc there's a flag for backward search from now on 'from' and 'to' represent only the search boundaries, not search direction */ if (core->io->va) { - __from = R_MIN (param.from, param.to); - param.to = R_MAX (param.from, param.to); - param.from = __from; + __from = R_MIN (from, to); + to = R_MAX (from, to); + from = __from; } else { ut64 rawsize = r_io_size (core->io); - param.from = R_MIN (param.from, rawsize); - param.to = R_MIN (param.to, rawsize); + from = R_MIN (from, rawsize); + to = R_MIN (to, rawsize); } core->search->bckwrds = false; - if (param.from == param.to) { + if (from == to) { eprintf ("WARNING from == to?\n"); } /* Quick & dirty check for json output */ @@ -2345,7 +2307,8 @@ reread: { bool bin_verbose = r_config_get_i (core->config, "bin.verbose"); r_config_set_i (core->config, "bin.verbose", false); - cmd_search_bin (core, param.from, param.to); + // TODO : iter maps? + cmd_search_bin (core, from, to); r_config_set_i (core->config, "bin.verbose", bin_verbose); } break; @@ -2357,9 +2320,9 @@ reread: core->search->bckwrds = param.bckwrds = param.do_bckwrd_srch = true; /* if backward search and __to wasn't specified search from the beginning */ - if ((unsigned int) param.to == UT32_MAX) { - param.to = param.from; - param.from = 0; + if ((unsigned int) to == UT32_MAX) { + to = from; + from = 0; } goto reread; case 'o': { // "/o" print the offset of the Previous opcode @@ -2382,7 +2345,8 @@ reread: if (input[1] == '?') { r_core_cmd_help (core, help_msg_slash_R); } else if (input[1] == '/') { - r_core_search_rop (core, param.from, param.to, 0, input + 1, 1); +// TODO search on boundaries + r_core_search_rop (core, from, to, 0, input + 1, 1); } else if (input[1] == 'k') { if (input[2] == '?') { r_core_cmd_help (core, help_msg_slash_Rk); @@ -2390,30 +2354,49 @@ reread: rop_kuery (core, input + 2); } } else { - r_core_search_rop (core, param.from, param.to, 0, input + 1, 0); + r_core_search_rop (core, from, to, 0, input + 1, 0); } goto beach; case 'r': // "/r" and "/re" switch (input[1]) { case 'c': // "/rc" - r_core_anal_search (core, param.from, param.to, UT64_MAX, 'c'); + { + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + r_core_anal_search (core, map->from, map->to, UT64_MAX, 'c'); + } + } break; case 'a': // "/ra" - r_core_anal_search (core, param.from, param.to, UT64_MAX, 0); + { + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + r_core_anal_search (core, map->from, map->to, UT64_MAX, 0); + } + } break; case 'e': // "/re" if (input[2] == '?') { eprintf ("Usage: /re $$ - to find references to current address\n"); } else { - ut64 refptr = r_num_math (core->num, input + 2); - ut64 curseek = core->offset; - r_core_seek (core, param.from, 1); - char *arg = r_str_newf ("%"PFMT64d, param.to - param.from); - char *trg = refptr? r_str_newf ("%"PFMT64d, refptr): strdup (""); - r_core_anal_esil (core, arg, trg); - free (arg); - free (trg); - r_core_seek (core, curseek, 1); + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + ut64 refptr = r_num_math (core->num, input + 2); + ut64 curseek = core->offset; + r_core_seek (core, map->from, 1); + char *arg = r_str_newf ("%"PFMT64d, map->to - map->from); + char *trg = refptr? r_str_newf ("%"PFMT64d, refptr): strdup (""); + r_core_anal_esil (core, arg, trg); + free (arg); + free (trg); + r_core_seek (core, curseek, 1); + } } break; case 'r': // "/rr" @@ -2421,14 +2404,23 @@ reread: break; case ' ': // "/r $$" case 0: // "/r" - if (input[param_offset - 1] == ' ') { - r_core_anal_search (core, param.from, param.to, - r_num_math (core->num, input + 2), 0); - r_core_cmdf (core, "axt @ 0x%"PFMT64x "\n", r_num_math (core->num, input + 2)); - } else { - r_core_anal_search (core, param.from, param.to, - core->offset, 0); - r_core_cmdf (core, "axt @ 0x%"PFMT64x "\n", core->offset); + { + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + ut64 from = map->from; + ut64 to = map->to; + if (input[param_offset - 1] == ' ') { + r_core_anal_search (core, from, to, + r_num_math (core->num, input + 2), 0); + r_core_cmdf (core, "axt @ 0x%"PFMT64x "\n", r_num_math (core->num, input + 2)); + } else { + r_core_anal_search (core, from, to, core->offset, 0); + r_core_cmdf (core, "axt @ 0x%"PFMT64x "\n", core->offset); + } + //r_core_anal_search (core, param.from, param.to, UT64_MAX, 'c'); + // r_core_anal_search (core, map->from, map->to, UT64_MAX, 'c'); + } } break; case '?': @@ -2441,22 +2433,28 @@ reread: break; case 'a': // "/a" if (input[1]) { - char *kwd = r_core_asm_search (core, input + param_offset, - param.from, param.to); - if (kwd) { - r_search_reset (core->search, R_SEARCH_KEYWORD); - r_search_set_distance (core->search, (int) - r_config_get_i (core->config, "search.distance")); - r_search_kw_add (core->search, - r_search_keyword_new_hexmask (kwd, NULL)); - r_search_begin (core->search); - free (kwd); - dosearch = true; - } else { - ret = false; - goto beach; + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + char *kwd = r_core_asm_search (core, input + param_offset, + map->from, map->to); + if (kwd) { + r_search_reset (core->search, R_SEARCH_KEYWORD); + r_search_set_distance (core->search, (int) + r_config_get_i (core->config, "search.distance")); + r_search_kw_add (core->search, + r_search_keyword_new_hexmask (kwd, NULL)); + r_search_begin (core->search); + free (kwd); + dosearch = true; + } else { + ret = false; + goto beach; + } + r_core_anal_search (core, map->from, map->to, UT64_MAX, 0); } - } + } break; case 'C': { // "/C" dosearch = true; @@ -2480,21 +2478,26 @@ reread: if (input[1] == ' ' || input[1] == '\0') { int ret; const char *file = input[1]? input + 2: NULL; - ut64 addr = param.from; - r_cons_break_push (NULL, NULL); - for (; addr < param.to; addr++) { - if (r_cons_is_breaked ()) { - break; + ut64 addr = from; + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + r_cons_break_push (NULL, NULL); + for (addr = map->from; addr < map->to; addr++) { + if (r_cons_is_breaked ()) { + break; + } + ret = r_core_magic_at (core, file, addr, 99, false); + if (ret == -1) { + // something went terribly wrong. + break; + } + addr += ret - 1; } - ret = r_core_magic_at (core, file, addr, 99, false); - if (ret == -1) { - // something went terribly wrong. - break; - } - addr += ret - 1; + r_cons_clear_line (1); + r_cons_break_pop (); } - r_cons_clear_line (1); - r_cons_break_pop (); } else { eprintf ("Usage: /m [file]\n"); } @@ -2505,10 +2508,15 @@ reread: if (input[param_offset - 1]) { int ps = atoi (input + param_offset); if (ps > 1) { - r_cons_break_push (NULL, NULL); - r_search_pattern_size (core->search, ps); - r_search_pattern (core->search, param.from, param.to); - r_cons_break_pop (); + RListIter *iter; + RIOMap *map; + r_list_foreach (param.boundaries, iter, map) { + eprintf ("-- %llx %llx\n", map->from, map->to); + r_cons_break_push (NULL, NULL); + r_search_pattern_size (core->search, ps); + r_search_pattern (core->search, map->from, map->to); + r_cons_break_pop (); + } break; } } @@ -2531,8 +2539,8 @@ reread: ut64 vmax = r_num_math (core->num, w); if (vsize > 0) { err = 0; - int hits = r_core_search_value_in_range (core, param.from, - param.to, vmin, vmax, vsize, asterisk, + int hits = r_core_search_value_in_range (core, from, + to, vmin, vmax, vsize, asterisk, _CbInRangeSearchV); eprintf ("hits: %d\n", hits); } @@ -2668,7 +2676,7 @@ reread: len = r_str_unescape (inp); if (!json) { eprintf ("Searching %d bytes from 0x%08"PFMT64x " to 0x%08"PFMT64x ": ", - len, param.from, param.to); + len, from, to); for (i = 0; i < len; i++) { eprintf ("%02x ", (ut8) inp[i]); } diff --git a/libr/core/cmd_zign.c b/libr/core/cmd_zign.c index 450c111681..d8f8288874 100644 --- a/libr/core/cmd_zign.c +++ b/libr/core/cmd_zign.c @@ -652,7 +652,6 @@ static bool search(RCore *core, bool rad) { RAnalFunction *fcni = NULL; RIOMap *map; bool retval = true; - ut64 sin_from = UT64_MAX, sin_to = UT64_MAX; int hits = 0; struct ctxSearchCB bytes_search_ctx = { core, rad, 0, "bytes" }; @@ -679,17 +678,12 @@ static bool search(RCore *core, bool rad) { // Bytes search if (useBytes) { - list = r_core_get_boundaries_prot (core, R_IO_EXEC | R_IO_WRITE | R_IO_READ, mode, &sin_from, &sin_to); - if (list) { - r_list_foreach (list, iter, map) { - eprintf ("[+] searching 0x%08"PFMT64x" - 0x%08"PFMT64x"\n", map->from, map->to); - retval &= searchRange (core, map->from, map->to, rad, &bytes_search_ctx); - } - r_list_free (list); - } else { - eprintf ("[+] searching 0x%08"PFMT64x" - 0x%08"PFMT64x"\n", sin_from, sin_to); - retval = searchRange (core, sin_from, sin_to, rad, &bytes_search_ctx); + list = r_core_get_boundaries_prot (core, R_IO_EXEC | R_IO_WRITE | R_IO_READ, mode); + r_list_foreach (list, iter, map) { + eprintf ("[+] searching 0x%08"PFMT64x" - 0x%08"PFMT64x"\n", map->from, map->to); + retval &= searchRange (core, map->from, map->to, rad, &bytes_search_ctx); } + r_list_free (list); } // Function search diff --git a/libr/core/file.c b/libr/core/file.c index b0d3775027..40adf17c03 100644 --- a/libr/core/file.c +++ b/libr/core/file.c @@ -63,6 +63,7 @@ R_API int r_core_file_reopen(RCore *core, const char *args, int perm, int loadbi free (obinfilepath); obinfilepath = strdup (ofilepath); + // r_str_chop (path); file = r_core_file_open (core, path, perm, laddr); if (file) { bool had_rbin_info = false; diff --git a/libr/include/r_core.h b/libr/include/r_core.h index cfc9eee4e1..50738b0b1b 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -540,8 +540,8 @@ R_API int r_core_visual_prompt (RCore *core); R_API bool r_core_visual_esil (RCore *core); R_API int r_core_search_preludes(RCore *core); R_API int r_core_search_prelude(RCore *core, ut64 from, ut64 to, const ut8 *buf, int blen, const ut8 *mask, int mlen); -R_API RList* /**/ r_core_get_boundaries_prot (RCore *core, int protection, const char *mode, ut64 *from, ut64 *to); -R_API RList* /**/ r_core_get_boundaries (RCore *core, const char *mode, ut64 *from, ut64 *to); +R_API RList* /**/ r_core_get_boundaries_prot (RCore *core, int protection, const char *mode); +R_API RList* /**/ r_core_get_boundaries (RCore *core, const char *mode); R_API RList* r_core_get_boundaries_ok(RCore *core); R_API int r_core_patch (RCore *core, const char *patch); diff --git a/libr/io/section.c b/libr/io/section.c index d196273c7a..801772f57c 100644 --- a/libr/io/section.c +++ b/libr/io/section.c @@ -153,7 +153,6 @@ R_API bool r_io_section_bin_rm(RIO *io, ut32 bin_id) { return (!(length == ls_length (io->sections))); } - R_API RIOSection *r_io_section_get_name(RIO *io, const char *name) { RIOSection *s; SdbListIter *iter;