From 26a8270955ba60d49a964142d0929bb2ea051f28 Mon Sep 17 00:00:00 2001 From: Rot127 Date: Tue, 13 Feb 2024 02:26:09 -0500 Subject: [PATCH] Extract buffer read and analyze into function. --- librz/core/cgraph.c | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/librz/core/cgraph.c b/librz/core/cgraph.c index 1615f0d4d29..9986727311e 100644 --- a/librz/core/cgraph.c +++ b/librz/core/cgraph.c @@ -1014,6 +1014,30 @@ static bool add_edge_to_cfg(RZ_NONNULL RzGraph /**/ *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. @@ -1052,20 +1076,19 @@ RZ_API RZ_OWN RzGraph /**/ *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; } @@ -1075,7 +1098,7 @@ RZ_API RZ_OWN RzGraph /**/ *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; } @@ -1092,11 +1115,7 @@ RZ_API RZ_OWN RzGraph /**/ *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; }