Skip to content

Commit

Permalink
Add op-count / bb-ratio info in afi/afij ##analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
radare authored and trufae committed Jun 1, 2024
1 parent 2de6b74 commit f9c2766
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 8 deletions.
59 changes: 56 additions & 3 deletions libr/core/canal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3008,6 +3008,28 @@ static int RAnalRef_compare_by_addr(const RAnalRef *ref1, const RAnalRef *ref2)
return 0;
}

static double midbbins(RAnalFunction *fcn) {
int bbins = 0;
RAnalBlock *bb;
RListIter *iter;
r_list_foreach (fcn->bbs, iter, bb) {
bbins += bb->ninstr;
}
return (double)bbins / r_list_length (fcn->bbs);
}

static int maxbbins(RAnalFunction *fcn) {
int bbins = 0;
RAnalBlock *bb;
RListIter *iter;
r_list_foreach (fcn->bbs, iter, bb) {
if (bb->ninstr > bbins) {
bbins = bb->ninstr;
}
}
return bbins;
}

// Lists function names and their calls (uniqified)
static int fcn_print_makestyle(RCore *core, RList *fcns, char mode) {
RListIter *fcniter;
Expand Down Expand Up @@ -3162,7 +3184,14 @@ static int fcn_print_json(RCore *core, RAnalFunction *fcn, bool dorefs, PJ *pj)
}
pj_kn (pj, "minbound", r_anal_function_min_addr (fcn));
pj_kn (pj, "maxbound", r_anal_function_max_addr (fcn));

