Rename r_sign_anal_additem to r_sign_add_item and add r_sign_get_item ##signatures (#17691)

This commit is contained in:
Florian Märkl 2020-09-22 14:09:17 +02:00 committed by GitHub
parent c9e98e4645
commit 1c485e8bfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 151 additions and 14 deletions

View File

@ -631,7 +631,26 @@ static void mergeItem(RSignItem *dst, RSignItem *src) {
}
}
R_API bool r_sign_anal_additem(RAnal *a, RSignItem *it) {
R_API RSignItem *r_sign_get_item(RAnal *a, const char *name) {
char k[R_SIGN_KEY_MAXSZ];
serializeKey (a, r_spaces_current (&a->zign_spaces), name, k);
const char *v = sdb_const_get (a->sdb_zigns, k, 0);
if (!v) {
return NULL;
}
RSignItem *it = r_sign_item_new ();
if (!it) {
return NULL;
}
if (!r_sign_deserialize (a, it, k, v)) {
r_sign_item_free (it);
return NULL;
}
return it;
}
R_API bool r_sign_add_item(RAnal *a, RSignItem *it) {
char key[R_SIGN_KEY_MAXSZ], val[R_SIGN_VAL_MAXSZ];
const char *curval = NULL;
bool retval = true;
@ -681,7 +700,7 @@ static bool addHash(RAnal *a, const char *name, int type, const char *val) {
switch (type) {
case R_SIGN_BBHASH:
it->hash->bbhash = strdup (val);
retval = r_sign_anal_additem (a, it);
retval = r_sign_add_item (a, it);
r_sign_item_free (it);
break;
}
@ -702,7 +721,7 @@ static bool addBBHash(RAnal *a, RAnalFunction *fcn, const char *name) {
it->space = r_spaces_current (&a->zign_spaces);
if (r_sign_addto_item (a, it, fcn, R_SIGN_BBHASH)) {
retval = r_sign_anal_additem (a, it);
retval = r_sign_add_item (a, it);
}
beach:
r_sign_item_free (it);
@ -743,7 +762,7 @@ static bool addBytes(RAnal *a, const char *name, ut64 size, const ut8 *bytes, co
goto fail;
}
memcpy (it->bytes->mask, mask, size);
retval = r_sign_anal_additem (a, it);
retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
fail:
@ -946,7 +965,7 @@ R_API bool r_sign_add_graph(RAnal *a, const char *name, RSignGraph graph) {
return false;
}
*it->graph = graph;
retval = r_sign_anal_additem (a, it);
retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
@ -962,7 +981,7 @@ R_API bool r_sign_add_comment(RAnal *a, const char *name, const char *comment) {
it->name = r_str_new (name);
it->space = r_spaces_current (&a->zign_spaces);
it->comment = strdup (comment);
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
}
@ -974,7 +993,7 @@ R_API bool r_sign_add_name(RAnal *a, const char *name, const char *realname) {
it->name = r_str_new (name);
it->realname = strdup (realname);
it->space = r_spaces_current (&a->zign_spaces);
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
}
@ -992,7 +1011,7 @@ R_API bool r_sign_add_addr(RAnal *a, const char *name, ut64 addr) {
it->space = r_spaces_current (&a->zign_spaces);
it->addr = addr;
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
@ -1019,7 +1038,7 @@ R_API bool r_sign_add_vars(RAnal *a, const char *name, RList *vars) {
r_list_foreach (vars, iter, var) {
r_list_append (it->vars, strdup (var));
}
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
@ -1045,7 +1064,7 @@ R_API bool r_sign_add_types(RAnal *a, const char *name, RList *types) {
r_list_foreach (types, iter, type) {
r_list_append (it->types, strdup (type));
}
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
@ -1070,7 +1089,7 @@ R_API bool r_sign_add_refs(RAnal *a, const char *name, RList *refs) {
r_list_foreach (refs, iter, ref) {
r_list_append (it->refs, strdup (ref));
}
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;
@ -1095,7 +1114,7 @@ R_API bool r_sign_add_xrefs(RAnal *a, const char *name, RList *xrefs) {
r_list_foreach (xrefs, iter, ref) {
r_list_append (it->xrefs, strdup (ref));
}
bool retval = r_sign_anal_additem (a, it);
bool retval = r_sign_add_item (a, it);
r_sign_item_free (it);
return retval;

View File

@ -157,7 +157,7 @@ static void addFcnZign(RCore *core, RAnalFunction *fcn, const char *name) {
/* r_sign_add_addr (core->anal, zigname, fcn->addr); */
// commit the item to anal
r_sign_anal_additem (core->anal, it);
r_sign_add_item (core->anal, it);
/*
XXX this is very slow and poorly tested

View File

@ -114,6 +114,8 @@ R_API bool r_sign_add_hash(RAnal *a, const char *name, int type, const char *val
R_API bool r_sign_add_bb_hash(RAnal *a, RAnalFunction *fcn, const char *name);
R_API char *r_sign_calc_bbhash(RAnal *a, RAnalFunction *fcn);
R_API bool r_sign_deserialize(RAnal *a, RSignItem *it, const char *k, const char *v);
R_API RSignItem *r_sign_get_item(RAnal *a, const char *name);
R_API bool r_sign_add_item(RAnal *a, RSignItem *it);
R_API bool r_sign_foreach(RAnal *a, RSignForeachCallback cb, void *user);
@ -132,7 +134,6 @@ R_API bool r_sign_load(RAnal *a, const char *file);
R_API bool r_sign_load_gz(RAnal *a, const char *filename);
R_API char *r_sign_path(RAnal *a, const char *file);
R_API bool r_sign_save(RAnal *a, const char *file);
R_API bool r_sign_anal_additem(RAnal *a, RSignItem *it);
R_API RSignItem *r_sign_item_new(void);
R_API void r_sign_item_free(RSignItem *item);

View File

@ -43,6 +43,7 @@ if get_option('enable_tests')
'queue',
'r2r',
'rbtree',
'sign',
'skiplist',
'spaces',
'sparse',

116
test/unit/test_sign.c Normal file
View File

@ -0,0 +1,116 @@
#include <r_anal.h>
#include <r_sign.h>
#include "minunit.h"
static bool test_anal_sign_get_set(void) {
RAnal *anal = r_anal_new ();
RSignItem *item = r_sign_item_new ();
item->name = strdup ("sym.mahboi");
item->realname = strdup ("sym.Mah.Boi");
item->comment = strdup ("This peace is what all true warriors strive for");
item->bytes = R_NEW0 (RSignBytes);
item->bytes->size = 4;
item->bytes->bytes = (ut8 *)strdup ("\xde\xad\xbe\xef");
item->bytes->mask = (ut8 *)strdup ("\xc0\xff\xee\x42");
item->graph = R_NEW0 (RSignGraph);
item->graph->bbsum = 42;
item->graph->cc = 123;
item->graph->ebbs = 13;
item->graph->edges = 12;
item->graph->nbbs = 11;
item->addr = 0x1337;
item->refs = r_list_newf (free);
r_list_append (item->refs, strdup ("gwonam"));
r_list_append (item->refs, strdup ("link"));
item->xrefs = r_list_newf (free);
r_list_append (item->xrefs, strdup ("king"));
r_list_append (item->xrefs, strdup ("ganon"));
item->vars = r_list_newf (free);
r_list_append (item->vars, strdup ("r16"));
r_list_append (item->vars, strdup ("s42"));
r_list_append (item->vars, strdup ("b13"));
item->types = r_list_newf (free);
r_list_append (item->types, strdup ("func.sym.mahboi.ret=char *"));
r_list_append (item->types, strdup ("func.sym.mahboi.args=2"));
r_list_append (item->types, strdup ("func.sym.mahboi.arg.0=\"int,arg0\""));
r_list_append (item->types, strdup ("func.sym.mahboi.arg.1=\"uint32_t,die\""));
item->hash = R_NEW0 (RSignHash);
item->hash->bbhash = strdup ("7bfa1358c427e26bc03c2384f41de7be6ebc01958a57e9a6deda5bdba9768851");
r_sign_add_item (anal, item);
r_sign_item_free (item);
r_spaces_set (&anal->zign_spaces, "koridai");
r_sign_add_comment (anal, "sym.boring", "gee it sure is boring around here");
// --
r_spaces_set (&anal->zign_spaces, NULL);
item = r_sign_get_item (anal, "sym.mahboi");
mu_assert_notnull (item, "get item");
mu_assert_streq (item->name, "sym.mahboi", "name");
mu_assert_streq (item->realname, "sym.Mah.Boi", "realname");
mu_assert_streq (item->comment, "This peace is what all true warriors strive for", "comment");
mu_assert_notnull (item->bytes, "bytes");
mu_assert_eq (item->bytes->size, 4, "bytes size");
mu_assert_memeq (item->bytes->bytes, (ut8 *)"\xde\xad\xbe\xef", 4, "bytes bytes");
mu_assert_memeq (item->bytes->mask, (ut8 *)"\xc0\xff\xee\x42", 4, "bytes mask");
mu_assert_notnull (item->graph, "graph");
mu_assert_eq (item->graph->bbsum, 42, "graph bbsum");
mu_assert_eq (item->graph->cc, 123, "graph cc");
mu_assert_eq (item->graph->ebbs, 13, "graph ebbs");
mu_assert_eq (item->graph->edges, 12, "graph edges");
mu_assert_eq (item->graph->nbbs, 11, "graph nbbs");
mu_assert_eq (item->addr, 0x1337, "addr");
mu_assert_notnull (item->refs, "refs");
mu_assert_eq (r_list_length (item->refs), 2, "refs count");
mu_assert_streq (r_list_get_n (item->refs, 0), "gwonam", "ref");
mu_assert_streq (r_list_get_n (item->refs, 1), "link", "ref");
mu_assert_notnull (item->xrefs, "xrefs");
mu_assert_eq (r_list_length (item->xrefs), 2, "xrefs count");
mu_assert_streq (r_list_get_n (item->xrefs, 0), "king", "xref");
mu_assert_streq (r_list_get_n (item->xrefs, 1), "ganon", "xref");
mu_assert_notnull (item->vars, "vars");
mu_assert_eq (r_list_length (item->vars), 3, "vars count");
mu_assert_streq (r_list_get_n (item->vars, 0), "r16", "var");
mu_assert_streq (r_list_get_n (item->vars, 1), "s42", "var");
mu_assert_streq (r_list_get_n (item->vars, 2), "b13", "var");
mu_assert_notnull (item->types, "types");
mu_assert_eq (r_list_length (item->types), 4, "types count");
mu_assert_streq (r_list_get_n (item->types, 0), "func.sym.mahboi.ret=char *", "type");
mu_assert_streq (r_list_get_n (item->types, 1), "func.sym.mahboi.args=2", "type");
mu_assert_streq (r_list_get_n (item->types, 2), "func.sym.mahboi.arg.0=\"int,arg0\"", "type");
mu_assert_streq (r_list_get_n (item->types, 3), "func.sym.mahboi.arg.1=\"uint32_t,die\"", "type");
mu_assert_notnull (item->hash, "hash");
mu_assert_streq (item->hash->bbhash, "7bfa1358c427e26bc03c2384f41de7be6ebc01958a57e9a6deda5bdba9768851", "hash val");
r_sign_item_free (item);
r_spaces_set (&anal->zign_spaces, "koridai");
item = r_sign_get_item (anal, "sym.boring");
mu_assert_notnull (item, "get item in space");
mu_assert_streq (item->comment, "gee it sure is boring around here", "item in space comment");
r_sign_item_free (item);
r_anal_free (anal);
mu_end;
}
int all_tests(void) {
mu_run_test (test_anal_sign_get_set);
return tests_passed != tests_run;
}
int main(int argc, char **argv) {
return all_tests ();
}