Skip to content

Commit 53997c3

Browse files
committed
Merge pull request #23 from 'feat/add-fuzz-harness'
test: add code harness to fuzz with AFL, libFuzz and honggfuzz
2 parents 571cc60 + bc56ce7 commit 53997c3

9 files changed

Lines changed: 126 additions & 5 deletions

File tree

.github/workflows/coverage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ jobs:
3030
- name: Make coverage directory
3131
run: mkdir coverage
3232
- name: Test and report coverage
33-
run: cargo +nightly llvm-cov -q --doctests --branch --all --ignore-filename-regex "example-crates/*" --all-features --lcov --output-path ./coverage/lcov.info
33+
run: cargo +nightly llvm-cov -q --doctests --branch --all --exclude sp_fuzz --ignore-filename-regex "example-crates/*" --all-features --lcov --output-path ./coverage/lcov.info
3434
- name: Generate HTML coverage report
3535
run: genhtml -o coverage-report.html --ignore-errors unmapped ./coverage/lcov.info
3636
- name: Coveralls upload

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
resolver = "2"
33
members = [
44
"silentpayments",
5-
"example-crates/example_silentpayments", "dleq", "indexer",
5+
"example-crates/example_silentpayments", "dleq", "indexer", "fuzz",
6+
]
7+
default-members = [
8+
"silentpayments", "dleq", "indexer",
69
]
710

811
[workspace.package]

fuzz/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
hfuzz_target
2+
hfuzz_workspace
3+
target
4+
corpus
5+
artifacts
6+
# AFL
7+
out

fuzz/Cargo.toml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[package]
2+
name = "sp_fuzz"
3+
version = "0.1.0"
4+
edition = "2024"
5+
publish = false
6+
authors.workspace = true
7+
8+
[features]
9+
afl_fuzz = ["afl"]
10+
honggfuzz_fuzz = ["honggfuzz"]
11+
libfuzzer_fuzz = ["libfuzzer-sys"]
12+
stdin_fuzz = []
13+
14+
[dependencies]
15+
bdk_sp = { version = "0.1.0", path = "../silentpayments" }
16+
afl = { version = "0.12", optional = true }
17+
honggfuzz = { version = "0.5", optional = true, default-features = false }
18+
libfuzzer-sys = { version = "0.4", optional = true }
19+
20+
[lib]
21+
name = "sp_fuzz"
22+
path = "src/lib.rs"
23+
24+
[[bin]]
25+
name = "sp_encoding"
26+
path = "src/bin/sp_code_target.rs"
27+
28+
[lints.rust.unexpected_cfgs]
29+
level = "forbid"
30+
check-cfg = [
31+
"cfg(fuzzing)",
32+
"cfg(secp256k1_fuzz)",
33+
"cfg(hashes_fuzz)",
34+
"cfg(taproot)",
35+
]

fuzz/seeds/seed.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, AFL!

fuzz/src/bin/sp_code_target.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
2+
3+
use bdk_sp::encoding::SilentPaymentCode;
4+
use core::convert::TryFrom;
5+
6+
use sp_fuzz::sp_code::sp_code_parse_run;
7+
8+
#[cfg(feature = "afl")]
9+
#[macro_use]
10+
extern crate afl;
11+
#[cfg(feature = "afl")]
12+
fn main() {
13+
fuzz!(|data| {
14+
sp_code_parse_run(data.as_ptr(), data.len());
15+
});
16+
}
17+
18+
#[cfg(feature = "honggfuzz")]
19+
#[macro_use]
20+
extern crate honggfuzz;
21+
#[cfg(feature = "honggfuzz")]
22+
fn main() {
23+
loop {
24+
fuzz!(|data| {
25+
sp_code_parse_run(data.as_ptr(), data.len());
26+
});
27+
}
28+
}
29+
30+
#[cfg(feature = "libfuzzer_fuzz")]
31+
#[macro_use]
32+
extern crate libfuzzer_sys;
33+
#[cfg(feature = "libfuzzer_fuzz")]
34+
fuzz_target!(|data: &[u8]| {
35+
sp_code_parse_run(data.as_ptr(), data.len());
36+
});
37+
38+
#[cfg(feature = "stdin_fuzz")]
39+
fn main() {
40+
use std::io::Read;
41+
42+
let mut data = Vec::with_capacity(1023);
43+
std::io::stdin().read_to_end(&mut data).unwrap();
44+
sp_code_parse_run(data.as_ptr(), data.len());
45+
}

fuzz/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#[cfg(not(fuzzing))]
2+
compile_error!("Fuzz targets need cfg=fuzzing");
3+
4+
#[cfg(not(hashes_fuzz))]
5+
compile_error!("Fuzz targets need cfg=hashes_fuzz");
6+
7+
#[cfg(not(secp256k1_fuzz))]
8+
compile_error!("Fuzz targets need cfg=secp256k1_fuzz");
9+
10+
pub mod sp_code;

fuzz/src/sp_code.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
use bdk_sp::encoding::SilentPaymentCode;
2+
use core::convert::TryFrom;
3+
4+
#[inline]
5+
pub fn do_test(data: &[u8]) {
6+
if let Ok(received_sp_code) = std::str::from_utf8(data) {
7+
if let Ok(sp_code) = SilentPaymentCode::try_from(received_sp_code) {
8+
let produced_sp_code = sp_code.to_string();
9+
assert_eq!(received_sp_code, produced_sp_code);
10+
}
11+
}
12+
}
13+
pub fn sp_code_parse_test(data: &[u8]) {
14+
do_test(data);
15+
}
16+
17+
#[unsafe(no_mangle)]
18+
pub extern "C" fn sp_code_parse_run(data: *const u8, datalen: usize) {
19+
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
20+
}

justfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ default:
1010

1111
[doc("Build the project")]
1212
build:
13-
cargo build
13+
cargo build --exclude sp_fuzz
1414

1515
[doc("Check code: formatting, compilation, linting, and commit signature")]
1616
check:
1717
cargo +nightly fmt --all -- --check
18-
cargo check --workspace --exclude 'example_*' --all-features
18+
cargo check --workspace --exclude sp_fuzz --exclude 'example_*' --all-features
1919
cargo clippy --all-features --all-targets -- -D warnings
2020
@[ "$(git log --pretty='format:%G?' -1 HEAD)" = "N" ] && \
2121
echo "\n⚠️ Unsigned commit: BDK requires that commits be signed." || \
@@ -27,7 +27,7 @@ fmt:
2727

2828
[doc("Run all tests on the workspace with all features")]
2929
test:
30-
cargo test --workspace --exclude 'example_*' --all-features
30+
cargo test --workspace --exclude sp_fuzz --exclude 'example_*' --all-features
3131

3232
[doc("Run pre-push suite: format, check, and test")]
3333
pre-push: fmt check test

0 commit comments

Comments
 (0)