forked from seL4/graph-refine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph-refine.py
227 lines (203 loc) · 6.25 KB
/
graph-refine.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# * Copyright 2015, NICTA
# *
# * This software may be distributed and modified according to the terms of
# * the BSD 2-Clause license. Note that NO WARRANTY is provided.
# * See "LICENSE_BSD2.txt" for details.
# *
# * @TAG(NICTA_BSD)
# toplevel graph-refine script
# usage: python graph-refine.py <target> <proofs>
import syntax
import pseudo_compile
import solver
import rep_graph
import problem
import check
import search
from target_objects import pairings, functions
from target_objects import trace, tracer, printout
import target_objects
import re
import random
import traceback
import time
#import diagnostic
import sys
if __name__ == '__main__':
args = target_objects.load_target_args ()
def toplevel_check (pair, check_loops = True, report = False, count = None):
printout ('Testing Function pair %s' % pair)
if count:
(i, n) = count
printout (' (function pairing %d of %d)' % (i + 1, n))
for (tag, fname) in pair.funs.iteritems ():
if not functions[fname].entry:
printout ('Skipping %s, underspecified %s' % (pair, tag))
return None
prev_tracer = tracer[0]
if report:
tracer[0] = lambda s, n: ()
exception = None
trace (time.asctime ())
start_time = time.time()
sys.stdout.flush ()
try:
p = check.build_problem (pair)
if report:
printout (' .. built problem, finding proof')
if not check_loops and p.loop_data:
printout ('Problem has loop!')
tracer[0] = prev_tracer
return 'Loop'
if check_loops == 'only' and not p.loop_data:
printout ('No loop in problem.')
tracer[0] = prev_tracer
return 'NoLoop'
proof = search.build_proof (p)
if report:
printout (' .. proof found.')
try:
if report:
result = check.check_proof_report (p, proof)
else:
result = check.check_proof (p, proof)
if result:
printout ('Refinement proven.')
else:
printout ('Refinement NOT proven.')
except solver.SolverFailure, e:
printout ('Solver timeout/failure in proof check.')
result = 'CheckSolverFailure'
except Exception, e:
trace ('EXCEPTION in checking %s:' % p.name)
exception = sys.exc_info ()
result = 'CheckEXCEPT'
except problem.Abort:
result = 'ProofAbort'
except search.NoSplit:
result = 'ProofNoSplit'
except solver.SolverFailure, e:
printout ('Solver timeout/failure in proof search.')
result = 'ProofSolverFailure'
except Exception, e:
trace ('EXCEPTION in handling %s:' % pair)
exception = sys.exc_info ()
result = 'ProofEXCEPT'
end_time = time.time ()
tracer[0] = prev_tracer
if exception:
(etype, evalue, tb) = exception
traceback.print_exception (etype, evalue, tb,
file = sys.stdout)
if not report:
printout ('Time taken to check %s for pair %s: %f'
% (result, pair, end_time - start_time))
elif result in ['ProofNoSplit', 'ProofEXCEPT', 'CheckEXCEPT']:
printout ('Proof process failure (%s).' % result)
sys.stdout.flush ()
return str (result)
def toplevel_check_wname (pair, check_loops = True,
report_mode = False, count = None):
r = toplevel_check (pair, count = count, report = report_mode)
return (pair.name, r)
word_re = re.compile('\\w+')
def name_search (s, tags = None):
pairs = list (set ([pair for f in pairings for pair in pairings[f]
if s in pair.name
if not tags or tags.issubset (set (pair.tags))]))
if len (pairs) == 1:
return pairs[0]
word_pairs = [p for p in pairs if s in word_re.findall (str (p))]
if len (word_pairs) == 1:
return word_pairs[0]
print 'Possibilities for %r: %s' % (s, [str (p) for p in pairs])
return None
def check_search (s, tags = None, report_mode = False,
check_loops = True):
pair = name_search (s, tags = tags)
if pair:
return toplevel_check_wname (pair,
report_mode = report_mode,
check_loops = check_loops)
def check_all (omit_set = set (), loops = True, tags = None,
report_mode = False):
pairs = list (set ([pair for f in pairings for pair in pairings[f]
if omit_set.isdisjoint (pair.funs.values ())
if not tags or tags.issubset (set (pair.tags))]))
num_pairs = len (pairs)
omitted = list (set ([pair for f in pairings for pair in pairings[f]
if not omit_set.isdisjoint (pair.funs.values())]))
random.shuffle (pairs)
results = [toplevel_check_wname (pair, check_loops = loops,
report_mode = report_mode, count = (i, num_pairs))
for (i, pair) in enumerate (pairs)]
printout ('Result summary: %s' % results)
count = len ([1 for (_, r) in results if r == 'True'])
printout (' - %d proofs checked' % count)
count = len ([1 for (_, r) in results
if r in ['ProofAbort', None]])
printout (' - %d proofs skipped' % count)
fails = [(nm, r) for (nm, r) in results
if r not in ['True', 'ProofAbort', None]]
printout (' - failures: %s' % fails)
if omitted:
printout (' - %d pairings omitted: %s'
% (len (omitted), omitted))
def save_compiled_funcs (fname):
out = open (fname, 'w')
for (f, func) in functions.iteritems ():
trace ('Saving %s' % f)
for s in func.serialise ():
out.write (s + '\n')
out.close ()
def main (args):
excluding = False
excludes = set ()
loops = True
tags = set ()
report = True
for arg in args:
try:
if arg == 'verbose':
report = False
elif arg.startswith ('trace-to:'):
(_, s) = arg.split (':', 1)
f = open (s, 'w')
target_objects.trace_files.append (f)
elif arg == 'all':
check_all (excludes, loops = loops, tags = tags,
report_mode = report)
elif arg == 'all_safe':
check_all (set.union (target_objects.danger_set,
excludes), loops = loops, tags = tags,
report_mode = report)
elif arg == 'no_loops':
loops = False
elif arg == 'only_loops':
loops = 'only'
elif arg.startswith('save:'):
save_compiled_funcs (arg[5:])
elif arg.startswith('save-proofs:'):
fname = arg[len ('save-proofs:') :]
save = check.save_proofs_to_file (fname, 'a')
check.save_checked_proofs[0] = save
elif arg == '-exclude':
excluding = True
elif arg == '-end-exclude':
excluding = False
elif arg.startswith ('t:'):
tags.add (arg[2:])
elif arg.startswith ('target:'):
pass
elif excluding:
excludes.add (arg)
else:
if arg not in excludes:
check_search (arg, tags = tags,
report_mode = report,
check_loops = loops)
except Exception, e:
print 'EXCEPTION in syscall arg %s:' % arg
print traceback.format_exc ()
if __name__ == '__main__':
main (args)