-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmc_util.py
75 lines (63 loc) · 1.84 KB
/
mc_util.py
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
70
71
72
73
74
75
# Copyright (c) 2015 Xi Wang
#
# This file is part of the UW CSE 551 lecture code. It is freely
# distributed under the MIT License.
from __future__ import print_function
from z3 import *
from multiprocessing import Lock
import os, atexit
solver = Solver()
lock = Lock()
def mc_log(s):
# "atomic" print; less concern about performance
with lock:
print("[%s] %s" % (os.getpid(), s), file=sys.stderr)
def mc_assume(b):
return solver.add(b)
def mc_model_repr(self):
decls = sorted(self.decls(), key=str)
return ", ".join(["%s = %s" % (k, self[k]) for k in decls])
setattr(ModelRef, "__repr__", mc_model_repr)
def mc_unsignedBitVec():
conf = {
"__div__" : UDiv,
"__rdiv__" : lambda self, other: UDiv(other, self),
"__mod__" : URem,
"__rmod__" : lambda self, other: URem(other, self),
"__rshift__" : LShR,
"__rrshift__": lambda self, other: LShR(other, self),
"__lt__" : ULT,
"__le__" : ULE,
"__gt__" : UGT,
"__ge__" : UGE
}
for k, v in conf.items():
setattr(BitVecRef, k, v)
def mc_excepthook(typ, value, tb):
import traceback
msg = ''.join(traceback.format_exception(typ, value, tb)).strip()
# print input/model
if solver.check() == sat:
msg = "%s: %s" % (msg, solver.model())
# highlight if pygments installed
try:
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import get_formatter_by_name
lexer = get_lexer_by_name("pytb", stripall=True)
formatter = get_formatter_by_name("terminal256")
msg = highlight(msg, lexer, formatter).strip()
except:
pass
mc_log(msg)
if sys.stderr.isatty():
sys.excepthook = mc_excepthook
def mc_exit():
# wait until all child processes done
try:
while True:
os.waitpid(0, 0)
except:
pass
mc_log("exit")
atexit.register(mc_exit)