Rizin
unix-like reverse engineering framework and cli tools
theory_bitv.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 
4 #include <rz_il/rz_il_opcodes.h>
5 #include <rz_il/rz_il_vm.h>
6 
9 
10  RzILOpArgsMsb *op_msb = &op->op.msb;
11  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_msb->bv);
12  RzILBool *result = bv ? rz_il_bool_new(rz_bv_msb(bv)) : NULL;
13  rz_bv_free(bv);
14 
16  return result;
17 }
18 
20  rz_return_val_if_fail(vm && op && type, NULL);
21 
22  RzILOpArgsLsb *op_lsb = &op->op.lsb;
23  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_lsb->bv);
24  RzILBool *result = bv ? rz_il_bool_new(rz_bv_lsb(bv)) : NULL;
25  rz_bv_free(bv);
26 
28  return result;
29 }
30 
32  rz_return_val_if_fail(vm && op && type, NULL);
33 
34  RzILOpArgsLsb *op_lsb = &op->op.lsb;
35  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_lsb->bv);
36  RzILBool *result = bv ? rz_il_bool_new(rz_bv_is_zero_vector(bv)) : NULL;
37  rz_bv_free(bv);
38 
40  return result;
41 }
42 
44  rz_return_val_if_fail(vm && op && type, NULL);
45 
46  RzILOpArgsNeg *neg = &op->op.neg;
47 
48  RzBitVector *bv_arg = rz_il_evaluate_bitv(vm, neg->bv);
49  RzBitVector *bv_result = bv_arg ? rz_bv_neg(bv_arg) : NULL;
50  rz_bv_free(bv_arg);
51 
53  return bv_result;
54 }
55 
57  rz_return_val_if_fail(vm && op && type, NULL);
58 
59  RzILOpArgsLogNot *op_not = &op->op.lognot;
60 
61  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_not->bv);
62  RzBitVector *result = bv ? rz_bv_not(bv) : NULL;
63  rz_bv_free(bv);
64 
66  return result;
67 }
68 
70  rz_return_val_if_fail(vm && op && type, NULL);
71 
72  RzILOpArgsSle *op_sle = &op->op.sle;
73 
74  RzBitVector *x = rz_il_evaluate_bitv(vm, op_sle->x);
75  RzBitVector *y = rz_il_evaluate_bitv(vm, op_sle->y);
76  RzILBool *result = x && y ? rz_il_bool_new(rz_bv_eq(x, y)) : NULL;
77  rz_bv_free(x);
78  rz_bv_free(y);
79 
81  return result;
82 }
83 
85  rz_return_val_if_fail(vm && op && type, NULL);
86 
87  RzILOpArgsSle *op_sle = &op->op.sle;
88 
89  RzBitVector *x = rz_il_evaluate_bitv(vm, op_sle->x);
90  RzBitVector *y = rz_il_evaluate_bitv(vm, op_sle->y);
91  RzILBool *result = x && y ? rz_il_bool_new(rz_bv_sle(x, y)) : NULL;
92  rz_bv_free(x);
93  rz_bv_free(y);
94 
96  return result;
97 }
98 
100  rz_return_val_if_fail(vm && op && type, NULL);
101 
102  RzILOpArgsUle *op_ule = &op->op.ule;
103 
104  RzBitVector *x = rz_il_evaluate_bitv(vm, op_ule->x);
105  RzBitVector *y = rz_il_evaluate_bitv(vm, op_ule->y);
106  RzILBool *result = x && y ? rz_il_bool_new(rz_bv_ule(x, y)) : NULL;
107  rz_bv_free(x);
108  rz_bv_free(y);
109 
111  return result;
112 }
113 
115  rz_return_val_if_fail(vm && op && type, NULL);
116 
117  RzILOpArgsAdd *op_add = &op->op.add;
118 
119  RzBitVector *x = rz_il_evaluate_bitv(vm, op_add->x);
120  RzBitVector *y = rz_il_evaluate_bitv(vm, op_add->y);
121  RzBitVector *result = x && y ? rz_bv_add(x, y, NULL) : NULL;
122 
123  rz_bv_free(x);
124  rz_bv_free(y);
125 
127  return result;
128 }
129 
131  rz_return_val_if_fail(vm && op && type, NULL);
132 
133  RzILOpArgsAppend *op_append = &op->op.append;
134 
135  RzBitVector *high = rz_il_evaluate_bitv(vm, op_append->high);
136  RzBitVector *low = rz_il_evaluate_bitv(vm, op_append->low);
137  RzBitVector *result = high && low ? rz_bv_append(high, low) : NULL;
138  rz_bv_free(low);
139  rz_bv_free(high);
140 
142  return result;
143 }
144 
146  rz_return_val_if_fail(vm && op && type, NULL);
147 
148  RzILOpArgsAdd *op_add = &op->op.add;
149 
150  RzBitVector *x = rz_il_evaluate_bitv(vm, op_add->x);
151  RzBitVector *y = rz_il_evaluate_bitv(vm, op_add->y);
152  RzBitVector *result = x && y ? rz_bv_and(x, y) : NULL;
153  rz_bv_free(x);
154  rz_bv_free(y);
155 
157  return result;
158 }
159 
161  rz_return_val_if_fail(vm && op && type, NULL);
162 
163  RzILOpArgsAdd *op_add = &op->op.add;
164 
165  RzBitVector *x = rz_il_evaluate_bitv(vm, op_add->x);
166  RzBitVector *y = rz_il_evaluate_bitv(vm, op_add->y);
167  RzBitVector *result = x && y ? rz_bv_or(x, y) : NULL;
168  rz_bv_free(x);
169  rz_bv_free(y);
170 
172  return result;
173 }
174 
176  rz_return_val_if_fail(vm && op && type, NULL);
177 
178  RzILOpArgsAdd *op_add = &op->op.add;
179 
180  RzBitVector *x = rz_il_evaluate_bitv(vm, op_add->x);
181  RzBitVector *y = rz_il_evaluate_bitv(vm, op_add->y);
182  RzBitVector *result = x && y ? rz_bv_xor(x, y) : NULL;
183  rz_bv_free(x);
184  rz_bv_free(y);
185 
187  return result;
188 }
189 
191  rz_return_val_if_fail(vm && op && type, NULL);
192 
193  RzILOpArgsSub *op_sub = &op->op.sub;
194 
195  RzBitVector *x = rz_il_evaluate_bitv(vm, op_sub->x);
196  RzBitVector *y = rz_il_evaluate_bitv(vm, op_sub->y);
197  RzBitVector *result = x && y ? rz_bv_sub(x, y, NULL) : NULL;
198  rz_bv_free(x);
199  rz_bv_free(y);
200 
202  return result;
203 }
204 
206  rz_return_val_if_fail(vm && op && type, NULL);
207 
208  RzILOpArgsMul *op_mul = &op->op.mul;
209 
210  RzBitVector *x = rz_il_evaluate_bitv(vm, op_mul->x);
211  RzBitVector *y = rz_il_evaluate_bitv(vm, op_mul->y);
212  RzBitVector *result = x && y ? rz_bv_mul(x, y) : NULL;
213 
214  rz_bv_free(x);
215  rz_bv_free(y);
216 
218  return result;
219 }
220 
222  rz_return_val_if_fail(vm && op && type, NULL);
223 
224  RzILOpArgsDiv *op_div = &op->op.div;
225 
226  RzBitVector *x = rz_il_evaluate_bitv(vm, op_div->x);
227  RzBitVector *y = rz_il_evaluate_bitv(vm, op_div->y);
228  RzBitVector *result = NULL;
229  if (x && y) {
230  if (rz_bv_is_zero_vector(y)) {
231  result = rz_bv_new(y->len);
232  rz_bv_set_all(result, true);
233  rz_il_vm_event_add(vm, rz_il_event_exception_new("division by zero"));
234  } else {
235  result = rz_bv_div(x, y);
236  }
237  }
238 
239  rz_bv_free(x);
240  rz_bv_free(y);
241 
243  return result;
244 }
245 
247  rz_return_val_if_fail(vm && op && type, NULL);
248 
249  RzILOpArgsSdiv *op_sdiv = &op->op.sdiv;
250 
251  RzBitVector *x = rz_il_evaluate_bitv(vm, op_sdiv->x);
252  RzBitVector *y = rz_il_evaluate_bitv(vm, op_sdiv->y);
253  RzBitVector *result = x && y ? rz_bv_sdiv(x, y) : NULL;
254  rz_bv_free(x);
255  rz_bv_free(y);
256 
258  return result;
259 }
260 
262  rz_return_val_if_fail(vm && op && type, NULL);
263 
264  RzILOpArgsMod *op_mod = &op->op.mod;
265 
266  RzBitVector *x = rz_il_evaluate_bitv(vm, op_mod->x);
267  RzBitVector *y = rz_il_evaluate_bitv(vm, op_mod->y);
268  RzBitVector *result = x && y ? rz_bv_mod(x, y) : NULL;
269  rz_bv_free(x);
270  rz_bv_free(y);
271 
273  return result;
274 }
275 
277  rz_return_val_if_fail(vm && op && type, NULL);
278 
279  RzILOpArgsSmod *op_smod = &op->op.smod;
280 
281  RzBitVector *x = rz_il_evaluate_bitv(vm, op_smod->x);
282  RzBitVector *y = rz_il_evaluate_bitv(vm, op_smod->y);
283  RzBitVector *result = x && y ? rz_bv_smod(x, y) : NULL;
284  rz_bv_free(x);
285  rz_bv_free(y);
286 
288  return result;
289 }
290 
292  rz_return_val_if_fail(vm && op && type, NULL);
293 
294  RzILOpArgsShiftLeft *op_shiftl = &op->op.shiftl;
295 
296  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_shiftl->x);
297  RzBitVector *shift = rz_il_evaluate_bitv(vm, op_shiftl->y);
298  RzILBool *fill_bit = rz_il_evaluate_bool(vm, op_shiftl->fill_bit);
299 
300  RzBitVector *result = NULL;
301  if (bv && shift && fill_bit) {
302  result = rz_bv_dup(bv);
303  rz_bv_lshift_fill(result, rz_bv_to_ut32(shift), fill_bit->b);
304  }
305  rz_bv_free(shift);
306  rz_bv_free(bv);
307  rz_il_bool_free(fill_bit);
308 
310  return result;
311 }
312 
314  rz_return_val_if_fail(vm && op && type, NULL);
315 
316  RzILOpArgsShiftRight *op_shr = &op->op.shiftr;
317 
318  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_shr->x);
319  RzBitVector *shift = rz_il_evaluate_bitv(vm, op_shr->y);
320  RzILBool *fill_bit = rz_il_evaluate_bool(vm, op_shr->fill_bit);
321 
322  RzBitVector *result = NULL;
323  if (bv && shift && fill_bit) {
324  result = rz_bv_dup(bv);
325  rz_bv_rshift_fill(result, rz_bv_to_ut32(shift), fill_bit->b);
326  }
327 
328  rz_bv_free(shift);
329  rz_bv_free(bv);
330  rz_il_bool_free(fill_bit);
331 
333  return result;
334 }
335 
337  rz_return_val_if_fail(vm && op && type, NULL);
338  RzILOpArgsBv *op_bitv = &op->op.bitv;
339 
340  RzBitVector *bv = rz_bv_dup(op_bitv->value);
341 
343  return bv;
344 }
345 
347  rz_return_val_if_fail(vm && op && type, NULL);
348 
349  RzILOpArgsCast *op_cast = &op->op.cast;
350  RzILBool *fill = rz_il_evaluate_bool(vm, op_cast->fill);
351  if (!fill) {
352  return NULL;
353  }
354  RzBitVector *bv = rz_il_evaluate_bitv(vm, op_cast->val);
355  if (!bv) {
356  return NULL;
357  }
358 
359  RzBitVector *ret = rz_bv_new(op_cast->length);
360  rz_bv_set_all(ret, fill->b);
361  rz_bv_copy_nbits(bv, 0, ret, 0, RZ_MIN(bv->len, ret->len));
362 
363  rz_il_bool_free(fill);
364  rz_bv_free(bv);
365 
367  return ret;
368 }
static RZ_NULLABLE RzILOpBitVector * shift(RzILOpBitVector *val, RZ_NULLABLE RzILOpBool **carry_out, arm_shifter type, RZ_OWN RzILOpBitVector *dist)
Definition: arm_il32.c:190
static ut32 neg(ArmOp *op)
Definition: armass64.c:981
RZ_API RzILBool * rz_il_bool_new(bool true_or_false)
Definition: bool.c:11
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_exception_new(RZ_NONNULL const char *exception)
Definition: il_events.c:52
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
int x
Definition: mipsasm.c:20
int type
Definition: mipsasm.c:17
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
RZ_API RZ_OWN RzBitVector * rz_bv_add(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *carry)
Definition: bitvector.c:683
RZ_API RZ_OWN RzBitVector * rz_bv_dup(const RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:167
RZ_API bool rz_bv_lsb(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1012
RZ_API RZ_OWN RzBitVector * rz_bv_or(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:573
RZ_API void rz_bv_free(RZ_NULLABLE RzBitVector *bv)
Definition: bitvector.c:85
RZ_API bool rz_bv_ule(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1052
RZ_API bool rz_bv_lshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:469
RZ_API bool rz_bv_sle(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1064
RZ_API bool rz_bv_is_zero_vector(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1021
RZ_API RZ_OWN RzBitVector * rz_bv_mod(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:865
RZ_API bool rz_bv_set_all(RZ_NONNULL RzBitVector *bv, bool b)
Definition: bitvector.c:367
RZ_API ut32 rz_bv_to_ut32(RZ_NONNULL const RzBitVector *x)
Definition: bitvector.c:1435
RZ_API RZ_OWN RzBitVector * rz_bv_xor(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:600
RZ_API RZ_OWN RzBitVector * rz_bv_append(RZ_NONNULL RzBitVector *bv1, RZ_NONNULL RzBitVector *bv2)
Definition: bitvector.c:326
RZ_API RZ_OWN RzBitVector * rz_bv_mul(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:734
RZ_API bool rz_bv_msb(RZ_NONNULL RzBitVector *bv)
Definition: bitvector.c:1003
RZ_API RZ_OWN RzBitVector * rz_bv_and(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:546
RZ_API RZ_OWN RzBitVector * rz_bv_new(ut32 length)
Definition: bitvector.c:71
RZ_API RZ_OWN RzBitVector * rz_bv_sub(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y, RZ_NULLABLE bool *borrow)
Definition: bitvector.c:715
#define rz_bv_neg
Definition: rz_bitvector.h:58
#define rz_bv_not
Definition: rz_bitvector.h:59
RZ_API bool rz_bv_rshift_fill(RZ_NONNULL RzBitVector *bv, ut32 size, bool fill_bit)
Definition: bitvector.c:508
RZ_API RZ_OWN RzBitVector * rz_bv_sdiv(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:893
RZ_API RZ_OWN RzBitVector * rz_bv_smod(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:952
RZ_API ut32 rz_bv_copy_nbits(RZ_NONNULL const RzBitVector *src, ut32 src_start_pos, RZ_NONNULL RzBitVector *dst, ut32 dst_start_pos, ut32 nbit)
Definition: bitvector.c:210
RZ_API bool rz_bv_eq(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:1041
RZ_API RZ_OWN RzBitVector * rz_bv_div(RZ_NONNULL RzBitVector *x, RZ_NONNULL RzBitVector *y)
Definition: bitvector.c:808
signatures of core theory opcodes
#define RZ_MIN(x, y)
RzILTypePure
Definition: sort.h:23
@ RZ_IL_TYPE_PURE_BOOL
Definition: sort.h:24
@ RZ_IL_TYPE_PURE_BITVECTOR
Definition: sort.h:25
structure for bitvector
Definition: rz_bitvector.h:19
ut32 len
number of bits – virtual / logical
Definition: rz_bitvector.h:25
bool b
Definition: bool.h:15
op structure for two-operand algorithm and logical operations ('s bitv -> 's bitv -> 's bitv)
Definition: rz_il_opcodes.h:97
RzILOpBitVector * x
left operand
Definition: rz_il_opcodes.h:98
RzILOpBitVector * y
right operand
Definition: rz_il_opcodes.h:99
op structure for appending 2 bitv: MSB:LSB high:low
RzILOpBitVector * low
bitvector occupying the least significant bits
RzILOpBitVector * high
bitvector occupying the most significant bits
value is a bitvector constant.
Definition: rz_il_opcodes.h:51
RzBitVector * value
value of bitvector
Definition: rz_il_opcodes.h:52
RzILOpBitVector * bv
unary operand
Definition: rz_il_opcodes.h:77
op structure for casting bitv
RzILOpBool * fill
If m = size val - length > 0 then m fill-bits are prepended to the most significant part of the vecto...
RzILOpBitVector * val
value to cast
ut32 length
new bits length
op structure for binary comparison ops ('a bitv -> 'a bitv -> bool)
RzILOpBitVector * x
index of operand 1
RzILOpBitVector * y
index of operand 2
op structure for lshift and rshift (bool -> 's bitv -> 'b bitv -> 's bitv)
RzILOpBitVector * x
index of operand 1
RzILOpBitVector * y
index of operand 2
RzILOpBool * fill_bit
index of fill bit
op structure for 's bitv -> bool [MSB] msb x is the most significant bit of x. [LSB] lsb x is the lea...
Definition: rz_il_opcodes.h:61
RzILOpBitVector * bv
Definition: rz_il_opcodes.h:62
An IL op performing a pure computation, 'a pure.
Low-level VM to execute raw IL code.
Definition: rz_il_vm.h:37
void * rz_il_handler_append(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:130
void * rz_il_handler_shiftr(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:313
void * rz_il_handler_eq(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:69
void * rz_il_handler_sle(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:84
void * rz_il_handler_add(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:114
void * rz_il_handler_mod(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:261
void * rz_il_handler_shiftl(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:291
void * rz_il_handler_sdiv(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:246
void * rz_il_handler_smod(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:276
void * rz_il_handler_ule(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:99
void * rz_il_handler_cast(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:346
void * rz_il_handler_neg(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:43
void * rz_il_handler_logical_and(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:145
void * rz_il_handler_logical_xor(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:175
void * rz_il_handler_lsb(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:19
void * rz_il_handler_bitv(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:336
void * rz_il_handler_is_zero(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:31
void * rz_il_handler_mul(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:205
void * rz_il_handler_logical_or(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:160
void * rz_il_handler_logical_not(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:56
void * rz_il_handler_sub(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:190
void * rz_il_handler_msb(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:7
void * rz_il_handler_div(RzILVM *vm, RzILOpBitVector *op, RzILTypePure *type)
Definition: theory_bitv.c:221
Definition: dis.c:32