2019-05-10 22:58:18 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# Copyright 2019 Google Inc.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
################################################################################
|
|
|
|
"""Build modified projects."""
|
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2020-11-20 16:40:47 +00:00
|
|
|
import enum
|
2019-05-10 22:58:18 +00:00
|
|
|
import os
|
|
|
|
import re
|
2020-02-05 17:10:05 +00:00
|
|
|
import sys
|
2019-05-10 22:58:18 +00:00
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
2021-08-05 00:04:55 +00:00
|
|
|
# pylint: disable=wrong-import-position,import-error
|
|
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
|
|
|
|
import constants
|
|
|
|
|
2020-11-20 16:40:47 +00:00
|
|
|
CANARY_PROJECT = 'skcms'
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
DEFAULT_ARCHITECTURES = ['x86_64']
|
2023-04-20 23:48:45 +00:00
|
|
|
DEFAULT_ENGINES = ['afl', 'honggfuzz', 'libfuzzer', 'centipede']
|
2019-05-13 16:55:06 +00:00
|
|
|
DEFAULT_SANITIZERS = ['address', 'undefined']
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
|
2021-01-28 00:06:04 +00:00
|
|
|
def get_changed_files_output():
|
2020-11-20 16:40:47 +00:00
|
|
|
"""Returns the output of a git command that discovers changed files."""
|
2021-01-28 00:06:04 +00:00
|
|
|
branch_commit_hash = subprocess.check_output(
|
2021-06-21 14:14:41 +00:00
|
|
|
['git', 'merge-base', 'HEAD', 'origin/HEAD']).strip().decode()
|
2021-01-28 00:06:04 +00:00
|
|
|
|
|
|
|
return subprocess.check_output(
|
|
|
|
['git', 'diff', '--name-only', branch_commit_hash + '..']).decode()
|
2020-11-20 16:40:47 +00:00
|
|
|
|
|
|
|
|
2019-07-02 23:11:37 +00:00
|
|
|
def get_modified_buildable_projects():
|
|
|
|
"""Returns a list of all the projects modified in this commit that have a
|
|
|
|
build.sh file."""
|
2021-01-28 00:06:04 +00:00
|
|
|
git_output = get_changed_files_output()
|
2019-05-10 22:58:18 +00:00
|
|
|
projects_regex = '.*projects/(?P<name>.*)/.*\n'
|
2020-11-20 16:40:47 +00:00
|
|
|
modified_projects = set(re.findall(projects_regex, git_output))
|
2019-07-02 23:11:37 +00:00
|
|
|
projects_dir = os.path.join(get_oss_fuzz_root(), 'projects')
|
2020-02-05 17:14:59 +00:00
|
|
|
# Filter out projects without Dockerfile files since new projects and reverted
|
2019-07-02 23:11:37 +00:00
|
|
|
# projects frequently don't have them. In these cases we don't want Travis's
|
|
|
|
# builds to fail.
|
|
|
|
modified_buildable_projects = []
|
|
|
|
for project in modified_projects:
|
2020-02-05 17:14:59 +00:00
|
|
|
if not os.path.exists(os.path.join(projects_dir, project, 'Dockerfile')):
|
|
|
|
print('Project {0} does not have Dockerfile. skipping build.'.format(
|
2019-07-02 23:11:37 +00:00
|
|
|
project))
|
|
|
|
continue
|
|
|
|
modified_buildable_projects.append(project)
|
|
|
|
return modified_buildable_projects
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_oss_fuzz_root():
|
|
|
|
"""Get the absolute path of the root of the oss-fuzz checkout."""
|
|
|
|
script_path = os.path.realpath(__file__)
|
|
|
|
return os.path.abspath(
|
|
|
|
os.path.dirname(os.path.dirname(os.path.dirname(script_path))))
|
|
|
|
|
|
|
|
|
|
|
|
def execute_helper_command(helper_command):
|
|
|
|
"""Execute |helper_command| using helper.py."""
|
|
|
|
root = get_oss_fuzz_root()
|
|
|
|
script_path = os.path.join(root, 'infra', 'helper.py')
|
|
|
|
command = ['python', script_path] + helper_command
|
2019-05-13 16:55:06 +00:00
|
|
|
print('Running command: %s' % ' '.join(command))
|
2019-05-10 22:58:18 +00:00
|
|
|
subprocess.check_call(command)
|
|
|
|
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
def build_fuzzers(project, engine, sanitizer, architecture):
|
2019-05-10 22:58:18 +00:00
|
|
|
"""Execute helper.py's build_fuzzers command on |project|. Build the fuzzers
|
2019-05-15 18:01:53 +00:00
|
|
|
with |engine| and |sanitizer| for |architecture|."""
|
2019-05-13 22:01:25 +00:00
|
|
|
execute_helper_command([
|
|
|
|
'build_fuzzers', project, '--engine', engine, '--sanitizer', sanitizer,
|
|
|
|
'--architecture', architecture
|
|
|
|
])
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
def check_build(project, engine, sanitizer, architecture):
|
2019-05-10 22:58:18 +00:00
|
|
|
"""Execute helper.py's check_build command on |project|, assuming it was most
|
2019-05-15 18:01:53 +00:00
|
|
|
recently built with |engine| and |sanitizer| for |architecture|."""
|
2019-05-14 17:18:02 +00:00
|
|
|
execute_helper_command([
|
|
|
|
'check_build', project, '--engine', engine, '--sanitizer', sanitizer,
|
|
|
|
'--architecture', architecture
|
|
|
|
])
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
|
2020-08-26 18:59:56 +00:00
|
|
|
def should_build_coverage(project_yaml):
|
|
|
|
"""Returns True if a coverage build should be done based on project.yaml
|
|
|
|
contents."""
|
|
|
|
# Enable coverage builds on projects that use engines. Those that don't use
|
|
|
|
# engines shouldn't get coverage builds.
|
|
|
|
engines = project_yaml.get('fuzzing_engines', DEFAULT_ENGINES)
|
|
|
|
engineless = 'none' in engines
|
|
|
|
if engineless:
|
|
|
|
assert_message = ('Forbidden to specify multiple engines for '
|
|
|
|
'"fuzzing_engines" if "none" is specified.')
|
|
|
|
assert len(engines) == 1, assert_message
|
|
|
|
return False
|
2022-11-17 20:11:59 +00:00
|
|
|
if 'wycheproof' in engines:
|
|
|
|
return False
|
2020-08-26 18:59:56 +00:00
|
|
|
|
|
|
|
language = project_yaml.get('language')
|
2021-08-05 00:04:55 +00:00
|
|
|
if language not in constants.LANGUAGES_WITH_COVERAGE_SUPPORT:
|
2020-08-26 18:59:56 +00:00
|
|
|
print(('Project is written in "{language}", '
|
|
|
|
'coverage is not supported yet.').format(language=language))
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2022-03-24 18:54:45 +00:00
|
|
|
def flatten_options(option_list):
|
|
|
|
"""Generator that flattens |option_list| (a list of sanitizers, architectures
|
|
|
|
or fuzzing engines) by returning each element in the list that isn't a
|
|
|
|
dictionary. For elements that are dictionaries, the sole key is returned."""
|
|
|
|
result = []
|
|
|
|
for option in option_list:
|
|
|
|
if isinstance(option, dict):
|
|
|
|
keys = list(option.keys())
|
|
|
|
assert len(keys) == 1
|
|
|
|
result.append(keys[0])
|
|
|
|
continue
|
|
|
|
result.append(option)
|
|
|
|
print(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
def should_build(project_yaml):
|
2020-08-26 18:59:56 +00:00
|
|
|
"""Returns True on if the build specified is enabled in the project.yaml."""
|
|
|
|
|
|
|
|
if os.getenv('SANITIZER') == 'coverage':
|
|
|
|
# This assumes we only do coverage builds with libFuzzer on x86_64.
|
|
|
|
return should_build_coverage(project_yaml)
|
2019-07-02 23:11:37 +00:00
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
def is_enabled(env_var, yaml_name, defaults):
|
|
|
|
"""Is the value of |env_var| enabled in |project_yaml| (in the |yaml_name|
|
|
|
|
section)? Uses |defaults| if |yaml_name| section is unspecified."""
|
2022-03-24 18:54:45 +00:00
|
|
|
return os.getenv(env_var) in flatten_options(
|
|
|
|
project_yaml.get(yaml_name, defaults))
|
2019-05-15 18:01:53 +00:00
|
|
|
|
2020-06-12 21:39:16 +00:00
|
|
|
return (is_enabled('ENGINE', 'fuzzing_engines', DEFAULT_ENGINES) and
|
|
|
|
is_enabled('SANITIZER', 'sanitizers', DEFAULT_SANITIZERS) and
|
|
|
|
is_enabled('ARCHITECTURE', 'architectures', DEFAULT_ARCHITECTURES))
|
2019-07-02 23:11:37 +00:00
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
def build_project(project):
|
2020-06-12 21:39:16 +00:00
|
|
|
"""Do the build of |project| that is specified by the environment variables -
|
|
|
|
SANITIZER, ENGINE, and ARCHITECTURE."""
|
2019-05-10 22:58:18 +00:00
|
|
|
root = get_oss_fuzz_root()
|
|
|
|
project_yaml_path = os.path.join(root, 'projects', project, 'project.yaml')
|
2020-02-05 17:10:05 +00:00
|
|
|
with open(project_yaml_path) as file_handle:
|
|
|
|
project_yaml = yaml.safe_load(file_handle)
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
if project_yaml.get('disabled', False):
|
2019-07-02 23:11:37 +00:00
|
|
|
print('Project {0} is disabled, skipping build.'.format(project))
|
2019-05-10 22:58:18 +00:00
|
|
|
return
|
|
|
|
|
2020-06-12 21:39:16 +00:00
|
|
|
engine = os.getenv('ENGINE')
|
|
|
|
sanitizer = os.getenv('SANITIZER')
|
|
|
|
architecture = os.getenv('ARCHITECTURE')
|
2019-05-10 22:58:18 +00:00
|
|
|
|
2020-08-26 18:59:56 +00:00
|
|
|
if not should_build(project_yaml):
|
2019-05-15 18:01:53 +00:00
|
|
|
print(('Specified build: engine: {0}, sanitizer: {1}, architecture: {2} '
|
2020-08-26 18:59:56 +00:00
|
|
|
'not enabled for this project: {3}. Skipping build.').format(
|
2019-05-15 18:01:53 +00:00
|
|
|
engine, sanitizer, architecture, project))
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
print('Building project', project)
|
|
|
|
build_fuzzers(project, engine, sanitizer, architecture)
|
2020-04-08 02:26:24 +00:00
|
|
|
|
2022-09-19 00:21:43 +00:00
|
|
|
run_tests = project_yaml.get('run_tests', True)
|
|
|
|
if engine != 'none' and sanitizer != 'coverage' and run_tests:
|
2019-09-06 20:49:09 +00:00
|
|
|
check_build(project, engine, sanitizer, architecture)
|
2019-05-13 22:01:25 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
|
2020-11-20 16:40:47 +00:00
|
|
|
class BuildModifiedProjectsResult(enum.Enum):
|
|
|
|
"""Enum containing the return values of build_modified_projects()."""
|
|
|
|
NONE_BUILT = 0
|
|
|
|
BUILD_SUCCESS = 1
|
|
|
|
BUILD_FAIL = 2
|
|
|
|
|
|
|
|
|
|
|
|
def build_modified_projects():
|
|
|
|
"""Build modified projects. Returns BuildModifiedProjectsResult.NONE_BUILT if
|
|
|
|
no builds were attempted. Returns BuildModifiedProjectsResult.BUILD_SUCCESS if
|
|
|
|
all attempts succeed, otherwise returns
|
|
|
|
BuildModifiedProjectsResult.BUILD_FAIL."""
|
2019-07-02 23:11:37 +00:00
|
|
|
projects = get_modified_buildable_projects()
|
2020-11-20 16:40:47 +00:00
|
|
|
if not projects:
|
|
|
|
return BuildModifiedProjectsResult.NONE_BUILT
|
|
|
|
|
2019-05-13 16:55:06 +00:00
|
|
|
failed_projects = []
|
2019-05-10 22:58:18 +00:00
|
|
|
for project in projects:
|
|
|
|
try:
|
|
|
|
build_project(project)
|
|
|
|
except subprocess.CalledProcessError:
|
2019-05-13 16:55:06 +00:00
|
|
|
failed_projects.append(project)
|
2019-05-10 22:58:18 +00:00
|
|
|
|
2019-05-13 16:55:06 +00:00
|
|
|
if failed_projects:
|
|
|
|
print('Failed projects:', ' '.join(failed_projects))
|
2020-11-20 16:40:47 +00:00
|
|
|
return BuildModifiedProjectsResult.BUILD_FAIL
|
|
|
|
|
|
|
|
return BuildModifiedProjectsResult.BUILD_SUCCESS
|
|
|
|
|
|
|
|
|
2020-11-20 18:09:17 +00:00
|
|
|
def is_infra_changed():
|
|
|
|
"""Returns True if the infra directory was changed."""
|
2021-01-28 00:06:04 +00:00
|
|
|
git_output = get_changed_files_output()
|
2020-11-20 16:40:47 +00:00
|
|
|
infra_code_regex = '.*infra/.*\n'
|
|
|
|
return re.search(infra_code_regex, git_output) is not None
|
|
|
|
|
|
|
|
|
2020-11-20 18:09:17 +00:00
|
|
|
def build_base_images():
|
|
|
|
"""Builds base images."""
|
|
|
|
# TODO(jonathanmetzman): Investigate why caching fails so often and
|
|
|
|
# when we improve it, build base-clang as well. Also, move this function
|
|
|
|
# to a helper command when we can support base-clang.
|
|
|
|
execute_helper_command(['pull_images'])
|
|
|
|
images = [
|
|
|
|
'base-image',
|
|
|
|
'base-builder',
|
2021-09-01 04:25:24 +00:00
|
|
|
'base-builder-go',
|
2022-08-23 21:41:42 +00:00
|
|
|
'base-builder-javascript',
|
2021-09-01 04:25:24 +00:00
|
|
|
'base-builder-jvm',
|
|
|
|
'base-builder-python',
|
|
|
|
'base-builder-rust',
|
2021-08-23 16:47:54 +00:00
|
|
|
'base-builder-swift',
|
2024-07-09 15:45:44 +00:00
|
|
|
'base-builder-ruby',
|
2020-11-20 18:09:17 +00:00
|
|
|
'base-runner',
|
|
|
|
]
|
|
|
|
for image in images:
|
|
|
|
try:
|
2021-06-17 15:32:20 +00:00
|
|
|
execute_helper_command(['build_image', image, '--no-pull', '--cache'])
|
2020-11-20 18:09:17 +00:00
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2020-11-20 16:40:47 +00:00
|
|
|
def build_canary_project():
|
|
|
|
"""Builds a specific project when infra/ is changed to verify that infra/
|
|
|
|
changes don't break things. Returns False if build was attempted but
|
|
|
|
failed."""
|
|
|
|
|
|
|
|
try:
|
|
|
|
build_project('skcms')
|
|
|
|
except subprocess.CalledProcessError:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""Build modified projects or canary project."""
|
2021-05-07 14:46:11 +00:00
|
|
|
os.environ['OSS_FUZZ_CI'] = '1'
|
2020-11-20 18:09:17 +00:00
|
|
|
infra_changed = is_infra_changed()
|
|
|
|
if infra_changed:
|
|
|
|
print('Pulling and building base images first.')
|
2021-01-20 19:37:37 +00:00
|
|
|
if build_base_images():
|
|
|
|
return 1
|
2020-11-20 18:09:17 +00:00
|
|
|
|
2020-11-20 16:40:47 +00:00
|
|
|
result = build_modified_projects()
|
|
|
|
if result == BuildModifiedProjectsResult.BUILD_FAIL:
|
|
|
|
return 1
|
|
|
|
|
|
|
|
# It's unnecessary to build the canary if we've built any projects already.
|
|
|
|
no_projects_built = result == BuildModifiedProjectsResult.NONE_BUILT
|
2020-11-20 18:09:17 +00:00
|
|
|
should_build_canary = no_projects_built and infra_changed
|
|
|
|
if should_build_canary and not build_canary_project():
|
2020-02-05 17:10:05 +00:00
|
|
|
return 1
|
|
|
|
|
|
|
|
return 0
|
2019-05-10 22:58:18 +00:00
|
|
|
|
2019-05-13 22:01:25 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
if __name__ == '__main__':
|
2020-02-05 17:10:05 +00:00
|
|
|
sys.exit(main())
|