Skip to content

Commit

Permalink
Refactor oss-fuzz support
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Jun 13, 2023
1 parent e65ff77 commit cf3b8b7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion fuzzing/private/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ exports_files([
])

config_setting(
name = "use_oss_fuzz",
name = "is_oss_fuzz",
flag_values = {
"@rules_fuzzing//fuzzing:cc_engine": "@rules_fuzzing_oss_fuzz//:oss_fuzz_engine",
},
Expand Down
26 changes: 19 additions & 7 deletions fuzzing/private/fuzz_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

"""The implementation of the {cc, java}_fuzz_test rules."""

load("@rules_fuzzing_oss_fuzz//:instrum.bzl", "native_library_sanitizer")
load("@rules_cc//cc:defs.bzl", "cc_binary")

# FIXME: Including this leads to a Stardoc error since defs.bzl is not visible. As a workaround, use native.java_binary.
Expand Down Expand Up @@ -213,6 +214,14 @@ def cc_fuzz_test(
test_timeout = timeout,
)

_ASAN_RUNTIME = Label("//fuzzing/private/runtime:asan")
_UBSAN_RUNTIME = Label("//fuzzing/private/runtime:ubsan")
_RUNTIME_BY_NAME = {
"asan": _ASAN_RUNTIME,
"ubsan": _UBSAN_RUNTIME,
"none": None,
}

# buildifier: disable=list-append
def java_fuzz_test(
name,
Expand Down Expand Up @@ -322,20 +331,23 @@ def java_fuzz_test(
raw_binary_name = name + "_raw_"
jazzer_fuzz_binary(
name = raw_binary_name,
sanitizer = select({
"@rules_fuzzing//fuzzing/private:is_oss_fuzz": native_library_sanitizer,
"@rules_fuzzing//fuzzing/private:use_asan": "asan",
"@rules_fuzzing//fuzzing/private:use_ubsan": "ubsan",
"//conditions:default": "none",
}),
sanitizer_options = select({
"@rules_fuzzing//fuzzing/private:use_oss_fuzz": Label("//fuzzing/private:oss_fuzz_jazzer_sanitizer_options.sh"),
"@rules_fuzzing//fuzzing/private:is_oss_fuzz": Label("//fuzzing/private:oss_fuzz_jazzer_sanitizer_options.sh"),
"//conditions:default": Label("//fuzzing/private:local_jazzer_sanitizer_options.sh"),
}),
sanitizer_runtime = select({
"@rules_fuzzing//fuzzing/private:use_asan": Label("//fuzzing/private/runtime:asan"),
"@rules_fuzzing//fuzzing/private:use_ubsan": Label("//fuzzing/private/runtime:ubsan"),
"@rules_fuzzing//fuzzing/private:is_oss_fuzz": _RUNTIME_BY_NAME[native_library_sanitizer],
"@rules_fuzzing//fuzzing/private:use_asan": _ASAN_RUNTIME,
"@rules_fuzzing//fuzzing/private:use_ubsan": _UBSAN_RUNTIME,
"//conditions:default": None,
}),
target = raw_target_name,
use_oss_fuzz = select({
"@rules_fuzzing//fuzzing/private:use_oss_fuzz": True,
"//conditions:default": False,
}),
tags = ["manual"],
)

Expand Down
20 changes: 4 additions & 16 deletions fuzzing/private/java_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@

load("//fuzzing/private:binary.bzl", "fuzzing_binary_transition")
load("//fuzzing/private:util.bzl", "runfile_path")
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")
load("@rules_fuzzing_oss_fuzz//:instrum.bzl", "native_library_sanitizer")

# A Starlark reimplementation of a part of Bazel's JavaCommon#determinePrimaryClass.
def determine_primary_class(srcs, name):
Expand Down Expand Up @@ -134,20 +132,10 @@ exec "$(rlocation {target})" {sanitizer_flags} "$@"
return script

def _jazzer_fuzz_binary_impl(ctx):
sanitizer = ctx.attr._cc_sanitizer[BuildSettingInfo].value
if ctx.attr.use_oss_fuzz:
if native_library_sanitizer == "address":
sanitizer = "asan"
elif native_library_sanitizer == "undefined":
sanitizer = "ubsan"
else:
fail("Jazzer only supports the OSS-Fuzz sanitizers \"address\" and \"undefined\", got: " + native_library_sanitizer)

sanitizer = ctx.attr.sanitizer
sanitizer_flags = []
if sanitizer in ["asan", "ubsan"]:
sanitizer_flags.append("--" + sanitizer)
elif sanitizer != "none":
fail("Jazzer only supports the sanitizer settings \"none\", \"asan\", \"ubsan\", got: " + sanitizer)
if not sanitizer_flags and ctx.attr.target[0][JavaInfo].transitive_native_libraries:
sanitizer_flags.append("--native")

Expand All @@ -173,6 +161,9 @@ jazzer_fuzz_binary = rule(
Rule that creates a binary that invokes Jazzer on the specified target.
""",
attrs = {
"sanitizer": attr.string(
values = ["asan", "ubsan", "none"],
),
"sanitizer_options": attr.label(
doc = "A shell script that can export environment variables with " +
"sanitizer options.",
Expand All @@ -195,9 +186,6 @@ Rule that creates a binary that invokes Jazzer on the specified target.
"_bash_runfiles_library": attr.label(
default = "@bazel_tools//tools/bash/runfiles",
),
"_cc_sanitizer": attr.label(
default = "//fuzzing:cc_engine_sanitizer",
),
},
executable = True,
)
5 changes: 4 additions & 1 deletion fuzzing/private/oss_fuzz/repository.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def _oss_fuzz_repository(repository_ctx):
{
"%{conlyopts}": _to_list_repr(build_params.instrum_conlyopts),
"%{cxxopts}": _to_list_repr(build_params.instrum_cxxopts),
"%{sanitizer}": sanitizer or "",
"%{sanitizer}": {
"address": "asan",
"undefined": "ubsan",
}.get(sanitizer, "none"),
},
)
repository_ctx.file(
Expand Down

0 comments on commit cf3b8b7

Please sign in to comment.