diff --git a/libr/anal/p/anal_arm_cs.c b/libr/anal/p/anal_arm_cs.c index 5bdd04452d..01d1de5138 100644 --- a/libr/anal/p/anal_arm_cs.c +++ b/libr/anal/p/anal_arm_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2013-2021 - pancake */ +/* radare2 - LGPL - Copyright 2013-2022 - pancake */ #include #include @@ -62,8 +62,8 @@ #define ISPREINDEX64() (((OPCOUNT64() == 2) && (ISMEM64(1)) && (ISWRITEBACK64())) || ((OPCOUNT64() == 3) && (ISMEM64(2)) && (ISWRITEBACK64()))) #define ISPOSTINDEX64() (((OPCOUNT64() == 3) && (ISIMM64(2)) && (ISWRITEBACK64())) || ((OPCOUNT64() == 4) && (ISIMM64(3)) && (ISWRITEBACK64()))) -static HtUU *ht_itblock = NULL; -static HtUU *ht_it = NULL; +static R_TH_LOCAL HtUU *ht_itblock = NULL; +static R_TH_LOCAL HtUU *ht_it = NULL; #define BITMASK_BY_WIDTH_COUNT 64 static const ut64 bitmask_by_width[BITMASK_BY_WIDTH_COUNT] = { diff --git a/libr/anal/p/anal_snes.c b/libr/anal/p/anal_snes.c index 21e50914a5..80a34aae7e 100644 --- a/libr/anal/p/anal_snes.c +++ b/libr/anal/p/anal_snes.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2015 - condret */ +/* radare - LGPL - Copyright 2015-2022 - condret */ #include #include @@ -8,7 +8,7 @@ #include "../../asm/arch/snes/snes_op_table.h" #include "../../asm/p/asm_snes.h" -static struct snes_asm_flags* snesflags = NULL; +static R_TH_LOCAL struct snes_asm_flags* snesflags = NULL; static int snes_anop(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *data, int len, RAnalOpMask mask) { op->size = snes_op_get_size(snesflags->M, snesflags->X, &snes_op[data[0]]); diff --git a/libr/anal/p/anal_tms320.c b/libr/anal/p/anal_tms320.c index d34cd41ddf..ae04a6674e 100644 --- a/libr/anal/p/anal_tms320.c +++ b/libr/anal/p/anal_tms320.c @@ -10,7 +10,7 @@ #include "anal_tms320c64x.c" #include "../../asm/arch/tms320/tms320_dasm.h" -static tms320_dasm_t engine = { 0 }; +static R_TH_LOCAL tms320_dasm_t engine = { 0 }; typedef int (* TMS_ANAL_OP_FN)(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf, int len); diff --git a/libr/anal/p/anal_x86_cs.c b/libr/anal/p/anal_x86_cs.c index c22abfaa6a..095bde6b70 100644 --- a/libr/anal/p/anal_x86_cs.c +++ b/libr/anal/p/anal_x86_cs.c @@ -59,7 +59,7 @@ struct Getarg { int bits; }; -static csh handle = 0; +static R_TH_LOCAL csh handle = 0; static void hidden_op(cs_insn *insn, cs_x86 *x, int mode) { unsigned int id = insn->id; @@ -3328,7 +3328,7 @@ static int cs_len_prefix_opcode(uint8_t *item) { } static int analop(RAnal *a, RAnalOp *op, ut64 addr, const ut8 *buf, int len, RAnalOpMask mask) { - static int omode = 0; + static R_TH_LOCAL int omode = 0; cs_insn *insn = NULL; int mode = (a->bits==64)? CS_MODE_64: (a->bits==32)? CS_MODE_32: diff --git a/libr/asm/arch/tms320/tms320_dasm.c b/libr/asm/arch/tms320/tms320_dasm.c index 437d0a1dc9..dcdaf8ec4d 100644 --- a/libr/asm/arch/tms320/tms320_dasm.c +++ b/libr/asm/arch/tms320/tms320_dasm.c @@ -94,8 +94,8 @@ int run_f_list(tms320_dasm_t * dasm) { break; case TMS320_FLAG_l1: - temp = get_bits(dasm->opcode64, flag->f, 1); - set_field_value(dasm, l1, temp); + temp = get_bits (dasm->opcode64, flag->f, 1); + set_field_value (dasm, l1, temp); break; case TMS320_FLAG_l3: temp = get_bits(dasm->opcode64, flag->f, 3); @@ -1026,9 +1026,8 @@ insn_item_t * decode_insn(tms320_dasm_t * dasm) } -insn_item_t * decode_insn_head(tms320_dasm_t * dasm) -{ - run_f_list(dasm); +insn_item_t * decode_insn_head(tms320_dasm_t * dasm) { + run_f_list (dasm); if (dasm->insn->i_list) { dasm->insn = dasm->insn->i_list; @@ -1112,8 +1111,7 @@ static int full_insn_size(tms320_dasm_t *dasm) * TMS320 disassembly engine public interface */ -int tms320_dasm(tms320_dasm_t * dasm, const ut8 * stream, int len) -{ +int tms320_dasm(tms320_dasm_t * dasm, const ut8 * stream, int len) { init_dasm(dasm, stream, len); if (tms320_f_get_cpu(dasm) != TMS320_F_CPU_C55X_PLUS) { @@ -1140,25 +1138,23 @@ int tms320_dasm(tms320_dasm_t * dasm, const ut8 * stream, int len) // insn_head_t c55x_list[] #include "c55x/table.h" -int tms320_dasm_init(tms320_dasm_t * dasm) { +bool tms320_dasm_init(tms320_dasm_t * dasm) { int i = 0; if (dasm->map) { /* already initialized */ - return 0; + return false; } dasm->map = ht_up_new0 (); if (!dasm->map) { - return 0; + return false; } for (i = 0; i < ARRAY_SIZE(c55x_list); i++) { ht_up_insert (dasm->map, c55x_list[i].byte, &c55x_list[i]); } - - tms320_f_set_cpu(dasm, TMS320_F_CPU_C55X); - - return 0; + tms320_f_set_cpu (dasm, TMS320_F_CPU_C55X); + return true; } int tms320_dasm_fini(tms320_dasm_t * dasm) { diff --git a/libr/asm/arch/tms320/tms320_dasm.h b/libr/asm/arch/tms320/tms320_dasm.h index f4e91b16ab..a5121fddb6 100644 --- a/libr/asm/arch/tms320/tms320_dasm.h +++ b/libr/asm/arch/tms320/tms320_dasm.h @@ -239,7 +239,7 @@ typedef struct { extern int tms320_dasm(tms320_dasm_t *, const ut8 *, int); -extern int tms320_dasm_init(tms320_dasm_t *); +extern bool tms320_dasm_init(tms320_dasm_t *); extern int tms320_dasm_fini(tms320_dasm_t *); #endif /* __TMS320_DASM_H__ */ diff --git a/libr/asm/p/asm_arm_cs.c b/libr/asm/p/asm_arm_cs.c index 4a35e25b2f..35e5736ab6 100644 --- a/libr/asm/p/asm_arm_cs.c +++ b/libr/asm/p/asm_arm_cs.c @@ -8,9 +8,10 @@ #include "./asm_arm_hacks.inc" bool arm64ass(const char *str, ut64 addr, ut32 *op); -static csh cd = 0; -static HtUU *ht_itblock = NULL; -static HtUU *ht_it = NULL; +// XXX kill globals +static R_TH_LOCAL csh cd = 0; +static R_TH_LOCAL HtUU *ht_itblock = NULL; +static R_TH_LOCAL HtUU *ht_it = NULL; #include "cs_mnemonics.c" diff --git a/libr/asm/p/asm_snes.c b/libr/asm/p/asm_snes.c index 0d4e754c69..c554045208 100644 --- a/libr/asm/p/asm_snes.c +++ b/libr/asm/p/asm_snes.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2012-2015 - condret, pancake */ +/* radare - LGPL - Copyright 2012-2021 - condret, pancake */ #include #include @@ -7,7 +7,7 @@ #include "../arch/snes/snesdis.c" #include "asm_snes.h" -static struct snes_asm_flags* snesflags = NULL; +static R_TH_LOCAL struct snes_asm_flags* snesflags = NULL; static bool snes_asm_init(void* user) { if (!snesflags) { diff --git a/libr/asm/p/asm_tms320.c b/libr/asm/p/asm_tms320.c index 4a07848778..e072c99163 100644 --- a/libr/asm/p/asm_tms320.c +++ b/libr/asm/p/asm_tms320.c @@ -72,7 +72,7 @@ static int tms320c64x_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) #include "../arch/tms320/tms320_dasm.h" -static tms320_dasm_t engine = { 0 }; +static R_TH_LOCAL tms320_dasm_t engine = { 0 }; static int tms320_disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { if (a->cpu && r_str_casecmp (a->cpu, "c54x") == 0) { diff --git a/libr/asm/p/asm_x86_cs.c b/libr/asm/p/asm_x86_cs.c index 2f92c29475..c111aefb93 100644 --- a/libr/asm/p/asm_x86_cs.c +++ b/libr/asm/p/asm_x86_cs.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2013-2021 - pancake */ +/* radare2 - LGPL - Copyright 2013-2022 - pancake */ #include #include @@ -6,8 +6,8 @@ #define USE_ITER_API 1 -static csh cd = 0; -static int n = 0; +static R_TH_LOCAL csh cd = 0; +static R_TH_LOCAL int n = 0; static bool the_end(void *p) { #if 0 @@ -57,7 +57,7 @@ static bool check_features(RAsm *a, cs_insn *insn) { } static int disassemble(RAsm *a, RAsmOp *op, const ut8 *buf, int len) { - static int omode = 0; + static R_TH_LOCAL int omode = 0; int mode, ret; ut64 off = a->pc; diff --git a/libr/bin/p/bin_dyldcache.c b/libr/bin/p/bin_dyldcache.c index 35802b7b47..d76699b72f 100644 --- a/libr/bin/p/bin_dyldcache.c +++ b/libr/bin/p/bin_dyldcache.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2018-2021 - pancake, keegan */ +/* radare2 - LGPL - Copyright 2018-2022 - pancake, mrmacete, keegan */ #include #include @@ -113,9 +113,29 @@ typedef struct _r_bin_image { ut32 nlist_count; } RDyldBinImage; -static RList *pending_bin_files = NULL; +static R_TH_LOCAL RList *pending_bin_files = NULL; -static ut64 va2pa(uint64_t addr, ut32 n_maps, cache_map_t *maps, RBuffer *cache_buf, ut64 slide, ut32 *offset, ut32 *left); +static ut64 va2pa(uint64_t addr, ut32 n_maps, cache_map_t *maps, RBuffer *cache_buf, ut64 slide, ut32 *offset, ut32 *left) { + ut64 res = UT64_MAX; + ut32 i; + + addr -= slide; + + for (i = 0; i < n_maps; i++) { + if (addr >= maps[i].address && addr < maps[i].address + maps[i].size) { + res = maps[i].fileOffset + addr - maps[i].address; + if (offset) { + *offset = addr - maps[i].address; + } + if (left) { + *left = maps[i].size - (addr - maps[i].address); + } + break; + } + } + + return res; +} static void free_bin(RDyldBinImage *bin) { if (!bin) { @@ -426,28 +446,6 @@ static void r_dyldcache_free(RDyldCache *cache) { R_FREE (cache); } -static ut64 va2pa(uint64_t addr, ut32 n_maps, cache_map_t *maps, RBuffer *cache_buf, ut64 slide, ut32 *offset, ut32 *left) { - ut64 res = UT64_MAX; - ut32 i; - - addr -= slide; - - for (i = 0; i < n_maps; i++) { - if (addr >= maps[i].address && addr < maps[i].address + maps[i].size) { - res = maps[i].fileOffset + addr - maps[i].address; - if (offset) { - *offset = addr - maps[i].address; - } - if (left) { - *left = maps[i].size - (addr - maps[i].address); - } - break; - } - } - - return res; -} - static ut64 bin_obj_va2pa(ut64 p, ut32 *offset, ut32 *left, RBinFile *bf) { if (!bf || !bf->o || !bf->o->bin_obj) { return 0; diff --git a/libr/bin/p/bin_java.c b/libr/bin/p/bin_java.c index 1712e31b49..d1ff10b95e 100644 --- a/libr/bin/p/bin_java.c +++ b/libr/bin/p/bin_java.c @@ -10,7 +10,8 @@ #define IFDBG_BIN_JAVA if (0) -static Sdb *DB = NULL; +static R_TH_LOCAL Sdb *DB = NULL; + static void add_bin_obj_to_sdb(RBinJavaObj *bin); static int add_sdb_bin_obj(const char *key, RBinJavaObj *bin_obj); diff --git a/libr/bin/p/bin_xnu_kernelcache.c b/libr/bin/p/bin_xnu_kernelcache.c index 0a7eb7e3fc..36b1c2db08 100644 --- a/libr/bin/p/bin_xnu_kernelcache.c +++ b/libr/bin/p/bin_xnu_kernelcache.c @@ -1,4 +1,4 @@ -/* radare2 - LGPL - Copyright 2019-2021 - mrmacete */ +/* radare2 - LGPL - Copyright 2019-2022 - mrmacete */ #include #include @@ -174,7 +174,7 @@ static void ensure_kexts_initialized(RKernelCacheObj *obj, RBinFile *bf); static void r_kernel_cache_free(RKernelCacheObj *obj); -static RList *pending_bin_files = NULL; +static R_TH_LOCAL RList *pending_bin_files = NULL; static bool load_buffer(RBinFile *bf, void **bin_obj, RBuffer *buf, ut64 loadaddr, Sdb *sdb) { RBuffer *fbuf = r_buf_ref (buf); diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 2c8036e760..247d5d9b8e 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -19,8 +19,8 @@ #define DEBUG_DISASM 0 // ugly globals but meh -static ut64 emustack_min = 0LL; -static ut64 emustack_max = 0LL; +static R_TH_LOCAL ut64 emustack_min = 0LL; +static R_TH_LOCAL ut64 emustack_max = 0LL; static const char* r_vline_a[] = { "|", // LINE_VERT @@ -881,7 +881,7 @@ static RDisasmState *ds_init(RCore *core) { return ds; } -static ut64 lastaddr = UT64_MAX; +static R_TH_LOCAL ut64 lastaddr = UT64_MAX; static void ds_reflines_fini(RDisasmState *ds) { RAnal *anal = ds->core->anal; diff --git a/libr/core/task.c b/libr/core/task.c index 2a727a7e63..1aef06ca58 100644 --- a/libr/core/task.c +++ b/libr/core/task.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2014-2019 - pancake, thestr4ng3r */ +/* radare - LGPL - Copyright 2014-2022 - pancake, thestr4ng3r */ #include diff --git a/libr/core/visual.c b/libr/core/visual.c index 4773461980..6504493d02 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2009-2021 - pancake */ +/* radare - LGPL - Copyright 2009-2022 - pancake */ #include #include diff --git a/libr/lang/lang.c b/libr/lang/lang.c index 34a12ded86..8995b69e95 100644 --- a/libr/lang/lang.c +++ b/libr/lang/lang.c @@ -20,7 +20,7 @@ R_LIB_VERSION(r_lang); #include "p/go.c" // hardcoded #include "p/lib.c" -static RLang *__lang = NULL; +static R_TH_LOCAL RLang *__lang = NULL; R_API void r_lang_plugin_free(RLangPlugin *p) { if (p && p->fini) { diff --git a/libr/lang/p/spp.c b/libr/lang/p/spp.c index 72ca1687a6..7bbeaafef6 100644 --- a/libr/lang/p/spp.c +++ b/libr/lang/p/spp.c @@ -5,7 +5,7 @@ #include "r_lang.h" #define USE_R2 1 -static RLang *Glang = NULL; +static R_TH_LOCAL RLang *Glang = NULL; #undef S_API // #include "../../../shlr/spp/spp.c" #include "../../../shlr/spp/spp.h" diff --git a/libr/util/lib.c b/libr/util/lib.c index 6216877950..c2fb584eae 100644 --- a/libr/util/lib.c +++ b/libr/util/lib.c @@ -1,4 +1,4 @@ -/* radare - LGPL - Copyright 2008-2021 - pancake */ +/* radare - LGPL - Copyright 2008-2022 - pancake */ #include #include @@ -7,7 +7,7 @@ R_LIB_VERSION(r_lib); /* TODO: avoid globals */ #define IFDBG if(__has_debug) -static bool __has_debug = false; +static R_TH_LOCAL bool __has_debug = false; /* XXX : this must be registered in runtime */ static const char *r_lib_types[] = { diff --git a/libr/util/log.c b/libr/util/log.c index a91e51b008..5e8b0b27a6 100644 --- a/libr/util/log.c +++ b/libr/util/log.c @@ -1,18 +1,18 @@ -/* radare - LGPL - Copyright 2007-2018 - pancake, ret2libc */ +/* radare - LGPL - Copyright 2007-2022 - pancake, ret2libc */ -#define LOG_CONFIGSTR_SIZE 512 +#define LOG_CONFIGSTR_SIZE 128 #define LOG_OUTPUTBUF_SIZE 512 #include #include // TODO: Use thread-local storage to make these variables thread-safe -static RList *log_cbs = NULL; // Functions to call when outputting log string -static int cfg_loglvl = R_LOGLVL_ERROR; // Log level output -static int cfg_logtraplvl = R_LOGLVL_FATAL; // Log trap level -static bool cfg_logsrcinfo = false; // Print out debug source info with the output -static bool cfg_logcolors = false; // Output colored log text based on level -static char cfg_logfile[LOG_CONFIGSTR_SIZE] = ""; // Output text to filename +static R_TH_LOCAL RList *log_cbs = NULL; // Functions to call when outputting log string +static R_TH_LOCAL int cfg_loglvl = R_LOGLVL_ERROR; // Log level output +static R_TH_LOCAL int cfg_logtraplvl = R_LOGLVL_FATAL; // Log trap level +static R_TH_LOCAL bool cfg_logsrcinfo = false; // Print out debug source info with the output +static R_TH_LOCAL bool cfg_logcolors = false; // Output colored log text based on level +static R_TH_LOCAL char cfg_logfile[LOG_CONFIGSTR_SIZE] = ""; // Output text to filename static const char *level_tags[] = { // Log level to tag string lookup array [R_LOGLVL_SILLY] = "SILLY", [R_LOGLVL_VERBOSE] = "VERBOSE", diff --git a/libr/util/print.c b/libr/util/print.c index 42c96f7688..c5b05a5cc4 100644 --- a/libr/util/print.c +++ b/libr/util/print.c @@ -5,7 +5,10 @@ #define DFLT_ROWS 16 +// global static const char hex[16] = "0123456789ABCDEF"; +// global mutable +static R_TH_LOCAL RPrintIsInterruptedCallback is_interrupted_cb = NULL; static int nullprinter(const char *a, ...) { return 0; @@ -27,8 +30,6 @@ static int libc_eprintf(const char *format, ...) { return 0; } -static RPrintIsInterruptedCallback is_interrupted_cb = NULL; - R_API void r_print_portionbar(RPrint *p, const ut64 *portions, int n_portions) { const int use_color = p->flags & R_PRINT_FLAGS_COLOR; int i, j; @@ -2059,9 +2060,9 @@ R_API const char* r_print_color_op_type(RPrint *p, ut32 anal_type) { } } -// Global buffer to speed up colorizing performance +// XXX Global buffer to speed up colorizing performance #define COLORIZE_BUFSIZE 1024 -static char o[COLORIZE_BUFSIZE]; +static R_TH_LOCAL char o[COLORIZE_BUFSIZE]; static bool issymbol(char c) { switch (c) { diff --git a/libr/util/sandbox.c b/libr/util/sandbox.c index 88177453db..6b0c5385d3 100644 --- a/libr/util/sandbox.c +++ b/libr/util/sandbox.c @@ -15,9 +15,9 @@ #include #endif -static bool G_enabled = false; -static bool G_disabled = false; -static int G_graintype = R_SANDBOX_GRAIN_NONE; +static R_TH_LOCAL bool G_enabled = false; +static R_TH_LOCAL bool G_disabled = false; +static R_TH_LOCAL int G_graintype = R_SANDBOX_GRAIN_NONE; #define R_SANDBOX_GUARD(x,y) if (G_enabled && !(G_graintype & (x))) { return (y); } diff --git a/libr/util/sys.c b/libr/util/sys.c index f48d7687d9..676de9151e 100644 --- a/libr/util/sys.c +++ b/libr/util/sys.c @@ -1247,7 +1247,11 @@ R_API char *r_sys_whoami(void) { } #elif __wasi__ strcpy (buf, "user"); +#elif HAVE_TH_LOCAL + char *user = r_sys_getenv ("USER"); + return user? user: r_str_newf ("uid%d", getuid ()); #else + // XXX this is not thread safe and getpwuid_r is not available struct passwd *pw = getpwuid (getuid ()); if (pw) { return strdup (pw->pw_name); @@ -1308,7 +1312,7 @@ R_API bool r_sys_tts(const char *txt, bool bg) { } R_API const char *r_sys_prefix(const char *pfx) { - static char *prefix = NULL; + static R_TH_LOCAL char *prefix = NULL; if (!prefix) { #if __WINDOWS__ prefix = r_sys_get_src_dir_w32 (); diff --git a/sys/sanitize.sh b/sys/sanitize.sh index 9106f1daaa..60e85f48e2 100755 --- a/sys/sanitize.sh +++ b/sys/sanitize.sh @@ -1,7 +1,9 @@ #!/bin/sh # SANITIZE="address leak memory undefined" # SANITIZE="address signed-integer-overflow" # Faster build +# default: SANITIZE=${SANITIZE:="address undefined signed-integer-overflow"} +# SANITIZE=${SANITIZE:="thread undefined signed-integer-overflow"} printf "\033[32m" echo "========================================================================="