From c12ad43b388fba82765c62de2de4b9d521007368 Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 28 Feb 2021 20:56:56 +0100 Subject: [PATCH] Fix issue when highlighting edges on low addresses + test --- libr/core/agraph.c | 37 +++++++++++++++++++++++-------------- libr/core/canal.c | 11 +++++++---- libr/core/cmd_anal.c | 4 ++-- test/db/cmd/cmd_graph | 39 ++++++++++++++++++++++++++++++++++----- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/libr/core/agraph.c b/libr/core/agraph.c index 6003b5a107..d2a2086b2d 100644 --- a/libr/core/agraph.c +++ b/libr/core/agraph.c @@ -333,8 +333,8 @@ static char *get_node_color(int color, int cur) { return cur ? cons->context->pal.graph_box2 : cons->context->pal.graph_box; } return color ? (\ - color==R_ANAL_DIFF_TYPE_MATCH ? cons->context->pal.graph_diff_match: - color==R_ANAL_DIFF_TYPE_UNMATCH? cons->context->pal.graph_diff_unmatch : cons->context->pal.graph_diff_new): cons->context->pal.graph_diff_unknown; + color == R_ANAL_DIFF_TYPE_MATCH ? cons->context->pal.graph_diff_match: + color == R_ANAL_DIFF_TYPE_UNMATCH? cons->context->pal.graph_diff_unmatch : cons->context->pal.graph_diff_new): cons->context->pal.graph_diff_unknown; } static void normal_RANode_print(const RAGraph *g, const RANode *n, int cur) { @@ -2326,7 +2326,10 @@ static void add_child(RCore *core, RAGraph *g, RANode *u, ut64 jump) { char *title = get_title (jump); RANode *v = r_agraph_get_node (g, title); free (title); - bool hl = sdb_const_get (core->sdb, sdb_fmt ("agraph.edge.%s_%s.highlight", u->title, title), 0) != NULL; + ut64 a = r_num_get (NULL, u->title); + ut64 b = r_num_get (NULL, title); + const char *k = sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", a, b); + bool hl = sdb_exists (core->sdb, k); r_agraph_add_edge (g, u, v, hl); } @@ -2950,10 +2953,19 @@ static void agraph_print_edges(RAGraph *g) { break; } } - if (sdb_const_get (g->db, sdb_fmt ("agraph.edge.%s_%s.highlight", a->title, b->title), 0)) { - style.ansicolor = Color_BYELLOW; // it's CYAN for graphviz - } else { - style.ansicolor = NULL; + if (!*b->title) { + /// XXX non-colorized edges happen because of those ghost nodes + // eprintf ("%s|%s%c", a->title, b->title, 10); + } + if (!R_STR_ISEMPTY (a->title) && !R_STR_ISEMPTY (b->title)) { + ut64 aa = r_num_get (NULL, a->title); + ut64 bb = r_num_get (NULL, b->title); + const char *k = sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", aa, bb); + if (sdb_exists (g->db, k)) { + style.ansicolor = Color_BYELLOW; // it's CYAN for graphviz + } else { + style.ansicolor = NULL; + } } switch (g->layout) { case 0: @@ -3855,7 +3867,9 @@ R_API void r_agraph_add_edge(const RAGraph *g, RANode *a, RANode *b, bool highli r_return_if_fail (g && a && b); r_graph_add_edge (g->graph, a->gnode, b->gnode); if (highlight) { - const char *k = sdb_fmt ("agraph.edge.%s_%s.highlight", a->title, b->title); + ut64 aa = r_num_get (NULL, a->title); + ut64 bb = r_num_get (NULL, b->title); + const char *k = sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", aa, bb); sdb_set (g->db, k, "true", 0); } if (a->title && b->title) { @@ -4848,13 +4862,8 @@ R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int { RIOUndos *undo = r_io_sundo (core->io, core->offset); r_io_sundo_redo (core->io); - - char *a = r_str_newf ("0x%08"PFMT64x, undo->off); - char *b = r_str_newf ("0x%08"PFMT64x, core->offset); - char *c = r_str_newf ("agraph.edge.%s_%s.highlight", a, b); + char *c = r_str_newf ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", undo->off, core->offset); sdb_set (g->db, c, "true", 0); - free (a); - free (b); free (c); } break; diff --git a/libr/core/canal.c b/libr/core/canal.c index 22b7aabba9..1d1207ecf7 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -1488,12 +1488,15 @@ static int core_anal_graph_construct_edges(RCore *core, RAnalFunction *fcn, int char *from = get_title (bbi->addr); char *to = get_title (bbi->jump); r_cons_printf ("age %s %s\n", from, to); - free(from); - free(to); + free (from); + free (to); } else { + const char* edge_color = bbi->fail != -1 ? pal_jump : pal_trfa; + if (sdb_const_get (core->sdb, sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", bbi->addr, bbi->jump), 0)) { + edge_color = "cyan"; + } r_cons_printf (" \"0x%08"PFMT64x"\" -> \"0x%08"PFMT64x"\" " - "[color=\"%s\"];\n", bbi->addr, bbi->jump, - bbi->fail != -1 ? pal_jump : pal_trfa); + "[color=\"%s\"];\n", bbi->addr, bbi->jump, edge_color); core_anal_color_curr_node (core, bbi); } } diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 552aa046a6..50b3a5a9f3 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -8467,7 +8467,7 @@ static void agraph_print_edge_dot(RANode *from, RANode *to, void *user) { RCore *core = (RCore *)user; ut64 a = r_num_math (NULL, from->title); ut64 b = r_num_math (NULL, to->title); - const char *k = sdb_fmt ("agraph.edge.0x%08"PFMT64x"_0x%08"PFMT64x".highlight", a, b); + const char *k = sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", a, b); if (sdb_exists (core->sdb, k)) { r_cons_printf ("\"%s\" -> \"%s\" [color=cyan]\n", from->title, to->title); } else { @@ -8564,7 +8564,7 @@ static bool cmd_ageh(RCore *core, const char *input) { ut64 a = r_num_math (core->num, arg); ut64 b = r_num_math (core->num, sp); - const char *k = sdb_fmt ("agraph.edge.0x%08"PFMT64x"_0x%08"PFMT64x".highlight", a, b); + const char *k = sdb_fmt ("agraph.edge.0x%"PFMT64x"_0x%"PFMT64x".highlight", a, b); sdb_set (core->sdb, k, add? "true": "", 0); return true; } diff --git a/test/db/cmd/cmd_graph b/test/db/cmd/cmd_graph index 40c4028a9d..183fe6a4fa 100644 --- a/test/db/cmd/cmd_graph +++ b/test/db/cmd/cmd_graph @@ -395,12 +395,41 @@ ageh 0x123 0x123 ageh EOF EXPECT=< "0x00000479" [color="#13a10e"]; + "0x0000044b" -> "0x00000452" [color="#c50f1f"]; + "0x00000452" -> "0x00000458" [color="cyan"]; + "0x00000458" -> "0x00000458" [color="#13a10e"]; + "0x00000458" -> "0x00000479" [color="#c50f1f"]; +} EOF RUN