Skip to content

Commit

Permalink
Extract buffer read and analyze into function.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rot127 committed Feb 13, 2024
1 parent dce6ac8 commit 26a8270
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions librz/core/cgraph.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,30 @@ static bool add_edge_to_cfg(RZ_NONNULL RzGraph /*<RzGraphNodeInfo *>*/ *graph,
return true;
}

/**
* \brief Reads \p buf_len bytes into \p buf and passes it into rz_analysis_op() for decoding.
*
* \param core The current RzCore.
* \param op The RzAnalysisOp to write the decoded operation into.
* \param addr The address where to read and decode at.
* \param buf The buffer to read the bytes at \p addr into.
* \param buf_len The buffer length in bytes. Must be greater than 0.
*
* \return true On success.
* \return false On failure.
*/
static bool read_buf_and_analyze(RZ_NONNULL const RzCore *core, RZ_NONNULL RZ_BORROW RzAnalysisOp *op, ut64 addr, RZ_NONNULL RZ_OUT ut8 *buf, size_t buf_len) {
rz_return_val_if_fail(core && core->io && core->analysis && op && buf && buf_len > 0, false);
if (rz_io_nread_at(core->io, addr, buf, buf_len) < 0) {
RZ_LOG_ERROR("rz_io_nread_at() failed to read at 0x%" PFMT64x ".\n", addr);
return false;
}
if (rz_analysis_op(core->analysis, op, addr, buf, buf_len, RZ_ANALYSIS_OP_MASK_DISASM) <= 0) {
return false;
}
return true;
}

/**
* \brief Get the procedual control flow graph (CFG) at an address.
* Calls are not followed.
Expand Down Expand Up @@ -1052,20 +1076,19 @@ RZ_API RZ_OWN RzGraph /*<RzGraphNodeInfo *>*/ *rz_core_graph_cfg(RZ_NONNULL RzCo
ut64 cur_addr = 0;
rz_vector_pop(to_visit, &cur_addr);

if (rz_io_nread_at(core->io, cur_addr, buf, sizeof(buf)) < 0) {
RZ_LOG_ERROR("Could not generate CFG at 0x%" PFMT64x ". rz_io_nread_at() failed at 0x%" PFMT64x ".\n", addr, cur_addr);
if (!read_buf_and_analyze(core, &curr_op, cur_addr, buf, sizeof(buf))) {
rz_analysis_op_fini(&curr_op);
goto error;
}

disas_bytes = rz_analysis_op(core->analysis, &curr_op, cur_addr, buf, sizeof(buf), RZ_ANALYSIS_OP_MASK_DISASM);
if (disas_bytes <= 0 || is_leaf_op(&curr_op)) {
if (is_leaf_op(&curr_op)) {
// A leaf. It was added before to the graph by the parent node.
rz_analysis_op_fini(&curr_op);
continue;
}

if (curr_op.jump != UT64_MAX && !is_call(&curr_op)) {
if (rz_analysis_op(core->analysis, &target_op, curr_op.jump, buf, sizeof(buf), RZ_ANALYSIS_OP_MASK_DISASM) <= 0) {
if (!read_buf_and_analyze(core, &target_op, curr_op.jump, buf, sizeof(buf))) {
rz_analysis_op_fini(&target_op);
goto error;
}
Expand All @@ -1075,7 +1098,7 @@ RZ_API RZ_OWN RzGraph /*<RzGraphNodeInfo *>*/ *rz_core_graph_cfg(RZ_NONNULL RzCo
rz_analysis_op_fini(&target_op);
}
if (curr_op.fail != UT64_MAX && !is_call(&curr_op)) {
if (rz_analysis_op(core->analysis, &target_op, curr_op.fail, buf, sizeof(buf), RZ_ANALYSIS_OP_MASK_DISASM) <= 0) {
if (!read_buf_and_analyze(core, &target_op, curr_op.fail, buf, sizeof(buf))) {
rz_analysis_op_fini(&target_op);
goto error;
}
Expand All @@ -1092,11 +1115,7 @@ RZ_API RZ_OWN RzGraph /*<RzGraphNodeInfo *>*/ *rz_core_graph_cfg(RZ_NONNULL RzCo

// Add next instruction
ut64 next_addr = cur_addr + disas_bytes;
if (rz_io_nread_at(core->io, next_addr, buf, sizeof(buf)) < 0) {
RZ_LOG_ERROR("Could not generate CFG at 0x%" PFMT64x ". rz_io_nread_at() failed at 0x%" PFMT64x ".\n", addr, cur_addr);
goto error;
}
if (rz_analysis_op(core->analysis, &target_op, next_addr, buf, sizeof(buf), RZ_ANALYSIS_OP_MASK_DISASM) <= 0) {
if (!read_buf_and_analyze(core, &target_op, next_addr, buf, sizeof(buf))) {
rz_analysis_op_fini(&target_op);
goto error;
}
Expand Down

0 comments on commit 26a8270

Please sign in to comment.