-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenTempl.py
executable file
·116 lines (103 loc) · 3.26 KB
/
genTempl.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
#!/usr/bin/env python2.7
"""
Generate syscalls from template.
"""
import os, sys
from gen import *
class Error(Exception) :
pass
def words(l) :
if '#' in l :
l = l[:l.index('#')]
return filter(None, l.strip().split(' '))
def lineWords(fn) :
for n,l in enumerate(file(fn, 'r')) :
ws = words(l)
if ws :
yield n+1, ws
def genArg(a) :
"""Generate a list of arg values for this argument generator."""
if a.startswith('0x') : # hex numbers
return [Num(int(a[2:], 16))]
elif a.startswith('0') and a != '0' : # octal numbers
return [Num(int(a[1:], 8))]
elif a.isdigit() : # decimal numbers
return [Num(int(a, 10))]
elif a.startswith('32[') : # vec32
if a[-1] != ']' :
Error("bad vec arg %r" % a)
vec = a[3:-1].split(',')
return [Vec32(*xs) for xs in genArgs(vec)]
elif a[0] == '[' : # vec64
if a[-1] != ']' :
Error("bad vec arg %r" % a)
vec = a[1:-1].split(',')
return [Vec64(*xs) for xs in genArgs(vec)]
elif a[0] == '{' : # union of several other arg types
if a[-1] != '}' :
Error("bad union arg %r" % a)
its = a[1:-1].split(';')
res = []
for it in its :
res += genArg(it)
return res
elif a[0] == '"' : # string literal as buffer
if a[-1] != '"' :
Error("bad string arg %r" % a)
return [StringZ(a[1:-1])]
elif a == 'fd' : # file descriptor options
# stdfile0 = "/", stdfile1 = tcp socket
return [Num(1), File("stuffhere"), StdFile(0), StdFile(1)]
elif a == 'fn' : # filename options
return [StringZ("/tmp/file9"), StringZ("/"), StringZ("/tmp"), Filename("#!/bin/sh\necho hi\n")]
elif a == 'str' : # initialized string/buffer options
return [StringZ("testing"), Alloc(256), Filename("stuffhere")]
elif a == 'buf' : # alloc uninitialized buffer options
return [Alloc(128), Alloc(4096)]
elif a == 'sz' : # sizes
return [Len()]
elif a == 'pid' : # proc id options
return [Num(0), Num(0xffffffffffffffff), ChildPid, MyPid] # skip PPid
else :
raise Error("bad arg type %r" % a)
def _cross(xs) :
if xs == [] :
yield []
else :
hds,tls = xs[0], xs[1:]
for hd in hds :
for tl in _cross(tls) :
yield [hd] + tl
def cross(xs) :
return _cross(list(xs))
def genArgs(gens) :
return cross(genArg(g) for g in gens)
TEST=0
def genCalls(nr, nm, args, notest) :
#print nr, nm, args
passed = 0
for n,xargs in enumerate(genArgs(args)) :
fn = 'inputs/%03d_%s_%03d' % (nr, nm, n)
#print fn, nr, n, nm, xargs
call = tuple([nr] + xargs)
writeFn(fn, mkSyscalls(call))
if TEST and not notest :
if test(fn) :
passed += 1
if TEST and not notest and not passed :
print nr, nm, "no pass"
def proc(fn) :
for lno,ws in lineWords(fn) :
notest = False
if not ws[0].isdigit() :
notest = True
ws = ws[1:]
call = int(ws[0])
name = ws[1]
args = ws[2:]
genCalls(call, name, args, notest)
def main() :
for fn in sys.argv[1:] :
proc(fn)
if __name__ == '__main__' :
main()