Skip to content

Commit

Permalink
WIP roll our own fuzz
Browse files Browse the repository at this point in the history
  • Loading branch information
nikomatsakis committed Jul 4, 2024
1 parent b83b946 commit 1c2b0a3
Show file tree
Hide file tree
Showing 9 changed files with 749 additions and 234 deletions.
52 changes: 32 additions & 20 deletions crates/formality-core/src/binder/fuzz.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use bolero::{TypeGenerator, ValueGenerator};

use crate::{fold::CoreFold, fuzz::PushGuard, language::Language, variable::CoreBoundVar};
use crate::{fold::CoreFold, fuzz::Fuzzable, language::Language, variable::CoreBoundVar};

use super::{fresh_bound_var, CoreBinder};

Expand All @@ -9,16 +7,24 @@ pub struct KindVec<L: Language> {
pub vec: Vec<L::Kind>,
}

impl<L: Language> bolero::TypeGenerator for KindVec<L>
impl<L: Language> Fuzzable for KindVec<L>
where
L::Kind: TypeGenerator,
L::Kind: Fuzzable,
{
fn generate<D: bolero::Driver>(driver: &mut D) -> Option<Self> {
let num_kinds: usize = L::fuzz_binder_range().get().generate(driver)?;
Some(KindVec {
vec: (0..num_kinds)
.map(|_| driver.gen())
.collect::<Option<_>>()?,
fn estimate_cardinality(cx: &mut crate::fuzz::FuzzCx) -> f64 {
cx.enter_estimate_cardinality::<Self>(|guard| {
guard.binder_range().len() as f64 * guard.estimate_cardinality::<L::Kind>()
})
}

fn fuzz(cx: &mut crate::fuzz::FuzzCx) -> Option<Self> {
cx.enter_fuzz::<Self>(|guard| {
let binder_range = guard.binder_range().clone();
let len = guard.fuzz_usize(binder_range)?;
let vec = (0..len)
.map(|_| guard.fuzz::<L::Kind>())
.collect::<Option<_>>()?;
Some(KindVec { vec })
})
}
}
Expand Down Expand Up @@ -66,16 +72,22 @@ impl<L: Language> PushKindGuard<L> {
}
}

impl<L: Language, T> bolero::TypeGenerator for CoreBinder<L, T>
impl<L: Language, T> Fuzzable for CoreBinder<L, T>
where
T: bolero::TypeGenerator + CoreFold<L>,
L::Kind: bolero::TypeGenerator,
T: Fuzzable + CoreFold<L>,
{
/// Generate a binder with some fresh data inside.
fn generate<D: bolero::Driver>(driver: &mut D) -> Option<Self> {
let kinds: KindVec<L> = driver.gen()?;
let guard = L::open_fuzz_binder(&kinds.vec);
let bound_term: T = driver.gen()?;
Some(guard.into_binder(bound_term))
fn estimate_cardinality(cx: &mut crate::fuzz::FuzzCx) -> f64 {
cx.enter_estimate_cardinality::<Self>(|guard| {
guard.estimate_cardinality::<KindVec<L>>() * guard.estimate_cardinality::<T>()
})
}

fn fuzz(cx: &mut crate::fuzz::FuzzCx) -> Option<Self> {
cx.enter_fuzz::<Self>(|guard| {
let kinds = guard.fuzz::<KindVec<L>>()?;
let guard = L::open_fuzz_binder(&kinds.vec);
let bound_term = guard.fuzz::<T>()?;
Some(guard.into_binder(bound_term))
})
}
}
Loading

0 comments on commit 1c2b0a3

Please sign in to comment.