Skip to content

Commit d29150d

Browse files
fuzz: add fuzz target for P2PGossipSync gossip message handling
Signed-off-by: Nishant Bansal <nishant.bansal.282003@gmail.com>
1 parent 67bf852 commit d29150d

File tree

5 files changed

+598
-0
lines changed

5 files changed

+598
-0
lines changed

fuzz/src/bin/gen_target.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ GEN_TEST fromstr_to_netaddress
2929
GEN_TEST feature_flags
3030
GEN_TEST lsps_message
3131
GEN_TEST fs_store
32+
GEN_TEST gossip_discovery
3233

3334
GEN_TEST msg_accept_channel msg_targets::
3435
GEN_TEST msg_announcement_signatures msg_targets::
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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::gossip_discovery::*;
27+
use lightning_fuzz::utils::test_logger;
28+
29+
#[cfg(feature = "afl")]
30+
#[macro_use] extern crate afl;
31+
#[cfg(feature = "afl")]
32+
fn main() {
33+
fuzz!(|data| {
34+
gossip_discovery_test(&data, test_logger::DevNull {});
35+
});
36+
}
37+
38+
#[cfg(feature = "honggfuzz")]
39+
#[macro_use] extern crate honggfuzz;
40+
#[cfg(feature = "honggfuzz")]
41+
fn main() {
42+
loop {
43+
fuzz!(|data| {
44+
gossip_discovery_test(&data, test_logger::DevNull {});
45+
});
46+
}
47+
}
48+
49+
#[cfg(feature = "libfuzzer_fuzz")]
50+
#[macro_use] extern crate libfuzzer_sys;
51+
#[cfg(feature = "libfuzzer_fuzz")]
52+
fuzz_target!(|data: &[u8]| {
53+
gossip_discovery_test(data, test_logger::DevNull {});
54+
});
55+
56+
#[cfg(feature = "stdin_fuzz")]
57+
fn main() {
58+
use std::io::Read;
59+
60+
// On macOS, panic=abort causes the process to send SIGABRT which can leave it
61+
// stuck in an uninterruptible state due to the ReportCrash daemon. Using
62+
// process::exit in a panic hook avoids this by terminating cleanly.
63+
#[cfg(target_os = "macos")]
64+
std::panic::set_hook(Box::new(|panic_info| {
65+
use std::io::Write;
66+
let _ = std::io::stdout().flush();
67+
eprintln!("{}\n{}", panic_info, std::backtrace::Backtrace::force_capture());
68+
let _ = std::io::stderr().flush();
69+
std::process::exit(1);
70+
}));
71+
72+
let mut data = Vec::with_capacity(8192);
73+
std::io::stdin().read_to_end(&mut data).unwrap();
74+
gossip_discovery_test(&data, lightning_fuzz::utils::test_logger::Stdout {});
75+
}
76+
77+
#[test]
78+
fn run_test_cases() {
79+
use std::fs;
80+
use std::io::Read;
81+
use lightning_fuzz::utils::test_logger::StringBuffer;
82+
83+
use std::sync::{atomic, Arc};
84+
{
85+
let data: Vec<u8> = vec![0];
86+
gossip_discovery_test(&data, test_logger::DevNull {});
87+
}
88+
let mut threads = Vec::new();
89+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
90+
if let Ok(tests) = fs::read_dir("test_cases/gossip_discovery") {
91+
for test in tests {
92+
let mut data: Vec<u8> = Vec::new();
93+
let path = test.unwrap().path();
94+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
95+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
96+
97+
let thread_count_ref = Arc::clone(&threads_running);
98+
let main_thread_ref = std::thread::current();
99+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
100+
std::thread::spawn(move || {
101+
let string_logger = StringBuffer::new();
102+
103+
let panic_logger = string_logger.clone();
104+
let res = if ::std::panic::catch_unwind(move || {
105+
gossip_discovery_test(&data, panic_logger);
106+
}).is_err() {
107+
Some(string_logger.into_string())
108+
} else { None };
109+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
110+
main_thread_ref.unpark();
111+
res
112+
})
113+
));
114+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
115+
std::thread::park();
116+
}
117+
}
118+
}
119+
let mut failed_outputs = Vec::new();
120+
for (test, thread) in threads.drain(..) {
121+
if let Some(output) = thread.join().unwrap() {
122+
println!("\nOutput of {}:\n{}\n", test, output);
123+
failed_outputs.push(test);
124+
}
125+
}
126+
if !failed_outputs.is_empty() {
127+
println!("Test cases which failed: ");
128+
for case in failed_outputs {
129+
println!("{}", case);
130+
}
131+
panic!();
132+
}
133+
}

0 commit comments

Comments
 (0)