forked from google/vanir
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefiner_test.py
352 lines (321 loc) · 13.2 KB
/
refiner_test.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
# Copyright 2023 Google LLC
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
"""Tests for refiner."""
import concurrent
import concurrent.futures
import dataclasses
import functools
import os
from unittest import mock
import mmh3
from vanir import parser
from vanir import refiner
from vanir import signature
from vanir.code_extractors import code_extractor_base
from absl.testing import absltest
from pybind11_abseil import status
_TEST_NORMALIZED_FUNCTION_CODE = (
'DTYPE FUNCNAME ( const unsigned DTYPE PARAM ) { '
'const DTYPE * VAR = ( DTYPE * ) globalvar -> data ; '
'FUNCCALL ( PARAM , VAR ) ; '
'0xe8 ( ) ; '
'return 0 ; }')
_TEST_NORMALIZED_LINE_CODE = {
1: 'struct teststruct globalvar = configvar ;',
2: 'int test_func1 ( const unsigned int64 test_arg ) {',
3: 'const int * testvar = ( int * ) globalvar -> data ;',
4: 'test_func2 ( test_arg , testvar ) ;',
5: '0xe8 ( ) ;',
6: 'return 0 ;',
7: '}',
}
_TEST_PATCHED_CODE = '\n'.join([
'// Library for test functions',
'#include "test_func_lib.h"',
'struct teststruct globalVar = configVar;',
'int test_func1(const unsigned int64 test_arg) { ',
' const int* testvar = (int *)globalVar -> data;',
' test_func2(test_arg , testvar);',
' // security patch: remove vulnerable function call',
' return 0;',
'}',
])
# Signatures generated from FUNCTION_CODE will match this since it has an
# unexpected test_func3() in patched file that's similar to the unpatched func1.
# Alhough the function signature for test_func1() would match against
# test_func3(), the line signature generated from test_func1() is still valid.
_TEST_PATCHED_CODE_WITH_FALSE_FUNC_SIG_MATCH = '\n'.join([
'// Library for test functions. This file does something awesome.',
'#include "test_func_lib.h"',
'struct teststruct globalVar = configVar;',
'int test_func1(const unsigned int64 test_arg) { ',
' const int* testvar = (int *)globalVar -> data;',
' test_func2(test_arg , testvar);',
' // Security patch: removed vulnerable function call.',
' // We removed a macro function call which caused bad problems.',
' // Never use 0xe8() here anymore.', ' return 0;', '}', '',
' ',
'int test_func3(const unsigned float ratio) { ',
' const int* testvar = (int *)globalVar -> data;',
' test_func4(ratio , testvar);',
' 0xe8(); // a black magic macro function. Safe to use here.',
' return 0;', '}'
])
# A patch that only adds comments so there is no meaningful difference between
# the patched file and unpatched file, which will cause false positives.
_TEST_PATCHED_CODE_WITH_FALSE_LINE_AND_FUNC_SIG_MATCHES = '\n'.join([
'// Library for test functions', '#include "test_func_lib.h"',
'struct teststruct globalVar = configVar;',
'int test_func1(const unsigned int64 test_arg) { ',
' const int* testvar = (int *)globalVar -> data;',
' test_func2(test_arg , testvar);', ' 0xe8();',
' /* ',
' * We have reviewed this function thoroughly for security ',
' * and we confirm caeven lling 0xe8() is totally fine. ',
' * This comment is for confirming the safety of this use.',
' * So, do not delete this line in any future commit.',
' */ ',
' return 0;', '}'
])
_TEST_OSV_ID = 'ASB-A-123'
_TEST_LINE_SIG_HASH = 'linesighash'
_TEST_FUNC_SIG_HASH = 'functionsighash'
_TEST_TARGET_FILE = 'foo/bar/test_func_lib.c'
class RefinerTest(absltest.TestCase):
def _create_tempfile(self, filename: str, content: str) -> str:
tempfile = self.create_tempfile(
file_path=os.path.join(self.create_tempdir(), filename),
content=content,
mode='w',
)
return tempfile.full_path
def _create_groundtruth_commit_with_a_file(self, content):
mock_commit = mock.create_autospec(
code_extractor_base.Commit, instance=True
)
mock_commit.get_url.return_value = hash(content)
tempfile = self._create_tempfile(_TEST_TARGET_FILE, content)
mock_commit.get_patched_files.return_value = {
_TEST_TARGET_FILE: tempfile,
'unhandled.filetype': 'nonexistent_file',
}
return mock_commit
def setUp(self):
super().setUp()
self._hash = functools.partial(
mmh3.hash128, seed=0, x64arch=True, signed=False
)
self._test_function_sig = signature.FunctionSignature(
signature_hash=_TEST_FUNC_SIG_HASH,
signature_id_prefix=_TEST_OSV_ID,
signature_version=signature._VANIR_SIGNATURE_VERSION,
source=mock.Mock(),
target_file=_TEST_TARGET_FILE,
deprecated=mock.Mock(),
exact_target_file_match_only=False,
match_only_versions=None,
truncated_path_level=None,
function_hash=self._hash(_TEST_NORMALIZED_FUNCTION_CODE),
length=len(_TEST_NORMALIZED_FUNCTION_CODE),
target_function='test_func1',
)
test_line_hashes = []
line_number_ngrams = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6],
[4, 5, 6, 7]]
for line_numbers in line_number_ngrams:
ngram = ' '.join([
_TEST_NORMALIZED_LINE_CODE[line_number]
for line_number in line_numbers
])
test_line_hashes.append(self._hash(ngram))
self._test_line_sig = signature.LineSignature(
signature_hash=_TEST_LINE_SIG_HASH,
signature_id_prefix=_TEST_OSV_ID,
signature_version=signature._VANIR_SIGNATURE_VERSION,
source=mock.Mock(),
target_file=_TEST_TARGET_FILE,
deprecated=mock.Mock(),
exact_target_file_match_only=False,
match_only_versions=None,
truncated_path_level=None,
line_hashes=test_line_hashes,
threshold=0.9,
)
self._test_patched_commit_with_false_line_and_func_sig_matches = (
self._create_groundtruth_commit_with_a_file(
_TEST_PATCHED_CODE_WITH_FALSE_LINE_AND_FUNC_SIG_MATCHES
)
)
self._test_patched_commit_with_false_func_sig_match_only = (
self._create_groundtruth_commit_with_a_file(
_TEST_PATCHED_CODE_WITH_FALSE_FUNC_SIG_MATCH
)
)
self._test_patched_commit_without_false_positive = (
self._create_groundtruth_commit_with_a_file(_TEST_PATCHED_CODE)
)
self._refiner = refiner.Refiner()
# Mock concurrent executor: forkserver is inherently not working with mock
def mock_apply(func, *args):
res = mock.MagicMock()
res.result.side_effect = lambda: func(*args)
return res
self._mock_executor = self.enter_context(
mock.patch.object(
concurrent.futures, 'ProcessPoolExecutor', autospec=True
)
).return_value.__enter__
self._mock_executor.return_value.submit.side_effect = mock_apply
def test_function_chunk_refinement_allows_good_sign(self):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_without_false_positive],
refiner.RemoveBadSignature(),
)
self.assertEqual(
refined_signatures, {self._test_function_sig, self._test_line_sig},
)
def test_function_chunk_refinement_filters_out_bad_sign(self):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_with_false_func_sig_match_only],
refiner.RemoveBadSignature(),
)
self.assertEqual(refined_signatures, {self._test_line_sig})
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_with_false_line_and_func_sig_matches],
refiner.RemoveBadSignature(),
)
self.assertEmpty(refined_signatures)
def test_line_chunk_refinement_allows_good_sign(self):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_without_false_positive],
refiner.RemoveBadSignature(),
)
self.assertEqual(
refined_signatures, {self._test_function_sig, self._test_line_sig},
)
def test_line_chunk_refinement_filters_out_bad_sign(self):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_with_false_line_and_func_sig_matches],
refiner.RemoveBadSignature(),
)
self.assertEmpty(refined_signatures)
def test_refinement_patch_series_should_take_latest_file(self):
# Simulate patch -> (partial) revert. Should cause signature to match.
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[
self._test_patched_commit_without_false_positive,
self._test_patched_commit_with_false_func_sig_match_only,
],
refiner.RemoveBadSignature(),
)
self.assertEqual(refined_signatures, {self._test_line_sig})
def test_refinement_with_patch_series_should_take_latest_file_clean(self):
# Simulate patch -> (partial) revert -> patch. Should not cause any flag.
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[
self._test_patched_commit_with_false_func_sig_match_only,
self._test_patched_commit_without_false_positive,
],
refiner.RemoveBadSignature(),
)
self.assertEqual(
refined_signatures, {self._test_function_sig, self._test_line_sig},
)
def test_refinement_match_other_versions_mark_as_version_specific(self):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_with_false_func_sig_match_only],
refiner.MarkAsSpecificToVersions([10, 11]),
)
self.assertEqual(
refined_signatures,
{
self._test_line_sig,
dataclasses.replace(
self._test_function_sig, match_only_versions=frozenset({10, 11})
)
}
)
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_with_false_line_and_func_sig_matches],
refiner.MarkAsSpecificToVersions([10, 11]),
)
self.assertEqual(
refined_signatures,
{
dataclasses.replace(
self._test_line_sig, match_only_versions=frozenset({10, 11})
),
dataclasses.replace(
self._test_function_sig, match_only_versions=frozenset({10, 11})
)
}
)
def test_refinement_match_other_files_versions_mark_as_version_specific(self):
# Simulate a commit where the patched file is unrelated to the signatures,
# but the signature target file at that rev matches the function signature.
clean_file = self._create_tempfile('unrelated.c', _TEST_PATCHED_CODE)
false_positive_file = self._create_tempfile(
_TEST_TARGET_FILE, _TEST_PATCHED_CODE_WITH_FALSE_FUNC_SIG_MATCH
)
mock_commit = mock.create_autospec(
code_extractor_base.Commit, instance=True
)
mock_commit.get_url.return_value = hash(_TEST_PATCHED_CODE)
mock_commit.get_patched_files.return_value = {'unrelated.c': clean_file}
mock_commit.get_file_at_rev.return_value = false_positive_file
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[mock_commit],
refiner.MarkAsSpecificToVersions([10, 11]),
)
self.assertEqual(
refined_signatures,
{
self._test_line_sig,
dataclasses.replace(
self._test_function_sig, match_only_versions=frozenset({10, 11})
)
}
)
def test_refinement_skip_when_parser_fails(self):
with mock.patch.object(
parser, 'Parser',
side_effect=status.BuildStatusNotOk(
status.StatusCode.CANCELLED, 'mock error'
),
):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_without_false_positive],
refiner.RemoveBadSignature(),
)
self.assertEqual(
refined_signatures, {self._test_function_sig, self._test_line_sig}
)
def test_refinement_skip_when_parser_crashed(self):
with mock.patch.object(
parser, 'Parser',
side_effect=concurrent.futures.process.BrokenProcessPool(),
):
refined_signatures = self._refiner.refine_against_patch_series(
[self._test_function_sig, self._test_line_sig],
[self._test_patched_commit_without_false_positive],
refiner.RemoveBadSignature(),
)
self.assertEqual(
refined_signatures, {self._test_function_sig, self._test_line_sig}
)
if __name__ == '__main__':
absltest.main()