• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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"""Script for running CIFuzz end-to-end. This is meant to work outside any
15docker image. This cannot depend on any CIFuzz code or third party packages."""
16import os
17import subprocess
18import sys
19import tempfile
20import logging
21
22INFRA_DIR = os.path.dirname(os.path.dirname(__file__))
23DEFAULT_ENVS = [('DRY_RUN', '0'), ('SANITIZER', 'address')]
24BASE_CIFUZZ_DOCKER_TAG = 'gcr.io/oss-fuzz-base'
25
26
27def set_default_env_var_if_unset(env_var, default_value):
28  """Sets the value of |env_var| in the environment to |default_value| if it was
29  not already set."""
30  if env_var not in os.environ:
31    os.environ[env_var] = default_value
32
33
34def docker_run(name, workspace, project_src_path):
35  """Runs a CIFuzz docker container with |name|."""
36  command = [
37      'docker', 'run', '--name', name, '--rm', '-e', 'PROJECT_SRC_PATH', '-e',
38      'OSS_FUZZ_PROJECT_NAME', '-e', 'WORKSPACE', '-e', 'REPOSITORY', '-e',
39      'DRY_RUN', '-e', 'CI', '-e', 'SANITIZER', '-e', 'GIT_SHA'
40  ]
41  if project_src_path:
42    command += ['-v', f'{project_src_path}:{project_src_path}']
43  command += [
44      '-v', '/var/run/docker.sock:/var/run/docker.sock', '-v',
45      f'{workspace}:{workspace}', f'{BASE_CIFUZZ_DOCKER_TAG}/{name}'
46  ]
47  print('Running docker command:', command)
48  subprocess.run(command, check=True)
49
50
51def docker_build(image):
52  """Builds the CIFuzz |image|. Only suitable for building CIFuzz images."""
53  command = [
54      'docker', 'build', '-t', f'{BASE_CIFUZZ_DOCKER_TAG}/{image}', '--file',
55      f'{image}.Dockerfile', '.'
56  ]
57  subprocess.run(command, check=True, cwd=INFRA_DIR)
58
59
60def main():
61  """Builds and runs fuzzers using CIFuzz."""
62  for env_var, default_value in DEFAULT_ENVS:
63    set_default_env_var_if_unset(env_var, default_value)
64
65  repository = os.getenv('REPOSITORY')
66  assert repository
67
68  project_src_path = os.getenv('PROJECT_SRC_PATH')
69
70  with tempfile.TemporaryDirectory() as temp_dir:
71    if 'WORKSPACE' not in os.environ:
72      os.environ['WORKSPACE'] = temp_dir
73
74    workspace = os.environ['WORKSPACE']
75
76    docker_build('build_fuzzers')
77    docker_run('build_fuzzers', workspace, project_src_path)
78    docker_build('run_fuzzers')
79    try:
80      docker_run('run_fuzzers', workspace, project_src_path)
81    except subprocess.CalledProcessError:
82      logging.error('run_fuzzers failed.')
83      return 1
84    return 0
85
86
87if __name__ == '__main__':
88  sys.exit(main())
89