Skip to content

Commit cb9e2a8

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 cb9e2a8

5 files changed

Lines changed: 586 additions & 0 deletions

File tree

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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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+
let mut data = Vec::with_capacity(8192);
61+
std::io::stdin().read_to_end(&mut data).unwrap();
62+
gossip_discovery_test(&data, lightning_fuzz::utils::test_logger::Stdout {});
63+
}
64+
65+
#[test]
66+
fn run_test_cases() {
67+
use std::fs;
68+
use std::io::Read;
69+
use lightning_fuzz::utils::test_logger::StringBuffer;
70+
71+
use std::sync::{atomic, Arc};
72+
{
73+
let data: Vec<u8> = vec![0];
74+
gossip_discovery_test(&data, test_logger::DevNull {});
75+
}
76+
let mut threads = Vec::new();
77+
let threads_running = Arc::new(atomic::AtomicUsize::new(0));
78+
if let Ok(tests) = fs::read_dir("test_cases/gossip_discovery") {
79+
for test in tests {
80+
let mut data: Vec<u8> = Vec::new();
81+
let path = test.unwrap().path();
82+
fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
83+
threads_running.fetch_add(1, atomic::Ordering::AcqRel);
84+
85+
let thread_count_ref = Arc::clone(&threads_running);
86+
let main_thread_ref = std::thread::current();
87+
threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
88+
std::thread::spawn(move || {
89+
let string_logger = StringBuffer::new();
90+
91+
let panic_logger = string_logger.clone();
92+
let res = if ::std::panic::catch_unwind(move || {
93+
gossip_discovery_test(&data, panic_logger);
94+
}).is_err() {
95+
Some(string_logger.into_string())
96+
} else { None };
97+
thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
98+
main_thread_ref.unpark();
99+
res
100+
})
101+
));
102+
while threads_running.load(atomic::Ordering::Acquire) > 32 {
103+
std::thread::park();
104+
}
105+
}
106+
}
107+
let mut failed_outputs = Vec::new();
108+
for (test, thread) in threads.drain(..) {
109+
if let Some(output) = thread.join().unwrap() {
110+
println!("\nOutput of {}:\n{}\n", test, output);
111+
failed_outputs.push(test);
112+
}
113+
}
114+
if !failed_outputs.is_empty() {
115+
println!("Test cases which failed: ");
116+
for case in failed_outputs {
117+
println!("{}", case);
118+
}
119+
panic!();
120+
}
121+
}

0 commit comments

Comments
 (0)