-
Notifications
You must be signed in to change notification settings - Fork 421
/
ast_diff_default.rs
65 lines (58 loc) · 2.09 KB
/
ast_diff_default.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
});