Skip to content
Merged
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
1 change: 1 addition & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports_files(["MODULE.bazel"])
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 = [
"@com_google_fuzztest//rust/coverage",
"@com_google_fuzztest//rust/engine",
"@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
],
)

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.workspace = true
clap.workspace = true
coverage = { path = "coverage" }
engine = { path = "engine", package = "engine-ffi" }
fuzztest-macro = { path = "fuzztest_macro" }
humantime.workspace = true
inventory.workspace = true
num-traits.workspace = true
postcard.workspace = true
rand.workspace = true
serde.workspace = true
spin.workspace = true
tempfile.workspace = true

[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"
69 changes: 69 additions & 0 deletions rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# FuzzTest Rust

A framework for fuzzing Rust projects using 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 the path that contains the previously
built `libcentipede_engine_static.a` using the `FUZZTEST_LIB_PATH` environment
variable:

```bash
# Run all fuzztests in smoke-test mode.
FUZZTEST_LIB_PATH="$HOME/.local/lib" cargo test __fuzztest_mod__
```

// TODO(the-shank): add example of more commands for fuzzing, along with sample
outputs.
36 changes: 36 additions & 0 deletions rust/coverage/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

licenses(["notice"])

rust_library(
name = "coverage",
srcs = [
"src/coverage.rs",
"src/lib.rs",
],
edition = "2024",
visibility = ["@com_google_fuzztest//rust:__subpackages__"],
deps = [
"@com_google_fuzztest//centipede:sancov_runtime",
],
)

rust_test(
name = "coverage_test",
# Avoid interference on coverage collection between threads.
args = ["--test-threads=1"],
crate = ":coverage",
edition = "2024",
env = {
"CENTIPEDE_RUNNER_FLAGS": ":use_cmp_features:",
},
# TODO: b/437896409 - Replace with global config once it's available.
rustc_flags = [
"-Cpasses=sancov-module",
"-Cllvm-args=-sanitizer-coverage-level=1",
"-Cllvm-args=-sanitizer-coverage-trace-compares",
],
deps = [
"@crate_index//:googletest",
],
)
9 changes: 9 additions & 0 deletions rust/coverage/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "coverage"
version = "0.1.0"
edition = "2024"

[lib]
test = false

[dependencies]
78 changes: 78 additions & 0 deletions rust/coverage/src/coverage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use std::slice;

#[repr(C)]
pub struct SanCovRuntimeRawFeatureParts {
// Safety invariant: The only way to receive this type is through `SanCovRuntimeGetCoverage()`
// which guarantees that `ptr` always points to a valid slice of `size` elements.
ptr: *const u64,
size: usize,
}

impl SanCovRuntimeRawFeatureParts {
/// # Safety
///
/// Must not be held on when its data has to be mutated. For example, do not hold on to it
/// when calling `SanCovRuntimeGetCoverage()`.
pub unsafe fn as_slice(&self) -> &[u64] {
// Safety: self ensures that `ptr` always points to a valid slice of `size` elements.
unsafe { slice::from_raw_parts(self.ptr, self.size) }
}
}

unsafe extern "C" {
pub safe fn SanCovRuntimeClearCoverage(full_clear: bool);

pub safe fn SanCovRuntimeGetCoverage(reject_input: bool) -> SanCovRuntimeRawFeatureParts;

pub safe fn SanCovRuntimePostProcessCoverage(reject_input: bool);

// Exposed only for testing purposes.
pub safe fn __sanitizer_cov_trace_const_cmp1(Arg1: u8, Arg2: u8);
}

#[cfg(test)]
mod test {
use super::*;
use googletest::prelude::*;

#[gtest]
fn should_collect_features_from_sancov_callback() {
SanCovRuntimeClearCoverage(true);

__sanitizer_cov_trace_const_cmp1(1, 1);

let features = SanCovRuntimeGetCoverage(false);
expect_false!(features.ptr.is_null());
expect_gt!(features.size, 0);
}

#[gtest]
fn should_not_collect_features_from_empty_user_code() {
SanCovRuntimeClearCoverage(true);

// An execution where sancov hooks such as
// `__sanitizer_cov_trace_const_cmp1` are not called.

let features = SanCovRuntimeGetCoverage(false);
expect_false!(features.ptr.is_null());
expect_eq!(features.size, 0);
}

#[gtest]
fn should_collect_features_from_user_code() {
let x = std::env::args().count();

SanCovRuntimeClearCoverage(true);

{
// An LLVM trace cmp callback is expected to be instrumented here.
if x == 2 {
std::hint::black_box(2); // To prevent dead code elimination.
}
}

let features = SanCovRuntimeGetCoverage(false);
expect_false!(features.ptr.is_null());
expect_gt!(features.size, 0);
}
}
15 changes: 15 additions & 0 deletions rust/coverage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
mod coverage;

pub use coverage::SanCovRuntimeRawFeatureParts;

pub fn prepare_coverage(full_clear: bool) {
coverage::SanCovRuntimeClearCoverage(full_clear)
}

pub fn get_coverage(reject_input: bool) -> SanCovRuntimeRawFeatureParts {
coverage::SanCovRuntimeGetCoverage(reject_input)
}

pub fn post_process_coverage(reject_input: bool) {
coverage::SanCovRuntimePostProcessCoverage(reject_input)
}
98 changes: 98 additions & 0 deletions rust/e2e_tests/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test")

licenses(["notice"])

rust_test(
name = "worker_test",
srcs = ["worker_test.rs"],
data = [
"@com_google_fuzztest//rust/e2e_tests/testdata:fuzztest_main",
],
edition = "2024",
deps = [
":test_utils",
"@crate_index//:googletest",
],
)

rust_test(
name = "standalone_mode_test",
srcs = ["standalone_mode_test.rs"],
data = [
"@com_google_fuzztest//centipede:centipede_uninstrumented",
"@com_google_fuzztest//rust/e2e_tests/testdata:fuzztest_main",
"@com_google_fuzztest//rust/e2e_tests/testdata:standalone_fuzz_tests_bin",
],
edition = "2024",
deps = [
":test_utils",
"@crate_index//:googletest",
],
)

rust_test(
name = "worker_with_centipede_test",
srcs = ["worker_with_centipede_test.rs"],
data = [
"@com_google_fuzztest//centipede:centipede_uninstrumented",
"@com_google_fuzztest//rust/e2e_tests/testdata:fuzztest_main",
],
edition = "2024",
deps = [
":test_utils",
"@crate_index//:googletest",
"@crate_index//:rand", # v0_10
],
)

rust_test(
name = "worker_with_centipede_sanitizer_test",
srcs = ["worker_with_centipede_sanitizer_test.rs"],
data = [
"@com_google_fuzztest//centipede:centipede_uninstrumented",
"@com_google_fuzztest//rust/e2e_tests/testdata:fuzztest_main",
],
edition = "2024",
rustc_flags = ["-Zallow-features=cfg_sanitize"],
deps = [
":test_utils",
"@crate_index//:googletest",
],
)

rust_library(
name = "test_utils",
testonly = 1,
srcs = ["test_utils.rs"],
edition = "2024",
deps = [
"@crate_index//:googletest",
],
)

rust_test(
name = "gtest_registration_test",
srcs = ["gtest_registration_test.rs"],
data = [
"@com_google_fuzztest//rust/e2e_tests/testdata:fuzz_tests_as_gtests",
],
edition = "2024",
deps = [
":test_utils",
"@crate_index//:googletest",
],
)

rust_test(
name = "replay_test",
srcs = ["replay_test.rs"],
data = [
"@com_google_fuzztest//centipede:centipede_uninstrumented",
"@com_google_fuzztest//rust/e2e_tests/testdata:replay_fuzz_tests_bin",
],
edition = "2024",
deps = [
":test_utils",
"@crate_index//:googletest",
],
)
Loading
Loading