From 5bf6f11c9c335a70a89ebd19b809d66c03126c7e Mon Sep 17 00:00:00 2001 From: pancake Date: Sun, 9 Apr 2023 17:30:59 +0200 Subject: [PATCH] Add experimental zign.mangled to not use demmangled symbol names ##signatures --- libr/anal/p/anal_loongarch_gnu.c | 6 +- libr/anal/sign.c | 21 +++- libr/core/cbin.c | 6 +- libr/core/cconfig.c | 1 + libr/core/cmd_info.c | 2 +- libr/core/cmd_zign.c | 11 +- libr/include/r_anal.h | 1 + libr/include/r_bind.h | 5 +- libr/util/buf_bytes.c | 3 + test/db/cmd/cmd_zignature | 26 +++++ test/db/formats/bflt | 184 +++++++++++++++---------------- test/db/formats/coff | 5 +- test/db/formats/dex | 5 +- test/db/formats/elf/crash | 2 +- test/db/formats/elf/riscv | 5 +- test/db/formats/elf/sections | 2 +- test/db/formats/elf/symbols | 90 ++++++++------- test/db/formats/le | 5 +- test/db/formats/mach0/fatmach0 | 5 +- test/db/formats/mach0/mach0 | 5 +- test/db/formats/mangling/bin | 8 +- test/db/formats/ne | 5 +- test/db/formats/omf | 15 +-- 23 files changed, 224 insertions(+), 194 deletions(-) diff --git a/libr/anal/p/anal_loongarch_gnu.c b/libr/anal/p/anal_loongarch_gnu.c index 87126af110..c867c6942e 100644 --- a/libr/anal/p/anal_loongarch_gnu.c +++ b/libr/anal/p/anal_loongarch_gnu.c @@ -1197,7 +1197,7 @@ static void insn_memory_error_func(int status, bfd_vma memaddr, struct disassemb static int loongarch_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *b, int len, RAnalOpMask mask) { struct loongarch_ASE *ase = NULL; const struct loongarch_anal_opcode *it; - ut32 opcode, optype; + ut32 opcode; // , optype; ut32 insn_id = 0; if (!op || (len < INSNLEN)) { return INSNLEN; @@ -1209,7 +1209,7 @@ static int loongarch_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *b, int l opcode = r_read_le32 (b); /* eprintf("opcode: 0x%x \n", opcode); */ - optype = 0; + // optype = 0; for (ase = la_ases; ase->opcode; ase++) { if (!ase->opc_htab_inited) { @@ -1230,7 +1230,7 @@ static int loongarch_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *b, int l it = ase->la_opcode_ht[LA_INSN_HASH(opcode)]; /* it = ase->opcode; */ for (; it->match; it++) { - optype ++; + // optype ++; if ((opcode & it->mask) == it->match) { insn_id = it->index; op->type = it->r_type; diff --git a/libr/anal/sign.c b/libr/anal/sign.c index d76f347bf8..f3038beb0a 100644 --- a/libr/anal/sign.c +++ b/libr/anal/sign.c @@ -892,6 +892,21 @@ static char *get_unique_name(Sdb *sdb, const char *name, const RSpace *sp) { return NULL; } +static char *real_function_name(RAnal *a, ut64 addr, const char *name) { + RCore *core = a->coreb.core; + if (a->coreb.cfggeti (core, "zign.mangled")) { + // resolve the manged name + char *res = a->coreb.cmdstrf (core, "is,vaddr/eq/0x%"PFMT64x",name/cols,a/head/1,:quiet", addr); + if (res) { + r_str_trim (res); + if (*res) { + return res; + } + } + } + return strdup (name); +} + R_API int r_sign_all_functions(RAnal *a, bool merge) { RAnalFunction *fcni = NULL; RListIter *iter = NULL; @@ -904,15 +919,17 @@ R_API int r_sign_all_functions(RAnal *a, bool merge) { if (r_cons_is_breaked ()) { break; } + char *realname = real_function_name (a, fcni->addr, fcni->name); RSignItem *it = NULL; - if (merge || !name_exists (a->sdb_zigns, fcni->name, sp)) { - it = item_from_func (a, fcni, fcni->name); + if (merge || !name_exists (a->sdb_zigns, realname, sp)) { + it = item_from_func (a, fcni, realname); } else { char *name = get_unique_name (a->sdb_zigns, fcni->name, sp); if (name) { it = item_from_func (a, fcni, name); } free (name); + free (realname); } if (it) { if (prev_name) { diff --git a/libr/core/cbin.c b/libr/core/cbin.c index 10ec2a88d7..f5f2720e34 100644 --- a/libr/core/cbin.c +++ b/libr/core/cbin.c @@ -304,7 +304,7 @@ R_API bool r_core_bin_load_structs(RCore *core, const char *file) { return false; } } - if (strchr (file, '\"')) { // TODO: escape "? + if (strchr (file, '\"')) { // TODO: escape "? R_LOG_ERROR ("Invalid char found in filename"); return false; } @@ -2312,7 +2312,7 @@ static void snFini(SymName *sn) { R_FREE (sn->methflag); } -static bool isAnExport(RBinSymbol *s) { +static bool its_an_export(RBinSymbol *s) { /* workaround for some bin plugs */ if (s->is_imported) { return false; @@ -2409,7 +2409,7 @@ static int bin_symbols(RCore *r, PJ *pj, int mode, ut64 laddr, int va, ut64 at, return 0; } - bool is_arm = info && info->arch && !strncmp (info->arch, "arm", 3); + bool is_arm = info && info->arch && r_str_startswith (info->arch, "arm"); const char *lang = bin_demangle ? r_config_get (r->config, "bin.lang") : NULL; RList *symbols = r_bin_get_symbols (r->bin); diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index a7b087211f..0f6fac4c8a 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -3864,6 +3864,7 @@ R_API int r_core_config_init(RCore *core) { SETPREF ("zign.diff.bthresh", "1.0", "threshold for diffing zign bytes [0, 1] (see zc?)"); SETPREF ("zign.diff.gthresh", "1.0", "threshold for diffing zign graphs [0, 1] (see zc?)"); SETPREF ("zign.threshold", "0.0", "minimum similarity required for inclusion in zb output"); + SETBPREF ("zign.mangled", "false", "use the manged name for zignatures (EXPERIMENTAL)"); /* diff */ SETCB ("diff.sort", "addr", &cb_diff_sort, "specify function diff sorting column see (e diff.sort=?)"); diff --git a/libr/core/cmd_info.c b/libr/core/cmd_info.c index be5e1347fb..9735b8ddcb 100644 --- a/libr/core/cmd_info.c +++ b/libr/core/cmd_info.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2022 - pancake */ +/* radare - LGPL - Copyright 2009-2023 - pancake */ #include #include "../bin/format/pdb/pdb_downloader.h" diff --git a/libr/core/cmd_zign.c b/libr/core/cmd_zign.c index 0f41cc25f2..f6ec9677df 100644 --- a/libr/core/cmd_zign.c +++ b/libr/core/cmd_zign.c @@ -734,7 +734,7 @@ static bool search(RCore *core, bool rad, bool only_func) { } } print_ctx_hits (&ctx); - return ctx.count > 0? true: false; + return ctx.count > 0; } static void print_possible_matches(RList *list, bool json, RCore *core) { @@ -843,11 +843,11 @@ static bool bestmatch_fcn(RCore *core, const char *input, bool json) { } free (argv); - if (!r_config_get_i (core->config, "zign.bytes")) { + if (!r_config_get_b (core->config, "zign.bytes")) { r_sign_bytes_free (it->bytes); it->bytes = NULL; } - if (!r_config_get_i (core->config, "zign.graph")) { + if (!r_config_get_b (core->config, "zign.graph")) { r_sign_graph_free (it->graph); it->graph = NULL; } @@ -886,7 +886,7 @@ static bool bestmatch_sig(RCore *core, const char *input, bool json) { return false; } - if (r_config_get_i (core->config, "zign.bytes")) { + if (r_config_get_b (core->config, "zign.bytes")) { r_sign_addto_item (core->anal, item, fcn, R_SIGN_BYTES); RSignBytes *b = item->bytes; int minsz = r_config_get_i (core->config, "zign.minsz"); @@ -894,7 +894,7 @@ static bool bestmatch_sig(RCore *core, const char *input, bool json) { R_LOG_WARN ("Function signature is too small (%d < %d) See e zign.minsz", b->size, minsz); } } - if (r_config_get_i (core->config, "zign.graph")) { + if (r_config_get_b (core->config, "zign.graph")) { r_sign_addto_item (core->anal, item, fcn, R_SIGN_GRAPH); } @@ -947,7 +947,6 @@ static bool bestmatch(void *data, const char *input) { static bool _sig_bytediff_cb(RLevBuf *va, RLevBuf *vb, ut32 ia, ut32 ib) { RSignBytes *a = (RSignBytes *)va->buf; RSignBytes *b = (RSignBytes *)vb->buf; - if ((a->bytes[ia] & a->mask[ia]) == (b->bytes[ib] & b->mask[ib])) { return false; } diff --git a/libr/include/r_anal.h b/libr/include/r_anal.h index 825b4dcae9..ee4ebf96ab 100644 --- a/libr/include/r_anal.h +++ b/libr/include/r_anal.h @@ -268,6 +268,7 @@ typedef struct r_anal_function_meta_t { typedef struct r_anal_function_t { char *name; + // R2_590: add realname for the mangled one int bits; // ((> bits 0) (set-bits bits)) int type; const char *cc; // calling convention, should come from RAnal.constpool diff --git a/libr/include/r_bind.h b/libr/include/r_bind.h index fe1647d52c..364e63e307 100644 --- a/libr/include/r_bind.h +++ b/libr/include/r_bind.h @@ -1,11 +1,8 @@ -/* radare2 - LGPL - Copyright 2015-2021 - pancake */ +/* radare2 - LGPL - Copyright 2015-2023 - pancake */ #ifndef R2_BIND_H #define R2_BIND_H -// TODO: move riobind here too? -// TODO: move rprint here too - #ifdef __cplusplus extern "C" { #endif diff --git a/libr/util/buf_bytes.c b/libr/util/buf_bytes.c index 36cdf4e529..f06fe93541 100644 --- a/libr/util/buf_bytes.c +++ b/libr/util/buf_bytes.c @@ -74,6 +74,9 @@ static bool buf_bytes_resize(RBuffer *b, ut64 newsize) { static st64 buf_bytes_read(RBuffer *b, ut8 *buf, ut64 len) { struct buf_bytes_priv *priv = get_priv_bytes (b); + if (!priv->buf) { + return 0; + } ut64 real_len = priv->length < priv->offset? 0: R_MIN (priv->length - priv->offset, len); memmove (buf, priv->buf + priv->offset, real_len); priv->offset += real_len; diff --git a/test/db/cmd/cmd_zignature b/test/db/cmd/cmd_zignature index 3d4154287a..144c59bb31 100644 --- a/test/db/cmd/cmd_zignature +++ b/test/db/cmd/cmd_zignature @@ -1732,3 +1732,29 @@ EXPECT=<()V 3 0x0000025c 0x0000025c GLOBAL FUNC 34 LHello.method.main([Ljava/lang/String;)V 4 0x00000290 0x00000290 GLOBAL FUNC 84 LHello.method.foo(I)V diff --git a/test/db/formats/elf/crash b/test/db/formats/elf/crash index de8b7b217f..557b46a8b7 100644 --- a/test/db/formats/elf/crash +++ b/test/db/formats/elf/crash @@ -23,7 +23,7 @@ EXPECT=<::getInstance() -51 0x00001a40 0x00001a40 GLOBAL FUNC 636 getBufferSizeAndDimensions(int, int, int, int&, int&) -52 0x00001cbc 0x00001cbc GLOBAL FUNC 244 alloc_buffer(private_handle_t**, int, int, int, int) -54 0x00001db0 0x00001db0 WEAK FUNC 44 android::Singleton::hasInstance() -55 0x00001ddc 0x00001ddc WEAK FUNC 2 android::Singleton::~Singleton() -56 0x00001ddc 0x00001ddc WEAK FUNC 2 android::Singleton::~Singleton() -57 0x00001ddc 0x00001ddc WEAK FUNC 2 android::Singleton::Singleton() -58 0x00001ddc 0x00001ddc WEAK FUNC 2 android::Singleton::Singleton() -60 0x00002e38 0x00003e38 GLOBAL OBJ 24 vtable for gralloc::IonController -61 ---------- 0x0000400c GLOBAL OBJ 4 gralloc::IAllocController::sController -62 ---------- 0x00004004 GLOBAL OBJ 4 android::Singleton::sInstance -63 ---------- 0x00004008 GLOBAL OBJ 4 android::Singleton::sLock +nth paddr vaddr bind type size lib name demangled +------------------------------------------------------------------------------------------------------------------------------ +3 0x00002dd0 0x00003dd0 WEAK OBJ 36 _ZTVN7gralloc9IMemAllocE vtable for gralloc::IMemAlloc +6 0x00001210 0x00001210 GLOBAL FUNC 68 _ZN7gralloc8IonAlloc12unmap_bufferEPvji gralloc::IonAlloc::unmap_buffer(void*, unsigned int, int) +11 0x00001254 0x00001254 GLOBAL FUNC 96 _ZN7gralloc8IonAlloc11open_deviceEv gralloc::IonAlloc::open_device() +13 0x000012b4 0x000012b4 GLOBAL FUNC 240 _ZN7gralloc8IonAlloc12clean_bufferEPvjiii gralloc::IonAlloc::clean_buffer(void*, unsigned int, int, int, int) +15 0x000013a4 0x000013a4 GLOBAL FUNC 84 _ZN7gralloc8IonAlloc10map_bufferEPPvjii gralloc::IonAlloc::map_buffer(void**, unsigned int, int, int) +17 0x000013f8 0x000013f8 GLOBAL FUNC 68 _ZN7gralloc8IonAlloc11free_bufferEPvjii gralloc::IonAlloc::free_buffer(void*, unsigned int, int, int) +21 0x0000143c 0x0000143c GLOBAL FUNC 304 _ZN7gralloc8IonAlloc12alloc_bufferERNS_10alloc_dataE gralloc::IonAlloc::alloc_buffer(gralloc::alloc_data&) +22 0x0000156c 0x0000156c GLOBAL FUNC 22 _ZN7gralloc8IonAlloc12close_deviceEv gralloc::IonAlloc::close_device() +24 0x00002df8 0x00003df8 GLOBAL OBJ 36 _ZTVN7gralloc8IonAllocE vtable for gralloc::IonAlloc +26 0x00002e20 0x00003e20 WEAK OBJ 24 _ZTVN7gralloc16IAllocControllerE vtable for gralloc::IAllocController +27 0x00001614 0x00001614 GLOBAL FUNC 48 _ZN7gralloc13IonController12getAllocatorEi gralloc::IonController::getAllocator(int) +28 0x00001644 0x00001644 GLOBAL FUNC 452 _ZN7gralloc13IonController8allocateERNS_10alloc_dataEi gralloc::IonController::allocate(gralloc::alloc_data&, int) +36 0x00001808 0x00001808 GLOBAL FUNC 40 _ZN13AdrenoMemInfoC2Ev AdrenoMemInfo::AdrenoMemInfo() +39 0x00001808 0x00001808 GLOBAL FUNC 40 _ZN13AdrenoMemInfoC1Ev AdrenoMemInfo::AdrenoMemInfo() +40 0x00001830 0x00001830 GLOBAL FUNC 16 _ZN13AdrenoMemInfoD2Ev AdrenoMemInfo::~AdrenoMemInfo() +42 0x00001830 0x00001830 GLOBAL FUNC 16 _ZN13AdrenoMemInfoD1Ev AdrenoMemInfo::~AdrenoMemInfo() +43 0x00001840 0x00001840 GLOBAL FUNC 288 _ZN13AdrenoMemInfo9getStrideEii AdrenoMemInfo::getStride(int, int) +45 0x00001960 0x00001960 GLOBAL FUNC 68 _ZN7gralloc13IonControllerC2Ev gralloc::IonController::IonController() +47 0x00001960 0x00001960 GLOBAL FUNC 68 _ZN7gralloc13IonControllerC1Ev gralloc::IonController::IonController() +48 0x000019a4 0x000019a4 GLOBAL FUNC 40 _ZN7gralloc16IAllocController11getInstanceEv gralloc::IAllocController::getInstance() +49 0x000019cc 0x000019cc GLOBAL FUNC 60 _Z11free_bufferP16private_handle_t free_buffer(private_handle_t*) +50 0x00001a08 0x00001a08 WEAK FUNC 56 _ZN7android9SingletonI13AdrenoMemInfoE11getInstanceEv android::Singleton::getInstance() +51 0x00001a40 0x00001a40 GLOBAL FUNC 636 _Z26getBufferSizeAndDimensionsiiiRiS_ getBufferSizeAndDimensions(int, int, int, int&, int&) +52 0x00001cbc 0x00001cbc GLOBAL FUNC 244 _Z12alloc_bufferPP16private_handle_tiiii alloc_buffer(private_handle_t**, int, int, int, int) +54 0x00001db0 0x00001db0 WEAK FUNC 44 _ZN7android9SingletonI13AdrenoMemInfoE11hasInstanceEv android::Singleton::hasInstance() +55 0x00001ddc 0x00001ddc WEAK FUNC 2 _ZN7android9SingletonI13AdrenoMemInfoED2Ev android::Singleton::~Singleton() +56 0x00001ddc 0x00001ddc WEAK FUNC 2 _ZN7android9SingletonI13AdrenoMemInfoED1Ev android::Singleton::~Singleton() +57 0x00001ddc 0x00001ddc WEAK FUNC 2 _ZN7android9SingletonI13AdrenoMemInfoEC2Ev android::Singleton::Singleton() +58 0x00001ddc 0x00001ddc WEAK FUNC 2 _ZN7android9SingletonI13AdrenoMemInfoEC1Ev android::Singleton::Singleton() +60 0x00002e38 0x00003e38 GLOBAL OBJ 24 _ZTVN7gralloc13IonControllerE vtable for gralloc::IonController +61 ---------- 0x0000400c GLOBAL OBJ 4 _ZN7gralloc16IAllocController11sControllerE gralloc::IAllocController::sController +62 ---------- 0x00004004 GLOBAL OBJ 4 _ZN7android9SingletonI13AdrenoMemInfoE9sInstanceE android::Singleton::sInstance +63 ---------- 0x00004008 GLOBAL OBJ 4 _ZN7android9SingletonI13AdrenoMemInfoE5sLockE android::Singleton::sLock 64 ---------- 0x00004004 GLOBAL NOTYPE 0 _edata 65 ---------- 0x00004004 GLOBAL NOTYPE 0 __bss_start 66 ---------- 0x00004010 GLOBAL NOTYPE 0 _end 1 0x00001050 0x00001050 GLOBAL FUNC 16 imp.__cxa_finalize 2 0x0000105c 0x0000105c GLOBAL FUNC 16 imp.__cxa_atexit 4 0x00000000 0x00000000 GLOBAL FUNC 16 imp.__aeabi_unwind_cpp_pr0 -5 0x00001068 0x00001068 GLOBAL FUNC 16 operator delete(void*) +5 0x00001068 0x00001068 GLOBAL FUNC 16 imp._ZdlPv operator delete(void*) 7 0x00001074 0x00001074 GLOBAL FUNC 16 imp.munmap 8 0x00001080 0x00001080 GLOBAL FUNC 16 imp.__errno 9 0x0000108c 0x0000108c GLOBAL FUNC 16 imp.strerror @@ -161,7 +157,7 @@ nth paddr vaddr bind type size lib name 20 0x000010e0 0x000010e0 GLOBAL FUNC 16 imp.pthread_mutex_unlock 23 0x000010ec 0x000010ec GLOBAL FUNC 16 imp.pthread_mutex_destroy 25 0x000010f8 0x000010f8 GLOBAL FUNC 16 imp.__cxa_pure_virtual -29 0x00001104 0x00001104 GLOBAL FUNC 16 operator new(unsigned int) +29 0x00001104 0x00001104 GLOBAL FUNC 16 imp._Znwj operator new(unsigned int) 30 0x00001110 0x00001110 GLOBAL FUNC 16 imp.property_get 31 0x0000111c 0x0000111c GLOBAL FUNC 16 imp.strncmp 32 0x00001128 0x00001128 GLOBAL FUNC 16 imp.__stack_chk_fail diff --git a/test/db/formats/le b/test/db/formats/le index 71cbda8a76..a1b1fbc358 100644 --- a/test/db/formats/le +++ b/test/db/formats/le @@ -330,9 +330,8 @@ FILE=bins/le/GNUGREP.DLL CMDS=is EXPECT=<