Skip to content

Commit d811ebf

Browse files
PSEC-1167 Improve rust_fuzz_test_binary
1 parent 57ace6c commit d811ebf

File tree

7 files changed

+64
-11
lines changed

7 files changed

+64
-11
lines changed

.bazelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ build:check --output_groups=build_metadata
119119
# Fuzzing configuration
120120
build:fuzzing --@rules_rust//rust/toolchain/channel=nightly
121121
build:fuzzing --build_tag_filters=fuzz_test
122-
# TODO(PSEC-1167) Move from define to transition in fuzz_testing.bzl
122+
# Ignoring transitions for now since it doesn't add any additional improvement to current setup.
123123
build:fuzzing --//bazel:enable_fuzzing_code=True
124124

125125
# Suppress all additional output to make it more convenient in scripts

bazel/fuzz_testing.bzl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,31 @@ RUSTC_FLAGS_DEFAULTS_FOR_FUZZING = [
1919
"-Zsanitizer=address",
2020
]
2121

22-
def rust_fuzz_test_binary(name, srcs, proc_macro_deps = [], deps = []):
23-
# Builds the fuzzer using the Rust nightly toolchain so it can be run by libfuzzer. The fuzzer must be compiled using
24-
# Rust nightly, e.g.
25-
# bazel run --@rules_rust//rust/toolchain/channel=nightly --build_tag_filters=fuzz_test //rs/types/ic00_types/fuzz:decode_install_code_args
22+
def rust_fuzz_test_binary(name, srcs, rustc_flags = [], crate_features = [], proc_macro_deps = [], deps = [], **kwargs):
23+
"""Wrapper for the rust_binary to compile a fuzzing rust_binary
24+
25+
Args:
26+
name: name of the fuzzer target.
27+
srcs: source files for the fuzzer.
28+
rustc_flags: Additional rustc_flags for rust_binary rule. This facilitates using additional sanitizers to the fuzzer target.
29+
Address sanitizer is added by default.
30+
crate_features: Additional crate_features to be used for compilation.
31+
fuzzing is added by default.
32+
deps: Fuzzer dependencies.
33+
proc_macro_deps: Fuzzer proc_macro dependencies.
34+
**kwargs: additional arguments to pass a rust_binary rule.
35+
"""
2636
rust_binary(
2737
name = name,
2838
srcs = srcs,
2939
aliases = {},
30-
crate_features = ["fuzzing"],
40+
crate_features = crate_features + ["fuzzing"],
3141
proc_macro_deps = proc_macro_deps,
3242
deps = deps,
33-
rustc_flags = RUSTC_FLAGS_DEFAULTS_FOR_FUZZING,
43+
rustc_flags = rustc_flags + RUSTC_FLAGS_DEFAULTS_FOR_FUZZING,
3444
tags = [
3545
# Makes sure this target is not run in normal CI builds. It would fail due to non-nightly Rust toolchain.
3646
"fuzz_test",
3747
],
48+
**kwargs
3849
)

