Rizin
unix-like reverse engineering framework and cli tools
il_trace.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Heersin <teablearcher@gmail.com>
2 // SPDX-License-Identifier: LGPL-3.0-only
3 
16 #include <rz_util.h>
17 #include <rz_analysis.h>
18 
25  RzILTraceInstruction *instruction_trace = RZ_NEW0(RzILTraceInstruction);
26  if (!instruction_trace) {
27  RZ_LOG_ERROR("Cannot create instruction trace\n");
28  return NULL;
29  }
30 
31  instruction_trace->addr = addr;
32 
33  instruction_trace->read_mem_ops = rz_pvector_new((RzPVectorFree)free);
34  instruction_trace->read_reg_ops = rz_pvector_new((RzPVectorFree)free);
35  instruction_trace->write_mem_ops = rz_pvector_new((RzPVectorFree)free);
36  instruction_trace->write_reg_ops = rz_pvector_new((RzPVectorFree)free);
37 
38  if (!(instruction_trace->read_reg_ops &&
39  instruction_trace->read_mem_ops &&
40  instruction_trace->write_reg_ops &&
41  instruction_trace->write_mem_ops)) {
42  rz_analysis_il_trace_instruction_free(instruction_trace);
43  return NULL;
44  }
45 
46  return instruction_trace;
47 }
48 
54  if (!instruction) {
55  return;
56  }
57  rz_pvector_free(instruction->write_reg_ops);
58  rz_pvector_free(instruction->read_reg_ops);
59  rz_pvector_free(instruction->write_mem_ops);
60  rz_pvector_free(instruction->read_mem_ops);
62 }
63 
70 RZ_API bool rz_analysis_il_trace_add_mem(RzILTraceInstruction *trace, RzILTraceMemOp *mem) {
71  if (!(trace && mem)) {
72  return false;
73  }
74 
75  if (rz_analysis_il_mem_trace_contains(trace, mem->addr, mem->behavior)) {
76  return false;
77  }
78 
79  bool ret = false;
80  switch (mem->behavior) {
81  case RZ_IL_TRACE_OP_WRITE:
82  ret = !!rz_pvector_push(trace->write_mem_ops, mem);
83  trace->stats |= RZ_IL_TRACE_INS_HAS_MEM_W;
84  break;
85  case RZ_IL_TRACE_OP_READ:
86  ret = !!rz_pvector_push(trace->read_mem_ops, mem);
87  trace->stats |= RZ_IL_TRACE_INS_HAS_MEM_R;
88  break;
89  default:
91  break;
92  }
93  return ret;
94 }
95 
102 RZ_API bool rz_analysis_il_trace_add_reg(RzILTraceInstruction *trace, RzILTraceRegOp *reg) {
103  if (!(trace && reg)) {
104  return false;
105  }
106 
107  if (rz_analysis_il_reg_trace_contains(trace, reg->reg_name, reg->behavior)) {
108  return false;
109  }
110 
111  bool ret = false;
112  switch (reg->behavior) {
113  case RZ_IL_TRACE_OP_WRITE:
114  ret = !!rz_pvector_push(trace->write_reg_ops, reg);
115  trace->stats |= RZ_IL_TRACE_INS_HAS_REG_W;
116  break;
117  case RZ_IL_TRACE_OP_READ:
118  ret = !!rz_pvector_push(trace->read_reg_ops, reg);
119  trace->stats |= RZ_IL_TRACE_INS_HAS_REG_R;
120  break;
121  default:
123  break;
124  }
125  return ret;
126 }
127 
135 RZ_API RzILTraceMemOp *rz_analysis_il_get_mem_op_trace(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type) {
136  if (!trace) {
137  return NULL;
138  }
139 
140  RzPVector *mem_ops;
141  RzILTraceMemOp *mem_op;
142  switch (op_type) {
143  case RZ_IL_TRACE_OP_WRITE:
144  mem_ops = trace->write_mem_ops;
145  break;
146  case RZ_IL_TRACE_OP_READ:
147  mem_ops = trace->read_mem_ops;
148  break;
149  default:
151  return NULL;
152  }
153 
154  void **iter;
155  rz_pvector_foreach (mem_ops, iter) {
156  mem_op = *iter;
157  if (mem_op->addr == addr) {
158  return mem_op;
159  }
160  }
161 
162  return NULL;
163 }
164 
172 RZ_API RzILTraceRegOp *rz_analysis_il_get_reg_op_trace(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type) {
173  if (!(trace && regname)) {
174  return NULL;
175  }
176 
177  RzPVector *reg_ops;
178  RzILTraceRegOp *reg_op;
179  switch (op_type) {
180  case RZ_IL_TRACE_OP_WRITE:
181  reg_ops = trace->write_reg_ops;
182  break;
183  case RZ_IL_TRACE_OP_READ:
184  reg_ops = trace->read_reg_ops;
185  break;
186  default:
188  return NULL;
189  }
190 
191  void **iter;
192  rz_pvector_foreach (reg_ops, iter) {
193  reg_op = *iter;
194  if (strcmp(reg_op->reg_name, regname) == 0) {
195  return reg_op;
196  }
197  }
198 
199  return NULL;
200 }
201 
209 RZ_API bool rz_analysis_il_mem_trace_contains(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type) {
210  return rz_analysis_il_get_mem_op_trace(trace, addr, op_type) ? true : false;
211 }
212 
220 RZ_API bool rz_analysis_il_reg_trace_contains(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type) {
221  return rz_analysis_il_get_reg_op_trace(trace, regname, op_type) ? true : false;
222 }
#define RZ_API
#define NULL
Definition: cris-opc.c:27
#define true
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API RzILTraceMemOp * rz_analysis_il_get_mem_op_trace(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
Definition: il_trace.c:135
RZ_API bool rz_analysis_il_mem_trace_contains(RzILTraceInstruction *trace, ut64 addr, RzILTraceOpType op_type)
Definition: il_trace.c:209
RZ_API RzILTraceInstruction * rz_analysis_il_trace_instruction_new(ut64 addr)
Definition: il_trace.c:24
RZ_API bool rz_analysis_il_trace_add_mem(RzILTraceInstruction *trace, RzILTraceMemOp *mem)
Definition: il_trace.c:70
RZ_API bool rz_analysis_il_reg_trace_contains(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
Definition: il_trace.c:220
RZ_API void rz_analysis_il_trace_instruction_free(RzILTraceInstruction *instruction)
Definition: il_trace.c:53
RZ_API bool rz_analysis_il_trace_add_reg(RzILTraceInstruction *trace, RzILTraceRegOp *reg)
Definition: il_trace.c:102
RZ_API RzILTraceRegOp * rz_analysis_il_get_reg_op_trace(RzILTraceInstruction *trace, const char *regname, RzILTraceOpType op_type)
Definition: il_trace.c:172
#define reg(n)
void * mem
Definition: libc.cpp:91
#define rz_warn_if_reached()
Definition: rz_assert.h:29
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
#define RZ_NEW0(x)
Definition: rz_types.h:284
RZ_API RzPVector * rz_pvector_new(RzPVectorFree free)
Definition: vector.c:302
void(* RzPVectorFree)(void *e)
Definition: rz_vector.h:43
static void ** rz_pvector_push(RzPVector *vec, void *x)
Definition: rz_vector.h:300
RZ_API void rz_pvector_free(RzPVector *vec)
Definition: vector.c:336
#define rz_pvector_foreach(vec, it)
Definition: rz_vector.h:334
ut64(WINAPI *w32_GetEnabledXStateFeatures)()
static char * regname(int reg)
Definition: dis.c:71
static int addr
Definition: z80asm.c:58