Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Mark the bytes that is referenced as data and not belonging to any fu… #3804

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions librz/core/canalysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -2075,6 +2075,55 @@ RZ_API void rz_core_analysis_resolve_jumps(RZ_NONNULL RzCore *core) {
rz_list_free(xrefs);
}

/**
* \brief Mark the bytes that are referenced and don't belong
* to any functions as data.
*/
static void core_analysis_referenced_data(RzCore *core) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a command to run this analysis manually. (subcommand under aa).

RzList *xrefs = rz_analysis_xrefs_list(core->analysis);
RzListIter *it, *it1;
RzAnalysisXRef *x, *x1;
rz_list_foreach (xrefs, it, x) {
if (x->type != RZ_ANALYSIS_XREF_TYPE_DATA) {
continue;
}
ut64 to = x->to;

// the location doesn't belong to any function
RzList *funcs = rz_analysis_get_functions_in(core->analysis, to);
if (!rz_list_empty(funcs)) {
rz_list_free(funcs);
continue;
}
rz_list_free(funcs);

// the location is only referenced as DATA instead of CODE
RzList *to_xrefs = rz_analysis_xrefs_get_to(core->analysis, to);
bool has_nondata_ref = false;
rz_list_foreach (to_xrefs, it1, x1) {
if (x1->type != RZ_ANALYSIS_XREF_TYPE_DATA) {
has_nondata_ref = true;
}
}
rz_list_free(to_xrefs);
if (has_nondata_ref) {
continue;
}

RzAsmOp asmop;
const int max_opsz = rz_analysis_archinfo(core->analysis, RZ_ANALYSIS_ARCHINFO_MAX_OP_SIZE);
ut8 *buf = malloc(max_opsz);
rz_io_read_at(core->io, to, buf, max_opsz);
int opsz = rz_asm_disassemble(core->rasm, &asmop, buf, max_opsz);
free(buf);
if (opsz < 1 || strcmp("invalid", rz_strbuf_get(&asmop.buf_asm))) {
continue;
}
rz_meta_set_data_at(core->analysis, to, asmop.size);
rz_asm_op_fini(&asmop);
}
}

/**
* \brief Analyze xrefs and prints the result.
*
Expand Down Expand Up @@ -4904,6 +4953,7 @@ RZ_API bool rz_core_analysis_everything(RzCore *core, bool experimental, char *d
rz_analysis_add_device_peripheral_map(core->bin->cur->o, core->analysis);
}

core_analysis_referenced_data(core);
core_analysis_using_plugins(core);
return true;
}
Expand Down
Loading