Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Force the use of Clang for all builds.
# Set clang as the default compiler for most configurations.
build --repo_env=CC=clang

# Needed for abseil-cpp until https://github.com/bazelbuild/bazel/pull/19794 is released.
Expand Down Expand Up @@ -74,6 +74,13 @@ build:ubsan-honggfuzz --//fuzzing:cc_engine=//fuzzing/engines:honggfuzz
build:ubsan-honggfuzz --@rules_fuzzing//fuzzing:cc_engine_instrumentation=honggfuzz
build:ubsan-honggfuzz --@rules_fuzzing//fuzzing:cc_engine_sanitizer=ubsan

# Honggfuzz + ASAN (GCC)
build:asan-honggfuzz-gcc --//fuzzing:cc_engine=//fuzzing/engines:honggfuzz
build:asan-honggfuzz-gcc --@rules_fuzzing//fuzzing:cc_engine_instrumentation=honggfuzz
build:asan-honggfuzz-gcc --@rules_fuzzing//fuzzing:cc_engine_sanitizer=asan
build:asan-honggfuzz-gcc --repo_env=CC=gcc
build:asan-honggfuzz-gcc --@rules_fuzzing//fuzzing:compiler_type=gcc

# Replay + ASAN
build:asan-replay --//fuzzing:cc_engine=//fuzzing/engines:replay
build:asan-replay --@rules_fuzzing//fuzzing:cc_engine_instrumentation=none
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ This section will walk you through the steps to set up fuzzing in your Bazel pro

The fuzzing rules have been tested on Bazel 4.0.0 or later. Check your Bazel version by running `bazel --version`.

C++ fuzzing requires a Clang compiler. The libFuzzer engine requires at least Clang 6.0. In addition, the Honggfuzz engine requires the `libunwind-dev` and `libblocksruntime-dev` packages:
The libFuzzer engine requires at least Clang 6.0. Honggfuzz works with both clang and gcc (8 or later) and requires the `libunwind-dev` and `libblocksruntime-dev` packages:

```sh
$ sudo apt-get install clang libunwind-dev libblocksruntime-dev
$ sudo apt-get install libunwind-dev libblocksruntime-dev
```

Java fuzzing requires Clang and the LLD linker:
Expand Down
16 changes: 16 additions & 0 deletions fuzzing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,22 @@ bool_flag(
visibility = ["//visibility:public"],
)

string_flag(
name = "compiler_type",
build_setting_default = "clang",
values = [
"clang",
"gcc",
],
visibility = ["//visibility:public"],
)

config_setting(
name = "is_gcc",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be replaced with the predefined settings in @rules_cc//cc/compiler. Then we don't need compiler_type at all as the choice will be determined by the C++ toolchain.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I originally attempted something based on @rules_cc//cc/compiler but got stuck on this error

ERROR: .../rules_fuzzing/examples/java/BUILD:31:15: On dependency edge //examples/java:EmptyFuzzTest_bin (3b35bd8) -|binary|-> //examples/java:EmptyFuzzTest_raw_: attempting to transition on '@@rules_cc+//cc/compiler:gcc' which is not a build setting

when adding @rules_cc//cc/compiler:gcc as an input to the fuzzing_binary_transition transition. Do you have an idea how to access the compiler type from the transition without it being an explicit flag?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I followed up on bazelbuild/rules_cc#435. If we had that, we could read the compiler from an implicit attribute of the fuzzing_binary rule.

Let's wait for a few days before we look into alternatives.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's in now, we would just need to update rules_cc.

flag_values = {":compiler_type": "gcc"},
visibility = ["//visibility:public"],
)

