-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate.py
executable file
·89 lines (77 loc) · 3.25 KB
/
generate.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
#!/usr/bin/env python3
import argparse
import sys
import os.path as path
import logging
from functools import reduce
import coloredlogs
import z3
import pefile
log = logging.getLogger(__name__)
coloredlogs.install(level="INFO", logger=log)
from modelLang import Parser
from modelLang import Z3Backend
def write_testcase(testcase, fout):
with open(fout, "wb") as fp:
fp.write(testcase)
if __name__ == "__main__":
argparser = argparse.ArgumentParser(description="Interpret models and generate testcases")
argparser.add_argument('--asserts', '-A', action="append",
metavar="model", type=str, nargs="+",
default=[],
help="List of models to assert")
argparser.add_argument('--negates', '-N', action="append",
metavar="model", type=str, nargs="*",
default=[],
help="List of models to negate")
argparser.add_argument('--out', '-O', action="store",
metavar="outfile", type=str, nargs=1,
default="testcase",
help="Output file for testcase (default = 'testcase')")
argparser.add_argument('--var', '-V', action="store",
metavar="variable", type=str, nargs=1,
default="HEADER",
help="Name of the variable in the model representing the entire file (default 'HEADER')")
argparser.add_argument('--define', '-D', action="store", metavar="define",
type=lambda x: (x.split(":")[0],
int(x.split(":")[1])),
nargs="*",
help="List of constants in the model to overwrite. Syntax <constant name>:<new value>. E.g., FILESIZE:1024")
argparser.add_argument('--size', '-B', action="store", metavar="bytes",
type=int, default=None,
help="Size in bytes of the testcase to generate")
args = argparser.parse_args()
if len(args.asserts) == 0:
argparser.print_help()
sys.exit(0)
asserts = reduce(lambda x,y: x + [*y], args.asserts, list())
negates = reduce(lambda x,y: x + [*y], args.negates, list())
outfile = args.out
voi = args.var
size = args.size
defs = dict(args.define) if args.define else {}
z3_models_assert = []
z3_models_negate = []
for model in [*asserts, *negates]:
modelname = path.basename(model)
parser = Parser(ptype=Parser.ParserType.GENERATOR, input_size=size,
custom_defs=defs)
parser.parse_file(model)
backend = Z3Backend(name=modelname, voi=voi)
backend.exec_statements(parser.statements)
if model in asserts:
z3_models_assert.append(backend)
else:
z3_models_negate.append(backend)
backend = z3_models_assert[0]
for b in z3_models_assert[1:]:
backend &= b
for b in z3_models_negate:
backend &= ~b
solver = backend.solver
model = backend.model
if model:
testcase = backend.generate_testcase()
write_testcase(testcase, outfile)
pef = pefile.PE(outfile)
print(pef)