Skip to content

Commit b39d32c

Browse files
authored
Merge pull request #4005 from elnosh/fuzz-static-invoice
Fuzz `StaticInvoice`
2 parents 3b16c77 + 0854152 commit b39d32c

File tree

5 files changed

+154
-0
lines changed

5 files changed

+154
-0
lines changed

fuzz/src/bin/gen_target.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ GEN_TEST invoice_deser
1414
GEN_TEST invoice_request_deser
1515
GEN_TEST offer_deser
1616
GEN_TEST bolt11_deser
17+
GEN_TEST static_invoice_deser
1718
GEN_TEST onion_message
1819
GEN_TEST peer_crypt
1920
GEN_TEST process_network_graph
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 lightning_fuzz;
26+
use lightning_fuzz::static_invoice_deser::*;
27+
28+
#[cfg(feature = "afl")]
29+
#[macro_use] extern crate afl;
30+
#[cfg(feature = "afl")]
31+
fn main() {
32+
fuzz!(|data| {
33+
static_invoice_deser_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+
static_invoice_deser_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+
static_invoice_deser_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+
static_invoice_deser_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+
use lightning_fuzz::utils::test_logger::StringBuffer;
69+
70+
use std::sync::{atomic, Arc};
71+
{
72+
let data: Vec<u8> = vec![0];
73+
static_invoice_deser_run(data.as_ptr(), data.len());
74+
}
75+
let mut threads = Vec::new();
76+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
77+
if let Ok(tests) = fs::read_dir("test_cases/static_invoice_deser") {
78+
for test in tests {
79+
let mut data: Vec<u8> = Vec::new();
80+
let path = test.unwrap().path();
81+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
82+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
83+
84+
let thread_count_ref = Arc::clone(&threads_running);
85+
let main_thread_ref = std::thread::current();
86+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
87+
std::thread::spawn(move || {
88+
let string_logger = StringBuffer::new();
89+
90+
let panic_logger = string_logger.clone();
91+
let res = if ::std::panic::catch_unwind(move || {
92+
static_invoice_deser_test(&data, panic_logger);
93+
}).is_err() {
94+
Some(string_logger.into_string())
95+
} else { None };
96+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
97+
main_thread_ref.unpark();
98+
res
99+
})
100+
));
101+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
102+
std::thread::park();
103+
}
104+
}
105+
}
106+
let mut failed_outputs = Vec::new();
107+
for (test, thread) in threads.drain(..) {
108+
if let Some(output) = thread.join().unwrap() {
109+
println!("\nOutput of {}:\n{}\n", test, output);
110+
failed_outputs.push(test);
111+
}
112+
}
113+
if !failed_outputs.is_empty() {
114+
println!("Test cases which failed: ");
115+
for case in failed_outputs {
116+
println!("{}", case);
117+
}
118+
panic!();
119+
}
120+
}

fuzz/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub mod process_network_graph;
4242
pub mod process_onion_failure;
4343
pub mod refund_deser;
4444
pub mod router;
45+
pub mod static_invoice_deser;
4546
pub mod zbase32;
4647

4748
pub mod msg_targets;

fuzz/src/static_invoice_deser.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
use crate::utils::test_logger;
11+
use core::convert::TryFrom;
12+
use lightning::offers::static_invoice::StaticInvoice;
13+
use lightning::util::ser::Writeable;
14+
15+
#[inline]
16+
pub fn do_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
17+
if let Ok(static_invoice) = StaticInvoice::try_from(data.to_vec()) {
18+
let mut bytes = Vec::with_capacity(data.len());
19+
static_invoice.write(&mut bytes).unwrap();
20+
assert_eq!(data, bytes);
21+
}
22+
}
23+
24+
pub fn static_invoice_deser_test<Out: test_logger::Output>(data: &[u8], out: Out) {
25+
do_test(data, out);
26+
}
27+
28+
#[no_mangle]
29+
pub extern "C" fn static_invoice_deser_run(data: *const u8, datalen: usize) {
30+
do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {});
31+
}

fuzz/targets.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ void invoice_deser_run(const unsigned char* data, size_t data_len);
77
void invoice_request_deser_run(const unsigned char* data, size_t data_len);
88
void offer_deser_run(const unsigned char* data, size_t data_len);
99
void bolt11_deser_run(const unsigned char* data, size_t data_len);
10+
void static_invoice_deser_run(const unsigned char* data, size_t data_len);
1011
void onion_message_run(const unsigned char* data, size_t data_len);
1112
void peer_crypt_run(const unsigned char* data, size_t data_len);
1213
void process_network_graph_run(const unsigned char* data, size_t data_len);

0 commit comments

Comments
 (0)