Skip to content

Commit

Permalink
Remove RzFlag.base
Browse files Browse the repository at this point in the history
The only place where this field was set to a non-zero value was the now
removed fb command, and it was never used for anything sensible. Thus it
is safe to remove.
  • Loading branch information
thestr4ng3r committed Apr 3, 2024
1 parent c88a876 commit d70c922
Show file tree
Hide file tree
Showing 10 changed files with 130 additions and 31 deletions.
2 changes: 1 addition & 1 deletion librz/core/cbin.c
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ RZ_API bool rz_core_bin_apply_symbols(RzCore *core, RzBinFile *binfile, bool va)
}
if (fi) {
rz_flag_item_set_realname(fi, sn.methname);
if ((fi->offset - core->flags->base) == addr) {
if (fi->offset == addr) {
rz_flag_unset(core->flags, fi);
}
} else {
Expand Down
18 changes: 17 additions & 1 deletion librz/core/project_migrate.c
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ RZ_API bool rz_project_migrate_v14_v15(RzProject *prj, RzSerializeResultInfo *re
// --
// Migration 15 -> 16
//
// Changes from <commit hash not yet known>:
// Changes from f9422ac0cd6922f73208e5f5e6f47b3d64b3bd0d:
// Removed options:
// - `bin.maxstr`
// Renamed options:
Expand All @@ -631,6 +631,21 @@ RZ_API bool rz_project_migrate_v15_v16(RzProject *prj, RzSerializeResultInfo *re
return true;
}

// --
// Migration 16 -> 17
//
// Changes from <commit hash not yet known>:
// Removed /core/flags/base key (RzFlag.base)

RZ_API bool rz_project_migrate_v16_v17(RzProject *prj, RzSerializeResultInfo *res) {
Sdb *core_db;
RZ_SERIALIZE_SUB(prj, core_db, res, "core", return false;);
Sdb *flags_db;
RZ_SERIALIZE_SUB(core_db, flags_db, res, "flags", return false;);
sdb_unset(flags_db, "base", 0);
return true;
}

static bool (*const migrations[])(RzProject *prj, RzSerializeResultInfo *res) = {
rz_project_migrate_v1_v2,
rz_project_migrate_v2_v3,
Expand All @@ -647,6 +662,7 @@ static bool (*const migrations[])(RzProject *prj, RzSerializeResultInfo *res) =
rz_project_migrate_v13_v14,
rz_project_migrate_v14_v15,
rz_project_migrate_v15_v16,
rz_project_migrate_v16_v17
};

/// Migrate the given project to the current version in-place
Expand Down
3 changes: 1 addition & 2 deletions librz/flag/flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ RZ_API RzFlag *rz_flag_new(void) {
rz_flag_free(f);
return NULL;
}
f->base = 0;
f->zones = NULL;
f->tags = sdb_new0();
f->ht_name = ht_pp_new(NULL, ht_free_flag, NULL);
Expand Down Expand Up @@ -660,7 +659,7 @@ RZ_API RzFlagItem *rz_flag_set(RzFlag *f, const char *name, ut64 off, ut32 size)
item->space = rz_flag_space_cur(f);
item->size = size;

update_flag_item_offset(f, item, off + f->base, is_new, true);
update_flag_item_offset(f, item, off, is_new, true);
update_flag_item_name(f, item, name, true);
return item;
err:
Expand Down
18 changes: 2 additions & 16 deletions librz/flag/serialize_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
* SDB Format:
*
* /
* base=<base>
* realnames=<realnames?"1":"0">
* /spaces
* see spaces.c
Expand All @@ -18,7 +17,6 @@
* <zone name>={"from":<from>,"to":<to>}
* /flags
* <flag name>={"realname":<str>,"demangled":<bool>,"offset":<uint>,"size":<uint>,"space":<str>,"color":<str>,"comment":<str>,"alias":<str>}
*
*/

RZ_API void rz_serialize_flag_zones_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzList /*<RzFlagZoneItem *>*/ *zones) {
Expand Down Expand Up @@ -122,11 +120,6 @@ static bool flag_save_cb(RzFlagItem *flag, void *user) {

RZ_API void rz_serialize_flag_save(RZ_NONNULL Sdb *db, RZ_NONNULL RzFlag *flag) {
rz_serialize_spaces_save(sdb_ns(db, "spaces", true), &flag->spaces);
char buf[32];
if (snprintf(buf, sizeof(buf), "%" PFMT64d, flag->base) < 0) {
return;
}
sdb_set(db, "base", buf, 0);
sdb_set(db, "realnames", flag->realnames ? "1" : "0", 0);
sdb_copy(flag->tags, sdb_ns(db, "tags", true));
rz_serialize_flag_zones_save(sdb_ns(db, "zones", true), flag->zones);
Expand Down Expand Up @@ -227,7 +220,7 @@ static bool flag_load_cb(void *user, const char *k, const char *v) {
goto beach;
}

RzFlagItem *item = rz_flag_set(ctx->flag, k, proto.offset - ctx->flag->base, proto.size);
RzFlagItem *item = rz_flag_set(ctx->flag, k, proto.offset, proto.size);
if (proto.realname) {
rz_flag_item_set_realname(item, proto.realname);
}
Expand Down Expand Up @@ -270,14 +263,7 @@ static bool load_flags(RZ_NONNULL Sdb *flags_db, RZ_NONNULL RzFlag *flag) {
RZ_API bool rz_serialize_flag_load(RZ_NONNULL Sdb *db, RZ_NONNULL RzFlag *flag, RZ_NULLABLE RzSerializeResultInfo *res) {
rz_flag_unset_all(flag);

const char *str = sdb_const_get(db, "base", NULL);
if (!str) {
RZ_SERIALIZE_ERR(res, "flag base key is missing");
return false;
}
flag->base = strtoll(str, NULL, 0);

str = sdb_const_get(db, "realnames", 0);
const char *str = sdb_const_get(db, "realnames", 0);
if (!str) {
RZ_SERIALIZE_ERR(res, "flag realnames key is missing");
return false;
Expand Down
1 change: 0 additions & 1 deletion librz/include/rz_flag.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ typedef struct rz_flag_item_t {

typedef struct rz_flag_t {
RzSpaces spaces; /* handle flag spaces */
st64 base; /* base address for all flag items */
bool realnames;
Sdb *tags;
RzNum *num;
Expand Down
3 changes: 2 additions & 1 deletion librz/include/rz_project.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
extern "C" {
#endif

#define RZ_PROJECT_VERSION 16
#define RZ_PROJECT_VERSION 17

typedef Sdb RzProject;

Expand Down Expand Up @@ -61,6 +61,7 @@ RZ_API bool rz_project_migrate_v12_v13(RzProject *prj, RzSerializeResultInfo *re
RZ_API bool rz_project_migrate_v13_v14(RzProject *prj, RzSerializeResultInfo *res);
RZ_API bool rz_project_migrate_v14_v15(RzProject *prj, RzSerializeResultInfo *res);
RZ_API bool rz_project_migrate_v15_v16(RzProject *prj, RzSerializeResultInfo *res);
RZ_API bool rz_project_migrate_v16_v17(RzProject *prj, RzSerializeResultInfo *res);
RZ_API bool rz_project_migrate(RzProject *prj, unsigned long version, RzSerializeResultInfo *res);

#ifdef __cplusplus
Expand Down
1 change: 1 addition & 0 deletions test/db/cmd/project
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ Detailed project load info:
project migrated from version 13 to 14.
project migrated from version 14 to 15.
project migrated from version 15 to 16.
project migrated from version 16 to 17.
EOF
RUN

Expand Down
26 changes: 26 additions & 0 deletions test/integration/test_project_migrate.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,22 @@ static bool test_migrate_v15_v16_str_config() {
mu_end;
}

static bool test_migrate_v16_v17_flags_base() {
RzProject *prj = rz_project_load_file_raw("prj/v16-flags-base.rzdb");
mu_assert_notnull(prj, "load raw project");
RzSerializeResultInfo *res = rz_serialize_result_info_new();
bool s = rz_project_migrate_v16_v17(prj, res);
mu_assert_true(s, "migrate success");
Sdb *core_db = sdb_ns(prj, "core", false);
mu_assert_notnull(core_db, "core ns");
Sdb *config_db = sdb_ns(core_db, "flags", false);
mu_assert_notnull(config_db, "config ns");
mu_assert_null(sdb_get(config_db, "base", 0), "flags base");
rz_serialize_result_info_free(res);
rz_project_free(prj);
mu_end;
}


/// Load project of given version from file into core and check the log for migration success messages
#define BEGIN_LOAD_TEST(core, version, file) \
Expand Down Expand Up @@ -954,6 +970,14 @@ static bool test_load_v15_str_config() {
mu_end;
}

static bool test_load_v16() {
RzCore *core = rz_core_new();
BEGIN_LOAD_TEST(core, 16, "prj/v16-flags-base.rzdb");
// No new or changed info here
rz_core_free(core);
mu_end;
}

int all_tests() {
mu_run_test(test_migrate_v1_v2_noreturn);
mu_run_test(test_migrate_v1_v2_noreturn_empty);
Expand All @@ -973,6 +997,7 @@ int all_tests() {
mu_run_test(test_migrate_v2_v12);
mu_run_test(test_migrate_v14_v15);
mu_run_test(test_migrate_v15_v16_str_config);
mu_run_test(test_migrate_v16_v17_flags_base);
mu_run_test(test_load_v1_noreturn);
mu_run_test(test_load_v1_noreturn_empty);
mu_run_test(test_load_v1_unknown_type);
Expand All @@ -994,6 +1019,7 @@ int all_tests() {
mu_run_test(test_load_v14);
mu_run_test(test_load_v15_seek_history);
mu_run_test(test_load_v15_str_config);
mu_run_test(test_load_v16);
return tests_passed != tests_run;
}

Expand Down
76 changes: 76 additions & 0 deletions test/prj/v16-flags-base.rzdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/
type=rizin rz-db project
version=16

/core
blocksize=0x100
offset=0x5ae0

/core/analysis

/core/analysis/blocks

/core/analysis/callables

/core/analysis/cc

/core/analysis/classes

/core/analysis/classes/attrs

/core/analysis/functions

/core/analysis/hints

/core/analysis/imports

/core/analysis/meta

/core/analysis/meta/spaces
name=CS
spacestack=["*"]

/core/analysis/meta/spaces/spaces
bin=s

/core/analysis/noreturn

/core/analysis/types

/core/analysis/vars

/core/analysis/xrefs

/core/config

/core/debug

/core/debug/breakpoints

/core/file
relative=../bins/elf/ls

/core/flags
base=100
realnames=1

/core/flags/flags

/core/flags/spaces
name=fs
spacestack=["*"]

/core/flags/spaces/spaces
classes=s
relocs=s
sections=s
segments=s
strings=s
symbols=s

/core/flags/tags

/core/flags/zones

/core/seek
0={"offset":23264,"cursor":0,"current":true}
13 changes: 4 additions & 9 deletions test/unit/test_serialize_flag.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
Sdb *ref_0_db() {
Sdb *db = sdb_new0();

sdb_set(db, "base", "-1337", 0);
sdb_set(db, "realnames", "1", 0);

Sdb *spaces_db = sdb_ns(db, "spaces", true);
Expand All @@ -27,17 +26,16 @@ Sdb *ref_0_db() {
sdb_set(zones_db, PERTURBATOR, "{\"from\":3735928559,\"to\":18446744073709551614}", 0);

Sdb *flags_db = sdb_ns(db, "flags", true);
sdb_set(flags_db, "foobars", "{\"realname\":\"Foobars\",\"demangled\":true,\"offset\":3582,\"size\":16,\"space\":\"reveries\",\"color\":\"white\",\"comment\":\"windowpane\",\"alias\":\"f00b4r5\"}", 0);
sdb_set(flags_db, "f00b4r5", "{\"realname\":\"f00b4r5\",\"demangled\":false,\"offset\":3582,\"size\":1}", 0);
sdb_set(flags_db, "deliverance", "{\"realname\":\"deliverance\",\"demangled\":false,\"offset\":66,\"size\":19}", 0);
sdb_set(flags_db, "foobars", "{\"realname\":\"Foobars\",\"demangled\":true,\"offset\":4919,\"size\":16,\"space\":\"reveries\",\"color\":\"white\",\"comment\":\"windowpane\",\"alias\":\"f00b4r5\"}", 0);
sdb_set(flags_db, "f00b4r5", "{\"realname\":\"f00b4r5\",\"demangled\":false,\"offset\":4919,\"size\":1}", 0);
sdb_set(flags_db, "deliverance", "{\"realname\":\"deliverance\",\"demangled\":false,\"offset\":1403,\"size\":19}", 0);

return db;
}

RzFlag *ref_0_flag() {
RzFlag *flag = rz_flag_new();

flag->base = -1337;
flag->realnames = true;

rz_flag_set(flag, "deliverance", 0x42 + 1337, 0x13);
Expand Down Expand Up @@ -67,7 +65,6 @@ RzFlag *ref_0_flag() {
Sdb *ref_1_db() {
Sdb *db = sdb_new0();

sdb_set(db, "base", "0", 0);
sdb_set(db, "realnames", "0", 0);

Sdb *spaces_db = sdb_ns(db, "spaces", true);
Expand All @@ -83,7 +80,6 @@ Sdb *ref_1_db() {

RzFlag *ref_1_flag() {
RzFlag *flag = rz_flag_new();
flag->base = 0;
flag->realnames = false;
return flag;
}
Expand Down Expand Up @@ -204,7 +200,6 @@ static bool test_load(Sdb *db, RzFlag *ref) {
continue;
}

mu_assert_eq_fmt(flag->base, ref->base, "base", "0x%" PFMT64x);
mu_assert_eq(flag->realnames, ref->realnames, "realnames");
assert_sdb_eq(flag->tags, ref->tags, "tags");

Expand Down Expand Up @@ -238,4 +233,4 @@ int all_tests() {
return tests_passed != tests_run;
}

mu_main(all_tests)
mu_main(all_tests)

0 comments on commit d70c922

Please sign in to comment.