Skip to content

Commit

Permalink
Add HtSP, HtSS, HtSU, SetS (#4415)
Browse files Browse the repository at this point in the history
- All `ht_*_new0()` are removed
- `freefn` field of HT options was renamed to `finiKV`
- `finiKV_user` fields was added to HT options
- Added `ht_*_new_opt_size` API
- `HtSP` and `HtUP` are created with `ValueFree` callback  that allows to reduce extra LOC and prevent bugs
- `SetP` replaced with `SetS` (based on `HtSP`)
- `rz_th_ht_*_new0()` and `rz_th_ht_*_new_opt()` are replaced with `rz_th_ht_*_new(HtXX *)`
  • Loading branch information
pelijah committed Apr 16, 2024
1 parent c5d2e3f commit f2d8e0d
Show file tree
Hide file tree
Showing 145 changed files with 1,308 additions and 1,300 deletions.
3 changes: 2 additions & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ AlignOperands: false
Cpp11BracedListStyle: false
ForEachMacros: ['rz_list_foreach', 'rz_list_foreach_safe', 'rz_pvector_foreach', 'rz_rbtree_foreach', 'rz_interval_tree_foreach', 'ls_foreach', 'rz_skiplist_foreach', 'graph_foreach_anode']
SortIncludes: false
RequiresClausePosition: SingleLine
RequiresClausePosition: SingleLine
TypenameMacros: ['HT_', 'Ht_', 'HtName_']
19 changes: 7 additions & 12 deletions binrz/rz-test/rz-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ typedef struct rz_test_state_t {

RzThreadCond *cond; // signaled from workers to main thread to update status
RzThreadLock *lock; // protects everything below
HtPP *path_left; // char * (path to test file) => RzTestFileCounts *
HtSP *path_left; // char * (path to test file) => RzTestFileCounts *
RzPVector /*<char *>*/ completed_paths;
ut64 ok_count;
ut64 xx_count;
Expand Down Expand Up @@ -106,11 +106,6 @@ static int help(bool verbose) {
return 1;
}

static void path_left_free_kv(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

static bool rz_test_chdir(const char *argv0) {
#if __UNIX__
if (rz_file_is_directory("db")) {
Expand Down Expand Up @@ -517,15 +512,15 @@ int rz_test_main(int argc, const char **argv) {
if (log_mode) {
// Log mode prints the state after every completed file.
// The count of tests left per file is stored in a ht.
state.path_left = ht_pp_new(NULL, path_left_free_kv, NULL);
state.path_left = ht_sp_new(HT_STR_DUP, NULL, free);
if (state.path_left) {
void **it;
rz_pvector_foreach (&state.queue, it) {
RzTest *test = *it;
RzTestFileCounts *counts = ht_pp_find(state.path_left, test->path, NULL);
RzTestFileCounts *counts = ht_sp_find(state.path_left, test->path, NULL);
if (!counts) {
counts = calloc(1, sizeof(RzTestFileCounts));
ht_pp_insert(state.path_left, test->path, counts);
ht_sp_insert(state.path_left, test->path, counts);
}
counts->tests_left++;
}
Expand Down Expand Up @@ -615,7 +610,7 @@ int rz_test_main(int argc, const char **argv) {
rz_test_test_database_free(state.db);
rz_th_lock_free(state.lock);
rz_th_cond_free(state.cond);
ht_pp_free(state.path_left);
ht_sp_free(state.path_left);
beach:
free(output_file);
free(rizin_cmd);
Expand Down Expand Up @@ -709,7 +704,7 @@ static void *worker_th(RzTestState *state) {
}
}
if (state->path_left) {
RzTestFileCounts *counts = ht_pp_find(state->path_left, test->path, NULL);
RzTestFileCounts *counts = ht_sp_find(state->path_left, test->path, NULL);
if (counts) {
switch (result->result) {
case RZ_TEST_RESULT_OK:
Expand Down Expand Up @@ -953,7 +948,7 @@ static void print_log(RzTestState *state, ut64 prev_completed, ut64 prev_paths_c
}
printf("[**] %50s ", name);
if (state->path_left) {
RzTestFileCounts *counts = ht_pp_find(state->path_left, name, NULL);
RzTestFileCounts *counts = ht_sp_find(state->path_left, name, NULL);
if (counts) {
state->ok_count += counts->ok;
state->xx_count += counts->xx;
Expand Down
15 changes: 5 additions & 10 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ static void meta_item_free(void *item) {
free(it);
}

static void global_kv_free(HtPPKv *kv) {
free(kv->key);
rz_analysis_var_global_free(kv->value);
}

RZ_API RzAnalysis *rz_analysis_new(void) {
RzAnalysis *analysis = RZ_NEW0(RzAnalysis);
if (!analysis) {
Expand All @@ -86,8 +81,8 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
return NULL;
}
analysis->bb_tree = NULL;
analysis->ht_addr_fun = ht_up_new0();
analysis->ht_name_fun = ht_pp_new0();
analysis->ht_addr_fun = ht_up_new(NULL, NULL);
analysis->ht_name_fun = ht_sp_new(HT_STR_DUP, NULL, NULL);
analysis->os = strdup(RZ_SYS_OS);
analysis->esil_goto_limit = RZ_ANALYSIS_ESIL_GOTO_LIMIT;
analysis->opt.nopskip = true; // skip nops in code analysis
Expand Down Expand Up @@ -133,7 +128,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
rz_analysis_plugin_add(analysis, plugin);
}
}
analysis->ht_global_var = ht_pp_new(NULL, global_kv_free, NULL);
analysis->ht_global_var = ht_sp_new(HT_STR_DUP, NULL, (HtSPFreeValue)rz_analysis_var_global_free);
analysis->global_var_tree = NULL;
analysis->il_vm = NULL;
analysis->hash = rz_hash_new();
Expand Down Expand Up @@ -164,7 +159,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) {
rz_analysis_il_vm_cleanup(a);
rz_list_free(a->fcns);
ht_up_free(a->ht_addr_fun);
ht_pp_free(a->ht_name_fun);
ht_sp_free(a->ht_name_fun);
set_u_free(a->visited);
rz_analysis_hint_storage_fini(a);
rz_interval_tree_fini(&a->meta);
Expand All @@ -189,7 +184,7 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) {
free(a->last_disasm_reg);
rz_list_free(a->imports);
rz_str_constpool_fini(&a->constpool);
ht_pp_free(a->ht_global_var);
ht_sp_free(a->ht_global_var);
rz_list_free(a->plugins);
rz_analysis_debug_info_free(a->debug_info);
free(a);
Expand Down
19 changes: 5 additions & 14 deletions librz/arch/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static bool is_register(const char *name, RZ_BORROW const RzRegSet *regset) {
bool found = false;
for (ut32 i = 0; i < RZ_REG_TYPE_LAST; ++i) {
if (regset[i].ht_regs) {
ht_pp_find(regset[i].ht_regs, name, &found);
ht_sp_find(regset[i].ht_regs, name, &found);
if (found) {
return true;
}
Expand Down Expand Up @@ -345,7 +345,7 @@ RZ_API void rz_asm_free(RzAsm *a) {
free(a->cpu);
free(a->features);
sdb_free(a->pair);
ht_pp_free(a->flags);
ht_ss_free(a->flags);
a->pair = NULL;
free(a);
}
Expand Down Expand Up @@ -835,15 +835,6 @@ RZ_API RzAsmCode *rz_asm_mdisassemble_hexstr(RzAsm *a, RzParse *p, const char *h
return ret;
}

static void __flag_free_kv(HtPPKv *kv) {
free(kv->key);
free(kv->value);
}

static void *__dup_val(const void *v) {
return (void *)strdup((char *)v);
}

RZ_API RzAsmCode *rz_asm_massemble(RzAsm *a, const char *assembly) {
int num, stage, ret, idx, ctr, i, linenum = 0;
char *lbuf = NULL, *ptr2, *ptr = NULL, *ptr_start = NULL;
Expand All @@ -862,8 +853,8 @@ RZ_API RzAsmCode *rz_asm_massemble(RzAsm *a, const char *assembly) {
free(tokens);
return NULL;
}
ht_pp_free(a->flags);
if (!(a->flags = ht_pp_new(__dup_val, __flag_free_kv, NULL))) {
ht_ss_free(a->flags);
if (!(a->flags = ht_ss_new(HT_STR_DUP, HT_STR_DUP))) {
free(tokens);
return NULL;
}
Expand Down Expand Up @@ -1037,7 +1028,7 @@ RZ_API RzAsmCode *rz_asm_massemble(RzAsm *a, const char *assembly) {
off += (acode->code_align - (off % acode->code_align));
}
char *food = rz_str_newf("0x%" PFMT64x, off);
ht_pp_insert(a->flags, ptr_start, food);
ht_ss_insert(a->flags, ptr_start, food);
rz_asm_code_set_equ(acode, p, food);
free(p);
free(food);
Expand Down
21 changes: 10 additions & 11 deletions librz/arch/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ RZ_API bool rz_analysis_block_recurse(RzAnalysisBlock *block, RzAnalysisBlockCb
RzAnalysisBlockRecurseContext ctx;
ctx.analysis = block->analysis;
rz_pvector_init(&ctx.to_visit, NULL);
ctx.visited = ht_up_new0();
ctx.visited = ht_up_new(NULL, NULL);
if (!ctx.visited) {
goto beach;
}
Expand Down Expand Up @@ -464,7 +464,7 @@ RZ_API bool rz_analysis_block_recurse_followthrough(RzAnalysisBlock *block, RzAn
RzAnalysisBlockRecurseContext ctx;
ctx.analysis = block->analysis;
rz_pvector_init(&ctx.to_visit, NULL);
ctx.visited = ht_up_new0();
ctx.visited = ht_up_new(NULL, NULL);
if (!ctx.visited) {
goto beach;
}
Expand Down Expand Up @@ -497,7 +497,7 @@ RZ_API bool rz_analysis_block_recurse_depth_first(RzAnalysisBlock *block, RzAnal
rz_return_val_if_fail(block && cb, true);
RzVector path;
bool breaked = false;
HtUP *visited = ht_up_new0();
HtUP *visited = ht_up_new(NULL, NULL);
rz_vector_init(&path, sizeof(RecurseDepthFirstCtx), NULL, NULL);
if (!visited) {
goto beach;
Expand Down Expand Up @@ -637,7 +637,7 @@ RZ_API RZ_NULLABLE RzList /*<RzAnalysisBlock *>*/ *rz_analysis_block_shortest_pa
ctx.next_visit = &visit_a;
RzPVector *cur_visit = &visit_b; // cur visit is the current level in the tree

ctx.visited = ht_up_new0();
ctx.visited = ht_up_new(NULL, NULL);
if (!ctx.visited) {
goto beach;
}
Expand Down Expand Up @@ -721,8 +721,7 @@ typedef struct {
bool reachable;
} NoreturnSuccessor;

static void noreturn_successor_free(HtUPKv *kv) {
NoreturnSuccessor *succ = kv->value;
static void noreturn_successor_free(NoreturnSuccessor *succ) {
rz_analysis_block_unref(succ->block);
free(succ);
}
Expand Down Expand Up @@ -776,7 +775,7 @@ RZ_API RzAnalysisBlock *rz_analysis_block_chop_noreturn(RzAnalysisBlock *block,

// Cache all recursive successors of block here.
// These are the candidates that we might have to remove from functions later.
HtUP *succs = ht_up_new(NULL, noreturn_successor_free, NULL); // maps block addr (ut64) => NoreturnSuccessor *
HtUP *succs = ht_up_new(NULL, (HtUPFreeValue)noreturn_successor_free); // maps block addr (ut64) => NoreturnSuccessor *
if (!succs) {
return block;
}
Expand Down Expand Up @@ -897,12 +896,12 @@ static bool automerge_get_predecessors_cb(void *user, const ut64 k, const void *
RZ_API void rz_analysis_block_automerge(RzPVector /*<RzAnalysisBlock *>*/ *blocks) {
rz_return_if_fail(blocks);
AutomergeCtx ctx = {
.predecessors = ht_up_new0(),
.visited_blocks = ht_up_new0(),
.blocks = ht_up_new0()
.predecessors = ht_up_new(NULL, NULL),
.visited_blocks = ht_up_new(NULL, NULL),
.blocks = ht_up_new(NULL, NULL)
};

HtUP *relevant_fcns = ht_up_new0(); // all the functions that contain some of our blocks (ht abused as a set)
HtUP *relevant_fcns = ht_up_new(NULL, NULL); // all the functions that contain some of our blocks (ht abused as a set)
RzList *fixup_candidates = rz_list_new(); // used further down
if (!ctx.predecessors || !ctx.visited_blocks || !ctx.blocks || !relevant_fcns || !fixup_candidates) {
goto beach;
Expand Down
14 changes: 7 additions & 7 deletions librz/arch/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -1284,7 +1284,7 @@ RZ_API RzGraph /*<RzGraphNodeInfo *>*/ *rz_analysis_class_get_inheritance_graph(
rz_graph_free(class_graph);
return NULL;
}
HtPP /*<char *name, RzGraphNode *node>*/ *hashmap = ht_pp_new0();
HtSP /*<char *name, RzGraphNode *node>*/ *hashmap = ht_sp_new(HT_STR_DUP, NULL, NULL);
if (!hashmap) {
rz_graph_free(class_graph);
ls_free(classes);
Expand All @@ -1296,39 +1296,39 @@ RZ_API RzGraph /*<RzGraphNodeInfo *>*/ *rz_analysis_class_get_inheritance_graph(
ls_foreach (classes, iter, kv) {
const char *name = sdbkv_key(kv);
// create nodes
RzGraphNode *curr_node = ht_pp_find(hashmap, name, NULL);
RzGraphNode *curr_node = ht_sp_find(hashmap, name, NULL);
if (!curr_node) {
curr_node = rz_graph_add_node_info(class_graph, name, NULL, 0);
if (!curr_node) {
goto failure;
}
ht_pp_insert(hashmap, name, curr_node);
ht_sp_insert(hashmap, name, curr_node);
}
// create edges between node and it's parents
RzVector *bases = rz_analysis_class_base_get_all(analysis, name);
RzAnalysisBaseClass *base;
rz_vector_foreach(bases, base) {
bool base_found = false;
RzGraphNode *base_node = ht_pp_find(hashmap, base->class_name, &base_found);
RzGraphNode *base_node = ht_sp_find(hashmap, base->class_name, &base_found);
// If base isn't processed, do it now
if (!base_found) {
base_node = rz_graph_add_node_info(class_graph, base->class_name, NULL, 0);
if (!base_node) {
goto failure;
}
ht_pp_insert(hashmap, base->class_name, base_node);
ht_sp_insert(hashmap, base->class_name, base_node);
}
rz_graph_add_edge(class_graph, base_node, curr_node);
}
rz_vector_free(bases);
}
ls_free(classes);
ht_pp_free(hashmap);
ht_sp_free(hashmap);
return class_graph;

failure:
ls_free(classes);
ht_pp_free(hashmap);
ht_sp_free(hashmap);
rz_graph_free(class_graph);
return NULL;
}
Expand Down
Loading

0 comments on commit f2d8e0d

Please sign in to comment.