-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathverify.py
executable file
·137 lines (126 loc) · 4.71 KB
/
verify.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/python3.5
# @Author: Giovanni Camurati <Camurati>, Corteggiani Nassim <Corteggiani>
# @Date: 04-July-2017
# @Email: [email protected], [email protected]
# @Filename: generator.py
# @Last modified by: Camurati
# @Last modified time: 04-July-2017
# @License: GPLv3
import os
import sys
import getopt
import os_run
# read command line args
def print_usage_error():
print("Wrong parameters, usage:")
print("./verify.py \
-c <(continue in case of error) True/False> \
[-i <input folder> \
[-o <output folder> \
[-f <reglist to filter>]")
sys.exit(0)
ifolder="test_cases"
ofolder="test_results"
filter_reglist = []
if len(sys.argv) <= 1:
print_usage_error()
try:
opts,args = getopt.getopt(sys.argv[1:],"hc:i:o:f:",["help",
"continue=",
"input-folder=",
"output-folder="])
except getopt.GetoptError:
print_usage_error()
for opt,arg in opts:
if opt in ("-h","--help"):
print("./generator.py")
print("options:")
print(" c,continue=: True->skip errors, False->stop on error")
print(" i,input-folder=: folder where test cases are stored")
print(" o,output-folder=: folder where test results are stored")
print(" f,filter-reglist=: comma-separated list of registers not \
to take into cosideration for the check")
print("")
elif opt in ("-c","--continue"):
if arg == "True":
cont = True
elif arg == "False":
cont = False
else:
print("Error, continue must be True or False")
sys.exit(1)
elif opt in ("-i","--input-folder="):
ifolder = arg
elif opt in ("-o","--output-folder="):
ofolder = arg
elif opt in ("-f","--filter-reglist="):
filter_reglist = arg.split(",")
# create output folder starting from the input
os_run.run_catch_error("mkdir -p %s"%(ofolder),False)
os_run.run_catch_error("cp %s/* %s/"%(ifolder,ofolder),False)
# save version
vfile = os.path.abspath(ofolder+"/version_verify.log")
os_run.run_catch_error("./versioning.sh %s"%(vfile),False)
# Retrieve number of generated tests
with open('%s/Ntests'%(ofolder),mode='r') as Ntests_file:
Ntests = int(Ntests_file.readline())
Ntests_file.close
# DUT
# C + asm stubs => llvm IR
# TODO better
for i in range(0,Ntests):
os_run.run_catch_error('echo "#define KLEE\n$(cat %s/main%d.c)" > \
%s/main%d.c'%(ofolder,i,ofolder,i),cont)
ret = os_run.run_catch_error('./build.sh main%d inception %s'%(i,ofolder),cont)
if(ret != 0):
continue
# Run klee and dump registers
print ("Running klee ...")
ret = os_run.run_catch_error("./run_klee.sh "+str(i)+" "+str(ofolder),cont)
if(ret != 0):
print ("--> main%d klee failed"%(i))
continue
# check
with open('%s/reg_diff%d.log'%(ofolder,i),mode='r') as reg_diff_file:
lines = reg_diff_file.read().splitlines()
reg_diff_file.close
reg_diff = set([(reg,val) \
for reg,val in zip(lines[::2],lines[1::2]) \
if reg not in filter_reglist])
#print (reg_diff)
with open('%s/reg_diff_klee%d.log'%(ofolder,i),mode='r') as reg_diff_klee_file:
lines_klee = reg_diff_klee_file.read().splitlines()
reg_diff_klee_file.close
reg_diff_klee = set([(reg,val) \
for reg,val in zip(lines_klee[::2],lines_klee[1::2]) \
if reg not in filter_reglist])
#print (reg_diff_klee)
diff = reg_diff_klee.symmetric_difference(reg_diff)
if(diff == set()):
print ("TEST %d PASSED"%(i))
print ("\t[Test]")
print ("\t"+str(reg_diff_klee))
print ("\t[Oracle]")
print ("\t"+str(reg_diff))
print ("\t[Differences]")
print ("\t"+str(diff))
else:
print ("-----------------[TEST %d]---------------------"%(i))
print ("[RESULT]\t main%d The test failed ..."%(i),diff)
print ("")
print ("[Source Code]\t Available into %s/main%d.c :"%(ofolder,i))
print ("")
with open('%s/main%d.c'%(ofolder,i),mode='r') as source_file:
print (source_file.read())
print ("")
print ("\t[Test]")
print ("\t"+str(reg_diff_klee))
print ("\t[Oracle]")
print ("\t"+str(reg_diff))
print ("\t[Differences]")
print ("\t"+str(diff))
print ("-----------------------------------------------")
print("")
if(cont == False):
print("aborting...")
sys.exit(1)