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 generating coverage reports.""" 15import os 16 17import base_runner_utils 18import fuzz_target 19import utils 20 21 22def run_coverage_command(config, workspace): 23 """Runs the coverage command in base-runner to generate a coverage report.""" 24 env = base_runner_utils.get_env(config, workspace) 25 env['HTTP_PORT'] = '' 26 env['COVERAGE_EXTRA_ARGS'] = '' 27 env['CORPUS_DIR'] = workspace.corpora 28 env['COVERAGE_OUTPUT_DIR'] = workspace.coverage_report 29 command = 'coverage' 30 return utils.execute(command, env=env) 31 32 33def download_corpora(fuzz_target_paths, clusterfuzz_deployment): 34 """Downloads corpora for fuzz targets in |fuzz_target_paths| using 35 |clusterfuzz_deployment| to download corpora from ClusterFuzz/OSS-Fuzz.""" 36 for target_path in fuzz_target_paths: 37 target_name = os.path.basename(target_path) 38 corpus_dir = fuzz_target.get_fuzz_target_corpus_dir( 39 clusterfuzz_deployment.workspace, target_name) 40 clusterfuzz_deployment.download_corpus(target_name, corpus_dir) 41 42 43def generate_coverage_report(fuzz_target_paths, workspace, 44 clusterfuzz_deployment, config): 45 """Generates a coverage report using Clang's source based coverage.""" 46 download_corpora(fuzz_target_paths, clusterfuzz_deployment) 47 run_coverage_command(config, workspace) 48 clusterfuzz_deployment.upload_coverage() 49