Skip to content

Commit

Permalink
analysis: sort function by addr when print (afl, aflm, afl=)
Browse files Browse the repository at this point in the history
  • Loading branch information
imbillow committed Feb 2, 2024
1 parent b4e73e3 commit 4b1555b
Show file tree
Hide file tree
Showing 19 changed files with 718 additions and 681 deletions.
4 changes: 3 additions & 1 deletion librz/asm/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ static bool has_bits(RzAsmPlugin *h, int bits) {

RZ_DEPRECATE RZ_API int rz_asm_set_bits(RzAsm *a, int bits) {
if (has_bits(a->cur, bits)) {
a->bits = bits; // TODO : use OR? :)
if (a->bits != bits) {
a->bits = bits; // TODO : use OR? :)
}
return true;
}
return false;
Expand Down
1 change: 1 addition & 0 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2464,6 +2464,7 @@ RZ_API void rz_core_analysis_data(RZ_NONNULL RzCore *core, ut64 addr, ut32 count
ut8 *buf = core->block;
ut32 old_len = core->blocksize;
ut64 old_offset = core->offset;
rz_core_seek_arch_bits(core, addr);
int word = wordsize ? wordsize : core->rasm->bits / 8;
char *str = NULL;
RzConsPrintablePalette *pal = rz_config_get_i(core->config, "scr.color") ? &rz_cons_singleton()->context->pal : NULL;
Expand Down
39 changes: 35 additions & 4 deletions librz/core/cmd/cmd_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -3796,9 +3796,36 @@ static void function_list_print_to_json(RzCore *core, RzList /*<RzAnalysisFuncti
pj_end(state->d.pj);
}

static int fcn_cmp_addr(const void *a, const void *b) {
const RzAnalysisFunction *fa = a;
const RzAnalysisFunction *fb = b;
if (fa->addr > fb->addr) {
return 1;
} else if (fa->addr == fb->addr) {
return 0;
}
return -1;
}

static RzList *functions_sorted_by_addr(RzAnalysis *analysis) {
RzList *list = rz_analysis_function_list(analysis);
if (!list) {
return NULL;
}
RzList *sorted = rz_list_clone(list);
if (!sorted) {
return NULL;
}
rz_list_sort(sorted, fcn_cmp_addr);
return sorted;
}

RZ_IPI RzCmdStatus rz_analysis_function_list_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
RzCmdStatus res = RZ_CMD_STATUS_OK;
RzList *list = rz_analysis_function_list(core->analysis);
RzList *list = functions_sorted_by_addr(core->analysis);
if (!list) {
return RZ_CMD_STATUS_ERROR;
}
switch (state->mode) {
case RZ_OUTPUT_MODE_STANDARD:
function_list_print(core, list);
Expand Down Expand Up @@ -3828,6 +3855,7 @@ RZ_IPI RzCmdStatus rz_analysis_function_list_handler(RzCore *core, int argc, con
res = RZ_CMD_STATUS_WRONG_ARGS;
break;
}
rz_list_free(list);
return res;
}

Expand Down Expand Up @@ -3922,7 +3950,10 @@ static void function_print_calls(RzCore *core, RzList /*<RzAnalysisFunction *>*/

RZ_IPI RzCmdStatus rz_analysis_function_list_calls_handler(RzCore *core, int argc, const char **argv, RzCmdStateOutput *state) {
RzCmdStatus res = RZ_CMD_STATUS_OK;
RzList *list = rz_analysis_function_list(core->analysis);
RzList *list = functions_sorted_by_addr(core->analysis);
if (!list) {
return RZ_CMD_STATUS_ERROR;
}
switch (state->mode) {
case RZ_OUTPUT_MODE_STANDARD:
case RZ_OUTPUT_MODE_QUIET:
Expand All @@ -3934,15 +3965,15 @@ RZ_IPI RzCmdStatus rz_analysis_function_list_calls_handler(RzCore *core, int arg
res = RZ_CMD_STATUS_WRONG_ARGS;
break;
}
rz_list_free(list);
return res;
}

RZ_IPI RzCmdStatus rz_analysis_function_list_ascii_handler(RzCore *core, int argc, const char **argv) {
RzList *fcns = rz_list_clone(rz_analysis_function_list(core->analysis));
RzList *fcns = functions_sorted_by_addr(core->analysis);
if (!fcns) {
return RZ_CMD_STATUS_ERROR;
}
rz_list_sort(fcns, fcn_cmpaddr);
RzList *flist = rz_list_newf((RzListFree)rz_listinfo_free);
if (!flist) {
rz_list_free(fcns);
Expand Down
3 changes: 3 additions & 0 deletions librz/core/disasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -6604,6 +6604,9 @@ RZ_API RZ_OWN char *rz_core_disasm_instruction(RzCore *core, ut64 addr, ut64 rel
}
rz_io_read_at(core->io, addr, buf, size);
rz_asm_set_pc(core->rasm, addr);
// use core binding to set asm.bits correctly based on the addr
// this is because of the hassle of arm/thumb
rz_core_seek_arch_bits(core, addr);
rz_asm_disassemble(core->rasm, &asmop, buf, size);
int ba_len = rz_strbuf_length(&asmop.buf_asm) + 128;
char *ba = malloc(ba_len);
Expand Down
14 changes: 7 additions & 7 deletions test/db/analysis/arm
Original file line number Diff line number Diff line change
Expand Up @@ -590,21 +590,21 @@ aa
afl
EOF
EXPECT=<<EOF
0x000102c8 1 44 entry0
0x00010278 1 12 sym._init
0x00010298 1 12 sym.imp.printf
0x000102a4 1 12 sym.imp.__libc_start_main
0x000102bc 1 12 sym.imp.abort
0x000102c8 1 44 entry0
0x00010304 1 28 sym.call_weak_fn
0x00010328 1 36 sym.deregister_tm_clones
0x00010358 1 44 sym.register_tm_clones
0x00010390 1 36 sym.__do_global_dtors_aux
0x000103b8 4 44 entry.init0
0x00010494 1 4 sym.__libc_csu_fini
0x00010498 1 8 sym._fini
0x000103ec 1 28 sym.func
0x00010434 3 88 sym.__libc_csu_init
0x00010278 1 12 sym._init
0x00010408 1 40 main
0x00010298 1 12 sym.imp.printf
0x000102bc 1 12 sym.imp.abort
0x00010434 3 88 sym.__libc_csu_init
0x00010494 1 4 sym.__libc_csu_fini
0x00010498 1 8 sym._fini
EOF
RUN

Expand Down
12 changes: 6 additions & 6 deletions test/db/analysis/dalvik
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ NAME=Dalvik HelloWorld afl
FILE=bins/dex/HelloWorld.dex
CMDS=aa; afl
EXPECT=<<EOF
0x100000238 1 20 method.public.constructor.Hello.void__init
0x10000025c 1 34 entry0
0x100000290 1 84 method.public.Hello.void_foo_int
0x1000002f4 1 20 method.public.constructor.World.void__init
0x100000318 1 68 method.public.World.void_foo_int
0x8000000000 1 2 sym.java.io.PrintStream.void_println_String
0x100000238 1 20 method.public.constructor.Hello.void__init
0x8000000002 1 2 sym.Integer.void__init__int
0x8000000004 1 2 sym.Integer.String_toString
0x8000000006 1 2 sym.Object.void__init
0x100000290 1 84 method.public.Hello.void_foo_int
0x8000000008 1 2 sym.StringBuilder.void__init
0x800000000a 1 2 sym.StringBuilder.StringBuilder_append_String
0x8000000002 1 2 sym.Integer.void__init__int
0x8000000004 1 2 sym.Integer.String_toString
0x800000000c 1 2 sym.StringBuilder.String_toString
0x1000002f4 1 20 method.public.constructor.World.void__init
0x100000318 1 68 method.public.World.void_foo_int
EOF
RUN

Expand Down
Loading

0 comments on commit 4b1555b

Please sign in to comment.