Skip to content

Commit

Permalink
fuzz: add logging support to the fuzzer
Browse files Browse the repository at this point in the history
This makes it a little easier to introspect what the regex crate is
doing. Just pass RUST_LOG=debug to get a general sense of things, and
RUST_LOG=trace to get a lot more output.
  • Loading branch information
BurntSushi committed Jul 5, 2023
1 parent 72e9f20 commit ad9ba18
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 2 deletions.
11 changes: 9 additions & 2 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,18 @@ cargo-fuzz = true
[dependencies]
arbitrary = { version = "1.3.0", features = ["derive"] }
libfuzzer-sys = { version = "0.4.1", features = ["arbitrary-derive"] }
regex = { path = ".." }
regex-automata = { path = "../regex-automata" }
regex = { path = "..", features = ["logging"] }
regex-automata = { path = "../regex-automata", features = ["logging"] }
regex-lite = { path = "../regex-lite" }
regex-syntax = { path = "../regex-syntax", features = ["arbitrary"] }

[dependencies.env_logger]
# Note that this is currently using an older version because of the dependency
# tree explosion that happened in 0.10.
version = "0.9.3"
default-features = false
features = ["atty", "humantime", "termcolor"]

# Prevent this from interfering with workspaces
[workspace]
members = ["."]
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/ast_fuzz_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ impl std::fmt::Debug for FuzzData {
}

fuzz_target!(|data: FuzzData| {
let _ = env_logger::try_init();

let pattern = format!("{}", data.ast);
let Ok(re) = RegexBuilder::new(&pattern).size_limit(1<<20).build() else {
return
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/ast_fuzz_match_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ impl std::fmt::Debug for FuzzData {
}

fuzz_target!(|data: FuzzData| {
let _ = env_logger::try_init();

let pattern = format!("{}", data.ast);
let Ok(re) = RegexBuilder::new(&pattern).size_limit(1<<20).build() else {
return
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/ast_fuzz_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ impl std::fmt::Debug for FuzzData {
}

fuzz_target!(|data: FuzzData| {
let _ = env_logger::try_init();

let pattern = format!("{}", data.ast);
RegexBuilder::new(&pattern).size_limit(1 << 20).build().ok();
});
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/ast_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ impl Visitor for VerboseVisitor {
}

fuzz_target!(|data: FuzzData| -> Corpus {
let _ = env_logger::try_init();

let pattern = format!("{}", data.ast);
let Ok(ast) = Parser::new().parse(&pattern) else {
return Corpus::Keep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fuzz_target!(|data: &[u8]| {
fn run(given_data: &[u8]) -> Option<()> {
use regex_automata::dfa::Automaton;

let _ = env_logger::try_init();

if given_data.len() < 2 {
return None;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ fuzz_target!(|data: &[u8]| {
fn run(given_data: &[u8]) -> Option<()> {
use regex_automata::dfa::Automaton;

let _ = env_logger::try_init();

if given_data.len() < 2 {
return None;
}
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/fuzz_regex_lite_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ re.is_match({haystack:?});
}

fuzz_target!(|case: FuzzCase| {
let _ = env_logger::try_init();

let Ok(re) = regex_lite::RegexBuilder::new(case.pattern)
.case_insensitive(case.case_insensitive)
.multi_line(case.multi_line)
Expand Down
2 changes: 2 additions & 0 deletions fuzz/fuzz_targets/fuzz_regex_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ re.is_match({haystack:?});
}

fuzz_target!(|case: FuzzCase| {
let _ = env_logger::try_init();

let Ok(re) = regex::RegexBuilder::new(case.pattern)
.case_insensitive(case.case_insensitive)
.multi_line(case.multi_line)
Expand Down

0 comments on commit ad9ba18

Please sign in to comment.