2019-12-12 17:26:37 +00:00
|
|
|
# Copyright 2019 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.
|
|
|
|
"""Module to build a image from a specific commit, branch or pull request
|
|
|
|
|
|
|
|
This module is allows each of the OSS Fuzz projects fuzzers to be built
|
|
|
|
from a specific point in time. This feature can be used for implementations
|
|
|
|
like continuious integration fuzzing and bisection to find errors
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
|
2019-12-17 00:48:49 +00:00
|
|
|
import helper
|
|
|
|
import repo_manager
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_fuzzer_from_commit(project_name,
|
|
|
|
commit,
|
|
|
|
local_store_path,
|
|
|
|
engine='libfuzzer',
|
|
|
|
sanitizer='address',
|
2019-12-17 00:48:49 +00:00
|
|
|
architecture='x86_64',
|
|
|
|
old_repo_manager=None):
|
|
|
|
"""Builds a OSS-Fuzz fuzzer at a specific commit SHA.
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
Args:
|
2019-12-17 00:48:49 +00:00
|
|
|
project_name: The OSS-Fuzz project name
|
2019-12-12 17:26:37 +00:00
|
|
|
commit: The commit SHA to build the fuzzers at
|
|
|
|
local_store_path: The full file path of a place where a temp git repo is stored
|
|
|
|
engine: The fuzzing engine to be used
|
|
|
|
sanitizer: The fuzzing sanitizer to be used
|
|
|
|
architecture: The system architiecture to be used for fuzzing
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
0 on successful build 1 on failure
|
|
|
|
"""
|
2019-12-17 00:48:49 +00:00
|
|
|
if not old_repo_manager:
|
|
|
|
inferred_url = infer_main_repo(project_name, local_store_path, commit)
|
|
|
|
old_repo_manager = repo_manager.RepoManager(inferred_url, local_store_path)
|
|
|
|
old_repo_manager.checkout_commit(commit)
|
|
|
|
return helper.build_fuzzers_impl(
|
|
|
|
project_name=project_name,
|
|
|
|
clean=True,
|
|
|
|
engine=engine,
|
|
|
|
sanitizer=sanitizer,
|
|
|
|
architecture=architecture,
|
|
|
|
env_to_add=None,
|
|
|
|
source_path=old_repo_manager.repo_dir)
|
2019-12-12 17:26:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
def infer_main_repo(project_name, local_store_path, example_commit=None):
|
|
|
|
"""Tries to guess the main repo a project based on the Dockerfile.
|
|
|
|
|
|
|
|
NOTE: This is a fragile implementation and only works for git
|
|
|
|
Args:
|
2019-12-17 00:48:49 +00:00
|
|
|
project_name: The OSS-Fuzz project that you are checking the repo of
|
2019-12-12 17:26:37 +00:00
|
|
|
example_commit: A commit that is in the main repos tree
|
|
|
|
Returns:
|
|
|
|
The guessed repo url path or None on failue
|
|
|
|
"""
|
2019-12-17 00:48:49 +00:00
|
|
|
if not helper.check_project_exists(project_name):
|
2019-12-12 17:26:37 +00:00
|
|
|
return None
|
2019-12-17 00:48:49 +00:00
|
|
|
docker_path = helper.get_dockerfile_path(project_name)
|
2019-12-12 17:26:37 +00:00
|
|
|
with open(docker_path, 'r') as file_path:
|
|
|
|
lines = file_path.read()
|
|
|
|
# Use generic git format and project name to guess main repo
|
|
|
|
if example_commit is None:
|
|
|
|
repo_url = re.search(
|
|
|
|
r'\b(?:http|https|git)://[^ ]*' + re.escape(project_name) +
|
|
|
|
r'(.git)?', lines)
|
|
|
|
if repo_url:
|
|
|
|
return repo_url.group(0)
|
|
|
|
else:
|
|
|
|
# Use example commit SHA to guess main repo
|
|
|
|
for clone_command in re.findall('.*clone.*', lines):
|
|
|
|
repo_url = re.search(r'\b(?:https|http|git)://[^ ]*',
|
|
|
|
clone_command).group(0)
|
|
|
|
print(repo_url)
|
|
|
|
try:
|
2019-12-17 00:48:49 +00:00
|
|
|
test_repo_manager = repo_manager.RepoManager(repo_url.rstrip(),
|
|
|
|
local_store_path)
|
|
|
|
if test_repo_manager.commit_exists(example_commit):
|
2019-12-12 17:26:37 +00:00
|
|
|
return repo_url
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return None
|