Add files using upload-large-folder tool
Browse files- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/COMPRESS-320/recreate.sh +9 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD.ar +29 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD_crc.cpio +0 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD_newc.cpio +0 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/SunOS.ar +29 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/SunOS_-c.cpio +0 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/files.txt +11 -0
- local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/pack200/hw.jar +0 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/affected_fuzz_targets_test.py +113 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/build_fuzzers_entrypoint.py +60 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/clusterfuzz_deployment.py +385 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/clusterfuzz_deployment_test.py +268 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/config_utils_test.py +200 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/continuous_integration_test.py +88 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/docker.py +127 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/docker_test.py +119 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/environment.py +51 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/fuzz_target.py +408 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/sarif_utils.py +251 -0
- local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/workspace_utils.py +85 -0
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/COMPRESS-320/recreate.sh
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/bin/bash
|
| 2 |
+
|
| 3 |
+
rm *.7z
|
| 4 |
+
for COMPRESSION in "LZMA" "LZMA2" "PPMd" "BZip2" "Deflate" "Copy"; do
|
| 5 |
+
# New solid block every 10 files.
|
| 6 |
+
7za a -m0=$COMPRESSION -ms10f $COMPRESSION-solid.7z ../../../../src/main/java/org/apache/commons/compress/compressors
|
| 7 |
+
# Each file in isolation
|
| 8 |
+
7za a -m0=$COMPRESSION -ms=off $COMPRESSION.7z ../../../../src/main/java/org/apache/commons/compress/compressors
|
| 9 |
+
done
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD.ar
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!<arch>
|
| 2 |
+
1/ 1238278221 1721 1721 100664 27 `
|
| 3 |
+
abcdefghijklmnopqrstuvwxyz
|
| 4 |
+
|
| 5 |
+
12/ 1238278221 1721 1721 100664 27 `
|
| 6 |
+
abcdefghijklmnopqrstuvwxyz
|
| 7 |
+
|
| 8 |
+
123/ 1238278221 1721 1721 100664 27 `
|
| 9 |
+
abcdefghijklmnopqrstuvwxyz
|
| 10 |
+
|
| 11 |
+
1234/ 1238278222 1721 1721 100664 27 `
|
| 12 |
+
abcdefghijklmnopqrstuvwxyz
|
| 13 |
+
|
| 14 |
+
12345/ 1238278222 1721 1721 100664 27 `
|
| 15 |
+
abcdefghijklmnopqrstuvwxyz
|
| 16 |
+
|
| 17 |
+
123450/ 1238278222 1721 1721 100644 27 `
|
| 18 |
+
abcdefghijklmnopqrstuvwxyz
|
| 19 |
+
|
| 20 |
+
123456/ 1238278222 1721 1721 100664 26 `
|
| 21 |
+
bcdefghijklmnopqrstuvwxyz
|
| 22 |
+
123457/ 1238278222 1721 1721 100644 25 `
|
| 23 |
+
cdefghijklmnopqrstuvwxyz
|
| 24 |
+
|
| 25 |
+
123458/ 1238278222 1721 1721 100644 24 `
|
| 26 |
+
defghijklmnopqrstuvwxyz
|
| 27 |
+
123459/ 1238278222 1721 1721 100644 23 `
|
| 28 |
+
efghijklmnopqrstuvwxyz
|
| 29 |
+
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD_crc.cpio
ADDED
|
Binary file (2.05 kB). View file
|
|
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/FreeBSD_newc.cpio
ADDED
|
Binary file (2.05 kB). View file
|
|
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/SunOS.ar
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
!<arch>
|
| 2 |
+
1/ 1238264462 2606 1 100644 27 `
|
| 3 |
+
abcdefghijklmnopqrstuvwxyz
|
| 4 |
+
|
| 5 |
+
12/ 1238277260 2606 1 100644 27 `
|
| 6 |
+
abcdefghijklmnopqrstuvwxyz
|
| 7 |
+
|
| 8 |
+
123/ 1238277265 2606 1 100644 27 `
|
| 9 |
+
abcdefghijklmnopqrstuvwxyz
|
| 10 |
+
|
| 11 |
+
1234/ 1238277275 2606 1 100644 27 `
|
| 12 |
+
abcdefghijklmnopqrstuvwxyz
|
| 13 |
+
|
| 14 |
+
12345/ 1238277277 2606 1 100644 27 `
|
| 15 |
+
abcdefghijklmnopqrstuvwxyz
|
| 16 |
+
|
| 17 |
+
123450/ 1238277385 2606 1 100644 27 `
|
| 18 |
+
abcdefghijklmnopqrstuvwxyz
|
| 19 |
+
|
| 20 |
+
123456/ 1238277620 2606 1 100644 26 `
|
| 21 |
+
bcdefghijklmnopqrstuvwxyz
|
| 22 |
+
123457/ 1238277660 2606 1 100644 25 `
|
| 23 |
+
cdefghijklmnopqrstuvwxyz
|
| 24 |
+
|
| 25 |
+
123458/ 1238277670 2606 1 100644 24 `
|
| 26 |
+
defghijklmnopqrstuvwxyz
|
| 27 |
+
123459/ 1238277679 2606 1 100644 23 `
|
| 28 |
+
efghijklmnopqrstuvwxyz
|
| 29 |
+
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/SunOS_-c.cpio
ADDED
|
Binary file (2.05 kB). View file
|
|
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/archives/files.txt
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Sizes and filenames in the archives
|
| 2 |
+
27 1
|
| 3 |
+
27 12
|
| 4 |
+
27 123
|
| 5 |
+
27 1234
|
| 6 |
+
27 12345
|
| 7 |
+
27 123450
|
| 8 |
+
26 123456
|
| 9 |
+
25 123457
|
| 10 |
+
24 123458
|
| 11 |
+
23 123459
|
local-test-commons-compress-delta-02/afc-commons-compress/src/test/resources/pack200/hw.jar
ADDED
|
Binary file (842 Bytes). View file
|
|
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/affected_fuzz_targets_test.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for affected_fuzz_targets.py"""
|
| 15 |
+
import os
|
| 16 |
+
import shutil
|
| 17 |
+
import tempfile
|
| 18 |
+
import unittest
|
| 19 |
+
from unittest import mock
|
| 20 |
+
|
| 21 |
+
import parameterized
|
| 22 |
+
|
| 23 |
+
import affected_fuzz_targets
|
| 24 |
+
import clusterfuzz_deployment
|
| 25 |
+
import get_coverage
|
| 26 |
+
import test_helpers
|
| 27 |
+
import workspace_utils
|
| 28 |
+
|
| 29 |
+
# pylint: disable=protected-access
|
| 30 |
+
|
| 31 |
+
# NOTE: This integration test relies on
|
| 32 |
+
# https://github.com/google/oss-fuzz/tree/master/projects/example project.
|
| 33 |
+
EXAMPLE_PROJECT = 'example'
|
| 34 |
+
|
| 35 |
+
EXAMPLE_FILE_CHANGED = 'test.txt'
|
| 36 |
+
|
| 37 |
+
TEST_DATA_OUT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)),
|
| 38 |
+
'test_data', 'build-out')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class RemoveUnaffectedFuzzTargetsTest(unittest.TestCase):
|
| 42 |
+
"""Tests remove_unaffected_fuzzers."""
|
| 43 |
+
|
| 44 |
+
TEST_FUZZER_1 = os.path.join(TEST_DATA_OUT_PATH, 'example_crash_fuzzer')
|
| 45 |
+
TEST_FUZZER_2 = os.path.join(TEST_DATA_OUT_PATH, 'example_nocrash_fuzzer')
|
| 46 |
+
|
| 47 |
+
# yapf: disable
|
| 48 |
+
@parameterized.parameterized.expand([
|
| 49 |
+
# Tests specific affected fuzzers are kept.
|
| 50 |
+
([[EXAMPLE_FILE_CHANGED], None], 2,),
|
| 51 |
+
|
| 52 |
+
# Tests specific affected fuzzer is kept.
|
| 53 |
+
([[EXAMPLE_FILE_CHANGED], ['not/a/real/file']], 1),
|
| 54 |
+
|
| 55 |
+
# Tests all fuzzers are kept if none are deemed affected.
|
| 56 |
+
([None, None], 2),
|
| 57 |
+
|
| 58 |
+
# Tests that multiple fuzzers are kept if multiple fuzzers are affected.
|
| 59 |
+
([[EXAMPLE_FILE_CHANGED], [EXAMPLE_FILE_CHANGED]], 2),
|
| 60 |
+
])
|
| 61 |
+
# yapf: enable
|
| 62 |
+
def test_remove_unaffected_fuzz_targets(self, side_effect, expected_dir_len):
|
| 63 |
+
"""Tests that remove_unaffected_fuzzers has the intended effect."""
|
| 64 |
+
config = test_helpers.create_run_config(
|
| 65 |
+
cfl_platform='github',
|
| 66 |
+
oss_fuzz_project_name=EXAMPLE_PROJECT,
|
| 67 |
+
workspace='/workspace')
|
| 68 |
+
workspace = workspace_utils.Workspace(config)
|
| 69 |
+
deployment = clusterfuzz_deployment.get_clusterfuzz_deployment(
|
| 70 |
+
config, workspace)
|
| 71 |
+
# We can't use fakefs in this test because this test executes
|
| 72 |
+
# utils.is_fuzz_target_local. This function relies on the executable bit
|
| 73 |
+
# being set, which doesn't work properly in fakefs.
|
| 74 |
+
with tempfile.TemporaryDirectory() as tmp_dir, mock.patch(
|
| 75 |
+
'get_coverage.OSSFuzzCoverage.get_files_covered_by_target'
|
| 76 |
+
) as mock_get_files:
|
| 77 |
+
with mock.patch('get_coverage._get_oss_fuzz_fuzzer_stats_dir_url',
|
| 78 |
+
return_value=1):
|
| 79 |
+
mock_get_files.side_effect = side_effect
|
| 80 |
+
shutil.copy(self.TEST_FUZZER_1, tmp_dir)
|
| 81 |
+
shutil.copy(self.TEST_FUZZER_2, tmp_dir)
|
| 82 |
+
|
| 83 |
+
affected_fuzz_targets.remove_unaffected_fuzz_targets(
|
| 84 |
+
deployment, tmp_dir, [EXAMPLE_FILE_CHANGED], '')
|
| 85 |
+
self.assertEqual(expected_dir_len, len(os.listdir(tmp_dir)))
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class IsFuzzTargetAffectedTest(unittest.TestCase):
|
| 89 |
+
"""Tests for is_fuzz_target_affected."""
|
| 90 |
+
|
| 91 |
+
def setUp(self):
|
| 92 |
+
self.fuzz_target_path = '/fuzz_target'
|
| 93 |
+
|
| 94 |
+
def test_relative_paths(self):
|
| 95 |
+
"""Tests that is_fuzz_target_affected works as intended when the covered
|
| 96 |
+
files are relative paths."""
|
| 97 |
+
with mock.patch.object(
|
| 98 |
+
get_coverage.FilesystemCoverage,
|
| 99 |
+
'get_files_covered_by_target',
|
| 100 |
+
) as get_files_covered_by_target:
|
| 101 |
+
get_files_covered_by_target.return_value = [
|
| 102 |
+
'/work/build/../../src/systemd/src/basic/alloc-util.c'
|
| 103 |
+
]
|
| 104 |
+
coverage = get_coverage.FilesystemCoverage('/', '/')
|
| 105 |
+
|
| 106 |
+
self.assertTrue(
|
| 107 |
+
affected_fuzz_targets.is_fuzz_target_affected(
|
| 108 |
+
coverage, self.fuzz_target_path,
|
| 109 |
+
['/src/systemd/src/basic/alloc-util.c']))
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
if __name__ == '__main__':
|
| 113 |
+
unittest.main()
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/build_fuzzers_entrypoint.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Builds a specific OSS-Fuzz project's fuzzers for CI tools."""
|
| 15 |
+
import logging
|
| 16 |
+
import sys
|
| 17 |
+
|
| 18 |
+
import build_fuzzers
|
| 19 |
+
import logs
|
| 20 |
+
import config_utils
|
| 21 |
+
|
| 22 |
+
# pylint: disable=c-extension-no-member
|
| 23 |
+
# pylint gets confused because of the relative import of cifuzz.
|
| 24 |
+
|
| 25 |
+
logs.init()
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def build_fuzzers_entrypoint():
|
| 29 |
+
"""Builds OSS-Fuzz project's fuzzers for CI tools."""
|
| 30 |
+
config = config_utils.BuildFuzzersConfig()
|
| 31 |
+
|
| 32 |
+
if config.dry_run:
|
| 33 |
+
# Sets the default return code on error to success.
|
| 34 |
+
returncode = 0
|
| 35 |
+
else:
|
| 36 |
+
# The default return code when an error occurs.
|
| 37 |
+
returncode = 1
|
| 38 |
+
|
| 39 |
+
if not build_fuzzers.build_fuzzers(config):
|
| 40 |
+
logging.error('Error building fuzzers for (commit: %s, pr_ref: %s).',
|
| 41 |
+
config.git_sha, config.pr_ref)
|
| 42 |
+
return returncode
|
| 43 |
+
|
| 44 |
+
return 0
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def main():
|
| 48 |
+
"""Builds OSS-Fuzz project's fuzzers for CI tools.
|
| 49 |
+
|
| 50 |
+
Note: The resulting fuzz target binaries of this build are placed in
|
| 51 |
+
the directory: ${GITHUB_WORKSPACE}/out
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
0 on success or nonzero on failure.
|
| 55 |
+
"""
|
| 56 |
+
return build_fuzzers_entrypoint()
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
if __name__ == '__main__':
|
| 60 |
+
sys.exit(main())
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/clusterfuzz_deployment.py
ADDED
|
@@ -0,0 +1,385 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for interacting with the ClusterFuzz deployment."""
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
import sys
|
| 18 |
+
import urllib.error
|
| 19 |
+
import urllib.request
|
| 20 |
+
|
| 21 |
+
import config_utils
|
| 22 |
+
import continuous_integration
|
| 23 |
+
import filestore_utils
|
| 24 |
+
import http_utils
|
| 25 |
+
import get_coverage
|
| 26 |
+
import repo_manager
|
| 27 |
+
|
| 28 |
+
# pylint: disable=wrong-import-position,import-error
|
| 29 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 30 |
+
import utils
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class BaseClusterFuzzDeployment:
|
| 34 |
+
"""Base class for ClusterFuzz deployments."""
|
| 35 |
+
|
| 36 |
+
def __init__(self, config, workspace):
|
| 37 |
+
self.config = config
|
| 38 |
+
self.workspace = workspace
|
| 39 |
+
self.ci_system = continuous_integration.get_ci(config)
|
| 40 |
+
|
| 41 |
+
def download_latest_build(self):
|
| 42 |
+
"""Downloads the latest build from ClusterFuzz.
|
| 43 |
+
|
| 44 |
+
Returns:
|
| 45 |
+
A path to where the OSS-Fuzz build was stored, or None if it wasn't.
|
| 46 |
+
"""
|
| 47 |
+
raise NotImplementedError('Child class must implement method.')
|
| 48 |
+
|
| 49 |
+
def upload_build(self, commit):
|
| 50 |
+
"""Uploads the build with the given commit sha to the filestore."""
|
| 51 |
+
raise NotImplementedError('Child class must implement method.')
|
| 52 |
+
|
| 53 |
+
def download_corpus(self, target_name, corpus_dir):
|
| 54 |
+
"""Downloads the corpus for |target_name| from ClusterFuzz to |corpus_dir|.
|
| 55 |
+
|
| 56 |
+
Returns:
|
| 57 |
+
A path to where the OSS-Fuzz build was stored, or None if it wasn't.
|
| 58 |
+
"""
|
| 59 |
+
raise NotImplementedError('Child class must implement method.')
|
| 60 |
+
|
| 61 |
+
def upload_crashes(self):
|
| 62 |
+
"""Uploads crashes in |crashes_dir| to filestore."""
|
| 63 |
+
raise NotImplementedError('Child class must implement method.')
|
| 64 |
+
|
| 65 |
+
def upload_corpus(self, target_name, corpus_dir, replace=False): # pylint: disable=no-self-use,unused-argument
|
| 66 |
+
"""Uploads the corpus for |target_name| to filestore."""
|
| 67 |
+
raise NotImplementedError('Child class must implement method.')
|
| 68 |
+
|
| 69 |
+
def upload_coverage(self):
|
| 70 |
+
"""Uploads the coverage report to the filestore."""
|
| 71 |
+
raise NotImplementedError('Child class must implement method.')
|
| 72 |
+
|
| 73 |
+
def get_coverage(self, repo_path):
|
| 74 |
+
"""Returns the project coverage object for the project."""
|
| 75 |
+
raise NotImplementedError('Child class must implement method.')
|
| 76 |
+
|
| 77 |
+
|
| 78 |
+
def _make_empty_dir_if_nonexistent(path):
|
| 79 |
+
"""Makes an empty directory at |path| if it does not exist."""
|
| 80 |
+
os.makedirs(path, exist_ok=True)
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
class ClusterFuzzLite(BaseClusterFuzzDeployment):
|
| 84 |
+
"""Class representing a deployment of ClusterFuzzLite."""
|
| 85 |
+
|
| 86 |
+
COVERAGE_NAME = 'latest'
|
| 87 |
+
LATEST_BUILD_WINDOW = 3
|
| 88 |
+
|
| 89 |
+
def __init__(self, config, workspace):
|
| 90 |
+
super().__init__(config, workspace)
|
| 91 |
+
self.filestore = filestore_utils.get_filestore(self.config)
|
| 92 |
+
|
| 93 |
+
def download_latest_build(self):
|
| 94 |
+
if os.path.exists(self.workspace.clusterfuzz_build):
|
| 95 |
+
# This path is necessary because download_latest_build can be called
|
| 96 |
+
# multiple times.That is the case because it is called only when we need
|
| 97 |
+
# to see if a bug is novel, i.e. until we want to check a bug is novel we
|
| 98 |
+
# don't want to waste time calling this, but therefore this method can be
|
| 99 |
+
# called if multiple bugs are found.
|
| 100 |
+
return self.workspace.clusterfuzz_build
|
| 101 |
+
|
| 102 |
+
repo_dir = self.ci_system.repo_dir
|
| 103 |
+
if not repo_dir:
|
| 104 |
+
raise RuntimeError('Repo checkout does not exist.')
|
| 105 |
+
|
| 106 |
+
_make_empty_dir_if_nonexistent(self.workspace.clusterfuzz_build)
|
| 107 |
+
repo = repo_manager.RepoManager(repo_dir)
|
| 108 |
+
|
| 109 |
+
diff_base = self.ci_system.get_diff_base()
|
| 110 |
+
if not diff_base:
|
| 111 |
+
diff_base = 'HEAD^'
|
| 112 |
+
|
| 113 |
+
# Builds are stored by commit, so try the latest |LATEST_BUILD_WINDOW|
|
| 114 |
+
# commits before the current diff base.
|
| 115 |
+
# TODO(ochang): If API usage becomes an issue, this can be optimized by the
|
| 116 |
+
# filestore accepting a list of filenames to try.
|
| 117 |
+
try:
|
| 118 |
+
# TODO(metzman): Why do we default to 'origin', we should avoid going down
|
| 119 |
+
# this path entirely and not need to catch an exception.
|
| 120 |
+
commit_list = repo.get_commit_list(diff_base,
|
| 121 |
+
limit=self.LATEST_BUILD_WINDOW)
|
| 122 |
+
except ValueError as error:
|
| 123 |
+
logging.error('Can\'t get commit list: %s', error)
|
| 124 |
+
return None
|
| 125 |
+
|
| 126 |
+
for old_commit in commit_list:
|
| 127 |
+
logging.info('Trying to downloading previous build %s.', old_commit)
|
| 128 |
+
build_name = self._get_build_name(old_commit)
|
| 129 |
+
try:
|
| 130 |
+
if self.filestore.download_build(build_name,
|
| 131 |
+
self.workspace.clusterfuzz_build):
|
| 132 |
+
logging.info('Done downloading previous build.')
|
| 133 |
+
return self.workspace.clusterfuzz_build
|
| 134 |
+
|
| 135 |
+
logging.info('Build for %s does not exist.', old_commit)
|
| 136 |
+
except Exception as err: # pylint: disable=broad-except
|
| 137 |
+
logging.error('Could not download build for %s because of: %s',
|
| 138 |
+
old_commit, err)
|
| 139 |
+
|
| 140 |
+
return None
|
| 141 |
+
|
| 142 |
+
def download_corpus(self, target_name, corpus_dir):
|
| 143 |
+
_make_empty_dir_if_nonexistent(corpus_dir)
|
| 144 |
+
logging.info('Downloading corpus for %s to %s.', target_name, corpus_dir)
|
| 145 |
+
corpus_name = self._get_corpus_name(target_name)
|
| 146 |
+
try:
|
| 147 |
+
self.filestore.download_corpus(corpus_name, corpus_dir)
|
| 148 |
+
logging.info('Done downloading corpus. Contains %d elements.',
|
| 149 |
+
len(os.listdir(corpus_dir)))
|
| 150 |
+
except Exception as err: # pylint: disable=broad-except
|
| 151 |
+
logging.error('Failed to download corpus for target: %s. Error: %s',
|
| 152 |
+
target_name, str(err))
|
| 153 |
+
return corpus_dir
|
| 154 |
+
|
| 155 |
+
def _get_build_name(self, name):
|
| 156 |
+
return f'{self.config.sanitizer}-{name}'
|
| 157 |
+
|
| 158 |
+
def _get_corpus_name(self, target_name): # pylint: disable=no-self-use
|
| 159 |
+
"""Returns the name of the corpus artifact."""
|
| 160 |
+
return target_name
|
| 161 |
+
|
| 162 |
+
def upload_corpus(self, target_name, corpus_dir, replace=False):
|
| 163 |
+
"""Upload the corpus produced by |target_name|."""
|
| 164 |
+
logging.info('Uploading corpus in %s for %s.', corpus_dir, target_name)
|
| 165 |
+
name = self._get_corpus_name(target_name)
|
| 166 |
+
try:
|
| 167 |
+
self.filestore.upload_corpus(name, corpus_dir, replace=replace)
|
| 168 |
+
logging.info('Done uploading corpus.')
|
| 169 |
+
except Exception as err: # pylint: disable=broad-except
|
| 170 |
+
logging.error('Failed to upload corpus for target: %s. Error: %s.',
|
| 171 |
+
target_name, err)
|
| 172 |
+
|
| 173 |
+
def upload_build(self, commit):
|
| 174 |
+
"""Upload the build produced by CIFuzz as the latest build."""
|
| 175 |
+
logging.info('Uploading latest build in %s.', self.workspace.out)
|
| 176 |
+
build_name = self._get_build_name(commit)
|
| 177 |
+
try:
|
| 178 |
+
result = self.filestore.upload_build(build_name, self.workspace.out)
|
| 179 |
+
logging.info('Done uploading latest build.')
|
| 180 |
+
return result
|
| 181 |
+
except Exception as err: # pylint: disable=broad-except
|
| 182 |
+
logging.error('Failed to upload latest build: %s. Error: %s',
|
| 183 |
+
self.workspace.out, err)
|
| 184 |
+
|
| 185 |
+
def upload_crashes(self):
|
| 186 |
+
"""Uploads crashes."""
|
| 187 |
+
artifact_dirs = os.listdir(self.workspace.artifacts)
|
| 188 |
+
if not artifact_dirs:
|
| 189 |
+
logging.info('No crashes in %s. Not uploading.', self.workspace.artifacts)
|
| 190 |
+
return
|
| 191 |
+
|
| 192 |
+
for crash_target in artifact_dirs:
|
| 193 |
+
artifact_dir = os.path.join(self.workspace.artifacts, crash_target)
|
| 194 |
+
if not os.path.isdir(artifact_dir):
|
| 195 |
+
logging.warning('%s is not an expected artifact directory, skipping.',
|
| 196 |
+
crash_target)
|
| 197 |
+
continue
|
| 198 |
+
|
| 199 |
+
logging.info('Uploading crashes in %s.', artifact_dir)
|
| 200 |
+
try:
|
| 201 |
+
self.filestore.upload_crashes(crash_target, artifact_dir)
|
| 202 |
+
logging.info('Done uploading crashes.')
|
| 203 |
+
except Exception as err: # pylint: disable=broad-except
|
| 204 |
+
logging.error('Failed to upload crashes. Error: %s', err)
|
| 205 |
+
|
| 206 |
+
def upload_coverage(self):
|
| 207 |
+
"""Uploads the coverage report to the filestore."""
|
| 208 |
+
self.filestore.upload_coverage(self.COVERAGE_NAME,
|
| 209 |
+
self.workspace.coverage_report)
|
| 210 |
+
|
| 211 |
+
def get_coverage(self, repo_path):
|
| 212 |
+
"""Returns the project coverage object for the project."""
|
| 213 |
+
_make_empty_dir_if_nonexistent(self.workspace.clusterfuzz_coverage)
|
| 214 |
+
try:
|
| 215 |
+
if not self.filestore.download_coverage(
|
| 216 |
+
self.COVERAGE_NAME, self.workspace.clusterfuzz_coverage):
|
| 217 |
+
logging.error('Could not download coverage.')
|
| 218 |
+
return None
|
| 219 |
+
return get_coverage.FilesystemCoverage(
|
| 220 |
+
repo_path, self.workspace.clusterfuzz_coverage)
|
| 221 |
+
except Exception as err: # pylint: disable=broad-except
|
| 222 |
+
logging.error('Could not get coverage: %s.', err)
|
| 223 |
+
return None
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
class OSSFuzz(BaseClusterFuzzDeployment):
|
| 227 |
+
"""The OSS-Fuzz ClusterFuzz deployment."""
|
| 228 |
+
|
| 229 |
+
# Location of clusterfuzz builds on GCS.
|
| 230 |
+
CLUSTERFUZZ_BUILDS = 'clusterfuzz-builds'
|
| 231 |
+
|
| 232 |
+
# Zip file name containing the corpus.
|
| 233 |
+
CORPUS_ZIP_NAME = 'public.zip'
|
| 234 |
+
|
| 235 |
+
def get_latest_build_name(self):
|
| 236 |
+
"""Gets the name of the latest OSS-Fuzz build of a project.
|
| 237 |
+
|
| 238 |
+
Returns:
|
| 239 |
+
A string with the latest build version or None.
|
| 240 |
+
"""
|
| 241 |
+
version_file = (
|
| 242 |
+
f'{self.config.oss_fuzz_project_name}-{self.config.sanitizer}'
|
| 243 |
+
'-latest.version')
|
| 244 |
+
version_url = utils.url_join(utils.GCS_BASE_URL, self.CLUSTERFUZZ_BUILDS,
|
| 245 |
+
self.config.oss_fuzz_project_name,
|
| 246 |
+
version_file)
|
| 247 |
+
try:
|
| 248 |
+
response = urllib.request.urlopen(version_url)
|
| 249 |
+
except urllib.error.HTTPError:
|
| 250 |
+
logging.error('Error getting latest build version for %s from: %s.',
|
| 251 |
+
self.config.oss_fuzz_project_name, version_url)
|
| 252 |
+
return None
|
| 253 |
+
return response.read().decode()
|
| 254 |
+
|
| 255 |
+
def download_latest_build(self):
|
| 256 |
+
"""Downloads the latest OSS-Fuzz build from GCS.
|
| 257 |
+
|
| 258 |
+
Returns:
|
| 259 |
+
A path to where the OSS-Fuzz build was stored, or None if it wasn't.
|
| 260 |
+
"""
|
| 261 |
+
if os.path.exists(self.workspace.clusterfuzz_build):
|
| 262 |
+
# This function can be called multiple times, don't download the build
|
| 263 |
+
# again.
|
| 264 |
+
return self.workspace.clusterfuzz_build
|
| 265 |
+
|
| 266 |
+
_make_empty_dir_if_nonexistent(self.workspace.clusterfuzz_build)
|
| 267 |
+
|
| 268 |
+
latest_build_name = self.get_latest_build_name()
|
| 269 |
+
if not latest_build_name:
|
| 270 |
+
return None
|
| 271 |
+
|
| 272 |
+
logging.info('Downloading latest build.')
|
| 273 |
+
oss_fuzz_build_url = utils.url_join(utils.GCS_BASE_URL,
|
| 274 |
+
self.CLUSTERFUZZ_BUILDS,
|
| 275 |
+
self.config.oss_fuzz_project_name,
|
| 276 |
+
latest_build_name)
|
| 277 |
+
if http_utils.download_and_unpack_zip(oss_fuzz_build_url,
|
| 278 |
+
self.workspace.clusterfuzz_build):
|
| 279 |
+
logging.info('Done downloading latest build.')
|
| 280 |
+
return self.workspace.clusterfuzz_build
|
| 281 |
+
|
| 282 |
+
return None
|
| 283 |
+
|
| 284 |
+
def upload_build(self, commit): # pylint: disable=no-self-use
|
| 285 |
+
"""Noop Implementation of upload_build."""
|
| 286 |
+
logging.info('Not uploading latest build because on OSS-Fuzz.')
|
| 287 |
+
|
| 288 |
+
def upload_corpus(self, target_name, corpus_dir, replace=False): # pylint: disable=no-self-use,unused-argument
|
| 289 |
+
"""Noop Implementation of upload_corpus."""
|
| 290 |
+
logging.info('Not uploading corpus because on OSS-Fuzz.')
|
| 291 |
+
|
| 292 |
+
def upload_crashes(self): # pylint: disable=no-self-use
|
| 293 |
+
"""Noop Implementation of upload_crashes."""
|
| 294 |
+
logging.info('Not uploading crashes because on OSS-Fuzz.')
|
| 295 |
+
|
| 296 |
+
def download_corpus(self, target_name, corpus_dir):
|
| 297 |
+
"""Downloads the latest OSS-Fuzz corpus for the target.
|
| 298 |
+
|
| 299 |
+
Returns:
|
| 300 |
+
The local path to to corpus or None if download failed.
|
| 301 |
+
"""
|
| 302 |
+
_make_empty_dir_if_nonexistent(corpus_dir)
|
| 303 |
+
project_qualified_fuzz_target_name = target_name
|
| 304 |
+
qualified_name_prefix = self.config.oss_fuzz_project_name + '_'
|
| 305 |
+
if not target_name.startswith(qualified_name_prefix):
|
| 306 |
+
project_qualified_fuzz_target_name = qualified_name_prefix + target_name
|
| 307 |
+
|
| 308 |
+
corpus_url = (f'{utils.GCS_BASE_URL}{self.config.oss_fuzz_project_name}'
|
| 309 |
+
'-backup.clusterfuzz-external.appspot.com/corpus/'
|
| 310 |
+
f'libFuzzer/{project_qualified_fuzz_target_name}/'
|
| 311 |
+
f'{self.CORPUS_ZIP_NAME}')
|
| 312 |
+
logging.info('Downloading corpus from OSS-Fuzz: %s', corpus_url)
|
| 313 |
+
|
| 314 |
+
if not http_utils.download_and_unpack_zip(corpus_url, corpus_dir):
|
| 315 |
+
logging.warning('Failed to download corpus for %s.', target_name)
|
| 316 |
+
return corpus_dir
|
| 317 |
+
|
| 318 |
+
def upload_coverage(self):
|
| 319 |
+
"""Noop Implementation of upload_coverage_report."""
|
| 320 |
+
logging.info('Not uploading coverage report because on OSS-Fuzz.')
|
| 321 |
+
|
| 322 |
+
def get_coverage(self, repo_path):
|
| 323 |
+
"""Returns the project coverage object for the project."""
|
| 324 |
+
try:
|
| 325 |
+
return get_coverage.OSSFuzzCoverage(repo_path,
|
| 326 |
+
self.config.oss_fuzz_project_name)
|
| 327 |
+
except get_coverage.CoverageError:
|
| 328 |
+
return None
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
class NoClusterFuzzDeployment(BaseClusterFuzzDeployment):
|
| 332 |
+
"""ClusterFuzzDeployment implementation used when there is no deployment of
|
| 333 |
+
ClusterFuzz to use."""
|
| 334 |
+
|
| 335 |
+
def upload_build(self, commit): # pylint: disable=no-self-use
|
| 336 |
+
"""Noop Implementation of upload_build."""
|
| 337 |
+
logging.info('Not uploading latest build because no ClusterFuzz '
|
| 338 |
+
'deployment.')
|
| 339 |
+
|
| 340 |
+
def upload_corpus(self, target_name, corpus_dir, replace=False): # pylint: disable=no-self-use,unused-argument
|
| 341 |
+
"""Noop Implementation of upload_corpus."""
|
| 342 |
+
logging.info('Not uploading corpus because no ClusterFuzz deployment.')
|
| 343 |
+
|
| 344 |
+
def upload_crashes(self): # pylint: disable=no-self-use
|
| 345 |
+
"""Noop Implementation of upload_crashes."""
|
| 346 |
+
logging.info('Not uploading crashes because no ClusterFuzz deployment.')
|
| 347 |
+
|
| 348 |
+
def download_corpus(self, target_name, corpus_dir):
|
| 349 |
+
"""Noop Implementation of download_corpus."""
|
| 350 |
+
logging.info('Not downloading corpus because no ClusterFuzz deployment.')
|
| 351 |
+
return _make_empty_dir_if_nonexistent(corpus_dir)
|
| 352 |
+
|
| 353 |
+
def download_latest_build(self): # pylint: disable=no-self-use
|
| 354 |
+
"""Noop Implementation of download_latest_build."""
|
| 355 |
+
logging.info(
|
| 356 |
+
'Not downloading latest build because no ClusterFuzz deployment.')
|
| 357 |
+
|
| 358 |
+
def upload_coverage(self):
|
| 359 |
+
"""Noop Implementation of upload_coverage."""
|
| 360 |
+
logging.info(
|
| 361 |
+
'Not uploading coverage report because no ClusterFuzz deployment.')
|
| 362 |
+
|
| 363 |
+
def get_coverage(self, repo_path):
|
| 364 |
+
"""Noop Implementation of get_coverage."""
|
| 365 |
+
logging.info(
|
| 366 |
+
'Not getting project coverage because no ClusterFuzz deployment.')
|
| 367 |
+
|
| 368 |
+
|
| 369 |
+
_PLATFORM_CLUSTERFUZZ_DEPLOYMENT_MAPPING = {
|
| 370 |
+
config_utils.BaseConfig.Platform.INTERNAL_GENERIC_CI: OSSFuzz,
|
| 371 |
+
config_utils.BaseConfig.Platform.INTERNAL_GITHUB: OSSFuzz,
|
| 372 |
+
config_utils.BaseConfig.Platform.EXTERNAL_GENERIC_CI: ClusterFuzzLite,
|
| 373 |
+
config_utils.BaseConfig.Platform.EXTERNAL_GITHUB: ClusterFuzzLite,
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
|
| 377 |
+
def get_clusterfuzz_deployment(config, workspace):
|
| 378 |
+
"""Returns object reprsenting deployment of ClusterFuzz used by |config|."""
|
| 379 |
+
deployment_cls = _PLATFORM_CLUSTERFUZZ_DEPLOYMENT_MAPPING[config.platform]
|
| 380 |
+
if config.no_clusterfuzz_deployment:
|
| 381 |
+
logging.info('Overriding ClusterFuzzDeployment. Using None.')
|
| 382 |
+
deployment_cls = NoClusterFuzzDeployment
|
| 383 |
+
result = deployment_cls(config, workspace)
|
| 384 |
+
logging.info('ClusterFuzzDeployment: %s.', result)
|
| 385 |
+
return result
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/clusterfuzz_deployment_test.py
ADDED
|
@@ -0,0 +1,268 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for clusterfuzz_deployment.py"""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import unittest
|
| 18 |
+
from unittest import mock
|
| 19 |
+
|
| 20 |
+
import parameterized
|
| 21 |
+
from pyfakefs import fake_filesystem_unittest
|
| 22 |
+
|
| 23 |
+
import clusterfuzz_deployment
|
| 24 |
+
import config_utils
|
| 25 |
+
import test_helpers
|
| 26 |
+
import workspace_utils
|
| 27 |
+
|
| 28 |
+
# NOTE: This integration test relies on
|
| 29 |
+
# https://github.com/google/oss-fuzz/tree/master/projects/example project.
|
| 30 |
+
EXAMPLE_PROJECT = 'example'
|
| 31 |
+
|
| 32 |
+
# An example fuzzer that triggers an error.
|
| 33 |
+
EXAMPLE_FUZZER = 'example_crash_fuzzer'
|
| 34 |
+
|
| 35 |
+
WORKSPACE = '/workspace'
|
| 36 |
+
EXPECTED_LATEST_BUILD_PATH = os.path.join(WORKSPACE, 'cifuzz-prev-build')
|
| 37 |
+
|
| 38 |
+
# pylint: disable=unused-argument
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def _create_config(**kwargs):
|
| 42 |
+
"""Creates a config object and then sets every attribute that is a key in
|
| 43 |
+
|kwargs| to the corresponding value. Asserts that each key in |kwargs| is an
|
| 44 |
+
attribute of Config."""
|
| 45 |
+
defaults = {
|
| 46 |
+
'cfl_platform': 'github',
|
| 47 |
+
'oss_fuzz_project_name': EXAMPLE_PROJECT,
|
| 48 |
+
'workspace': WORKSPACE,
|
| 49 |
+
}
|
| 50 |
+
for default_key, default_value in defaults.items():
|
| 51 |
+
if default_key not in kwargs:
|
| 52 |
+
kwargs[default_key] = default_value
|
| 53 |
+
|
| 54 |
+
return test_helpers.create_run_config(**kwargs)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _create_deployment(**kwargs):
|
| 58 |
+
config = _create_config(**kwargs)
|
| 59 |
+
workspace = workspace_utils.Workspace(config)
|
| 60 |
+
return clusterfuzz_deployment.get_clusterfuzz_deployment(config, workspace)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
class OSSFuzzTest(fake_filesystem_unittest.TestCase):
|
| 64 |
+
"""Tests OSSFuzz."""
|
| 65 |
+
|
| 66 |
+
def setUp(self):
|
| 67 |
+
self.setUpPyfakefs()
|
| 68 |
+
self.deployment = _create_deployment()
|
| 69 |
+
self.corpus_dir = os.path.join(self.deployment.workspace.corpora,
|
| 70 |
+
EXAMPLE_FUZZER)
|
| 71 |
+
|
| 72 |
+
@mock.patch('http_utils.download_and_unpack_zip', return_value=True)
|
| 73 |
+
def test_download_corpus(self, mock_download_and_unpack_zip):
|
| 74 |
+
"""Tests that we can download a corpus for a valid project."""
|
| 75 |
+
self.deployment.download_corpus(EXAMPLE_FUZZER, self.corpus_dir)
|
| 76 |
+
expected_url = ('https://storage.googleapis.com/example-backup.'
|
| 77 |
+
'clusterfuzz-external.appspot.com/corpus/libFuzzer/'
|
| 78 |
+
'example_crash_fuzzer/public.zip')
|
| 79 |
+
call_args, _ = mock_download_and_unpack_zip.call_args
|
| 80 |
+
self.assertEqual(call_args, (expected_url, self.corpus_dir))
|
| 81 |
+
self.assertTrue(os.path.exists(self.corpus_dir))
|
| 82 |
+
|
| 83 |
+
@mock.patch('http_utils.download_and_unpack_zip', return_value=False)
|
| 84 |
+
def test_download_corpus_fail(self, _):
|
| 85 |
+
"""Tests that when downloading fails, an empty corpus directory is still
|
| 86 |
+
returned."""
|
| 87 |
+
self.deployment.download_corpus(EXAMPLE_FUZZER, self.corpus_dir)
|
| 88 |
+
self.assertEqual(os.listdir(self.corpus_dir), [])
|
| 89 |
+
|
| 90 |
+
def test_get_latest_build_name(self):
|
| 91 |
+
"""Tests that the latest build name can be retrieved from GCS."""
|
| 92 |
+
latest_build_name = self.deployment.get_latest_build_name()
|
| 93 |
+
self.assertTrue(latest_build_name.endswith('.zip'))
|
| 94 |
+
self.assertTrue('address' in latest_build_name)
|
| 95 |
+
|
| 96 |
+
@parameterized.parameterized.expand([
|
| 97 |
+
('upload_build', ('commit',),
|
| 98 |
+
'Not uploading latest build because on OSS-Fuzz.'),
|
| 99 |
+
('upload_corpus', ('target', 'corpus-dir'),
|
| 100 |
+
'Not uploading corpus because on OSS-Fuzz.'),
|
| 101 |
+
('upload_crashes', tuple(), 'Not uploading crashes because on OSS-Fuzz.'),
|
| 102 |
+
])
|
| 103 |
+
def test_noop_methods(self, method, method_args, expected_message):
|
| 104 |
+
"""Tests that certain methods are noops for OSS-Fuzz."""
|
| 105 |
+
with mock.patch('logging.info') as mock_info:
|
| 106 |
+
method = getattr(self.deployment, method)
|
| 107 |
+
self.assertIsNone(method(*method_args))
|
| 108 |
+
mock_info.assert_called_with(expected_message)
|
| 109 |
+
|
| 110 |
+
@mock.patch('http_utils.download_and_unpack_zip', return_value=True)
|
| 111 |
+
def test_download_latest_build(self, mock_download_and_unpack_zip):
|
| 112 |
+
"""Tests that downloading the latest build works as intended under normal
|
| 113 |
+
circumstances."""
|
| 114 |
+
self.assertEqual(self.deployment.download_latest_build(),
|
| 115 |
+
EXPECTED_LATEST_BUILD_PATH)
|
| 116 |
+
expected_url = ('https://storage.googleapis.com/clusterfuzz-builds/example/'
|
| 117 |
+
'example-address-202008030600.zip')
|
| 118 |
+
mock_download_and_unpack_zip.assert_called_with(expected_url,
|
| 119 |
+
EXPECTED_LATEST_BUILD_PATH)
|
| 120 |
+
|
| 121 |
+
@mock.patch('http_utils.download_and_unpack_zip', return_value=False)
|
| 122 |
+
def test_download_latest_build_fail(self, _):
|
| 123 |
+
"""Tests that download_latest_build returns None when it fails to download a
|
| 124 |
+
build."""
|
| 125 |
+
self.assertIsNone(self.deployment.download_latest_build())
|
| 126 |
+
|
| 127 |
+
|
| 128 |
+
class ClusterFuzzLiteTest(fake_filesystem_unittest.TestCase):
|
| 129 |
+
"""Tests for ClusterFuzzLite."""
|
| 130 |
+
|
| 131 |
+
def setUp(self):
|
| 132 |
+
self.setUpPyfakefs()
|
| 133 |
+
self.deployment = _create_deployment(mode='batch',
|
| 134 |
+
oss_fuzz_project_name='',
|
| 135 |
+
cloud_bucket='gs://bucket')
|
| 136 |
+
self.corpus_dir = os.path.join(self.deployment.workspace.corpora,
|
| 137 |
+
EXAMPLE_FUZZER)
|
| 138 |
+
|
| 139 |
+
@mock.patch('filestore.github_actions.GithubActionsFilestore.download_corpus',
|
| 140 |
+
return_value=True)
|
| 141 |
+
def test_download_corpus(self, mock_download_corpus):
|
| 142 |
+
"""Tests that download_corpus works for a valid project."""
|
| 143 |
+
self.deployment.download_corpus(EXAMPLE_FUZZER, self.corpus_dir)
|
| 144 |
+
mock_download_corpus.assert_called_with('example_crash_fuzzer',
|
| 145 |
+
self.corpus_dir)
|
| 146 |
+
self.assertTrue(os.path.exists(self.corpus_dir))
|
| 147 |
+
|
| 148 |
+
@mock.patch('filestore.github_actions.GithubActionsFilestore.download_corpus',
|
| 149 |
+
side_effect=Exception)
|
| 150 |
+
def test_download_corpus_fail(self, _):
|
| 151 |
+
"""Tests that when downloading fails, an empty corpus directory is still
|
| 152 |
+
returned."""
|
| 153 |
+
self.deployment.download_corpus(EXAMPLE_FUZZER, self.corpus_dir)
|
| 154 |
+
self.assertEqual(os.listdir(self.corpus_dir), [])
|
| 155 |
+
|
| 156 |
+
@mock.patch('filestore.github_actions.GithubActionsFilestore.download_build',
|
| 157 |
+
side_effect=[False, True])
|
| 158 |
+
@mock.patch('repo_manager.RepoManager.get_commit_list',
|
| 159 |
+
return_value=['commit1', 'commit2'])
|
| 160 |
+
@mock.patch('continuous_integration.GithubCiMixin.repo_dir',
|
| 161 |
+
return_value='/path/to/repo')
|
| 162 |
+
def test_download_latest_build(self, mock_repo_dir, mock_get_commit_list,
|
| 163 |
+
mock_download_build):
|
| 164 |
+
"""Tests that downloading the latest build works as intended under normal
|
| 165 |
+
circumstances."""
|
| 166 |
+
self.assertEqual(self.deployment.download_latest_build(),
|
| 167 |
+
EXPECTED_LATEST_BUILD_PATH)
|
| 168 |
+
expected_artifact_name = 'address-commit2'
|
| 169 |
+
mock_download_build.assert_called_with(expected_artifact_name,
|
| 170 |
+
EXPECTED_LATEST_BUILD_PATH)
|
| 171 |
+
|
| 172 |
+
@mock.patch('filestore.github_actions.GithubActionsFilestore.download_build',
|
| 173 |
+
side_effect=Exception)
|
| 174 |
+
@mock.patch('repo_manager.RepoManager.get_commit_list',
|
| 175 |
+
return_value=['commit1', 'commit2'])
|
| 176 |
+
@mock.patch('continuous_integration.GithubCiMixin.repo_dir',
|
| 177 |
+
return_value='/path/to/repo')
|
| 178 |
+
def test_download_latest_build_fail(self, mock_repo_dir, mock_get_commit_list,
|
| 179 |
+
_):
|
| 180 |
+
"""Tests that download_latest_build returns None when it fails to download a
|
| 181 |
+
build."""
|
| 182 |
+
self.assertIsNone(self.deployment.download_latest_build())
|
| 183 |
+
|
| 184 |
+
@mock.patch('filestore.github_actions.GithubActionsFilestore.upload_build')
|
| 185 |
+
def test_upload_build(self, mock_upload_build):
|
| 186 |
+
"""Tests that upload_build works as intended."""
|
| 187 |
+
self.deployment.upload_build('commit')
|
| 188 |
+
mock_upload_build.assert_called_with('address-commit',
|
| 189 |
+
'/workspace/build-out')
|
| 190 |
+
|
| 191 |
+
|
| 192 |
+
class NoClusterFuzzDeploymentTest(fake_filesystem_unittest.TestCase):
|
| 193 |
+
"""Tests for NoClusterFuzzDeployment."""
|
| 194 |
+
|
| 195 |
+
def setUp(self):
|
| 196 |
+
self.setUpPyfakefs()
|
| 197 |
+
config = test_helpers.create_run_config(workspace=WORKSPACE,
|
| 198 |
+
cfl_platform='other',
|
| 199 |
+
filestore='no_filestore',
|
| 200 |
+
no_clusterfuzz_deployment=True)
|
| 201 |
+
workspace = workspace_utils.Workspace(config)
|
| 202 |
+
self.deployment = clusterfuzz_deployment.get_clusterfuzz_deployment(
|
| 203 |
+
config, workspace)
|
| 204 |
+
|
| 205 |
+
self.corpus_dir = os.path.join(workspace.corpora, EXAMPLE_FUZZER)
|
| 206 |
+
|
| 207 |
+
@mock.patch('logging.info')
|
| 208 |
+
def test_download_corpus(self, mock_info):
|
| 209 |
+
"""Tests that download corpus returns the path to the empty corpus
|
| 210 |
+
directory."""
|
| 211 |
+
self.deployment.download_corpus(EXAMPLE_FUZZER, self.corpus_dir)
|
| 212 |
+
mock_info.assert_called_with(
|
| 213 |
+
'Not downloading corpus because no ClusterFuzz deployment.')
|
| 214 |
+
self.assertTrue(os.path.exists(self.corpus_dir))
|
| 215 |
+
|
| 216 |
+
@parameterized.parameterized.expand([
|
| 217 |
+
('upload_build', ('commit',),
|
| 218 |
+
'Not uploading latest build because no ClusterFuzz deployment.'),
|
| 219 |
+
('upload_corpus', ('target', 'corpus-dir'),
|
| 220 |
+
'Not uploading corpus because no ClusterFuzz deployment.'),
|
| 221 |
+
('upload_crashes', tuple(),
|
| 222 |
+
'Not uploading crashes because no ClusterFuzz deployment.'),
|
| 223 |
+
('download_latest_build', tuple(),
|
| 224 |
+
'Not downloading latest build because no ClusterFuzz deployment.')
|
| 225 |
+
])
|
| 226 |
+
def test_noop_methods(self, method, method_args, expected_message):
|
| 227 |
+
"""Tests that certain methods are noops for NoClusterFuzzDeployment."""
|
| 228 |
+
with mock.patch('logging.info') as mock_info:
|
| 229 |
+
method = getattr(self.deployment, method)
|
| 230 |
+
self.assertIsNone(method(*method_args))
|
| 231 |
+
mock_info.assert_called_with(expected_message)
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
class GetClusterFuzzDeploymentTest(unittest.TestCase):
|
| 235 |
+
"""Tests for get_clusterfuzz_deployment."""
|
| 236 |
+
|
| 237 |
+
def setUp(self):
|
| 238 |
+
test_helpers.patch_environ(self)
|
| 239 |
+
os.environ['GITHUB_REPOSITORY'] = 'owner/myproject'
|
| 240 |
+
|
| 241 |
+
@parameterized.parameterized.expand([
|
| 242 |
+
(config_utils.BaseConfig.Platform.INTERNAL_GENERIC_CI,
|
| 243 |
+
clusterfuzz_deployment.OSSFuzz),
|
| 244 |
+
(config_utils.BaseConfig.Platform.INTERNAL_GITHUB,
|
| 245 |
+
clusterfuzz_deployment.OSSFuzz),
|
| 246 |
+
(config_utils.BaseConfig.Platform.EXTERNAL_GENERIC_CI,
|
| 247 |
+
clusterfuzz_deployment.ClusterFuzzLite),
|
| 248 |
+
(config_utils.BaseConfig.Platform.EXTERNAL_GITHUB,
|
| 249 |
+
clusterfuzz_deployment.ClusterFuzzLite),
|
| 250 |
+
])
|
| 251 |
+
def test_get_clusterfuzz_deployment(self, platform, expected_deployment_cls):
|
| 252 |
+
"""Tests that get_clusterfuzz_deployment returns the correct value."""
|
| 253 |
+
with mock.patch('config_utils.BaseConfig.platform',
|
| 254 |
+
return_value=platform,
|
| 255 |
+
new_callable=mock.PropertyMock):
|
| 256 |
+
with mock.patch('filestore_utils.get_filestore', return_value=None):
|
| 257 |
+
with mock.patch('platform_config.github._get_event_data',
|
| 258 |
+
return_value={}):
|
| 259 |
+
config = _create_config()
|
| 260 |
+
workspace = workspace_utils.Workspace(config)
|
| 261 |
+
|
| 262 |
+
self.assertIsInstance(
|
| 263 |
+
clusterfuzz_deployment.get_clusterfuzz_deployment(
|
| 264 |
+
config, workspace), expected_deployment_cls)
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
if __name__ == '__main__':
|
| 268 |
+
unittest.main()
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/config_utils_test.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for config_utils."""
|
| 15 |
+
import os
|
| 16 |
+
import unittest
|
| 17 |
+
from unittest import mock
|
| 18 |
+
|
| 19 |
+
import config_utils
|
| 20 |
+
import constants
|
| 21 |
+
import test_helpers
|
| 22 |
+
|
| 23 |
+
# pylint: disable=no-self-use,protected-access
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class BaseConfigTest(unittest.TestCase):
|
| 27 |
+
"""Tests for BaseConfig."""
|
| 28 |
+
|
| 29 |
+
def setUp(self):
|
| 30 |
+
test_helpers.patch_environ(self)
|
| 31 |
+
|
| 32 |
+
def _create_config(self):
|
| 33 |
+
return config_utils.BuildFuzzersConfig()
|
| 34 |
+
|
| 35 |
+
def test_language_default(self):
|
| 36 |
+
"""Tests that the correct default language is set."""
|
| 37 |
+
config = self._create_config()
|
| 38 |
+
self.assertEqual(config.language, 'c++')
|
| 39 |
+
|
| 40 |
+
def test_language(self):
|
| 41 |
+
"""Tests that the correct language is set."""
|
| 42 |
+
language = 'python'
|
| 43 |
+
os.environ['LANGUAGE'] = language
|
| 44 |
+
config = self._create_config()
|
| 45 |
+
self.assertEqual(config.language, language)
|
| 46 |
+
|
| 47 |
+
def test_is_coverage(self):
|
| 48 |
+
"""Tests that is_coverage is set correctly."""
|
| 49 |
+
# Test it is set when it is supposed to be.
|
| 50 |
+
os.environ['SANITIZER'] = 'coverage'
|
| 51 |
+
config = self._create_config()
|
| 52 |
+
self.assertTrue(config.is_coverage)
|
| 53 |
+
|
| 54 |
+
# Test it is not set when it is not supposed to be.
|
| 55 |
+
os.environ['SANITIZER'] = 'address'
|
| 56 |
+
config = self._create_config()
|
| 57 |
+
self.assertFalse(config.is_coverage)
|
| 58 |
+
|
| 59 |
+
@mock.patch('logging.error')
|
| 60 |
+
def test_validate_no_workspace(self, mock_error):
|
| 61 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 62 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 63 |
+
config = self._create_config()
|
| 64 |
+
self.assertFalse(config.validate())
|
| 65 |
+
mock_error.assert_called_with('Must set WORKSPACE.')
|
| 66 |
+
|
| 67 |
+
@mock.patch('logging.error')
|
| 68 |
+
def test_validate_invalid_language(self, mock_error):
|
| 69 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 70 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 71 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 72 |
+
os.environ['LANGUAGE'] = 'invalid-language'
|
| 73 |
+
config = self._create_config()
|
| 74 |
+
self.assertFalse(config.validate())
|
| 75 |
+
mock_error.assert_called_with('Invalid LANGUAGE: %s. Must be one of: %s.',
|
| 76 |
+
os.environ['LANGUAGE'], constants.LANGUAGES)
|
| 77 |
+
|
| 78 |
+
@mock.patch('logging.error')
|
| 79 |
+
def test_validate_invalid_sanitizer(self, mock_error):
|
| 80 |
+
"""Tests that validate returns False if GITHUB_WORKSPACE isn't set."""
|
| 81 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 82 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 83 |
+
os.environ['SANITIZER'] = 'invalid-sanitizer'
|
| 84 |
+
config = self._create_config()
|
| 85 |
+
self.assertFalse(config.validate())
|
| 86 |
+
mock_error.assert_called_with('Invalid SANITIZER: %s. Must be one of: %s.',
|
| 87 |
+
os.environ['SANITIZER'],
|
| 88 |
+
config_utils.SANITIZERS)
|
| 89 |
+
|
| 90 |
+
def test_validate(self):
|
| 91 |
+
"""Tests that validate returns True if config is valid."""
|
| 92 |
+
os.environ['OSS_FUZZ_PROJECT_NAME'] = 'example'
|
| 93 |
+
os.environ['WORKSPACE'] = '/workspace'
|
| 94 |
+
os.environ['REPOSITORY'] = 'repo'
|
| 95 |
+
config = self._create_config()
|
| 96 |
+
self.assertTrue(config.validate())
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
class BuildFuzzersConfigTest(unittest.TestCase):
|
| 100 |
+
"""Tests for BuildFuzzersConfig."""
|
| 101 |
+
|
| 102 |
+
def setUp(self):
|
| 103 |
+
test_helpers.patch_environ(self)
|
| 104 |
+
|
| 105 |
+
def _create_config(self):
|
| 106 |
+
return config_utils.BuildFuzzersConfig()
|
| 107 |
+
|
| 108 |
+
@mock.patch('platform_config.github._get_event_data', return_value={})
|
| 109 |
+
def test_github_base_ref(self, _):
|
| 110 |
+
"""Tests that base_ref is set properly."""
|
| 111 |
+
expected_base_ref = 'expected_base_ref'
|
| 112 |
+
os.environ['GITHUB_BASE_REF'] = expected_base_ref
|
| 113 |
+
os.environ['CFL_PLATFORM'] = 'github'
|
| 114 |
+
os.environ['GITHUB_REPOSITORY'] = 'owner/repo'
|
| 115 |
+
config = self._create_config()
|
| 116 |
+
self.assertEqual(config.base_ref, expected_base_ref)
|
| 117 |
+
|
| 118 |
+
def test_base_ref(self):
|
| 119 |
+
"""Tests that base_ref is set properly."""
|
| 120 |
+
expected_base_ref = 'expected_base_ref'
|
| 121 |
+
os.environ['GIT_BASE_REF'] = expected_base_ref
|
| 122 |
+
config = self._create_config()
|
| 123 |
+
self.assertEqual(config.base_ref, expected_base_ref)
|
| 124 |
+
|
| 125 |
+
def test_keep_unaffected_defaults_to_true(self):
|
| 126 |
+
"""Tests that keep_unaffected_fuzz_targets defaults to true."""
|
| 127 |
+
config = self._create_config()
|
| 128 |
+
self.assertTrue(config.keep_unaffected_fuzz_targets)
|
| 129 |
+
|
| 130 |
+
def test_keep_unaffected_defaults_to_false_when_pr(self):
|
| 131 |
+
"""Tests that keep_unaffected_fuzz_targets defaults to false when from a
|
| 132 |
+
pr."""
|
| 133 |
+
os.environ['GIT_BASE_REF'] = 'base-ref'
|
| 134 |
+
config = self._create_config()
|
| 135 |
+
self.assertFalse(config.keep_unaffected_fuzz_targets)
|
| 136 |
+
|
| 137 |
+
|
| 138 |
+
class RunFuzzersConfigTest(unittest.TestCase):
|
| 139 |
+
"""Tests for RunFuzzersConfig."""
|
| 140 |
+
|
| 141 |
+
def setUp(self):
|
| 142 |
+
test_helpers.patch_environ(self)
|
| 143 |
+
|
| 144 |
+
def _create_config(self):
|
| 145 |
+
return config_utils.RunFuzzersConfig()
|
| 146 |
+
|
| 147 |
+
def test_coverage(self):
|
| 148 |
+
"""Tests that mode is overriden properly based on is_coverage."""
|
| 149 |
+
# Test that it is overriden when it is supposed to be.
|
| 150 |
+
os.environ['SANITIZER'] = 'coverage'
|
| 151 |
+
os.environ['MODE'] = 'code-change'
|
| 152 |
+
config = self._create_config()
|
| 153 |
+
self.assertEqual(config.mode, 'coverage')
|
| 154 |
+
|
| 155 |
+
# Test that it isn't overriden when it isn't supposed to be.
|
| 156 |
+
os.environ['SANITIZER'] = 'address'
|
| 157 |
+
mode = 'code-change'
|
| 158 |
+
os.environ['MODE'] = mode
|
| 159 |
+
config = self._create_config()
|
| 160 |
+
self.assertEqual(config.mode, mode)
|
| 161 |
+
|
| 162 |
+
def test_run_config_validate(self):
|
| 163 |
+
"""Tests that _run_config_validate returns True when the config is valid."""
|
| 164 |
+
self.assertTrue(self._create_config()._run_config_validate())
|
| 165 |
+
|
| 166 |
+
@mock.patch('logging.error')
|
| 167 |
+
def test_run_config_invalid_mode(self, mock_error):
|
| 168 |
+
"""Tests that _run_config_validate returns False when mode is invalid."""
|
| 169 |
+
fake_mode = 'fake-mode'
|
| 170 |
+
os.environ['MODE'] = fake_mode
|
| 171 |
+
self.assertFalse(self._create_config()._run_config_validate())
|
| 172 |
+
mock_error.assert_called_with('Invalid MODE: %s. Must be one of %s.',
|
| 173 |
+
fake_mode,
|
| 174 |
+
config_utils.RunFuzzersConfig.MODES)
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
class GetSanitizerTest(unittest.TestCase):
|
| 178 |
+
"""Tests for _get_sanitizer."""
|
| 179 |
+
|
| 180 |
+
def setUp(self):
|
| 181 |
+
test_helpers.patch_environ(self)
|
| 182 |
+
self.sanitizer = 'memory'
|
| 183 |
+
|
| 184 |
+
def test_default_value(self):
|
| 185 |
+
"""Tests that the default value returned by _get_sanitizer is correct."""
|
| 186 |
+
self.assertEqual(config_utils._get_sanitizer(), 'address')
|
| 187 |
+
|
| 188 |
+
def test_normal_case(self):
|
| 189 |
+
"""Tests that _get_sanitizer returns the correct value in normal cases."""
|
| 190 |
+
os.environ['SANITIZER'] = self.sanitizer
|
| 191 |
+
self.assertEqual(config_utils._get_sanitizer(), self.sanitizer)
|
| 192 |
+
|
| 193 |
+
def test_capitalization(self):
|
| 194 |
+
"""Tests that that _get_sanitizer handles capitalization properly."""
|
| 195 |
+
os.environ['SANITIZER'] = self.sanitizer.upper()
|
| 196 |
+
self.assertEqual(config_utils._get_sanitizer(), self.sanitizer)
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
if __name__ == '__main__':
|
| 200 |
+
unittest.main()
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/continuous_integration_test.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests for continuous_integration_module."""
|
| 15 |
+
import os
|
| 16 |
+
import sys
|
| 17 |
+
import unittest
|
| 18 |
+
from unittest import mock
|
| 19 |
+
|
| 20 |
+
import continuous_integration
|
| 21 |
+
import docker
|
| 22 |
+
|
| 23 |
+
# pylint: disable=wrong-import-position,import-error
|
| 24 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 25 |
+
|
| 26 |
+
import repo_manager
|
| 27 |
+
|
| 28 |
+
# pylint: disable=no-self-use
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class FixGitRepoForDiffTest(unittest.TestCase):
|
| 32 |
+
"""Tests for fix_git_repo_for_diff."""
|
| 33 |
+
|
| 34 |
+
@mock.patch('utils.execute')
|
| 35 |
+
def test_fix_git_repo_for_diff(self, mock_execute):
|
| 36 |
+
"""Tests that fix_git_repo_for_diff works as intended."""
|
| 37 |
+
repo_dir = '/dir'
|
| 38 |
+
repo_manager_obj = repo_manager.RepoManager(repo_dir)
|
| 39 |
+
continuous_integration.fix_git_repo_for_diff(repo_manager_obj)
|
| 40 |
+
expected_command = [
|
| 41 |
+
'git', 'symbolic-ref', 'refs/remotes/origin/HEAD',
|
| 42 |
+
'refs/remotes/origin/master'
|
| 43 |
+
]
|
| 44 |
+
|
| 45 |
+
mock_execute.assert_called_with(expected_command, location=repo_dir)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class GetBuildCommand(unittest.TestCase):
|
| 49 |
+
"""Tests for get_build_command."""
|
| 50 |
+
|
| 51 |
+
def test_build_command(self):
|
| 52 |
+
"""Tests that get_build_command works as intended."""
|
| 53 |
+
self.assertEqual(continuous_integration.get_build_command(), 'compile')
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
class GetReplaceRepoAndBuildCommand(unittest.TestCase):
|
| 57 |
+
"""Tests for get_replace_repo_and_build_command."""
|
| 58 |
+
|
| 59 |
+
def test_get_replace_repo_and_build_command(self):
|
| 60 |
+
"""Tests that get_replace_repo_and_build_command works as intended."""
|
| 61 |
+
host_repo_path = '/path/on/host/to/repo'
|
| 62 |
+
image_repo_path = '/src/repo'
|
| 63 |
+
command = continuous_integration.get_replace_repo_and_build_command(
|
| 64 |
+
host_repo_path, image_repo_path)
|
| 65 |
+
expected_command = ('cd / && rm -rf /src/repo/* && '
|
| 66 |
+
'cp -r /path/on/host/to/repo /src && cd - '
|
| 67 |
+
'&& compile')
|
| 68 |
+
self.assertEqual(command, expected_command)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
class BuildExternalProjetDockerImage(unittest.TestCase):
|
| 72 |
+
"""Tests for build_external_project_docker_image."""
|
| 73 |
+
|
| 74 |
+
@mock.patch('helper.docker_build')
|
| 75 |
+
def test_build_external_project_docker_image(self, mock_docker_build):
|
| 76 |
+
"""Tests that build_external_project_docker_image works as intended."""
|
| 77 |
+
build_integration_path = '.clusterfuzzlite'
|
| 78 |
+
project_src = '/path/to/project/src'
|
| 79 |
+
continuous_integration.build_external_project_docker_image(
|
| 80 |
+
project_src, build_integration_path)
|
| 81 |
+
|
| 82 |
+
mock_docker_build.assert_called_with([
|
| 83 |
+
'-t', docker.EXTERNAL_PROJECT_IMAGE, '-f',
|
| 84 |
+
os.path.join('.clusterfuzzlite', 'Dockerfile'), project_src
|
| 85 |
+
])
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
# TODO(metzman): Write tests for the rest of continuous_integration.py.
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/docker.py
ADDED
|
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for dealing with docker."""
|
| 15 |
+
import logging
|
| 16 |
+
import os
|
| 17 |
+
import sys
|
| 18 |
+
import uuid
|
| 19 |
+
|
| 20 |
+
# pylint: disable=wrong-import-position,import-error
|
| 21 |
+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 22 |
+
|
| 23 |
+
import constants
|
| 24 |
+
import utils
|
| 25 |
+
import environment
|
| 26 |
+
|
| 27 |
+
BASE_BUILDER_TAG = 'ghcr.io/aixcc-finals/base-builder'
|
| 28 |
+
PROJECT_TAG_PREFIX = 'gcr.io/oss-fuzz/'
|
| 29 |
+
|
| 30 |
+
# Default fuzz configuration.
|
| 31 |
+
_DEFAULT_DOCKER_RUN_ARGS = [
|
| 32 |
+
'-e', 'FUZZING_ENGINE=' + constants.DEFAULT_ENGINE, '-e', 'CIFUZZ=True'
|
| 33 |
+
]
|
| 34 |
+
|
| 35 |
+
UNIQUE_ID_SUFFIX = '-' + uuid.uuid4().hex
|
| 36 |
+
|
| 37 |
+
# TODO(metzman): Make run_fuzzers able to delete this image.
|
| 38 |
+
EXTERNAL_PROJECT_IMAGE = 'external-cfl-project' + UNIQUE_ID_SUFFIX
|
| 39 |
+
|
| 40 |
+
_DEFAULT_DOCKER_RUN_COMMAND = [
|
| 41 |
+
'docker',
|
| 42 |
+
'run',
|
| 43 |
+
'--rm',
|
| 44 |
+
'--privileged',
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def get_docker_env_vars(env_mapping):
|
| 49 |
+
"""Returns a list of docker arguments that sets each key in |env_mapping| as
|
| 50 |
+
an env var and the value of that key in |env_mapping| as the value."""
|
| 51 |
+
env_var_args = []
|
| 52 |
+
for env_var, env_var_val in env_mapping.items():
|
| 53 |
+
env_var_args.extend(['-e', f'{env_var}={env_var_val}'])
|
| 54 |
+
return env_var_args
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def get_project_image_name(project):
|
| 58 |
+
"""Returns the name of the project builder image for |project_name|."""
|
| 59 |
+
# TODO(jonathanmetzman): We may need unique names to support parallel fuzzing
|
| 60 |
+
# for CIFuzz (like CFL supports). Don't do this for now because no one has
|
| 61 |
+
# asked for it and build_specified_commit would need to be modified to support
|
| 62 |
+
# this.
|
| 63 |
+
if project:
|
| 64 |
+
return PROJECT_TAG_PREFIX + project
|
| 65 |
+
|
| 66 |
+
return EXTERNAL_PROJECT_IMAGE
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def delete_images(images):
|
| 70 |
+
"""Deletes |images|."""
|
| 71 |
+
command = ['docker', 'rmi', '-f'] + images
|
| 72 |
+
utils.execute(command)
|
| 73 |
+
utils.execute(['docker', 'builder', 'prune', '-f'])
|
| 74 |
+
|
| 75 |
+
|
| 76 |
+
def get_base_docker_run_args(workspace,
|
| 77 |
+
sanitizer=constants.DEFAULT_SANITIZER,
|
| 78 |
+
language=constants.DEFAULT_LANGUAGE,
|
| 79 |
+
architecture=constants.DEFAULT_ARCHITECTURE,
|
| 80 |
+
docker_in_docker=False):
|
| 81 |
+
"""Returns arguments that should be passed to every invocation of 'docker
|
| 82 |
+
run'."""
|
| 83 |
+
docker_args = _DEFAULT_DOCKER_RUN_ARGS.copy()
|
| 84 |
+
env_mapping = {
|
| 85 |
+
'SANITIZER': sanitizer,
|
| 86 |
+
'ARCHITECTURE': architecture,
|
| 87 |
+
'FUZZING_LANGUAGE': language,
|
| 88 |
+
'OUT': workspace.out
|
| 89 |
+
}
|
| 90 |
+
docker_args += get_docker_env_vars(env_mapping)
|
| 91 |
+
docker_container = environment.get('CFL_CONTAINER_ID',
|
| 92 |
+
utils.get_container_name())
|
| 93 |
+
logging.info('Docker container: %s.', docker_container)
|
| 94 |
+
if docker_container and not docker_in_docker:
|
| 95 |
+
# Don't map specific volumes if in a docker container, it breaks when
|
| 96 |
+
# running a sibling container.
|
| 97 |
+
docker_args += ['--volumes-from', docker_container]
|
| 98 |
+
else:
|
| 99 |
+
docker_args += _get_args_mapping_host_path_to_container(workspace.workspace)
|
| 100 |
+
return docker_args, docker_container
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
def get_base_docker_run_command(workspace,
|
| 104 |
+
sanitizer=constants.DEFAULT_SANITIZER,
|
| 105 |
+
language=constants.DEFAULT_LANGUAGE,
|
| 106 |
+
architecture=constants.DEFAULT_ARCHITECTURE,
|
| 107 |
+
docker_in_docker=False):
|
| 108 |
+
"""Returns part of the command that should be used everytime 'docker run' is
|
| 109 |
+
invoked."""
|
| 110 |
+
docker_args, docker_container = get_base_docker_run_args(
|
| 111 |
+
workspace,
|
| 112 |
+
sanitizer,
|
| 113 |
+
language,
|
| 114 |
+
architecture,
|
| 115 |
+
docker_in_docker=docker_in_docker)
|
| 116 |
+
command = _DEFAULT_DOCKER_RUN_COMMAND.copy() + docker_args
|
| 117 |
+
return command, docker_container
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
def _get_args_mapping_host_path_to_container(host_path, container_path=None):
|
| 121 |
+
"""Get arguments to docker run that will map |host_path| a path on the host to
|
| 122 |
+
a path in the container. If |container_path| is specified, that path is mapped
|
| 123 |
+
to. If not, then |host_path| is mapped to itself in the container."""
|
| 124 |
+
# WARNING: Do not use this function when running in production (and
|
| 125 |
+
# --volumes-from) is used for mapping volumes. It will break production.
|
| 126 |
+
container_path = host_path if container_path is None else container_path
|
| 127 |
+
return ['-v', f'{host_path}:{container_path}']
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/docker_test.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Tests the functionality of the docker module."""
|
| 15 |
+
import unittest
|
| 16 |
+
from unittest import mock
|
| 17 |
+
|
| 18 |
+
import docker
|
| 19 |
+
import test_helpers
|
| 20 |
+
import workspace_utils
|
| 21 |
+
|
| 22 |
+
CONTAINER_NAME = 'example-container'
|
| 23 |
+
config = test_helpers.create_run_config(oss_fuzz_project_name='project',
|
| 24 |
+
workspace='/workspace')
|
| 25 |
+
config.workspace = '/workspace'
|
| 26 |
+
WORKSPACE = workspace_utils.Workspace(config)
|
| 27 |
+
SANITIZER = 'example-sanitizer'
|
| 28 |
+
LANGUAGE = 'example-language'
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class GetProjectImageTest(unittest.TestCase):
|
| 32 |
+
"""Tests for get_project_image."""
|
| 33 |
+
|
| 34 |
+
def test_get_project_image(self):
|
| 35 |
+
"""Tests that get_project_image_name works as intended."""
|
| 36 |
+
project = 'my-project'
|
| 37 |
+
self.assertEqual(docker.get_project_image_name(project),
|
| 38 |
+
'gcr.io/oss-fuzz/my-project')
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class GetDeleteImagesTest(unittest.TestCase):
|
| 42 |
+
"""Tests for delete_images."""
|
| 43 |
+
|
| 44 |
+
@mock.patch('utils.execute')
|
| 45 |
+
def test_delete_images(self, mock_execute): # pylint: disable=no-self-use
|
| 46 |
+
"""Tests that get_project_image_name works as intended."""
|
| 47 |
+
images = ['image']
|
| 48 |
+
docker.delete_images(images)
|
| 49 |
+
expected_calls = [
|
| 50 |
+
mock.call(['docker', 'rmi', '-f'] + images),
|
| 51 |
+
mock.call(['docker', 'builder', 'prune', '-f'])
|
| 52 |
+
]
|
| 53 |
+
|
| 54 |
+
mock_execute.assert_has_calls(expected_calls)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class GetBaseDockerRunArgsTest(unittest.TestCase):
|
| 58 |
+
"""Tests get_base_docker_run_args."""
|
| 59 |
+
|
| 60 |
+
@mock.patch('utils.get_container_name', return_value=CONTAINER_NAME)
|
| 61 |
+
def test_get_base_docker_run_args_container(self, _):
|
| 62 |
+
"""Tests that get_base_docker_run_args works as intended when inside a
|
| 63 |
+
container."""
|
| 64 |
+
docker_args, docker_container = docker.get_base_docker_run_args(
|
| 65 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 66 |
+
self.assertEqual(docker_container, CONTAINER_NAME)
|
| 67 |
+
expected_docker_args = []
|
| 68 |
+
expected_docker_args = [
|
| 69 |
+
'-e',
|
| 70 |
+
'FUZZING_ENGINE=libfuzzer',
|
| 71 |
+
'-e',
|
| 72 |
+
'CIFUZZ=True',
|
| 73 |
+
'-e',
|
| 74 |
+
f'SANITIZER={SANITIZER}',
|
| 75 |
+
'-e',
|
| 76 |
+
'ARCHITECTURE=x86_64',
|
| 77 |
+
'-e',
|
| 78 |
+
f'FUZZING_LANGUAGE={LANGUAGE}',
|
| 79 |
+
'-e',
|
| 80 |
+
f'OUT={WORKSPACE.out}',
|
| 81 |
+
'--volumes-from',
|
| 82 |
+
CONTAINER_NAME,
|
| 83 |
+
]
|
| 84 |
+
self.assertEqual(docker_args, expected_docker_args)
|
| 85 |
+
|
| 86 |
+
@mock.patch('utils.get_container_name', return_value=None)
|
| 87 |
+
def test_get_base_docker_run_args_no_container(self, _):
|
| 88 |
+
"""Tests that get_base_docker_run_args works as intended when not inside a
|
| 89 |
+
container."""
|
| 90 |
+
docker_args, docker_container = docker.get_base_docker_run_args(
|
| 91 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 92 |
+
self.assertEqual(docker_container, None)
|
| 93 |
+
expected_docker_args = [
|
| 94 |
+
'-e', 'FUZZING_ENGINE=libfuzzer', '-e', 'CIFUZZ=True', '-e',
|
| 95 |
+
f'SANITIZER={SANITIZER}', '-e', 'ARCHITECTURE=x86_64', '-e',
|
| 96 |
+
f'FUZZING_LANGUAGE={LANGUAGE}', '-e', f'OUT={WORKSPACE.out}', '-v',
|
| 97 |
+
f'{WORKSPACE.workspace}:{WORKSPACE.workspace}'
|
| 98 |
+
]
|
| 99 |
+
self.assertEqual(docker_args, expected_docker_args)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class GetBaseDockerRunCommandTest(unittest.TestCase):
|
| 103 |
+
"""Tests get_base_docker_run_args."""
|
| 104 |
+
|
| 105 |
+
@mock.patch('utils.get_container_name', return_value=None)
|
| 106 |
+
def test_get_base_docker_run_command_no_container(self, _):
|
| 107 |
+
"""Tests that get_base_docker_run_args works as intended when not inside a
|
| 108 |
+
container."""
|
| 109 |
+
docker_args, docker_container = docker.get_base_docker_run_command(
|
| 110 |
+
WORKSPACE, SANITIZER, LANGUAGE)
|
| 111 |
+
self.assertEqual(docker_container, None)
|
| 112 |
+
expected_docker_command = [
|
| 113 |
+
'docker', 'run', '--rm', '--privileged', '-e',
|
| 114 |
+
'FUZZING_ENGINE=libfuzzer', '-e', 'CIFUZZ=True', '-e',
|
| 115 |
+
f'SANITIZER={SANITIZER}', '-e', 'ARCHITECTURE=x86_64', '-e',
|
| 116 |
+
f'FUZZING_LANGUAGE={LANGUAGE}', '-e', f'OUT={WORKSPACE.out}', '-v',
|
| 117 |
+
f'{WORKSPACE.workspace}:{WORKSPACE.workspace}'
|
| 118 |
+
]
|
| 119 |
+
self.assertEqual(docker_args, expected_docker_command)
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/environment.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for dealing with env vars."""
|
| 15 |
+
|
| 16 |
+
import ast
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _eval_value(value_string):
|
| 21 |
+
"""Returns evaluated value."""
|
| 22 |
+
try:
|
| 23 |
+
return ast.literal_eval(value_string)
|
| 24 |
+
except: # pylint: disable=bare-except
|
| 25 |
+
# String fallback.
|
| 26 |
+
return value_string
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def get(env_var, default_value=None):
|
| 30 |
+
"""Returns an environment variable value."""
|
| 31 |
+
value_string = os.getenv(env_var)
|
| 32 |
+
if value_string is None:
|
| 33 |
+
return default_value
|
| 34 |
+
|
| 35 |
+
return _eval_value(value_string)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
def get_bool(env_var, default_value=None):
|
| 39 |
+
"""Returns a boolean environment variable value. This is needed because a lot
|
| 40 |
+
of CIFuzz users specified 'false' for dry-run. So we need to special case
|
| 41 |
+
this."""
|
| 42 |
+
value = get(env_var, default_value)
|
| 43 |
+
if not isinstance(value, str):
|
| 44 |
+
return bool(value)
|
| 45 |
+
|
| 46 |
+
lower_value = value.lower()
|
| 47 |
+
allowed_values = {'true', 'false'}
|
| 48 |
+
if lower_value not in allowed_values:
|
| 49 |
+
raise Exception(f'Bool env var {env_var} value {value} is invalid. '
|
| 50 |
+
f'Must be one of {allowed_values}.')
|
| 51 |
+
return lower_value == 'true'
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/fuzz_target.py
ADDED
|
@@ -0,0 +1,408 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2020 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""A module to handle running a fuzz target for a specified amount of time."""
|
| 15 |
+
import collections
|
| 16 |
+
import logging
|
| 17 |
+
import multiprocessing
|
| 18 |
+
import os
|
| 19 |
+
import shutil
|
| 20 |
+
import stat
|
| 21 |
+
import tempfile
|
| 22 |
+
from typing import Optional
|
| 23 |
+
|
| 24 |
+
import clusterfuzz.environment
|
| 25 |
+
import clusterfuzz.fuzz
|
| 26 |
+
|
| 27 |
+
import config_utils
|
| 28 |
+
import logs
|
| 29 |
+
|
| 30 |
+
logs.init()
|
| 31 |
+
|
| 32 |
+
# Use len_control=0 since we don't have enough time fuzzing for len_control to
|
| 33 |
+
# make sense (probably).
|
| 34 |
+
LIBFUZZER_OPTIONS_BATCH = ['-len_control=0']
|
| 35 |
+
# Use a fixed seed for determinism for code change fuzzing.
|
| 36 |
+
LIBFUZZER_OPTIONS_CODE_CHANGE = LIBFUZZER_OPTIONS_BATCH + ['-seed=1337']
|
| 37 |
+
LIBFUZZER_OPTIONS_NO_REPORT_OOM = ['-rss_limit_mb=0']
|
| 38 |
+
|
| 39 |
+
# The number of reproduce attempts for a crash.
|
| 40 |
+
REPRODUCE_ATTEMPTS = 10
|
| 41 |
+
|
| 42 |
+
DEFAULT_REPRODUCE_TIME_SECONDS = 30
|
| 43 |
+
PER_LANGUAGE_REPRODUCE_TIMEOUTS = {
|
| 44 |
+
'python': 30 * 4 # Python takes a bit longer on startup.
|
| 45 |
+
}
|
| 46 |
+
MINIMIZE_TIME_SECONDS = 60 * 4
|
| 47 |
+
|
| 48 |
+
# Seconds on top of duration until a timeout error is raised.
|
| 49 |
+
BUFFER_TIME = 10
|
| 50 |
+
|
| 51 |
+
# Log message if we can't check if crash reproduces on an recent build.
|
| 52 |
+
COULD_NOT_TEST_ON_CLUSTERFUZZ_MESSAGE = (
|
| 53 |
+
'Could not run previous build of target to determine if this code change '
|
| 54 |
+
'(pr/commit) introduced crash. Assuming crash was newly introduced.')
|
| 55 |
+
|
| 56 |
+
FuzzResult = collections.namedtuple('FuzzResult',
|
| 57 |
+
['testcase', 'stacktrace', 'corpus_path'])
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def get_libfuzzer_parallel_options():
|
| 61 |
+
"""Returns a list containing options to pass to libFuzzer to fuzz using all
|
| 62 |
+
available cores."""
|
| 63 |
+
return ['-jobs=' + str(multiprocessing.cpu_count())]
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class ReproduceError(Exception):
|
| 67 |
+
"""Error for when we can't attempt to reproduce a crash."""
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
def get_fuzz_target_corpus_dir(workspace, target_name):
|
| 71 |
+
"""Returns the directory for storing |target_name|'s corpus in |workspace|."""
|
| 72 |
+
return os.path.join(workspace.corpora, target_name)
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def get_fuzz_target_pruned_corpus_dir(workspace, target_name):
|
| 76 |
+
"""Returns the directory for storing |target_name|'s puned corpus in
|
| 77 |
+
|workspace|."""
|
| 78 |
+
return os.path.join(workspace.pruned_corpora, target_name)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
class FuzzTarget: # pylint: disable=too-many-instance-attributes
|
| 82 |
+
"""A class to manage a single fuzz target.
|
| 83 |
+
|
| 84 |
+
Attributes:
|
| 85 |
+
target_name: The name of the fuzz target.
|
| 86 |
+
duration: The length of time in seconds that the target should run.
|
| 87 |
+
target_path: The location of the fuzz target binary.
|
| 88 |
+
workspace: The workspace for storing things related to fuzzing.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
# pylint: disable=too-many-arguments
|
| 92 |
+
def __init__(self, target_path, duration, workspace, clusterfuzz_deployment,
|
| 93 |
+
config):
|
| 94 |
+
"""Represents a single fuzz target.
|
| 95 |
+
|
| 96 |
+
Args:
|
| 97 |
+
target_path: The location of the fuzz target binary.
|
| 98 |
+
duration: The length of time in seconds the target should run.
|
| 99 |
+
workspace: The path used for storing things needed for fuzzing.
|
| 100 |
+
clusterfuzz_deployment: The object representing the ClusterFuzz
|
| 101 |
+
deployment.
|
| 102 |
+
config: The config of this project.
|
| 103 |
+
"""
|
| 104 |
+
self.target_path = target_path
|
| 105 |
+
self.target_name = os.path.basename(self.target_path)
|
| 106 |
+
self.duration = int(duration)
|
| 107 |
+
self.workspace = workspace
|
| 108 |
+
self.clusterfuzz_deployment = clusterfuzz_deployment
|
| 109 |
+
self.config = config
|
| 110 |
+
self.latest_corpus_path = get_fuzz_target_corpus_dir(
|
| 111 |
+
self.workspace, self.target_name)
|
| 112 |
+
os.makedirs(self.latest_corpus_path, exist_ok=True)
|
| 113 |
+
self.pruned_corpus_path = get_fuzz_target_pruned_corpus_dir(
|
| 114 |
+
self.workspace, self.target_name)
|
| 115 |
+
os.makedirs(self.pruned_corpus_path, exist_ok=True)
|
| 116 |
+
|
| 117 |
+
def _download_corpus(self):
|
| 118 |
+
"""Downloads the corpus for the target from ClusterFuzz and returns the path
|
| 119 |
+
to the corpus. An empty directory is provided if the corpus can't be
|
| 120 |
+
downloaded or is empty."""
|
| 121 |
+
self.clusterfuzz_deployment.download_corpus(self.target_name,
|
| 122 |
+
self.latest_corpus_path)
|
| 123 |
+
return self.latest_corpus_path
|
| 124 |
+
|
| 125 |
+
def _target_artifact_path(self):
|
| 126 |
+
"""Target artifact path."""
|
| 127 |
+
artifact_path = os.path.join(self.workspace.artifacts, self.target_name,
|
| 128 |
+
self.config.sanitizer)
|
| 129 |
+
os.makedirs(artifact_path, exist_ok=True)
|
| 130 |
+
return artifact_path
|
| 131 |
+
|
| 132 |
+
def _save_crash(self, crash):
|
| 133 |
+
"""Add stacktraces to crashes."""
|
| 134 |
+
target_reproducer_path = os.path.join(self._target_artifact_path(),
|
| 135 |
+
os.path.basename(crash.input_path))
|
| 136 |
+
shutil.copy(crash.input_path, target_reproducer_path)
|
| 137 |
+
bug_summary_artifact_path = target_reproducer_path + '.summary'
|
| 138 |
+
with open(bug_summary_artifact_path, 'w') as handle:
|
| 139 |
+
handle.write(crash.stacktrace)
|
| 140 |
+
|
| 141 |
+
# Set permissions of testcase to be the same as summary so that we're sure
|
| 142 |
+
# it can be read by necessary users.
|
| 143 |
+
permissions_mode = os.stat(bug_summary_artifact_path).st_mode
|
| 144 |
+
os.chmod(target_reproducer_path, permissions_mode & 0o777)
|
| 145 |
+
return target_reproducer_path
|
| 146 |
+
|
| 147 |
+
def prune(self):
|
| 148 |
+
"""Prunes the corpus and returns the result."""
|
| 149 |
+
self._download_corpus()
|
| 150 |
+
with clusterfuzz.environment.Environment(config_utils.DEFAULT_ENGINE,
|
| 151 |
+
self.config.sanitizer,
|
| 152 |
+
self.target_path):
|
| 153 |
+
engine_impl = clusterfuzz.fuzz.get_engine(config_utils.DEFAULT_ENGINE)
|
| 154 |
+
result = engine_impl.minimize_corpus(self.target_path, [],
|
| 155 |
+
[self.latest_corpus_path],
|
| 156 |
+
self.pruned_corpus_path,
|
| 157 |
+
self._target_artifact_path(),
|
| 158 |
+
self.duration)
|
| 159 |
+
|
| 160 |
+
print(result.logs)
|
| 161 |
+
return FuzzResult(None, result.logs, self.pruned_corpus_path)
|
| 162 |
+
|
| 163 |
+
def fuzz(self, batch=False) -> Optional[FuzzResult]:
|
| 164 |
+
"""Starts the fuzz target run for the length of time specified by duration.
|
| 165 |
+
|
| 166 |
+
Returns:
|
| 167 |
+
FuzzResult namedtuple with stacktrace and testcase if applicable.
|
| 168 |
+
"""
|
| 169 |
+
logging.info('Running fuzzer: %s.', self.target_name)
|
| 170 |
+
|
| 171 |
+
self._download_corpus()
|
| 172 |
+
corpus_path = self.latest_corpus_path
|
| 173 |
+
|
| 174 |
+
logging.info('Starting fuzzing')
|
| 175 |
+
with tempfile.TemporaryDirectory() as artifacts_dir:
|
| 176 |
+
with clusterfuzz.environment.Environment(config_utils.DEFAULT_ENGINE,
|
| 177 |
+
self.config.sanitizer,
|
| 178 |
+
self.target_path) as env:
|
| 179 |
+
engine_impl = clusterfuzz.fuzz.get_engine(config_utils.DEFAULT_ENGINE)
|
| 180 |
+
options = engine_impl.prepare(corpus_path, env.target_path,
|
| 181 |
+
env.build_dir)
|
| 182 |
+
options.merge_back_new_testcases = False
|
| 183 |
+
options.analyze_dictionary = False
|
| 184 |
+
if batch:
|
| 185 |
+
options.arguments.extend(LIBFUZZER_OPTIONS_BATCH)
|
| 186 |
+
else:
|
| 187 |
+
options.arguments.extend(LIBFUZZER_OPTIONS_CODE_CHANGE)
|
| 188 |
+
|
| 189 |
+
if not self.config.report_ooms:
|
| 190 |
+
options.arguments.extend(LIBFUZZER_OPTIONS_NO_REPORT_OOM)
|
| 191 |
+
|
| 192 |
+
if self.config.parallel_fuzzing:
|
| 193 |
+
if self.config.sanitizer == 'memory':
|
| 194 |
+
# TODO(https://github.com/google/oss-fuzz/issues/11915): Don't gate
|
| 195 |
+
# this after jobs is fixed for MSAN.
|
| 196 |
+
logging.info('Not using jobs because it breaks MSAN.')
|
| 197 |
+
else:
|
| 198 |
+
options.arguments.extend(get_libfuzzer_parallel_options())
|
| 199 |
+
|
| 200 |
+
result = engine_impl.fuzz(self.target_path, options, artifacts_dir,
|
| 201 |
+
self.duration)
|
| 202 |
+
print(f'Fuzzing logs:\n{result.logs}')
|
| 203 |
+
|
| 204 |
+
if not result.crashes:
|
| 205 |
+
# Libfuzzer max time was reached.
|
| 206 |
+
logging.info('Fuzzer %s finished with no crashes discovered.',
|
| 207 |
+
self.target_name)
|
| 208 |
+
return FuzzResult(None, None, self.latest_corpus_path)
|
| 209 |
+
|
| 210 |
+
if result.timed_out:
|
| 211 |
+
logging.info('Not reporting crash in %s because process timed out.',
|
| 212 |
+
self.target_name)
|
| 213 |
+
return FuzzResult(None, None, self.latest_corpus_path)
|
| 214 |
+
|
| 215 |
+
# Only report first crash.
|
| 216 |
+
crash = result.crashes[0]
|
| 217 |
+
logging.info('Fuzzer: %s. Detected bug.', self.target_name)
|
| 218 |
+
|
| 219 |
+
is_reportable = self.is_crash_reportable(crash.input_path,
|
| 220 |
+
crash.reproduce_args,
|
| 221 |
+
batch=batch)
|
| 222 |
+
if is_reportable or self.config.upload_all_crashes:
|
| 223 |
+
logging.info('SAVING CRASH')
|
| 224 |
+
fuzzer_logs = result.logs
|
| 225 |
+
testcase_path = self._save_crash(crash)
|
| 226 |
+
if is_reportable and self.config.minimize_crashes:
|
| 227 |
+
# TODO(metzman): We don't want to minimize unreproducible crashes.
|
| 228 |
+
# Use is_reportable to decide this even though reportable crashes
|
| 229 |
+
# are a subset of reproducible ones.
|
| 230 |
+
self.minimize_testcase(testcase_path)
|
| 231 |
+
else:
|
| 232 |
+
logging.info('NOT MINIMIZED')
|
| 233 |
+
else:
|
| 234 |
+
fuzzer_logs = None
|
| 235 |
+
testcase_path = None
|
| 236 |
+
|
| 237 |
+
return FuzzResult(testcase_path, fuzzer_logs, self.latest_corpus_path)
|
| 238 |
+
|
| 239 |
+
def minimize_testcase(self, testcase_path):
|
| 240 |
+
"""Minimizes the testcase located at |testcase_path|."""
|
| 241 |
+
with clusterfuzz.environment.Environment(config_utils.DEFAULT_ENGINE,
|
| 242 |
+
self.config.sanitizer,
|
| 243 |
+
self.target_path):
|
| 244 |
+
engine_impl = clusterfuzz.fuzz.get_engine(config_utils.DEFAULT_ENGINE)
|
| 245 |
+
minimized_testcase_path = testcase_path + '-minimized'
|
| 246 |
+
return engine_impl.minimize_testcase(self.target_path, [],
|
| 247 |
+
testcase_path,
|
| 248 |
+
minimized_testcase_path,
|
| 249 |
+
max_time=MINIMIZE_TIME_SECONDS)
|
| 250 |
+
|
| 251 |
+
def free_disk_if_needed(self, delete_fuzz_target=True):
|
| 252 |
+
"""Deletes things that are no longer needed from fuzzing this fuzz target to
|
| 253 |
+
save disk space if needed."""
|
| 254 |
+
if not self.config.low_disk_space:
|
| 255 |
+
logging.info('Not freeing disk space after running fuzz target.')
|
| 256 |
+
return
|
| 257 |
+
logging.info('Deleting corpus and seed corpus of %s to save disk.',
|
| 258 |
+
self.target_name)
|
| 259 |
+
|
| 260 |
+
# Delete the seed corpus, corpus, and fuzz target.
|
| 261 |
+
for corpus_path in [self.latest_corpus_path, self.pruned_corpus_path]:
|
| 262 |
+
# Use ignore_errors=True to fix
|
| 263 |
+
# https://github.com/google/oss-fuzz/issues/5383.
|
| 264 |
+
shutil.rmtree(corpus_path, ignore_errors=True)
|
| 265 |
+
|
| 266 |
+
target_seed_corpus_path = self.target_path + '_seed_corpus.zip'
|
| 267 |
+
if os.path.exists(target_seed_corpus_path):
|
| 268 |
+
os.remove(target_seed_corpus_path)
|
| 269 |
+
|
| 270 |
+
if delete_fuzz_target:
|
| 271 |
+
logging.info('Deleting fuzz target: %s.', self.target_name)
|
| 272 |
+
os.remove(self.target_path)
|
| 273 |
+
logging.info('Done deleting.')
|
| 274 |
+
|
| 275 |
+
def is_reproducible(self, testcase, target_path, reproduce_args):
|
| 276 |
+
"""Checks if the testcase reproduces.
|
| 277 |
+
|
| 278 |
+
Args:
|
| 279 |
+
testcase: The path to the testcase to be tested.
|
| 280 |
+
target_path: The path to the fuzz target to be tested
|
| 281 |
+
reproduce_args: The arguments to pass to the target to reproduce the
|
| 282 |
+
crash.
|
| 283 |
+
|
| 284 |
+
Returns:
|
| 285 |
+
True if crash is reproducible and we were able to run the
|
| 286 |
+
binary.
|
| 287 |
+
|
| 288 |
+
Raises:
|
| 289 |
+
ReproduceError if we can't attempt to reproduce the crash.
|
| 290 |
+
"""
|
| 291 |
+
if not os.path.exists(target_path):
|
| 292 |
+
logging.info('Target: %s does not exist.', target_path)
|
| 293 |
+
raise ReproduceError(f'Target {target_path} not found.')
|
| 294 |
+
|
| 295 |
+
os.chmod(target_path, stat.S_IRWXO)
|
| 296 |
+
|
| 297 |
+
logging.info('Trying to reproduce crash using: %s.', testcase)
|
| 298 |
+
with clusterfuzz.environment.Environment(config_utils.DEFAULT_ENGINE,
|
| 299 |
+
self.config.sanitizer,
|
| 300 |
+
target_path):
|
| 301 |
+
reproduce_time_seconds = PER_LANGUAGE_REPRODUCE_TIMEOUTS.get(
|
| 302 |
+
self.config.language, DEFAULT_REPRODUCE_TIME_SECONDS)
|
| 303 |
+
for _ in range(REPRODUCE_ATTEMPTS):
|
| 304 |
+
engine_impl = clusterfuzz.fuzz.get_engine(config_utils.DEFAULT_ENGINE)
|
| 305 |
+
try:
|
| 306 |
+
result = engine_impl.reproduce(target_path,
|
| 307 |
+
testcase,
|
| 308 |
+
arguments=reproduce_args,
|
| 309 |
+
max_time=reproduce_time_seconds)
|
| 310 |
+
except TimeoutError as error:
|
| 311 |
+
logging.error('%s.', error)
|
| 312 |
+
return False
|
| 313 |
+
|
| 314 |
+
if result.return_code != 0:
|
| 315 |
+
logging.info('Reproduce command returned: %s. Reproducible on %s.',
|
| 316 |
+
result.return_code, target_path)
|
| 317 |
+
|
| 318 |
+
return True
|
| 319 |
+
|
| 320 |
+
logging.info('Reproduce command returned: 0. Not reproducible on %s.',
|
| 321 |
+
target_path)
|
| 322 |
+
return False
|
| 323 |
+
|
| 324 |
+
def is_crash_reportable(self, testcase, reproduce_args, batch=False):
|
| 325 |
+
"""Returns True if a crash is reportable. This means the crash is
|
| 326 |
+
reproducible but not reproducible on a build from the ClusterFuzz deployment
|
| 327 |
+
(meaning the crash was introduced by this PR/commit/code change).
|
| 328 |
+
|
| 329 |
+
Args:
|
| 330 |
+
testcase: The path to the testcase that triggered the crash.
|
| 331 |
+
reproduce_args: The arguments to pass to the target to reproduce the
|
| 332 |
+
crash.
|
| 333 |
+
|
| 334 |
+
Returns:
|
| 335 |
+
True if the crash was introduced by the current pull request.
|
| 336 |
+
|
| 337 |
+
Raises:
|
| 338 |
+
ReproduceError if we can't attempt to reproduce the crash on the PR build.
|
| 339 |
+
"""
|
| 340 |
+
|
| 341 |
+
if not self.is_crash_type_reportable(testcase):
|
| 342 |
+
return False
|
| 343 |
+
|
| 344 |
+
if not os.path.exists(testcase):
|
| 345 |
+
raise ReproduceError(f'Testcase {testcase} not found.')
|
| 346 |
+
|
| 347 |
+
try:
|
| 348 |
+
reproducible_on_code_change = self.is_reproducible(
|
| 349 |
+
testcase, self.target_path, reproduce_args)
|
| 350 |
+
except ReproduceError as error:
|
| 351 |
+
logging.error('Could not check for crash reproducibility.'
|
| 352 |
+
'Please file an issue:'
|
| 353 |
+
'https://github.com/google/oss-fuzz/issues/new.')
|
| 354 |
+
raise error
|
| 355 |
+
|
| 356 |
+
if not reproducible_on_code_change:
|
| 357 |
+
logging.info('Crash is not reproducible.')
|
| 358 |
+
return self.config.report_unreproducible_crashes
|
| 359 |
+
|
| 360 |
+
logging.info('Crash is reproducible.')
|
| 361 |
+
if batch:
|
| 362 |
+
# We don't need to check if the crash is novel for batch fuzzing.
|
| 363 |
+
return True
|
| 364 |
+
|
| 365 |
+
return self.is_crash_novel(testcase, reproduce_args)
|
| 366 |
+
|
| 367 |
+
def is_crash_type_reportable(self, testcase):
|
| 368 |
+
"""Returns True if |testcase| is an actual crash. If crash is a timeout or
|
| 369 |
+
OOM then returns True if config says we should report those."""
|
| 370 |
+
# TODO(metzman): Use a less hacky method.
|
| 371 |
+
testcase = os.path.basename(testcase)
|
| 372 |
+
if testcase.startswith('oom-'):
|
| 373 |
+
return self.config.report_ooms
|
| 374 |
+
if testcase.startswith('timeout-'):
|
| 375 |
+
return self.config.report_timeouts
|
| 376 |
+
return True
|
| 377 |
+
|
| 378 |
+
def is_crash_novel(self, testcase, reproduce_args):
|
| 379 |
+
"""Returns whether or not the crash is new. A crash is considered new if it
|
| 380 |
+
can't be reproduced on an older ClusterFuzz build of the target."""
|
| 381 |
+
if not os.path.exists(testcase):
|
| 382 |
+
raise ReproduceError('Testcase %s not found.' % testcase)
|
| 383 |
+
clusterfuzz_build_dir = self.clusterfuzz_deployment.download_latest_build()
|
| 384 |
+
if not clusterfuzz_build_dir:
|
| 385 |
+
# Crash is reproducible on PR build and we can't test on a recent
|
| 386 |
+
# ClusterFuzz/OSS-Fuzz build.
|
| 387 |
+
logging.info(COULD_NOT_TEST_ON_CLUSTERFUZZ_MESSAGE)
|
| 388 |
+
return True
|
| 389 |
+
|
| 390 |
+
clusterfuzz_target_path = os.path.join(clusterfuzz_build_dir,
|
| 391 |
+
self.target_name)
|
| 392 |
+
|
| 393 |
+
try:
|
| 394 |
+
reproducible_on_clusterfuzz_build = self.is_reproducible(
|
| 395 |
+
testcase, clusterfuzz_target_path, reproduce_args)
|
| 396 |
+
except ReproduceError:
|
| 397 |
+
# This happens if the project has ClusterFuzz builds, but the fuzz target
|
| 398 |
+
# is not in it (e.g. because the fuzz target is new).
|
| 399 |
+
logging.info(COULD_NOT_TEST_ON_CLUSTERFUZZ_MESSAGE)
|
| 400 |
+
return True
|
| 401 |
+
|
| 402 |
+
if reproducible_on_clusterfuzz_build:
|
| 403 |
+
logging.info('The crash is reproducible on previous build. '
|
| 404 |
+
'Code change (pr/commit) did not introduce crash.')
|
| 405 |
+
return False
|
| 406 |
+
logging.info('The crash is not reproducible on previous build. '
|
| 407 |
+
'Code change (pr/commit) introduced crash.')
|
| 408 |
+
return True
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/sarif_utils.py
ADDED
|
@@ -0,0 +1,251 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2023 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for outputting SARIF data."""
|
| 15 |
+
import copy
|
| 16 |
+
import json
|
| 17 |
+
import logging
|
| 18 |
+
import os
|
| 19 |
+
|
| 20 |
+
from clusterfuzz import stacktraces
|
| 21 |
+
|
| 22 |
+
SARIF_RULES = [
|
| 23 |
+
{
|
| 24 |
+
'id': 'no-crashes',
|
| 25 |
+
'shortDescription': {
|
| 26 |
+
'text': 'Don\'t crash'
|
| 27 |
+
},
|
| 28 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/416.html',
|
| 29 |
+
'properties': {
|
| 30 |
+
'category': 'Crashes'
|
| 31 |
+
}
|
| 32 |
+
},
|
| 33 |
+
{
|
| 34 |
+
'id': 'heap-use-after-free',
|
| 35 |
+
'shortDescription': {
|
| 36 |
+
'text': 'Use of a heap-object after it has been freed.'
|
| 37 |
+
},
|
| 38 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/416.html',
|
| 39 |
+
'properties': {
|
| 40 |
+
'category': 'Crashes'
|
| 41 |
+
}
|
| 42 |
+
},
|
| 43 |
+
{
|
| 44 |
+
'id': 'heap-buffer-overflow',
|
| 45 |
+
'shortDescription': {
|
| 46 |
+
'text': 'A read or write past the end of a heap buffer.'
|
| 47 |
+
},
|
| 48 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/122.html',
|
| 49 |
+
'properties': {
|
| 50 |
+
'category': 'Crashes'
|
| 51 |
+
}
|
| 52 |
+
},
|
| 53 |
+
{
|
| 54 |
+
'id': 'stack-buffer-overflow',
|
| 55 |
+
'shortDescription': {
|
| 56 |
+
'text': 'A read or write past the end of a stack buffer.'
|
| 57 |
+
},
|
| 58 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/121.html',
|
| 59 |
+
'properties': {
|
| 60 |
+
'category': 'Crashes'
|
| 61 |
+
}
|
| 62 |
+
},
|
| 63 |
+
{
|
| 64 |
+
'id': 'global-buffer-overflow',
|
| 65 |
+
'shortDescription': {
|
| 66 |
+
'text': 'A read or write past the end of a global buffer.'
|
| 67 |
+
},
|
| 68 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/121.html',
|
| 69 |
+
'properties': {
|
| 70 |
+
'category': 'Crashes'
|
| 71 |
+
}
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
'id': 'stack-use-after-return',
|
| 75 |
+
'shortDescription': {
|
| 76 |
+
'text':
|
| 77 |
+
'A stack-based variable has been used after the function returned.'
|
| 78 |
+
},
|
| 79 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/562.html',
|
| 80 |
+
'properties': {
|
| 81 |
+
'category': 'Crashes'
|
| 82 |
+
}
|
| 83 |
+
},
|
| 84 |
+
{
|
| 85 |
+
'id': 'stack-use-after-scope',
|
| 86 |
+
'shortDescription': {
|
| 87 |
+
'text':
|
| 88 |
+
'A stack-based variable has been used outside of the scope in which it exists.'
|
| 89 |
+
},
|
| 90 |
+
'helpUri': 'https://cwe.mitre.org/data/definitions/562.html',
|
| 91 |
+
'properties': {
|
| 92 |
+
'category': 'Crashes'
|
| 93 |
+
}
|
| 94 |
+
},
|
| 95 |
+
{
|
| 96 |
+
'id': 'initialization-order-fiasco',
|
| 97 |
+
'shortDescription': {
|
| 98 |
+
'text': 'Problem with order of initialization of global objects.'
|
| 99 |
+
},
|
| 100 |
+
'helpUri': 'https://isocpp.org/wiki/faq/ctors#static-init-order',
|
| 101 |
+
'properties': {
|
| 102 |
+
'category': 'Crashes'
|
| 103 |
+
}
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
'id':
|
| 107 |
+
'direct-leak',
|
| 108 |
+
'shortDescription': {
|
| 109 |
+
'text': 'Memory is leaked.'
|
| 110 |
+
},
|
| 111 |
+
'helpUri':
|
| 112 |
+
'https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer',
|
| 113 |
+
'properties': {
|
| 114 |
+
'category': 'Crashes'
|
| 115 |
+
}
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
'id':
|
| 119 |
+
'indirect-leak',
|
| 120 |
+
'shortDescription': {
|
| 121 |
+
'text': 'Memory is leaked.'
|
| 122 |
+
},
|
| 123 |
+
'helpUri':
|
| 124 |
+
'https://github.com/google/sanitizers/wiki/AddressSanitizerLeakSanitizer',
|
| 125 |
+
'properties': {
|
| 126 |
+
'category': 'Crashes'
|
| 127 |
+
}
|
| 128 |
+
},
|
| 129 |
+
]
|
| 130 |
+
SARIF_DATA = {
|
| 131 |
+
'version':
|
| 132 |
+
'2.1.0',
|
| 133 |
+
'$schema':
|
| 134 |
+
'http://json.schemastore.org/sarif-2.1.0-rtm.4',
|
| 135 |
+
'runs': [{
|
| 136 |
+
'tool': {
|
| 137 |
+
'driver': {
|
| 138 |
+
'name': 'ClusterFuzzLite/CIFuzz',
|
| 139 |
+
'informationUri': 'https://google.github.io/clusterfuzzlite/',
|
| 140 |
+
'rules': SARIF_RULES,
|
| 141 |
+
}
|
| 142 |
+
},
|
| 143 |
+
'results': []
|
| 144 |
+
}]
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
SRC_ROOT = '/src/'
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
def redact_src_path(src_path):
|
| 151 |
+
"""Redact the src path so that it can be reported to users."""
|
| 152 |
+
src_path = os.path.normpath(src_path)
|
| 153 |
+
if src_path.startswith(SRC_ROOT):
|
| 154 |
+
src_path = src_path[len(SRC_ROOT):]
|
| 155 |
+
|
| 156 |
+
src_path = os.sep.join(src_path.split(os.sep)[1:])
|
| 157 |
+
return src_path
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def get_error_frame(crash_info):
|
| 161 |
+
"""Returns the stackframe where the error occurred."""
|
| 162 |
+
if not crash_info.crash_state:
|
| 163 |
+
return None
|
| 164 |
+
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]])
|
| 167 |
+
|
| 168 |
+
for crash_frames in crash_info.frames:
|
| 169 |
+
for frame in crash_frames:
|
| 170 |
+
# TODO(metzman): Do something less fragile here.
|
| 171 |
+
if frame.function_name is None:
|
| 172 |
+
continue
|
| 173 |
+
if state in frame.function_name:
|
| 174 |
+
return frame
|
| 175 |
+
return None
|
| 176 |
+
|
| 177 |
+
|
| 178 |
+
def get_error_source_info(crash_info):
|
| 179 |
+
"""Returns the filename and the line where the bug occurred."""
|
| 180 |
+
frame = get_error_frame(crash_info)
|
| 181 |
+
if not frame:
|
| 182 |
+
return (None, 1)
|
| 183 |
+
try:
|
| 184 |
+
return redact_src_path(frame.filename), int(frame.fileline or 1)
|
| 185 |
+
except TypeError:
|
| 186 |
+
return (None, 1)
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def get_rule_index(crash_type):
|
| 190 |
+
"""Returns the rule index describe the rule that |crash_type| ran afoul of."""
|
| 191 |
+
# Don't include "READ" or "WRITE" or number of bytes.
|
| 192 |
+
crash_type = crash_type.replace('\n', ' ').split(' ')[0].lower()
|
| 193 |
+
logging.info('crash_type: %s.', crash_type)
|
| 194 |
+
for idx, rule in enumerate(SARIF_RULES):
|
| 195 |
+
if rule['id'] == crash_type:
|
| 196 |
+
logging.info('Rule index: %d.', idx)
|
| 197 |
+
return idx
|
| 198 |
+
|
| 199 |
+
return get_rule_index('no-crashes')
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def get_sarif_data(stacktrace, target_path):
|
| 203 |
+
"""Returns a description of the crash in SARIF."""
|
| 204 |
+
data = copy.deepcopy(SARIF_DATA)
|
| 205 |
+
if stacktrace is None:
|
| 206 |
+
return data
|
| 207 |
+
|
| 208 |
+
fuzz_target = os.path.basename(target_path)
|
| 209 |
+
stack_parser = stacktraces.StackParser(fuzz_target=fuzz_target,
|
| 210 |
+
symbolized=True,
|
| 211 |
+
detect_ooms_and_hangs=True,
|
| 212 |
+
include_ubsan=True)
|
| 213 |
+
crash_info = stack_parser.parse(stacktrace)
|
| 214 |
+
error_source_info = get_error_source_info(crash_info)
|
| 215 |
+
rule_idx = get_rule_index(crash_info.crash_type)
|
| 216 |
+
rule_id = SARIF_RULES[rule_idx]['id']
|
| 217 |
+
uri = error_source_info[0]
|
| 218 |
+
|
| 219 |
+
result = {
|
| 220 |
+
'level': 'error',
|
| 221 |
+
'message': {
|
| 222 |
+
'text': crash_info.crash_type
|
| 223 |
+
},
|
| 224 |
+
'locations': [{
|
| 225 |
+
'physicalLocation': {
|
| 226 |
+
'artifactLocation': {
|
| 227 |
+
'uri': uri,
|
| 228 |
+
'index': 0
|
| 229 |
+
},
|
| 230 |
+
'region': {
|
| 231 |
+
'startLine': error_source_info[1],
|
| 232 |
+
# We don't have this granualarity fuzzing.
|
| 233 |
+
'startColumn': 1,
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
}],
|
| 237 |
+
'ruleId': rule_id,
|
| 238 |
+
'ruleIndex': rule_idx
|
| 239 |
+
}
|
| 240 |
+
if uri:
|
| 241 |
+
data['runs'][0]['results'].append(result)
|
| 242 |
+
return data
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
def write_stacktrace_to_sarif(stacktrace, target_path, workspace):
|
| 246 |
+
"""Writes a description of the crash in stacktrace to a SARIF file."""
|
| 247 |
+
data = get_sarif_data(stacktrace, target_path)
|
| 248 |
+
if not os.path.exists(workspace.sarif):
|
| 249 |
+
os.makedirs(workspace.sarif)
|
| 250 |
+
with open(os.path.join(workspace.sarif, 'results.sarif'), 'w') as file_handle:
|
| 251 |
+
file_handle.write(json.dumps(data))
|
local-test-commons-compress-delta-02/fuzz-tooling/infra/cifuzz/workspace_utils.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright 2021 Google LLC
|
| 2 |
+
#
|
| 3 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 4 |
+
# you may not use this file except in compliance with the License.
|
| 5 |
+
# You may obtain a copy of the License at
|
| 6 |
+
#
|
| 7 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 8 |
+
#
|
| 9 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 10 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 11 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 12 |
+
# See the License for the specific language governing permissions and
|
| 13 |
+
# limitations under the License.
|
| 14 |
+
"""Module for representing the workspace directory which CIFuzz uses."""
|
| 15 |
+
|
| 16 |
+
import os
|
| 17 |
+
import shutil
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
class Workspace:
|
| 21 |
+
"""Class representing the workspace directory."""
|
| 22 |
+
|
| 23 |
+
def __init__(self, config):
|
| 24 |
+
self.workspace = config.workspace
|
| 25 |
+
|
| 26 |
+
def initialize_dir(self, directory): # pylint: disable=no-self-use
|
| 27 |
+
"""Creates directory if it doesn't already exist, otherwise does nothing."""
|
| 28 |
+
os.makedirs(directory, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
@property
|
| 31 |
+
def repo_storage(self):
|
| 32 |
+
"""The parent directory for repo storage."""
|
| 33 |
+
return os.path.join(self.workspace, 'storage')
|
| 34 |
+
|
| 35 |
+
@property
|
| 36 |
+
def out(self):
|
| 37 |
+
"""The out directory used for storing the fuzzer build built by
|
| 38 |
+
build_fuzzers."""
|
| 39 |
+
# Don't use 'out' because it needs to be used by artifacts.
|
| 40 |
+
return os.path.join(self.workspace, 'build-out')
|
| 41 |
+
|
| 42 |
+
@property
|
| 43 |
+
def work(self):
|
| 44 |
+
"""The directory used as the work directory for the fuzzer build/run."""
|
| 45 |
+
return os.path.join(self.workspace, 'work')
|
| 46 |
+
|
| 47 |
+
@property
|
| 48 |
+
def artifacts(self):
|
| 49 |
+
"""The directory used to store artifacts for download by CI-system users."""
|
| 50 |
+
# This is hardcoded by a lot of clients, so we need to use this.
|
| 51 |
+
return os.path.join(self.workspace, 'out', 'artifacts')
|
| 52 |
+
|
| 53 |
+
@property
|
| 54 |
+
def clusterfuzz_build(self):
|
| 55 |
+
"""The directory where builds from ClusterFuzz are stored."""
|
| 56 |
+
return os.path.join(self.workspace, 'cifuzz-prev-build')
|
| 57 |
+
|
| 58 |
+
@property
|
| 59 |
+
def clusterfuzz_coverage(self):
|
| 60 |
+
"""The directory where builds from ClusterFuzz are stored."""
|
| 61 |
+
return os.path.join(self.workspace, 'cifuzz-prev-coverage')
|
| 62 |
+
|
| 63 |
+
@property
|
| 64 |
+
def coverage_report(self):
|
| 65 |
+
"""The directory where coverage reports generated by cifuzz are put."""
|
| 66 |
+
return os.path.join(self.workspace, 'cifuzz-coverage')
|
| 67 |
+
|
| 68 |
+
@property
|
| 69 |
+
def corpora(self):
|
| 70 |
+
"""The directory where corpora from ClusterFuzz are stored."""
|
| 71 |
+
return os.path.join(self.workspace, 'cifuzz-corpus')
|
| 72 |
+
|
| 73 |
+
@property
|
| 74 |
+
def pruned_corpora(self):
|
| 75 |
+
"""The directory where pruned corpora are stored."""
|
| 76 |
+
return os.path.join(self.workspace, 'cifuzz-pruned-corpus')
|
| 77 |
+
|
| 78 |
+
@property
|
| 79 |
+
def sarif(self):
|
| 80 |
+
"""The directory where sarif files are stored."""
|
| 81 |
+
return os.path.join(self.workspace, 'cifuzz-sarif')
|
| 82 |
+
|
| 83 |
+
def make_repo_for_sarif(self, repo_manager):
|
| 84 |
+
"""Copies the repo over for the sarif upload GitHub action."""
|
| 85 |
+
return shutil.copytree(repo_manager.repo_dir, self.sarif, symlinks=True)
|