Skip to content

Commit 456883a

Browse files
Mark R. Tuttlemarkrtuttle
authored andcommitted
Strip external source locations from error traces.
It is possible for a source location to point to code outside of the source tree (like an inlined function definition in a system header file). This patch replaces all such external source locations in error traces with a special 'missing' source location, inhibiting linking to external source code.
1 parent ac69b29 commit 456883a

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
__pycache__/
2+
build/
13
cbmc-viewer
24
cbmc_viewer.egg-info/
5+
dist/
36
easy_install
47
easy_install-*
58
make-coverage
@@ -10,5 +13,3 @@ make-result
1013
make-source
1114
make-symbol
1215
make-trace
13-
build/
14-
dist/

cbmc_viewer/markup_link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def path_to_file(dst, src):
4444
def link_text_to_file(text, to_file, from_file=None):
4545
"""Link text to a file in the source tree."""
4646

47-
if srcloct.is_builtin(to_file):
47+
if srcloct.is_builtin(to_file) or srcloct.is_missing(to_file):
4848
return html.escape(str(text))
4949

5050
from_file = from_file or '.'
@@ -54,7 +54,7 @@ def link_text_to_file(text, to_file, from_file=None):
5454
def link_text_to_line(text, to_file, line, from_file=None):
5555
"""Link text to a line in a file in the source tree."""
5656

57-
if srcloct.is_builtin(to_file):
57+
if srcloct.is_builtin(to_file) or srcloct.is_missing(to_file):
5858
return html.escape(str(text))
5959

6060
from_file = from_file or '.'

cbmc_viewer/srcloct.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,29 @@ def relpath(path, root):
104104
required=True
105105
)
106106

107+
################################################################
108+
# A "missing source location" for use when the source location really
109+
# is missing from cbmc output, or when the source location might
110+
# otherwise point to code outside of the source tree (like an inlined
111+
# function definition in a system header file).
112+
113+
MISSING = 'MISSING'
114+
MISSING_SRCLOC = {'file': MISSING, 'function': MISSING, 'line': 0}
115+
116+
def is_missing(name):
117+
"""The name of a file or function is missing."""
118+
return name == MISSING
119+
120+
################################################################
121+
# Construct a viewer source location from cbmc source locations
122+
# appearing in cbmc output.
123+
107124
def make_srcloc(path, func, line, wkdir, root):
108125
"""Make a viewer source location from a CBMC source location."""
109126

110127
if path is None or line is None:
111128
logging.info("Generating a viewer srcloc for a missing CBMC srcloc.")
112-
return {'file': 'MISSING', 'function': 'MISSING', 'line': 0}
129+
return MISSING_SRCLOC
113130

114131
path = normpath(path)
115132
if is_builtin(path):

cbmc_viewer/tracet.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,11 @@ def handle_duplicate(key, val1, val2):
150150
logging.warning("Found duplicate traces for property %s", key)
151151

152152
traces = util.merge_dicts(trace_lists, handle_duplicate)
153-
return {name: close_function_stack_frames(trace)
154-
for name, trace in traces.items()}
153+
traces = {name: close_function_stack_frames(trace)
154+
for name, trace in traces.items()}
155+
traces = {name: strip_external_srclocs(trace)
156+
for name, trace in traces.items()}
157+
return traces
155158

156159
################################################################
157160

@@ -621,6 +624,30 @@ def pop_stack(stack):
621624

622625
return trace
623626

627+
################################################################
628+
629+
def strip_external_srclocs(trace):
630+
"""Strip source locations pointing to code outside the source tree.
631+
632+
It is possible for a source location to point to code outside of
633+
the source tree (like an inlined function definition in a system
634+
header file). This function replaces all such external source
635+
locations in the trace with the special 'missing' source location.
636+
"""
637+
638+
for step in trace:
639+
640+
# The source locations produced by srcloct have paths
641+
# relative to the source root for all files under the
642+
# root, and have absolute paths for all other files.
643+
644+
if step['location']['file'].startswith('/'):
645+
step['location'] = srcloct.MISSING_SRCLOC
646+
if step['detail'].get('location'):
647+
if step['detail']['location']['file'].startswith('/'):
648+
step['detail']['location'] = srcloct.MISSING_SRCLOC
649+
return trace
650+
624651
################################################################
625652
# make-trace
626653

0 commit comments

Comments
 (0)