-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
390 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub use nom; | |
|
||
pub mod ast; | ||
pub mod parser; | ||
pub mod snippet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use annotate_snippets::display_list::DisplayList; | ||
use antinom::rng::AntiNomRandRng; | ||
use itertools::Itertools; | ||
use rand_chacha::rand_core::SeedableRng; | ||
use rstest::rstest; | ||
|
||
use crate::{ast::GraphDefinitionSignature, snippet::snippet_for_input_and_part}; | ||
|
||
use super::{fuzzer, SpannedResult}; | ||
|
||
const FUZZ_ITERATIONS: usize = 1000; | ||
|
||
macro_rules! fuzz_test { | ||
($seed: expr, $fuzzer:path, $parser:path) => { | ||
for _ in 0..FUZZ_ITERATIONS { | ||
let mut rng = AntiNomRandRng { | ||
rng: if let Some(seed) = $seed { | ||
rand_chacha::ChaCha8Rng::from_seed(seed) | ||
} else { | ||
rand_chacha::ChaCha8Rng::from_entropy() | ||
}, | ||
}; | ||
println!( | ||
"Seed: [{}]", | ||
rng.rng | ||
.get_seed() | ||
.iter() | ||
.map(|b| format!("0x{:02x}", b)) | ||
.join(", ") | ||
); | ||
let mut dtpt = String::new(); | ||
$fuzzer(&mut rng, &mut dtpt); | ||
if let Err(err) = $parser(&dtpt) { | ||
let error = match &err { | ||
nom::Err::Incomplete(_) => "incomplete".into(), | ||
nom::Err::Error(err) | nom::Err::Failure(err) => err.kind.description(), | ||
}; | ||
let part = match &err { | ||
nom::Err::Incomplete(_) => &dtpt, | ||
nom::Err::Error(err) | nom::Err::Failure(err) => err.span, | ||
}; | ||
eprintln!( | ||
"{}", | ||
DisplayList::from(snippet_for_input_and_part(&error, &dtpt, part)) | ||
); | ||
panic!("parse error {}", error); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
#[rstest] | ||
#[case(crate::parser::graph_definition_signature, None)] | ||
fn fuzz_graph_definition_signature<F>(#[case] parser: F, #[case] seed: Option<[u8; 32]>) | ||
where | ||
F: Fn(&str) -> SpannedResult<&str, GraphDefinitionSignature>, | ||
{ | ||
fuzz_test!(seed, fuzzer::graph_definition_signature, parser); | ||
} | ||
|
||
#[rstest] | ||
#[case(crate::parser::identifier, None)] | ||
fn fuzz_identifier<F>(#[case] parser: F, #[case] seed: Option<[u8; 32]>) | ||
where | ||
F: Fn(&str) -> SpannedResult<&str, &str>, | ||
{ | ||
fuzz_test!(seed, fuzzer::identifier, parser); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
use antinom::{ | ||
branch::alt, | ||
bytes::complete::tag, | ||
character::complete::{alpha1, alphanumeric1, multispace0}, | ||
combinator::{cut, opt, recognize}, | ||
multi::{many0, separated_list0}, | ||
rng::AntiNomRng, | ||
sequence::{delimited, pair, preceded, terminated, tuple}, | ||
Buffer, Generator, | ||
}; | ||
|
||
const MAX_FILTER_STREAMS: u8 = 5; | ||
const MAX_IDENTIFIER_FRAGMENT_LENGTH: u8 = 3; | ||
const MAX_IDENTIFIER_FRAGMENTS: u8 = 3; | ||
const MAX_PARAMS: u8 = 5; | ||
const MAX_SPACES: u8 = 3; | ||
|
||
pub fn graph_definition_signature<R>(rng: &mut R, buffer: &mut String) | ||
where | ||
R: AntiNomRng, | ||
{ | ||
tuple(( | ||
opt_streams0, | ||
ps(identifier), | ||
cut(pair(ps(params), ps(opt_streams0))), | ||
)) | ||
.gen(rng, buffer) | ||
} | ||
|
||
pub fn params<R>(rng: &mut R, buffer: &mut String) | ||
where | ||
R: AntiNomRng, | ||
{ | ||
delimited( | ||
token("("), | ||
ps(separated_list0(token(","), ds(identifier), MAX_PARAMS)), | ||
token(")"), | ||
) | ||
.gen(rng, buffer) | ||
} | ||
|
||
pub fn opt_streams0<R>(rng: &mut R, buffer: &mut String) | ||
where | ||
R: AntiNomRng, | ||
{ | ||
opt(preceded(token("["), cut(opened_streams0))).gen(rng, buffer) | ||
} | ||
|
||
pub fn opened_streams0<R>(rng: &mut R, buffer: &mut String) | ||
where | ||
R: AntiNomRng, | ||
{ | ||
terminated( | ||
ps(separated_list0( | ||
token(","), | ||
ds(identifier), | ||
MAX_FILTER_STREAMS, | ||
)), | ||
token("]"), | ||
) | ||
.gen(rng, buffer) | ||
} | ||
|
||
pub fn identifier<R>(rng: &mut R, buffer: &mut String) | ||
where | ||
R: AntiNomRng, | ||
{ | ||
recognize(tuple(( | ||
alt((alpha1(MAX_IDENTIFIER_FRAGMENT_LENGTH), tag("_"))), | ||
many0( | ||
alt((alphanumeric1(MAX_IDENTIFIER_FRAGMENT_LENGTH), tag("_"))), | ||
MAX_IDENTIFIER_FRAGMENTS - 1, | ||
), | ||
))) | ||
.gen(rng, buffer) | ||
} | ||
|
||
fn token<R, B, T>(token: T) -> impl Generator<R, B> | ||
where | ||
R: AntiNomRng, | ||
B: Buffer, | ||
T: AsRef<B::Slice> + Clone, | ||
{ | ||
tag(token) | ||
} | ||
|
||
fn ds<R, B, F>(f: F) -> impl Generator<R, B> | ||
where | ||
R: AntiNomRng, | ||
B: Buffer, | ||
B::Char: From<u8>, | ||
F: Generator<R, B>, | ||
{ | ||
delimited(multispace0(MAX_SPACES), f, multispace0(MAX_SPACES)) | ||
} | ||
|
||
fn ps<R, B, F>(f: F) -> impl Generator<R, B> | ||
where | ||
R: AntiNomRng, | ||
B: Buffer, | ||
B::Char: From<u8>, | ||
F: Generator<R, B>, | ||
{ | ||
preceded(multispace0(MAX_SPACES), f) | ||
} |
Oops, something went wrong.