Skip to content

Commit

Permalink
Deps: Update arbitrary to 1.4.1.
Browse files Browse the repository at this point in the history
Includes implementing the new recommended `try_size_hint()` for
recursive types.
  • Loading branch information
kpreid committed Nov 8, 2024
1 parent 4460348 commit feef26e
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 54 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ allocation-counter = { version = "0.8.1", default-features = false }
# Only used in std-using packages, so default features enabled
anyhow = "1.0.70"
# Each use should be { optional = true }.
arbitrary = { version = "1.3.2", features = ["derive"] }
arbitrary = { version = "1.4.1", features = ["derive"] }
arrayvec = { version = "0.7.4", default-features = false }
async_fn_traits = { version = "0.1.1", default-features = false }
base64 = { version = "0.22.1", default-features = false }
Expand Down
15 changes: 11 additions & 4 deletions all-is-cubes-base/src/math/vol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,15 +741,22 @@ pub(crate) mod vol_arb {
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::recursion_guard(depth, |depth| {
let (lower, upper) = V::size_hint(depth);
arbitrary::size_hint::and(
// recommended impl from trait documentation
Self::try_size_hint(depth).unwrap_or_default()
}

fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
let (lower, upper) = V::try_size_hint(depth)?;
Ok(arbitrary::size_hint::and(
ARBITRARY_BOUNDS_SIZE_HINT,
(
lower.saturating_mul(MAX_VOLUME),
upper.map(|u| u.saturating_mul(MAX_VOLUME)),
),
)
))
})
}
}
Expand Down
23 changes: 15 additions & 8 deletions all-is-cubes/src/block/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,21 @@ impl<'a> arbitrary::Arbitrary<'a> for BlockAttributes {
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and_all(&[
alloc::string::String::size_hint(depth),
bool::size_hint(depth),
InvInBlock::size_hint(depth),
RotationPlacementRule::size_hint(depth),
TickAction::size_hint(depth),
AnimationHint::size_hint(depth),
])
Self::try_size_hint(depth).unwrap_or_default()
}
fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::and_all(&[
alloc::string::String::try_size_hint(depth)?,
bool::try_size_hint(depth)?,
InvInBlock::try_size_hint(depth)?,
RotationPlacementRule::try_size_hint(depth)?,
TickAction::try_size_hint(depth)?,
AnimationHint::try_size_hint(depth)?,
]))
})
}
}

Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/src/block/block_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ impl<'a> arbitrary::Arbitrary<'a> for BlockDef {
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
// We don't need to bother with try_size_hint() because Block short-circuits recursion
Block::size_hint(depth)
}
}
Expand Down
19 changes: 18 additions & 1 deletion all-is-cubes/src/block/eval/evaluated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,12 @@ impl<'a> arbitrary::Arbitrary<'a> for EvaluatedBlock {
fn size_hint(depth: usize) -> (usize, Option<usize>) {
MinEval::size_hint(depth)
}

fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
MinEval::try_size_hint(depth)
}
}

/// The result of <code>[AIR].[evaluate()](Block::evaluate)</code>, as a constant.
Expand Down Expand Up @@ -500,7 +506,18 @@ impl<'a> arbitrary::Arbitrary<'a> for MinEval {
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::and(BlockAttributes::size_hint(depth), Evoxels::size_hint(depth))
Self::try_size_hint(depth).unwrap_or_default()
}

fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::and(
BlockAttributes::try_size_hint(depth)?,
Evoxels::try_size_hint(depth)?,
))
})
}
}

