Skip to content

Commit bf0e3eb

Browse files
[CIfuzz] Fix SARIF functionality. (#10349)
1 parent 58ff523 commit bf0e3eb

File tree

5 files changed

+654
-6
lines changed

5 files changed

+654
-6
lines changed

infra/cifuzz/sarif_utils.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,13 @@ def get_error_frame(crash_info):
162162
if not crash_info.crash_state:
163163
return None
164164
state = crash_info.crash_state.split('\n')[0]
165+
logging.info('state: %s frames %s, %s', state, crash_info.frames,
166+
[f.function_name for f in crash_info.frames[0]])
165167

166168
for crash_frames in crash_info.frames:
167169
for frame in crash_frames:
168170
# TODO(metzman): Do something less fragile here.
169-
if frame.function_name.startswith(state):
171+
if state in frame.function_name:
170172
return frame
171173
return None
172174

@@ -205,9 +207,9 @@ def get_sarif_data(stacktrace, target_path):
205207
include_ubsan=True)
206208
crash_info = stack_parser.parse(stacktrace)
207209
error_source_info = get_error_source_info(crash_info)
208-
uri = error_source_info[0]
209210
rule_idx = get_rule_index(crash_info.crash_type)
210211
rule_id = SARIF_RULES[rule_idx]['id']
212+
uri = error_source_info[0]
211213

212214
result = {
213215
'level': 'error',
@@ -230,7 +232,8 @@ def get_sarif_data(stacktrace, target_path):
230232
'ruleId': rule_id,
231233
'ruleIndex': rule_idx
232234
}
233-
data['runs'][0]['results'].append(result)
235+
if uri:
236+
data['runs'][0]['results'].append(result)
234237
return data
235238

236239

infra/cifuzz/sarif_utils_test.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
"""Tests for sarif_utils.py"""
15+
import copy
16+
import os
1517
import unittest
1618
from unittest import mock
1719

1820
import sarif_utils
1921

2022
CRASH_INFO_FILELINE = 403
2123

24+
TEST_DATA = os.path.join(os.path.dirname(__file__), 'test_data')
25+
2226

2327
class GetSarifDataTest(unittest.TestCase):
2428
"""Tests for get_sarif_data."""
@@ -31,6 +35,45 @@ def test_get_sarif_data_none(self):
3135
self.assertEqual(sarif_utils.get_sarif_data(None, '/root/target'),
3236
sarif_utils.SARIF_DATA)
3337

38+
def test_ordinary_case(self):
39+
stacktrace_filename = os.path.join(TEST_DATA,
40+
'sarif_utils_systemd_stack.txt')
41+
with open(stacktrace_filename, 'r') as fp:
42+
stacktrace = fp.read()
43+
expected_result = {
44+
'level': 'error',
45+
'message': {
46+
'text': 'Heap-buffer-overflow\nREAD 4'
47+
},
48+
'locations': [{
49+
'physicalLocation': {
50+
'artifactLocation': {
51+
'uri': 'src/core/fuzz-unit-file.c',
52+
'index': 0
53+
},
54+
'region': {
55+
'startLine': 30,
56+
# We don't have this granualarity fuzzing.
57+
'startColumn': 1,
58+
}
59+
}
60+
}],
61+
'ruleId': 'heap-buffer-overflow',
62+
'ruleIndex': 2
63+
}
64+
actual_result = sarif_utils.get_sarif_data(
65+
stacktrace, '/root/target')['runs'][0]['results'][0]
66+
self.assertEqual(actual_result, expected_result)
67+
68+
def test_llvmfuzzertestoneinput_case(self):
69+
stacktrace_filename = os.path.join(TEST_DATA,
70+
'sarif_utils_only_llvmfuzzer_stack.txt')
71+
with open(stacktrace_filename, 'r') as fp:
72+
stacktrace = fp.read()
73+
actual_result = sarif_utils.get_sarif_data(
74+
stacktrace, '/root/target')['runs'][0]['results']
75+
self.assertEqual(actual_result, [])
76+
3477

3578
class RedactSrcPathTest(unittest.TestCase):
3679
"""Tests for redact_src_path."""

0 commit comments

Comments
 (0)