Skip to content

Commit

Permalink
test: add basic fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
snormore committed Apr 24, 2024
1 parent 96a0937 commit 42f882e
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
4 changes: 4 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target
corpus
artifacts
coverage
64 changes: 64 additions & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
36 changes: 36 additions & 0 deletions fuzz/fuzz_targets/fuzz_basic_borrows.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
});

0 comments on commit 42f882e

Please sign in to comment.