From 42f882ee040113c947df431cf90200fb4f5fbf3e Mon Sep 17 00:00:00 2001 From: Steven Normore Date: Wed, 24 Apr 2024 14:08:23 -0400 Subject: [PATCH] test: add basic fuzz --- fuzz/.gitignore | 4 ++ fuzz/Cargo.lock | 64 +++++++++++++++++++++++++ fuzz/Cargo.toml | 21 ++++++++ fuzz/fuzz_targets/fuzz_basic_borrows.rs | 36 ++++++++++++++ 4 files changed, 125 insertions(+) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.lock create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/fuzz_basic_borrows.rs diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 0000000..1a45eee --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,4 @@ +target +corpus +artifacts +coverage diff --git a/fuzz/Cargo.lock b/fuzz/Cargo.lock new file mode 100644 index 0000000..ab6e5c9 --- /dev/null +++ b/fuzz/Cargo.lock @@ -0,0 +1,64 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "arbitrary" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d5a26814d8dcb93b0e5a0ff3c6d80a8843bafb21b39e8e18a6f05471870e110" + +[[package]] +name = "cc" +version = "1.0.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d32a725bc159af97c3e629873bb9f88fb8cf8a4867175f76dc987815ea07c83b" +dependencies = [ + "jobserver", + "libc", + "once_cell", +] + +[[package]] +name = "jobserver" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b099aaa34a9751c5bf0878add70444e1ed2dd73f347be99003d4577277de6e" +dependencies = [ + "libc", +] + +[[package]] +name = "libc" +version = "0.2.153" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" + +[[package]] +name = "libfuzzer-sys" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a96cfd5557eb82f2b83fed4955246c988d331975a002961b07c81584d107e7f7" +dependencies = [ + "arbitrary", + "cc", + "once_cell", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "owned_ref_cell" +version = "0.1.0" + +[[package]] +name = "owned_ref_cell-fuzz" +version = "0.0.0" +dependencies = [ + "libfuzzer-sys", + "owned_ref_cell", +] diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 0000000..711cf8d --- /dev/null +++ b/fuzz/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "owned_ref_cell-fuzz" +version = "0.0.0" +publish = false +edition = "2021" + +[package.metadata] +cargo-fuzz = true + +[dependencies] +libfuzzer-sys = "0.4" + +[dependencies.owned_ref_cell] +path = ".." + +[[bin]] +name = "fuzz_basic_borrows" +path = "fuzz_targets/fuzz_basic_borrows.rs" +test = false +doc = false +bench = false diff --git a/fuzz/fuzz_targets/fuzz_basic_borrows.rs b/fuzz/fuzz_targets/fuzz_basic_borrows.rs new file mode 100644 index 0000000..2fbdd38 --- /dev/null +++ b/fuzz/fuzz_targets/fuzz_basic_borrows.rs @@ -0,0 +1,36 @@ +#![no_main] + +use libfuzzer_sys::fuzz_target; +use owned_ref_cell::OwnedRefCell; +use std::vec::Vec; + +fuzz_target!(|data: &[u8]| { + let cell = OwnedRefCell::new(0); + let mut immutable_borrows = Vec::new(); + let mut mutable_borrows = Vec::new(); + + for &byte in data.iter() { + match byte % 3 { + 0 => { + if let Some(borrow) = cell.try_borrow() { + immutable_borrows.push(borrow); // hold the immutable borrow + } + } + 1 => { + if let Some(mut borrow_mut) = cell.try_borrow_mut() { + *borrow_mut += 1; // mutate the content + mutable_borrows.push(borrow_mut); // hold the mutable borrow + } + } + _ => { + // Randomly drop borrows from both vectors + if !immutable_borrows.is_empty() { + immutable_borrows.pop(); + } + if !mutable_borrows.is_empty() { + mutable_borrows.pop(); + } + } + } + } +});