-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add OSS-Fuzz support to the Bazel fuzzing rules. (#96)
* The OSS-Fuzz support in one change. * Clarify why we're skipping the fuzzing build mode flag.
- Loading branch information
1 parent
79cd836
commit 5471279
Showing
12 changed files
with
285 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
exports_files([ | ||
"package.bzl", | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
load("@rules_fuzzing//fuzzing:cc_deps.bzl", "cc_fuzzing_engine") | ||
load("@rules_cc//cc:defs.bzl", "cc_library") | ||
|
||
cc_fuzzing_engine( | ||
name = "oss_fuzz_engine", | ||
display_name = "OSS-Fuzz", | ||
launcher = "oss_fuzz_launcher.sh", | ||
library = ":oss_fuzz_stub", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
cc_library( | ||
name = "oss_fuzz_stub", | ||
srcs = [%{stub_srcs}], | ||
linkopts = [%{stub_linkopts}], | ||
) | ||
|
||
exports_files([ | ||
"instrum.bzl", | ||
]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
"""Instrumentation options for OSS-Fuzz.""" | ||
|
||
load("@rules_fuzzing//fuzzing/private:instrum_opts.bzl", "instrum_opts") | ||
|
||
oss_fuzz_opts = instrum_opts.make( | ||
conlyopts = [%{conlyopts}], | ||
cxxopts = [%{cxxopts}], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
"""Rule for packaging fuzz tests in the expected OSS-Fuzz format.""" | ||
|
||
load("//fuzzing/private:binary.bzl", "CcFuzzingBinaryInfo") | ||
|
||
def _oss_fuzz_package_impl(ctx): | ||
output_archive = ctx.actions.declare_file(ctx.label.name + ".tar") | ||
binary_info = ctx.attr.binary[CcFuzzingBinaryInfo] | ||
|
||
action_inputs = [binary_info.binary_file] | ||
if binary_info.corpus_dir: | ||
action_inputs.append(binary_info.corpus_dir) | ||
if binary_info.dictionary_file: | ||
action_inputs.append(binary_info.dictionary_file) | ||
ctx.actions.run_shell( | ||
outputs = [output_archive], | ||
inputs = action_inputs, | ||
command = """ | ||
declare -r STAGING_DIR="$(pwd)/{output}.staging" | ||
mkdir "$STAGING_DIR" | ||
ln -s "$(pwd)/{binary_path}" "$STAGING_DIR/{base_name}" | ||
if [[ -n "{corpus_dir}" ]]; then | ||
pushd "{corpus_dir}" >/dev/null | ||
zip --quiet -r "$STAGING_DIR/{base_name}_seed_corpus.zip" ./* | ||
popd >/dev/null | ||
fi | ||
if [[ -n "{dictionary_path}" ]]; then | ||
ln -s "$(pwd)/{dictionary_path}" "$STAGING_DIR/{base_name}.dict" | ||
fi | ||
tar -chf "{output}" -C "$STAGING_DIR" . | ||
""".format( | ||
base_name = ctx.executable.binary.basename, | ||
binary_path = binary_info.binary_file.path, | ||
corpus_dir = binary_info.corpus_dir.path if binary_info.corpus_dir else "", | ||
dictionary_path = binary_info.dictionary_file.path if binary_info.dictionary_file else "", | ||
output = output_archive.path, | ||
), | ||
) | ||
return [DefaultInfo(files = depset([output_archive]))] | ||
|
||
oss_fuzz_package = rule( | ||
implementation = _oss_fuzz_package_impl, | ||
doc = """ | ||
Packages a fuzz test in a TAR archive compatible with the OSS-Fuzz format. | ||
> NOTE: The current implementation does not yet support packaging the | ||
> binary runfiles. | ||
""", | ||
attrs = { | ||
"binary": attr.label( | ||
executable = True, | ||
doc = "The fuzz test executable.", | ||
providers = [CcFuzzingBinaryInfo], | ||
mandatory = True, | ||
cfg = "target", | ||
), | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright 2020 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 | ||
# | ||
# https://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. | ||
|
||
"""Repository rule for configuring the OSS-Fuzz engine and instrumentation.""" | ||
|
||
def _to_list_repr(elements): | ||
return ", ".join([repr(element) for element in elements]) | ||
|
||
def _extract_build_params( | ||
repository_ctx, | ||
fuzzing_engine_library, | ||
cflags, | ||
cxxflags): | ||
stub_srcs = [] | ||
stub_linkopts = [] | ||
instrum_conlyopts = [] | ||
instrum_cxxopts = [] | ||
|
||
if fuzzing_engine_library: | ||
if fuzzing_engine_library.startswith("-"): | ||
# This is actually a flag, add it to the linker flags. | ||
stub_linkopts.append(fuzzing_engine_library) | ||
elif fuzzing_engine_library.endswith(".a"): | ||
repository_ctx.symlink( | ||
repository_ctx.path(fuzzing_engine_library), | ||
"oss_fuzz_engine.a", | ||
) | ||
stub_srcs.append("oss_fuzz_engine.a") | ||
else: | ||
fail("Unsupported $LIB_FUZZING_ENGINE value '%s'" % fuzzing_engine_library) | ||
for cflag in cflags: | ||
# Skip the fuzzing build more flag, since it is separately controlled | ||
# by the --//fuzzing:cc_fuzzing_build_mode configuration flag. | ||
if cflag == "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION": | ||
continue | ||
instrum_conlyopts.append(cflag) | ||
if cflag not in stub_linkopts: | ||
stub_linkopts.append(cflag) | ||
for cxxflag in cxxflags: | ||
if cxxflag == "-DFUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION": | ||
continue | ||
instrum_cxxopts.append(cxxflag) | ||
if cxxflag not in stub_linkopts: | ||
stub_linkopts.append(cxxflag) | ||
|
||
return struct( | ||
stub_srcs = stub_srcs, | ||
stub_linkopts = stub_linkopts, | ||
instrum_conlyopts = instrum_conlyopts, | ||
instrum_cxxopts = instrum_cxxopts, | ||
) | ||
|
||
def _oss_fuzz_repository(repository_ctx): | ||
environ = repository_ctx.os.environ | ||
fuzzing_engine_library = environ.get("LIB_FUZZING_ENGINE") | ||
cflags = environ.get("CFLAGS", "").split(" ") | ||
cxxflags = environ.get("CXXFLAGS", "").split(" ") | ||
|
||
build_params = _extract_build_params( | ||
repository_ctx, | ||
fuzzing_engine_library, | ||
cflags, | ||
cxxflags, | ||
) | ||
|
||
repository_ctx.template( | ||
"BUILD", | ||
repository_ctx.path(Label("@rules_fuzzing//fuzzing/private/oss_fuzz:BUILD.tpl")), | ||
{ | ||
"%{stub_srcs}": _to_list_repr(build_params.stub_srcs), | ||
"%{stub_linkopts}": _to_list_repr(build_params.stub_linkopts), | ||
}, | ||
) | ||
repository_ctx.template( | ||
"instrum.bzl", | ||
repository_ctx.path(Label("@rules_fuzzing//fuzzing/private/oss_fuzz:instrum.bzl.tpl")), | ||
{ | ||
"%{conlyopts}": _to_list_repr(build_params.instrum_conlyopts), | ||
"%{cxxopts}": _to_list_repr(build_params.instrum_cxxopts), | ||
}, | ||
) | ||
repository_ctx.file( | ||
"oss_fuzz_launcher.sh", | ||
"echo 'The OSS-Fuzz engine is not meant to be executed.'; exit 1", | ||
) | ||
|
||
oss_fuzz_repository = repository_rule( | ||
implementation = _oss_fuzz_repository, | ||
environ = [ | ||
"LIB_FUZZING_ENGINE", | ||
"CFLAGS", | ||
"CXXFLAGS", | ||
"SANITIZER", | ||
], | ||
local = True, | ||
doc = """ | ||
Generates a repository containing an OSS-Fuzz fuzzing engine defintion. | ||
The fuzzing engine is defined in the //:oss_fuzz_engine target. | ||
""", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters