Skip to content

Commit 53664cf

Browse files
committed
fuzz: remove afl feature, clean up fuzztests a bit
Substantially rewrite the `encode_decode` fuzztest, which had some ugly/awkward control flow. For the other tests, remove the AFL feature which has not been used in years and the now-unneeded `extern crate` and `macro_use` calls. Now you can fuzz things by just running `cargo hfuzz` directly without needing to set rustflags etc. Also run shellcheck on fuzz.sh, add a bunch of quotes to it, and change the runtime from 100000 iterations (which seems to run in less than a second on my system for all the existing tests) to a fixed 10 seconds. This does **not** pull the full rust-bitcoin fuzztest scripts over. I would like to wait until the "move fuzztests to cron" PR merges in rust-bitcoin, and consider trying to move all the scripts into a shared repo rather than copy/pasting them in, before doing so.
1 parent 665f864 commit 53664cf

File tree

5 files changed

+36
-81
lines changed

5 files changed

+36
-81
lines changed

fuzz/Cargo.toml

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
[package]
22
name = "bech32-fuzz"
3+
edition = "2021"
4+
rust-version = "1.56.1"
35
version = "0.0.1"
46
authors = ["Automatically generated"]
57
publish = false
68

79
[package.metadata]
810
cargo-fuzz = true
911

10-
[features]
11-
afl_fuzz = ["afl"]
12-
honggfuzz_fuzz = ["honggfuzz"]
13-
1412
[dependencies]
15-
honggfuzz = { version = "0.5", default-features = false, optional = true }
16-
afl = { version = "0.3", optional = true }
17-
libc = "0.2"
13+
honggfuzz = { version = "0.5.55", default-features = false }
1814
bech32 = { path = ".." }
1915

2016
# Prevent this from interfering with workspaces