{
int _maxbbins = maxbbins (fcn);
double _midbbins = midbbins (fcn);
double _ratbbins = _maxbbins / _midbbins;
pj_kn (pj, "maxbbins", _maxbbins);
pj_kd (pj, "midbbins", _midbbins);
pj_kd (pj, "ratbbins", _ratbbins);
}
int outdegree = 0;
int indegree = 0;
if (dorefs) {
Expand Down Expand Up @@ -3533,6 +3562,11 @@ static int fcn_print_legacy(RCore *core, RAnalFunction *fcn, bool dorefs) {
}
RVecAnalRef_free (refs);
}
int a = maxbbins (fcn);
double b = midbbins (fcn);
r_cons_printf ("\nmaxbbins: %d", a);
r_cons_printf ("\nmidbbins: %.02f", b);
r_cons_printf ("\nratbbins: %.02f", ((double)a / b));
r_cons_printf ("\nnoreturn: %s", r_str_bool (fcn->is_noreturn));
r_cons_printf ("\nin-degree: %d", indegree);
r_cons_printf ("\nout-degree: %d", outdegree);
Expand Down Expand Up @@ -3580,6 +3614,9 @@ static int fcn_list_table(RCore *core, const char *q, int fmt) {
RTable *t = r_core_table (core, "fcns");
RTableColumnType *typeString = r_table_type ("string");
RTableColumnType *typeNumber = r_table_type ("number");
#if 0 && R2_USE_NEW_ABI
RTableColumnType *typeFloat = r_table_type ("float");
#endif
r_table_add_column (t, typeNumber, "addr", 0);
r_table_add_column (t, typeNumber, "size", 0);
r_table_add_column (t, typeString, "name", 0);
Expand All @@ -3590,6 +3627,11 @@ static int fcn_list_table(RCore *core, const char *q, int fmt) {
r_table_add_column (t, typeNumber, "xref", 0);
r_table_add_column (t, typeNumber, "axref", 0);
r_table_add_column (t, typeNumber, "calls", 0);
#if 0 && R2_USE_NEW_ABI
r_table_add_column (t, typeFloat, "maxbi", 0);
r_table_add_column (t, typeFloat, "midbi", 0);
r_table_add_column (t, typeFloat, "ratbi", 0);
#endif
r_table_add_column (t, typeNumber, "cc", 0);
r_table_add_column (t, typeNumber, "file", 0);
r_list_foreach (core->anal->fcns, iter, fcn) {
Expand All @@ -3611,7 +3653,6 @@ static int fcn_list_table(RCore *core, const char *q, int fmt) {
xrefs = r_anal_function_get_all_xrefs (fcn);
snprintf (axref, sizeof (axref), "%"PFMT64u, xrefs ? RVecAnalRef_length (xrefs) : 0);
RVecAnalRef_free (xrefs);

RVecAnalRef *calls = r_core_anal_fcn_get_calls (core, fcn);
if (calls) {
RVecAnalRef_sort (calls, RAnalRef_compare_by_addr);
Expand All @@ -3626,7 +3667,19 @@ static int fcn_list_table(RCore *core, const char *q, int fmt) {
if (!file) {
file = strdup ("");
}
r_table_add_row (t, fcnAddr, fcnSize, fcn->name, noret, nbbs, nins, refs, xref, axref, castr, ccstr, file, NULL);
#if 0 && R2_USE_NEW_ABI
double _maxbbins = maxbbins (fcn);
double _midbbins = midbbins (fcn);
double _ratbbins = _maxbbins / _midbbins;
r_table_add_row (t, fcnAddr, fcnSize, fcn->name, noret, nbbs,
nins, refs, xref, axref, castr,
_maxbbins, _midbbins, _ratbbins,
ccstr, file, NULL);
#else
r_table_add_row (t, fcnAddr, fcnSize, fcn->name, noret, nbbs,
nins, refs, xref, axref, castr,
ccstr, file, NULL);
#endif
free (file);
}
if (r_table_query (t, q)) {
Expand Down
4 changes: 2 additions & 2 deletions libr/util/pj.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2018-2023 - pancake */
/* radare - LGPL - Copyright 2018-2024 - pancake */

#include <r_util.h>
#include <r_util/r_print.h>
Expand Down Expand Up @@ -322,7 +322,7 @@ R_API PJ *pj_f(PJ *j, float f) {
R_API PJ *pj_d(PJ *j, double d) {
r_return_val_if_fail (j, NULL);
char numstr[64];
snprintf (numstr, sizeof (numstr), "%lf", d);
snprintf (numstr, sizeof (numstr), "%.03lf", d);
pj_comma (j);
pj_raw (j, numstr);
return j;
Expand Down
3 changes: 3 additions & 0 deletions test/db/anal/msp430
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ minbound: 0x00004438
maxbound: 0x00004476
is-lineal: true
end-bbs: 0
maxbbins: 9
midbbins: 3.80
ratbbins: 2.37
noreturn: false
in-degree: 0
out-degree: 6
Expand Down
15 changes: 15 additions & 0 deletions test/db/anal/x86_64
Original file line number Diff line number Diff line change
Expand Up @@ -2705,6 +2705,9 @@ minbound: 0x00000000
maxbound: 0x00000007
is-lineal: true
end-bbs: 0
maxbbins: 1
midbbins: 0.50
ratbbins: 2.00
noreturn: false
in-degree: 0
out-degree: 0
Expand Down Expand Up @@ -2764,6 +2767,9 @@ minbound: 0x00000000
maxbound: 0x0000000d
is-lineal: false
end-bbs: 1
maxbbins: 3
midbbins: 2.50
ratbbins: 1.20
noreturn: false
in-degree: 0
out-degree: 0
Expand Down Expand Up @@ -2839,6 +2845,9 @@ minbound: 0x00000000
maxbound: 0x0000000e
is-lineal: false
end-bbs: 0
maxbbins: 3
midbbins: 2.50
ratbbins: 1.20
noreturn: false
in-degree: 0
out-degree: 0
Expand Down Expand Up @@ -2922,6 +2931,9 @@ minbound: 0x00000000
maxbound: 0x0000000e
is-lineal: false
end-bbs: 0
maxbbins: 3
midbbins: 1.67
ratbbins: 1.80
noreturn: false
in-degree: 0
out-degree: 0
Expand Down Expand Up @@ -3014,6 +3026,9 @@ minbound: 0x00000000
maxbound: 0x00000013
is-lineal: false
end-bbs: 0
maxbbins: 3
midbbins: 1.75
ratbbins: 1.71
noreturn: false
in-degree: 0
out-degree: 0
Expand Down
3 changes: 3 additions & 0 deletions test/db/cmd/cmd_af
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ minbound: 0x00001119
maxbound: 0x00001133
is-lineal: true
end-bbs: 1
maxbbins: 8
midbbins: 8.00
ratbbins: 1.00
noreturn: false
in-degree: 1
out-degree: 0
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_afb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ afi.
afb.
EOF
EXPECT=<<EOF
33
36
main
0x1000011e8 0x100001211 00:0000 41 j 0x100001216 f 0x100001211
EOF
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_afl
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ afij~{.callrefs}
afixj~{.callrefs}
EOF
EXPECT=<<EOF
{"offset":4294983524,"name":"main","size":52,"is-pure":"false","realsz":52,"noreturn":false,"stackframe":32,"calltype":"arm64","cost":5,"cc":1,"bits":64,"type":"fcn","nbbs":1,"tracecov":0,"is-lineal":true,"ninstrs":13,"edges":0,"ebbs":1,"signature":"int main (int argc);","minbound":4294983524,"maxbound":4294983576,"indegree":0,"outdegree":2,"nlocals":2,"nargs":1,"bpvars":[],"spvars":[{"name":"argc","kind":"arg","type":"int64_t","ref":{"base":"sp","offset":64}},{"name":"var_8h","kind":"var","type":"int64_t","ref":{"base":"sp","offset":8}},{"name":"var_10h","kind":"var","type":"int64_t","ref":{"base":"sp","offset":16}}],"regvars":[],"difftype":"new"}
{"offset":4294983524,"name":"main","size":52,"is-pure":"false","realsz":52,"noreturn":false,"stackframe":32,"calltype":"arm64","cost":5,"cc":1,"bits":64,"type":"fcn","nbbs":1,"tracecov":0,"is-lineal":true,"ninstrs":13,"edges":0,"ebbs":1,"signature":"int main (int argc);","minbound":4294983524,"maxbound":4294983576,"maxbbins":13,"midbbins":13.000,"ratbbins":1.000,"indegree":0,"outdegree":2,"nlocals":2,"nargs":1,"bpvars":[],"spvars":[{"name":"argc","kind":"arg","type":"int64_t","ref":{"base":"sp","offset":64}},{"name":"var_8h","kind":"var","type":"int64_t","ref":{"base":"sp","offset":8}},{"name":"var_10h","kind":"var","type":"int64_t","ref":{"base":"sp","offset":16}}],"regvars":[],"difftype":"new"}
--
[{"addr":4294983576,"type":"CALL","at":4294983548},{"addr":4294983588,"type":"CALL","at":4294983556}]
EOF
Expand Down
2 changes: 1 addition & 1 deletion test/db/cmd/cmd_zignature
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ za sym.exact b 415541544989fc55534883ec08e89e02ffff488b2d77952a004889c38b450048:
zbrj sym.exact 1
EOF
EXPECT=<<EOF
[{"name":"fcn.00410210","similarity":1.000000,"byte similarity":1.000000,"graph similarity":1.000000}]
[{"name":"fcn.00410210","similarity":1.000,"byte similarity":1.000,"graph similarity":1.000}]
EOF
RUN

Expand Down

0 comments on commit f9c2766

Please sign in to comment.