bin/build-all-fuzzers.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/env bash
2+
# A utility script to build all fuzzers on the IC in a single directory.
3+
# TODO(PSEC-998) - Precursor to a scheduled CI pipeline which will upload these to Clusterfuzz via gsutil
4+
set -euo pipefail
5+
6+
case $1 in
7+
-h | --help)
8+
cat <<EOF >&2
9+
usage:
10+
$0 --bin path/to/dir/ # builds all fuzzing binaries in the given directory.
11+
$0 --zip path/to/dir/ # builds and zips' all binaries in given directory. Includes a version.txt file with sha256sum.
12+
EOF
13+
exit 0
14+
;;
15+
--bin | --zip)
16+
if [ -z "${2-}" ]; then
17+
echo "No directory provided. Using fuzzer_build/"
18+
BUILD_DIR="fuzzer_build/"
19+
else
20+
echo "Using $2 for building"
21+
BUILD_DIR=$2
22+
fi
23+
CLUSTERFUZZ_ZIP_PREFIX="libfuzzer_linux"
24+
LIST_OF_FUZZERS=$(bazel query 'attr(tags, "fuzz_test", //rs/...)')
25+
# ui_event_filters to suppress WARNING: info command does not support starlark options
26+
WORKSPACE=$(bazel info workspace --ui_event_filters=-WARNING,-INFO 2>/dev/null)
27+
mkdir -p $BUILD_DIR
28+
cd $BUILD_DIR
29+
for FUZZER in $LIST_OF_FUZZERS; do
30+
bazel build --config=fuzzing $FUZZER
31+
SOURCE_BINARY="$WORKSPACE/$(bazel cquery --config=fuzzing --output=files $FUZZER)"
32+
if [ $1 == "--bin" ]; then
33+
cp -p $SOURCE_BINARY .
34+
else # zip branch
35+
SOURCE_BASENAME=$(basename $SOURCE_BINARY)
36+
# gzip -c $SOURCE_BINARY > "${CLUSTERFUZZ_ZIP_PREFIX}_${SOURCE_BASENAME}.gz"
37+
zip -j "${CLUSTERFUZZ_ZIP_PREFIX}_${SOURCE_BASENAME}.zip" $SOURCE_BINARY
38+
echo $(sha256sum "${CLUSTERFUZZ_ZIP_PREFIX}_${SOURCE_BASENAME}.zip") >>version.txt
39+
fi
40+
done
41+
;;
42+
esac

rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/fuzz/fuzz_targets/cbor_deserialize_dealing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use ic_crypto_internal_threshold_sig_ecdsa::IDkgDealingInternal;
55

66
// This fuzzer tries to find panics in CBOR deserialization of dealings. We ignore errors
77
// returned by the decoding, as these do not lead to panics. You can run the fuzzer locally:
8-
// bazel run --@rules_rust//rust/toolchain/channel=nightly --build_tag_filters=fuzz_test //rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/fuzz:cbor_deserialize_dealing
8+
// bazel run --config=fuzzing //rs/crypto/internal/crypto_lib/threshold_sig/tecdsa/fuzz:cbor_deserialize_dealing
99
fuzz_target!(|data: &[u8]| {
1010
let _ = IDkgDealingInternal::deserialize(data);
1111
});

rs/embedders/fuzz/fuzz_targets/compile_wasm_using_embedder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use wasm_smith::MaybeInvalidModule;
1717
// The fuzz test is only compiled but not executed by CI.
1818
//
1919
// To execute the fuzzer run
20-
// bazel run --@rules_rust//rust/toolchain/channel=nightly --build_tag_filters=fuzz_test //rs/embedders/fuzz:embedders_fuzzer -- corpus/
20+
// bazel run --config=fuzzing //rs/embedders/fuzz:compile_wasm_using_embedder -- corpus/
2121

2222
fuzz_target!(|module: MaybeInvalidModule| {
2323
let config = EmbeddersConfig::default();

rs/embedders/fuzz/fuzz_targets/execute_with_wasm_executor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use wasm_smith::ConfiguredModule;
3838
// The fuzz test is only compiled but not executed by CI.
3939
//
4040
// To execute the fuzzer run
41-
// bazel run --@rules_rust//rust/toolchain/channel=nightly --build_tag_filters=fuzz_test //rs/embedders/fuzz:execute_with_wasm_executor -- corpus/
41+
// bazel run --config=fuzzing --build_tag_filters=fuzz_test //rs/embedders/fuzz:execute_with_wasm_executor -- corpus/
4242

4343
fuzz_target!(|module: ConfiguredModule<ICWasmConfig>| {
4444
let wasm = module.module.to_bytes();

rs/embedders/fuzz/fuzz_targets/execute_with_wasmtime.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use wasm_smith::ConfiguredModule;
1111
// The fuzz test is only compiled but not executed by CI.
1212
//
1313
// To execute the fuzzer run
14-
// bazel run --@rules_rust//rust/toolchain/channel=nightly --build_tag_filters=fuzz_test //rs/embedders/fuzz:execute_with_wasmtime -- corpus/
14+
// bazel run --config=fuzzing //rs/embedders/fuzz:execute_with_wasmtime -- corpus/
1515

1616
fuzz_target!(|module: ConfiguredModule<ICWasmConfig>| -> Corpus {
1717
let wasm = module.module.to_bytes();

0 commit comments

Comments
 (0)