-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathleakexpand.py
58 lines (46 loc) · 1.72 KB
/
leakexpand.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
import os
import sys
from collections import defaultdict
import yaml
import jinja2
spec, = sys.argv[1:]
leakname = os.path.splitext(os.path.basename(spec))[0]
class Hook(object):
def __init__(self):
self.probes = {}
@property
def func(self):
return '{}$${}'.format(self.cls, self.method.replace(':', '$_'))
hooks = defaultdict(Hook)
with open(spec, 'r') as f:
probes = yaml.load(f.read())
for probe, pattrs in probes.items():
probeargs = pattrs['args']
for cls, methods in pattrs['methods'].items():
for method in methods:
name = method['name']
assert name[:1] == '-', "only instance methods are supported yet"
sel = name[1:]
rtype = method['rtype']
hook = hooks[(cls, sel)]
hook.cls = cls
hook.method = sel # TODO: automatic type discovery
hook.rtype = rtype
hook.args = method['args']
hook.probes['{}_{}'.format(leakname, probe.replace('__', '_')).upper()] = probeargs
def expand(template, output, **kwargs):
with open(template, 'r') as t:
text = jinja2.Template(t.read()).render(**kwargs)
with open(output, 'w+') as o:
o.write(text)
path_prefix = 'leaks/{}'.format(leakname)
os.makedirs(path_prefix)
path = lambda suffix: '{}/{}.{}'.format(path_prefix, leakname, suffix)
expand('hook.template', path('m'),
leakname=leakname, hooks=hooks.values())
expand('provider.template', path('provider.d'),
leakname=leakname, probes=[(name, attrs['args']) for name, attrs in probes.items()])
expand('makefile.template', path_prefix + '/Makefile',
leakname=leakname)
expand('lldb.template', path('lldb'),
leakname=leakname)