Rizin
unix-like reverse engineering framework and cli tools
theory_effect.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2021 Florian Märkl <info@florianmaerkl.de>
2 // SPDX-FileCopyrightText: 2021 heersin <teablearcher@gmail.com>
3 // SPDX-License-Identifier: LGPL-3.0-only
4 
5 #include <rz_il/rz_il_opcodes.h>
6 #include <rz_il/rz_il_vm.h>
7 
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 }
16 
17 static void rz_il_set(RzILVM *vm, const char *var_name, bool is_local, RZ_OWN RzILVal *val) {
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 }
27 
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 }
33 
35  rz_return_val_if_fail(vm && op, false);
36  return true;
37 }
38 
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 }
49 
52  rz_bv_free(vm->pc);
53  vm->pc = dst;
54 }
55 
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 }
65 
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 }
82 
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 }
88 
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 }
99 
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 }
117 
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 }
ut16 val
Definition: armass64_const.h:6
RZ_API void rz_il_bool_free(RzILBool *bool_var)
Definition: bool.c:74
#define NULL
Definition: cris-opc.c:27
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 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 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 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 RzILEffectLabel * rz_il_vm_find_label_by_name(RZ_NONNULL RzILVM *vm, RZ_NONNULL const char *lbl_name)
Definition: il_vm.c:291
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
RZ_API RZ_BORROW RzILVar * rz_il_vm_get_var(RZ_NONNULL RzILVM *vm, RzILVarKind kind, const char *name)
Definition: il_vm.c:251
RZ_API bool rz_il_evaluate_effect(RZ_NONNULL RzILVM *vm, RZ_NONNULL RzILOpEffect *op)
Definition: il_vm_eval.c:341
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
RZ_API void rz_il_vm_event_add(RzILVM *vm, RzILEvent *evt)
Definition: il_vm_eval.c:201
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
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
@ EFFECT_LABEL_HOOK
Definition: label.h:20
@ EFFECT_LABEL_SYSCALL
Definition: label.h:19
char * dst
Definition: lz4.h:724
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
RZ_API void rz_bv_free(RZ_NULLABLE RzBitVector *bv)
Definition: bitvector.c:85
RZ_API RZ_OWN char * rz_bv_as_string(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:98
signatures of core theory opcodes
void(* RzILVmHook)(RzILVM *vm, RzILOpEffect *op)
Definition: rz_il_vm.h:32
#define RZ_LOG_WARN(fmtstr,...)
Definition: rz_log.h:56
#define RZ_OWN
Definition: rz_types.h:62
structure for bitvector
Definition: rz_bitvector.h:19
Definition: dis.h:35
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
op structure for goto (label -> ctrl eff)
const char * lbl
name of the label, const one
op structure for Seq ('a eff -> 'a eff -> 'a eff)
RzILOpEffect * y
perform this second
RzILOpEffect * x
perform this first
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
Definition of a variable inside the vm.
Definition: variable.h:19
char * name
Definition: variable.h:20
Low-level VM to execute raw IL code.
Definition: rz_il_vm.h:37
RzBitVector * pc
Program Counter of VM.
Definition: rz_il_vm.h:46
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
op structure for repeat (bool -> data eff -> data eff)
RzILOpBool * condition
index of BOOL condition
RzILOpEffect * data_eff
index of data effect
bool rz_il_handler_branch(RzILVM *vm, RzILOpEffect *op)
bool rz_il_handler_nop(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:34
bool rz_il_handler_jmp(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:56
static RzILEvent * il_event_new_write_from_var(RzILVM *vm, RzILVar *var, RzILVal *new_val)
Definition: theory_effect.c:8
static void perform_jump(RzILVM *vm, RZ_OWN RzBitVector *dst)
Definition: theory_effect.c:50
bool rz_il_handler_set(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:39
bool rz_il_handler_goto(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:66
bool rz_il_handler_empty(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:28
bool rz_il_handler_seq(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:83
static void rz_il_set(RzILVM *vm, const char *var_name, bool is_local, RZ_OWN RzILVal *val)
Definition: theory_effect.c:17
bool rz_il_handler_repeat(RzILVM *vm, RzILOpEffect *op)
bool rz_il_handler_blk(RzILVM *vm, RzILOpEffect *op)
Definition: theory_effect.c:89
Definition: dis.c:32
@ RZ_IL_VAR_KIND_GLOBAL
global var, usually bound to a physical representation like a register.
Definition: variable.h:47