• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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."""
15import logging
16import os
17import sys
18
19import build_fuzzers
20import config_utils
21
22# pylint: disable=c-extension-no-member
23# pylint gets confused because of the relative import of cifuzz.
24
25# TODO: Turn default logging to INFO when CIFuzz is stable
26logging.basicConfig(
27    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
28    level=logging.DEBUG)
29
30
31def main():
32  """Build OSS-Fuzz project's fuzzers for CI tools.
33  This script is used to kick off the Github Actions CI tool. It is the
34  entrypoint of the Dockerfile in this directory. This action can be added to
35  any OSS-Fuzz project's workflow that uses Github.
36
37  Note: The resulting clusterfuzz binaries of this build are placed in
38  the directory: ${GITHUB_WORKSPACE}/out
39
40  Required environment variables:
41    OSS_FUZZ_PROJECT_NAME: The name of OSS-Fuzz project.
42    GITHUB_REPOSITORY: The name of the Github repo that called this script.
43    GITHUB_SHA: The commit SHA that triggered this script.
44    GITHUB_EVENT_NAME: The name of the hook event that triggered this script.
45    GITHUB_EVENT_PATH:
46      The path to the file containing the POST payload of the webhook:
47      https://help.github.com/en/actions/reference/virtual-environments-for-github-hosted-runners#filesystems-on-github-hosted-runners
48    GITHUB_WORKSPACE: The shared volume directory where input artifacts are.
49    DRY_RUN: If true, no failures will surface.
50    SANITIZER: The sanitizer to use when running fuzzers.
51
52  Returns:
53    0 on success or 1 on failure.
54  """
55  config = config_utils.BuildFuzzersConfig()
56
57  if config.dry_run:
58    # Sets the default return code on error to success.
59    returncode = 0
60  else:
61    # The default return code when an error occurs.
62    returncode = 1
63
64  if not config.workspace:
65    logging.error('This script needs to be run within Github actions.')
66    return returncode
67
68  if not build_fuzzers.build_fuzzers(config):
69    logging.error(
70        'Error building fuzzers for project %s (commit: %s, pr_ref: %s).',
71        config.project_name, config.commit_sha, config.pr_ref)
72    return returncode
73
74  out_dir = os.path.join(config.workspace, 'out')
75
76  if not config.bad_build_check:
77    # If we've gotten to this point and we don't need to do bad_build_check,
78    # then the build has succeeded.
79    returncode = 0
80  # yapf: disable
81  elif build_fuzzers.check_fuzzer_build(
82      out_dir,
83      config.sanitizer,
84      config.language,
85      allowed_broken_targets_percentage=config.allowed_broken_targets_percentage
86  ):
87    # yapf: enable
88    returncode = 0
89
90  return returncode
91
92
93if __name__ == '__main__':
94  sys.exit(main())
95