From 0d884a65d5a9f01f932bb0060b4f6716238b5148 Mon Sep 17 00:00:00 2001 From: Noah Hellman Date: Sun, 12 Mar 2023 20:03:58 +0100 Subject: [PATCH] afl: add debug feature leave out debug prints when actually fuzzing to increase fuzz performance --- Makefile | 4 ++-- tests/afl/Cargo.toml | 4 ++++ tests/afl/src/lib.rs | 24 ++++++++++++++++++++++-- 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index afc8818..7bb9cb2 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,7 @@ AFL_TARGET_CRASH?=crashes afl: rm -rf tests/afl/out (cd tests/afl && \ - cargo afl build --release --config profile.release.debug-assertions=true && \ + cargo afl build --no-default-features --release --config profile.release.debug-assertions=true && \ (AFL_NO_UI=1 cargo afl fuzz -i in -o out -Mm target/release/${AFL_TARGET} &) && \ for i in $$(seq $$((${AFL_JOBS} - 1))); do \ AFL_NO_UI=1 cargo afl fuzz -i in -o out -Ss$$i target/release/${AFL_TARGET} & \ @@ -71,7 +71,7 @@ afl: afl_quick: rm -rf tests/afl/out (cd tests/afl && \ - cargo afl build --release --config profile.release.debug-assertions=true && \ + cargo afl build --no-default-features --release --config profile.release.debug-assertions=true && \ AFL_NO_UI=1 AFL_BENCH_UNTIL_CRASH=1 \ cargo afl fuzz -i in -o out -V 60 target/release/${AFL_TARGET}) diff --git a/tests/afl/Cargo.toml b/tests/afl/Cargo.toml index 6b92727..70e156b 100644 --- a/tests/afl/Cargo.toml +++ b/tests/afl/Cargo.toml @@ -20,3 +20,7 @@ path = "src/parse.rs" [[bin]] name = "html" path = "src/html.rs" + +[features] +default = ["debug"] +debug = [] diff --git a/tests/afl/src/lib.rs b/tests/afl/src/lib.rs index 694720b..530a6ae 100644 --- a/tests/afl/src/lib.rs +++ b/tests/afl/src/lib.rs @@ -26,13 +26,18 @@ pub fn html(data: &[u8]) { } fn validate_html(html: &str) { + #[cfg(feature = "debug")] let mut has_error = false; html5ever::parse_document( Dom { names: Vec::new(), + #[cfg(feature = "debug")] has_error: &mut has_error, + #[cfg(feature = "debug")] line_no: 1, + #[cfg(not(feature = "debug"))] + _lifetime: std::marker::PhantomData, }, html5ever::ParseOpts { tokenizer: tokenizer::TokenizerOpts { @@ -50,6 +55,7 @@ fn validate_html(html: &str) { .read_from(&mut std::io::Cursor::new(html)) .unwrap(); + #[cfg(feature = "debug")] if has_error { eprintln!("html:"); html.split('\n').enumerate().for_each(|(i, l)| { @@ -62,8 +68,12 @@ fn validate_html(html: &str) { struct Dom<'a> { names: Vec, + #[cfg(feature = "debug")] has_error: &'a mut bool, + #[cfg(feature = "debug")] line_no: u64, + #[cfg(not(feature = "debug"))] + _lifetime: std::marker::PhantomData<&'a ()>, } impl<'a> tree_builder::TreeSink for Dom<'a> { @@ -110,16 +120,26 @@ impl<'a> tree_builder::TreeSink for Dom<'a> { "Unexpected open element while closing", ]; if !whitelist.iter().any(|e| msg.starts_with(e)) { - *self.has_error = true; - eprintln!("{}: {}\n", self.line_no, msg); + #[cfg(feature = "debug")] + { + *self.has_error = true; + eprintln!("{}: {}\n", self.line_no, msg); + } + #[cfg(not(feature = "debug"))] + { + panic!("invalid html"); + } } } fn set_quirks_mode(&mut self, _: tree_builder::QuirksMode) {} + #[cfg(feature = "debug")] fn set_current_line(&mut self, l: u64) { self.line_no = l; } + #[cfg(not(feature = "debug"))] + fn set_current_line(&mut self, _: u64) {} fn append(&mut self, _: &usize, _: tree_builder::NodeOrText) {} fn append_before_sibling(&mut self, _: &usize, _: tree_builder::NodeOrText) {}