forked from google/kernel-sanitizers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kasan_symbolize.py
executable file
·368 lines (307 loc) · 10.7 KB
/
kasan_symbolize.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
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
#!/usr/bin/python
# Tool for symbolizing stack traces in BUG reports, mainly those produced
# by KASAN.
from __future__ import print_function
from collections import defaultdict
import getopt
import os
import re
import sys
import subprocess
# Matches the timestamp or a thread/cpu number prefix of a log line.
BRACKET_PREFIX_RE = re.compile(
'^(?P<time>\[ *[TC0-9\.]+\]) ?(?P<body>.*)$'
)
# A hexadecimal number without the leading 0x.
HEXNUM_RE = '[0-9A-Fa-f]+'
# An address in the form [<ffffffff12345678>].
FRAME_ADDR_RE = (
'((\[\<(?P<addr>' + HEXNUM_RE + ')\>\]) )'
)
# A function name with an offset and function size, plus an optional module
# name, e.g.:
# __asan_load8+0x64/0x66
FRAME_BODY_RE = (
'(?P<body>' +
'(?P<function>[^\+]+)' +
'\+' +
'0x(?P<offset>' + HEXNUM_RE + ')' +
'/' +
'0x(?P<size>' + HEXNUM_RE + ')' +
')'
)
# Matches a single stacktrace frame (without time or thread/cpu number prefix).
FRAME_RE = re.compile(
'^' +
'(?P<prefix> *)' +
FRAME_ADDR_RE + '?' +
'((?P<precise>\?) )?' +
FRAME_BODY_RE +
'( \[(?P<module>.+)\])?' +
'$'
)
# Matches the 'RIP:' line in BUG reports.
RIP_RE = re.compile(
'^' +
'(?P<prefix>RIP: ' + HEXNUM_RE + ':)' +
FRAME_BODY_RE +
'$'
)
# Matches the 'lr :' and 'pc :' lines in BUG reports.
LR_RE = re.compile(
'^' +
'(?P<prefix>(lr|pc) : )' +
FRAME_BODY_RE +
'$'
)
# Matches sanitizers' 'in fuction+0x42/0x420' headers.
KSAN_RE = re.compile(
'^' +
'(?P<prefix>(BUG:).+in )' +
FRAME_BODY_RE +
'$'
)
# Matches a single line of `nm -S` output.
NM_RE = re.compile(
'^(?P<offset>' + HEXNUM_RE + ')( (?P<size>' + HEXNUM_RE + '))?' +
' [a-zA-Z] (?P<symbol>[^ ]+)$'
)
class Symbolizer(object):
def __init__(self, binary_path):
self.proc = subprocess.Popen(
['addr2line', '-f', '-i', '-e', binary_path],
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def process(self, addr):
self.proc.stdin.write(addr + '\n')
self.proc.stdin.write('ffffffffffffffff\n')
self.proc.stdin.flush()
result = []
while True:
func = self.proc.stdout.readline().rstrip()
fileline = self.proc.stdout.readline().rstrip()
if func == '??':
if len(result) == 0:
self.proc.stdout.readline()
self.proc.stdout.readline()
return result
result.append((func, fileline))
def close(self):
self.proc.kill()
self.proc.wait()
def find_file(path, name):
path = os.path.expanduser(path)
for root, dirs, files in os.walk(path):
if name in files:
return os.path.join(root, name)
return None
class SymbolOffsetTable(object):
"""A table of symbol offsets.
There can be several symbols with similar names. The only possible way to
distinguish between them is by their size. For each symbol name we keep a
mapping from the sizes of symbols with that name to their offsets.
To conform with the kernel behavior, instead of the actual symbol size
returned by nm we store the difference between the next symbol's offset and
this symbol's offset.
"""
def __init__(self, binary_path):
output = subprocess.check_output(['nm', '-Sn', binary_path])
self.offsets = defaultdict(dict)
prev_symbol = None
prev_offset, prev_size = 0, 0
for line in output.split('\n'):
match = NM_RE.match(line)
if match != None:
offset = int(match.group('offset'), 16)
size = 0 if not match.group('size') else int(match.group('size'), 16)
if prev_symbol:
ksyms_size = offset - prev_offset
if ksyms_size >= prev_size:
prev_size = ksyms_size
self.offsets[prev_symbol][prev_size] = prev_offset
prev_symbol = match.group('symbol')
prev_offset, prev_size = offset, size
self.offsets[prev_symbol][0] = prev_offset
def lookup_offset(self, symbol, size):
offsets = self.offsets.get(symbol)
if offsets is None:
return None
if (size not in offsets):
return None
return offsets[size]
class ReportProcessor(object):
def __init__(self, linux_path, strip_path):
self.strip_path = strip_path
self.linux_path = linux_path
self.module_symbolizers = {}
self.module_offset_tables = {}
self.loaded_files = {}
def process_input(self, context_size, questionable):
for line in sys.stdin:
line = line.rstrip()
line = self.strip_time(line)
self.process_line(line, context_size, questionable)
def strip_time(self, line):
# Strip time prefix if present.
match = BRACKET_PREFIX_RE.match(line)
if match != None:
line = match.group('body')
# Try to strip thread/cpu number prefix if present.
match = BRACKET_PREFIX_RE.match(line)
if match != None:
line = match.group('body')
return line
def process_line(self, line, context_size, questionable):
# |RIP_RE| is less general than |FRAME_RE|, so try it first.
match = None
for regexp in [RIP_RE, LR_RE, KSAN_RE, FRAME_RE]:
match = regexp.match(line)
if match:
break
if match == None:
print(line)
return
prefix = match.group('prefix')
try:
addr = match.group('addr')
except IndexError:
addr = None
body = match.group('body')
precise = True
if 'precise' in match.groupdict().keys():
precise = not match.group('precise')
# Don't print frames with '?' until user asked otherwise.
if not precise and not questionable:
if '<EOI>' in match.group('prefix'):
print(match.group('prefix'))
return
function = match.group('function')
offset = match.group('offset')
size = match.group('size')
try:
module = match.group('module')
except IndexError:
module = None
if module == None:
module = 'vmlinux'
else:
module += '.ko'
if not self.load_module(module):
print(line)
return
symbolizer = self.module_symbolizers[module]
loader = self.module_offset_tables[module]
symbol_offset = loader.lookup_offset(function, int(size, 16))
if symbol_offset is None:
print(line)
return
instruction_offset = int(offset, 16)
module_addr = hex(symbol_offset + instruction_offset - 1);
frames = symbolizer.process(module_addr)
if len(frames) == 0:
print(line)
return
for i, frame in enumerate(frames):
inlined = (i + 1 != len(frames))
func, fileline = frame[0], frame[1]
fileline = fileline.split(' (')[0] # strip ' (discriminator N)'
self.print_frame(inlined, precise, prefix, addr, func, fileline,
body)
self.print_lines(fileline, context_size)
def load_module(self, module):
if module in self.module_symbolizers.keys():
return True
module_path = find_file(self.linux_path, module)
if module_path == None:
return False
self.module_symbolizers[module] = Symbolizer(module_path)
self.module_offset_tables[module] = SymbolOffsetTable(module_path)
return True
def load_file(self, path):
if path in self.loaded_files.keys():
return self.loaded_files[path]
try:
with open(path) as f:
self.loaded_files[path] = f.readlines()
return self.loaded_files[path]
except:
return None
def print_frame(self, inlined, precise, prefix, addr, func, fileline, body):
if self.strip_path != None:
fileline_parts = fileline.split(self.strip_path, 1)
if len(fileline_parts) >= 2:
fileline = fileline_parts[1].lstrip('/')
if inlined:
if addr != None:
addr = ' inline ';
body = func
precise = '' if precise else '? '
if addr != None:
print('%s[<%s>] %s%s %s' % (prefix, addr, precise, body, fileline))
else:
print('%s%s%s %s' % (prefix, precise, body, fileline))
def print_lines(self, fileline, context_size):
if context_size == 0:
return
fileline = fileline.split(':')
filename, linenum = fileline[0], fileline[1]
try:
linenum = int(linenum)
except:
return
assert linenum >= 0
if linenum == 0: # addr2line failed to restore correct line info
return
linenum -= 1 # addr2line reports line numbers starting with 1
start = max(0, linenum - context_size / 2)
end = start + context_size
lines = self.load_file(filename)
if not lines:
return
for i, line in enumerate(lines[start:end]):
print(' {0:5d} {1}'.format(i + start + 1, line), end=' ')
def finalize(self):
for module, symbolizer in self.module_symbolizers.items():
symbolizer.close()
def print_usage():
print('Usage: {0} --linux=<linux path>'.format(sys.argv[0]), end=' ')
print('[--strip=<strip path>]', end=' ')
print('[--before=<lines before>]', end=' ')
print('[--after=<lines after>]', end=' ')
print('[--questionable]', end=' ')
print()
def main():
try:
opts, args = getopt.getopt(sys.argv[1:], 'l:s:c:q:',
['linux=', 'strip=', 'context=', 'questionable'])
except:
print_usage()
sys.exit(1)
linux_path = os.getcwd()
strip_path = os.getcwd()
context_size = 0
questionable = False
for opt, arg in opts:
if opt in ('-l', '--linux'):
linux_path = arg
elif opt in ('-s', '--strip'):
strip_path = arg
elif opt in ('-c', '--context'):
context_size = arg
elif opt in ('-q', '--questionable'):
questionable = True
try:
if isinstance(context_size, str):
context_size = int(context_size)
except:
print_usage()
sys.exit(1)
processor = ReportProcessor(linux_path, strip_path)
processor.process_input(context_size, questionable)
processor.finalize()
sys.exit(0)
if __name__ == '__main__':
main()