Skip to content

Commit 7fd009d

Browse files
committed
Add cashu fuzzing target
1 parent 340da1b commit 7fd009d

File tree

6 files changed

+161
-0
lines changed

6 files changed

+161
-0
lines changed

fuzz/src/bin/cashu_target.rs

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
// This file is auto-generated by gen_target.sh based on target_template.txt
11+
// To modify it, modify target_template.txt and run gen_target.sh instead.
12+
13+
#![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
14+
#![cfg_attr(rustfmt, rustfmt_skip)]
15+
16+
#[cfg(not(fuzzing))]
17+
compile_error!("Fuzz targets need cfg=fuzzing");
18+
19+
#[cfg(not(hashes_fuzz))]
20+
compile_error!("Fuzz targets need cfg=hashes_fuzz");
21+
22+
#[cfg(not(secp256k1_fuzz))]
23+
compile_error!("Fuzz targets need cfg=secp256k1_fuzz");
24+
25+
extern crate fuzz;
26+
use fuzz::cashu::*;
27+
28+
#[cfg(feature = "afl")]
29+
#[macro_use] extern crate afl;
30+
#[cfg(feature = "afl")]
31+
fn main() {
32+
fuzz!(|data| {
33+
cashu_run(data.as_ptr(), data.len());
34+
});
35+
}
36+
37+
#[cfg(feature = "honggfuzz")]
38+
#[macro_use] extern crate honggfuzz;
39+
#[cfg(feature = "honggfuzz")]
40+
fn main() {
41+
loop {
42+
fuzz!(|data| {
43+
cashu_run(data.as_ptr(), data.len());
44+
});
45+
}
46+
}
47+
48+
#[cfg(feature = "libfuzzer_fuzz")]
49+
#[macro_use] extern crate libfuzzer_sys;
50+
#[cfg(feature = "libfuzzer_fuzz")]
51+
fuzz_target!(|data: &[u8]| {
52+
cashu_run(data.as_ptr(), data.len());
53+
});
54+
55+
#[cfg(feature = "stdin_fuzz")]
56+
fn main() {
57+
use std::io::Read;
58+
59+
let mut data = Vec::with_capacity(8192);
60+
std::io::stdin().read_to_end(&mut data).unwrap();
61+
cashu_run(data.as_ptr(), data.len());
62+
}
63+
64+
#[test]
65+
fn run_test_cases() {
66+
use std::fs;
67+
use std::io::Read;
68+
69+
use std::sync::{atomic, Arc};
70+
{
71+
let data: Vec<u8> = vec![0];
72+
cashu_run(data.as_ptr(), data.len());
73+
}
74+
let mut threads = Vec::new();
75+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
76+
if let Ok(tests) = fs::read_dir("test_cases/cashu") {
77+
for test in tests {
78+
let mut data: Vec<u8> = Vec::new();
79+
let path = test.unwrap().path();
80+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
81+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
82+
83+
let thread_count_ref = Arc::clone(&threads_running);
84+
let main_thread_ref = std::thread::current();
85+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
86+
std::thread::spawn(move || {
87+
let res = if ::std::panic::catch_unwind(move || {
88+
cashu_test(&data);
89+
}).is_err() {
90+
Some(String::new())
91+
} else { None };
92+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
93+
main_thread_ref.unpark();
94+
res
95+
})
96+
));
97+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
98+
std::thread::park();
99+
}
100+
}
101+
}
102+
let mut failed_outputs = Vec::new();
103+
for (test, thread) in threads.drain(..) {
104+
if let Some(output) = thread.join().unwrap() {
105+
println!("\nOutput of {}:\n{}\n", test, output);
106+
failed_outputs.push(test);
107+
}
108+
}
109+
if !failed_outputs.is_empty() {
110+
println!("Test cases which failed: ");
111+
for case in failed_outputs {
112+
println!("{}", case);
113+
}
114+
panic!();
115+
}
116+
}

fuzz/src/bin/gen_target.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ GEN_TEST() {
66
echo "void $1_run(const unsigned char* data, size_t data_len);" >> ../../targets.h
77
}
88

9+
GEN_TEST cashu
910
GEN_TEST parse

fuzz/src/cashu.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// This file is Copyright its original authors, visible in version control
2+
// history.
3+
//
4+
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5+
// or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7+
// You may not use this file except in accordance with one or both of these
8+
// licenses.
9+
10+
//! We have quite a bit of logic in our cashu instruction decoder which is hard to reach from the
11+
//! outside because of the bech32 checksum. Instead, here, we fuzz it directly skipping the bech32
12+
//! check.
13+
14+
use bitcoin_payment_instructions::cashu::*;
15+
16+
#[inline]
17+
pub fn do_test(mut data: &[u8]) {
18+
if let Ok(req) = CashuPaymentRequest::from_bytes_fuzzy(data) {
19+
match req.to_bech32_string() {
20+
Ok(reencoded) => {
21+
let re_decoded = CashuPaymentRequest::from_bech32_string(&reencoded).unwrap();
22+
assert_eq!(re_decoded, req);
23+
},
24+
Err(e) => assert_eq!(e, Error::Bech32),
25+
}
26+
}
27+
}
28+
29+
pub fn cashu_test(data: &[u8]) {
30+
do_test(data);
31+
}
32+
33+
#[no_mangle]
34+
pub extern "C" fn cashu_run(data: *const u8, datalen: usize) {
35+
do_test(unsafe { std::slice::from_raw_parts(data, datalen) });
36+
}

fuzz/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
extern crate bitcoin;
1111
extern crate bitcoin_payment_instructions;
1212

13+
pub mod cashu;
1314
pub mod parse;

fuzz/targets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
#include <stdint.h>
2+
void cashu_run(const unsigned char* data, size_t data_len);
23
void parse_run(const unsigned char* data, size_t data_len);

src/cashu.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,12 @@ impl CashuPaymentRequest {
608608
Self::from_bech32_bytes(&data)
609609
}
610610

611+
#[cfg(fuzzing)]
612+
/// Decode from a byte array so the fuzzer can bypass bech32
613+
pub fn from_bytes_fuzzy(bytes: &[u8]) -> Result<CashuPaymentRequest, Error> {
614+
Self::from_bech32_bytes(bytes)
615+
}
616+
611617
/// Decode from TLV bytes
612618
fn from_bech32_bytes(bytes: &[u8]) -> Result<CashuPaymentRequest, Error> {
613619
let mut reader = TlvReader::new(bytes);

0 commit comments

Comments
 (0)