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