2021-07-27 19:11:27 +00:00
|
|
|
# Copyright 2021 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
"""Script for running CIFuzz end-to-end. This is meant to work outside any
|
|
|
|
docker image. This cannot depend on any CIFuzz code or third party packages."""
|
|
|
|
import os
|
|
|
|
import subprocess
|
2021-08-03 22:40:31 +00:00
|
|
|
import sys
|
2021-07-27 19:11:27 +00:00
|
|
|
import tempfile
|
|
|
|
import logging
|
|
|
|
|
|
|
|
INFRA_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
|
|
DEFAULT_ENVS = [('DRY_RUN', '0'), ('SANITIZER', 'address')]
|
2021-07-28 14:11:51 +00:00
|
|
|
BASE_CIFUZZ_DOCKER_TAG = 'gcr.io/oss-fuzz-base'
|
2021-07-27 19:11:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
def set_default_env_var_if_unset(env_var, default_value):
|
|
|
|
"""Sets the value of |env_var| in the environment to |default_value| if it was
|
|
|
|
not already set."""
|
|
|
|
if env_var not in os.environ:
|
|
|
|
os.environ[env_var] = default_value
|
|
|
|
|
|
|
|
|
2021-08-04 01:13:59 +00:00
|
|
|
def docker_run(name, workspace, project_src_path):
|
2021-07-27 19:11:27 +00:00
|
|
|
"""Runs a CIFuzz docker container with |name|."""
|
|
|
|
command = [
|
|
|
|
'docker', 'run', '--name', name, '--rm', '-e', 'PROJECT_SRC_PATH', '-e',
|
2021-08-04 23:13:51 +00:00
|
|
|
'OSS_FUZZ_PROJECT_NAME', '-e', 'WORKSPACE', '-e', 'REPOSITORY', '-e',
|
|
|
|
'DRY_RUN', '-e', 'CI', '-e', 'SANITIZER', '-e', 'GIT_SHA'
|
2021-07-27 19:11:27 +00:00
|
|
|
]
|
|
|
|
if project_src_path:
|
|
|
|
command += ['-v', f'{project_src_path}:{project_src_path}']
|
|
|
|
command += [
|
|
|
|
'-v', '/var/run/docker.sock:/var/run/docker.sock', '-v',
|
2021-08-04 01:13:59 +00:00
|
|
|
f'{workspace}:{workspace}', f'{BASE_CIFUZZ_DOCKER_TAG}/{name}'
|
2021-07-27 19:11:27 +00:00
|
|
|
]
|
|
|
|
print('Running docker command:', command)
|
|
|
|
subprocess.run(command, check=True)
|
|
|
|
|
|
|
|
|
|
|
|
def docker_build(image):
|
|
|
|
"""Builds the CIFuzz |image|. Only suitable for building CIFuzz images."""
|
|
|
|
command = [
|
2021-07-28 14:11:51 +00:00
|
|
|
'docker', 'build', '-t', f'{BASE_CIFUZZ_DOCKER_TAG}/{image}', '--file',
|
2021-07-27 19:11:27 +00:00
|
|
|
f'{image}.Dockerfile', '.'
|
|
|
|
]
|
|
|
|
subprocess.run(command, check=True, cwd=INFRA_DIR)
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Builds and runs fuzzers using CIFuzz."""
|
|
|
|
for env_var, default_value in DEFAULT_ENVS:
|
|
|
|
set_default_env_var_if_unset(env_var, default_value)
|
|
|
|
|
2021-08-04 01:13:59 +00:00
|
|
|
repository = os.getenv('REPOSITORY')
|
|
|
|
assert repository
|
2021-07-27 19:11:27 +00:00
|
|
|
|
|
|
|
project_src_path = os.getenv('PROJECT_SRC_PATH')
|
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
2021-08-04 01:13:59 +00:00
|
|
|
if 'WORKSPACE' not in os.environ:
|
|
|
|
os.environ['WORKSPACE'] = temp_dir
|
2021-07-27 19:11:27 +00:00
|
|
|
|
2021-08-04 01:13:59 +00:00
|
|
|
workspace = os.environ['WORKSPACE']
|
2021-07-27 19:11:27 +00:00
|
|
|
|
|
|
|
docker_build('build_fuzzers')
|
2021-08-04 01:13:59 +00:00
|
|
|
docker_run('build_fuzzers', workspace, project_src_path)
|
2021-07-27 19:11:27 +00:00
|
|
|
docker_build('run_fuzzers')
|
|
|
|
try:
|
2021-08-04 01:13:59 +00:00
|
|
|
docker_run('run_fuzzers', workspace, project_src_path)
|
2021-07-27 19:11:27 +00:00
|
|
|
except subprocess.CalledProcessError:
|
2021-07-28 14:11:51 +00:00
|
|
|
logging.error('run_fuzzers failed.')
|
2021-08-03 22:40:31 +00:00
|
|
|
return 1
|
|
|
|
return 0
|
2021-07-27 19:11:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-08-03 22:40:31 +00:00
|
|
|
sys.exit(main())
|