Skip to content

Commit

Permalink
Make RzFlagItem opaque
Browse files Browse the repository at this point in the history
Accessing properties through functions allows the implementation to be
swapped out, which is the plan for polymorphic flag sources.
  • Loading branch information
thestr4ng3r committed Apr 5, 2024
1 parent c6537d9 commit 0bb2905
Show file tree
Hide file tree
Showing 49 changed files with 632 additions and 470 deletions.
5 changes: 3 additions & 2 deletions librz/arch/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ RZ_API bool rz_analysis_noreturn_add(RzAnalysis *analysis, const char *name, ut6
RZ_LOG_ERROR("Cannot find function and flag at address 0x%" PFMT64x "\n", addr);
return false;
}
tmp_name = fcn ? fcn->name : fi->name;
tmp_name = fcn ? fcn->name : rz_flag_item_get_name(fi);
if (fcn) {
fcn->is_noreturn = true;
}
Expand Down Expand Up @@ -707,7 +707,8 @@ RZ_API bool rz_analysis_noreturn_at(RzAnalysis *analysis, ut64 addr) {
}
RzFlagItem *fi = analysis->flag_get(analysis->flb.f, addr);
if (fi) {
if (rz_analysis_noreturn_at_name(analysis, fi->realname ? fi->realname : fi->name)) {
const char *rn = rz_flag_item_get_realname(fi);
if (rz_analysis_noreturn_at_name(analysis, rn ? rn : rz_flag_item_get_name(fi))) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion librz/arch/class.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ static void rz_analysis_class_rename_flag(RzAnalysis *analysis, const char *old_
if (!flag) {
return;
}
ut64 addr = flag->offset;
ut64 addr = rz_flag_item_get_offset(flag);
analysis->flb.unset(analysis->flb.f, flag);
analysis->flg_class_set(analysis->flb.f, new_name, addr, 0);
}
Expand Down
9 changes: 5 additions & 4 deletions librz/arch/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ static bool isSymbolNextInstruction(RzAnalysis *analysis, RzAnalysisOp *op) {
rz_return_val_if_fail(analysis && op && analysis->flb.get_at, false);

RzFlagItem *fi = analysis->flb.get_at(analysis->flb.f, op->addr + op->size, false);
return (fi && fi->name && (strstr(fi->name, "imp.") || strstr(fi->name, "sym.") || strstr(fi->name, "entry") || strstr(fi->name, "main")));
const char *name = fi ? rz_flag_item_get_name(fi) : NULL;
return (fi && name && (strstr(name, "imp.") || strstr(name, "sym.") || strstr(name, "entry") || strstr(name, "main")));
}

static bool is_delta_pointer_table(ReadAhead *ra, RzAnalysis *analysis, ut64 addr, ut64 lea_ptr, ut64 *jmptbl_addr, ut64 *casetbl_addr, RzAnalysisOp *jmp_aop) {
Expand Down Expand Up @@ -474,7 +475,7 @@ static const char *retpoline_reg(RzAnalysis *analysis, ut64 addr) {
RzFlagItem *flag = analysis->flag_get(analysis->flb.f, addr);
if (flag) {
const char *token = "x86_indirect_thunk_";
const char *thunk = strstr(flag->name, token);
const char *thunk = strstr(rz_flag_item_get_name(flag), token);
if (thunk) {
return thunk + strlen(token);
}
Expand Down Expand Up @@ -737,7 +738,7 @@ static RzAnalysisBBEndCause run_basic_block_analysis(RzAnalysisTaskItem *item, R

if (analysis->opt.nopskip && fcn->addr == at) {
RzFlagItem *fi = analysis->flb.get_at(analysis->flb.f, addr, false);
if (!fi || strncmp(fi->name, "sym.", 4)) {
if (!fi || strncmp(rz_flag_item_get_name(fi), "sym.", 4)) {
if ((addr + delay.un_idx - oplen) == fcn->addr) {
if (rz_analysis_block_relocate(bb, bb->addr + oplen, bb->size - oplen)) {
fcn->addr += oplen;
Expand Down Expand Up @@ -1046,7 +1047,7 @@ static RzAnalysisBBEndCause run_basic_block_analysis(RzAnalysisTaskItem *item, R
}
{
RzFlagItem *fi = analysis->flb.get_at(analysis->flb.f, op.jump, false);
if (fi && strstr(fi->name, "imp.")) {
if (fi && strstr(rz_flag_item_get_name(fi), "imp.")) {
gotoBeach(RZ_ANALYSIS_RET_END);
}
}
Expand Down
20 changes: 11 additions & 9 deletions librz/arch/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@

static bool isvalidflag(RzFlagItem *flag) {
if (flag) {
if (strstr(flag->name, "main") || strstr(flag->name, "entry")) {
const char *name = rz_flag_item_get_name(flag);
if (strstr(name, "main") || strstr(name, "entry")) {
return true;
}
if (strchr(flag->name, '.')) {
if (strchr(name, '.')) {
return true;
}
}
Expand Down Expand Up @@ -234,8 +235,9 @@ static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char
// TODO: implement realname with flags, because functions dont hold this yet
if (f->realnames) {
flag = p->flag_get(f, off);
if (flag && flag->realname) {
name = flag->realname;
const char *rn = flag ? rz_flag_item_get_realname(flag) : NULL;
if (rn) {
name = rn;
}
}
snprintf(str, len, "%s%s%s", data, name,
Expand All @@ -254,16 +256,16 @@ static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char
flag = flag2;
}
}
if (flag && !strncmp(flag->name, "section.", 8)) {
if (flag && !strncmp(rz_flag_item_get_name(flag), "section.", 8)) {
flag = rz_flag_get_i(f, off);
}
const char *label = fcn ? p->label_get(fcn, off) : NULL;
if (label || isvalidflag(flag)) {
if (p->notin_flagspace) {
if (p->flagspace == flag->space) {
if (p->flagspace == rz_flag_item_get_space(flag)) {
continue;
}
} else if (p->flagspace && (p->flagspace != flag->space)) {
} else if (p->flagspace && (p->flagspace != rz_flag_item_get_space(flag))) {
ptr = ptr2;
continue;
}
Expand All @@ -289,7 +291,7 @@ static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char
if (label) {
flagname = rz_str_newf(".%s", label);
} else {
flagname = rz_str_dup(f->realnames ? flag->realname : flag->name);
flagname = rz_str_dup(f->realnames ? rz_flag_item_get_realname(flag) : rz_flag_item_get_name(flag));
}
int maxflagname = p->maxflagnamelen;
if (maxflagname > 0 && flagname && strlen(flagname) > maxflagname) {
Expand Down Expand Up @@ -330,7 +332,7 @@ static bool filter(RzParse *p, ut64 addr, RzFlag *f, RzAnalysisHint *hint, char
}
}
if (p->subrel_addr && !banned && lea) { // TODO: use remove_brackets
int flag_len = strlen(flag->name);
int flag_len = strlen(rz_flag_item_get_name(flag));
char *ptr_end = str + strlen(data) + flag_len - 1;
char *ptr_right = ptr_end + 1, *ptr_left, *ptr_esc;
bool ansi_found = false;
Expand Down
32 changes: 16 additions & 16 deletions librz/arch/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ RZ_API RZ_BORROW RzList /*<RzAnalysisFunction *>*/ *rz_analysis_function_list(Rz

#define MIN_MATCH_LEN 4

static RZ_OWN char *function_name_try_guess(RzTypeDB *typedb, RZ_NONNULL char *name) {
static RZ_OWN char *function_name_try_guess(RzTypeDB *typedb, RZ_NONNULL const char *name) {
if (strlen(name) < MIN_MATCH_LEN) {
return NULL;
}
Expand All @@ -427,15 +427,15 @@ static RZ_OWN char *function_name_try_guess(RzTypeDB *typedb, RZ_NONNULL char *n
return NULL;
}

static inline bool is_auto_named(char *func_name, size_t slen) {
static inline bool is_auto_named(const char *func_name, size_t slen) {
return slen > 4 && (rz_str_startswith(func_name, "fcn.") || rz_str_startswith(func_name, "loc."));
}

static inline bool has_rz_prefixes(char *func_name, int offset, size_t slen) {
static inline bool has_rz_prefixes(const char *func_name, int offset, size_t slen) {
return slen > 4 && (offset + 3 < slen) && func_name[offset + 3] == '.';
}

static char *strip_rz_prefixes(char *func_name, size_t slen) {
static const char *strip_rz_prefixes(const char *func_name, size_t slen) {
// strip rizin prefixes (sym, sym.imp, etc')
int offset = 0;
while (has_rz_prefixes(func_name, offset, slen)) {
Expand All @@ -444,7 +444,7 @@ static char *strip_rz_prefixes(char *func_name, size_t slen) {
return func_name + offset;
}

static char *strip_common_prefixes_stdlib(char *func_name) {
static const char *strip_common_prefixes_stdlib(const char *func_name) {
// strip common prefixes from standard lib functions
if (rz_str_startswith(func_name, "__isoc99_")) {
func_name += 9;
Expand All @@ -456,15 +456,15 @@ static char *strip_common_prefixes_stdlib(char *func_name) {
return func_name;
}

static char *strip_dll_prefix(char *func_name) {
char *tmp = strstr(func_name, "dll_");
static const char *strip_dll_prefix(const char *func_name) {
const char *tmp = strstr(func_name, "dll_");
if (tmp) {
return tmp + 3;
}
return func_name;
}

static void clean_function_name(char *func_name) {
static void clean_function_name(const char *func_name) {
char *last = (char *)rz_str_lchr(func_name, '_');
if (!last || !rz_str_isnumber(last + 1)) {
return;
Expand All @@ -475,7 +475,7 @@ static void clean_function_name(char *func_name) {
/**
* \brief Checks if the function name was generated by Rizin automatically
*/
RZ_API bool rz_analysis_function_is_autonamed(RZ_NONNULL char *name) {
RZ_API bool rz_analysis_function_is_autonamed(RZ_NONNULL const char *name) {
size_t len = strlen(name);
return (len >= MIN_MATCH_LEN) && (is_auto_named(name, len) || has_rz_prefixes(name, 0, len));
}
Expand All @@ -490,9 +490,9 @@ RZ_API bool rz_analysis_function_is_autonamed(RZ_NONNULL char *name) {
* \param typedb RzTypeDB instance
* \param name Function name to check
*/
RZ_API RZ_OWN char *rz_analysis_function_name_guess(RzTypeDB *typedb, RZ_NONNULL char *name) {
RZ_API RZ_OWN char *rz_analysis_function_name_guess(RzTypeDB *typedb, RZ_NONNULL const char *name) {
rz_return_val_if_fail(typedb && name, NULL);
char *str = name;
const char *str = name;
char *result = NULL;

size_t slen = strlen(str);
Expand All @@ -508,14 +508,14 @@ RZ_API RZ_OWN char *rz_analysis_function_name_guess(RzTypeDB *typedb, RZ_NONNULL
return result;
}

str = strdup(str);
clean_function_name(str);
char *rstr = strdup(str);
clean_function_name(rstr);

if (*str == '_' && (result = function_name_try_guess(typedb, str + 1))) {
free(str);
if (*rstr == '_' && (result = function_name_try_guess(typedb, rstr + 1))) {
free(rstr);
return result;
}

free(str);
free(rstr);
return result;
}
7 changes: 4 additions & 3 deletions librz/arch/rtti_itanium.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,12 @@ static RTypeInfoType rtti_itanium_type_info_type_from_flag(RVTableContext *conte
RzListIter *iter;
RzFlagItem *flag;
rz_list_foreach (flags, iter, flag) {
if (strstr(flag->name, VMI_CLASS_TYPE_INFO_NAME)) {
const char *name = rz_flag_item_get_name(flag);
if (strstr(name, VMI_CLASS_TYPE_INFO_NAME)) {
return RZ_TYPEINFO_TYPE_VMI_CLASS;
} else if (strstr(flag->name, SI_CLASS_TYPE_INFO_NAME)) {
} else if (strstr(name, SI_CLASS_TYPE_INFO_NAME)) {
return RZ_TYPEINFO_TYPE_SI_CLASS;
} else if (strstr(flag->name, CLASS_TYPE_INFO_NAME)) {
} else if (strstr(name, CLASS_TYPE_INFO_NAME)) {
return RZ_TYPEINFO_TYPE_CLASS;
}
}
Expand Down
6 changes: 3 additions & 3 deletions librz/arch/var.c
Original file line number Diff line number Diff line change
Expand Up @@ -1402,7 +1402,7 @@ RZ_API void rz_analysis_extract_rarg(RzAnalysis *analysis, RzAnalysisOp *op, RzA
RzCore *core = (RzCore *)analysis->coreb.core;
RzFlagItem *flag = rz_flag_get_by_spaces(core->flags, offset, RZ_FLAGS_FS_IMPORTS, NULL);
if (flag) {
callee = rz_analysis_function_name_guess(analysis->typedb, flag->name);
callee = rz_analysis_function_name_guess(analysis->typedb, rz_flag_item_get_name(flag));
if (callee) {
const char *cc = rz_analysis_cc_func(analysis, callee);
if (cc && !strcmp(fcn->cc, cc)) {
Expand Down Expand Up @@ -1668,7 +1668,7 @@ RZ_API void rz_analysis_fcn_vars_cache_fini(RzAnalysisFcnVarsCache *cache) {
rz_list_free(cache->arg_vars);
}

static char *sig_from_debuginfo(RzAnalysis *analysis, RZ_NONNULL RzAnalysisFunction *fcn, char *fcn_name, const char *fcn_name_pre, const char *fcn_name_post) {
static char *sig_from_debuginfo(RzAnalysis *analysis, RZ_NONNULL RzAnalysisFunction *fcn, const char *fcn_name, const char *fcn_name_pre, const char *fcn_name_post) {
if (!fcn->has_debuginfo || !rz_str_startswith(fcn_name, "dbg."))
return NULL;

Expand All @@ -1690,7 +1690,7 @@ static char *sig_from_debuginfo(RzAnalysis *analysis, RZ_NONNULL RzAnalysisFunct
* \param fcn_name_post The suffix to use to highlight the function name
* \return {return}
*/
RZ_API char *rz_analysis_fcn_format_sig(RZ_NONNULL RzAnalysis *analysis, RZ_NONNULL RzAnalysisFunction *fcn, RZ_NULLABLE char *fcn_name,
RZ_API char *rz_analysis_fcn_format_sig(RZ_NONNULL RzAnalysis *analysis, RZ_NONNULL RzAnalysisFunction *fcn, RZ_NULLABLE const char *fcn_name,
RZ_NULLABLE RzAnalysisFcnVarsCache *reuse_cache, RZ_NULLABLE const char *fcn_name_pre, RZ_NULLABLE const char *fcn_name_post) {
fcn_name = !fcn_name ? fcn->name : fcn_name;
if (!fcn_name) {
Expand Down
4 changes: 2 additions & 2 deletions librz/arch/var_global.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void global_var_set_type(RzAnalysisVarGlobal *glob, RzType *type) {

RzFlagItem *flag = rz_analysis_var_global_get_flag_item(glob);
if (flag) {
flag->size = rz_type_db_get_bitsize(glob->analysis->typedb, glob->type) / 8;
rz_flag_item_set_size(flag, rz_type_db_get_bitsize(glob->analysis->typedb, glob->type) / 8);
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ RZ_API RZ_NULLABLE RzFlagItem *rz_analysis_var_global_get_flag_item(RzAnalysisVa
return NULL;
}
RzFlagItem *r = rz_flag_get(a->flb.f, glob->name);
if (r && r->offset != glob->addr) {
if (r && rz_flag_item_get_offset(r) != glob->addr) {
return NULL;
}
return r;
Expand Down
4 changes: 2 additions & 2 deletions librz/core/analysis_objc.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,7 @@ static bool flag_with_space_exists_at(RzCore *core, ut64 addr, RzSpace *space) {
RzListIter *it;
RzFlagItem *fi;
rz_list_foreach (existing, it, fi) {
if (fi->space && fi->space == space) {
if (rz_flag_item_get_space(fi) && rz_flag_item_get_space(fi) == space) {
// Do not create a flag if there is already a symbol (unstripped bin)
return true;
}
Expand All @@ -518,7 +518,7 @@ static void apply_selector_stub_at(RzCore *core, ut64 addr, ut32 size, char *sel
// If there is already a function (e.g. from aa), rename it too
RzAnalysisFunction *fcn = rz_analysis_get_function_at(core->analysis, addr);
if (fcn) {
rz_core_analysis_function_rename(core, addr, fi->name);
rz_core_analysis_function_rename(core, addr, rz_flag_item_get_name(fi));
}
}

Expand Down
9 changes: 5 additions & 4 deletions librz/core/analysis_tp.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ static void type_match(RzCore *core, char *fcn_name, ut64 addr, ut64 baddr, cons
RzFlagItem *f = rz_flag_get_by_spaces(core->flags, op->ptr, RZ_FLAGS_FS_STRINGS, NULL);
if (f) {
char formatstr[0x200];
int read = rz_io_nread_at(core->io, f->offset, (ut8 *)formatstr, RZ_MIN(sizeof(formatstr) - 1, f->size));
int read = rz_io_nread_at(core->io, rz_flag_item_get_offset(f), (ut8 *)formatstr, RZ_MIN(sizeof(formatstr) - 1, rz_flag_item_get_size(f)));
if (read > 0) {
formatstr[read] = '\0';
if ((types = parse_format(core, formatstr))) {
Expand Down Expand Up @@ -649,7 +649,7 @@ void propagate_types_among_used_variables(RzCore *core, HtUP *op_cache, RzAnalys
RzILTraceInstruction *cur_instr_trace = rz_analysis_esil_get_instruction_trace(etrace, ctx->cur_idx);

if (aop->type == RZ_ANALYSIS_OP_TYPE_CALL || aop->type & RZ_ANALYSIS_OP_TYPE_UCALL) {
char *full_name = NULL;
const char *full_name = NULL;
ut64 callee_addr;
if (aop->type == RZ_ANALYSIS_OP_TYPE_CALL) {
RzAnalysisFunction *fcn_call = rz_analysis_get_fcn_in(core->analysis, aop->jump, -1);
Expand All @@ -659,8 +659,9 @@ void propagate_types_among_used_variables(RzCore *core, HtUP *op_cache, RzAnalys
}
} else if (aop->ptr != UT64_MAX) {
RzFlagItem *flag = rz_flag_get_by_spaces(core->flags, aop->ptr, RZ_FLAGS_FS_IMPORTS, NULL);
if (flag && flag->realname) {
full_name = flag->realname;
const char *rn = flag ? rz_flag_item_get_realname(flag) : NULL;
if (rn) {
full_name = rn;
callee_addr = aop->ptr;
}
}
Expand Down
Loading

0 comments on commit 0bb2905

Please sign in to comment.