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
|
|
|
|
|
|
|
|
import os
|
|
|
|
import re
|
|
|
|
import subprocess
|
|
|
|
import yaml
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
DEFAULT_ARCHITECTURES = ['x86_64']
|
|
|
|
DEFAULT_ENGINES = ['afl', 'libfuzzer']
|
2019-05-13 16:55:06 +00:00
|
|
|
DEFAULT_SANITIZERS = ['address', 'undefined']
|
2019-05-10 22:58:18 +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."""
|
2019-05-13 20:04:23 +00:00
|
|
|
master_head_sha = subprocess.check_output(
|
|
|
|
['git', 'merge-base', 'HEAD', 'FETCH_HEAD']).decode().strip()
|
|
|
|
output = subprocess.check_output(
|
|
|
|
['git', 'diff', '--name-only', 'HEAD', master_head_sha]).decode()
|
2019-05-10 22:58:18 +00:00
|
|
|
projects_regex = '.*projects/(?P<name>.*)/.*\n'
|
2019-07-02 23:11:37 +00:00
|
|
|
modified_projects = set(re.findall(projects_regex, output))
|
|
|
|
projects_dir = os.path.join(get_oss_fuzz_root(), 'projects')
|
|
|
|
# Filter out projects without build.sh files since new projects and reverted
|
|
|
|
# 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:
|
|
|
|
if not os.path.exists(os.path.join(projects_dir, project, 'build.sh')):
|
|
|
|
print('Project {0} does not have a build.sh. skipping build.'.format(
|
|
|
|
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
|
|
|
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
def should_build(project_yaml):
|
|
|
|
"""Is the build specified by travis enabled in the |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."""
|
|
|
|
return os.getenv(env_var) in project_yaml.get(yaml_name, defaults)
|
|
|
|
|
|
|
|
return (is_enabled('TRAVIS_ENGINE', 'fuzzing_engines', DEFAULT_ENGINES) and
|
|
|
|
is_enabled('TRAVIS_SANITIZER', 'sanitizers', DEFAULT_SANITIZERS) and
|
2019-07-02 23:11:37 +00:00
|
|
|
is_enabled('TRAVIS_ARCHITECTURE', 'architectures',
|
|
|
|
DEFAULT_ARCHITECTURES))
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
def build_project(project):
|
2019-05-15 18:01:53 +00:00
|
|
|
"""Do the build of |project| that is specified by the TRAVIS_* environment
|
|
|
|
variables (TRAVIS_SANITIZER, TRAVIS_ENGINE, and TRAVIS_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')
|
|
|
|
with open(project_yaml_path) as fp:
|
|
|
|
project_yaml = yaml.safe_load(fp)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
engine = os.getenv('TRAVIS_ENGINE')
|
|
|
|
sanitizer = os.getenv('TRAVIS_SANITIZER')
|
|
|
|
architecture = os.getenv('TRAVIS_ARCHITECTURE')
|
2019-05-10 22:58:18 +00:00
|
|
|
|
2019-05-15 18:01:53 +00:00
|
|
|
if not should_build(project_yaml):
|
|
|
|
print(('Specified build: engine: {0}, sanitizer: {1}, architecture: {2} '
|
2019-07-02 23:11:37 +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)
|
|
|
|
check_build(project, engine, sanitizer, architecture)
|
2019-05-13 22:01:25 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
|
|
|
|
def main():
|
2019-07-02 23:11:37 +00:00
|
|
|
projects = get_modified_buildable_projects()
|
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))
|
2019-05-10 22:58:18 +00:00
|
|
|
exit(1)
|
|
|
|
|
2019-05-13 22:01:25 +00:00
|
|
|
|
2019-05-10 22:58:18 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|