• 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 and runs specific OSS-Fuzz project's fuzzers for CI tools."""
15import logging
16import os
17import sys
18
19# pylint: disable=wrong-import-position
20sys.path.append(os.path.join(os.environ['OSS_FUZZ_ROOT'], 'infra', 'cifuzz'))
21import cifuzz
22
23# TODO: Turn default logging to INFO when CIFuzz is stable
24logging.basicConfig(
25    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
26    level=logging.DEBUG)
27
28
29def main():
30  """Runs OSS-Fuzz project's fuzzers for CI tools.
31  This script is used to kick off the Github Actions CI tool. It is the
32  entrypoint  of the Dockerfile in this directory. This action can be added to
33  any OSS-Fuzz project's workflow that uses Github.
34
35  Required environment variables:
36    PROJECT_NAME: The name of OSS-Fuzz project.
37    FUZZ_TIME: The length of time in seconds that fuzzers are to be run.
38    GITHUB_REPOSITORY: The name of the Github repo that called this script.
39    GITHUB_SHA: The commit SHA that triggered this script.
40    GITHUB_REF: The pull request reference that triggered this script.
41    GITHUB_EVENT_NAME: The name of the hook event that triggered this script.
42
43  Returns:
44    0 on success or 1 on Failure.
45  """
46  oss_fuzz_project_name = os.environ.get('PROJECT_NAME')
47  fuzz_seconds = int(os.environ.get('FUZZ_SECONDS', 360))
48  github_repo_name = os.path.basename(os.environ.get('GITHUB_REPOSITORY'))
49  pr_ref = os.environ.get('GITHUB_REF')
50  commit_sha = os.environ.get('GITHUB_SHA')
51  event = os.environ.get('GITHUB_EVENT_NAME')
52
53  # Get the shared volume directory and create required directorys.
54  workspace = os.environ.get('GITHUB_WORKSPACE')
55  if not workspace:
56    logging.error('This script needs to be run in the Github action context.')
57    return 1
58
59  if event == 'push' and not cifuzz.build_fuzzers(
60      oss_fuzz_project_name, github_repo_name, workspace,
61      commit_sha=commit_sha):
62    logging.error('Error building fuzzers for project %s with commit %s.',
63                  oss_fuzz_project_name, commit_sha)
64    return 1
65  if event == 'pull_request' and not cifuzz.build_fuzzers(
66      oss_fuzz_project_name, github_repo_name, workspace, pr_ref=pr_ref):
67    logging.error('Error building fuzzers for project %s with pull request %s.',
68                  oss_fuzz_project_name, pr_ref)
69    return 1
70
71  # Run the specified project's fuzzers from the build.
72  run_status, bug_found = cifuzz.run_fuzzers(oss_fuzz_project_name,
73                                             fuzz_seconds, workspace)
74  if not run_status:
75    logging.error('Error occured while running fuzzers for project %s.',
76                  oss_fuzz_project_name)
77    return 1
78  if bug_found:
79    logging.info('Bug found.')
80    # Return 2 when a bug was found by a fuzzer causing the CI to fail.
81    return 2
82  return 0
83
84
85if __name__ == '__main__':
86  sys.exit(main())
87