diff --git a/libr/anal/anal.c b/libr/anal/anal.c index c02aa53453..733df64914 100644 --- a/libr/anal/anal.c +++ b/libr/anal/anal.c @@ -122,7 +122,10 @@ R_API RAnal *r_anal_new(void) { if (!anal) { return NULL; } - anal->consts = NULL; + if (!r_str_constpool_init (&anal->constpool)) { + free (anal); + return NULL; + } anal->os = strdup (R_SYS_OS); anal->reflines = NULL; anal->esil_goto_limit = R_ANAL_ESIL_GOTO_LIMIT; @@ -218,7 +221,7 @@ R_API RAnal *r_anal_free(RAnal *a) { } free (a->last_disasm_reg); r_strbuf_free (a->cmdtail); - r_str_const_free (&a->consts); + r_str_constpool_fini (&a->constpool); free (a); return NULL; } diff --git a/libr/anal/cc.c b/libr/anal/cc.c index af21d442c2..2dfdc74b2b 100644 --- a/libr/anal/cc.c +++ b/libr/anal/cc.c @@ -108,7 +108,7 @@ R_API const char *r_anal_cc_arg(RAnal *anal, const char *convention, int n) { } #endif } - return r_str_const (ret); + return r_str_constpool_get (&anal->constpool, ret ? ret : ""); } R_API int r_anal_cc_max_arg(RAnal *anal, const char *cc) { diff --git a/libr/anal/fcn.c b/libr/anal/fcn.c index fda94636c6..fbc5737ba9 100644 --- a/libr/anal/fcn.c +++ b/libr/anal/fcn.c @@ -1659,7 +1659,7 @@ R_API int r_anal_fcn_add(RAnal *a, ut64 addr, ut64 size, const char *name, int t append = true; } fcn->addr = fcn->meta.min = addr; - fcn->cc = r_str_const_at (&a->consts, r_anal_cc_default (a)); + fcn->cc = r_str_constpool_get (&a->constpool, r_anal_cc_default (a)); fcn->bits = a->bits; r_anal_fcn_set_size (append ? NULL : a, fcn, size); free (fcn->name); diff --git a/libr/anal/p/anal_java.c b/libr/anal/p/anal_java.c index eb2a055648..fef61294ae 100644 --- a/libr/anal/p/anal_java.c +++ b/libr/anal/p/anal_java.c @@ -609,7 +609,7 @@ static int java_analyze_fns_from_buffer( RAnal *anal, ut64 start, ut64 end, int ut64 length = buf_len - offset; RAnalFunction *fcn = r_anal_fcn_new (); - fcn->cc = r_str_const_at (&anal->consts, r_anal_cc_default (anal)); + fcn->cc = r_str_constpool_get (&anal->constpool, r_anal_cc_default (anal)); result = analyze_from_code_buffer ( anal, fcn, addr, buffer+offset, length ); if (result == R_ANAL_RET_ERROR) { eprintf ("Failed to parse java fn: %s @ 0x%04"PFMT64x"\n", fcn->name, fcn->addr); @@ -658,7 +658,7 @@ static int java_analyze_fns( RAnal *anal, ut64 start, ut64 end, int reftype, int (check_addr_less_start (method, end) || check_addr_in_code (method, end))) { RAnalFunction *fcn = r_anal_fcn_new (); - fcn->cc = r_str_const_at (&anal->consts, r_anal_cc_default (anal)); + fcn->cc = r_str_constpool_get (&anal->constpool, r_anal_cc_default (anal)); java_set_function_prototype (anal, fcn, method); result = analyze_from_code_attr (anal, fcn, method, loadaddr); if (result == R_ANAL_RET_ERROR) { diff --git a/libr/bin/bin.c b/libr/bin/bin.c index 863e07c563..16a26c0cdb 100644 --- a/libr/bin/bin.c +++ b/libr/bin/bin.c @@ -497,6 +497,7 @@ R_API void r_bin_free(RBin *bin) { r_list_free (bin->binldrs); sdb_free (bin->sdb); r_id_storage_free (bin->ids); + r_str_constpool_fini (&bin->constpool); free (bin); } } @@ -862,6 +863,9 @@ R_API RBin *r_bin_new() { if (!bin) { return NULL; } + if (!r_str_constpool_init (&bin->constpool)) { + goto trashbin; + } bin->force = NULL; bin->filter_rules = UT64_MAX; bin->sdb = sdb_new0 (); @@ -884,8 +888,7 @@ R_API RBin *r_bin_new() { for (i = 0; bin_xtr_static_plugins[i]; i++) { static_xtr_plugin = R_NEW0 (RBinXtrPlugin); if (!static_xtr_plugin) { - free (bin); - return NULL; + goto trashbin_binxtrs; } *static_xtr_plugin = *bin_xtr_static_plugins[i]; r_bin_xtr_add (bin, static_xtr_plugin); @@ -896,13 +899,22 @@ R_API RBin *r_bin_new() { for (i = 0; bin_ldr_static_plugins[i]; i++) { static_ldr_plugin = R_NEW0 (RBinLdrPlugin); if (!static_ldr_plugin) { - free (bin); - return NULL; + goto trashbin_binldrs; } *static_ldr_plugin = *bin_ldr_static_plugins[i]; r_bin_ldr_add (bin, static_ldr_plugin); } return bin; +trashbin_binldrs: + r_list_free (bin->binldrs); +trashbin_binxtrs: + r_list_free (bin->binxtrs); + r_list_free (bin->binfiles); + r_id_storage_free (bin->ids); + r_str_constpool_fini (&bin->constpool); +trashbin: + free(bin); + return NULL; } R_API bool r_bin_use_arch(RBin *bin, const char *arch, int bits, const char *name) { diff --git a/libr/bin/format/elf/elf.c b/libr/bin/format/elf/elf.c index c991dae0b4..9d5fb44a99 100644 --- a/libr/bin/format/elf/elf.c +++ b/libr/bin/format/elf/elf.c @@ -3348,9 +3348,9 @@ RBinSymbol *Elf_(_r_bin_elf_convert_symbol)(struct Elf_(r_bin_elf_obj_t) *bin, return NULL; } ptr->name = symbol->name[0] ? r_str_newf (namefmt, &symbol->name[0]) : strdup (""); - ptr->forwarder = r_str_const ("NONE"); - ptr->bind = r_str_const (symbol->bind); - ptr->type = r_str_const (symbol->type); + ptr->forwarder = "NONE"; + ptr->bind = symbol->bind; + ptr->type = symbol->type; ptr->paddr = paddr; ptr->vaddr = vaddr; ptr->size = symbol->size; diff --git a/libr/bin/format/mach0/mach0.c b/libr/bin/format/mach0/mach0.c index 730af1141f..ae5a29aeeb 100644 --- a/libr/bin/format/mach0/mach0.c +++ b/libr/bin/format/mach0/mach0.c @@ -2413,7 +2413,7 @@ static void fill_exports_list(struct MACH0_(obj_t) *bin, const char *name, ut64 sym->paddr = offset; sym->type = "EXT"; sym->name = strdup (name); - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; r_list_append (list, sym); } diff --git a/libr/bin/format/mdmp/mdmp_pe.c b/libr/bin/format/mdmp/mdmp_pe.c index 973e0099b5..6b4d2b708a 100644 --- a/libr/bin/format/mdmp/mdmp_pe.c +++ b/libr/bin/format/mdmp/mdmp_pe.c @@ -110,8 +110,8 @@ RList *PE_(r_bin_mdmp_pe_get_imports) (struct PE_(r_bin_mdmp_pe_bin) * pe_bin) { } filter_import (imports[i].name); ptr->name = strdup ((char *)imports[i].name); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->bind = "NONE"; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->ordinal = imports[i].ordinal; r_list_append (ret, ptr); @@ -206,7 +206,7 @@ RList *PE_(r_bin_mdmp_pe_get_sections) (struct PE_(r_bin_mdmp_pe_bin) * pe_bin) return ret; } -RList *PE_(r_bin_mdmp_pe_get_symbols) (struct PE_(r_bin_mdmp_pe_bin) * pe_bin) { +RList *PE_(r_bin_mdmp_pe_get_symbols) (RBin *rbin, struct PE_(r_bin_mdmp_pe_bin) * pe_bin) { int i; ut64 offset; struct r_bin_pe_export_t *symbols = NULL; @@ -229,9 +229,9 @@ RList *PE_(r_bin_mdmp_pe_get_symbols) (struct PE_(r_bin_mdmp_pe_bin) * pe_bin) { offset -= pe_bin->vaddr; } ptr->name = strdup ((char *)symbols[i].name); - ptr->forwarder = r_str_const ((char *)symbols[i].forwarder); - ptr->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->forwarder = r_str_constpool_get (&rbin->constpool, (char *)symbols[i].forwarder); + ptr->bind = R_BIN_BIND_GLOBAL_STR; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->size = 0; ptr->vaddr = offset + pe_bin->vaddr; ptr->paddr = symbols[i].paddr + pe_bin->paddr; @@ -252,8 +252,8 @@ RList *PE_(r_bin_mdmp_pe_get_symbols) (struct PE_(r_bin_mdmp_pe_bin) * pe_bin) { offset -= pe_bin->vaddr; } ptr->name = r_str_newf ("imp.%s", imports[i].name); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->bind = "NONE"; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->size = 0; ptr->vaddr = offset + pe_bin->vaddr; ptr->paddr = imports[i].paddr + pe_bin->paddr; diff --git a/libr/bin/format/mdmp/mdmp_pe.h b/libr/bin/format/mdmp/mdmp_pe.h index ce7c591ca6..386f26c519 100644 --- a/libr/bin/format/mdmp/mdmp_pe.h +++ b/libr/bin/format/mdmp/mdmp_pe.h @@ -22,6 +22,6 @@ struct PE_(r_bin_mdmp_pe_bin) { RList *PE_(r_bin_mdmp_pe_get_entrypoint)(struct PE_(r_bin_mdmp_pe_bin) *pe_bin); RList *PE_(r_bin_mdmp_pe_get_imports)(struct PE_(r_bin_mdmp_pe_bin) *pe_bin); RList *PE_(r_bin_mdmp_pe_get_sections)(struct PE_(r_bin_mdmp_pe_bin) *pe_bin); -RList *PE_(r_bin_mdmp_pe_get_symbols)(struct PE_(r_bin_mdmp_pe_bin) *pe_bin); +RList *PE_(r_bin_mdmp_pe_get_symbols)(RBin *rbin, struct PE_(r_bin_mdmp_pe_bin) *pe_bin); #endif /* MDMP_PE_H */ diff --git a/libr/bin/format/ne/ne.c b/libr/bin/format/ne/ne.c index b216f5be8e..45cfe4420d 100644 --- a/libr/bin/format/ne/ne.c +++ b/libr/bin/format/ne/ne.c @@ -134,7 +134,7 @@ RList *r_bin_ne_get_symbols(r_bin_ne_obj_t *bin) { } sym->name = name; if (!first) { - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; } ut16 entry_off = r_buf_read_le16_at (bin->buf, off); off += 2; @@ -159,7 +159,7 @@ RList *r_bin_ne_get_symbols(r_bin_ne_obj_t *bin) { } sym->name = r_str_newf ("entry%d", i - 1); sym->paddr = en->paddr; - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; sym->ordinal = i; r_list_append (symbols, sym); } diff --git a/libr/bin/format/nxo/nxo.c b/libr/bin/format/nxo/nxo.c index d567bf8f47..5e2075926c 100644 --- a/libr/bin/format/nxo/nxo.c +++ b/libr/bin/format/nxo/nxo.c @@ -66,8 +66,8 @@ static void walkSymbols (RBuffer *buf, RBinNXOObj *bin, ut64 symtab, ut64 strtab free (symName); break; } - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); - sym->bind = r_str_const ("NONE"); + sym->type = R_BIN_TYPE_FUNC_STR; + sym->bind = "NONE"; sym->size = size; if (addr == 0) { @@ -83,11 +83,11 @@ static void walkSymbols (RBuffer *buf, RBinNXOObj *bin, ut64 symtab, ut64 strtab if (!imp->name) { goto out_walk_symbol; } - imp->type = r_str_const ("FUNC"); + imp->type = "FUNC"; if (!imp->type) { goto out_walk_symbol; } - imp->bind = r_str_const ("NONE"); + imp->bind = "NONE"; if (!imp->bind) { goto out_walk_symbol; } diff --git a/libr/bin/format/wasm/wasm.c b/libr/bin/format/wasm/wasm.c index c042964b88..8dc5e5f2f8 100644 --- a/libr/bin/format/wasm/wasm.c +++ b/libr/bin/format/wasm/wasm.c @@ -171,19 +171,19 @@ static RList *r_bin_wasm_get_sections_by_id (RList *sections, ut8 id) { const char *r_bin_wasm_valuetype_to_string (r_bin_wasm_value_type_t type) { switch (type) { case R_BIN_WASM_VALUETYPE_i32: - return r_str_const ("i32"); + return "i32"; case R_BIN_WASM_VALUETYPE_i64: - return r_str_const ("i62"); + return "i62"; case R_BIN_WASM_VALUETYPE_f32: - return r_str_const ("f32"); + return "f32"; case R_BIN_WASM_VALUETYPE_f64: - return r_str_const ("f64"); + return "f64"; case R_BIN_WASM_VALUETYPE_ANYFUNC: - return r_str_const ("ANYFUNC"); + return "ANYFUNC"; case R_BIN_WASM_VALUETYPE_FUNC: - return r_str_const ("FUNC"); + return "FUNC"; default: - return r_str_const (""); + return ""; } } diff --git a/libr/bin/p/bin_coff.c b/libr/bin/p/bin_coff.c index 7f99ad618a..134c219016 100644 --- a/libr/bin/p/bin_coff.c +++ b/libr/bin/p/bin_coff.c @@ -41,7 +41,7 @@ static RBinAddr *binsym(RBinFile *bf, int sym) { return NULL; } -static bool _fill_bin_symbol(struct r_bin_coff_obj *bin, int idx, RBinSymbol **sym) { +static bool _fill_bin_symbol(RBin *rbin, struct r_bin_coff_obj *bin, int idx, RBinSymbol **sym) { RBinSymbol *ptr = *sym; struct coff_symbol *s = NULL; if (idx < 0 || idx > bin->hdr.f_nsyms) { @@ -57,30 +57,30 @@ static bool _fill_bin_symbol(struct r_bin_coff_obj *bin, int idx, RBinSymbol **s } ptr->name = strdup (coffname); free (coffname); - ptr->forwarder = r_str_const ("NONE"); + ptr->forwarder = "NONE"; switch (s->n_sclass) { case COFF_SYM_CLASS_FUNCTION: - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->type = R_BIN_TYPE_FUNC_STR; break; case COFF_SYM_CLASS_FILE: - ptr->type = r_str_const ("FILE"); + ptr->type = "FILE"; break; case COFF_SYM_CLASS_SECTION: - ptr->type = r_str_const (R_BIN_TYPE_SECTION_STR); + ptr->type = R_BIN_TYPE_SECTION_STR; break; case COFF_SYM_CLASS_EXTERNAL: // should be prefixed with sym.imp if (bin->symbols[idx].n_scnum) { - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->type = R_BIN_TYPE_FUNC_STR; } else { - ptr->type = r_str_const ("EXTERNAL"); + ptr->type = "EXTERNAL"; } break; case COFF_SYM_CLASS_STATIC: - ptr->type = r_str_const ("STATIC"); + ptr->type = "STATIC"; break; default: - ptr->type = r_str_const (sdb_fmt ("%i", s->n_sclass)); + ptr->type = r_str_constpool_get (&rbin->constpool, sdb_fmt ("%i", s->n_sclass)); break; } if (bin->symbols[idx].n_scnum < bin->hdr.f_nscns + 1 && @@ -110,8 +110,8 @@ static RBinImport *_fill_bin_import(struct r_bin_coff_obj *bin, int idx) { return NULL; } ptr->name = strdup (coffname); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const ("FUNC"); + ptr->bind = "NONE"; + ptr->type = "FUNC"; return ptr; } @@ -191,7 +191,7 @@ static RList *symbols(RBinFile *bf) { if (!(ptr = R_NEW0 (RBinSymbol))) { break; } - if (_fill_bin_symbol (obj, i, &ptr)) { + if (_fill_bin_symbol (bf->rbin, obj, i, &ptr)) { r_list_append (ret, ptr); } else { free (ptr); @@ -264,7 +264,7 @@ static RList *relocs(RBinFile *bf) { if (!symbol) { continue; } - if (!_fill_bin_symbol (bin, rel[j].r_symndx, &symbol)) { + if (!_fill_bin_symbol (bf->rbin, bin, rel[j].r_symndx, &symbol)) { free (symbol); continue; } diff --git a/libr/bin/p/bin_dex.c b/libr/bin/p/bin_dex.c index 9d2fa166fc..0b20848579 100644 --- a/libr/bin/p/bin_dex.c +++ b/libr/bin/p/bin_dex.c @@ -10,8 +10,6 @@ #define r_hash_adler32 __adler32 #include "../../hash/adler32.c" -#define r_str_const(x) x - // globals to kill extern struct r_bin_dbginfo_t r_bin_dbginfo_dex; static bool dexdump = false; @@ -1145,10 +1143,10 @@ static const ut8 *parse_dex_class_fields(RBinFile *bf, RBinDexClass *c, RBinClas } if (is_sfield) { sym->name = r_str_newf ("%s.sfield_%s:%s", cls->name, fieldName, type_str); - sym->type = r_str_const ("STATIC"); + sym->type = "STATIC"; } else { sym->name = r_str_newf ("%s.ifield_%s:%s", cls->name, fieldName, type_str); - sym->type = r_str_const ("FIELD"); + sym->type = "FIELD"; } sym->name = r_str_replace (sym->name, "method.", "", 0); r_str_replace_char (sym->name, ';', 0); @@ -1399,19 +1397,19 @@ static const ut8 *parse_dex_class_method(RBinFile *bf, RBinDexClass *c, RBinClas // if method has code *addr points to code // otherwise it points to the encoded method if (MC > 0) { - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); + sym->type = R_BIN_TYPE_FUNC_STR; sym->paddr = MC;// + 0x10; sym->vaddr = MC;// + 0x10; } else { - sym->type = r_str_const (R_BIN_TYPE_METH_STR); + sym->type = R_BIN_TYPE_METH_STR; sym->paddr = encoded_method_addr - bufbuf; sym->vaddr = encoded_method_addr - bufbuf; } bin->code_from = R_MIN (bin->code_from, sym->paddr); if ((MA & 1) == 1) { - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; } else { - sym->bind = r_str_const (R_BIN_BIND_LOCAL_STR); + sym->bind = R_BIN_BIND_LOCAL_STR; } sym->method_flags = get_method_flags (MA); @@ -1809,8 +1807,8 @@ static bool dex_loadcode(RBinFile *bf) { return false; } imp->name = r_str_newf ("%s.method.%s%s", class_name, method_name, signature); - imp->type = r_str_const ("FUNC"); - imp->bind = r_str_const ("NONE"); + imp->type = "FUNC"; + imp->bind = "NONE"; imp->ordinal = import_count++; r_list_append (bin->imports_list, imp); @@ -1822,8 +1820,8 @@ static bool dex_loadcode(RBinFile *bf) { return false; } sym->name = r_str_newf ("imp.%s", imp->name); - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); - sym->bind = r_str_const ("NONE"); + sym->type = R_BIN_TYPE_FUNC_STR; + sym->bind = "NONE"; //XXX so damn unsafe check buffer boundaries!!!! //XXX use r_buf API!! sym->paddr = sym->vaddr = bin->header.method_offset + (sizeof (struct dex_method_t) * i) ; diff --git a/libr/bin/p/bin_dyldcache.c b/libr/bin/p/bin_dyldcache.c index dfd05b0059..4c7f6e0e1d 100644 --- a/libr/bin/p/bin_dyldcache.c +++ b/libr/bin/p/bin_dyldcache.c @@ -1263,10 +1263,9 @@ void symbols_from_bin(RList *ret, RBinFile *bf, RDyldBinImage *bin) { } } } - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ((symbols[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? - R_BIN_BIND_LOCAL_STR: R_BIN_BIND_GLOBAL_STR); - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); + sym->forwarder = "NONE"; + sym->bind = (symbols[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? R_BIN_BIND_LOCAL_STR: R_BIN_BIND_GLOBAL_STR; + sym->type = R_BIN_TYPE_FUNC_STR; sym->paddr = symbols[i].offset + bf->o->boffset; sym->size = symbols[i].size; sym->ordinal = i; diff --git a/libr/bin/p/bin_elf.inc b/libr/bin/p/bin_elf.inc index f486a777ad..5a0d1fa54c 100644 --- a/libr/bin/p/bin_elf.inc +++ b/libr/bin/p/bin_elf.inc @@ -537,8 +537,8 @@ static RList* imports(RBinFile *bf) { break; } ptr->name = strdup (import[i].name); - ptr->bind = r_str_const (import[i].bind); - ptr->type = r_str_const (import[i].type); + ptr->bind = import[i].bind; + ptr->type = import[i].type; ptr->ordinal = import[i].ordinal; setimpord (elf, ptr->ordinal, ptr); r_list_append (ret, ptr); diff --git a/libr/bin/p/bin_mach0.c b/libr/bin/p/bin_mach0.c index 7ddfa634c9..72356014cf 100644 --- a/libr/bin/p/bin_mach0.c +++ b/libr/bin/p/bin_mach0.c @@ -227,10 +227,9 @@ static RList *symbols(RBinFile *bf) { } } } - ptr->forwarder = r_str_const ("NONE"); - ptr->bind = r_str_const ((syms[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? - R_BIN_BIND_LOCAL_STR: R_BIN_BIND_GLOBAL_STR); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->forwarder = "NONE"; + ptr->bind = (syms[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? R_BIN_BIND_LOCAL_STR: R_BIN_BIND_GLOBAL_STR; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->vaddr = syms[i].addr; ptr->paddr = syms[i].offset + obj->boffset; ptr->size = syms[i].size; @@ -302,7 +301,7 @@ static RList *symbols(RBinFile *bf) { } #endif // FEATURE_SYMLIST -static RBinImport *import_from_name(const char *orig_name, HtPP *imports_by_name) { +static RBinImport *import_from_name(RBin *rbin, const char *orig_name, HtPP *imports_by_name) { if (imports_by_name) { bool found = false; RBinImport *ptr = ht_pp_find (imports_by_name, orig_name, &found); @@ -336,8 +335,8 @@ static RBinImport *import_from_name(const char *orig_name, HtPP *imports_by_name name++; } ptr->name = strdup (name); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const (type); + ptr->bind = "NONE"; + ptr->type = r_str_constpool_get (&rbin->constpool, type); if (imports_by_name) { ht_pp_insert (imports_by_name, orig_name, ptr); @@ -366,7 +365,7 @@ static RList *imports(RBinFile *bf) { bin->has_sanitizers = false; bin->has_blocks_ext = false; for (i = 0; !imports[i].last; i++) { - if (!(ptr = import_from_name (imports[i].name, NULL))) { + if (!(ptr = import_from_name (bf->rbin, imports[i].name, NULL))) { break; } name = ptr->name; @@ -420,7 +419,7 @@ static RList *relocs(RBinFile *bf) { ptr->import = bin->imports_by_ord[reloc->ord]; } else if (reloc->name[0]) { RBinImport *imp; - if (!(imp = import_from_name ((char*) reloc->name, bin->imports_by_name))) { + if (!(imp = import_from_name (bf->rbin, (char*) reloc->name, bin->imports_by_name))) { break; } ptr->import = imp; diff --git a/libr/bin/p/bin_mdmp.c b/libr/bin/p/bin_mdmp.c index 12ac7cd89b..90e1a7c5d8 100644 --- a/libr/bin/p/bin_mdmp.c +++ b/libr/bin/p/bin_mdmp.c @@ -440,12 +440,12 @@ static RList* symbols(RBinFile *bf) { obj = (struct r_bin_mdmp_obj *)bf->o->bin_obj; r_list_foreach (obj->pe32_bins, it, pe32_bin) { - list = Pe32_r_bin_mdmp_pe_get_symbols (pe32_bin); + list = Pe32_r_bin_mdmp_pe_get_symbols (bf->rbin, pe32_bin); r_list_join (ret, list); r_list_free (list); } r_list_foreach (obj->pe64_bins, it, pe64_bin) { - list = Pe64_r_bin_mdmp_pe_get_symbols (pe64_bin); + list = Pe64_r_bin_mdmp_pe_get_symbols (bf->rbin, pe64_bin); r_list_join (ret, list); r_list_free (list); } diff --git a/libr/bin/p/bin_omf.c b/libr/bin/p/bin_omf.c index 237d1c965a..b67a7f54e7 100644 --- a/libr/bin/p/bin_omf.c +++ b/libr/bin/p/bin_omf.c @@ -119,7 +119,7 @@ static RList *symbols(RBinFile *bf) { } sym_omf = ((r_bin_omf_obj *) bf->o->bin_obj)->symbols[ct_sym++]; sym->name = strdup (sym_omf->name); - sym->forwarder = r_str_const ("NONE"); + sym->forwarder = "NONE"; sym->paddr = r_bin_omf_get_paddr_sym (bf->o->bin_obj, sym_omf); sym->vaddr = r_bin_omf_get_vaddr_sym (bf->o->bin_obj, sym_omf); sym->ordinal = ct_sym; diff --git a/libr/bin/p/bin_pe.inc b/libr/bin/p/bin_pe.inc index a4ce5e4fd6..da3ff1a918 100644 --- a/libr/bin/p/bin_pe.inc +++ b/libr/bin/p/bin_pe.inc @@ -207,10 +207,10 @@ static RList* symbols(RBinFile *bf) { break; } ptr->name = strdup ((char *)symbols[i].name); - ptr->forwarder = r_str_const ((char *)symbols[i].forwarder); + ptr->forwarder = r_str_constpool_get (&bf->rbin->constpool, (char *)symbols[i].forwarder); //strncpy (ptr->bind, "NONE", R_BIN_SIZEOF_STRINGS); - ptr->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->bind = R_BIN_BIND_GLOBAL_STR; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->size = 0; ptr->vaddr = symbols[i].vaddr; ptr->paddr = symbols[i].paddr; @@ -229,8 +229,8 @@ static RList* symbols(RBinFile *bf) { //strncpy (ptr->name, (char*)symbols[i].name, R_BIN_SIZEOF_STRINGS); ptr->name = r_str_newf ("imp.%s", imports[i].name); //strncpy (ptr->forwarder, (char*)imports[i].forwarder, R_BIN_SIZEOF_STRINGS); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->bind = "NONE"; + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->size = 0; ptr->vaddr = imports[i].vaddr; ptr->paddr = imports[i].paddr; @@ -287,8 +287,8 @@ static RList* imports(RBinFile *bf) { } filter_import (imports[i].name); ptr->name = strdup ((char*)imports[i].name); - ptr->bind = r_str_const ("NONE"); - ptr->type = r_str_const ("FUNC"); + ptr->bind = "NONE"; + ptr->type = "FUNC"; ptr->ordinal = imports[i].ordinal; // NOTE(eddyb) a PE hint is just an optional possible DLL export table // index for the import. There is no point in exposing it. diff --git a/libr/bin/p/bin_symbols.c b/libr/bin/p/bin_symbols.c index 6bc0131cc6..a187a80303 100644 --- a/libr/bin/p/bin_symbols.c +++ b/libr/bin/p/bin_symbols.c @@ -343,8 +343,8 @@ static RBinSymbol *newSymbol(RBinString *s, ut64 addr, ut64 size) { sym->paddr = addr; sym->vaddr = addr; sym->size = size; - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); - sym->bind = r_str_const ("NONE"); + sym->type = R_BIN_TYPE_FUNC_STR; + sym->bind = "NONE"; } return sym; } diff --git a/libr/bin/p/bin_wasm.c b/libr/bin/p/bin_wasm.c index da3a45acd1..b475d0cda0 100644 --- a/libr/bin/p/bin_wasm.c +++ b/libr/bin/p/bin_wasm.c @@ -152,23 +152,23 @@ static RList *symbols (RBinFile *bf) { goto bad_alloc; } ptr->name = r_str_newf ("imp.%s.%s", imp->module_str, imp->field_str); - ptr->forwarder = r_str_const ("NONE"); - ptr->bind = r_str_const ("NONE"); + ptr->forwarder = "NONE"; + ptr->bind = "NONE"; switch (imp->kind) { case R_BIN_WASM_EXTERNALKIND_Function: - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->type = R_BIN_TYPE_FUNC_STR; fcn_idx++; break; case R_BIN_WASM_EXTERNALKIND_Table: - ptr->type = r_str_const ("TABLE"); + ptr->type = "TABLE"; table_idx++; break; case R_BIN_WASM_EXTERNALKIND_Memory: - ptr->type = r_str_const ("MEMORY"); + ptr->type = "MEMORY"; mem_idx++; break; case R_BIN_WASM_EXTERNALKIND_Global: - ptr->type = r_str_const (R_BIN_BIND_GLOBAL_STR); + ptr->type = R_BIN_BIND_GLOBAL_STR; global_idx++; break; } @@ -201,11 +201,11 @@ static RList *symbols (RBinFile *bf) { ptr->name = r_str_newf ("fcn.%d", fcn_idx); } - ptr->forwarder = r_str_const ("NONE"); + ptr->forwarder = "NONE"; if (!ptr->bind) { - ptr->bind = r_str_const ("NONE"); + ptr->bind = "NONE"; } - ptr->type = r_str_const (R_BIN_TYPE_FUNC_STR); + ptr->type = R_BIN_TYPE_FUNC_STR; ptr->size = func->len; ptr->vaddr = (ut64)func->code; ptr->paddr = (ut64)func->code; @@ -252,19 +252,19 @@ static RList *imports (RBinFile *bf) { ptr->name = strdup (import->field_str); ptr->classname = strdup (import->module_str); ptr->ordinal = i; - ptr->bind = r_str_const ("NONE"); + ptr->bind = "NONE"; switch (import->kind) { case R_BIN_WASM_EXTERNALKIND_Function: - ptr->type = r_str_const ("FUNC"); + ptr->type = "FUNC"; break; case R_BIN_WASM_EXTERNALKIND_Table: - ptr->type = r_str_const ("TABLE"); + ptr->type = "TABLE"; break; case R_BIN_WASM_EXTERNALKIND_Memory: - ptr->type = r_str_const ("MEM"); + ptr->type = "MEM"; break; case R_BIN_WASM_EXTERNALKIND_Global: - ptr->type = r_str_const ("GLOBAL"); + ptr->type = "GLOBAL"; break; } r_list_append (ret, ptr); diff --git a/libr/bin/p/bin_xnu_kernelcache.c b/libr/bin/p/bin_xnu_kernelcache.c index 284016aabb..044b9247d2 100644 --- a/libr/bin/p/bin_xnu_kernelcache.c +++ b/libr/bin/p/bin_xnu_kernelcache.c @@ -807,9 +807,9 @@ static void create_initterm_syms(RKext *kext, RList *ret, int type, ut64 *pointe sym->vaddr = func_vaddr; sym->paddr = func_vaddr - kext->pa2va_exec; sym->size = 0; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("GLOBAL"); - sym->type = r_str_const ("FUNC"); + sym->forwarder = "NONE"; + sym->bind = "GLOBAL"; + sym->type = "FUNC"; r_list_append (ret, sym); } @@ -861,9 +861,9 @@ static void process_constructors(RKernelCacheObj *obj, struct MACH0_(obj_t) *mac sym->vaddr = addr64; sym->paddr = paddr64; sym->size = 0; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("GLOBAL"); - sym->type = r_str_const ("FUNC"); + sym->forwarder = "NONE"; + sym->bind = "GLOBAL"; + sym->type = "FUNC"; r_list_append (ret, sym); } @@ -1133,10 +1133,9 @@ static void symbols_from_mach0(RList *ret, struct MACH0_(obj_t) *mach0, RBinFile } } } - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ((symbols[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? - "LOCAL": "GLOBAL"); - sym->type = r_str_const ("FUNC"); + sym->forwarder = "NONE"; + sym->bind = (symbols[i].type == R_BIN_MACH0_SYMBOL_TYPE_LOCAL)? "LOCAL": "GLOBAL"; + sym->type = "FUNC"; sym->paddr = symbols[i].offset + bf->o->boffset + paddr; sym->size = symbols[i].size; sym->ordinal = ordinal + i; @@ -1247,9 +1246,9 @@ static RList *resolve_syscalls(RKernelCacheObj *obj, ut64 enosys_addr) { sym->vaddr = sysent_vaddr; sym->paddr = cursor - data_const + data_const_offset; sym->size = 0; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("GLOBAL"); - sym->type = r_str_const ("OBJECT"); + sym->forwarder = "NONE"; + sym->bind = "GLOBAL"; + sym->type = "OBJECT"; r_list_append (syscalls, sym); i = 1; @@ -1268,9 +1267,9 @@ static RList *resolve_syscalls(RKernelCacheObj *obj, ut64 enosys_addr) { sym->vaddr = addr; sym->paddr = addr; sym->size = 0; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("GLOBAL"); - sym->type = r_str_const ("FUNC"); + sym->forwarder = "NONE"; + sym->bind = "GLOBAL"; + sym->type = "FUNC"; r_list_append (syscalls, sym); r_syscall_item_free (item); @@ -1433,9 +1432,9 @@ static RList *resolve_mig_subsystem(RKernelCacheObj *obj) { sym->vaddr = routine_p; sym->paddr = sym->vaddr - text_exec_vaddr + text_exec_offset; sym->size = 0; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("GLOBAL"); - sym->type = r_str_const ("OBJECT"); + sym->forwarder = "NONE"; + sym->bind = "GLOBAL"; + sym->type = "OBJECT"; r_list_append (subsystem, sym); } @@ -1522,9 +1521,9 @@ static void symbols_from_stubs(RList *ret, HtPP *kernel_syms_by_addr, RKernelCac sym->vaddr = vaddr; sym->paddr = stubs_cursor; sym->size = 12; - sym->forwarder = r_str_const ("NONE"); - sym->bind = r_str_const ("LOCAL"); - sym->type = r_str_const ("FUNC"); + sym->forwarder = "NONE"; + sym->bind = "LOCAL"; + sym->type = "FUNC"; sym->ordinal = ordinal ++; r_list_append (ret, sym); break; @@ -1552,9 +1551,9 @@ static void symbols_from_stubs(RList *ret, HtPP *kernel_syms_by_addr, RKernelCac remote_sym->vaddr = target_addr; remote_sym->paddr = target_addr - obj->pa2va_exec; remote_sym->size = 0; - remote_sym->forwarder = r_str_const ("NONE"); - remote_sym->bind = r_str_const ("GLOBAL"); - remote_sym->type = r_str_const ("FUNC"); + remote_sym->forwarder = "NONE"; + remote_sym->bind = "GLOBAL"; + remote_sym->type = "FUNC"; remote_sym->ordinal = ordinal ++; r_list_append (ret, remote_sym); @@ -1567,9 +1566,9 @@ static void symbols_from_stubs(RList *ret, HtPP *kernel_syms_by_addr, RKernelCac local_sym->vaddr = vaddr; local_sym->paddr = stubs_cursor; local_sym->size = 12; - local_sym->forwarder = r_str_const ("NONE"); - local_sym->bind = r_str_const ("GLOBAL"); - local_sym->type = r_str_const ("FUNC"); + local_sym->forwarder = "NONE"; + local_sym->bind = "GLOBAL"; + local_sym->type = "FUNC"; local_sym->ordinal = ordinal ++; r_list_append (ret, local_sym); } diff --git a/libr/cons/canvas.c b/libr/cons/canvas.c index 033987892c..8a7e6f8af6 100644 --- a/libr/cons/canvas.c +++ b/libr/cons/canvas.c @@ -69,7 +69,7 @@ static const char *set_attr(RConsCanvas *c, const char *s) { RStrBuf tmp; r_strbuf_init (&tmp); r_strbuf_append_n (&tmp, s, slen); - c->attr = r_str_const (r_strbuf_get (&tmp)); + c->attr = r_str_constpool_get (&c->constpool, r_strbuf_get (&tmp)); r_strbuf_fini (&tmp); } return p; @@ -146,19 +146,21 @@ static bool __expandLine(RConsCanvas *c, int real_len, int utf8_len) { } R_API void r_cons_canvas_free(RConsCanvas *c) { - if (c) { - if (c->b) { - int y; - for (y = 0; y < c->h; y++) { - free (c->b[y]); - } - free (c->b); - } - free (c->bsize); - free (c->blen); - ht_up_free (c->attrs); - free (c); + if (!c) { + return; } + if (c->b) { + int y; + for (y = 0; y < c->h; y++) { + free (c->b[y]); + } + free (c->b); + } + free (c->bsize); + free (c->blen); + ht_up_free (c->attrs); + r_str_constpool_fini (&c->constpool); + free (c); } static bool attribute_delete_cb(void *user, const ut64 key, const void *value) { @@ -253,6 +255,9 @@ R_API RConsCanvas *r_cons_canvas_new(int w, int h) { c->w = w; c->h = h; c->x = c->y = 0; + if (!r_str_constpool_init (&c->constpool)) { + goto beach; + } c->attrs = ht_up_new ((HtUPDupValue)strdup, attribute_free_kv, NULL); if (!c->attrs) { goto beach; @@ -261,6 +266,7 @@ R_API RConsCanvas *r_cons_canvas_new(int w, int h) { r_cons_canvas_clear (c); return c; beach: { + r_str_constpool_fini (&c->constpool); int j; for (j = 0; j < i; j++) { free (c->b[j]); diff --git a/libr/core/blaze.c b/libr/core/blaze.c index f3238261ee..26b26d880a 100644 --- a/libr/core/blaze.c +++ b/libr/core/blaze.c @@ -216,7 +216,7 @@ static void createFunction(RCore *core, fcn_t* fcn, const char *name) { f->name = name? strdup (name): r_str_newf ("%s.%" PFMT64x, pfx, fcn->addr); f->addr = fcn->addr; f->bits = core->anal->bits; - f->cc = r_str_const_at (&core->anal->consts, r_anal_cc_default (core->anal)); + f->cc = r_str_constpool_get (&core->anal->constpool, r_anal_cc_default (core->anal)); r_anal_fcn_set_size (NULL, f, fcn->size); f->type = R_ANAL_FCN_TYPE_FCN; diff --git a/libr/core/canal.c b/libr/core/canal.c index deb6ec8625..0ec3b8ce3f 100644 --- a/libr/core/canal.c +++ b/libr/core/canal.c @@ -768,7 +768,7 @@ static int core_anal_fcn(RCore *core, ut64 at, ut64 from, int reftype, int depth eprintf ("Error: new (fcn)\n"); return false; } - fcn->cc = r_str_const_at (&core->anal->consts, r_anal_cc_default (core->anal)); + fcn->cc = r_str_constpool_get (&core->anal->constpool, r_anal_cc_default (core->anal)); hint = r_anal_hint_get (core->anal, at); if (hint && hint->bits == 16) { // expand 16bit for function diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index f425420719..109eb8a2a1 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -3290,7 +3290,7 @@ static int cmd_anal_fcn(RCore *core, const char *input) { eprintf ("afc: Unknown calling convention '%s' for '%s'\n" "See afcl for available types\n", cc, asmOs); } else { - fcn->cc = r_str_const_at (&core->anal->consts, cc); + fcn->cc = r_str_constpool_get (&core->anal->constpool, cc); } free (argument); break; diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index 2af8a461a3..1d7713efe5 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -278,7 +278,7 @@ typedef struct r_anal_function_t { /*item_list *rets; // Type of return value */ char *rets; short fmod; // static, inline or volatile? - const char *cc; // calling convention, should come from RAnal.consts + const char *cc; // calling convention, should come from RAnal.constpool char* attr; // __attribute__(()) list ut64 addr; ut64 rb_max_addr; // maximum of meta.min + _size - 1 in the subtree, for fcn interval tree @@ -728,7 +728,7 @@ typedef struct r_anal_t { bool use_ex; RList *imports; // global imports SetU *visited; - char **consts; // for r_str_const_* + RStrConstPool constpool; } RAnal; typedef struct r_anal_hint_t { diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index acb098c480..8b1c0a0c92 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -334,6 +334,7 @@ typedef struct r_bin_t { bool verbose; bool use_xtr; // use extract plugins when loading a file? bool use_ldr; // use loader plugins when loading a file? + RStrConstPool constpool; } RBin; typedef struct r_bin_xtr_metadata_t { diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 96d798d773..f716df21ae 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -16,6 +16,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -354,6 +355,7 @@ typedef struct r_cons_canvas_t { int *bsize; const char *attr; //The current attr (inserted on each write) HtUP *attrs; // all the different attributes + RStrConstPool constpool; // Pool for non-compile-time attrs int sx; // scrollx int sy; // scrolly int color; diff --git a/libr/include/r_util.h b/libr/include/r_util.h index 9443d25b87..5f65cec1e9 100644 --- a/libr/include/r_util.h +++ b/libr/include/r_util.h @@ -62,6 +62,7 @@ int gettimeofday (struct timeval* p, void* tz); #include "r_util/r_ascii_table.h" #include "r_util/r_strbuf.h" #include "r_util/r_strpool.h" +#include "r_util/r_str_constpool.h" #include "r_util/r_sys.h" #include "r_util/r_tree.h" #include "r_util/r_uleb128.h" diff --git a/libr/include/r_util/r_str.h b/libr/include/r_util/r_str.h index 5f812b5cb4..03df7ae2fa 100644 --- a/libr/include/r_util/r_str.h +++ b/libr/include/r_util/r_str.h @@ -143,10 +143,6 @@ typedef void(*str_operation)(char *c); R_API int r_str_do_until_token(str_operation op, char *str, const char tok); -R_API const char *r_str_const(const char *ptr); -R_API const char *r_str_const_at(char ***consts, const char *ptr); -R_API void r_str_const_free(char ***consts); - R_API void r_str_reverse(char *str); R_API int r_str_re_match(const char *str, const char *reg); R_API int r_str_re_replace(const char *str, const char *reg, const char *sub); diff --git a/libr/include/r_util/r_str_constpool.h b/libr/include/r_util/r_str_constpool.h new file mode 100644 index 0000000000..9b88783191 --- /dev/null +++ b/libr/include/r_util/r_str_constpool.h @@ -0,0 +1,30 @@ +#ifndef R_STR_CONSTPOOL_H +#define R_STR_CONSTPOOL_H + +#include +#include + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* + * RStrConstPool is a pool of constant strings. + * References to strings will be valid as long as the RStrConstPool is alive. + */ + +typedef struct r_str_constpool_t { + HtPP *ht; +} RStrConstPool; + +R_API bool r_str_constpool_init(RStrConstPool *pool); +R_API void r_str_constpool_fini(RStrConstPool *pool); +R_API const char *r_str_constpool_get(RStrConstPool *pool, const char *str); + +#ifdef __cplusplus +} +#endif + +#endif //R_STR_CONSTPOOL_H diff --git a/libr/main/radare2.c b/libr/main/radare2.c index bda6487435..37808a2f9e 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -1565,7 +1565,6 @@ beach: r_core_fini (&r); r_cons_set_raw (0); free (file); - r_str_const_free (NULL); r_cons_free (); LISTS_FREE (); return ret; diff --git a/libr/util/Makefile b/libr/util/Makefile index e09c04561d..1f51d42846 100644 --- a/libr/util/Makefile +++ b/libr/util/Makefile @@ -21,7 +21,7 @@ OBJS+=seven.o randomart.o zip.o debruijn.o log.o getopt.o table.o OBJS+=utf8.o utf16.o utf32.o strbuf.o lib.o name.o spaces.o signal.o syscmd.o OBJS+=diff.o bdiff.o stack.o queue.o tree.o idpool.o assert.o OBJS+=punycode.o pkcs7.o x509.o asn1.o astr.o json_indent.o skiplist.o pj.o -OBJS+=rbtree.o qrcode.o vector.o str_trim.o ascii_table.o +OBJS+=rbtree.o qrcode.o vector.o str_constpool.o str_trim.o ascii_table.o # DO NOT BUILD r_big api (not yet used and its buggy) ifeq (1,0) diff --git a/libr/util/meson.build b/libr/util/meson.build index 138e5ed684..8145927c94 100644 --- a/libr/util/meson.build +++ b/libr/util/meson.build @@ -54,6 +54,7 @@ r_util_sources = [ 'spaces.c', 'stack.c', 'str.c', + 'str_constpool.c', 'str_trim.c', 'strbuf.c', 'strpool.c', diff --git a/libr/util/str.c b/libr/util/str.c index 6a6af927a6..912701e50d 100644 --- a/libr/util/str.c +++ b/libr/util/str.c @@ -2974,60 +2974,6 @@ R_API char *r_str_repeat(const char *ch, int sz) { return r_strbuf_drain (buf); } -static char **__consts = NULL; - -R_API const char *r_str_const_at(char ***consts, const char *ptr) { - if (!consts) { - consts = &__consts; - } - int ctr = 0; - if (!ptr) { - return NULL; - } - if (*consts) { - const char *p; - while ((p = (*consts)[ctr])) { - if (ptr == p || !strcmp (ptr, p)) { - return p; - } - ctr ++; - } - char **res = realloc (*consts, (ctr + 2) * sizeof (void*)); - if (!res) { - return NULL; - } - *consts = res; - } else { - *consts = malloc (sizeof (void*) * 2); - if (!*consts) { - return NULL; - } - } - (*consts)[ctr] = strdup (ptr); - (*consts)[ctr + 1] = NULL; - return (*consts)[ctr]; -} - -R_API const char *r_str_const(const char *ptr) { - if (!ptr) { - return NULL; - } - return r_str_const_at (&__consts, ptr); -} - -R_API void r_str_const_free(char ***consts) { - int i; - if (!consts) { - consts = &__consts; - } - if (*consts) { - for (i = 0; (*consts)[i]; i++) { - free ((*consts)[i]); - } - R_FREE (*consts); - } -} - R_API char *r_str_between(const char *cmt, const char *prefix, const char *suffix) { char *c0, *c1; if (!cmt || !prefix || !suffix || !*cmt) { diff --git a/libr/util/str_constpool.c b/libr/util/str_constpool.c new file mode 100644 index 0000000000..1613f177f6 --- /dev/null +++ b/libr/util/str_constpool.c @@ -0,0 +1,20 @@ +/* radare - LGPL - Copyright 2019 thestr4ng3r */ + +#include "r_util/r_str_constpool.h" + +R_API bool r_str_constpool_init(RStrConstPool *pool) { + pool->ht = sdb_ht_new (); + return pool->ht != NULL; +} + +R_API void r_str_constpool_fini(RStrConstPool *pool) { + sdb_ht_free (pool->ht); +} + +R_API const char *r_str_constpool_get(RStrConstPool *pool, const char *str) { + if (!str) { + return NULL; + } + sdb_ht_insert (pool->ht, str, str); + return sdb_ht_find (pool->ht, str, NULL); +} diff --git a/shlr/java/class.c b/shlr/java/class.c index 990232861a..ead20fa227 100644 --- a/shlr/java/class.c +++ b/shlr/java/class.c @@ -303,8 +303,7 @@ R_API ut32 U(r_bin_java_swap_uint)(ut32 x) { static bool R_BIN_JAVA_NULL_TYPE_INITTED = false; // XXX - this is a global variable used while parsing the class file -// if multi-threaded class parsing is enabled, this variable needs to -// be guarded with a lock. +// this variable should DIE. static RBinJavaObj *R_BIN_JAVA_GLOBAL_BIN = NULL; static RBinJavaAccessFlags FIELD_ACCESS_FLAGS[] = { { "public", R_BIN_JAVA_FIELD_ACC_PUBLIC, 6 }, @@ -520,7 +519,9 @@ R_API void r_bin_java_reset_bin_info(RBinJavaObj *bin) { r_list_free (bin->attrs_list); r_list_free (bin->cp_list); r_list_free (bin->interfaces_list); + r_str_constpool_fini (&bin->constpool); memset (bin, 0, sizeof (RBinJavaObj)); + r_str_constpool_init (&bin->constpool); bin->cf2.flags_str = strdup ("unknown"); bin->cf2.this_class_name = strdup ("unknown"); bin->imports_list = r_list_newf (free); @@ -2312,6 +2313,9 @@ R_API ut64 r_bin_java_parse_methods(RBinJavaObj *bin, const ut64 offset, const u R_API int r_bin_java_new_bin(RBinJavaObj *bin, ut64 loadaddr, Sdb *kv, const ut8 *buf, ut64 len) { R_BIN_JAVA_GLOBAL_BIN = bin; + if (!r_str_constpool_init (&bin->constpool)) { + return false; + } bin->lines.count = 0; bin->loadaddr = loadaddr; r_bin_java_get_java_null_cp (); @@ -2526,24 +2530,24 @@ R_API RBinSymbol *r_bin_java_create_new_symbol_from_field(RBinJavaField *fm_type sym->name = strdup (fm_type->name); // strncpy (sym->type, fm_type->descriptor, R_BIN_SIZEOF_STRINGS); if (fm_type->type == R_BIN_JAVA_FIELD_TYPE_METHOD) { - sym->type = r_str_const (R_BIN_TYPE_FUNC_STR); + sym->type = R_BIN_TYPE_FUNC_STR; sym->paddr = r_bin_java_get_method_code_offset (fm_type); sym->vaddr = r_bin_java_get_method_code_offset (fm_type) + baddr; sym->size = r_bin_java_get_method_code_size (fm_type); } else { - sym->type = r_str_const ("FIELD"); + sym->type = "FIELD"; sym->paddr = fm_type->file_offset;// r_bin_java_get_method_code_offset (fm_type); sym->vaddr = fm_type->file_offset + baddr; sym->size = fm_type->size; } if (r_bin_java_is_fm_type_protected (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_LOCAL_STR); + sym->bind = R_BIN_BIND_LOCAL_STR; } else if (r_bin_java_is_fm_type_private (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_LOCAL_STR); + sym->bind = R_BIN_BIND_LOCAL_STR; } else if (r_bin_java_is_fm_type_protected (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; } - sym->forwarder = r_str_const ("NONE"); + sym->forwarder = "NONE"; if (fm_type->class_name) { sym->classname = strdup (fm_type->class_name); } else { @@ -2568,18 +2572,18 @@ R_API RBinSymbol *r_bin_java_create_new_symbol_from_fm_type_meta(RBinJavaField * // char *new_name = malloc (new_name_len); sym->name = r_str_newf ("meta_%s", fm_type->name); if (fm_type->type == R_BIN_JAVA_FIELD_TYPE_METHOD) { - sym->type = r_str_const ("FUNC_META"); + sym->type = "FUNC_META"; } else { - sym->type = r_str_const ("FIELD_META"); + sym->type = "FIELD_META"; } if (r_bin_java_is_fm_type_protected (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_LOCAL_STR); + sym->bind = R_BIN_BIND_LOCAL_STR; } else if (r_bin_java_is_fm_type_private (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_LOCAL_STR); + sym->bind = R_BIN_BIND_LOCAL_STR; } else if (r_bin_java_is_fm_type_protected (fm_type)) { - sym->bind = r_str_const (R_BIN_BIND_GLOBAL_STR); + sym->bind = R_BIN_BIND_GLOBAL_STR; } - sym->forwarder = r_str_const ("NONE"); + sym->forwarder = "NONE"; if (fm_type->class_name) { sym->classname = strdup (fm_type->class_name); } else { @@ -2596,7 +2600,7 @@ R_API RBinSymbol *r_bin_java_create_new_symbol_from_fm_type_meta(RBinJavaField * return sym; } -R_API RBinSymbol *r_bin_java_create_new_symbol_from_ref(RBinJavaCPTypeObj *obj, ut64 baddr) { +R_API RBinSymbol *r_bin_java_create_new_symbol_from_ref(RBinJavaObj *bin, RBinJavaCPTypeObj *obj, ut64 baddr) { RBinSymbol *sym = R_NEW0 (RBinSymbol); if (!sym) { return NULL; @@ -2609,18 +2613,18 @@ R_API RBinSymbol *r_bin_java_create_new_symbol_from_ref(RBinJavaCPTypeObj *obj, return sym; } if (sym) { - class_name = r_bin_java_get_name_from_bin_cp_list (R_BIN_JAVA_GLOBAL_BIN, + class_name = r_bin_java_get_name_from_bin_cp_list (bin, obj->info.cp_method.class_idx); - name = r_bin_java_get_name_from_bin_cp_list (R_BIN_JAVA_GLOBAL_BIN, + name = r_bin_java_get_name_from_bin_cp_list (bin, obj->info.cp_method.name_and_type_idx); - type_name = r_bin_java_get_name_from_bin_cp_list (R_BIN_JAVA_GLOBAL_BIN, + type_name = r_bin_java_get_name_from_bin_cp_list (bin, obj->info.cp_method.name_and_type_idx); if (name) { sym->name = name; name = NULL; } if (type_name) { - sym->type = r_str_const (type_name); + sym->type = r_str_constpool_get (&bin->constpool, type_name); R_FREE (type_name); } if (class_name) { @@ -2764,7 +2768,7 @@ R_API RList *r_bin_java_enum_class_methods(RBinJavaObj *bin, ut16 class_idx) { if (field->field_ref_cp_obj && 0) { if ((field && field->field_ref_cp_obj->metas->ord == class_idx)) { RBinSymbol *sym = r_bin_java_create_new_symbol_from_ref ( - field->field_ref_cp_obj, bin->loadaddr); + bin, field->field_ref_cp_obj, bin->loadaddr); if (sym) { r_list_append (methods, sym); } @@ -2919,7 +2923,7 @@ R_API RBinSymbol *r_bin_java_create_new_symbol_from_cp_idx(ut32 cp_idx, ut64 bad case R_BIN_JAVA_CP_METHODREF: case R_BIN_JAVA_CP_FIELDREF: case R_BIN_JAVA_CP_INTERFACEMETHOD_REF: - sym = r_bin_java_create_new_symbol_from_ref (obj, baddr); + sym = r_bin_java_create_new_symbol_from_ref (R_BIN_JAVA_GLOBAL_BIN, obj, baddr); break; case R_BIN_JAVA_CP_INVOKEDYNAMIC: sym = r_bin_java_create_new_symbol_from_invoke_dynamic (obj, baddr); @@ -2955,8 +2959,8 @@ R_API void r_bin_add_import(RBinJavaObj *bin, RBinJavaCPTypeObj *obj, const char descriptor = descriptor ? descriptor : strdup ("INVALID DESCRIPTOR INDEX"); imp->classname = class_name; imp->name = name; - imp->bind = r_str_const ("NONE"); - imp->type = r_str_const (type); + imp->bind = "NONE"; + imp->type = r_str_constpool_get (&bin->constpool, type); imp->descriptor = descriptor; imp->ordinal = obj->idx; r_list_append (bin->imports_list, imp); @@ -3055,7 +3059,7 @@ R_API RList *r_bin_java_get_symbols(RBinJavaObj *bin) { free (sym); break; } - sym->type = r_str_const ("import"); + sym->type = "import"; if (!sym->type) { free (sym); break; @@ -3126,6 +3130,7 @@ R_API void *r_bin_java_free(RBinJavaObj *bin) { R_BIN_JAVA_GLOBAL_BIN = NULL; } free (bin->file); + r_str_constpool_fini (&bin->constpool); free (bin); return NULL; } diff --git a/shlr/java/class.h b/shlr/java/class.h index 4e5add8b18..e81b3cceb8 100644 --- a/shlr/java/class.h +++ b/shlr/java/class.h @@ -765,6 +765,8 @@ typedef struct r_bin_java_obj_t { Sdb *kv; Sdb *AllJavaBinObjs; ut32 id; + + RStrConstPool constpool; } RBinJavaObj; R_API RList * U(r_bin_java_get_interface_names)(RBinJavaObj * bin); @@ -928,7 +930,7 @@ R_API char * r_bin_java_print_class_cp_stringify(RBinJavaCPTypeObj* obj); R_API RBinSymbol* r_bin_java_create_new_symbol_from_field_with_access_flags(RBinJavaField *fm_type); R_API RBinSymbol* r_bin_java_create_new_symbol_from_cp_idx(ut32 cp_idx, ut64 baddr); R_API RBinSymbol* r_bin_java_create_new_symbol_from_invoke_dynamic(RBinJavaCPTypeObj *obj, ut64 baddr); -R_API RBinSymbol* r_bin_java_create_new_symbol_from_ref(RBinJavaCPTypeObj *obj, ut64 baddr); +R_API RBinSymbol* r_bin_java_create_new_symbol_from_ref(RBinJavaObj *bin, RBinJavaCPTypeObj *obj, ut64 baddr); R_API RBinSymbol* r_bin_java_create_new_symbol_from_method(RBinJavaField *fm_type); R_API ut64 r_bin_java_get_method_code_offset(RBinJavaField *fm_type);