From 65724c28d4b119eafd83351e8090edd9a03ebfa6 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Thu, 18 May 2023 11:00:24 -0400 Subject: [PATCH] fuzz: add logging support to the fuzzer 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. --- fuzz/Cargo.toml | 11 +++++++++-- fuzz/fuzz_targets/ast_fuzz_match.rs | 2 ++ fuzz/fuzz_targets/ast_fuzz_match_bytes.rs | 2 ++ fuzz/fuzz_targets/ast_fuzz_regex.rs | 2 ++ fuzz/fuzz_targets/ast_roundtrip.rs | 2 ++ .../fuzz_regex_automata_deserialize_dense_dfa.rs | 2 ++ .../fuzz_regex_automata_deserialize_sparse_dfa.rs | 2 ++ fuzz/fuzz_targets/fuzz_regex_lite_match.rs | 2 ++ fuzz/fuzz_targets/fuzz_regex_match.rs | 2 ++ 9 files changed, 25 insertions(+), 2 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 3fd2b8a3a..8688e73e0 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -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 = ["."] diff --git a/fuzz/fuzz_targets/ast_fuzz_match.rs b/fuzz/fuzz_targets/ast_fuzz_match.rs index 4565244cc..593ab193e 100644 --- a/fuzz/fuzz_targets/ast_fuzz_match.rs +++ b/fuzz/fuzz_targets/ast_fuzz_match.rs @@ -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 diff --git a/fuzz/fuzz_targets/ast_fuzz_match_bytes.rs b/fuzz/fuzz_targets/ast_fuzz_match_bytes.rs index 3f586d04f..8e7c879a5 100644 --- a/fuzz/fuzz_targets/ast_fuzz_match_bytes.rs +++ b/fuzz/fuzz_targets/ast_fuzz_match_bytes.rs @@ -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 diff --git a/fuzz/fuzz_targets/ast_fuzz_regex.rs b/fuzz/fuzz_targets/ast_fuzz_regex.rs index c4ba7e0a9..1257235a9 100644 --- a/fuzz/fuzz_targets/ast_fuzz_regex.rs +++ b/fuzz/fuzz_targets/ast_fuzz_regex.rs @@ -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(); }); diff --git a/fuzz/fuzz_targets/ast_roundtrip.rs b/fuzz/fuzz_targets/ast_roundtrip.rs index 90e6b5099..1b73cea56 100644 --- a/fuzz/fuzz_targets/ast_roundtrip.rs +++ b/fuzz/fuzz_targets/ast_roundtrip.rs @@ -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; diff --git a/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_dense_dfa.rs b/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_dense_dfa.rs index 88f94082b..9acf3fbd0 100644 --- a/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_dense_dfa.rs +++ b/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_dense_dfa.rs @@ -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; } diff --git a/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_sparse_dfa.rs b/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_sparse_dfa.rs index e70b5156b..c0470acd6 100644 --- a/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_sparse_dfa.rs +++ b/fuzz/fuzz_targets/fuzz_regex_automata_deserialize_sparse_dfa.rs @@ -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; } diff --git a/fuzz/fuzz_targets/fuzz_regex_lite_match.rs b/fuzz/fuzz_targets/fuzz_regex_lite_match.rs index 7dfead57f..5786d1324 100644 --- a/fuzz/fuzz_targets/fuzz_regex_lite_match.rs +++ b/fuzz/fuzz_targets/fuzz_regex_lite_match.rs @@ -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) diff --git a/fuzz/fuzz_targets/fuzz_regex_match.rs b/fuzz/fuzz_targets/fuzz_regex_match.rs index 1ce3e57c2..073776776 100644 --- a/fuzz/fuzz_targets/fuzz_regex_match.rs +++ b/fuzz/fuzz_targets/fuzz_regex_match.rs @@ -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)