From fd1f980ecae769acc21c00f75de125efae4db4f8 Mon Sep 17 00:00:00 2001 From: Riccardo Schirone Date: Mon, 16 Dec 2019 23:23:17 +0100 Subject: [PATCH] Pass R2_CONFIG file to ! commands and serialize methods ##config --- .travis.yml | 2 +- libr/config/config.c | 23 +++++++++++++++++++++++ libr/core/cfile.c | 24 ++++++++++++++++++++++-- libr/include/r_config.h | 3 +++ libr/main/rabin2.c | 13 ++++++++++++- 5 files changed, 61 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index de35fcfa74..1ac7d41b20 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ matrix: env: COMPILER_NAME=gcc CXX=g++ CC=gcc INSTALL_SYSTEM=meson COVERAGE=1 CFLAGS="-DR2_ASSERT_STDOUT=1" # OS X with clang - os: osx - env: COMPILER_NAME=clang CXX=clang++ CC=clang CFLAGS="-DR2_ASSERT_STDOUT=1" LC_ALL=C + env: COMPILER_NAME=clang CXX=clang++ CC=clang CFLAGS="-DR2_ASSERT_STDOUT=1" # Run Fuzzing Sanity Test on every push # XXX its broken it complains about invalid key #- os: linux diff --git a/libr/config/config.c b/libr/config/config.c index 5b471acec0..1285d3ab65 100644 --- a/libr/config/config.c +++ b/libr/config/config.c @@ -693,3 +693,26 @@ R_API void r_config_bump(RConfig *cfg, const char *key) { free (orig); } } + +R_API void r_config_serialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db) { + RListIter *iter; + RConfigNode *node; + r_list_foreach (config->nodes, iter, node) { + sdb_set (db, node->name, node->value, 0); + } +} + +static int load_config_cb(void *user, const char *k, const char *v) { + RConfig *config = user; + RConfigNode *node = r_config_node_get (config, k); + if (!node) { + return 1; + } + r_config_set (config, k, v); + return 1; +} + +R_API bool r_config_unserialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db, R_NULLABLE char **err) { + sdb_foreach (db, load_config_cb, config); + return true; +} diff --git a/libr/core/cfile.c b/libr/core/cfile.c index 0a35a154c6..8e3b1c7942 100644 --- a/libr/core/cfile.c +++ b/libr/core/cfile.c @@ -208,6 +208,14 @@ R_API void r_core_sysenv_end(RCore *core, const char *cmd) { r_sys_setenv ("R2_FILE", NULL); r_sys_setenv ("R2_BYTES", NULL); r_sys_setenv ("R2_OFFSET", NULL); + + // remove temporary R2_CONFIG file + char *r2_config = r_sys_getenv ("R2_CONFIG"); + if (r2_config) { + r_file_rm (r2_config); + r_sys_setenv ("R2_CONFIG", NULL); + free (r2_config); + } } #if DISCUSS @@ -238,12 +246,24 @@ R_API char *r_core_sysenv_begin(RCore * core, const char *cmd) { } } } - r_sys_setenv ("RABIN2_LANG", r_config_get (core->config, "bin.lang")); - r_sys_setenv ("RABIN2_DEMANGLE", r_config_get (core->config, "bin.demangle")); r_sys_setenv ("R2_OFFSET", sdb_fmt ("%"PFMT64d, core->offset)); r_sys_setenv ("R2_XOFFSET", sdb_fmt ("0x%08"PFMT64x, core->offset)); r_sys_setenv ("R2_ENDIAN", core->assembler->big_endian? "big": "little"); r_sys_setenv ("R2_BSIZE", sdb_fmt ("%d", core->blocksize)); + + // dump current config file so other r2 tools can use the same options + char *config_sdb_path = NULL; + int config_sdb_fd = r_file_mkstemp (NULL, &config_sdb_path); + close (config_sdb_fd); + + Sdb *config_sdb = sdb_new (NULL, config_sdb_path, 0); + r_config_serialize (core->config, config_sdb); + sdb_sync (config_sdb); + sdb_free (config_sdb); + r_sys_setenv ("R2_CONFIG", config_sdb_path); + + r_sys_setenv ("RABIN2_LANG", r_config_get (core->config, "bin.lang")); + r_sys_setenv ("RABIN2_DEMANGLE", r_config_get (core->config, "bin.demangle")); r_sys_setenv ("R2_ARCH", r_config_get (core->config, "asm.arch")); r_sys_setenv ("R2_BITS", sdb_fmt ("%d", r_config_get_i (core->config, "asm.bits"))); r_sys_setenv ("R2_COLOR", r_config_get_i (core->config, "scr.color")? "1": "0"); diff --git a/libr/include/r_config.h b/libr/include/r_config.h index 46adcf077a..85d503b1ed 100644 --- a/libr/include/r_config.h +++ b/libr/include/r_config.h @@ -95,6 +95,9 @@ R_API bool r_config_readonly (RConfig *cfg, const char *key); R_API void r_config_set_sort_column (char *column); R_API bool r_config_set_setter (RConfig *cfg, const char *key, RConfigCallback cb); R_API bool r_config_set_getter (RConfig *cfg, const char *key, RConfigCallback cb); + +R_API void r_config_serialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db); +R_API bool r_config_unserialize(R_NONNULL RConfig *config, R_NONNULL Sdb *db, R_NULLABLE char **err); #endif #ifdef __cplusplus diff --git a/libr/main/rabin2.c b/libr/main/rabin2.c index 8e58c614cb..c815117a78 100644 --- a/libr/main/rabin2.c +++ b/libr/main/rabin2.c @@ -94,7 +94,8 @@ static int rabin_show_help(int v) { " RABIN2_DMNGLRCMD: e bin.demanglercmd # try to purge false positives\n" " RABIN2_PDBSERVER: e pdb.server # use alternative PDB server\n" " RABIN2_SYMSTORE: e pdb.symstore # path to downstream symbol store\n" - " RABIN2_PREFIX: e bin.prefix # prefix symbols/sections/relocs with a specific string\n"); + " RABIN2_PREFIX: e bin.prefix # prefix symbols/sections/relocs with a specific string\n" + " R2_CONFIG: # sdb config file\n"); } return 1; } @@ -590,6 +591,16 @@ R_API int r_main_rabin2(int argc, char **argv) { } free (tmp); + if ((tmp = r_sys_getenv ("R2_CONFIG"))) { + Sdb *config_sdb = sdb_new (NULL, tmp, 0); + if (config_sdb) { + r_config_unserialize (core.config, config_sdb, NULL); + sdb_free (config_sdb); + } else { + printf ("Cannot open file specified in RADARE2_CONFIG\n"); + } + free (tmp); + } if ((tmp = r_sys_getenv ("RABIN2_DMNGLRCMD"))) { r_config_set (core.config, "bin.demanglecmd", tmp); free (tmp);