Skip to content

Commit 038ee0c

Browse files
the-shank2copybara-github
authored andcommitted
No public description
PiperOrigin-RevId: 951031646
1 parent 1bdc37c commit 038ee0c

56 files changed

Lines changed: 6727 additions & 4 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.bazelrc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,9 @@ build:macos --macos_minimum_os=10.15
4444
# bazel run //bazel:setup_configs > fuzztest.bazelrc
4545
#
4646
try-import %workspace%/fuzztest.bazelrc
47+
48+
# Configure rules_rust to use the nightly channel toolchain by default.
49+
build --@rules_rust//rust/toolchain/channel:channel=nightly
50+
51+
# Link standard C++ library for Rust binaries on Linux
52+
build:linux --linkopt=-lstdc++

BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports_files(["MODULE.bazel"])

MODULE.bazel

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,48 @@ bazel_dep(
2727
name = "rules_shell",
2828
version = "0.6.1",
2929
)
30+
bazel_dep(
31+
name = "rules_rust",
32+
version = "0.71.3",
33+
)
34+
35+
rust = use_extension("@rules_rust//rust:extensions.bzl", "rust")
36+
rust.toolchain(
37+
edition = "2024",
38+
versions = ["nightly/2025-01-15"],
39+
)
40+
use_repo(rust, "rust_toolchains")
41+
register_toolchains("@rust_toolchains//:all")
42+
43+
crate = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
44+
crate.spec(package = "anyhow", version = "1.0.100")
45+
crate.spec(package = "clap", version = "4.6.1", features = ["derive", "env"])
46+
crate.spec(package = "convert_case", version = "0.6.0")
47+
crate.spec(package = "googletest", version = "0.14.3")
48+
crate.spec(package = "humantime", version = "2.4.0")
49+
crate.spec(package = "inventory", version = "0.3.20")
50+
crate.spec(package = "num-traits", version = "0.2.19")
51+
crate.spec(package = "postcard", version = "1.0.0", features = ["use-std"])
52+
crate.spec(package = "proc-macro2", version = "1.0.106")
53+
crate.spec(package = "proc-macro-crate", version = "3.4.0")
54+
crate.spec(package = "quote", version = "1.0.45")
55+
crate.spec(package = "rand", version = "0.10.1")
56+
crate.spec(package = "serde", version = "1.0.219", features = ["derive"])
57+
crate.spec(package = "spin", version = "0.10.0")
58+
crate.spec(
59+
package = "syn",
60+
version = "2.0.117",
61+
features = [
62+
"extra-traits",
63+
"full",
64+
"parsing",
65+
"printing",
66+
"visit-mut",
67+
],
68+
)
69+
crate.spec(package = "tempfile", version = "3.27.0")
70+
crate.from_specs(name = "crate_index")
71+
use_repo(crate, "crate_index")
3072
bazel_dep(
3173
name = "abseil-cpp",
3274
version = "20260526.0",

centipede/BUILD

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,6 @@ cc_library(
980980
name = "engine_worker",
981981
srcs = [
982982
"engine_worker.cc",
983-
"runner_utils.cc",
984-
"runner_utils.h",
985983
],
986984
hdrs = ["engine_worker_abi.h"],
987985
deps = [
@@ -990,6 +988,7 @@ cc_library(
990988
":feature",
991989
":runner_request",
992990
":runner_result",
991+
":runner_utils",
993992
":shared_memory_blob_sequence",
994993
"@abseil-cpp//absl/base:nullability",
995994
"@com_google_fuzztest//common:defs",
@@ -1216,8 +1215,6 @@ cc_library(
12161215
"reverse_pc_table.h",
12171216
"runner_dl_info.cc",
12181217
"runner_dl_info.h",
1219-
"runner_utils.cc",
1220-
"runner_utils.h",
12211218
"sancov_callbacks.cc",
12221219
"sancov_interceptors.cc",
12231220
"sancov_object_array.cc",
@@ -1239,6 +1236,7 @@ cc_library(
12391236
":foreach_nonzero",
12401237
":int_utils",
12411238
":runner_cmp_trace",
1239+
":runner_utils",
12421240
"@abseil-cpp//absl/base:core_headers",
12431241
"@abseil-cpp//absl/base:nullability",
12441242
"@abseil-cpp//absl/numeric:bits",
@@ -2002,3 +2000,23 @@ sh_test(
20022000
":test_util_sh",
20032001
],
20042002
)
2003+
2004+
cc_library(
2005+
name = "runner_utils",
2006+
srcs = ["runner_utils.cc"],
2007+
hdrs = ["runner_utils.h"],
2008+
copts = DISABLE_SANCOV_COPTS,
2009+
deps = [
2010+
"@abseil-cpp//absl/base:nullability",
2011+
],
2012+
)
2013+
2014+
cc_static_library(
2015+
name = "centipede_engine_static",
2016+
deps = [
2017+
":engine_controller_with_subprocess",
2018+
":engine_worker",
2019+
":sancov_runtime",
2020+
":weak_sancov_stubs",
2021+
],
2022+
)

rust/.rustfmt.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
use_field_init_shorthand = true
2+
use_try_shorthand = true
3+
edition = "2021"

rust/BUILD

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")
2+
3+
licenses(["notice"])
4+
5+
exports_files(["BUILD"])
6+
7+
rust_library(
8+
name = "fuzztest",
9+
srcs = glob([
10+
"src/**/*.rs",
11+
]),
12+
edition = "2024",
13+
proc_macro_deps = [
14+
"@com_google_fuzztest//rust/fuzztest_macro:fuzztest_macro",
15+
],
16+
rustc_flags = ["-Zallow-features=cfg_sanitize"],
17+
visibility = ["__subpackages__"],
18+
deps = [
19+
"@crate_index//:anyhow", # v1
20+
"@crate_index//:clap", # v4
21+
"@crate_index//:humantime", # v2
22+
"@crate_index//:inventory", # v0_3
23+
"@crate_index//:num-traits",
24+
"@crate_index//:postcard", # v1
25+
"@crate_index//:rand", # v0_10
26+
"@crate_index//:serde", # v1
27+
"@crate_index//:spin", # v0_10
28+
"@crate_index//:tempfile", # v3
29+
"@com_google_fuzztest//rust/coverage",
30+
"@com_google_fuzztest//rust/engine",
31+
],
32+
)
33+
34+
rust_test(
35+
name = "fuzztest_test",
36+
# Avoid interference when setting/resetting environment variables in tests.
37+
args = ["--test-threads=1"],
38+
crate = ":fuzztest",
39+
edition = "2024",
40+
rustc_flags = ["-Zallow-features=cfg_sanitize"],
41+
deps = [
42+
"@crate_index//:googletest",
43+
],
44+
)

rust/Cargo.toml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
[package]
2+
name = "fuzztest"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[[test]]
7+
name = "trybuild"
8+
path = "tests/trybuild.rs"
9+
10+
[dependencies]
11+
anyhow = "1.0.100"
12+
fuzztest-macro = { path = "fuzztest_macro" }
13+
inventory = "0.3.20"
14+
num-traits = "0.2"
15+
postcard = { version = "1.0.0", features = ["use-std"] }
16+
rand = { version = "0.10.1", features = ["std_rng"] }
17+
serde = { version = "1.0", features = ["derive"] }
18+
coverage = { path = "coverage" }
19+
engine = { path = "engine", package = "engine-ffi" }
20+
spin = "0.10.0"
21+
tempfile = "3.27.0"
22+
clap = { version = "4.6.1", features = ["derive", "env"] }
23+
humantime = "2.4.0"
24+
25+
[dev-dependencies]
26+
googletest = "0.14.3"
27+
28+
[dev-dependencies.trybuild]
29+
version = "1.0.103"
30+
features = ["diff"]
31+
32+
[profile.fuzztest]
33+
inherits = 'release'
34+
panic = 'abort'
35+
opt-level = "s"
36+
debug = true
37+
split-debuginfo = "packed"

rust/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
# FuzzTest Rust
2+
3+
A framework for fuzzing Rust projects using Google FuzzTest.
4+
5+
## Prerequisites
6+
7+
1. Clone the repository.
8+
2. Build the C++ Centipede static library engine using Bazel (from the
9+
repository root):
10+
11+
```bash
12+
cd /path/to/fuzztest
13+
bazel build //centipede:centipede_engine_static
14+
```
15+
16+
3. Copy the libcentipede_engine_static.a library to another location:
17+
18+
```bash
19+
mkdir -p $HOME/.local/lib
20+
cp /path/to/fuzztest/bazel-bin/centipede/libcentipede_engine_static.a \
21+
$HOME/.local/lib/
22+
```
23+
24+
## Setup a Project
25+
26+
1. Create a new Rust project:
27+
28+
```bash
29+
cargo new my_fuzz_project --bin
30+
```
31+
32+
2. Add `fuzztest` as a dependency in your `Cargo.toml`:
33+
34+
```toml
35+
[dependencies]
36+
fuzztest = { path = "/path/to/fuzztest/rust" }
37+
googletest = "0.14.3"
38+
```
39+
40+
## Write a Fuzz Test
41+
42+
In `src/main.rs`:
43+
44+
```rust
45+
#[cfg(test)]
46+
mod tests {
47+
use fuzztest::domains::arbitrary::Arbitrary;
48+
use fuzztest::fuzztest;
49+
50+
#[fuzztest(a = Arbitrary::<i32>::default(), b = Arbitrary::<i32>::default())]
51+
fn test_addition(a: i32, b: i32) {
52+
let _ = a.wrapping_add(b);
53+
}
54+
}
55+
```
56+
57+
## Build and Run Fuzz Tests
58+
59+
To run the fuzz tests, you must specify:
60+
61+
```bash
62+
RUSTFLAGS="-L $HOME/.local/lib" cargo test __fuzztest_mod__::test_addition
63+
```

rust/cargo_fuzztest/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "cargo-fuzztest"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
clap = { version = "4.5", features = ["derive"] }
8+
anyhow = "1.0.100"
9+
serde = "1.0.228"
10+
serde_json = "1.0.150"

0 commit comments

Comments
 (0)