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

Convert RzList basic blocks to RzPVector #4067

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
5 changes: 3 additions & 2 deletions librz/analysis/analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,11 @@ RZ_API ut8 *rz_analysis_mask(RzAnalysis *analysis, ut32 size, const ut8 *data, u
RZ_API void rz_analysis_trace_bb(RzAnalysis *analysis, ut64 addr) {
RzAnalysisBlock *bbi;
XVilka marked this conversation as resolved.
Show resolved Hide resolved
RzAnalysisFunction *fcni;
RzListIter *iter2;
void **iter2;
fcni = rz_analysis_get_fcn_in(analysis, addr, 0);
if (fcni) {
rz_list_foreach (fcni->bbs, iter2, bbi) {
rz_pvector_foreach (fcni->bbs, iter2) {
bbi = *iter2;
if (addr >= bbi->addr && addr < (bbi->addr + bbi->size)) {
bbi->traced = true;
break;
Expand Down
5 changes: 3 additions & 2 deletions librz/analysis/block.c
Original file line number Diff line number Diff line change
Expand Up @@ -874,9 +874,10 @@ static bool automerge_predecessor_successor_cb(ut64 addr, void *user) {
static bool automerge_get_predecessors_cb(void *user, const ut64 k, const void *v) {
AutomergeCtx *ctx = user;
const RzAnalysisFunction *fcn = (const RzAnalysisFunction *)(size_t)k;
RzListIter *it;
void **it;
RzAnalysisBlock *block;
rz_list_foreach (fcn->bbs, it, block) {
rz_pvector_foreach (fcn->bbs, it) {
block = *it;
bool already_visited;
ht_up_find(ctx->visited_blocks, (ut64)(size_t)block, &already_visited);
if (already_visited) {
Expand Down
79 changes: 53 additions & 26 deletions librz/analysis/fcn.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ RZ_API void rz_analysis_fcn_invalidate_read_ahead_cache(void) {
RZ_API int rz_analysis_function_resize(RzAnalysisFunction *fcn, int newsize) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
RzAnalysis *analysis = fcn->analysis;
RzAnalysisBlock *bb;
RzListIter *iter, *iter2;
void **iter;

rz_return_val_if_fail(fcn, false);

Expand All @@ -105,7 +105,11 @@ RZ_API int rz_analysis_function_resize(RzAnalysisFunction *fcn, int newsize) {
}

ut64 eof = fcn->addr + newsize;
rz_list_foreach_safe (fcn->bbs, iter, iter2, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if(!bb) {
continue;
}
if (bb->addr >= eof) {
rz_analysis_function_remove_block(fcn, bb);
continue;
Expand Down Expand Up @@ -260,9 +264,10 @@ static bool is_delta_pointer_table(RzAnalysis *analysis, ut64 addr, ut64 lea_ptr

static ut64 try_get_cmpval_from_parents(RzAnalysis *analysis, RzAnalysisFunction *fcn, RzAnalysisBlock *my_bb, const char *cmp_reg) {
rz_return_val_if_fail(fcn && fcn->bbs && cmp_reg, UT64_MAX);
RzListIter *iter;
void **iter;
RzAnalysisBlock *tmp_bb;
rz_list_foreach (fcn->bbs, iter, tmp_bb) {
rz_pvector_foreach (fcn->bbs, iter) {
tmp_bb = *iter;
if (tmp_bb->jump == my_bb->addr || tmp_bb->fail == my_bb->addr) {
if (tmp_bb->cmpreg == cmp_reg) {
if (tmp_bb->cond) {
Expand Down Expand Up @@ -1767,10 +1772,11 @@ RZ_API bool rz_analysis_fcn_add_bb(RzAnalysis *a, RzAnalysisFunction *fcn, ut64
* \brief Returns the amount of loops located in the \p fcn function
*/
RZ_API int rz_analysis_function_loops(RzAnalysisFunction *fcn) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
ut32 loops = 0;
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if (bb->jump != UT64_MAX && bb->jump < bb->addr) {
loops++;
}
Expand All @@ -1796,10 +1802,11 @@ RZ_API int rz_analysis_function_loops(RzAnalysisFunction *fcn) {
RZ_API int rz_analysis_function_complexity(RzAnalysisFunction *fcn) {
RzAnalysis *analysis = fcn->analysis;
int E = 0, N = 0, P = 0;
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;

rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
N++; // nodes
if (!analysis && bb->jump == UT64_MAX && bb->fail != UT64_MAX) {
RZ_LOG_DEBUG("invalid bb jump/fail pair at 0x%08" PFMT64x " (fcn 0x%08" PFMT64x "\n", bb->addr, fcn->addr);
Expand Down Expand Up @@ -2034,9 +2041,10 @@ RZ_API RzAnalysisBlock *rz_analysis_fcn_bbget_in(const RzAnalysis *analysis, RzA
bool is_dalvik = !strncmp(analysis->cur->arch, "dalvik", 6);
can_jmpmid = analysis->opt.jmpmid && (is_dalvik || is_x86);
}
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if (addr >= bb->addr && addr < (bb->addr + bb->size) && (!can_jmpmid || rz_analysis_block_op_starts_at(bb, addr))) {
return bb;
}
Expand All @@ -2050,9 +2058,10 @@ RZ_API RzAnalysisBlock *rz_analysis_fcn_bbget_at(RzAnalysis *analysis, RzAnalysi
if (b) {
return b;
}
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if (addr == bb->addr) {
return bb;
}
Expand All @@ -2062,14 +2071,15 @@ RZ_API RzAnalysisBlock *rz_analysis_fcn_bbget_at(RzAnalysis *analysis, RzAnalysi

// compute the cyclomatic cost
RZ_API ut32 rz_analysis_function_cost(RzAnalysisFunction *fcn) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
ut32 totalCycles = 0;
if (!fcn) {
return 0;
}
RzAnalysis *analysis = fcn->analysis;
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
RzAnalysisOp op;
ut64 at, end = bb->addr + bb->size;
ut8 *buf = malloc(bb->size);
Expand All @@ -2096,13 +2106,14 @@ RZ_API ut32 rz_analysis_function_cost(RzAnalysisFunction *fcn) {

RZ_API int rz_analysis_function_count_edges(const RzAnalysisFunction *fcn, RZ_NULLABLE int *ebbs) {
rz_return_val_if_fail(fcn, 0);
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
int edges = 0;
if (ebbs) {
*ebbs = 0;
}
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
if (ebbs && bb->jump == UT64_MAX && bb->fail == UT64_MAX) {
*ebbs = *ebbs + 1;
} else {
Expand Down Expand Up @@ -2150,12 +2161,13 @@ static bool can_affect_bp(RzAnalysis *analysis, RzAnalysisOp *op) {
* and "pop bp" at the end).
*/
static void __analysis_fcn_check_bp_use(RzAnalysis *analysis, RzAnalysisFunction *fcn) {
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
if (!fcn) {
return;
}
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
RzAnalysisOp op;
ut64 at, end = bb->addr + bb->size;
ut8 *buf = malloc(bb->size);
Expand Down Expand Up @@ -2236,10 +2248,10 @@ static bool analize_addr_cb(ut64 addr, void *user) {
BlockRecurseCtx *ctx = user;
RzAnalysis *analysis = ctx->fcn->analysis;
RzAnalysisBlock *existing_bb = rz_analysis_get_block_at(analysis, addr);
if (!existing_bb || !rz_list_contains(ctx->fcn->bbs, existing_bb)) {
int old_len = rz_list_length(ctx->fcn->bbs);
if (!existing_bb || !rz_pvector_contains(ctx->fcn->bbs, existing_bb)) {
int old_len = rz_pvector_len(ctx->fcn->bbs);
analyze_function_locally(ctx->fcn->analysis, ctx->fcn, addr);
if (old_len != rz_list_length(ctx->fcn->bbs)) {
if (old_len != rz_pvector_len(ctx->fcn->bbs)) {
rz_analysis_block_recurse(rz_analysis_get_block_at(analysis, addr), mark_as_visited, user);
}
}
Expand Down Expand Up @@ -2313,7 +2325,8 @@ static void clear_bb_vars(RzAnalysisFunction *fcn, RzAnalysisBlock *bb, ut64 fro
}

static void update_analysis(RzAnalysis *analysis, RzList /*<RzAnalysisFunction *>*/ *fcns, HtUP *reachable) {
RzListIter *it, *it2, *tmp;
RzListIter *it;
void **it2;
RzAnalysisFunction *fcn;
bool old_jmpmid = analysis->opt.jmpmid;
analysis->opt.jmpmid = true;
Expand All @@ -2335,7 +2348,8 @@ static void update_analysis(RzAnalysis *analysis, RzList /*<RzAnalysisFunction *
rz_analysis_block_recurse(bb, analize_descendents, &ctx);

// Remove non-reachable blocks
rz_list_foreach_safe (fcn->bbs, it2, tmp, bb) {
rz_pvector_foreach (fcn->bbs, it2) {
bb = *it2;
if (ht_up_find_kv(ht, bb->addr, NULL)) {
continue;
}
Expand All @@ -2347,8 +2361,19 @@ static void update_analysis(RzAnalysis *analysis, RzList /*<RzAnalysisFunction *
fcn->ninstr -= bb->ninstr;
rz_analysis_function_remove_block(fcn, bb);
}
RzList *bbs_temp = rz_list_new();
if (!bbs_temp) {
// If memory allocation failed, print an error message and return from the function
eprintf("Failed to allocate memory for bbs.\n");
return;
}

void **it;
rz_pvector_foreach (fcn->bbs, it) {
rz_list_append(bbs_temp, *it);
}

RzList *bbs = rz_list_clone(fcn->bbs);
RzList *bbs = rz_list_clone(bbs_temp);
XVilka marked this conversation as resolved.
Show resolved Hide resolved
rz_analysis_block_automerge(bbs);
rz_analysis_function_delete_unused_vars(fcn);
rz_list_free(bbs);
Expand Down Expand Up @@ -2412,12 +2437,14 @@ RZ_API void rz_analysis_update_analysis_range(RzAnalysis *analysis, ut64 addr, i

RZ_API void rz_analysis_function_update_analysis(RzAnalysisFunction *fcn) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
rz_return_if_fail(fcn);
RzListIter *it, *it2, *tmp, *tmp2;
void **it;
RzListIter *it2, *tmp2;
RzAnalysisBlock *bb;
RzAnalysisFunction *f;
RzList *fcns = rz_list_new();
HtUP *reachable = ht_up_new(NULL, free_ht_up, NULL);
rz_list_foreach_safe (fcn->bbs, it, tmp, bb) {
rz_pvector_foreach (fcn->bbs, it) {
bb = *it;
if (rz_analysis_block_was_modified(bb)) {
rz_list_foreach_safe (bb->fcns, it2, tmp2, f) {
calc_reachable_and_remove_block(fcns, f, bb, reachable);
Expand Down
28 changes: 16 additions & 12 deletions librz/analysis/function.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ RZ_API RzAnalysisFunction *rz_analysis_function_new(RzAnalysis *analysis) {
fcn->addr = UT64_MAX;
fcn->cc = rz_str_constpool_get(&analysis->constpool, rz_analysis_cc_default(analysis));
fcn->bits = analysis->bits;
fcn->bbs = rz_list_new();
fcn->bbs = rz_pvector_new(NULL);
fcn->has_changed = true;
fcn->bp_frame = true;
fcn->is_noreturn = false;
Expand All @@ -93,12 +93,13 @@ RZ_API void rz_analysis_function_free(void *_fcn) {
}

RzAnalysisBlock *block;
RzListIter *iter;
rz_list_foreach (fcn->bbs, iter, block) {
void **iter;
rz_pvector_foreach (fcn->bbs, iter) {
block = *iter;
rz_list_delete_data(block->fcns, fcn);
rz_analysis_block_unref(block);
}
rz_list_free(fcn->bbs);
rz_pvector_free(fcn->bbs);

RzAnalysis *analysis = fcn->analysis;
if (ht_up_find(analysis->ht_addr_fun, fcn->addr, NULL) == _fcn) {
Expand Down Expand Up @@ -259,7 +260,7 @@ RZ_API void rz_analysis_function_add_block(RzAnalysisFunction *fcn, RzAnalysisBl
}
rz_list_append(bb->fcns, fcn); // associate the given fcn with this bb
rz_analysis_block_ref(bb);
rz_list_append(fcn->bbs, bb);
rz_pvector_push(fcn->bbs, bb);

if (fcn->meta._min != UT64_MAX) {
if (bb->addr + bb->size > fcn->meta._max) {
Expand All @@ -283,7 +284,7 @@ RZ_API void rz_analysis_function_remove_block(RzAnalysisFunction *fcn, RzAnalysi
fcn->meta._min = UT64_MAX;
}

rz_list_delete_data(fcn->bbs, bb);
rz_pvector_remove_data(fcn->bbs, bb);
rz_analysis_block_unref(bb);
}

Expand All @@ -294,8 +295,9 @@ static void ensure_fcn_range(RzAnalysisFunction *fcn) {
ut64 minval = UT64_MAX;
ut64 maxval = UT64_MIN;
RzAnalysisBlock *block;
RzListIter *iter;
rz_list_foreach (fcn->bbs, iter, block) {
void **iter;
rz_pvector_foreach (fcn->bbs, iter) {
block = *iter;
if (block->addr < minval) {
minval = block->addr;
}
Expand Down Expand Up @@ -328,11 +330,12 @@ RZ_API ut64 rz_analysis_function_size_from_entry(RzAnalysisFunction *fcn) {
}

RZ_API ut64 rz_analysis_function_realsize(const RzAnalysisFunction *fcn) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
RzListIter *iter;
void **iter;
RzAnalysisBlock *bb;
ut64 sz = 0;
if (!sz) {
rz_list_foreach (fcn->bbs, iter, bb) {
rz_pvector_foreach (fcn->bbs, iter) {
bb = *iter;
sz += bb->size;
}
}
Expand All @@ -357,9 +360,10 @@ RZ_API bool rz_analysis_function_contains(RzAnalysisFunction *fcn, ut64 addr) {

RZ_API bool rz_analysis_function_was_modified(RzAnalysisFunction *fcn) {
XVilka marked this conversation as resolved.
Show resolved Hide resolved
rz_return_val_if_fail(fcn, false);
RzListIter *it;
void **it;
RzAnalysisBlock *bb;
rz_list_foreach (fcn->bbs, it, bb) {
rz_pvector_foreach (fcn->bbs, it) {
bb = *it;
if (rz_analysis_block_was_modified(bb)) {
return true;
}
Expand Down
5 changes: 3 additions & 2 deletions librz/analysis/jmptbl.c
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ RZ_API bool rz_analysis_get_jmptbl_info(RZ_NONNULL RzAnalysis *analysis, RZ_NONN
rz_return_val_if_fail(analysis && fcn && params && block, false);
bool isValid = false;
int i;
RzListIter *iter;
void **iter;
RzAnalysisBlock *tmp_bb, *prev_bb;
prev_bb = 0;
if (!fcn->bbs) {
Expand All @@ -468,7 +468,8 @@ RZ_API bool rz_analysis_get_jmptbl_info(RZ_NONNULL RzAnalysis *analysis, RZ_NONN
}

// search for the predecessor bb
rz_list_foreach (fcn->bbs, iter, tmp_bb) {
rz_pvector_foreach (fcn->bbs, iter) {
tmp_bb = *iter;
if (tmp_bb->jump == block->addr || tmp_bb->fail == block->addr) {
prev_bb = tmp_bb;
break;
Expand Down
4 changes: 3 additions & 1 deletion librz/analysis/serialize_analysis.c
Original file line number Diff line number Diff line change
Expand Up @@ -1110,8 +1110,10 @@ static void function_store(RZ_NONNULL Sdb *db, const char *key, RzAnalysisFuncti

pj_ka(j, "bbs");
RzListIter *it;
void **iter;
RzAnalysisBlock *block;
rz_list_foreach (function->bbs, it, block) {
rz_pvector_foreach (function->bbs, iter) {
block = *iter;
pj_n(j, block->addr);
}
pj_end(j);
Expand Down
Loading
Loading