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

Remove static and global varibles in esil_trace.c #4066

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
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
37 changes: 18 additions & 19 deletions librz/analysis/esil/esil_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
#define CMP_REG_CHANGE(x, y) ((x) - ((RzAnalysisEsilRegChange *)(y))->idx)
#define CMP_MEM_CHANGE(x, y) ((x) - ((RzAnalysisEsilMemChange *)(y))->idx)

static int ocbs_set = false;
static RzAnalysisEsilCallbacks ocbs = { 0 };
#define ESILISTATE esil->analysis->esilinterstate

// IL trace wrapper of esil
static inline bool esil_add_mem_trace(RzAnalysisEsilTrace *etrace, RzILTraceMemOp *mem) {
Expand Down Expand Up @@ -127,10 +126,10 @@ static int trace_hook_reg_read(RzAnalysisEsil *esil, const char *name, ut64 *res
// RZ_LOG_WARN("Register not found in profile\n");
return 0;
}
if (ocbs.hook_reg_read) {
if (ESILISTATE->callbacks.hook_reg_read) {
RzAnalysisEsilCallbacks cbs = esil->cb;
esil->cb = ocbs;
ret = ocbs.hook_reg_read(esil, name, res, size);
esil->cb = ESILISTATE->callbacks;
ret = ESILISTATE->callbacks.hook_reg_read(esil, name, res, size);
esil->cb = cbs;
}
if (!ret && esil->cb.reg_read) {
Expand Down Expand Up @@ -171,10 +170,10 @@ static int trace_hook_reg_write(RzAnalysisEsil *esil, const char *name, ut64 *va

RzRegItem *ri = rz_reg_get(esil->analysis->reg, name, -1);
add_reg_change(esil->trace, esil->trace->idx + 1, ri, *val);
if (ocbs.hook_reg_write) {
if (ESILISTATE->callbacks.hook_reg_write) {
RzAnalysisEsilCallbacks cbs = esil->cb;
esil->cb = ocbs;
ret = ocbs.hook_reg_write(esil, name, val);
esil->cb = ESILISTATE->callbacks;
ret = ESILISTATE->callbacks.hook_reg_write(esil, name, val);
esil->cb = cbs;
}
return ret;
Expand Down Expand Up @@ -207,10 +206,10 @@ static int trace_hook_mem_read(RzAnalysisEsil *esil, ut64 addr, ut8 *buf, int le
RZ_FREE(mem_read);
}

if (ocbs.hook_mem_read) {
if (ESILISTATE->callbacks.hook_mem_read) {
RzAnalysisEsilCallbacks cbs = esil->cb;
esil->cb = ocbs;
ret = ocbs.hook_mem_read(esil, addr, buf, len);
esil->cb = ESILISTATE->callbacks;
ret = ESILISTATE->callbacks.hook_mem_read(esil, addr, buf, len);
esil->cb = cbs;
}
return ret;
Expand Down Expand Up @@ -245,10 +244,10 @@ static int trace_hook_mem_write(RzAnalysisEsil *esil, ut64 addr, const ut8 *buf,
add_mem_change(esil->trace, esil->trace->idx + 1, addr + i, buf[i]);
}

if (ocbs.hook_mem_write) {
if (ESILISTATE->callbacks.hook_mem_write) {
RzAnalysisEsilCallbacks cbs = esil->cb;
esil->cb = ocbs;
ret = ocbs.hook_mem_write(esil, addr, buf, len);
esil->cb = ESILISTATE->callbacks;
ret = ESILISTATE->callbacks.hook_mem_write(esil, addr, buf, len);
esil->cb = cbs;
}
return ret;
Expand Down Expand Up @@ -288,11 +287,11 @@ RZ_API void rz_analysis_esil_trace_op(RzAnalysisEsil *esil, RZ_NONNULL RzAnalysi
}
/* save old callbacks */
int esil_verbose = esil->verbose;
if (ocbs_set) {
if (ESILISTATE->callbacks_set) {
RZ_LOG_ERROR("esil: Cannot call recursively\n");
}
ocbs = esil->cb;
ocbs_set = true;
ESILISTATE->callbacks = esil->cb;
ESILISTATE->callbacks_set = true;

RzILTraceInstruction *instruction = rz_analysis_il_trace_instruction_new(op->addr);
rz_pvector_push(esil->trace->instructions, instruction);
Expand All @@ -310,8 +309,8 @@ RZ_API void rz_analysis_esil_trace_op(RzAnalysisEsil *esil, RZ_NONNULL RzAnalysi
rz_analysis_esil_parse(esil, expr);
rz_analysis_esil_stack_free(esil);
/* restore hooks */
esil->cb = ocbs;
ocbs_set = false;
esil->cb = ESILISTATE->callbacks;
ESILISTATE->callbacks_set = false;
esil->verbose = esil_verbose;
/* increment idx */
esil->trace->idx++;
Expand Down
2 changes: 2 additions & 0 deletions librz/include/rz_analysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,8 @@ typedef struct rz_analysis_esil_inter_state_t {
ut64 emustack_max;
RzList /*<RzAnalysisEsilMemoryRegion *>*/ *memreads;
RzList /*<RzAnalysisEsilMemoryRegion *>*/ *memwrites;
RzAnalysisEsilCallbacks callbacks;
bool callbacks_set;
} RzAnalysisEsilInterState;

/* Alias RegChange and MemChange */
Expand Down
Loading