Rizin
unix-like reverse engineering framework and cli tools
theory_effect.c File Reference

Go to the source code of this file.

Functions

static RzILEventil_event_new_write_from_var (RzILVM *vm, RzILVar *var, RzILVal *new_val)
 
static void rz_il_set (RzILVM *vm, const char *var_name, bool is_local, RZ_OWN RzILVal *val)
 
bool rz_il_handler_empty (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_nop (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_set (RzILVM *vm, RzILOpEffect *op)
 
static void perform_jump (RzILVM *vm, RZ_OWN RzBitVector *dst)
 
bool rz_il_handler_jmp (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_goto (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_seq (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_blk (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_repeat (RzILVM *vm, RzILOpEffect *op)
 
bool rz_il_handler_branch (RzILVM *vm, RzILOpEffect *op)
 

Function Documentation

◆ il_event_new_write_from_var()

static RzILEvent* il_event_new_write_from_var ( RzILVM vm,
RzILVar var,
RzILVal new_val 
)
static

Definition at line 8 of file theory_effect.c.

8  {
9  rz_return_val_if_fail(vm && var && new_val, NULL);
11  if (!old_val) {
12  return NULL;
13  }
14  return rz_il_event_var_write_new(var->name, old_val, new_val);
15 }
#define NULL
Definition: cris-opc.c:27
RZ_API RZ_OWN RzILEvent * rz_il_event_var_write_new(RZ_NONNULL const char *name, RZ_NULLABLE const RzILVal *old_v, RZ_NONNULL const RzILVal *new_v)
Definition: il_events.c:177
RZ_API RZ_BORROW RzILVal * rz_il_vm_get_var_value(RZ_NONNULL RzILVM *vm, RzILVarKind kind, const char *name)
Definition: il_vm.c:264
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
char * name
Definition: variable.h:20
@ RZ_IL_VAR_KIND_GLOBAL
global var, usually bound to a physical representation like a register.
Definition: variable.h:47

References rz_il_var_t::name, NULL, rz_il_event_var_write_new(), RZ_IL_VAR_KIND_GLOBAL, rz_il_vm_get_var_value(), and rz_return_val_if_fail.

Referenced by rz_il_set().

◆ perform_jump()

static void perform_jump ( RzILVM vm,
RZ_OWN RzBitVector dst 
)
static

Definition at line 50 of file theory_effect.c.

50  {
52  rz_bv_free(vm->pc);
53  vm->pc = dst;
54 }
RZ_API RZ_OWN RzILEvent * rz_il_event_pc_write_new(RZ_NONNULL const RzBitVector *old_pc, RZ_NONNULL const RzBitVector *new_pc)
Definition: il_events.c:76
RZ_API void rz_il_vm_event_add(RzILVM *vm, RzILEvent *evt)
Definition: il_vm_eval.c:201
char * dst
Definition: lz4.h:724
RZ_API void rz_bv_free(RZ_NULLABLE RzBitVector *bv)
Definition: bitvector.c:85
RzBitVector * pc
Program Counter of VM.
Definition: rz_il_vm.h:46

References dst, rz_il_vm_t::pc, rz_bv_free(), rz_il_event_pc_write_new(), and rz_il_vm_event_add().

Referenced by rz_il_handler_goto(), and rz_il_handler_jmp().

◆ rz_il_handler_blk()

bool rz_il_handler_blk ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 89 of file theory_effect.c.

89  {
90  rz_return_val_if_fail(vm && op, false);
91 
92  RzILOpArgsBlk *op_blk = &op->op.blk;
93  if (op_blk->label) {
94  rz_il_vm_create_label(vm, op_blk->label, vm->pc); // create the label if `blk` is labelled
95  }
96 
97  return rz_il_evaluate_effect(vm, op_blk->data_eff) && rz_il_evaluate_effect(vm, op_blk->ctrl_eff);
98 }
RZ_API RZ_BORROW RzILEffectLabel * rz_il_vm_create_label(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *name, RZ_NONNULL RZ_BORROW RzBitVector *addr)
Definition: il_vm.c:308
RZ_API bool rz_il_evaluate_effect(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpEffect *op)
Definition: il_vm_eval.c:341
op structure for blk (label -> data eff -> ctrl eff -> unit eff)
RzILOpEffect * data_eff
index of data_eff
const char * label
name of the label, const one
RzILOpEffect * ctrl_eff
index of ctrl_eff
Definition: dis.c:32

References rzil_op_blk_t::ctrl_eff, rzil_op_blk_t::data_eff, rzil_op_blk_t::label, rz_il_vm_t::pc, rz_il_evaluate_effect(), rz_il_vm_create_label(), and rz_return_val_if_fail.

◆ rz_il_handler_branch()

bool rz_il_handler_branch ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 118 of file theory_effect.c.

118  {
119  rz_return_val_if_fail(vm && op, false);
120 
121  RzILOpArgsBranch *op_branch = &op->op.branch;
122 
123  RzILBool *condition = rz_il_evaluate_bool(vm, op_branch->condition);
124  if (!condition) {
125  return false;
126  }
127  bool ret;
128  if (condition->b) {
129  ret = rz_il_evaluate_effect(vm, op_branch->true_eff);
130  } else {
131  ret = rz_il_evaluate_effect(vm, op_branch->false_eff);
132  }
133  rz_il_bool_free(condition);
134 
135  return ret;
136 }
RZ_API void rz_il_bool_free(RzILBool *bool_var)
Definition: bool.c:74
RZ_API RZ_NULLABLE RZ_OWN RzILBool * rz_il_evaluate_bool(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpBool *op)
Definition: il_vm_eval.c:289
bool b
Definition: bool.h:15
op structure for branch (bool -> 'a eff -> 'a eff -> 'a eff)
RZ_NONNULL RzILOpEffect * true_eff
effect for when condition evaluates to true
RZ_NONNULL RzILOpEffect * false_eff
effect for when condition evaluates to false

References rz_il_bool_t::b, rz_il_op_args_branch_t::condition, rz_il_op_args_branch_t::false_eff, rz_il_bool_free(), rz_il_evaluate_bool(), rz_il_evaluate_effect(), rz_return_val_if_fail, and rz_il_op_args_branch_t::true_eff.

◆ rz_il_handler_empty()

bool rz_il_handler_empty ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 28 of file theory_effect.c.

28  {
29  rz_return_val_if_fail(vm && op, false);
30  RZ_LOG_WARN("Encountered an empty instruction at %s", rz_bv_as_string(vm->pc))
31  return true;
32 }
#define true
RZ_API RZ_OWN char * rz_bv_as_string(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:98
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56

References rz_il_vm_t::pc, rz_bv_as_string(), RZ_LOG_WARN, and rz_return_val_if_fail.

◆ rz_il_handler_goto()

bool rz_il_handler_goto ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 66 of file theory_effect.c.

66  {
67  rz_return_val_if_fail(vm && op, false);
68  RzILOpArgsGoto *op_goto = &op->op.goto_;
69  const char *lname = op_goto->lbl;
71  if (!label) {
72  return false;
73  }
74  if (label->type == EFFECT_LABEL_SYSCALL || label->type == EFFECT_LABEL_HOOK) {
75  RzILVmHook internal_hook = (RzILVmHook)label->hook;
76  internal_hook(vm, op);
77  } else {
78  perform_jump(vm, rz_bv_dup(label->addr));
79  }
80  return true;
81 }
RZ_API RZ_BORROW RzILEffectLabel * rz_il_vm_find_label_by_name(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *lbl_name)
Definition: il_vm.c:291
@ EFFECT_LABEL_HOOK
Definition: label.h:20
@ EFFECT_LABEL_SYSCALL
Definition: label.h:19
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
void(* RzILVmHook)(RzILVM *vm, RzILOpEffect *op)
Definition: rz_il_vm.h:32
Definition: dis.h:35
op structure for goto (label -> ctrl eff)
const char * lbl
name of the label, const one
static void perform_jump(RzILVM *vm, RZ_OWN RzBitVector *dst)
Definition: theory_effect.c:50

References EFFECT_LABEL_HOOK, EFFECT_LABEL_SYSCALL, rz_il_op_args_goto_t::lbl, perform_jump(), rz_bv_dup(), rz_il_vm_find_label_by_name(), and rz_return_val_if_fail.

◆ rz_il_handler_jmp()

bool rz_il_handler_jmp ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 56 of file theory_effect.c.

56  {
57  rz_return_val_if_fail(vm && op, false);
58  RzBitVector *dst = rz_il_evaluate_bitv(vm, op->op.jmp.dst);
59  if (!dst) {
60  return false;
61  }
62  perform_jump(vm, dst);
63  return true;
64 }
RZ_API RZ_NULLABLE RZ_OWN RzBitVector * rz_il_evaluate_bitv(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpBitVector *op)
Definition: il_vm_eval.c:269
structure for bitvector
Definition: rz_bitvector.h:19

References dst, perform_jump(), rz_il_evaluate_bitv(), and rz_return_val_if_fail.

◆ rz_il_handler_nop()

bool rz_il_handler_nop ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 34 of file theory_effect.c.

34  {
35  rz_return_val_if_fail(vm && op, false);
36  return true;
37 }

References rz_return_val_if_fail.

◆ rz_il_handler_repeat()

bool rz_il_handler_repeat ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 100 of file theory_effect.c.

100  {
101  rz_return_val_if_fail(vm && op, NULL);
102 
103  RzILOpArgsRepeat *op_repeat = &op->op.repeat;
104  bool res = true;
105  RzILBool *condition;
106  while ((condition = rz_il_evaluate_bool(vm, op_repeat->condition))) {
107  if (!condition->b) {
108  break;
109  }
110  res = res && rz_il_evaluate_effect(vm, op_repeat->data_eff);
111  rz_il_bool_free(condition);
112  }
113  rz_il_bool_free(condition);
114 
115  return res;
116 }
op structure for repeat (bool -> data eff -> data eff)
RzILOpBool * condition
index of BOOL condition
RzILOpEffect * data_eff
index of data effect

References rz_il_bool_t::b, rzil_op_repeat_t::condition, rzil_op_repeat_t::data_eff, NULL, rz_il_bool_free(), rz_il_evaluate_bool(), rz_il_evaluate_effect(), and rz_return_val_if_fail.

◆ rz_il_handler_seq()

bool rz_il_handler_seq ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 83 of file theory_effect.c.

83  {
84  rz_return_val_if_fail(vm && op, false);
85  RzILOpArgsSeq *op_seq = &op->op.seq;
86  return rz_il_evaluate_effect(vm, op_seq->x) && rz_il_evaluate_effect(vm, op_seq->y);
87 }
op structure for Seq ('a eff -> 'a eff -> 'a eff)
RzILOpEffect * y
perform this second
RzILOpEffect * x
perform this first

References rz_il_evaluate_effect(), rz_return_val_if_fail, rz_il_op_args_seq_t::x, and rz_il_op_args_seq_t::y.

◆ rz_il_handler_set()

bool rz_il_handler_set ( RzILVM vm,
RzILOpEffect op 
)

Definition at line 39 of file theory_effect.c.

39  {
40  rz_return_val_if_fail(vm && op, false);
41  RzILOpArgsSet *set_op = &op->op.set;
42  RzILVal *val = rz_il_evaluate_val(vm, set_op->x);
43  if (!val) {
44  return false;
45  }
46  rz_il_set(vm, set_op->v, set_op->is_local, val);
47  return true;
48 }
ut16 val
Definition: armass64_const.h:6
RZ_API RZ_NULLABLE RZ_OWN RzILVal * rz_il_evaluate_val(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpPure *op)
Definition: il_vm_eval.c:309
op structure for set ('a var -> 'a pure -> data eff)
bool is_local
whether a global variable should be set or a local optionally created and set
const char * v
name of variable, const one
RzILOpPure * x
value to set the variable to
static void rz_il_set(RzILVM *vm, const char *var_name, bool is_local, RZ_OWN RzILVal *val)
Definition: theory_effect.c:17

References rz_il_op_args_set_t::is_local, rz_il_evaluate_val(), rz_il_set(), rz_return_val_if_fail, rz_il_op_args_set_t::v, val, and rz_il_op_args_set_t::x.

◆ rz_il_set()

static void rz_il_set ( RzILVM vm,
const char *  var_name,
bool  is_local,
RZ_OWN RzILVal val 
)
static

Definition at line 17 of file theory_effect.c.

17  {
18  if (is_local) {
19  rz_il_vm_set_local_var(vm, var_name, val);
20  } else {
21  RzILVar *var = rz_il_vm_get_var(vm, RZ_IL_VAR_KIND_GLOBAL, var_name);
22  RzILEvent *evt = il_event_new_write_from_var(vm, var, val);
23  rz_il_vm_event_add(vm, evt);
24  rz_il_vm_set_global_var(vm, var_name, val);
25  }
26 }
RZ_API void rz_il_vm_set_local_var(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *name, RZ_OWN RzILVal *val)
Definition: il_vm.c:202
RZ_API void rz_il_vm_set_global_var(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *name, RZ_OWN RzILVal *val)
Definition: il_vm.c:193
RZ_API RZ_BORROW RzILVar * rz_il_vm_get_var(RZ_NONNULL RzILVM *vm, RzILVarKind kind, const char *name)
Definition: il_vm.c:251
Definition of a variable inside the vm.
Definition: variable.h:19
static RzILEvent * il_event_new_write_from_var(RzILVM *vm, RzILVar *var, RzILVal *new_val)
Definition: theory_effect.c:8

References il_event_new_write_from_var(), RZ_IL_VAR_KIND_GLOBAL, rz_il_vm_event_add(), rz_il_vm_get_var(), rz_il_vm_set_global_var(), rz_il_vm_set_local_var(), and val.

Referenced by rz_il_handler_set().