-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsymbolic_integer_array.py
More file actions
69 lines (58 loc) · 2.21 KB
/
Copy pathsymbolic_integer_array.py
File metadata and controls
69 lines (58 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from dataclasses import dataclass
import random
from typing import Union
import z3
from model_wrapper import ModelWrapper
class SymbolicIntegerArray:
def __init__(self, name: str):
self.__name: str = name
self.__length: z3.ArithRef = z3.Int(f"{name}_length")
self.__initial_array: z3.ArrayRef = z3.Array(f"{name}_array", z3.IntSort(), z3.IntSort())
self.__array: z3.ArrayRef = self.__initial_array
def __getitem__(self, i: Union[int, z3.IntNumRef]):
return z3.Select(self.__array, z3.simplify(i) if z3.is_expr(i) else i)
def __setitem__(self, i: Union[int, z3.ArithRef], v):
i = z3.simplify(i) if z3.is_expr(i) else i
v = z3.simplify(v) if z3.is_expr(v) else v
self.__array = z3.Store(self.__array, i, v)
def length(self):
return self.__length
def __repr__(self):
return f"SymbolicIntegerArray({self.__name}, {self.__length}, {self.__array})"
def to_concrete(self, model: ModelWrapper):
l = model.evaluate(self.__length)
if z3.is_int_value(l):
l = l.as_long()
else:
l = random.randint(0, 10)
model.add(self.__length == l)
res = [random.randint(-100, 100) for i in range(l)]
for i in range(l):
e = model.evaluate(z3.Select(self.__array, i))
if z3.is_int_value(e):
res[i] = e.as_long()
else:
model.add(z3.Select(self.__array, i) == res[i])
continue
return res
def to_initial_state(self, model: ModelWrapper):
l = model.evaluate(self.__length)
if z3.is_int_value(l):
l = l.as_long()
else:
l = random.randint(0, 10)
model.add(self.__length == l)
res = [random.randint(-100, 100) for i in range(l)]
for i in range(l):
e = model.evaluate(z3.Select(self.__initial_array, i))
if z3.is_int_value(e):
res[i] = e.as_long()
else:
model.add(z3.Select(self.__initial_array, i) == res[i])
continue
return res
@dataclass
class HeapReference:
address: int
def __hash__(self):
return hash(self.address)