Skip to content

Commit d8564dc

Browse files
committed
[HWASan] Allow to linkify symbolizer output.
Reviewed By: eugenis Differential Revision: https://reviews.llvm.org/D124950
1 parent af4cf1c commit d8564dc

File tree

2 files changed

+104
-10
lines changed

2 files changed

+104
-10
lines changed

compiler-rt/lib/hwasan/scripts/hwasan_symbolize

Lines changed: 77 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
#===- lib/hwasan/scripts/hwasan_symbolize ----------------------------------===#
33
#
44
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -16,6 +16,8 @@ from __future__ import unicode_literals
1616

1717
import argparse
1818
import glob
19+
import html
20+
import json
1921
import mmap
2022
import os
2123
import re
@@ -106,10 +108,40 @@ class Symbolizer:
106108
self.__log = False
107109
self.__warnings = set()
108110
self.__index = {}
111+
self.__link_prefixes = []
112+
self.__html = False
113+
114+
def enable_html(self, enable):
115+
self.__html = enable
109116

110117
def enable_logging(self, enable):
111118
self.__log = enable
112119

120+
def maybe_escape(self, text):
121+
if self.__html:
122+
# We need to manually use   for leading spaces, html.escape does
123+
# not do that, and HTML ignores them.
124+
spaces = 0
125+
for i, c in enumerate(text):
126+
spaces = i
127+
if c != ' ':
128+
break
129+
text = text[spaces:]
130+
return spaces * ' ' + html.escape(text)
131+
return text
132+
133+
def print(self, line, escape=True):
134+
if escape:
135+
line = self.maybe_escape(line)
136+
if self.__html:
137+
line += '<br/>'
138+
print(line)
139+
140+
def read_linkify(self, filename):
141+
with open(filename, 'r') as fd:
142+
data = json.load(fd)
143+
self.__link_prefixes = [(e["prefix"], e["link"]) for e in data]
144+
113145
def __open_pipe(self):
114146
if not self.__pipe:
115147
opt = {}
@@ -207,6 +239,26 @@ class Symbolizer:
207239
except Symbolizer.__EOF:
208240
pass
209241

242+
def maybe_linkify(self, file_line):
243+
if not self.__html or not self.__link_prefixes:
244+
return file_line
245+
filename, line_col = file_line.split(':', 1)
246+
if not line_col:
247+
line = '0' # simplify the link generation
248+
else:
249+
line = line_col.split(':')[0]
250+
longest_prefix = max((
251+
(prefix, link) for prefix, link in self.__link_prefixes
252+
if filename.startswith(prefix)),
253+
key=lambda x: len(x[0]), default=None)
254+
if longest_prefix is None:
255+
return file_line
256+
else:
257+
prefix, link = longest_prefix
258+
return '<a href="{}">{}</a>'.format(
259+
html.escape(link.format(file=filename[len(prefix):], line=line,
260+
file_line=file_line, prefix=prefix)), file_line)
261+
210262
def build_index(self):
211263
for p in self.__binary_prefixes:
212264
for dname, _, fnames in os.walk(p):
@@ -229,16 +281,22 @@ def symbolize_line(line, symbolizer_path):
229281
frames = list(symbolizer.iter_call_stack(binary, buildid, addr))
230282

231283
if len(frames) > 0:
232-
print("%s#%s%s%s in %s" % (match.group(1), match.group(2),
233-
match.group(3), frames[0][0], frames[0][1]))
284+
symbolizer.print(
285+
symbolizer.maybe_escape(
286+
"%s#%s%s%s in " % (match.group(1), match.group(2), match.group(3),
287+
frames[0][0])
288+
) + symbolizer.maybe_linkify(frames[0][1]),
289+
escape=False)
234290
for i in range(1, len(frames)):
235291
space1 = ' ' * match.end(1)
236292
space2 = ' ' * (match.start(4) - match.end(1) - 2)
237-
print("%s->%s%s in %s" % (space1, space2, frames[i][0], frames[i][1]))
293+
symbolizer.print(
294+
symbolizer.maybe_escape("%s->%s%s in " % (space1, space2, frames[i][0]))
295+
+ symbolizer.maybe_linkify(frames[i][1]), escape=False)
238296
else:
239-
print(line.rstrip())
297+
symbolizer.print(line.rstrip())
240298
else:
241-
print(line.rstrip())
299+
symbolizer.print(line.rstrip())
242300

