Skip to content

Commit

Permalink
test: share structs between fuzz targets
Browse files Browse the repository at this point in the history
  • Loading branch information
bluurryy committed Oct 25, 2024
1 parent 8e134cb commit f4dd841
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 53 deletions.
11 changes: 1 addition & 10 deletions crates/fuzzing-support/src/allocator_api.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{dbg, eprintln, MaybeFailingAllocator, RcAllocator};
use crate::{dbg, eprintln, MaybeFailingAllocator, MinAlign, RcAllocator};
use arbitrary::{Arbitrary, Unstructured};
use bump_scope::{
allocator_api2::alloc::{Allocator, Global},
Expand Down Expand Up @@ -316,15 +316,6 @@ struct Allocation {
layout: Layout,
}

#[derive(Debug, Clone, Copy, Arbitrary)]
enum MinAlign {
Shl0 = 1 << 0,
Shl1 = 1 << 1,
Shl2 = 1 << 2,
Shl3 = 1 << 3,
Shl4 = 1 << 4,
}

#[derive(Debug, Clone, Copy, Arbitrary)]
enum Operation {
Allocate {
Expand Down
36 changes: 8 additions & 28 deletions crates/fuzzing-support/src/bumping.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#![allow(non_camel_case_types)]

use arbitrary::{Arbitrary, Unstructured};
use arbitrary::Arbitrary;
use bump_scope::{allocator_api2::alloc::Global, Bump, MinimumAlignment, SupportedMinimumAlignment};
use core::fmt::Debug;
use std::{alloc::Layout, mem};

use crate::{MinAlign, UpTo};

#[derive(Debug, Arbitrary)]
pub struct Fuzz {
up: bool,
Expand All @@ -25,11 +27,11 @@ impl Fuzz {

fn run_dir<const UP: bool>(self) {
match self.min_align {
MinAlign::A1 => self.run_dir_align::<UP, 1>(),
MinAlign::A2 => self.run_dir_align::<UP, 2>(),
MinAlign::A3 => self.run_dir_align::<UP, 4>(),
MinAlign::A4 => self.run_dir_align::<UP, 8>(),
MinAlign::A5 => self.run_dir_align::<UP, 16>(),
MinAlign::Shl0 => self.run_dir_align::<UP, 1>(),
MinAlign::Shl1 => self.run_dir_align::<UP, 2>(),
MinAlign::Shl2 => self.run_dir_align::<UP, 4>(),
MinAlign::Shl3 => self.run_dir_align::<UP, 8>(),
MinAlign::Shl4 => self.run_dir_align::<UP, 16>(),
}
}

Expand Down Expand Up @@ -99,28 +101,6 @@ impl_drop! {
t1 t2 t3 t4 t5 t6
}

#[derive(Debug, Clone, Copy, Arbitrary)]
enum MinAlign {
A1 = 1,
A2 = 2,
A3 = 4,
A4 = 8,
A5 = 16,
}

#[derive(Debug, Clone, Copy)]
struct UpTo<const MAX: usize>(usize);

impl<'a, const MAX: usize> Arbitrary<'a> for UpTo<MAX> {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self(u.int_in_range(0..=MAX)?))
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
<usize as Arbitrary>::size_hint(depth)
}
}

#[derive(Clone, Copy)]
struct Range {
start: usize,
Expand Down
24 changes: 23 additions & 1 deletion crates/fuzzing-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![feature(pointer_is_aligned_to, strict_provenance, allocator_api)]
#![allow(clippy::cargo_common_metadata)]

use arbitrary::Arbitrary;
use arbitrary::{Arbitrary, Unstructured};
use bump_scope::allocator_api2::alloc::{AllocError, Allocator};
use std::{alloc::Layout, cell::Cell, mem::swap, ops::Deref, ptr::NonNull, rc::Rc};

Expand Down Expand Up @@ -325,3 +325,25 @@ fn align(addr: usize) -> usize {
addr
}
}

#[derive(Debug, Clone, Copy, Arbitrary)]
enum MinAlign {
Shl0 = 1 << 0,
Shl1 = 1 << 1,
Shl2 = 1 << 2,
Shl3 = 1 << 3,
Shl4 = 1 << 4,
}

#[derive(Debug, Clone, Copy)]
struct UpTo<const MAX: usize>(usize);

impl<'a, const MAX: usize> Arbitrary<'a> for UpTo<MAX> {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(Self(u.int_in_range(0..=MAX)?))
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
<usize as Arbitrary>::size_hint(depth)
}
}
21 changes: 7 additions & 14 deletions crates/fuzzing-support/src/many_vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use arbitrary::Arbitrary;
use bump_scope::{allocator_api2::alloc::Global, Bump, BumpVec, MinimumAlignment, SupportedMinimumAlignment};
use zerocopy::{FromBytes, Immutable, IntoBytes};

use crate::MinAlign;

impl Fuzz {
pub fn run(self) {
if self.up {
Expand All @@ -13,11 +15,11 @@ impl Fuzz {

fn run_dir<const UP: bool>(self) {
match self.min_align {
MinAlign::A1 => self.run_dir_align::<UP, 1>(),
MinAlign::A2 => self.run_dir_align::<UP, 2>(),
MinAlign::A3 => self.run_dir_align::<UP, 4>(),
MinAlign::A4 => self.run_dir_align::<UP, 8>(),
MinAlign::A5 => self.run_dir_align::<UP, 16>(),
MinAlign::Shl0 => self.run_dir_align::<UP, 1>(),
MinAlign::Shl1 => self.run_dir_align::<UP, 2>(),
MinAlign::Shl2 => self.run_dir_align::<UP, 4>(),
MinAlign::Shl3 => self.run_dir_align::<UP, 8>(),
MinAlign::Shl4 => self.run_dir_align::<UP, 16>(),
}
}

Expand Down Expand Up @@ -157,15 +159,6 @@ enum Align {
T6 = 1 << 5,
}

#[derive(Debug, Clone, Copy, Arbitrary)]
enum MinAlign {
A1 = 1,
A2 = 2,
A3 = 4,
A4 = 8,
A5 = 16,
}

#[repr(transparent)]
#[derive(Clone, Default, IntoBytes, FromBytes, Immutable)]
#[allow(dead_code)]
Expand Down

0 comments on commit f4dd841

Please sign in to comment.