Expand Down
17 changes: 14 additions & 3 deletions all-is-cubes/src/block/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,16 +418,27 @@ impl<'a> arbitrary::Arbitrary<'a> for Text {
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::recursion_guard(depth, |depth| {
arbitrary::size_hint::and_all(&[
// recommended impl from trait documentation
Self::try_size_hint(depth).unwrap_or_default()
}

fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
// Note that `Text` is recursive because `Text` contains `Block` and `Block` contains
// `Text`. However, this will still produce a useful, cheap result because
// `Block::size_hint()` has an explicitly set bound.

arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::and_all(&[
alloc::string::String::size_hint(depth),
Font::size_hint(depth),
Block::size_hint(depth),
Option::<Block>::size_hint(depth),
Resolution::size_hint(depth),
GridAab::size_hint(depth),
bool::size_hint(depth),
])
]))
})
}
}
Expand Down
15 changes: 10 additions & 5 deletions all-is-cubes/src/inv/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,12 +596,17 @@ impl<'a, T: arbitrary::Arbitrary<'a>> arbitrary::Arbitrary<'a> for EphemeralOpaq
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
Self::try_size_hint(depth).unwrap_or_default()
}
fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
use arbitrary::{size_hint, Arbitrary};
size_hint::recursion_guard(depth, |depth| {
size_hint::and(
<usize as Arbitrary>::size_hint(depth),
<T as Arbitrary>::size_hint(depth),
)
size_hint::try_recursion_guard(depth, |depth| {
Ok(size_hint::and(
<bool as Arbitrary>::size_hint(depth),
<T as Arbitrary>::try_size_hint(depth)?,
))
})
}
}
Expand Down
60 changes: 38 additions & 22 deletions all-is-cubes/src/universe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,28 +117,44 @@ impl fmt::Display for Name {

// Manual impl because `ArcStr` doesn't impl Arbitrary.
#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Name {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
Ok(match u.int_in_range::<u8>(0..=2)? {
0 => Name::Specific(u.arbitrary::<String>()?.into()),
1 => Name::Anonym(u.arbitrary()?),
2 => Name::Pending,
_ => unreachable!(),
})
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
use arbitrary::size_hint;
size_hint::recursion_guard(depth, |depth| {
size_hint::and(
u8::size_hint(depth),
size_hint::or_all(&[
String::size_hint(depth),
usize::size_hint(depth),
<()>::size_hint(depth),
]),
)
})
mod impl_arbitrary {
use super::*;
#[derive(arbitrary::Arbitrary)]
enum ArbName {
Specific(String),
Anonym(usize),
Pending,
}

impl<'a> arbitrary::Arbitrary<'a> for Name {
fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
let input = ArbName::arbitrary(u)?;
let value = match input {
ArbName::Specific(name) => Name::Specific(name.into()),
ArbName::Anonym(index) => Name::Anonym(index),
ArbName::Pending => Name::Pending,
};
if false {
// This non-executed code proves ArbName has as many variants as Name
let _ = match value {
Name::Specific(name) => ArbName::Specific(name.to_string()),
Name::Anonym(index) => ArbName::Anonym(index),
Name::Pending => ArbName::Pending,
};
unreachable!()
} else {
Ok(value)
}
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
ArbName::size_hint(depth)
}
fn try_size_hint(
depth: usize,
) -> arbitrary::Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
ArbName::try_size_hint(depth)
}
}
}

Expand Down
18 changes: 13 additions & 5 deletions all-is-cubes/src/universe/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,14 +483,22 @@ impl<'a, T: arbitrary::Arbitrary<'a> + 'static> arbitrary::Arbitrary<'a> for Han
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
arbitrary::size_hint::recursion_guard(depth, |depth| {
arbitrary::size_hint::and(
Self::try_size_hint(depth).unwrap_or_default()
}
fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), arbitrary::MaxRecursionReached> {
arbitrary::size_hint::try_recursion_guard(depth, |depth| {
Ok(arbitrary::size_hint::and(
bool::size_hint(depth),
arbitrary::size_hint::or(
Name::size_hint(depth),
arbitrary::size_hint::and(Name::size_hint(depth), T::size_hint(depth)),
Name::try_size_hint(depth)?,
arbitrary::size_hint::and(
Name::try_size_hint(depth)?,
T::try_size_hint(depth)?,
),
),
)
))
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cargo-fuzz = true
all-is-cubes = { path = "../all-is-cubes", features = ["arbitrary"] }
all-is-cubes-gpu = { path = "../all-is-cubes-gpu" }
all-is-cubes-mesh = { path = "../all-is-cubes-mesh", features = ["arbitrary"] }
arbitrary = { version = "1.3.2", features = ["derive"] }
arbitrary = { version = "1.4.1", features = ["derive"] }
libfuzzer-sys = "0.4"

[patch.crates-io]
Expand Down

0 comments on commit feef26e

Please sign in to comment.