/
show.py
646 lines (528 loc) · 20.6 KB
/
show.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Handler for showing the testcase detail page."""
from builtins import object
from builtins import range
import cgi
import datetime
import jinja2
import re
from base import utils
from build_management import revisions
from build_management import source_mapper
from config import db_config
from crash_analysis import severity_analyzer
from datastore import data_handler
from datastore import data_types
from fuzzing import leak_blacklist
from google_cloud_utils import blobs
from handlers import base_handler
from libs import access
from libs import auth
from libs import form
from libs import handler
from libs import helpers
from libs.issue_management import issue_tracker_utils
from metrics import crash_stats
from system import environment
FIND_SIMILAR_ISSUES_OPTIONS = [{
'type': 'open',
'label': 'Open'
}, {
'type': 'all',
'label': 'All'
}]
CRASH_STATE_REGEXES = [
r'bad-cast to (.+) from.*', r'bad-cast to (.+)', r'(.+) +in +.+'
]
COMPILED_CRASH_STATE_REGEXES = [
re.compile(r, flags=re.IGNORECASE) for r in CRASH_STATE_REGEXES
]
MAX_PREVIEW_STACKTRACE_LINE_COUNT = 25
def _parse_suspected_cls(predator_result):
"""Parse raw suspected_cls into dict."""
if not predator_result:
return None
# The raw result contains some additional information that we don't need here.
# Everything we're concerned with is a part of the "result" object included
# with the response.
predator_result = predator_result['result']
return {
'found': predator_result.get('found'),
'suspected_project': predator_result.get('suspected_project'),
'suspected_components': predator_result.get('suspected_components'),
'changelists': predator_result.get('suspected_cls'),
'feedback_url': predator_result.get('feedback_url'),
'error_message': predator_result.get('error_message'),
}
def highlight_common_stack_frames(crash_stacktrace):
"""Highlights common stack frames between first two stacks."""
crash_stacks = [[]]
highlighted_crash_stacktrace_lines = []
old_frame_no = 0
stack_index = 0
stack_trace_line_format = '^ *#([0-9]+) *0x[0-9a-f]+ (.*)'
for line in crash_stacktrace.splitlines():
# Stacktrace separator prefix.
if stack_index and line.startswith('+-'):
break
match = re.match(stack_trace_line_format, line)
if match:
frame_no = int(match.group(1))
# This means we encountered another stack like free or alloc stack.
if old_frame_no > frame_no:
stack_index += 1
crash_stacks.append([])
crash_stacks[stack_index].append(match.group(2))
old_frame_no = frame_no
# If we have just one crash stack and no other stack,
# then nothing to highlight.
if stack_index == 0:
return crash_stacktrace
# Compare stack frames between first two stacks.
match_index = -1
start_index_crash_stack_1 = len(crash_stacks[0]) - 1
start_index_crash_stack_2 = len(crash_stacks[1]) - 1
while True:
if (crash_stacks[0][start_index_crash_stack_1] !=
crash_stacks[1][start_index_crash_stack_2]):
break
match_index = [start_index_crash_stack_1, start_index_crash_stack_2]
if not start_index_crash_stack_1:
break
if not start_index_crash_stack_2:
break
start_index_crash_stack_1 -= 1
start_index_crash_stack_2 -= 1
# No match found, nothing to highlight.
if match_index == -1:
return crash_stacktrace
old_frame_no = 0
stack_index = 0
frame_index = -1
for line in crash_stacktrace.splitlines():
match = re.match(stack_trace_line_format, line)
if match:
frame_no = int(match.group(1))
# This means we encountered another stack like free or alloc stack.
if old_frame_no > frame_no:
stack_index += 1
frame_index = -1
frame_index += 1
old_frame_no = frame_no
# We only care about highlighting the first two stacks.
if stack_index <= 1 and frame_index >= match_index[stack_index]:
line = '<b>%s</b>' % line
highlighted_crash_stacktrace_lines.append(line)
return '\n'.join(highlighted_crash_stacktrace_lines)
def filter_stacktrace(crash_stacktrace, crash_type, revisions_dict):
"""Clean up and format a stack trace for display."""
if not crash_stacktrace:
return ''
filtered_crash_lines = []
for line in crash_stacktrace.splitlines():
# Html escape line content to prevent XSS.
line = cgi.escape(line, quote=True)
line = source_mapper.linkify_stack_frame(line, revisions_dict)
filtered_crash_lines.append(line)
filtered_crash_stacktrace = '\n'.join(filtered_crash_lines)
if crash_type == leak_blacklist.DIRECT_LEAK_LABEL:
return leak_blacklist.highlight_first_direct_leak(filtered_crash_stacktrace)
return highlight_common_stack_frames(filtered_crash_stacktrace)
class Line(object):
"""Represent a stacktrace line."""
def __init__(self, line_number, content, important):
self.line_number = line_number
self.content = content
self.important = important
def __str__(self):
return 'Line(%d, "%s", %s)' % (self.line_number, self.content,
self.important)
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
return hash(self.__str__())
def to_dict(self):
return {
'lineNumber': self.line_number,
'content': self.content,
'important': self.important,
'type': 'Line'
}
class Gap(object):
"""Represent a gap in a previewed stacktrace."""
def __init__(self, size):
self.size = size
def __str__(self):
return 'Gap(%d)' % self.size
def __eq__(self, other):
return hash(self) == hash(other)
def __hash__(self):
return hash(self.__str__())
def to_dict(self):
return {'type': 'Gap', 'size': self.size}
def _is_line_important(line_content, frames):
"""Check if the line contains a frame; it means the line is
important."""
for frame in frames:
if frame in line_content:
return True
return False
def get_stack_frames(crash_state_lines):
"""Get the stack frames from the crash state. Sometimes the crash state
contains a type of crash, e.g. 'Bad-cast to content::RenderWidget from
content::RenderWidgetHostViewAura'. The stack frame is
'content::RenderWidget'."""
frames = []
for line in crash_state_lines:
added = False
for regex in COMPILED_CRASH_STATE_REGEXES:
matches = re.match(regex, line)
if matches:
frames.append(matches.group(1))
added = True
break
if not added:
frames.append(line)
return frames
def convert_to_lines(raw_stacktrace, crash_state_lines, crash_type):
"""Convert an array of string to an array of Line."""
if not raw_stacktrace or not raw_stacktrace.strip():
return []
raw_lines = raw_stacktrace.splitlines()
frames = get_stack_frames(crash_state_lines)
escaped_frames = [jinja2.escape(f) for f in frames]
combined_frames = frames + escaped_frames
# Certain crash types have their own customized frames that are not related to
# the stacktrace. Therefore, we make our best effort to preview stacktrace
# in a reasonable way; we preview around the the top of the stacktrace.
for unique_type in data_types.CRASH_TYPES_WITH_UNIQUE_STATE:
if crash_type.startswith(unique_type):
combined_frames = ['ERROR']
break
lines = []
for index, content in enumerate(raw_lines):
important = _is_line_important(content, combined_frames)
lines.append(Line(index + 1, content, important))
return lines
def _preview_stacktrace(lines,
limit=MAX_PREVIEW_STACKTRACE_LINE_COUNT,
padding=7):
"""Preview stacktrace around crash_state."""
preview_lines = set()
length = len(lines)
if length <= limit:
return []
for index, line in enumerate(lines):
if line.important:
this_preview_lines = set()
for i in range(index - padding, index + padding + 1):
if i < 0 or i >= length:
continue
this_preview_lines.add(lines[i])
if (len(preview_lines | this_preview_lines)) <= limit:
preview_lines |= this_preview_lines
preview_lines = sorted(list(preview_lines), key=lambda l: l.line_number)
preview_lines_with_gaps = []
previous_line_number = 0
for line in preview_lines:
if (line.line_number - 1) > previous_line_number:
preview_lines_with_gaps.append(
Gap(line.line_number - previous_line_number - 1))
preview_lines_with_gaps.append(line)
previous_line_number = line.line_number
if not preview_lines_with_gaps:
return []
if length > previous_line_number:
preview_lines_with_gaps.append(Gap(length - previous_line_number))
return preview_lines_with_gaps
def get_testcase_detail_by_id(testcase_id):
"""Get testcase detail for rendering the testcase detail page."""
testcase = access.check_access_and_get_testcase(testcase_id)
return get_testcase_detail(testcase)
def _get_revision_range_html_from_string(job_type, revision_range):
"""Return revision range html for a revision range and job type given a range
string."""
try:
start_revision, end_revision = revision_range.split(':')
except:
return 'Bad revision range.'
return _get_revision_range_html(job_type, start_revision, end_revision)
def _get_revision_range_html(job_type, start_revision, end_revision=None):
"""Return revision range html for a revision range and job type."""
if end_revision is None:
end_revision = start_revision
component_rev_list = revisions.get_component_range_list(
start_revision, end_revision, job_type)
if not component_rev_list:
return ('%s:%s (No component revisions found!)' % (start_revision,
end_revision))
return revisions.format_revision_list(component_rev_list)
def _get_blob_size_string(blob_key):
"""Return blob size string."""
blob_size = blobs.get_blob_size(blob_key)
if blob_size is None:
return None
return utils.get_size_string(blob_size)
def _format_reproduction_help(reproduction_help):
"""Format a reproduction help string as HTML (linkified with break tags)."""
if not reproduction_help:
return ''
return jinja2.utils.urlize(reproduction_help).replace('\n', '<br>')
def get_testcase_detail(testcase):
"""Get testcase detail for rendering the testcase detail page."""
config = db_config.get()
crash_address = testcase.crash_address
crash_state = testcase.crash_state
crash_state_lines = crash_state.strip().splitlines()
crash_type = data_handler.get_crash_type_string(testcase)
external_user = not access.has_access(job_type=testcase.job_type)
issue_url = issue_tracker_utils.get_issue_url(testcase)
metadata = testcase.get_metadata()
original_testcase_size = _get_blob_size_string(testcase.fuzzed_keys)
minimized_testcase_size = _get_blob_size_string(testcase.minimized_keys)
has_issue_tracker = bool(data_handler.get_issue_tracker_name())
fuzzer_display = data_handler.get_fuzzer_display(testcase)
formatted_reproduction_help = _format_reproduction_help(
data_handler.get_formatted_reproduction_help(testcase))
# When we have a HELP_TEMPLATE, ignore any default values set for HELP_URL.
if not formatted_reproduction_help:
reproduction_help_url = data_handler.get_reproduction_help_url(
testcase, config)
else:
reproduction_help_url = None
if not testcase.regression:
regression = 'Pending'
elif testcase.regression == 'NA':
regression = 'NA'
else:
regression = _get_revision_range_html_from_string(testcase.job_type,
testcase.regression)
fixed_full = None
if 'progression_pending' in metadata:
fixed = 'Pending'
elif not testcase.fixed:
fixed = 'NO'
elif testcase.fixed == 'NA':
fixed = 'NA'
elif testcase.fixed == 'Yes':
fixed = 'YES'
else:
fixed = 'YES'
fixed_full = _get_revision_range_html_from_string(testcase.job_type,
testcase.fixed)
last_tested = None
last_tested_revision = (
metadata.get('last_tested_revision') or testcase.crash_revision)
if last_tested_revision:
last_tested = _get_revision_range_html(testcase.job_type,
last_tested_revision)
crash_revision = testcase.crash_revision
crash_revisions_dict = revisions.get_component_revisions_dict(
crash_revision, testcase.job_type)
crash_stacktrace = data_handler.get_stacktrace(testcase)
crash_stacktrace = filter_stacktrace(crash_stacktrace, testcase.crash_type,
crash_revisions_dict)
crash_stacktrace = convert_to_lines(crash_stacktrace, crash_state_lines,
crash_type)
crash_stacktrace_preview_lines = _preview_stacktrace(crash_stacktrace)
last_tested_crash_revision = metadata.get('last_tested_crash_revision')
last_tested_crash_revisions_dict = revisions.get_component_revisions_dict(
last_tested_crash_revision, testcase.job_type)
last_tested_crash_stacktrace = data_handler.get_stacktrace(
testcase, stack_attribute='last_tested_crash_stacktrace')
last_tested_crash_stacktrace = filter_stacktrace(
last_tested_crash_stacktrace, testcase.crash_type,
last_tested_crash_revisions_dict)
last_tested_crash_stacktrace = convert_to_lines(last_tested_crash_stacktrace,
crash_state_lines, crash_type)
last_tested_crash_stacktrace_preview_lines = _preview_stacktrace(
last_tested_crash_stacktrace)
privileged_user = access.has_access(need_privileged_access=True)
# Fix build url link. |storage.cloud.google.com| takes care of using the
# right set of authentication credentials needed to access the link.
if 'build_url' in metadata:
metadata['build_url'] = metadata['build_url'].replace(
'gs://', 'https://storage.cloud.google.com/')
pending_blame_task = (
testcase.has_blame() and 'blame_pending' in metadata and
metadata['blame_pending'])
pending_impact_task = (
testcase.has_impacts() and not testcase.is_impact_set_flag)
pending_minimize_task = not testcase.minimized_keys
pending_progression_task = ('progression_pending' in metadata and
metadata['progression_pending'])
pending_regression_task = not testcase.regression
pending_stack_task = testcase.last_tested_crash_stacktrace == 'Pending'
needs_refresh = (
testcase.status == 'Pending' or
((testcase.status == 'Processed' or testcase.status == 'Duplicate') and
(pending_blame_task or pending_impact_task or pending_minimize_task or
pending_progression_task or pending_regression_task or
pending_stack_task)))
if data_types.SecuritySeverity.is_valid(testcase.security_severity):
security_severity = severity_analyzer.severity_to_string(
testcase.security_severity)
else:
security_severity = None
auto_delete_timestamp = None
auto_close_timestamp = None
if testcase.one_time_crasher_flag:
last_crash_time = (
crash_stats.get_last_crash_time(testcase) or testcase.timestamp)
# Set auto-delete timestamp for unreproducible testcases with
# no associated bug.
if not testcase.bug_information:
auto_delete_timestamp = utils.utc_datetime_to_timestamp(
last_crash_time + datetime.timedelta(
days=data_types.UNREPRODUCIBLE_TESTCASE_NO_BUG_DEADLINE))
# Set auto-close timestamp for unreproducible testcases with
# an associated bug.
if testcase.open and testcase.bug_information:
auto_close_timestamp = utils.utc_datetime_to_timestamp(
last_crash_time + datetime.timedelta(
days=data_types.UNREPRODUCIBLE_TESTCASE_WITH_BUG_DEADLINE))
memory_tool_display_string = environment.get_memory_tool_display_string(
testcase.job_type)
memory_tool_display_label = memory_tool_display_string.split(':')[0]
memory_tool_display_value = memory_tool_display_string.split(':')[1].strip()
helpers.log('Testcase %s' % testcase.key.id(), helpers.VIEW_OPERATION)
return {
'id':
testcase.key.id(),
'crash_type':
crash_type,
'crash_address':
crash_address,
'crash_state':
crash_state, # Used by reproduce tool.
'crash_state_lines':
crash_state_lines,
'crash_revision':
testcase.crash_revision,
'csrf_token':
form.generate_csrf_token(),
'external_user':
external_user,
'footer':
testcase.comments,
'formatted_reproduction_help':
formatted_reproduction_help,
'fixed':
fixed,
'fixed_full':
fixed_full,
'issue_url':
issue_url,
'is_admin':
auth.is_current_user_admin(),
'metadata':
metadata,
'minimized_testcase_size':
minimized_testcase_size,
'needs_refresh':
needs_refresh,
'original_testcase_size':
original_testcase_size,
'privileged_user':
privileged_user,
'regression':
regression,
'crash_stacktrace': {
'lines':
crash_stacktrace,
'preview_lines':
crash_stacktrace_preview_lines,
'revision':
revisions.get_real_revision(
crash_revision, testcase.job_type, display=True)
},
'last_tested_crash_stacktrace': {
'lines':
last_tested_crash_stacktrace,
'preview_lines':
last_tested_crash_stacktrace_preview_lines,
'revision':
revisions.get_real_revision(
last_tested_crash_revision, testcase.job_type, display=True)
},
'security_severity':
security_severity,
'security_severities':
data_types.SecuritySeverity.list(),
'stats': {
'min_hour': crash_stats.get_min_hour(),
'max_hour': crash_stats.get_max_hour(),
},
'suspected_cls':
_parse_suspected_cls(metadata.get('predator_result')),
'testcase':
testcase,
'timestamp':
utils.utc_datetime_to_timestamp(testcase.timestamp),
'show_blame':
testcase.has_blame(),
'show_impact':
testcase.has_impacts(),
'impacts_production':
testcase.impacts_production(),
'find_similar_issues_options':
FIND_SIMILAR_ISSUES_OPTIONS,
'auto_delete_timestamp':
auto_delete_timestamp,
'auto_close_timestamp':
auto_close_timestamp,
'memory_tool_display_label':
memory_tool_display_label,
'memory_tool_display_value':
memory_tool_display_value,
'last_tested':
last_tested,
'is_admin_or_not_oss_fuzz':
is_admin_or_not_oss_fuzz(),
'has_issue_tracker':
has_issue_tracker,
'reproduction_help_url':
reproduction_help_url,
'is_local_development':
environment.is_running_on_app_engine_development(),
'fuzzer_display':
vars(fuzzer_display),
}
def is_admin_or_not_oss_fuzz():
"""Return True if the current user is an admin or if this is not OSS-Fuzz."""
return not utils.is_oss_fuzz() or auth.is_current_user_admin()
class Handler(base_handler.Handler):
"""Handler that shows a testcase in detail."""
@handler.get(handler.HTML)
def get(self, testcase_id):
"""Serve the testcase detail HTML page."""
values = {'info': get_testcase_detail_by_id(testcase_id)}
self.render('testcase-detail.html', values)
class DeprecatedHandler(base_handler.Handler):
"""Deprecated handler to show old style testcase link with key."""
def get(self):
"""Serve the redirect to the current test case detail page."""
testcase_id = self.request.get('key')
if not testcase_id:
raise helpers.EarlyExitException('No testcase key provided.', 400)
self.redirect('/testcase-detail/%s' % testcase_id)
class RefreshHandler(base_handler.Handler):
"""Handler that shows a testcase in detail through JSON."""
@handler.post(handler.JSON, handler.JSON)
@handler.oauth
def post(self):
"""Serve the testcase detail JSON."""
testcase_id = self.request.get('testcaseId')
self.render_json(get_testcase_detail_by_id(testcase_id))