forked from tamarin-prover/tamarin-prover
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtamarin-troop.py
More file actions
executable file
·572 lines (490 loc) · 20.1 KB
/
Copy pathtamarin-troop.py
File metadata and controls
executable file
·572 lines (490 loc) · 20.1 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#!/usr/bin/python3
import signal
import multiprocessing
import re
import argparse
import subprocess
import os
import itertools
import sys
import subprocess
import shutil
import time
def eprint(*args, **kwargs):
print(*args, file=sys.stderr, **kwargs)
# Timeout for the jobs in seconds
JOB_TIMEOUT = 5
# Tamarins heustics
HEURISTICS = [ 's', 'S', 'c', 'C', 'i', 'I' ]
DEEPSEC = "deepsec"
SPTHY = "spthy"
PROVERIF = "proverif"
TAMARIN_COMMAND = "tamarin-prover"
PROVERIF_COMMAND = "proverif"
DEEPSEC_COMMAND = "deepsec"
GSVERIF_COMMAND = "gsverif"
PROOF = "proof"
COUNTEREXAMPLE = 'counterexample'
INCONCLUSIVE = 'inconclusive'
NOT_STARTED = 'not_started'
STARTED = 'started'
FINISHED = 'finished'
TIMEOUT = 'timeout'
INTERRUPTED = 'interrupted'
ERROR = 'error'
PARSE_ERROR = "parse_error"
# Colors
GREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
# These job classes only exists to dynamically decide how we execute a job
# The arguments are fixed in the 'generate_tamarin/non_tamarin_jobs' functions
class Job():
def __init__(self, arguments, lemma):
# Arguments to execute the job
self.arguments = arguments
# Lemmas the proofs
# For a Deepsec/ProVerif job, this will be a singleton list
self.lemma = lemma
# The result of the job
self.result = None
# returncode of job
self.returncode = None
# Status of the job
self.status = NOT_STARTED
# Time the job took
self.time = ""
def __str__(self):
return str(self.arguments)
def __repr__(self):
return str(self)
def execute(self, result_queue):
"""
Executes the job specified by self.arguments and, iff
it was sucessful, returns a dictionary containing the result
"""
self.status = STARTED
call = "'" + " ".join(self.arguments) + "'"
print("Executing " + call)
try:
p = None
def execute_signal_handler(signum, frame):
self.status = INTERRUPTED
if p:
# If the job already started
p.terminate()
# kill own process
sys.exit(1)
# Register new signal handler
signal.signal(signal.SIGINT, execute_signal_handler)
signal.signal(signal.SIGTERM, execute_signal_handler)
p = subprocess.Popen(self.arguments, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, universal_newlines=True)
# Start timer
starttime = time.time()
stdout_data, stderr_data = p.communicate(timeout=JOB_TIMEOUT)
# calculate time
self.time = format(time.time() - starttime, '.4f')
if p.returncode != 0:
# Check if job completed correctly
# Save returncode to later report it
self.returncode = p.returncode
self.status = ERROR
return
# Get the results
self.parse_results(stdout_data, stderr_data)
if self.status == PARSE_ERROR:
return
self.status = FINISHED
except subprocess.TimeoutExpired:
self.status = TIMEOUT
finally:
result = (self.status, call, self.lemma, self.returncode, self.result, self.time)
result_queue.put(result)
print_single_result(result)
class TamarinJob(Job):
def parse_results(self, stdout, stderr):
# Parse the results for the self.lemma from the output
# and save them in self.results
# If no lemmas was specified, we assume that there is
# only one lemma in a .spthy file
# Search for the time Tamarin took
basepattern = self.lemma + " .*: "
verpattern = re.compile(basepattern + "verified")
falsepattern = re.compile(basepattern + "falsified")
incpattern = re.compile(basepattern + "analysis incomplete")
result = ""
if verpattern.search(stdout):
result = PROOF
elif falsepattern.search(stdout):
result = COUNTEREXAMPLE
elif incpattern.search(stdout):
result = INCONCLUSIVE
else:
self.status = PARSE_ERROR
return
self.result = result
class ProverifJob(Job):
def parse_results(self, stdout, stderr):
# Parse the results for the self.lemma from the output
# and save them in self.results
# We assume that there is only one query in a .pv file
# Search for the time ProVerif took
# Search for the result in ProVerif stdout
result = ""
if re.search(r"RESULT .* is true", stdout):
result = PROOF
elif re.search(r"RESULT .* is false", stdout):
result = COUNTEREXAMPLE
elif re.search(r"RESULT .* cannot be proved", stdout):
result = INCONCLUSIVE
else:
self.status = PARSE_ERROR
return
# Update dict
self.result = result
class DeepsecJob(Job):
def parse_results(self, stdout, stderr):
# Parse the results for the self.lemma from the output
# and save them in self.results
# We assume that there is only one query in a .dps file
# Search for the time Deepsec took
result = ""
if re.search(r"not session equivalent", stdout):
result = COUNTEREXAMPLE
elif re.search(r"session equivalent", stdout):
result = PROOF
else:
self.status = PARSE_ERROR
return
self.result = result
# Dict mapping tool to file type
TOOL_TO_FILE_TYPE = { SPTHY: ".spthy", PROVERIF: ".pv", DEEPSEC: ".dps" }
TOOL_TO_JOB_CLASS = { SPTHY: TamarinJob, PROVERIF: ProverifJob,
DEEPSEC: DeepsecJob }
def add_double_dashes_to_arguments(cliarguments):
return [['--' + argument for argument in argumentlist] for argumentlist \
in cliarguments ]
def add_dashes_to_arguments(cliarguments):
return [['-' + argument for argument in argumentlist] for argumentlist \
in cliarguments ]
def execute_joblist(joblist, queue):
# Concurrently execute the jobs in the joblist.
processes = [None] * len(joblist)
result_queue = multiprocessing.SimpleQueue()
for i in range(len(processes)):
job = joblist[i]
processes[i] = multiprocessing.Process(target=job.execute,
args=(result_queue, ))
try:
for p in processes:
p.start()
# Counter to abort once all jobs are done
counter = len(processes)
# We want the results as they finish
results = []
while counter:
result = result_queue.get()
counter -= 1
results.append(result)
# Check job status
if result[0] == FINISHED:
# abort all jobs as we got a valid result
for p in processes:
if p.is_alive():
os.kill(p.pid, signal.SIGINT)
break
finally:
for p in processes:
p.join()
# return results via queue
queue.put(results)
def print_single_result(result):
status, call, lemma, returncode, outcome, time = result
if status == ERROR:
eprint(FAIL + "ERROR: " + ENDC + \
call + " returned returncode " + str(returncode))
elif status == TIMEOUT:
eprint(WARNING + "WARNING: " + ENDC + \
call + " timed out after " + str(JOB_TIMEOUT) + " seconds")
elif status == INTERRUPTED:
eprint(WARNING + "INTERRUPTED: " + ENDC + call)
elif status == PARSE_ERROR:
eprint(FAIL + "ERROR: " + ENDC + "The result from "\
+ call + " could not be parsed.")
elif status == FINISHED:
color = GREEN if outcome == PROOF else \
(FAIL if outcome == COUNTEREXAMPLE else WARNING)
print("Finished " + call + " after " + str(time) + " seconds: " + color + outcome + ENDC)
def report_results(results):
# Here we save the good results
for results_per_lemma in results:
# get lemma name, ugly... but yeah
# Initialize best result to None
best_result = None
bad_results = []
lemma = ""
for result in results_per_lemma:
lemma = result[2]
# String for error reporting
status, call, lemma, returncode, outcome, time = result
# Make sure every job ran
assert status == FINISHED or status == TIMEOUT or \
status == ERROR or status == PARSE_ERROR
if status != FINISHED:
# Report error/timeout etc.
bad_results.append(result)
continue
# Result is good
if best_result is None:
best_result = result
if best_result[0] != status:
# Report mismatch
oldcall = best_result[1]
oldresult = best_result[5]
eprint(FAIL + "ERROR: " + ENDC + "'" + oldcall + "'" + \
" had result: " + oldresult)
eprint(FAIL + "ERROR: " + ENDC + "'" + call + "'" + \
" had result: " + outcome)
elif best_result[5] > time:
# compare time
best_result = result
# Report results for this lemma
print("=" * 90)
lemmastring = " for " + lemma if lemma else ""
if bad_results:
print("Reporting errors" + lemmastring + ":\n")
for bad_result in bad_results:
print_single_result(bad_result)
if best_result:
print("Reporting result" + lemmastring + ":\n")
print_single_result(best_result)
return results
def intermediate_file_name(input_file, tool, lemma):
"""
This function generates a file name for the intermediate generated
for deepsec/proverif.
TODO: Once we can generate files on a per lemma basis, we need to
include the name of the lemma in the file name as well.
"""
# Get input_file without '.spthy'
file_name, _ = os.path.splitext(input_file)
lemmastring = lemma + "_" if lemma else ""
return file_name + "_" + lemmastring + tool + TOOL_TO_FILE_TYPE[tool]
def generate_files(input_file, flags, lemmas, argdict, diff, gsverif):
"""
This function generates files for all tools in argdict except
for Tamarin. For Tamarin, we do not need to generate intermediate
files, but can use the original input file.
"""
# Format flags in the way the Tamarin CLI wants them
flags = [ '-D=' + flag for flag in flags ] if flags else []
# Diff mode?
diffstring = " --diff" if diff else ""
for tool in argdict.keys():
if tool == SPTHY:
# Skip Tamarin/spthy. Tamarin does not need intermediate files.
# It works on the sapic file itself.
continue
# For each tool generate a file using Tamarin -m
# Hack to make the loop work if not lemmas are specified
lemmas = lemmas if lemmas else [""]
for lemma in lemmas:
if lemma:
# Not the dummy value but a real lemma
tamarin_call = [TAMARIN_COMMAND, '-m='+tool, '--lemma=' + lemma]
else:
tamarin_call = [TAMARIN_COMMAND, '-m='+tool]
cmd = " ".join(tamarin_call + flags + [input_file])
# Add diff flag
cmd = cmd + diffstring
# Change file type according to current tool
file_name = intermediate_file_name(input_file, tool, lemma)
destination = " > " + file_name
# Concatenate the cmd and destination, and run it
subprocess.run(cmd + destination, shell=True, check=True)
if tool == PROVERIF and gsverif:
# If GSVerif flag is set, we use it on the ProVerif file
gsverif_call = [GSVERIF_COMMAND, file_name, '-o', file_name]
subprocess.run(gsverif_call, check=True)
def generate_jobs(input_file, lemmas, argdict, flags):
"""
This function generates the jobs that we want to concurrently execute.
A job is a list of arguments that, when used by a Popen call,
correspond to a call to Tamarin/Deepsec/Proverif on a file/lemma we
want to time.
For instance:
[ proverif, example.pv, -test]
is a valid job. Another example:
[ deepsec, example.dps, --local-workers 12]
is also a valid job.
"""
jobs = []
# Hack to make the loop work even if not lemmas were specified
lemmas = lemmas if lemmas else [""]
for lemma in lemmas:
jobs_for_lemma = []
# For every lemma create the jobs for each tool
for tool in argdict.keys():
if tool == SPTHY:
jobs_for_lemma += generate_tamarin_jobs(
input_file, lemma, argdict, flags)
else:
jobs_for_lemma += generate_non_tamarin_jobs(
tool, input_file, lemma, argdict)
jobs.append(jobs_for_lemma)
return jobs
def generate_non_tamarin_jobs(tool, input_file, lemma, argdict):
"""
Returns a list of jobs (list of lists).
"""
jobs = []
toolcmd = TOOL_TO_COMMAND[tool]
toolclass = TOOL_TO_JOB_CLASS[tool]
if not argdict[tool][0]:
# If there were no CLI params specified.
jobs += [toolclass([toolcmd, \
intermediate_file_name(input_file, tool, lemma)], lemma)]
else:
# If CLI params were specified, we use them.
jobs += [toolclass([toolcmd, intermediate_file_name(input_file, \
tool, lemma)] + list(tuple), lemma) \
for tuple in itertools.product(*argdict[tool])]
return jobs
def generate_tamarin_jobs(input_file, lemma, argdict, flags):
# TODO: Might need to revisit this once the Tamarin CLI has changed
lemmacli = [ '--prove=' + lemma ] if lemma else ["--prove"]
# heuristics = [] if argdict.
flags = [ '-D=' + flag for flag in flags ] if flags else []
toolcmd = TOOL_TO_COMMAND[SPTHY]
jobs = []
if not argdict[SPTHY]:
jobs = [TamarinJob([toolcmd, input_file] + lemmacli + flags, lemma)]
else:
jobs = [TamarinJob([toolcmd, input_file] + lemmacli + flags + \
list(tuple), lemma) for tuple in itertools.product(*argdict[SPTHY])]
return jobs
def std_signal_handler(sig, frame):
# Standard signal handler that exits.
# We use this to catch SIGINT
print("Std sig handler called")
sys.exit(1)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser._action_groups.pop()
required = parser.add_argument_group('required arguments')
optional = parser.add_argument_group('optional arguments')
# The source file we built the models from
required.add_argument('-file', type=str, help='the sapic file', required=True)
optional.add_argument('-l', '--lemma', action='extend', nargs='+', type=str,
help='prove the given lemmas. If no lemmas are \
specified, the tool assumes that there is only one \
lemma in the given .spthy')
# Flags for Tamarin's preprocessor. Used during file/model generation
optional.add_argument('-D', '--defines', action='extend', nargs='+', type=str,
help="flags for Tamarin's preprocessor")
# Args for ProVerif.
optional.add_argument('-p', '--proverif', action='append',
nargs='*', type=str, help='arguments for ProVerif')
# Args for Tamarin
optional.add_argument('-t', '--tamarin', action='append',
nargs='*', type=str, help='arguments for Tamarin')
# Heuristics for Tamarin
optional.add_argument('-H', '--heuristic', action='extend',
nargs='+', type=str, choices=HEURISTICS,
help='heuristics Tamarin should use')
# Args for Deepsec
optional.add_argument('-d', '--deepsec', action='append',
nargs='*', type=str, help='arguments for Deepsec')
# Flag for Tamarin diff mode
optional.add_argument('--diff', action='store_true',
help="use Tamarin's diff mode for file generation")
# Flag for GSVerif
optional.add_argument('--gs', action='store_true',
help="use GSVerif on the exported ProVerif file.")
# Custom Tamarin command
optional.add_argument('-tname', '--tname', action='store', type=str,
help='customize how Tamarin is called; defaults to \
"tamarin-prover"')
# Custom ProVerif command
optional.add_argument('-pname', '--pname', action='store', type=str,
help='customize how ProVerif is called; defaults to \
"proverif"')
# Custom Deepsec command
optional.add_argument('-dname', '--dname', action='store', type=str,
help='customize how Deepsec is called; defaults to \
"deepsec"')
# Custom timeout
optional.add_argument('-to', '--timeout', action='store',
type=int, help='timeout for the jobs')
args = parser.parse_args()
# Error handling
if args.deepsec is None and args.tamarin is None and args.proverif is None:
eprint("Provide command line arguments for at least one tool!")
sys.exit(1)
# Change tool commands if specified
if args.tname:
TAMARIN_COMMAND = args.tname
if args.dname:
DEEPSEC_COMMAND = args.dname
if args.pname:
PROVERIF_COMMAND = args.pname
if args.timeout:
JOB_TIMEOUT = args.timeout
# Map tools to their command
TOOL_TO_COMMAND = { SPTHY: TAMARIN_COMMAND, PROVERIF: PROVERIF_COMMAND,
DEEPSEC: DEEPSEC_COMMAND }
# Extract the list of lemmas
lemmas = args.lemma if args.lemma else []
# Extract the list of preprocessor Flags
flags = args.defines if args.defines else []
# Create a dict that maps tool (Tamarin=spthy, ProVerif=proverif,
# and Deepsec=deepsec) to the parsed args
argdict = dict()
if args.deepsec:
argdict[DEEPSEC] = add_double_dashes_to_arguments(args.deepsec)
if args.proverif:
argdict[PROVERIF] = add_dashes_to_arguments(args.proverif)
if args.tamarin:
if args.tamarin[0]:
# Actual arguments, not only -t
tamarinargs = add_double_dashes_to_arguments(args.tamarin)
argdict[SPTHY] = tamarinargs
else:
tamarinargs = []
if args.heuristic:
heuristics = [ '--heuristic=' + heuristic for heuristic in \
args.heuristic ]
tamarinargs.append(heuristics)
argdict[SPTHY] = tamarinargs
# Generate desired model files from the input file
generate_files(args.file, flags, lemmas, argdict, args.diff, args.gs)
# Generate the jobs that we want to concurrently execute
jobs = generate_jobs(args.file, lemmas, argdict, flags)
# For every lemma/joblist start a concurrent execution of its jobs.
# This first layer of concurrency uses a worker per lemma/joblist
processes = [None] * len(jobs)
# Register signal handler
# This handler is inherited by the child processes
signal.signal(signal.SIGINT, std_signal_handler)
signal.signal(signal.SIGTERM, std_signal_handler)
for i in range(len(processes)):
# Processes[i] = jobs[i] + queue for communication
q = multiprocessing.SimpleQueue()
processes[i] = multiprocessing.Process(target=execute_joblist,
args=(jobs[i], q, )), q
try:
# Start all the processes
for p, q in processes:
p.start()
# Collect the results of the Processes
results = [ q.get() for p, q in processes ]
# q.get() blocks until a result is available
finally:
for p, q in processes:
p.join()
# Current results list contains results with timeouts, errors etc.
# Sort these out of the list, and report them.
results = report_results(results)
sys.exit(0)