Skip to content

Commit

Permalink
Merge branch 'dev' of https://github.com/rizinorg/rizin into rm--Q
Browse files Browse the repository at this point in the history
  • Loading branch information
kazarmy committed May 9, 2024
2 parents 052ca71 + 416fc1e commit ef8ee18
Show file tree
Hide file tree
Showing 42 changed files with 725 additions and 757 deletions.
47 changes: 22 additions & 25 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -712,41 +712,38 @@ RZ_API bool rz_analysis_noreturn_at(RzAnalysis *analysis, ut64 addr) {
return false;
}

static bool filter_noreturn(void *user, const SdbKv *kv) {
ut32 klen = sdbkv_key_len(kv);
ut32 vlen = sdbkv_value_len(kv);
return vlen == 4 && !strcmp(sdbkv_value(kv), "true") && klen > 9 && !strcmp(sdbkv_key(kv) + (klen - 9), ".noreturn");
}

RZ_API RzList /*<char *>*/ *rz_analysis_noreturn_functions(RzAnalysis *analysis) {
rz_return_val_if_fail(analysis, NULL);

// At first we read all noreturn functions from the Types DB
RzList *noretl = rz_type_noreturn_function_names(analysis->typedb);
// Then we propagate all noreturn functions that were inferred by
// the analysis process
SdbKv *kv;
SdbListIter *iter;
SdbList *l = sdb_foreach_list(analysis->sdb_noret, true);
ls_foreach (l, iter, kv) {
void **iter;
RzPVector *items = sdb_get_items_filter(analysis->sdb_noret, filter_noreturn, NULL, false);
rz_pvector_foreach (items, iter) {
SdbKv *kv = *iter;
const char *k = sdbkv_key(kv);
if (!strncmp(k, "func.", 5) && strstr(k, ".noreturn")) {
char *s = strdup(k + 5);
char *d = strchr(s, '.');
if (d) {
*d = 0;
}
rz_list_append(noretl, strdup(s));
free(s);
const ut32 klen = sdbkv_key_len(kv);
// strlen("func." ".noreturn") = 14
if (klen > 14 && !strncmp(k, "func.", 5)) {
rz_list_append(noretl, rz_str_ndup(k + 5, klen - 14));
}
if (!strncmp(k, "addr.", 5)) {
char *off;
if (!(off = strdup(k + 5))) {
break;
}
char *ptr = strstr(off, ".noreturn");
if (ptr) {
*ptr = 0;
char *addr = rz_str_newf("0x%s", off);
rz_list_append(noretl, addr);
}
free(off);
// strlen("addr." ".noreturn") = 14
if (RZ_BETWEEN(15, klen, 30) && !strncmp(k, "addr.", 5)) {
char addr[17];
memcpy(addr, k + 5, klen - 14);
addr[klen - 14] = '\0';
rz_list_append(noretl, rz_str_newf("0x%s", addr));
}
}
ls_free(l);
rz_pvector_free(items);
return noretl;
}

Expand Down
18 changes: 10 additions & 8 deletions librz/arch/cc.c
Original file line number Diff line number Diff line change
Expand Up @@ -265,16 +265,18 @@ RZ_API const char *rz_analysis_cc_func(RzAnalysis *analysis, const char *func_na
return cc ? cc : rz_analysis_cc_default(analysis);
}

static bool filter_cc(void *user, const SdbKv *kv) {
return sdbkv_value_len(kv) == 2 && !strcmp(sdbkv_value(kv), "cc");
}

RZ_API RzList /*<char *>*/ *rz_analysis_calling_conventions(RzAnalysis *analysis) {
RzList *ccl = rz_list_new();
SdbKv *kv;
SdbListIter *iter;
SdbList *l = sdb_foreach_list(analysis->sdb_cc, true);
ls_foreach (l, iter, kv) {
if (!strcmp(sdbkv_value(kv), "cc")) {
rz_list_append(ccl, strdup(sdbkv_key(kv)));
}
void **iter;
RzPVector *items = sdb_get_items_filter(analysis->sdb_cc, filter_cc, NULL, true);
rz_pvector_foreach (items, iter) {
SdbKv *kv = *iter;
rz_list_append(ccl, strdup(sdbkv_key(kv)));
}
ls_free(l);
rz_pvector_free(items);
return ccl;
}
36 changes: 16 additions & 20 deletions librz/arch/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ RZ_API bool rz_analysis_class_exists(RzAnalysis *analysis, const char *name) {
return r;
}

RZ_API SdbList *rz_analysis_class_get_all(RzAnalysis *analysis, bool sorted) {
return sdb_foreach_list(analysis->sdb_classes, sorted);
RZ_API RZ_OWN RzPVector /*<SdbKv *>*/ *rz_analysis_class_get_all(RzAnalysis *analysis, bool sorted) {
return sdb_get_items(analysis->sdb_classes, sorted);
}

RZ_API void rz_analysis_class_foreach(RzAnalysis *analysis, SdbForeachCallback cb, void *user) {
Expand Down Expand Up @@ -1031,14 +1031,13 @@ typedef struct {
const char *class_name;
} DeleteClassCtx;

static bool rz_analysis_class_base_delete_class_cb(void *user, const char *k, const char *v) {
(void)v;
static bool rz_analysis_class_base_delete_class_cb(void *user, const SdbKv *kv) {
DeleteClassCtx *ctx = user;
RzVector *bases = rz_analysis_class_base_get_all(ctx->analysis, k);
RzVector *bases = rz_analysis_class_base_get_all(ctx->analysis, sdbkv_key(kv));
RzAnalysisBaseClass *base;
rz_vector_foreach (bases, base) {
if (base->class_name && strcmp(base->class_name, ctx->class_name) == 0) {
rz_analysis_class_base_delete(ctx->analysis, k, base->id);
rz_analysis_class_base_delete(ctx->analysis, sdbkv_key(kv), base->id);
}
}
rz_vector_free(bases);
Expand All @@ -1056,18 +1055,17 @@ typedef struct {
const char *class_name_new;
} RenameClassCtx;

static bool rz_analysis_class_base_rename_class_cb(void *user, const char *k, const char *v) {
(void)v;
static bool rz_analysis_class_base_rename_class_cb(void *user, const SdbKv *kv) {
RenameClassCtx *ctx = user;
RzVector *bases = rz_analysis_class_base_get_all(ctx->analysis, k);
RzVector *bases = rz_analysis_class_base_get_all(ctx->analysis, sdbkv_key(kv));
RzAnalysisBaseClass *base;
rz_vector_foreach (bases, base) {
if (base->class_name && strcmp(base->class_name, ctx->class_name_old) == 0) {
rz_analysis_class_base_set_raw(ctx->analysis, k, base, ctx->class_name_new);
rz_analysis_class_base_set_raw(ctx->analysis, sdbkv_key(kv), base, ctx->class_name_new);
}
}
rz_vector_free(bases);
return 1;
return true;
}

static void rz_analysis_class_base_rename_class(RzAnalysis *analysis, const char *class_name_old, const char *class_name_new) {
Expand Down Expand Up @@ -1279,21 +1277,19 @@ RZ_API RzGraph /*<RzGraphNodeInfo *>*/ *rz_analysis_class_get_inheritance_graph(
if (!class_graph) {
return NULL;
}
SdbList *classes = rz_analysis_class_get_all(analysis, true);
RzPVector *classes = rz_analysis_class_get_all(analysis, true);
if (!classes) {
rz_graph_free(class_graph);
return NULL;
}
HtSP /*<char *name, RzGraphNode *node>*/ *hashmap = ht_sp_new(HT_STR_DUP, NULL, NULL);
if (!hashmap) {
rz_graph_free(class_graph);
ls_free(classes);
return NULL;
goto failure;
}
SdbListIter *iter;
SdbKv *kv;
void **iter;
// Traverse each class and create a node and edges
ls_foreach (classes, iter, kv) {
rz_pvector_foreach (classes, iter) {
SdbKv *kv = *iter;
const char *name = sdbkv_key(kv);
// create nodes
RzGraphNode *curr_node = ht_sp_find(hashmap, name, NULL);
Expand Down Expand Up @@ -1322,12 +1318,12 @@ RZ_API RzGraph /*<RzGraphNodeInfo *>*/ *rz_analysis_class_get_inheritance_graph(
}
rz_vector_free(bases);
}
ls_free(classes);
rz_pvector_free(classes);
ht_sp_free(hashmap);
return class_graph;

failure:
ls_free(classes);
rz_pvector_free(classes);
ht_sp_free(hashmap);
rz_graph_free(class_graph);
return NULL;
Expand Down
19 changes: 10 additions & 9 deletions librz/arch/platform_profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,15 @@ static inline bool cpu_reload_needed(RzPlatformTarget *c, const char *cpu, const

static bool sdb_load_arch_profile(RzPlatformTarget *t, Sdb *sdb) {
rz_return_val_if_fail(t && sdb, false);
SdbKv *kv;
SdbListIter *iter;

RzPlatformProfile *c = rz_platform_profile_new();
if (!c) {
return false;
}
SdbList *l = sdb_foreach_list(sdb, false);
ls_foreach (l, iter, kv) {
void **iter;
RzPVector *items = sdb_get_items(sdb, false);
rz_pvector_foreach (items, iter) {
SdbKv *kv = *iter;
if (!strcmp(sdbkv_key(kv), "PC")) {
c->pc = rz_num_math(NULL, sdbkv_value(kv));
} else if (!strcmp(sdbkv_key(kv), "EEPROM_SIZE")) {
Expand All @@ -127,21 +128,21 @@ static bool sdb_load_arch_profile(RzPlatformTarget *t, Sdb *sdb) {
c->ram_size = rz_num_math(NULL, sdbkv_value(kv));
}
if (!strcmp(sdbkv_value(kv), "io")) {
char *io_name = sdbkv_key(kv);
const char *io_name = sdbkv_key(kv);
char *argument_key = rz_str_newf("%s.address", io_name);
ut64 io_address = sdb_num_get(sdb, argument_key, NULL);
free(argument_key);
ht_up_insert(c->registers_mmio, io_address, io_name);
ht_up_insert(c->registers_mmio, io_address, (char *)io_name);
}
if (!strcmp(sdbkv_value(kv), "ext_io")) {
char *ext_io_name = sdbkv_key(kv);
const char *ext_io_name = sdbkv_key(kv);
char *argument_key = rz_str_newf("%s.address", ext_io_name);
ut64 ext_io_address = sdb_num_get(sdb, argument_key, NULL);
free(argument_key);
ht_up_insert(c->registers_extended, ext_io_address, ext_io_name);
ht_up_insert(c->registers_extended, ext_io_address, (char *)ext_io_name);
}
}
ls_free(l);
rz_pvector_free(items);
rz_platform_profile_free(t->profile);
t->profile = c;
return true;
Expand Down
16 changes: 8 additions & 8 deletions librz/arch/platform_target_index.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,17 @@ RZ_API void rz_platform_item_free(RzPlatformItem *item) {

static bool sdb_load_platform_profile(RZ_NONNULL RzPlatformTargetIndex *t, RZ_NONNULL Sdb *sdb) {
rz_return_val_if_fail(t && sdb, false);
SdbKv *kv;
SdbListIter *iter;
SdbList *l = sdb_foreach_list(sdb, false);
char *argument_key, *comment, *name;
ls_foreach (l, iter, kv) {

void **iter;
RzPVector *items = sdb_get_items(sdb, false);
rz_pvector_foreach (items, iter) {
SdbKv *kv = *iter;
if (!strcmp(sdbkv_value(kv), "name")) {
name = sdbkv_key(kv);
const char *name = sdbkv_key(kv);

RzPlatformItem *item = rz_platform_item_new(name);

argument_key = rz_str_newf("%s.address", item->name);
char *argument_key = rz_str_newf("%s.address", item->name);
if (!argument_key) {
rz_platform_item_free(item);
return false;
Expand All @@ -81,7 +81,7 @@ static bool sdb_load_platform_profile(RZ_NONNULL RzPlatformTargetIndex *t, RZ_NO
}

argument_key = rz_str_newf("%s.comment", item->name);
comment = sdb_get(sdb, argument_key, NULL);
char *comment = sdb_get(sdb, argument_key, NULL);
if (comment) {
item->comment = comment;
}
Expand Down
38 changes: 19 additions & 19 deletions librz/arch/serialize_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,10 @@ typedef struct {
RzKeyParser *parser;
} BlockLoadCtx;

static bool block_load_cb(void *user, const char *k, const char *v) {
static bool block_load_cb(void *user, const SdbKv *kv) {
BlockLoadCtx *ctx = user;

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -359,7 +359,7 @@ static bool block_load_cb(void *user, const char *k, const char *v) {
free(json_str);

errno = 0;
ut64 addr = strtoull(k, NULL, 0);
ut64 addr = strtoull(sdbkv_key(kv), NULL, 0);
if (errno || proto.size == UT64_MAX || (proto.op_pos && proto.op_pos_size != proto.ninstr - 1)) { // op_pos_size > ninstr - 1 is legal but we require the format to be like this.
goto error;
}
Expand Down Expand Up @@ -953,10 +953,10 @@ typedef struct {
RzKeyParser *parser;
} GlobalVarCtx;

static bool global_var_load_cb(void *user, const char *k, const char *v) {
static bool global_var_load_cb(void *user, const SdbKv *kv) {
GlobalVarCtx *ctx = user;

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -1182,10 +1182,10 @@ enum {
FUNCTION_FIELD_LABELS
};

static bool function_load_cb(void *user, const char *k, const char *v) {
static bool function_load_cb(void *user, const SdbKv *kv) {
RzSerializeAnalysisFunctionLoadCtx *ctx = user;

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -1338,7 +1338,7 @@ static bool function_load_cb(void *user, const char *k, const char *v) {

bool ret = true;
errno = 0;
function->addr = strtoull(k, NULL, 0);
function->addr = strtoull(sdbkv_key(kv), NULL, 0);
if (errno || !function->name || !rz_analysis_add_function(ctx->analysis, function)) {
rz_analysis_function_free(function);
ret = false;
Expand Down Expand Up @@ -1443,16 +1443,16 @@ RZ_API void rz_serialize_analysis_xrefs_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzAn
ht_up_foreach(analysis->ht_xrefs_from, store_xrefs_list_cb, db);
}

static bool xrefs_load_cb(void *user, const char *k, const char *v) {
static bool xrefs_load_cb(void *user, const SdbKv *kv) {
RzAnalysis *analysis = user;

errno = 0;
ut64 from = strtoull(k, NULL, 0);
ut64 from = strtoull(sdbkv_key(kv), NULL, 0);
if (errno) {
return false;
}

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -1606,16 +1606,16 @@ RZ_API void rz_serialize_analysis_meta_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzAna
pj_free(j);
}

static bool meta_load_cb(void *user, const char *k, const char *v) {
static bool meta_load_cb(void *user, const SdbKv *kv) {
RzAnalysis *analysis = user;

errno = 0;
ut64 addr = strtoull(k, NULL, 0);
ut64 addr = strtoull(sdbkv_key(kv), NULL, 0);
if (errno) {
return false;
}

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -1920,17 +1920,17 @@ typedef struct {
RzKeyParser *parser;
} HintsLoadCtx;

static bool hints_load_cb(void *user, const char *k, const char *v) {
static bool hints_load_cb(void *user, const SdbKv *kv) {
HintsLoadCtx *ctx = user;
RzAnalysis *analysis = ctx->analysis;

errno = 0;
ut64 addr = strtoull(k, NULL, 0);
ut64 addr = strtoull(sdbkv_key(kv), NULL, 0);
if (errno) {
return false;
}

char *json_str = strdup(v);
char *json_str = sdbkv_dup_value(kv);
if (!json_str) {
return true;
}
Expand Down Expand Up @@ -2130,8 +2130,8 @@ RZ_API void rz_serialize_analysis_imports_save(RZ_NONNULL Sdb *db, RZ_NONNULL Rz
}
}

static bool import_load_cb(void *user, const char *k, const char *v) {
rz_analysis_add_import(user, k);
static bool import_load_cb(void *user, const SdbKv *kv) {
rz_analysis_add_import(user, sdbkv_key(kv));
return true;
}

Expand Down
Loading

0 comments on commit ef8ee18

Please sign in to comment.