-
Notifications
You must be signed in to change notification settings - Fork 446
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
1 parent
a91d953
commit b0fcd6f
Showing
2 changed files
with
69 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#![no_main] | ||
|
||
use { | ||
libfuzzer_sys::{fuzz_target, Corpus}, | ||
regex::RegexBuilder, | ||
regex_automata::nfa::thompson::pikevm::PikeVM as NfaRegex, | ||
regex_syntax::ast::Ast, | ||
}; | ||
|
||
#[derive(Eq, PartialEq, arbitrary::Arbitrary)] | ||
struct FuzzData { | ||
ast: Ast, | ||
haystack: String, | ||
} | ||
|
||
impl std::fmt::Debug for FuzzData { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
let mut builder = f.debug_struct("FuzzData"); | ||
builder.field("ast", &format!("{}", self.ast)); | ||
builder.field("haystack", &self.haystack); | ||
builder.finish() | ||
} | ||
} | ||
|
||
fuzz_target!(|data: FuzzData| -> Corpus { | ||
let _ = env_logger::try_init(); | ||
|
||
let pattern = format!("{}", data.ast); | ||
let Ok(re) = RegexBuilder::new(&pattern).size_limit(1<<20).build() else { | ||
return Corpus::Reject; | ||
}; | ||
let Ok(baseline) = NfaRegex::new(&pattern) else { | ||
return Corpus::Reject; // should we error here? | ||
}; | ||
let mut cache = baseline.create_cache(); | ||
|
||
assert_eq!( | ||
re.is_match(&data.haystack), | ||
baseline.is_match(&mut cache, &data.haystack) | ||
); | ||
let found1 = re.find(&data.haystack); | ||
let found2 = baseline.find(&mut cache, &data.haystack); | ||
if let Some(found1) = found1 { | ||
let found2 = found2.expect("Found in target, but not in baseline!"); | ||
assert_eq!(found1.start(), found2.start()); | ||
assert_eq!(found1.end(), found2.end()); | ||
} | ||
if let Some(captures) = re.captures(&data.haystack) { | ||
let mut baseline_captures = baseline.create_captures(); | ||
|
||
baseline.captures(&mut cache, &data.haystack, &mut baseline_captures); | ||
drop(cache); | ||
assert_eq!(captures.len(), baseline_captures.group_len()); | ||
for (c1, c2) in captures.iter().zip(baseline_captures.iter()) { | ||
if let Some(c1) = c1 { | ||
let c2 = c2.expect("Matched in target, but not baseline!"); | ||
assert_eq!(c1.start(), c2.start); | ||
assert_eq!(c1.end(), c2.end); | ||
} else { | ||
assert!(!c2.is_some(), "Matched in baseline, but not target!"); | ||
} | ||
} | ||
} | ||
Corpus::Keep | ||
}); |