fuzz/fuzz.sh

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22
set -e
33
cargo install --force honggfuzz --no-default-features
44
for TARGET in fuzz_targets/*; do
5-
FILENAME=$(basename $TARGET)
5+
FILENAME=$(basename "$TARGET")
66
FILE="${FILENAME%.*}"
7-
if [ -d hfuzz_input/$FILE ]; then
7+
if [ -d "hfuzz_input/$FILE" ]; then
88
HFUZZ_INPUT_ARGS="-f hfuzz_input/$FILE/input"
99
fi
10-
HFUZZ_BUILD_ARGS="--features honggfuzz_fuzz" HFUZZ_RUN_ARGS="-N1000000 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run $FILE
10+
HFUZZ_RUN_ARGS="--run_time 10 --exit_upon_crash -v $HFUZZ_INPUT_ARGS" cargo hfuzz run "$FILE"
1111

12-
if [ -f hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT ]; then
13-
cat hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT
14-
for CASE in hfuzz_workspace/$FILE/SIG*; do
15-
cat $CASE | xxd -p
12+
if [ -f "hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT" ]; then
13+
cat "hfuzz_workspace/$FILE/HONGGFUZZ.REPORT.TXT"
14+
for CASE in "hfuzz_workspace/$FILE/SIG"*; do
15+
xxd -p < "$CASE"
1616
done
1717
exit 1
1818
fi

fuzz/fuzz_targets/decode_rnd.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
extern crate bech32;
2-
31
use bech32::primitives::decode::{CheckedHrpstring, SegwitHrpstring, UncheckedHrpstring};
42
use bech32::Bech32m;
3+
use honggfuzz::fuzz;
54

65
// Checks that we do not crash if passed random data while decoding.
76
fn do_test(data: &[u8]) {
@@ -11,19 +10,6 @@ fn do_test(data: &[u8]) {
1110
let _ = SegwitHrpstring::new(&data_str);
1211
}
1312

14-
#[cfg(feature = "afl")]
15-
extern crate afl;
16-
#[cfg(feature = "afl")]
17-
fn main() {
18-
afl::read_stdio_bytes(|data| {
19-
do_test(&data);
20-
});
21-
}
22-
23-
#[cfg(feature = "honggfuzz")]
24-
#[macro_use]
25-
extern crate honggfuzz;
26-
#[cfg(feature = "honggfuzz")]
2713
fn main() {
2814
loop {
2915
fuzz!(|data| {
@@ -39,9 +25,9 @@ mod tests {
3925
for (idx, c) in hex.as_bytes().iter().enumerate() {
4026
b <<= 4;
4127
match *c {
42-
b'A'...b'F' => b |= c - b'A' + 10,
43-
b'a'...b'f' => b |= c - b'a' + 10,
44-
b'0'...b'9' => b |= c - b'0',
28+
b'A'..=b'F' => b |= c - b'A' + 10,
29+
b'a'..=b'f' => b |= c - b'a' + 10,
30+
b'0'..=b'9' => b |= c - b'0',
4531
_ => panic!("Bad hex"),
4632
}
4733
if (idx & 1) == 1 {

fuzz/fuzz_targets/encode_decode.rs

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
extern crate bech32;
2-
31
use std::str;
42

53
use bech32::{Bech32m, Hrp};
4+
use honggfuzz::fuzz;
65

76
fn do_test(data: &[u8]) {
8-
if data.len() < 1 {
7+
if data.is_empty() {
98
return;
109
}
1110

@@ -17,35 +16,23 @@ fn do_test(data: &[u8]) {
1716

1817
let dp = &data[hrp_end..];
1918

20-
match str::from_utf8(&data[1..hrp_end]) {
19+
let s = match str::from_utf8(&data[1..hrp_end]) {
20+
Ok(s) => s,
2121
Err(_) => return,
22-
Ok(s) => {
23-
match Hrp::parse(&s) {
24-
Err(_) => return,
25-
Ok(hrp) => {
26-
if let Ok(address) = bech32::encode::<Bech32m>(hrp, dp) {
27-
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
28-
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
29-
}
30-
}
31-
}
32-
}
33-
}
34-
}
22+
};
23+
let hrp = match Hrp::parse(s) {
24+
Ok(hrp) => hrp,
25+
Err(_) => return,
26+
};
27+
let address = match bech32::encode::<Bech32m>(hrp, dp) {
28+
Ok(addr) => addr,
29+
Err(_) => return,
30+
};
3531

36-
#[cfg(feature = "afl")]
37-
extern crate afl;
38-
#[cfg(feature = "afl")]
39-
fn main() {
40-
afl::read_stdio_bytes(|data| {
41-
do_test(&data);
42-
});
32+
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
33+
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
4334
}
4435

45-
#[cfg(feature = "honggfuzz")]
46-
#[macro_use]
47-
extern crate honggfuzz;
48-
#[cfg(feature = "honggfuzz")]
4936
fn main() {
5037
loop {
5138
fuzz!(|data| {
@@ -61,9 +48,9 @@ mod tests {
6148
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
6249
b <<= 4;
6350
match *c {
64-
b'A'...b'F' => b |= c - b'A' + 10,
65-
b'a'...b'f' => b |= c - b'a' + 10,
66-
b'0'...b'9' => b |= c - b'0',
51+
b'A'..=b'F' => b |= c - b'A' + 10,
52+
b'a'..=b'f' => b |= c - b'a' + 10,
53+
b'0'..=b'9' => b |= c - b'0',
6754
_ => panic!("Bad hex"),
6855
}
6956
if (idx & 1) == 1 {

fuzz/fuzz_targets/parse_hrp.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
extern crate bech32;
2-
31
use bech32::Hrp;
2+
use honggfuzz::fuzz;
43

54
fn do_test(data: &[u8]) {
65
let s = String::from_utf8_lossy(data);
@@ -10,19 +9,6 @@ fn do_test(data: &[u8]) {
109
let _ = Hrp::parse(&s);
1110
}
1211

13-
#[cfg(feature = "afl")]
14-
extern crate afl;
15-
#[cfg(feature = "afl")]
16-
fn main() {
17-
afl::read_stdio_bytes(|data| {
18-
do_test(&data);
19-
});
20-
}
21-
22-
#[cfg(feature = "honggfuzz")]
23-
#[macro_use]
24-
extern crate honggfuzz;
25-
#[cfg(feature = "honggfuzz")]
2612
fn main() {
2713
loop {
2814
fuzz!(|data| {
@@ -38,9 +24,9 @@ mod tests {
3824
for (idx, c) in hex.as_bytes().iter().filter(|&&c| c != b'\n').enumerate() {
3925
b <<= 4;
4026
match *c {
41-
b'A'...b'F' => b |= c - b'A' + 10,
42-
b'a'...b'f' => b |= c - b'a' + 10,
43-
b'0'...b'9' => b |= c - b'0',
27+
b'A'..=b'F' => b |= c - b'A' + 10,
28+
b'a'..=b'f' => b |= c - b'a' + 10,
29+
b'0'..=b'9' => b |= c - b'0',
4430
_ => panic!("Bad hex"),
4531
}
4632
if (idx & 1) == 1 {

0 commit comments

Comments
 (0)