exports_files([
"cc_defs.bzl",
"java_defs.bzl",
Expand Down
40 changes: 27 additions & 13 deletions fuzzing/instrum_opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,35 @@ load(
"oss_fuzz_opts",
)

# Fuzz test binary instrumentation configurations.
# Fuzz test binary instrumentation configurations by compiler type.
instrum_configs = {
"none": instrum_opts.make(),
"libfuzzer": instrum_defaults.libfuzzer,
"jazzer": instrum_defaults.jazzer,
"honggfuzz": instrum_defaults.honggfuzz,
"oss-fuzz": oss_fuzz_opts,
"clang": {
"none": instrum_opts.make(),
"libfuzzer": instrum_defaults.libfuzzer,
"jazzer": instrum_defaults.jazzer,
"honggfuzz": instrum_defaults.honggfuzz_clang,
"oss-fuzz": oss_fuzz_opts,
},
"gcc": {
"none": instrum_opts.make(),
"honggfuzz": instrum_defaults.honggfuzz_gcc,
},
}

# Sanitizer configurations.
# Sanitizer configurations by compiler type.
sanitizer_configs = {
"none": instrum_opts.make(),
"asan": instrum_defaults.asan,
"msan": instrum_defaults.msan,
"msan-origin-tracking": instrum_defaults.msan_origin_tracking,
"ubsan": instrum_defaults.ubsan,
"asan-ubsan": instrum_opts.merge(instrum_defaults.asan, instrum_defaults.ubsan),
"clang": {
"none": instrum_opts.make(),
"asan": instrum_defaults.asan,
"msan": instrum_defaults.msan,
"msan-origin-tracking": instrum_defaults.msan_origin_tracking,
"ubsan": instrum_defaults.ubsan_clang,
"asan-ubsan": instrum_opts.merge(instrum_defaults.asan, instrum_defaults.ubsan_clang),
},
"gcc": {
"none": instrum_opts.make(),
"asan": instrum_defaults.asan,
"ubsan": instrum_defaults.ubsan_gcc,
"asan-ubsan": instrum_opts.merge(instrum_defaults.asan, instrum_defaults.ubsan_gcc),
},
}
14 changes: 8 additions & 6 deletions fuzzing/private/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,23 @@ def _fuzzing_binary_transition_impl(settings, _attr):
cxxopts = settings["//command_line_option:cxxopt"],
linkopts = settings["//command_line_option:linkopt"],
)
compiler_type = settings["@rules_fuzzing//fuzzing:compiler_type"]

is_fuzzing_build_mode = settings["@rules_fuzzing//fuzzing:cc_fuzzing_build_mode"]
if is_fuzzing_build_mode:
opts = instrum_opts.merge(opts, instrum_defaults.fuzzing_build)

instrum_config = settings["@rules_fuzzing//fuzzing:cc_engine_instrumentation"]
if instrum_config in instrum_configs:
opts = instrum_opts.merge(opts, instrum_configs[instrum_config])
if instrum_config in instrum_configs[compiler_type]:
opts = instrum_opts.merge(opts, instrum_configs[compiler_type][instrum_config])
else:
fail("unsupported engine instrumentation '%s'" % instrum_config)
fail("unsupported engine instrumentation '%s' for compiler '%s'" % (instrum_config, compiler_type))

sanitizer_config = settings["@rules_fuzzing//fuzzing:cc_engine_sanitizer"]
if sanitizer_config in sanitizer_configs:
opts = instrum_opts.merge(opts, sanitizer_configs[sanitizer_config])
if sanitizer_config in sanitizer_configs[compiler_type]:
opts = instrum_opts.merge(opts, sanitizer_configs[compiler_type][sanitizer_config])
else:
fail("unsupported sanitizer '%s'" % sanitizer_config)
fail("unsupported sanitizer '%s' for compiler '%s'" % (sanitizer_config, compiler_type))

return {
"//command_line_option:copt": opts.copts,
Expand All @@ -87,6 +88,7 @@ fuzzing_binary_transition = transition(
"@rules_fuzzing//fuzzing:cc_engine_instrumentation",
"@rules_fuzzing//fuzzing:cc_engine_sanitizer",
"@rules_fuzzing//fuzzing:cc_fuzzing_build_mode",
"@rules_fuzzing//fuzzing:compiler_type",
"//command_line_option:copt",
"//command_line_option:conlyopt",
"//command_line_option:cxxopt",
Expand Down
22 changes: 20 additions & 2 deletions fuzzing/private/instrum_opts.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ instrum_defaults = struct(
),
# Reflects the set of options at
# https://github.com/google/honggfuzz/blob/master/hfuzz_cc/hfuzz-cc.c
honggfuzz = _make_opts(
honggfuzz_clang = _make_opts(
copts = [
"-mllvm",
"-inline-threshold=2000",
Expand All @@ -105,6 +105,16 @@ instrum_defaults = struct(
"-fno-sanitize=fuzzer",
],
),
honggfuzz_gcc = _make_opts(
copts = [
"-finline-limit=1000",
"-fsanitize-coverage=trace-pc,trace-cmp",
"-fno-builtin",
"-fno-omit-frame-pointer",
"-D__NO_STRING_INLINES",
],
linkopts = [],
),
asan = _make_opts(
copts = ["-fsanitize=address"],
linkopts = ["-fsanitize=address"],
Expand All @@ -120,7 +130,7 @@ instrum_defaults = struct(
],
linkopts = ["-fsanitize=memory"],
),
ubsan = _make_opts(
ubsan_clang = _make_opts(
copts = [
"-fsanitize=undefined",
],
Expand All @@ -133,4 +143,12 @@ instrum_defaults = struct(
"-fsanitize-link-c++-runtime",
],
),
ubsan_gcc = _make_opts(
copts = [
"-fsanitize=undefined",
],
linkopts = [
"-fsanitize=undefined",
],
),
)
6 changes: 3 additions & 3 deletions fuzzing/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ def rules_fuzzing_dependencies(oss_fuzz = True, honggfuzz = True, jazzer = True)
http_archive,
name = "honggfuzz",
build_file = "@rules_fuzzing//:honggfuzz.BUILD",
sha256 = "6b18ba13bc1f36b7b950c72d80f19ea67fbadc0ac0bb297ec89ad91f2eaa423e",
url = "https://github.com/google/honggfuzz/archive/2.5.zip",
strip_prefix = "honggfuzz-2.5",
integrity = "sha256-d+DpDrzBMqmSNCuz0+/Vi3jTUiIFNnulWGAeBJaaHJM=",
url = "https://github.com/google/honggfuzz/archive/4cfa62f4fdb56e3027c1cb3aecf04812e786f0fd.zip",
strip_prefix = "honggfuzz-4cfa62f4fdb56e3027c1cb3aecf04812e786f0fd",
)

if jazzer:
Expand Down
43 changes: 29 additions & 14 deletions honggfuzz.BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,31 @@ COMMON_COPTS = [
"-Wall",
"-Wextra",
"-Werror",
"-Wno-override-init",
"-Wno-initializer-overrides",
"-Wno-gnu-empty-initializer",
"-Wno-format-pedantic",
"-Wno-gnu-statement-expression",
"-mllvm",
"-inline-threshold=2000",
"-fblocks",

# Do not instrument Honggfuzz itself, in order to avoid recursive
# instrumentation calls that would crash the fuzz test binary.
"-fsanitize-coverage=0",
"-fno-sanitize=all",
]
] + select({
"@rules_fuzzing//fuzzing:is_gcc": [
"-Wno-override-init",
"-Wno-format-truncation",
# Do not instrument Honggfuzz itself, in order to avoid recursive
# instrumentation calls that would crash the fuzz test binary.
"-fno-sanitize-coverage=trace-pc,trace-cmp",
"-fno-sanitize=all",
],
# Default to clang compiler flags
"//conditions:default": [
"-mllvm",
"-inline-threshold=2000",
"-fblocks",
"-Wno-override-init",
"-Wno-initializer-overrides",
"-Wno-gnu-empty-initializer",
"-Wno-format-pedantic",
"-Wno-gnu-statement-expression",
# Do not instrument Honggfuzz itself, in order to avoid recursive
# instrumentation calls that would crash the fuzz test binary.
"-fsanitize-coverage=0",
"-fno-sanitize=all",
],
})

LIBRARY_COPTS = [
"-fno-stack-protector",
Expand Down Expand Up @@ -113,6 +124,10 @@ SYMBOL_WRAP_LINKOPTS = select({
"-Wl,--wrap=Curl_safe_strcasecompare",
"-Wl,--wrap=Curl_strncasecompare",
"-Wl,--wrap=curl_strnequal",
# SQLite3
"-Wl,--wrap=sqlite3_stricmp",
"-Wl,--wrap=sqlite3_strnicmp",
"-Wl,--wrap=sqlite3StrICmp",
],
})

Expand Down