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
6 changes: 6 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ build:macos --macos_minimum_os=10.15
# bazel run //bazel:setup_configs > fuzztest.bazelrc
#
try-import %workspace%/fuzztest.bazelrc

# Configure rules_rust to use the nightly channel toolchain by default.
build --@rules_rust//rust/toolchain/channel:channel=nightly

# Link standard C++ library for Rust binaries on Linux
build:linux --linkopt=-lstdc++
1 change: 1 addition & 0 deletions BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["MODULE.bazel"])
42 changes: 42 additions & 0 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,48 @@ bazel_dep(
name = "rules_shell",
version = "0.6.1",
)
bazel_dep(
name = "rules_rust",
version = "0.71.3",
)

rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
rust.toolchain(
edition = "2024",
versions = ["nightly/2025-01-15"],
)
use_repo(rust, "rust_toolchains")
register_toolchains("@rust_toolchains//:all")

crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
crate.spec(package = "anyhow", version = "1.0.100")
crate.spec(package = "clap", version = "4.6.1", features = ["derive", "env"])
crate.spec(package = "convert_case", version = "0.6.0")
crate.spec(package = "googletest", version = "0.14.3")
crate.spec(package = "humantime", version = "2.4.0")
crate.spec(package = "inventory", version = "0.3.20")
crate.spec(package = "num-traits", version = "0.2.19")
crate.spec(package = "postcard", version = "1.0.0", features = ["use-std"])
crate.spec(package = "proc-macro2", version = "1.0.106")
crate.spec(package = "proc-macro-crate", version = "3.4.0")
crate.spec(package = "quote", version = "1.0.45")
crate.spec(package = "rand", version = "0.10.1")
crate.spec(package = "serde", version = "1.0.219", features = ["derive"])
crate.spec(package = "spin", version = "0.10.0")
crate.spec(
package = "syn",
version = "2.0.117",
features = [
"extra-traits",
"full",
"parsing",
"printing",
"visit-mut",
],
)
crate.spec(package = "tempfile", version = "3.27.0")
crate.from_specs(name = "crate_index")
use_repo(crate, "crate_index")
bazel_dep(
name = "abseil-cpp",
version = "20260526.0",
Expand Down
26 changes: 22 additions & 4 deletions centipede/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -980,8 +980,6 @@ cc_library(
name = "engine_worker",
srcs = [
"engine_worker.cc",
"runner_utils.cc",
"runner_utils.h",
],
hdrs = ["engine_worker_abi.h"],
deps = [
Expand All @@ -990,6 +988,7 @@ cc_library(
":feature",
":runner_request",
":runner_result",
":runner_utils",
":shared_memory_blob_sequence",
"@abseil-cpp//absl/base:nullability",
"@com_google_fuzztest//common:defs",
Expand Down Expand Up @@ -1216,8 +1215,6 @@ cc_library(
"reverse_pc_table.h",
"runner_dl_info.cc",
"runner_dl_info.h",
"runner_utils.cc",
"runner_utils.h",
"sancov_callbacks.cc",
"sancov_interceptors.cc",
"sancov_object_array.cc",
Expand All @@ -1239,6 +1236,7 @@ cc_library(
":foreach_nonzero",
":int_utils",
":runner_cmp_trace",
":runner_utils",
"@abseil-cpp//absl/base:core_headers",
"@abseil-cpp//absl/base:nullability",
"@abseil-cpp//absl/numeric:bits",
Expand Down Expand Up @@ -2002,3 +2000,23 @@ sh_test(
":test_util_sh",
],
)

cc_library(
name = "runner_utils",
srcs = ["runner_utils.cc"],
hdrs = ["runner_utils.h"],
copts = DISABLE_SANCOV_COPTS,
deps = [
"@abseil-cpp//absl/base:nullability",
],
)

cc_static_library(
name = "centipede_engine_static",
deps = [
":engine_controller_with_subprocess",
":engine_worker",
":sancov_runtime",
":weak_sancov_stubs",
],
)
3 changes: 3 additions & 0 deletions rust/.rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use_field_init_shorthand = true
use_try_shorthand = true
edition = "2021"
44 changes: 44 additions & 0 deletions rust/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

licenses(["notice"])

exports_files(["BUILD"])

rust_library(
name = "fuzztest",
srcs = glob([
"src/**/*.rs",
]),
edition = "2024",
proc_macro_deps = [
"@com_google_fuzztest//rust/fuzztest_macro:fuzztest_macro",
],
rustc_flags = ["-Zallow-features=cfg_sanitize"],
visibility = ["__subpackages__"],
deps = [
"@crate_index//:anyhow", # v1
"@crate_index//:clap", # v4
"@crate_index//:humantime", # v2
"@crate_index//:inventory", # v0_3
"@crate_index//:num-traits",
"@crate_index//:postcard", # v1
"@crate_index//:rand", # v0_10
"@crate_index//:serde", # v1
"@crate_index//:spin", # v0_10
"@crate_index//:tempfile", # v3
"@com_google_fuzztest//rust/coverage",
"@com_google_fuzztest//rust/engine",
],
)

rust_test(
name = "fuzztest_test",
# Avoid interference when setting/resetting environment variables in tests.
args = ["--test-threads=1"],
crate = ":fuzztest",
edition = "2024",
rustc_flags = ["-Zallow-features=cfg_sanitize"],
deps = [
"@crate_index//:googletest",
],
)
37 changes: 37 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[package]
name = "fuzztest"
version = "0.1.0"
edition = "2024"

[[test]]
name = "trybuild"
path = "tests/trybuild.rs"

[dependencies]
anyhow = "1.0.100"
fuzztest-macro = { path = "fuzztest_macro" }
inventory = "0.3.20"
num-traits = "0.2"
postcard = { version = "1.0.0", features = ["use-std"] }
rand = { version = "0.10.1", features = ["std_rng"] }
serde = { version = "1.0", features = ["derive"] }
coverage = { path = "coverage" }
engine = { path = "engine", package = "engine-ffi" }
spin = "0.10.0"
tempfile = "3.27.0"
clap = { version = "4.6.1", features = ["derive", "env"] }
humantime = "2.4.0"

[dev-dependencies]
googletest = "0.14.3"

[dev-dependencies.trybuild]
version = "1.0.103"
features = ["diff"]

[profile.fuzztest]
inherits = 'release'
panic = 'abort'
opt-level = "s"
debug = true
split-debuginfo = "packed"
63 changes: 63 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# FuzzTest Rust

A framework for fuzzing Rust projects using Google FuzzTest.

## Prerequisites

1. Clone the repository.
2. Build the C++ Centipede static library engine using Bazel (from the
repository root):

```bash
cd /path/to/fuzztest
bazel build //centipede:centipede_engine_static
```

3. Copy the libcentipede_engine_static.a library to another location:

```bash
mkdir -p $HOME/.local/lib
cp /path/to/fuzztest/bazel-bin/centipede/libcentipede_engine_static.a \
$HOME/.local/lib/
```

## Setup a Project

1. Create a new Rust project:

```bash
cargo new my_fuzz_project --bin
```

2. Add `fuzztest` as a dependency in your `Cargo.toml`:

```toml
[dependencies]
fuzztest = { path = "/path/to/fuzztest/rust" }
googletest = "0.14.3"
```

## Write a Fuzz Test

In `src/main.rs`:

```rust
#[cfg(test)]
mod tests {
use fuzztest::domains::arbitrary::Arbitrary;
use fuzztest::fuzztest;

#[fuzztest(a = Arbitrary::<i32>::default(), b = Arbitrary::<i32>::default())]
fn test_addition(a: i32, b: i32) {
let _ = a.wrapping_add(b);
}
}
```

## Build and Run Fuzz Tests

To run the fuzz tests, you must specify:

```bash
RUSTFLAGS="-L $HOME/.local/lib" cargo test __fuzztest_mod__::test_addition
```
10 changes: 10 additions & 0 deletions rust/cargo_fuzztest/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "cargo-fuzztest"
version = "0.1.0"
edition = "2024"

[dependencies]
clap = { version = "4.5", features = ["derive"] }
anyhow = "1.0.100"
serde = "1.0.228"
serde_json = "1.0.150"
Loading
Loading