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 fuzz targets affected by the change-under-test 15(CUT).""" 16import logging 17import os 18import sys 19 20# pylint: disable=wrong-import-position,import-error 21sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) 22import utils 23 24 25def remove_unaffected_fuzz_targets(clusterfuzz_deployment, out_dir, 26 files_changed, repo_path): 27 """Removes all non affected fuzz targets in the out directory. 28 29 Args: 30 clusterfuzz_deployment: The ClusterFuzz deployment object. 31 out_dir: The location of the fuzz target binaries. 32 files_changed: A list of files changed compared to HEAD. 33 repo_path: The location of the OSS-Fuzz repo in the docker image. 34 35 This function will not delete fuzz targets unless it knows that the fuzz 36 targets are unaffected. For example, this means that fuzz targets which don't 37 have coverage data on will not be deleted. 38 """ 39 if not files_changed: 40 # Don't remove any fuzz targets if there is no difference from HEAD. 41 logging.info('No files changed compared to HEAD.') 42 return 43 44 logging.info('Files changed in PR: %s', files_changed) 45 46 fuzz_target_paths = utils.get_fuzz_targets(out_dir) 47 if not fuzz_target_paths: 48 # Nothing to remove. 49 logging.error('No fuzz targets found in out dir.') 50 return 51 52 coverage = clusterfuzz_deployment.get_coverage(repo_path) 53 if not coverage: 54 # Don't remove any fuzz targets unless we have data. 55 logging.error('Could not find latest coverage report.') 56 return 57 58 affected_fuzz_targets = get_affected_fuzz_targets(coverage, fuzz_target_paths, 59 files_changed) 60 61 if not affected_fuzz_targets: 62 logging.info('No affected fuzz targets detected, keeping all as fallback.') 63 return 64 65 logging.info('Using affected fuzz targets: %s.', affected_fuzz_targets) 66 unaffected_fuzz_targets = set(fuzz_target_paths) - affected_fuzz_targets 67 logging.info('Removing unaffected fuzz targets: %s.', unaffected_fuzz_targets) 68 69 # Remove all the targets that are not affected. 70 for fuzz_target_path in unaffected_fuzz_targets: 71 try: 72 os.remove(fuzz_target_path) 73 except OSError as error: 74 logging.error('%s occurred while removing file %s', error, 75 fuzz_target_path) 76 77 78def is_fuzz_target_affected(coverage, fuzz_target_path, files_changed): 79 """Returns True if a fuzz target (|fuzz_target_path|) is affected by 80 |files_changed|.""" 81 fuzz_target = os.path.basename(fuzz_target_path) 82 covered_files = coverage.get_files_covered_by_target(fuzz_target) 83 if not covered_files: 84 # Assume a fuzz target is affected if we can't get its coverage from 85 # OSS-Fuzz. 86 # TODO(metzman): Figure out what we should do if covered_files is []. 87 # Should we act as if we couldn't get the coverage? 88 logging.info('Could not get coverage for %s. Treating as affected.', 89 fuzz_target) 90 return True 91 92 logging.info('Fuzz target %s is affected by: %s', fuzz_target, covered_files) 93 for filename in files_changed: 94 if filename in covered_files: 95 logging.info('Fuzz target %s is affected by changed file: %s', 96 fuzz_target, filename) 97 return True 98 99 logging.info('Fuzz target %s is not affected.', fuzz_target) 100 return False 101 102 103def get_affected_fuzz_targets(coverage, fuzz_target_paths, files_changed): 104 """Returns a list of paths of affected targets.""" 105 affected_fuzz_targets = set() 106 for fuzz_target_path in fuzz_target_paths: 107 if is_fuzz_target_affected(coverage, fuzz_target_path, files_changed): 108 affected_fuzz_targets.add(fuzz_target_path) 109 110 return affected_fuzz_targets 111