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

Refactor: Use HtSP for storing plugins. #4687

Merged
merged 11 commits into from
Oct 26, 2024
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ AlignConsecutiveMacros: true
AlignTrailingComments: false
AlignOperands: false
Cpp11BracedListStyle: false
ForEachMacros: ['rz_list_foreach', 'rz_list_foreach_safe', 'rz_vector_foreach', 'rz_vector_foreach_prev', 'rz_vector_enumerate', 'rz_pvector_foreach', 'rz_pvector_enumerate', 'rz_rbtree_foreach', 'rz_interval_tree_foreach', 'rz_skiplist_foreach', 'graph_foreach_anode']
ForEachMacros: ['rz_list_foreach', 'rz_list_foreach_enum', 'rz_list_foreach_safe', 'rz_vector_foreach', 'rz_vector_foreach_prev', 'rz_vector_enumerate', 'rz_pvector_foreach', 'rz_pvector_enumerate', 'rz_rbtree_foreach', 'rz_interval_tree_foreach', 'rz_skiplist_foreach', 'graph_foreach_anode']
SortIncludes: false
RequiresClausePosition: SingleLine
TypenameMacros: ['HT_', 'Ht_', 'HtName_']
TypenameMacros: ['HT_', 'Ht_', 'HtName_']
54 changes: 30 additions & 24 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <rz_analysis.h>
#include <rz_util.h>
#include <rz_list.h>
#include <rz_util/rz_assert.h>
#include <rz_util/rz_path.h>
#include <rz_arch.h>
#include <rz_lib.h>
Expand Down Expand Up @@ -117,7 +118,7 @@ RZ_API RzAnalysis *rz_analysis_new(void) {
analysis->leaddrs = NULL;
analysis->imports = rz_list_newf(free);
rz_analysis_set_bits(analysis, 32);
analysis->plugins = rz_list_new();
analysis->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL);
if (analysis->plugins) {
const size_t n_plugins = rz_arch_get_n_plugins();
for (size_t i = 0; i < n_plugins; i++) {
Expand Down Expand Up @@ -189,15 +190,17 @@ RZ_API RzAnalysis *rz_analysis_free(RzAnalysis *a) {
rz_str_constpool_fini(&a->constpool);
ht_sp_free(a->ht_global_var);
ht_up_free(a->ht_rop_semantics);
rz_list_free(a->plugins);
ht_sp_free(a->plugins);
rz_analysis_debug_info_free(a->debug_info);
free(a);
return NULL;
}

RZ_API bool rz_analysis_plugin_add(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPlugin *p) {
rz_return_val_if_fail(analysis && p, false);
RZ_PLUGIN_CHECK_AND_ADD(analysis->plugins, p, RzAnalysisPlugin);
if (!ht_sp_insert(analysis->plugins, p->name, p)) {
RZ_LOG_WARN("Plugin '%s' was already added.\n", p->name);
}
return true;
}

Expand All @@ -207,34 +210,37 @@ RZ_API bool rz_analysis_plugin_del(RzAnalysis *analysis, RZ_NONNULL RzAnalysisPl
plugin_fini(analysis);
analysis->cur = NULL;
}
return rz_list_delete_data(analysis->plugins, p);
return ht_sp_delete(analysis->plugins, p->name);
}

RZ_API bool rz_analysis_use(RzAnalysis *analysis, const char *name) {
RzListIter *it;
RzAnalysisPlugin *h;
rz_return_val_if_fail(analysis && name, false);
if (analysis->cur && !strcmp(analysis->cur->name, name)) {
return true;
}

if (analysis) {
if (analysis->cur && !strcmp(analysis->cur->name, name)) {
return true;
RzIterator *it = ht_sp_as_iter(analysis->plugins);
RzAnalysisPlugin **val;
rz_iterator_foreach(it, val) {
RzAnalysisPlugin *h = *val;
if (!h || !h->name || strcmp(h->name, name)) {
continue;
}
rz_list_foreach (analysis->plugins, it, h) {
if (!h || !h->name || strcmp(h->name, name)) {
continue;
}
plugin_fini(analysis);
analysis->cur = h;
if (h->init && !h->init(&analysis->plugin_data)) {
RZ_LOG_ERROR("analysis plugin '%s' failed to initialize.\n", h->name);
return false;
}
rz_analysis_set_reg_profile(analysis);
if (analysis->il_vm) {
rz_analysis_il_vm_setup(analysis);
}
return true;
plugin_fini(analysis);
analysis->cur = h;
if (h->init && !h->init(&analysis->plugin_data)) {
RZ_LOG_ERROR("analysis plugin '%s' failed to initialize.\n", h->name);
rz_iterator_free(it);
return false;
}
rz_analysis_set_reg_profile(analysis);
if (analysis->il_vm) {
rz_analysis_il_vm_setup(analysis);
}
rz_iterator_free(it);
return true;
}
rz_iterator_free(it);
return false;
}

Expand Down
79 changes: 50 additions & 29 deletions librz/arch/asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ RZ_API RzAsm *rz_asm_new(void) {
a->bits = RZ_SYS_BITS;
a->bitshift = 0;
a->syntax = RZ_ASM_SYNTAX_INTEL;
a->plugins = rz_list_new();
a->plugins = ht_sp_new(HT_STR_DUP, NULL, NULL);
if (!a->plugins) {
free(a);
return NULL;
Expand Down Expand Up @@ -338,7 +338,7 @@ RZ_API void rz_asm_free(RzAsm *a) {
}
plugin_fini(a);
if (a->plugins) {
rz_list_free(a->plugins);
ht_sp_free(a->plugins);
a->plugins = NULL;
}
rz_syscall_free(a->syscall);
Expand All @@ -358,7 +358,9 @@ RZ_API bool rz_asm_plugin_add(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) {
if (rz_asm_is_valid(a, p->name)) {
return false;
}
RZ_PLUGIN_CHECK_AND_ADD(a->plugins, p, RzAsmPlugin);
if (!ht_sp_insert(a->plugins, p->name, p)) {
RZ_LOG_WARN("Plugin '%s' was already added.\n", p->name);
}
return true;
}

Expand All @@ -371,37 +373,46 @@ RZ_API bool rz_asm_plugin_del(RzAsm *a, RZ_NONNULL RzAsmPlugin *p) {
if (a->acur == p) {
a->acur = NULL;
}
return rz_list_delete_data(a->plugins, p);
return ht_sp_delete(a->plugins, p->name);
}

RZ_API bool rz_asm_is_valid(RzAsm *a, const char *name) {
RzAsmPlugin *h;
RzListIter *iter;
if (!name || !*name) {
return false;
}
rz_list_foreach (a->plugins, iter, h) {

RzIterator *iter = ht_sp_as_iter(a->plugins);
RzAsmPlugin **val;
rz_iterator_foreach(iter, val) {
RzAsmPlugin *h = *val;
if (!strcmp(h->name, name)) {
rz_iterator_free(iter);
return true;
}
}
rz_iterator_free(iter);
return false;
}

RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) {
RzAsmPlugin *h;
RzListIter *iter;
if (a) {
if (name && *name) {
rz_list_foreach (a->plugins, iter, h) {
if (h->assemble && !strcmp(h->name, name)) {
a->acur = h;
return true;
}
}
}
if (!a) {
return false;
}
if (!(name && *name)) {
a->acur = NULL;
}
RzIterator *iter = ht_sp_as_iter(a->plugins);
RzAsmPlugin **val;
rz_iterator_foreach(iter, val) {
RzAsmPlugin *h = *val;
if (h->assemble && !strcmp(h->name, name)) {
a->acur = h;
rz_iterator_free(iter);
return true;
}
}
rz_iterator_free(iter);
a->acur = NULL;
return false;
}

Expand All @@ -414,7 +425,9 @@ RZ_API bool rz_asm_use_assembler(RzAsm *a, const char *name) {
static void set_plugin_configs(RZ_BORROW RzCore *core, const char *plugin_name, RZ_OWN RzConfig *pcfg) {
rz_return_if_fail(pcfg && core);
rz_config_lock(pcfg, 1);
ht_sp_insert(core->plugin_configs, plugin_name, pcfg);
if (!ht_sp_insert(core->plugin_configs, plugin_name, pcfg)) {
RZ_LOG_WARN("Plugin '%s' was already added.\n", plugin_name);
}
}

/**
Expand All @@ -437,17 +450,19 @@ static void remove_plugin_config(RZ_BORROW RzCore *core, const char *plugin_name
* \return true Put Asm plugin successfully in use.
* \return false Asm plugin failed to be enabled.
*/
RZ_API bool rz_asm_use(RzAsm *a, const char *name) {
RzAsmPlugin *h;
RzListIter *iter;
if (!a || !name) {
RZ_API bool rz_asm_use(RzAsm *a, RZ_NULLABLE const char *name) {
rz_return_val_if_fail(a, false);
if (!name) {
return false;
}
RzCore *core = a->core;
if (a->cur && !strcmp(a->cur->arch, name)) {
return true;
}
rz_list_foreach (a->plugins, iter, h) {
RzIterator *iter = ht_sp_as_iter(a->plugins);
RzAsmPlugin **val;
RzCore *core = a->core;
rz_iterator_foreach(iter, val) {
RzAsmPlugin *h = *val;
if (h->arch && h->name && !strcmp(h->name, name)) {
if (!a->cur || (a->cur && strcmp(a->cur->arch, h->arch))) {
plugin_fini(a);
Expand All @@ -463,6 +478,7 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) {
}
if (h->init && !h->init(&a->plugin_data)) {
RZ_LOG_ERROR("asm plugin '%s' failed to initialize.\n", h->name);
rz_iterator_free(iter);
return false;
}

Expand All @@ -473,9 +489,11 @@ RZ_API bool rz_asm_use(RzAsm *a, const char *name) {
set_plugin_configs(core, h->name, h->get_config(a->plugin_data));
}
a->cur = h;
rz_iterator_free(iter);
return true;
}
}
rz_iterator_free(iter);
sdb_free(a->pair);
a->pair = NULL;
return false;
Expand Down Expand Up @@ -627,12 +645,13 @@ static bool assemblerMatches(RzAsm *a, RzAsmPlugin *h) {

static Ase findAssembler(RzAsm *a, const char *kw) {
Ase ase = NULL;
RzAsmPlugin *h;
RzListIter *iter;
RzIterator *iter = ht_sp_as_iter(a->plugins);
RzAsmPlugin **val;
if (a->acur && a->acur->assemble) {
return a->acur->assemble;
}
rz_list_foreach (a->plugins, iter, h) {
rz_iterator_foreach(iter, val) {
RzAsmPlugin *h = *val;
if (assemblerMatches(a, h)) {
if (kw) {
if (strstr(h->name, kw)) {
Expand All @@ -643,6 +662,7 @@ static Ase findAssembler(RzAsm *a, const char *kw) {
}
}
}
rz_iterator_free(iter);
return ase;
}

Expand Down Expand Up @@ -1193,7 +1213,8 @@ RZ_API char *rz_asm_describe(RzAsm *a, const char *str) {
return (a && a->pair) ? sdb_get(a->pair, str) : NULL;
}

RZ_API RzList /*<RzAsmPlugin *>*/ *rz_asm_get_plugins(RzAsm *a) {
RZ_API RZ_BORROW HtSP /*<RzAsmPlugin *>*/ *rz_asm_get_plugins(RZ_BORROW RZ_NONNULL RzAsm *a) {
rz_return_val_if_fail(a, NULL);
return a->plugins;
}

Expand Down
Loading
Loading