Skip to content

Commit

Permalink
Setup fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Sep 13, 2023
1 parent dccb69b commit faa3ef6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
/Cargo.lock
target
Cargo.lock
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
28 changes: 28 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[package]
name = "slabbin-fuzz"
version = "0.0.0"
publish = false
edition = "2021"

[package.metadata]
cargo-fuzz = true

[dependencies]
arbitrary = { version = "1.0", features = ["derive_arbitrary"] }
libfuzzer-sys = "0.4"

[dependencies.slabbin]
path = ".."

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[profile.release]
debug = 1

[[bin]]
name = "allocate_deallocate"
path = "fuzz_targets/allocate_deallocate.rs"
test = false
doc = false
44 changes: 44 additions & 0 deletions fuzz/fuzz_targets/allocate_deallocate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#![no_main]

use arbitrary::Arbitrary;
use libfuzzer_sys::fuzz_target;
use slabbin::SlabAllocator;

#[derive(Arbitrary, Debug)]
struct Input {
slab_capacity: u8,
methods: Vec<AllocatorMethod>,
}

#[derive(Arbitrary, Debug)]
enum AllocatorMethod {
Allocate,
Deallocate { index: usize },
}

fuzz_target!(|input: Input| {
if input.slab_capacity == 0 {
return;
}

let allocator = SlabAllocator::<i32>::new(usize::from(input.slab_capacity));
let mut ptrs = Vec::new();

for method in input.methods {
match method {
AllocatorMethod::Allocate => {
ptrs.push(Some(allocator.allocate()));
}
AllocatorMethod::Deallocate { index } => {
if let Some(&Some(ptr)) = ptrs.get(index) {
unsafe { allocator.deallocate(ptr) };
ptrs[index] = None;
}
}
}
}

for ptr in ptrs.into_iter().flatten() {
unsafe { allocator.deallocate(ptr) };
}
});

0 comments on commit faa3ef6

Please sign in to comment.