librz: do not duplicate plugins (#3313)
* librz: do not duplicate plugins Plugins structures are statically defined at the file scope and not dynamically allocated. Plugins should not allocate them either as RzLibPlugin keeps a reference to them anyway.
This commit is contained in:
parent
c362b28254
commit
e933003b5b
19 changed files with 61 additions and 47 deletions
|
|
@ -113,7 +113,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_newf(free);
|
||||
analysis->plugins = rz_list_new();
|
||||
if (analysis->plugins) {
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(analysis_static_plugins); i++) {
|
||||
rz_analysis_plugin_add(analysis, analysis_static_plugins[i]);
|
||||
|
|
|
|||
|
|
@ -270,7 +270,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_newf(free);
|
||||
a->plugins = rz_list_new();
|
||||
if (!a->plugins) {
|
||||
free(a);
|
||||
return NULL;
|
||||
|
|
|
|||
|
|
@ -426,6 +426,10 @@ RZ_API bool rz_bin_xtr_plugin_add(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin)
|
|||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_fini(RzBin *bin, RzBinXtrPlugin *p) {
|
||||
return !p->fini || p->fini(bin->user);
|
||||
}
|
||||
|
||||
RZ_API bool rz_bin_xtr_plugin_del(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin) {
|
||||
rz_return_val_if_fail(bin && plugin, false);
|
||||
|
||||
|
|
@ -437,7 +441,7 @@ RZ_API bool rz_bin_xtr_plugin_del(RzBin *bin, RZ_NONNULL RzBinXtrPlugin *plugin)
|
|||
rz_bin_file_delete(bin, bf);
|
||||
}
|
||||
}
|
||||
if (plugin->fini && !plugin->fini(bin->user)) {
|
||||
if (!plugin_fini(bin, plugin)) {
|
||||
return false;
|
||||
}
|
||||
return rz_list_delete_data(bin->binxtrs, plugin);
|
||||
|
|
@ -453,6 +457,12 @@ RZ_API void rz_bin_free(RzBin *bin) {
|
|||
free(bin->strenc);
|
||||
// rz_bin_free_bin_files (bin);
|
||||
rz_list_free(bin->binfiles);
|
||||
|
||||
RzListIter *it, *tmp;
|
||||
RzBinXtrPlugin *p;
|
||||
rz_list_foreach_safe (bin->binxtrs, it, tmp, p) {
|
||||
plugin_fini(bin, p);
|
||||
}
|
||||
rz_list_free(bin->binxtrs);
|
||||
rz_list_free(bin->plugins);
|
||||
sdb_free(bin->sdb);
|
||||
|
|
@ -742,7 +752,7 @@ RZ_API RzBin *rz_bin_new(void) {
|
|||
bin->filter_rules = UT64_MAX;
|
||||
bin->sdb = sdb_new0();
|
||||
bin->cb_printf = (PrintfCallback)printf;
|
||||
bin->plugins = rz_list_newf(free);
|
||||
bin->plugins = rz_list_new();
|
||||
bin->minstrlen = 0;
|
||||
bin->strpurge = NULL;
|
||||
bin->strenc = NULL;
|
||||
|
|
@ -761,7 +771,7 @@ RZ_API RzBin *rz_bin_new(void) {
|
|||
rz_bin_plugin_add(bin, bin_static_plugins[i]);
|
||||
}
|
||||
/* extractors */
|
||||
bin->binxtrs = rz_list_newf(free);
|
||||
bin->binxtrs = rz_list_new();
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(bin_xtr_static_plugins); i++) {
|
||||
static_xtr_plugin = RZ_NEW0(RzBinXtrPlugin);
|
||||
if (!static_xtr_plugin) {
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ RZ_API RzBreakpoint *rz_bp_new(RZ_BORROW RZ_NONNULL RzBreakpointContext *ctx) {
|
|||
bp->traces = rz_bp_traptrace_new();
|
||||
bp->cb_printf = (PrintfCallback)printf;
|
||||
bp->bps = rz_list_newf((RzListFree)rz_bp_item_free);
|
||||
bp->plugins = rz_list_newf(free);
|
||||
bp->plugins = rz_list_new();
|
||||
bp->nhwbps = 0;
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(bp_static_plugins); i++) {
|
||||
rz_bp_plugin_add(bp, bp_static_plugins[i]);
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ RZ_API bool rz_core_plugin_del(RzCore *core, RZ_NONNULL RzCorePlugin *plugin) {
|
|||
|
||||
RZ_API bool rz_core_plugin_init(RzCore *core) {
|
||||
int i;
|
||||
core->plugins = rz_list_newf(free);
|
||||
core->plugins = rz_list_new();
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(core_static_plugins); i++) {
|
||||
if (!rz_core_plugin_add(core, core_static_plugins[i])) {
|
||||
RZ_LOG_ERROR("core: error loading core plugin\n");
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ RZ_API RzCrypto *rz_crypto_new(void) {
|
|||
goto rz_crypto_new_bad;
|
||||
}
|
||||
|
||||
cry->plugins = rz_list_newf(free);
|
||||
cry->plugins = rz_list_new();
|
||||
if (!cry->plugins) {
|
||||
goto rz_crypto_new_bad;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ static RzDebugPlugin *debug_static_plugins[] = { RZ_DEBUG_STATIC_PLUGINS };
|
|||
|
||||
RZ_API void rz_debug_plugin_init(RzDebug *dbg) {
|
||||
int i;
|
||||
dbg->plugins = rz_list_newf(free);
|
||||
dbg->plugins = rz_list_new();
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(debug_static_plugins); i++) {
|
||||
rz_debug_plugin_add(dbg, debug_static_plugins[i]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -166,7 +166,7 @@ RZ_API bool rz_demangler_plugin_add(RZ_NONNULL RzDemangler *dem, RZ_NONNULL RzDe
|
|||
}
|
||||
}
|
||||
|
||||
RZ_PLUGIN_ADD(dem->plugins, plugin, RzDemanglerPlugin);
|
||||
rz_list_append(dem->plugins, plugin);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ RZ_API RzEgg *rz_egg_new(void) {
|
|||
if (!egg->patches) {
|
||||
goto beach;
|
||||
}
|
||||
egg->plugins = rz_list_newf(free);
|
||||
egg->plugins = rz_list_new();
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(egg_static_plugins); i++) {
|
||||
rz_egg_plugin_add(egg, egg_static_plugins[i]);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ typedef struct hash_cfg_config_t {
|
|||
const RzHashPlugin *plugin;
|
||||
} HashCfgConfig;
|
||||
|
||||
const static RzHashPlugin *hash_static_plugins[] = { RZ_HASH_STATIC_PLUGINS };
|
||||
static RzHashPlugin *hash_static_plugins[] = { RZ_HASH_STATIC_PLUGINS };
|
||||
|
||||
/**
|
||||
* \brief Calculates the ssdeep digest of the given input
|
||||
|
|
@ -659,7 +659,7 @@ RZ_API RzHash *rz_hash_new(void) {
|
|||
if (!rh) {
|
||||
return NULL;
|
||||
}
|
||||
rh->plugins = rz_list_newf(free);
|
||||
rh->plugins = rz_list_new();
|
||||
for (int i = 0; i < RZ_ARRAY_SIZE(hash_static_plugins); i++) {
|
||||
rz_hash_plugin_add(rh, hash_static_plugins[i]);
|
||||
}
|
||||
|
|
@ -678,7 +678,7 @@ RZ_API void rz_hash_free(RzHash *rh) {
|
|||
* \brief Add a new plugin to \p rh so that \p RzHashCfg can be created using
|
||||
* specific algorithms.
|
||||
*/
|
||||
RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN const RzHashPlugin *plugin) {
|
||||
RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin) {
|
||||
rz_return_val_if_fail(rh && plugin && plugin->name, false);
|
||||
RZ_PLUGIN_CHECK_AND_ADD(rh->plugins, plugin, RzHashPlugin);
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -52,7 +52,7 @@ typedef struct rz_hash_cfg_t {
|
|||
|
||||
RZ_API RzHash *rz_hash_new(void);
|
||||
RZ_API void rz_hash_free(RzHash *rh);
|
||||
RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN const RzHashPlugin *plugin);
|
||||
RZ_API bool rz_hash_plugin_add(RZ_NONNULL RzHash *rh, RZ_NONNULL RZ_OWN RzHashPlugin *plugin);
|
||||
RZ_API bool rz_hash_plugin_del(RZ_NONNULL RzHash *rh, RZ_NONNULL RzHashPlugin *plugin);
|
||||
RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_index(RZ_NONNULL RzHash *rh, size_t index);
|
||||
RZ_API RZ_BORROW const RzHashPlugin *rz_hash_plugin_by_name(RZ_NONNULL RzHash *rh, RZ_NONNULL const char *name);
|
||||
|
|
|
|||
|
|
@ -60,7 +60,6 @@ RZ_API int rz_lang_run_file(RzLang *lang, const char *file);
|
|||
RZ_API void rz_lang_set_user_ptr(RzLang *lang, void *user);
|
||||
RZ_API bool rz_lang_set_argv(RzLang *lang, int argc, char **argv);
|
||||
RZ_API int rz_lang_prompt(RzLang *lang);
|
||||
RZ_API void rz_lang_plugin_free(RzLangPlugin *p); // XXX
|
||||
RZ_API RzLangPlugin *rz_lang_get_by_name(RzLang *lang, const char *name);
|
||||
RZ_API RzLangPlugin *rz_lang_get_by_extension(RzLang *lang, const char *ext);
|
||||
|
||||
|
|
|
|||
|
|
@ -105,16 +105,6 @@ typedef struct rz_lib_t {
|
|||
HtPU *opened_dirs; ///< Hashtable to keep track of already opened directories
|
||||
} RzLib;
|
||||
|
||||
#define RZ_PLUGIN_ADD(plugins, plugin, py_type) \
|
||||
do { \
|
||||
py_type *_dup = RZ_NEW(py_type); \
|
||||
if (!_dup) { \
|
||||
return false; \
|
||||
} \
|
||||
memcpy(_dup, (plugin), sizeof(py_type)); \
|
||||
rz_list_append(plugins, _dup); \
|
||||
} while (0)
|
||||
|
||||
#define RZ_PLUGIN_CHECK_AND_ADD(plugins, plugin, py_type) \
|
||||
do { \
|
||||
RzListIter *_it; \
|
||||
|
|
@ -124,7 +114,7 @@ typedef struct rz_lib_t {
|
|||
return false; \
|
||||
} \
|
||||
} \
|
||||
RZ_PLUGIN_ADD(plugins, plugin, py_type); \
|
||||
rz_list_append(plugins, plugin); \
|
||||
} while (0)
|
||||
|
||||
#ifdef RZ_API
|
||||
|
|
|
|||
|
|
@ -82,7 +82,7 @@ RZ_API void rz_list_init(RZ_NONNULL RzList *list);
|
|||
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter);
|
||||
RZ_API bool rz_list_delete_data(RZ_NONNULL RzList *list, void *ptr);
|
||||
RZ_API void rz_list_purge(RZ_NONNULL RzList *list);
|
||||
RZ_API void rz_list_free(RZ_NONNULL RzList *list);
|
||||
RZ_API void rz_list_free(RzList *list);
|
||||
RZ_API RZ_OWN RzListIter *rz_list_item_new(void *data);
|
||||
RZ_API void rz_list_split(RZ_NONNULL RzList *list, void *ptr);
|
||||
RZ_API void rz_list_split_iter(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter);
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ RZ_API bool rz_io_plugin_init(RzIO *io) {
|
|||
if (!io) {
|
||||
return false;
|
||||
}
|
||||
io->plugins = rz_list_newf(free);
|
||||
io->plugins = rz_list_new();
|
||||
for (i = 0; i < RZ_ARRAY_SIZE(io_static_plugins); i++) {
|
||||
if (!io_static_plugins[i]->name) {
|
||||
continue;
|
||||
|
|
|
|||
|
|
@ -10,12 +10,12 @@
|
|||
RZ_LIB_VERSION(rz_lang);
|
||||
|
||||
static RzLangPlugin *lang_static_plugins[] = { RZ_LANG_STATIC_PLUGINS };
|
||||
static RzLang *__lang = NULL;
|
||||
|
||||
RZ_API void rz_lang_plugin_free(RzLangPlugin *p) {
|
||||
if (p && p->fini) {
|
||||
p->fini(__lang);
|
||||
static bool plugin_fini(RzLang *lang, RzLangPlugin *plugin) {
|
||||
if (plugin->fini) {
|
||||
return plugin->fini(lang);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RZ_API RzLang *rz_lang_new(void) {
|
||||
|
|
@ -29,7 +29,6 @@ RZ_API RzLang *rz_lang_new(void) {
|
|||
rz_lang_free(lang);
|
||||
return NULL;
|
||||
}
|
||||
lang->langs->free = (RzListFree)rz_lang_plugin_free;
|
||||
lang->defs = rz_list_new();
|
||||
if (!lang->defs) {
|
||||
rz_lang_free(lang);
|
||||
|
|
@ -45,14 +44,20 @@ RZ_API RzLang *rz_lang_new(void) {
|
|||
}
|
||||
|
||||
RZ_API void rz_lang_free(RzLang *lang) {
|
||||
if (lang) {
|
||||
__lang = NULL;
|
||||
rz_lang_undef(lang, NULL);
|
||||
rz_list_free(lang->langs);
|
||||
rz_list_free(lang->defs);
|
||||
// TODO: remove langs plugins
|
||||
free(lang);
|
||||
if (!lang) {
|
||||
return;
|
||||
}
|
||||
RzListIter *it;
|
||||
RzLangPlugin *p;
|
||||
|
||||
rz_list_foreach (lang->langs, it, p) {
|
||||
plugin_fini(lang, p);
|
||||
}
|
||||
|
||||
rz_lang_undef(lang, NULL);
|
||||
rz_list_free(lang->langs);
|
||||
rz_list_free(lang->defs);
|
||||
free(lang);
|
||||
}
|
||||
|
||||
// XXX: This is only used actually to pass 'core' structure
|
||||
|
|
@ -118,7 +123,7 @@ RZ_API bool rz_lang_plugin_add(RzLang *lang, RZ_NONNULL RzLangPlugin *plugin) {
|
|||
if (rz_lang_get_by_name(lang, plugin->name)) {
|
||||
return false;
|
||||
}
|
||||
RZ_PLUGIN_ADD(lang->langs, plugin, RzLangPlugin);
|
||||
rz_list_append(lang->langs, plugin);
|
||||
if (plugin->init) {
|
||||
plugin->init(lang);
|
||||
}
|
||||
|
|
@ -127,7 +132,7 @@ RZ_API bool rz_lang_plugin_add(RzLang *lang, RZ_NONNULL RzLangPlugin *plugin) {
|
|||
|
||||
RZ_API bool rz_lang_plugin_del(RzLang *lang, RZ_NONNULL RzLangPlugin *plugin) {
|
||||
rz_return_val_if_fail(lang && plugin, false);
|
||||
if (plugin->fini && !plugin->fini(lang)) {
|
||||
if (!plugin_fini(lang, plugin)) {
|
||||
return false;
|
||||
}
|
||||
return rz_list_delete_data(lang->langs, plugin);
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ RZ_API RzParse *rz_parse_new(void) {
|
|||
if (!p) {
|
||||
return NULL;
|
||||
}
|
||||
p->parsers = rz_list_newf(NULL); // memleak
|
||||
p->parsers = rz_list_new();
|
||||
if (!p->parsers) {
|
||||
rz_parse_free(p);
|
||||
return NULL;
|
||||
|
|
@ -39,6 +39,16 @@ RZ_API RzParse *rz_parse_new(void) {
|
|||
}
|
||||
|
||||
RZ_API void rz_parse_free(RzParse *p) {
|
||||
if (!p) {
|
||||
return;
|
||||
}
|
||||
RzListIter *it, *tmp;
|
||||
RzParsePlugin *plugin;
|
||||
rz_list_foreach_safe (p->parsers, it, tmp, plugin) {
|
||||
if (plugin->fini) {
|
||||
plugin->fini(p, p->user);
|
||||
}
|
||||
}
|
||||
rz_list_free(p->parsers);
|
||||
free(p);
|
||||
}
|
||||
|
|
@ -51,7 +61,7 @@ RZ_API bool rz_parse_plugin_add(RzParse *p, RZ_NONNULL RzParsePlugin *plugin) {
|
|||
itsFine = plugin->init(p, p->user);
|
||||
}
|
||||
if (itsFine) {
|
||||
RZ_PLUGIN_ADD(p->parsers, plugin, RzParsePlugin);
|
||||
rz_list_append(p->parsers, plugin);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ RZ_API RzLib *rz_lib_new(RZ_NULLABLE const char *symname, RZ_NULLABLE const char
|
|||
return NULL;
|
||||
}
|
||||
lib->handlers = rz_list_newf(free);
|
||||
lib->plugins = rz_list_newf(free);
|
||||
lib->plugins = rz_list_new();
|
||||
lib->symname = strdup(symname ? symname : RZ_LIB_SYMNAME);
|
||||
lib->symnamefunc = strdup(symnamefunc ? symnamefunc : RZ_LIB_SYMFUNC);
|
||||
lib->opened_dirs = ht_pu_new0();
|
||||
|
|
|
|||
|
|
@ -126,7 +126,7 @@ RZ_API void rz_list_purge(RZ_NONNULL RzList *list) {
|
|||
* \brief Empties the list and frees the list pointer
|
||||
*
|
||||
**/
|
||||
RZ_API void rz_list_free(RZ_NONNULL RzList *list) {
|
||||
RZ_API void rz_list_free(RzList *list) {
|
||||
if (list) {
|
||||
rz_list_purge(list);
|
||||
free(list);
|
||||
|
|
|
|||
Loading…
Reference in a new issue