Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove duplicated function signature entries #23098

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions libr/anal/sign.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,8 +948,10 @@ R_API int r_sign_all_functions(RAnal *a, bool merge) {
const RSpace *sp = r_spaces_current (&a->zign_spaces);
char *prev_name = NULL;
r_cons_break_push (NULL, NULL);
RCore *core = a->coreb.core;
bool do_mangled = a->coreb.cfggeti (core, "zign.mangled");
RCoreBind cb = a->coreb;
RCore *core = cb.core;
bool do_mangled = cb.cfggeti (core, "zign.mangled");
bool zign_dups = cb.cfggeti (core, "zign.dups");
r_list_foreach_prev (a->fcns, iter, fcn) {
if (r_cons_is_breaked ()) {
break;
Expand All @@ -958,14 +960,14 @@ R_API int r_sign_all_functions(RAnal *a, bool merge) {
RSignItem *it = NULL;
if (merge || !name_exists (a->sdb_zigns, realname, sp)) {
it = item_from_func (a, fcn, realname);
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the fix looks good and i think thats a good default behaviour, but maybe we should allow users to have the same signature twice too. maybe we can have the zign.dups config variable for this, and maybe we need to break the abi to add such option inside RAnalOptions. but i think is better to have this feature as an option and disabled by default rather than removing the code. what do you think?

} else if (zign_dups) {
char *name = get_unique_name (a->sdb_zigns, fcn->name, sp);
if (name) {
it = item_from_func (a, fcn, name);
}
free (name);
free (realname);
}
free (realname);
if (it) {
if (prev_name) {
it->next = prev_name;
Expand Down
1 change: 1 addition & 0 deletions libr/core/cconfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -3869,6 +3869,7 @@ R_API int r_core_config_init(RCore *core) {
SETI ("zign.maxsz", 500, "maximum zignature length");
SETI ("zign.minsz", 16, "minimum zignature length for matching");
SETI ("zign.mincc", 10, "minimum cyclomatic complexity for matching");
SETBPREF ("zign.dups", "false", "allow duplicate zignatures");
SETBPREF ("zign.graph", "true", "use graph metrics for matching");
SETBPREF ("zign.bytes", "true", "use bytes patterns for matching");
SETBPREF ("zign.offset", "false", "use original offset for matching");
Expand Down
25 changes: 25 additions & 0 deletions test/unit/test_sign.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <r_core.h>
#include <r_anal.h>
#include <r_sign.h>

Expand Down Expand Up @@ -116,8 +117,32 @@ static bool test_anal_sign_get_set(void) {
mu_end;
}

bool test_anal_sign_avoid_dup_functions(void) {
RCore *core = r_core_new ();

RAnalFunction *fcn1 = r_anal_create_function (core->anal, "fcn1", 0x2137, 0, NULL);
RAnalBlock *first_block = r_anal_create_block (core->anal, 0x2137, 13);
r_anal_function_add_block (fcn1, first_block);

RAnalFunction *fcn2 = r_anal_create_function (core->anal, "fcn2", 0xdeadbeef, 0, NULL);
RAnalBlock *second_block = r_anal_create_block (core->anal, 0xdeadbeef, 31);
r_anal_function_add_block (fcn2, second_block);

r_core_cmd0 (core, "aF"); // find functions

int count = r_sign_all_functions (core->anal, false); // "zg"
mu_assert_eq (count, 2, "Should create 2 new zignatures for the unseen functions");

count = r_sign_all_functions (core->anal, false);
mu_assert_eq (count, 0, "Should not create new zignatures for the same functions");

r_core_free (core);
mu_end;
}

int all_tests(void) {
mu_run_test (test_anal_sign_get_set);
mu_run_test (test_anal_sign_avoid_dup_functions);
return tests_passed != tests_run;
}

Expand Down
Loading