Skip to content

Commit

Permalink
test: improve greedy fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
bluurryy committed Sep 3, 2024
1 parent 9b8ff03 commit 9680036
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 8 deletions.
4 changes: 2 additions & 2 deletions crates/fuzzing-support/src/bump_greedy_down.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use arbitrary::Arbitrary;

use crate::{from_bump_scope, FuzzBumpProps};
use crate::{from_bump_scope, FuzzBumpGreedyProps};

#[derive(Debug, Arbitrary)]
pub struct Fuzz {
props: FuzzBumpProps,
props: FuzzBumpGreedyProps,
}

impl Fuzz {
Expand Down
4 changes: 2 additions & 2 deletions crates/fuzzing-support/src/bump_greedy_up.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use arbitrary::Arbitrary;

use crate::{from_bump_scope, FuzzBumpProps};
use crate::{from_bump_scope, FuzzBumpGreedyProps};

#[derive(Debug, Arbitrary)]
pub struct Fuzz {
props: FuzzBumpProps,
props: FuzzBumpGreedyProps,
}

impl Fuzz {
Expand Down
6 changes: 4 additions & 2 deletions crates/fuzzing-support/src/from_bump_scope/bumping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ pub(crate) fn bump_greedy_up(
min_align,
align_is_const,
size_is_const: _,
size_is_multiple_of_align: _,
size_is_multiple_of_align,
}: BumpProps,
) -> Option<Range<usize>> {
debug_assert!(size_is_multiple_of_align);
debug_assert!(start <= end);
debug_assert!(end % MIN_CHUNK_ALIGN == 0);

Expand Down Expand Up @@ -281,9 +282,10 @@ pub(crate) fn bump_greedy_down(
min_align,
align_is_const,
size_is_const: _,
size_is_multiple_of_align: _,
size_is_multiple_of_align,
}: BumpProps,
) -> Option<Range<usize>> {
debug_assert!(size_is_multiple_of_align);
debug_assert!(start <= end);

if align_is_const && layout.align() <= min_align {
Expand Down
77 changes: 77 additions & 0 deletions crates/fuzzing-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,83 @@ impl FuzzBumpProps {
}
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct FuzzBumpGreedyProps {
pub(crate) start: usize,
pub(crate) end: usize,
pub(crate) layout: Layout,
pub(crate) min_align: usize,
pub(crate) align_is_const: bool,
}

impl<'a> Arbitrary<'a> for FuzzBumpGreedyProps {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let (mut start, mut end) = u.arbitrary()?;

if end < start {
swap(&mut start, &mut end);
}

start = align(start);
end = align(end);

let layout = {
let size: usize = u.arbitrary()?;
let align_pow2 = u.int_in_range(0..=10)?;
let align = 1 << align_pow2;
Layout::from_size_align(
size.checked_mul(align).ok_or_else(|| arbitrary::Error::IncorrectFormat)?,
align,
)
.map_err(|_| arbitrary::Error::IncorrectFormat)?
};

let min_align = *u.choose(&[1, 2, 4, 8, 16])?;

Ok(Self {
start,
end,
layout,
min_align,
align_is_const: u.arbitrary()?,
})
}
}

impl FuzzBumpGreedyProps {
fn for_up(mut self) -> Self {
self.start = down_align(self.start, self.min_align);
self
}

fn for_down(mut self) -> Self {
self.end = down_align(self.end, self.min_align);
self
}
}

impl FuzzBumpGreedyProps {
fn to(self) -> from_bump_scope::bumping::BumpProps {
let Self {
start,
end,
layout,
min_align,
align_is_const,
} = self;

from_bump_scope::bumping::BumpProps {
start,
end,
layout,
min_align,
align_is_const,
size_is_const: false,
size_is_multiple_of_align: layout.size() % layout.align() == 0,
}
}
}

#[inline(always)]
fn down_align(addr: usize, align: usize) -> usize {
debug_assert!(align.is_power_of_two());
Expand Down
6 changes: 4 additions & 2 deletions src/bumping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,10 @@ pub(crate) fn bump_greedy_up(
min_align,
align_is_const,
size_is_const: _,
size_is_multiple_of_align: _,
size_is_multiple_of_align,
}: BumpProps,
) -> Option<Range<usize>> {
debug_assert!(size_is_multiple_of_align);
debug_assert!(start <= end);
debug_assert!(end % MIN_CHUNK_ALIGN == 0);

Expand Down Expand Up @@ -281,9 +282,10 @@ pub(crate) fn bump_greedy_down(
min_align,
align_is_const,
size_is_const: _,
size_is_multiple_of_align: _,
size_is_multiple_of_align,
}: BumpProps,
) -> Option<Range<usize>> {
debug_assert!(size_is_multiple_of_align);
debug_assert!(start <= end);

if align_is_const && layout.align() <= min_align {
Expand Down
1 change: 1 addition & 0 deletions src/chunk_raw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ impl<const UP: bool, A> RawChunk<UP, A> {
///
/// - `range.start` and `range.end` are aligned.
/// - `layout.size` must not be zero
/// - `layout.size` must be a multiple of `layout.align`
///
/// [`MutBumpVec`]: crate::MutBumpVec
/// [`into_slice`]: crate::MutBumpVec::into_slice
Expand Down

0 comments on commit 9680036

Please sign in to comment.