-
Notifications
You must be signed in to change notification settings - Fork 3
/
trace.py
executable file
·184 lines (170 loc) · 7.25 KB
/
trace.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
#!/usr/bin/python3
import os
import sys
import time
import argparse
import subprocess
DIR = os.path.dirname(os.path.abspath(os.path.realpath(__file__)))
ENV = {"LD_LIBRARY_PATH": os.path.join(DIR, "llvm/lib")}
NPROC = 200
def build_cmd(ostag, out, reg, mem, index=None, replay=None, iolist=None, nullpad=True,compact=False,cntpct=0,timebounded=0,persist=False):
if index != None:
outdir = os.path.join(DIR, out, "qemu_"+str(index))
else:
outdir = os.path.join(DIR, out)
if not os.path.exists(outdir):
os.makedirs(outdir)
if ostag == "romulus":
arch = 'arm'
cpu = 'arm1176'
memrange = '0x80000000-0x90000000'
elif ostag == "beagle":
arch = 'arm'
cpu = 'cortex-a8'
memrange = '0x80000000-0x90000000'
elif ostag in ["linux", "linux_virt", "freebsd", "riscos"]:
arch = 'arm'
cpu = 'cortex-a7'
memrange = '0x00000000-0x3f000000'
elif ostag in ["sabre", "vxwork"]:
arch = 'arm'
cpu = 'cortex-a9'
memrange = '0x10000000-0x18000000'
elif ostag == "nuri":
arch = 'arm'
cpu = 'cortex-a9'
memrange = '0x40000000-0x80000000'
elif ostag == "steamlink":
arch = 'arm'
cpu = 'cortex-a9'
memrange = '0x1000000-0x12000000'
elif ostag == "wrt":
arch = 'mipsel'
cpu = '4KEc'
memrange = '0-0x1000000'
if replay == None:
if index != None:
candi = [i*NPROC+index for i in range(int(32/NPROC)+1) if i*NPROC+index <= 32]
enum_args = [
'enuml1', f'l1={"|".join([hex((1<<i)&0xffffffff)[2:] for i in range(33)])}',
'enuml2', f'l2={"|".join([hex((1<<i)&0xffffffff)[2:] for i in candi])}',
'enuml3', f'l3={"|".join([hex((1<<i)&0xffffffff)[2:] for i in range(33)])}',
#'enuml4', f'l4={"|".join([hex((1<<i)&0xffffffff)[2:] for i in range(33)])}',
]
else:
enum_args = [
#'enuml1', f'l1={"|".join([hex((1<<i)&0xffffffff)[2:] for i in range(33)])}',
#'enuml1', f'l1={"|".join([hex(i)[2:] for i in range(256)])}',
'enuml1', 'l1=23|2f',
'enuml2', 'l2=0',
'enuml3', f'l3={"|".join([hex((1<<i)&0xffffffff)[2:] for i in range(33)])}',
#'enuml2',
#'enuml3',
]
if ostag == "wrt":
enum_args = None
ioargs = [f'tracedir={outdir}', f'mem={mem}', f'cpu={reg}', 'tracelimit', 'interrupt']
if enum_args:
ioargs.extend([f'{",".join(enum_args)}', 'auto'])
else:
ioargs = [f'tracedir={outdir}', f'mem={mem}', f'cpu={reg}', f'replay={replay}',
'tracelimit', 'interrupt']
# Raspberry Pi Linux: calibrate timer with random IRQ after machine state reset
if ostag == "linux":
#ioargs.append('calib')
ioargs.append(f'cntpct={hex(cntpct)[2:]}')
if ostag == "riscos":
ioargs.append('nosvc')
if ostag == "vxwork":
ioargs.append('clearirq')
if ostag == "steamlink":
nullpad = False
#timebounded = 2*1000 # 1 seconds
#persist = True
if ostag == "wrt":
nullpad = False
if nullpad:
ioargs.append('null')
if compact:
ioargs.extend(['pack', 'dedup'])
if persist:
ioargs.append('persist')
if iolist:
ioargs.append(f'iolist={iolist}')
#ioargs.append('debug')
if timebounded != 0:
ioargs.append(f'timebounded={timebounded}')
cmd = [os.path.join(DIR, f"fuzzer/build/{arch}-softmmu/qemu-system-{arch}"),
'-machine', f'rehosting,mem-map=MEM {memrange}',
'-panda', f'ioreplay:{",".join(ioargs)}',
'-cpu', f'{cpu}',
'-display', 'none',
]
print(cmd)
return cmd
def mc(ostag, outdir, reg, mem, null=True,replaydir=None,iolist=None,tar=True,cntpct=0):
if replaydir:
comp = None
cleaner = None
rmcmd = None
for d,_,fs in os.walk(replaydir):
replaylog = [os.path.join(d,f) for f in fs]
for bi in range(0, len(replaylog), NPROC):
pool = [subprocess.Popen(
build_cmd(ostag, outdir, reg, mem, index=bi+i,compact=True,iolist=iolist,cntpct=cntpct,
replay=os.path.join(replaydir,f"replay_{str(bi+i)}"),nullpad=null),
env=ENV)
for i in range(min(NPROC, len(replaylog)-bi))]
map(lambda x: os.sched_setaffinity(pool[x].pid, {hex(1<<x)}), range(len(pool)))
while None in map(lambda x: x.poll(), pool):
time.sleep(60)
print(map(lambda x: x.poll(), pool))
# async compress results
if comp:
comp.wait()
# pipeline a remove cmd, cause tar --remove-files is sooooo sloooooow
if cleaner:
cleaner.wait()
cleaner = subprocess.Popen(rmcmd)
if tar:
compcmd = ['tar', '-czf', os.path.join(outdir, f'{str(bi)}_{str(bi+len(pool)-1)}.tar.gz')] # ,'--remove-files']
rmcmd = ['rm', '-rf']
compcmd.extend([os.path.join(outdir, f"qemu_{str(bi+i)}") for i in range(len(pool))])
rmcmd.extend([os.path.join(outdir, f"qemu_{str(bi+i)}") for i in range(len(pool))])
comp = subprocess.Popen(compcmd)
if comp:
comp.wait()
if cleaner:
cleaner.wait()
subprocess.Popen(rmcmd).wait()
else:
pool = [subprocess.Popen(build_cmd(ostag, outdir, reg, mem, index=i,iolist=iolist,nullpad=null,compact=True,cntpct=cntpct),env=ENV) for i in range(NPROC)]
map(lambda x: os.sched_setaffinity(pool[x].pid, {hex(1<<x)}), range(NPROC))
while None in map(lambda x: x.poll(), pool):
time.sleep(60)
print(map(lambda x: x.poll(), pool))
def debug(ostag, outdir, reg, mem, null=True,iolist=None,replay=None,cntpct=0):
p = subprocess.Popen(build_cmd(ostag, outdir, reg, mem, iolist=iolist,replay=replay,nullpad=null,cntpct=cntpct), env=ENV)
os.sched_setaffinity(p.pid, {3})
try:
p.wait(timeout=4*60*60) # 4 hours timeout
except subprocess.TimeoutExpired:
p.kill()
p.wait()
print(p.poll())
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('ostag')
parser.add_argument('outdir')
parser.add_argument('reg')
parser.add_argument('mem')
parser.add_argument('--replay', '-r', default=None)
parser.add_argument('--iolist', '-l', default=None)
args = parser.parse_args()
outdir = os.path.join(DIR, args.outdir)
reg = os.path.join(DIR, args.reg)
mem = os.path.join(DIR, args.mem)
# Ensure that RaspberryPi Linux emulation has CNTPCT (emulated coprocessor) no less than the stored counter value to avoid (infinite) loops while irq_enter
# The stored counter value is reverse engineered through (timekeeper_advance)arch_counter_get_cntpct, mm._read_word(mm.translate(0x80DC0828))
#debug(args.ostag, outdir, reg, mem, replay=args.replay, iolist=args.iolist, cntpct=0x31186096)
mc(args.ostag, outdir, reg, mem, replaydir=args.replay, iolist=args.iolist, cntpct=0x31186096)