From d7ff7a97ea7d6c424d59a25eb8db40a8537caaee Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 10 Jan 2019 15:23:16 +0100 Subject: [PATCH] Add bbsum metric for zignatures ##sign --- libr/anal/sign.c | 22 ++++++++++++++++------ libr/core/cmd_zign.c | 4 ++++ libr/include/r_sign.h | 1 + 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/libr/anal/sign.c b/libr/anal/sign.c index 310771b03f..7f26a05517 100644 --- a/libr/anal/sign.c +++ b/libr/anal/sign.c @@ -717,14 +717,14 @@ static void listGraph(RAnal *a, RSignItem *it, int format) { RSignGraph *graph = it->graph; if (format == '*') { - a->cb_printf ("za %s g cc=%d nbbs=%d edges=%d ebbs=%d\n", - it->name, graph->cc, graph->nbbs, graph->edges, graph->ebbs); + a->cb_printf ("za %s g cc=%d nbbs=%d edges=%d ebbs=%d bbsum=%d\n", + it->name, graph->cc, graph->nbbs, graph->edges, graph->ebbs, graph->bbsum); } else if (format == 'j') { - a->cb_printf ("\"graph\":{\"cc\":\"%d\",\"nbbs\":\"%d\",\"edges\":\"%d\",\"ebbs\":\"%d\"},", - graph->cc, graph->nbbs, graph->edges, graph->ebbs); + a->cb_printf ("\"graph\":{\"cc\":%d,\"nbbs\":%d,\"edges\":%d,\"ebbs\":%d,\"bbsum\":%d},", + graph->cc, graph->nbbs, graph->edges, graph->ebbs, graph->bbsum); } else { - a->cb_printf (" graph: cc=%d nbbs=%d edges=%d ebbs=%d\n", - graph->cc, graph->nbbs, graph->edges, graph->ebbs); + a->cb_printf (" graph: cc=%d nbbs=%d edges=%d ebbs=%d bbsum=%d\n", + graph->cc, graph->nbbs, graph->edges, graph->ebbs, graph->bbsum); } } @@ -1227,6 +1227,13 @@ R_API int r_sign_search_update(RAnal *a, RSignSearch *ss, ut64 *at, const ut8 *b return r_search_update (ss->search, *at, buf, len); } +// allow ~10% of margin error +static int matchCount(int a, int b) { + int c = a - b; + int m = a / 10; + return R_ABS (c) < m; +} + static bool fcnMetricsCmp(RSignItem *it, RAnalFunction *fcn) { RSignGraph *graph = it->graph; int ebbs = -1; @@ -1243,6 +1250,9 @@ static bool fcnMetricsCmp(RSignItem *it, RAnalFunction *fcn) { if (graph->ebbs != -1 && graph->ebbs != ebbs) { return false; } + if (graph->bbsum > 0 && matchCount (graph->bbsum, r_anal_fcn_size (fcn))) { + return false; + } return true; } diff --git a/libr/core/cmd_zign.c b/libr/core/cmd_zign.c index 07fbeaadec..c6b1911f4c 100644 --- a/libr/core/cmd_zign.c +++ b/libr/core/cmd_zign.c @@ -122,6 +122,7 @@ static bool addFcnGraph(RCore *core, RAnalFunction *fcn, const char *name) { }; // XXX ebbs doesnt gets initialized if calling this from inside the struct graph.edges = r_anal_fcn_count_edges (fcn, &graph.ebbs); + graph.bbsum = r_anal_fcn_size (fcn); return r_sign_add_graph (core->anal, name, graph); } @@ -176,6 +177,7 @@ static bool parseGraphMetrics(const char *args0, int nargs, RSignGraph *graph) { graph->nbbs = -1; graph->edges = -1; graph->ebbs = -1; + graph->bbsum = 0; for (i = 0; i < nargs; i++) { ptr = r_str_word_get0 (args0, i); @@ -187,6 +189,8 @@ static bool parseGraphMetrics(const char *args0, int nargs, RSignGraph *graph) { graph->edges = atoi (ptr + 6); } else if (r_str_startswith (ptr, "ebbs=")) { graph->ebbs = atoi (ptr + 5); + } else if (r_str_startswith (ptr, "bbsum=")) { + graph->bbsum = atoi (ptr + 6); } else { return false; } diff --git a/libr/include/r_sign.h b/libr/include/r_sign.h index 211f3e0e45..86376de491 100644 --- a/libr/include/r_sign.h +++ b/libr/include/r_sign.h @@ -32,6 +32,7 @@ typedef struct r_sign_graph_t { int nbbs; int edges; int ebbs; + int bbsum; } RSignGraph; typedef struct r_sign_bytes_t {