243301
def save_access_address(line):
244302
global last_access_address, last_access_tag
@@ -280,10 +338,10 @@ def process_stack_history(line, symbolizer, ignore_tags=False):
280338
tag_offset = local[5]
281339
if not ignore_tags and (tag_offset is None or base_tag ^ tag_offset != last_access_tag):
282340
continue
283-
print('')
284-
print('Potentially referenced stack object:')
285-
print(' %d bytes inside variable "%s" in stack frame of function "%s"' % (obj_offset, local[2], local[0]))
286-
print(' at %s' % (local[1],))
341+
symbolizer.print('')
342+
symbolizer.print('Potentially referenced stack object:')
343+
symbolizer.print(' %d bytes inside variable "%s" in stack frame of function "%s"' % (obj_offset, local[2], local[0]))
344+
symbolizer.print(' at %s' % (local[1],))
287345
return True
288346
return False
289347

@@ -295,6 +353,8 @@ parser.add_argument('--symbols', action='append')
295353
parser.add_argument('--source', action='append')
296354
parser.add_argument('--index', action='store_true')
297355
parser.add_argument('--symbolizer')
356+
parser.add_argument('--linkify', type=str)
357+
parser.add_argument('--html', action='store_true')
298358
parser.add_argument('args', nargs=argparse.REMAINDER)
299359
args = parser.parse_args()
300360

@@ -380,10 +440,17 @@ if args.v:
380440
print()
381441

382442
symbolizer = Symbolizer(symbolizer_path, binary_prefixes, paths_to_cut)
443+
symbolizer.enable_html(args.html)
383444
symbolizer.enable_logging(args.d)
384445
if args.index:
385446
symbolizer.build_index()
386447

448+
if args.linkify:
449+
if not args.html:
450+
print('Need --html to --linkify', file=sys.stderr)
451+
sys.exit(1)
452+
symbolizer.read_linkify(args.linkify)
453+
387454
for line in sys.stdin:
388455
if sys.version_info.major < 3:
389456
line = line.decode('utf-8')
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// RUN: %clang_hwasan -Wl,--build-id -g %s -o %t
2+
// RUN: echo '[{"prefix": "'"$(realpath $(dirname %s))"'/", "link": "http://test.invalid/{file}:{line}"}]' > %t.linkify
3+
// RUN: %env_hwasan_opts=symbolize=0 not %run %t 2>&1 | hwasan_symbolize --html --symbols $(dirname %t) --index | FileCheck %s
4+
// RUN: %env_hwasan_opts=symbolize=0 not %run %t 2>&1 | hwasan_symbolize --html --linkify %t.linkify --symbols $(dirname %t) --index | FileCheck --check-prefixes=CHECK,LINKIFY %s
5+
// RUN: %env_hwasan_opts=symbolize=0 not %run %t 2>&1 | hwasan_symbolize --symbols $(dirname %t) --index | FileCheck %s
6+
7+
// There are currently unrelated problems on x86_64, so skipping for now.
8+
// REQUIRES: android
9+
// REQUIRES: stable-runtime
10+
11+
#include <sanitizer/hwasan_interface.h>
12+
#include <stdlib.h>
13+
14+
static volatile char sink;
15+
16+
int main(int argc, char **argv) {
17+
__hwasan_enable_allocator_tagging();
18+
char *volatile x = (char *)malloc(10);
19+
sink = x[100];
20+
// LINKIFY: <a href="http://test.invalid/hwasan_symbolize.cpp:[[@LINE-1]]">
21+
// CHECK: hwasan_symbolize.cpp:[[@LINE-2]]
22+
// CHECK: Cause: heap-buffer-overflow
23+
// CHECK: allocated here:
24+
// LINKIFY: <a href="http://test.invalid/hwasan_symbolize.cpp:[[@LINE-6]]">
25+
// CHECK: hwasan_symbolize.cpp:[[@LINE-7]]
26+
return 0;
27+
}

0 commit comments

Comments
 (0)