fuzz: disable tests unless 'cfg(fuzzing)' is passed; update README for reproducing failures

This commit is contained in:
Andrew Poelstra 2023-03-22 15:07:50 +00:00
parent 6e2ee5be66
commit 6467728202
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
19 changed files with 70 additions and 18 deletions

View File

@ -65,4 +65,29 @@ If it is working, you will see a rapid stream of data for many seconds
(you can hit Ctrl+C to stop it early). If not, you should quickly see (you can hit Ctrl+C to stop it early). If not, you should quickly see
an error. an error.
# Reproducing Failures
If a fuzztest fails, it will exit with a summary which looks something like
```
...
fuzzTarget : hfuzz_target/x86_64-unknown-linux-gnu/release/hashes_sha256
CRASH:
DESCRIPTION:
ORIG_FNAME: 00000000000000000000000000000000.00000000.honggfuzz.cov
FUZZ_FNAME: hfuzz_workspace/hashes_sha256/SIGABRT.PC.7ffff7c8abc7.STACK.18826d9b64.CODE.-6.ADDR.0.INSTR.mov____%eax,%ebp.fuzz
...
=====================================================================
fff400610004
```
The final line is a hex-encoded version of the input that caused the crash. You
can test this directly by editing the `duplicate_crash` test to copy/paste the
hex output into the call to `extend_vec_from_hex`. Then run the test with
RUSTFLAGS=--cfg=fuzzing cargo test
It is important to add the `cfg=fuzzing` flag, which tells rustc to compile the
library as though it were running a fuzztest. In particular, this will disable
or weaken all the cryptography.

View File

@ -13,7 +13,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -19,7 +19,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -36,7 +36,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -13,7 +13,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -22,7 +22,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -22,7 +22,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -52,7 +52,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -34,7 +34,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -18,7 +18,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -49,7 +49,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -23,3 +23,30 @@ fn main() {
}); });
} }
} }
#[cfg(all(test, fuzzing))]
mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0;
for (idx, c) in hex.as_bytes().iter().enumerate() {
b <<= 4;
match *c {
b'A'..=b'F' => b |= c - b'A' + 10,
b'a'..=b'f' => b |= c - b'a' + 10,
b'0'..=b'9' => b |= c - b'0',
_ => panic!("Bad hex"),
}
if (idx & 1) == 1 {
out.push(b);
b = 0;
}
}
}
#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00000", &mut a);
super::do_test(&a);
}
}

View File

@ -28,7 +28,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -28,7 +28,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -20,7 +20,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -20,7 +20,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -20,7 +20,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;
@ -42,7 +42,7 @@ mod tests {
#[test] #[test]
fn duplicate_crash() { fn duplicate_crash() {
let mut a = Vec::new(); let mut a = Vec::new();
extend_vec_from_hex("00000", &mut a); extend_vec_from_hex("fff400610004", &mut a);
super::do_test(&a); super::do_test(&a);
} }
} }

View File

@ -20,7 +20,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;

View File

@ -20,7 +20,7 @@ fn main() {
} }
} }
#[cfg(test)] #[cfg(all(test, fuzzing))]
mod tests { mod tests {
fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) { fn extend_vec_from_hex(hex: &str, out: &mut Vec<u8>) {
let mut b = 0; let mut b = 0;