From 6745aff9b682fddb544968ae11b2d92665b58da2 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 20 Feb 2020 20:11:40 -0500 Subject: [PATCH] Thread fuzz test cases Adds threading (with logger capture) to fuzz targets so that we can more effeciently test a failing fuzz corpus on new code changes. --- fuzz/Cargo.toml | 4 ++ fuzz/src/bin/chanmon_consistency_target.rs | 45 ++++++++++++-- fuzz/src/bin/chanmon_deser_target.rs | 45 ++++++++++++-- fuzz/src/bin/full_stack_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_accept_channel_target.rs | 45 ++++++++++++-- .../bin/msg_announcement_signatures_target.rs | 45 ++++++++++++-- .../bin/msg_channel_announcement_target.rs | 45 ++++++++++++-- .../src/bin/msg_channel_reestablish_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_channel_update_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_closing_signed_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_commitment_signed_target.rs | 45 ++++++++++++-- .../msg_decoded_onion_error_packet_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_error_message_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_funding_created_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_funding_locked_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_funding_signed_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_init_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_node_announcement_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_onion_hop_data_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_open_channel_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_ping_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_pong_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_revoke_and_ack_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_shutdown_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_update_add_htlc_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_update_fail_htlc_target.rs | 45 ++++++++++++-- .../msg_update_fail_malformed_htlc_target.rs | 45 ++++++++++++-- fuzz/src/bin/msg_update_fee_target.rs | 45 ++++++++++++-- .../src/bin/msg_update_fulfill_htlc_target.rs | 45 ++++++++++++-- fuzz/src/bin/peer_crypt_target.rs | 45 ++++++++++++-- fuzz/src/bin/router_target.rs | 45 ++++++++++++-- fuzz/src/bin/target_template.txt | 45 ++++++++++++-- fuzz/src/chanmon_consistency.rs | 12 ++-- fuzz/src/chanmon_deser.rs | 10 +++- fuzz/src/full_stack.rs | 8 ++- fuzz/src/lib.rs | 2 +- fuzz/src/msg_targets/msg_accept_channel.rs | 6 +- .../msg_announcement_signatures.rs | 6 +- .../msg_targets/msg_channel_announcement.rs | 6 +- .../msg_targets/msg_channel_reestablish.rs | 6 +- fuzz/src/msg_targets/msg_channel_update.rs | 6 +- fuzz/src/msg_targets/msg_closing_signed.rs | 6 +- fuzz/src/msg_targets/msg_commitment_signed.rs | 6 +- .../msg_decoded_onion_error_packet.rs | 6 +- fuzz/src/msg_targets/msg_error_message.rs | 6 +- fuzz/src/msg_targets/msg_funding_created.rs | 6 +- fuzz/src/msg_targets/msg_funding_locked.rs | 6 +- fuzz/src/msg_targets/msg_funding_signed.rs | 6 +- fuzz/src/msg_targets/msg_init.rs | 6 +- fuzz/src/msg_targets/msg_node_announcement.rs | 6 +- fuzz/src/msg_targets/msg_onion_hop_data.rs | 6 +- fuzz/src/msg_targets/msg_open_channel.rs | 6 +- fuzz/src/msg_targets/msg_ping.rs | 6 +- fuzz/src/msg_targets/msg_pong.rs | 6 +- fuzz/src/msg_targets/msg_revoke_and_ack.rs | 6 +- fuzz/src/msg_targets/msg_shutdown.rs | 6 +- fuzz/src/msg_targets/msg_target_template.txt | 6 +- fuzz/src/msg_targets/msg_update_add_htlc.rs | 6 +- fuzz/src/msg_targets/msg_update_fail_htlc.rs | 6 +- .../msg_update_fail_malformed_htlc.rs | 6 +- fuzz/src/msg_targets/msg_update_fee.rs | 6 +- .../msg_targets/msg_update_fulfill_htlc.rs | 6 +- fuzz/src/peer_crypt.rs | 6 ++ fuzz/src/router.rs | 10 +++- fuzz/src/utils/mod.rs | 2 +- fuzz/src/utils/test_logger.rs | 58 ++++++++++++++----- 66 files changed, 1398 insertions(+), 265 deletions(-) diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index 1cb7a8d6d..5e3197f8f 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -37,6 +37,10 @@ members = ["."] lto = true codegen-units = 1 +# When testing a large fuzz corpus, -O1 offers a nice speedup +[profile.dev] +opt-level = 1 + [lib] name = "lightning_fuzz" path = "src/lib.rs" diff --git a/fuzz/src/bin/chanmon_consistency_target.rs b/fuzz/src/bin/chanmon_consistency_target.rs index a6d3ff061..845503da1 100644 --- a/fuzz/src/bin/chanmon_consistency_target.rs +++ b/fuzz/src/bin/chanmon_consistency_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - chanmon_consistency_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + chanmon_consistency_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/chanmon_consistency") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - chanmon_consistency_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + chanmon_consistency_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/chanmon_deser_target.rs b/fuzz/src/bin/chanmon_deser_target.rs index 91d5f1e11..1677721b2 100644 --- a/fuzz/src/bin/chanmon_deser_target.rs +++ b/fuzz/src/bin/chanmon_deser_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - chanmon_deser_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + chanmon_deser_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/chanmon_deser") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - chanmon_deser_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + chanmon_deser_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/full_stack_target.rs b/fuzz/src/bin/full_stack_target.rs index 955202d24..94da2888f 100644 --- a/fuzz/src/bin/full_stack_target.rs +++ b/fuzz/src/bin/full_stack_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - full_stack_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + full_stack_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/full_stack") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - full_stack_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + full_stack_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_accept_channel_target.rs b/fuzz/src/bin/msg_accept_channel_target.rs index 8112ebf94..f7bfc10d0 100644 --- a/fuzz/src/bin/msg_accept_channel_target.rs +++ b/fuzz/src/bin/msg_accept_channel_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_accept_channel_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_accept_channel_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_accept_channel") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_accept_channel_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_accept_channel_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_announcement_signatures_target.rs b/fuzz/src/bin/msg_announcement_signatures_target.rs index f234dadfd..63c6407eb 100644 --- a/fuzz/src/bin/msg_announcement_signatures_target.rs +++ b/fuzz/src/bin/msg_announcement_signatures_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_announcement_signatures_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_announcement_signatures_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_announcement_signatures") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_announcement_signatures_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_announcement_signatures_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_channel_announcement_target.rs b/fuzz/src/bin/msg_channel_announcement_target.rs index a2a63643f..8aae27702 100644 --- a/fuzz/src/bin/msg_channel_announcement_target.rs +++ b/fuzz/src/bin/msg_channel_announcement_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_channel_announcement_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_channel_announcement_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_channel_announcement") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_channel_announcement_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_channel_announcement_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_channel_reestablish_target.rs b/fuzz/src/bin/msg_channel_reestablish_target.rs index 908d20676..fb5e4774b 100644 --- a/fuzz/src/bin/msg_channel_reestablish_target.rs +++ b/fuzz/src/bin/msg_channel_reestablish_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_channel_reestablish_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_channel_reestablish_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_channel_reestablish") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_channel_reestablish_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_channel_reestablish_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_channel_update_target.rs b/fuzz/src/bin/msg_channel_update_target.rs index 6eb7ba190..78c6404f3 100644 --- a/fuzz/src/bin/msg_channel_update_target.rs +++ b/fuzz/src/bin/msg_channel_update_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_channel_update_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_channel_update_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_channel_update") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_channel_update_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_channel_update_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_closing_signed_target.rs b/fuzz/src/bin/msg_closing_signed_target.rs index 3377ffd6a..97b49ca8d 100644 --- a/fuzz/src/bin/msg_closing_signed_target.rs +++ b/fuzz/src/bin/msg_closing_signed_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_closing_signed_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_closing_signed_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_closing_signed") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_closing_signed_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_closing_signed_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_commitment_signed_target.rs b/fuzz/src/bin/msg_commitment_signed_target.rs index a9cef5fe2..a343e45f7 100644 --- a/fuzz/src/bin/msg_commitment_signed_target.rs +++ b/fuzz/src/bin/msg_commitment_signed_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_commitment_signed_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_commitment_signed_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_commitment_signed") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_commitment_signed_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_commitment_signed_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs b/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs index cefcf06bd..26e26fac9 100644 --- a/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs +++ b/fuzz/src/bin/msg_decoded_onion_error_packet_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_decoded_onion_error_packet_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_decoded_onion_error_packet_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_decoded_onion_error_packet") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_decoded_onion_error_packet_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_decoded_onion_error_packet_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_error_message_target.rs b/fuzz/src/bin/msg_error_message_target.rs index c8706d21a..34facca3f 100644 --- a/fuzz/src/bin/msg_error_message_target.rs +++ b/fuzz/src/bin/msg_error_message_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_error_message_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_error_message_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_error_message") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_error_message_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_error_message_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_funding_created_target.rs b/fuzz/src/bin/msg_funding_created_target.rs index f51caf57e..602f99067 100644 --- a/fuzz/src/bin/msg_funding_created_target.rs +++ b/fuzz/src/bin/msg_funding_created_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_funding_created_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_funding_created_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_funding_created") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_funding_created_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_funding_created_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_funding_locked_target.rs b/fuzz/src/bin/msg_funding_locked_target.rs index 14954096a..ee44753de 100644 --- a/fuzz/src/bin/msg_funding_locked_target.rs +++ b/fuzz/src/bin/msg_funding_locked_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_funding_locked_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_funding_locked_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_funding_locked") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_funding_locked_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_funding_locked_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_funding_signed_target.rs b/fuzz/src/bin/msg_funding_signed_target.rs index dcd5dd9d9..d61da65fa 100644 --- a/fuzz/src/bin/msg_funding_signed_target.rs +++ b/fuzz/src/bin/msg_funding_signed_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_funding_signed_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_funding_signed_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_funding_signed") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_funding_signed_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_funding_signed_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_init_target.rs b/fuzz/src/bin/msg_init_target.rs index 4dd75f2e3..62b4e4159 100644 --- a/fuzz/src/bin/msg_init_target.rs +++ b/fuzz/src/bin/msg_init_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_init_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_init_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_init") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_init_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_init_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_node_announcement_target.rs b/fuzz/src/bin/msg_node_announcement_target.rs index ae778b679..4605f9495 100644 --- a/fuzz/src/bin/msg_node_announcement_target.rs +++ b/fuzz/src/bin/msg_node_announcement_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_node_announcement_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_node_announcement_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_node_announcement") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_node_announcement_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_node_announcement_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_onion_hop_data_target.rs b/fuzz/src/bin/msg_onion_hop_data_target.rs index 95453bc95..1dcb5c7d5 100644 --- a/fuzz/src/bin/msg_onion_hop_data_target.rs +++ b/fuzz/src/bin/msg_onion_hop_data_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_onion_hop_data_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_onion_hop_data_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_onion_hop_data") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_onion_hop_data_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_onion_hop_data_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_open_channel_target.rs b/fuzz/src/bin/msg_open_channel_target.rs index fa385e564..96fed7f33 100644 --- a/fuzz/src/bin/msg_open_channel_target.rs +++ b/fuzz/src/bin/msg_open_channel_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_open_channel_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_open_channel_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_open_channel") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_open_channel_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_open_channel_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_ping_target.rs b/fuzz/src/bin/msg_ping_target.rs index e8b0a5983..cf2cacf2d 100644 --- a/fuzz/src/bin/msg_ping_target.rs +++ b/fuzz/src/bin/msg_ping_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_ping_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_ping_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_ping") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_ping_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_ping_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_pong_target.rs b/fuzz/src/bin/msg_pong_target.rs index f7408ec52..c538d1383 100644 --- a/fuzz/src/bin/msg_pong_target.rs +++ b/fuzz/src/bin/msg_pong_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_pong_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_pong_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_pong") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_pong_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_pong_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_revoke_and_ack_target.rs b/fuzz/src/bin/msg_revoke_and_ack_target.rs index 47f125a44..08fe92a2a 100644 --- a/fuzz/src/bin/msg_revoke_and_ack_target.rs +++ b/fuzz/src/bin/msg_revoke_and_ack_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_revoke_and_ack_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_revoke_and_ack_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_revoke_and_ack") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_revoke_and_ack_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_revoke_and_ack_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_shutdown_target.rs b/fuzz/src/bin/msg_shutdown_target.rs index fb400f773..647e7cc7b 100644 --- a/fuzz/src/bin/msg_shutdown_target.rs +++ b/fuzz/src/bin/msg_shutdown_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_shutdown_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_shutdown_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_shutdown") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_shutdown_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_shutdown_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_update_add_htlc_target.rs b/fuzz/src/bin/msg_update_add_htlc_target.rs index e433a4468..abb749a68 100644 --- a/fuzz/src/bin/msg_update_add_htlc_target.rs +++ b/fuzz/src/bin/msg_update_add_htlc_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_update_add_htlc_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_update_add_htlc_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_update_add_htlc") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_update_add_htlc_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_update_add_htlc_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_update_fail_htlc_target.rs b/fuzz/src/bin/msg_update_fail_htlc_target.rs index ee8bb3af1..278df1fe8 100644 --- a/fuzz/src/bin/msg_update_fail_htlc_target.rs +++ b/fuzz/src/bin/msg_update_fail_htlc_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_update_fail_htlc_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_update_fail_htlc_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_update_fail_htlc") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_update_fail_htlc_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_update_fail_htlc_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_update_fail_malformed_htlc_target.rs b/fuzz/src/bin/msg_update_fail_malformed_htlc_target.rs index 06eb31d32..8c6b1e998 100644 --- a/fuzz/src/bin/msg_update_fail_malformed_htlc_target.rs +++ b/fuzz/src/bin/msg_update_fail_malformed_htlc_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_update_fail_malformed_htlc") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_update_fail_malformed_htlc_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_update_fail_malformed_htlc_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_update_fee_target.rs b/fuzz/src/bin/msg_update_fee_target.rs index 12e5c384f..2b01b2b6b 100644 --- a/fuzz/src/bin/msg_update_fee_target.rs +++ b/fuzz/src/bin/msg_update_fee_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_update_fee_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_update_fee_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_update_fee") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_update_fee_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_update_fee_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/msg_update_fulfill_htlc_target.rs b/fuzz/src/bin/msg_update_fulfill_htlc_target.rs index a5a8bda33..dbe56d7b5 100644 --- a/fuzz/src/bin/msg_update_fulfill_htlc_target.rs +++ b/fuzz/src/bin/msg_update_fulfill_htlc_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - msg_update_fulfill_htlc_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + msg_update_fulfill_htlc_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/msg_update_fulfill_htlc") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - msg_update_fulfill_htlc_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + msg_update_fulfill_htlc_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/peer_crypt_target.rs b/fuzz/src/bin/peer_crypt_target.rs index b32854f05..f68eaf1b2 100644 --- a/fuzz/src/bin/peer_crypt_target.rs +++ b/fuzz/src/bin/peer_crypt_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - peer_crypt_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + peer_crypt_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/peer_crypt") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - peer_crypt_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + peer_crypt_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/router_target.rs b/fuzz/src/bin/router_target.rs index 3e82b196a..8ab074b58 100644 --- a/fuzz/src/bin/router_target.rs +++ b/fuzz/src/bin/router_target.rs @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - router_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + router_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/router") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - router_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + router_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/bin/target_template.txt b/fuzz/src/bin/target_template.txt index 9815e9ccc..ae2130a9c 100644 --- a/fuzz/src/bin/target_template.txt +++ b/fuzz/src/bin/target_template.txt @@ -45,15 +45,48 @@ fn main() { #[test] fn run_test_cases() { - let mut data: Vec = vec![0]; - TARGET_NAME_run(data.as_ptr(), data.len()); + use lightning_fuzz::utils::test_logger::StringBuffer; + + use std::sync::{atomic, Arc}; + { + let data: Vec = vec![0]; + TARGET_NAME_run(data.as_ptr(), data.len()); + } + let mut threads = Vec::new(); + let threads_running = Arc::new(atomic::AtomicUsize::new(0)); if let Ok(tests) = fs::read_dir("test_cases/TARGET_NAME") { for test in tests { - data.clear(); + let mut data: Vec = Vec::new(); let path = test.unwrap().path(); - println!("Running test {}...", path.file_name().unwrap().to_str().unwrap()); - fs::File::open(path).unwrap().read_to_end(&mut data).unwrap(); - TARGET_NAME_run(data.as_ptr(), data.len()); + fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap(); + threads_running.fetch_add(1, atomic::Ordering::AcqRel); + + let thread_count_ref = Arc::clone(&threads_running); + let main_thread_ref = std::thread::current(); + threads.push((path.file_name().unwrap().to_str().unwrap().to_string(), + std::thread::spawn(move || { + let string_logger = StringBuffer::new(); + + let panic_logger = string_logger.clone(); + let res = if ::std::panic::catch_unwind(move || { + TARGET_NAME_test(&data, panic_logger); + }).is_err() { + Some(string_logger.into_string()) + } else { None }; + thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel); + main_thread_ref.unpark(); + res + }) + )); + while threads_running.load(atomic::Ordering::Acquire) > 32 { + std::thread::park(); + } + } + } + for (test, thread) in threads.drain(..) { + if let Some(output) = thread.join().unwrap() { + println!("Output of {}:\n{}", test, output); + panic!(); } } } diff --git a/fuzz/src/chanmon_consistency.rs b/fuzz/src/chanmon_consistency.rs index 3d14808b0..c524eab40 100644 --- a/fuzz/src/chanmon_consistency.rs +++ b/fuzz/src/chanmon_consistency.rs @@ -180,13 +180,13 @@ impl KeysInterface for KeyProvider { } #[inline] -pub fn do_test(data: &[u8]) { +pub fn do_test(data: &[u8], out: Out) { let fee_est = Arc::new(FuzzEstimator{}); let broadcast = Arc::new(TestBroadcaster{}); macro_rules! make_node { ($node_id: expr) => { { - let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string())); + let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone())); let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger))); let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone())); @@ -202,7 +202,7 @@ pub fn do_test(data: &[u8]) { macro_rules! reload_node { ($ser: expr, $node_id: expr, $old_monitors: expr) => { { - let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string())); + let logger: Arc = Arc::new(test_logger::TestLogger::new($node_id.to_string(), out.clone())); let watch = Arc::new(ChainWatchInterfaceUtil::new(Network::Bitcoin, Arc::clone(&logger))); let monitor = Arc::new(TestChannelMonitor::new(watch.clone(), broadcast.clone(), logger.clone(), fee_est.clone())); @@ -794,7 +794,11 @@ pub fn do_test(data: &[u8]) { } } +pub fn chanmon_consistency_test(data: &[u8], out: Out) { + do_test(data, out); +} + #[no_mangle] pub extern "C" fn chanmon_consistency_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{}); } diff --git a/fuzz/src/chanmon_deser.rs b/fuzz/src/chanmon_deser.rs index c9042dd41..cd8f7cde8 100644 --- a/fuzz/src/chanmon_deser.rs +++ b/fuzz/src/chanmon_deser.rs @@ -24,8 +24,8 @@ impl Writer for VecWriter { } #[inline] -pub fn do_test(data: &[u8]) { - let logger = Arc::new(test_logger::TestLogger::new("".to_owned())); +pub fn do_test(data: &[u8], out: Out) { + let logger = Arc::new(test_logger::TestLogger::new("".to_owned(), out)); if let Ok((latest_block_hash, monitor)) = <(Sha256dHash, channelmonitor::ChannelMonitor)>::read(&mut Cursor::new(data), logger.clone()) { let mut w = VecWriter(Vec::new()); monitor.write_for_disk(&mut w).unwrap(); @@ -37,7 +37,11 @@ pub fn do_test(data: &[u8]) { } } +pub fn chanmon_deser_test(data: &[u8], out: Out) { + do_test(data, out); +} + #[no_mangle] pub extern "C" fn chanmon_deser_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull{}); } diff --git a/fuzz/src/full_stack.rs b/fuzz/src/full_stack.rs index be2e0401b..c75909430 100644 --- a/fuzz/src/full_stack.rs +++ b/fuzz/src/full_stack.rs @@ -561,15 +561,19 @@ pub fn do_test(data: &[u8], logger: &Arc) { } } +pub fn full_stack_test(data: &[u8], out: Out) { + let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned(), out)); + do_test(data, &logger); +} + #[no_mangle] pub extern "C" fn full_stack_run(data: *const u8, datalen: usize) { - let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned())); + let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned(), test_logger::DevNull {})); do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, &logger); } #[cfg(test)] mod tests { - use utils::test_logger; use lightning::util::logger::{Logger, Record}; use std::collections::HashMap; use std::sync::{Arc, Mutex}; diff --git a/fuzz/src/lib.rs b/fuzz/src/lib.rs index fde5316cf..f87c7511c 100644 --- a/fuzz/src/lib.rs +++ b/fuzz/src/lib.rs @@ -4,7 +4,7 @@ extern crate lightning; extern crate secp256k1; extern crate hex; -mod utils; +pub mod utils; pub mod chanmon_deser; pub mod chanmon_consistency; diff --git a/fuzz/src/msg_targets/msg_accept_channel.rs b/fuzz/src/msg_targets/msg_accept_channel.rs index 94baec595..0844b77f4 100644 --- a/fuzz/src/msg_targets/msg_accept_channel.rs +++ b/fuzz/src/msg_targets/msg_accept_channel.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_accept_channel_test(data: &[u8], _out: Out) { test_msg!(msgs::AcceptChannel, data); } #[no_mangle] pub extern "C" fn msg_accept_channel_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::AcceptChannel, data); } diff --git a/fuzz/src/msg_targets/msg_announcement_signatures.rs b/fuzz/src/msg_targets/msg_announcement_signatures.rs index 065295b9e..d9a1f0eaa 100644 --- a/fuzz/src/msg_targets/msg_announcement_signatures.rs +++ b/fuzz/src/msg_targets/msg_announcement_signatures.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_announcement_signatures_test(data: &[u8], _out: Out) { test_msg!(msgs::AnnouncementSignatures, data); } #[no_mangle] pub extern "C" fn msg_announcement_signatures_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::AnnouncementSignatures, data); } diff --git a/fuzz/src/msg_targets/msg_channel_announcement.rs b/fuzz/src/msg_targets/msg_channel_announcement.rs index dafcdafed..073179304 100644 --- a/fuzz/src/msg_targets/msg_channel_announcement.rs +++ b/fuzz/src/msg_targets/msg_channel_announcement.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_channel_announcement_test(data: &[u8], _out: Out) { test_msg_exact!(msgs::ChannelAnnouncement, data); } #[no_mangle] pub extern "C" fn msg_channel_announcement_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_exact!(msgs::ChannelAnnouncement, data); } diff --git a/fuzz/src/msg_targets/msg_channel_reestablish.rs b/fuzz/src/msg_targets/msg_channel_reestablish.rs index 8d05450e7..880bfe8be 100644 --- a/fuzz/src/msg_targets/msg_channel_reestablish.rs +++ b/fuzz/src/msg_targets/msg_channel_reestablish.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_channel_reestablish_test(data: &[u8], _out: Out) { test_msg!(msgs::ChannelReestablish, data); } #[no_mangle] pub extern "C" fn msg_channel_reestablish_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::ChannelReestablish, data); } diff --git a/fuzz/src/msg_targets/msg_channel_update.rs b/fuzz/src/msg_targets/msg_channel_update.rs index 0d3edfe88..d0326bfa4 100644 --- a/fuzz/src/msg_targets/msg_channel_update.rs +++ b/fuzz/src/msg_targets/msg_channel_update.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_channel_update_test(data: &[u8], _out: Out) { test_msg_exact!(msgs::ChannelUpdate, data); } #[no_mangle] pub extern "C" fn msg_channel_update_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_exact!(msgs::ChannelUpdate, data); } diff --git a/fuzz/src/msg_targets/msg_closing_signed.rs b/fuzz/src/msg_targets/msg_closing_signed.rs index 8043d9de0..a2c01bc56 100644 --- a/fuzz/src/msg_targets/msg_closing_signed.rs +++ b/fuzz/src/msg_targets/msg_closing_signed.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_closing_signed_test(data: &[u8], _out: Out) { test_msg!(msgs::ClosingSigned, data); } #[no_mangle] pub extern "C" fn msg_closing_signed_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::ClosingSigned, data); } diff --git a/fuzz/src/msg_targets/msg_commitment_signed.rs b/fuzz/src/msg_targets/msg_commitment_signed.rs index 319f46c59..9534c082f 100644 --- a/fuzz/src/msg_targets/msg_commitment_signed.rs +++ b/fuzz/src/msg_targets/msg_commitment_signed.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_commitment_signed_test(data: &[u8], _out: Out) { test_msg!(msgs::CommitmentSigned, data); } #[no_mangle] pub extern "C" fn msg_commitment_signed_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::CommitmentSigned, data); } diff --git a/fuzz/src/msg_targets/msg_decoded_onion_error_packet.rs b/fuzz/src/msg_targets/msg_decoded_onion_error_packet.rs index d6e5a829d..cad0c205f 100644 --- a/fuzz/src/msg_targets/msg_decoded_onion_error_packet.rs +++ b/fuzz/src/msg_targets/msg_decoded_onion_error_packet.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_decoded_onion_error_packet_test(data: &[u8], _out: Out) { test_msg!(msgs::DecodedOnionErrorPacket, data); } #[no_mangle] pub extern "C" fn msg_decoded_onion_error_packet_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::DecodedOnionErrorPacket, data); } diff --git a/fuzz/src/msg_targets/msg_error_message.rs b/fuzz/src/msg_targets/msg_error_message.rs index 16594953e..ed7f45460 100644 --- a/fuzz/src/msg_targets/msg_error_message.rs +++ b/fuzz/src/msg_targets/msg_error_message.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_error_message_test(data: &[u8], _out: Out) { test_msg_hole!(msgs::ErrorMessage, data, 32, 2); } #[no_mangle] pub extern "C" fn msg_error_message_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_hole!(msgs::ErrorMessage, data, 32, 2); } diff --git a/fuzz/src/msg_targets/msg_funding_created.rs b/fuzz/src/msg_targets/msg_funding_created.rs index cc322cbb0..83532bad0 100644 --- a/fuzz/src/msg_targets/msg_funding_created.rs +++ b/fuzz/src/msg_targets/msg_funding_created.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_funding_created_test(data: &[u8], _out: Out) { test_msg!(msgs::FundingCreated, data); } #[no_mangle] pub extern "C" fn msg_funding_created_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::FundingCreated, data); } diff --git a/fuzz/src/msg_targets/msg_funding_locked.rs b/fuzz/src/msg_targets/msg_funding_locked.rs index a546ed8bb..73f84242f 100644 --- a/fuzz/src/msg_targets/msg_funding_locked.rs +++ b/fuzz/src/msg_targets/msg_funding_locked.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_funding_locked_test(data: &[u8], _out: Out) { test_msg!(msgs::FundingLocked, data); } #[no_mangle] pub extern "C" fn msg_funding_locked_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::FundingLocked, data); } diff --git a/fuzz/src/msg_targets/msg_funding_signed.rs b/fuzz/src/msg_targets/msg_funding_signed.rs index 0210b5ec6..f1808991d 100644 --- a/fuzz/src/msg_targets/msg_funding_signed.rs +++ b/fuzz/src/msg_targets/msg_funding_signed.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_funding_signed_test(data: &[u8], _out: Out) { test_msg!(msgs::FundingSigned, data); } #[no_mangle] pub extern "C" fn msg_funding_signed_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::FundingSigned, data); } diff --git a/fuzz/src/msg_targets/msg_init.rs b/fuzz/src/msg_targets/msg_init.rs index 52b92acd0..be2a4f0a7 100644 --- a/fuzz/src/msg_targets/msg_init.rs +++ b/fuzz/src/msg_targets/msg_init.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_init_test(data: &[u8], _out: Out) { test_msg_simple!(msgs::Init, data); } #[no_mangle] pub extern "C" fn msg_init_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_simple!(msgs::Init, data); } diff --git a/fuzz/src/msg_targets/msg_node_announcement.rs b/fuzz/src/msg_targets/msg_node_announcement.rs index bb09f87e7..d66295471 100644 --- a/fuzz/src/msg_targets/msg_node_announcement.rs +++ b/fuzz/src/msg_targets/msg_node_announcement.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_node_announcement_test(data: &[u8], _out: Out) { test_msg_exact!(msgs::NodeAnnouncement, data); } #[no_mangle] pub extern "C" fn msg_node_announcement_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_exact!(msgs::NodeAnnouncement, data); } diff --git a/fuzz/src/msg_targets/msg_onion_hop_data.rs b/fuzz/src/msg_targets/msg_onion_hop_data.rs index 9d7ad6025..79243b52e 100644 --- a/fuzz/src/msg_targets/msg_onion_hop_data.rs +++ b/fuzz/src/msg_targets/msg_onion_hop_data.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_onion_hop_data_test(data: &[u8], _out: Out) { test_msg_simple!(msgs::OnionHopData, data); } #[no_mangle] pub extern "C" fn msg_onion_hop_data_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_simple!(msgs::OnionHopData, data); } diff --git a/fuzz/src/msg_targets/msg_open_channel.rs b/fuzz/src/msg_targets/msg_open_channel.rs index 9c700c2b2..f3a207dee 100644 --- a/fuzz/src/msg_targets/msg_open_channel.rs +++ b/fuzz/src/msg_targets/msg_open_channel.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_open_channel_test(data: &[u8], _out: Out) { test_msg!(msgs::OpenChannel, data); } #[no_mangle] pub extern "C" fn msg_open_channel_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::OpenChannel, data); } diff --git a/fuzz/src/msg_targets/msg_ping.rs b/fuzz/src/msg_targets/msg_ping.rs index 67e2382f0..90065cdc1 100644 --- a/fuzz/src/msg_targets/msg_ping.rs +++ b/fuzz/src/msg_targets/msg_ping.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_ping_test(data: &[u8], _out: Out) { test_msg_simple!(msgs::Ping, data); } #[no_mangle] pub extern "C" fn msg_ping_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_simple!(msgs::Ping, data); } diff --git a/fuzz/src/msg_targets/msg_pong.rs b/fuzz/src/msg_targets/msg_pong.rs index cec5c5adf..2fd1c3a85 100644 --- a/fuzz/src/msg_targets/msg_pong.rs +++ b/fuzz/src/msg_targets/msg_pong.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_pong_test(data: &[u8], _out: Out) { test_msg_simple!(msgs::Pong, data); } #[no_mangle] pub extern "C" fn msg_pong_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_simple!(msgs::Pong, data); } diff --git a/fuzz/src/msg_targets/msg_revoke_and_ack.rs b/fuzz/src/msg_targets/msg_revoke_and_ack.rs index 6df64e3e4..ce7305107 100644 --- a/fuzz/src/msg_targets/msg_revoke_and_ack.rs +++ b/fuzz/src/msg_targets/msg_revoke_and_ack.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_revoke_and_ack_test(data: &[u8], _out: Out) { test_msg!(msgs::RevokeAndACK, data); } #[no_mangle] pub extern "C" fn msg_revoke_and_ack_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::RevokeAndACK, data); } diff --git a/fuzz/src/msg_targets/msg_shutdown.rs b/fuzz/src/msg_targets/msg_shutdown.rs index d70d9a6a7..4017db294 100644 --- a/fuzz/src/msg_targets/msg_shutdown.rs +++ b/fuzz/src/msg_targets/msg_shutdown.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_shutdown_test(data: &[u8], _out: Out) { test_msg!(msgs::Shutdown, data); } #[no_mangle] pub extern "C" fn msg_shutdown_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::Shutdown, data); } diff --git a/fuzz/src/msg_targets/msg_target_template.txt b/fuzz/src/msg_targets/msg_target_template.txt index ea785ff4e..afb1d759d 100644 --- a/fuzz/src/msg_targets/msg_target_template.txt +++ b/fuzz/src/msg_targets/msg_target_template.txt @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn TARGET_NAME_test(data: &[u8], _out: Out) { TEST_MSG!(msgs::MSG_TARGET, dataEXTRA_ARGS); } #[no_mangle] pub extern "C" fn TARGET_NAME_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + TEST_MSG!(msgs::MSG_TARGET, dataEXTRA_ARGS); } diff --git a/fuzz/src/msg_targets/msg_update_add_htlc.rs b/fuzz/src/msg_targets/msg_update_add_htlc.rs index 13b648f0a..ac4e67474 100644 --- a/fuzz/src/msg_targets/msg_update_add_htlc.rs +++ b/fuzz/src/msg_targets/msg_update_add_htlc.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_update_add_htlc_test(data: &[u8], _out: Out) { test_msg_hole!(msgs::UpdateAddHTLC, data, 85, 33); } #[no_mangle] pub extern "C" fn msg_update_add_htlc_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg_hole!(msgs::UpdateAddHTLC, data, 85, 33); } diff --git a/fuzz/src/msg_targets/msg_update_fail_htlc.rs b/fuzz/src/msg_targets/msg_update_fail_htlc.rs index e21d2146c..3603941d0 100644 --- a/fuzz/src/msg_targets/msg_update_fail_htlc.rs +++ b/fuzz/src/msg_targets/msg_update_fail_htlc.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_update_fail_htlc_test(data: &[u8], _out: Out) { test_msg!(msgs::UpdateFailHTLC, data); } #[no_mangle] pub extern "C" fn msg_update_fail_htlc_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::UpdateFailHTLC, data); } diff --git a/fuzz/src/msg_targets/msg_update_fail_malformed_htlc.rs b/fuzz/src/msg_targets/msg_update_fail_malformed_htlc.rs index e27b85281..78591ada4 100644 --- a/fuzz/src/msg_targets/msg_update_fail_malformed_htlc.rs +++ b/fuzz/src/msg_targets/msg_update_fail_malformed_htlc.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_update_fail_malformed_htlc_test(data: &[u8], _out: Out) { test_msg!(msgs::UpdateFailMalformedHTLC, data); } #[no_mangle] pub extern "C" fn msg_update_fail_malformed_htlc_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::UpdateFailMalformedHTLC, data); } diff --git a/fuzz/src/msg_targets/msg_update_fee.rs b/fuzz/src/msg_targets/msg_update_fee.rs index 5051bb87d..b106288f6 100644 --- a/fuzz/src/msg_targets/msg_update_fee.rs +++ b/fuzz/src/msg_targets/msg_update_fee.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_update_fee_test(data: &[u8], _out: Out) { test_msg!(msgs::UpdateFee, data); } #[no_mangle] pub extern "C" fn msg_update_fee_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::UpdateFee, data); } diff --git a/fuzz/src/msg_targets/msg_update_fulfill_htlc.rs b/fuzz/src/msg_targets/msg_update_fulfill_htlc.rs index b8b3d98a9..d97653c2d 100644 --- a/fuzz/src/msg_targets/msg_update_fulfill_htlc.rs +++ b/fuzz/src/msg_targets/msg_update_fulfill_htlc.rs @@ -4,13 +4,15 @@ use lightning::ln::msgs; use msg_targets::utils::VecWriter; +use utils::test_logger; #[inline] -pub fn do_test(data: &[u8]) { +pub fn msg_update_fulfill_htlc_test(data: &[u8], _out: Out) { test_msg!(msgs::UpdateFulfillHTLC, data); } #[no_mangle] pub extern "C" fn msg_update_fulfill_htlc_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + let data = unsafe { std::slice::from_raw_parts(data, datalen) }; + test_msg!(msgs::UpdateFulfillHTLC, data); } diff --git a/fuzz/src/peer_crypt.rs b/fuzz/src/peer_crypt.rs index e0ff02f04..176a7caab 100644 --- a/fuzz/src/peer_crypt.rs +++ b/fuzz/src/peer_crypt.rs @@ -2,6 +2,8 @@ use lightning::ln::peer_channel_encryptor::PeerChannelEncryptor; use secp256k1::key::{PublicKey,SecretKey}; +use utils::test_logger; + #[inline] fn slice_to_be16(v: &[u8]) -> u16 { ((v[0] as u16) << 8*1) | @@ -75,6 +77,10 @@ pub fn do_test(data: &[u8]) { } } +pub fn peer_crypt_test(data: &[u8], _out: Out) { + do_test(data); +} + #[no_mangle] pub extern "C" fn peer_crypt_run(data: *const u8, datalen: usize) { do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); diff --git a/fuzz/src/router.rs b/fuzz/src/router.rs index b1a766ba7..7dcf1de72 100644 --- a/fuzz/src/router.rs +++ b/fuzz/src/router.rs @@ -93,7 +93,7 @@ impl ChainWatchInterface for DummyChainWatcher { } #[inline] -pub fn do_test(data: &[u8]) { +pub fn do_test(data: &[u8], out: Out) { let input = Arc::new(InputData { data: data.to_vec(), read_pos: AtomicUsize::new(0), @@ -150,7 +150,7 @@ pub fn do_test(data: &[u8]) { } } - let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned())); + let logger: Arc = Arc::new(test_logger::TestLogger::new("".to_owned(), out)); let chain_monitor = Arc::new(DummyChainWatcher { input: Arc::clone(&input), }); @@ -232,7 +232,11 @@ pub fn do_test(data: &[u8]) { } } +pub fn router_test(data: &[u8], out: Out) { + do_test(data, out); +} + #[no_mangle] pub extern "C" fn router_run(data: *const u8, datalen: usize) { - do_test(unsafe { std::slice::from_raw_parts(data, datalen) }); + do_test(unsafe { std::slice::from_raw_parts(data, datalen) }, test_logger::DevNull {}); } diff --git a/fuzz/src/utils/mod.rs b/fuzz/src/utils/mod.rs index a7d7c32d5..79dc5d24b 100644 --- a/fuzz/src/utils/mod.rs +++ b/fuzz/src/utils/mod.rs @@ -1 +1 @@ -pub(crate) mod test_logger; +pub mod test_logger; diff --git a/fuzz/src/utils/test_logger.rs b/fuzz/src/utils/test_logger.rs index 097d001db..cd037ad81 100644 --- a/fuzz/src/utils/test_logger.rs +++ b/fuzz/src/utils/test_logger.rs @@ -1,23 +1,55 @@ use lightning::util::logger::{Logger, Record}; -pub struct TestLogger { - #[cfg(test)] +use std::sync::{Arc, Mutex}; +use std::io::Write; + +pub trait Output : Clone + Sync + Send + 'static { + fn locked_write(&self, data: &[u8]); +} + +#[derive(Clone)] +pub struct DevNull {} +impl Output for DevNull { + fn locked_write(&self, _data: &[u8]) {} +} +#[derive(Clone)] +pub struct StringBuffer(Arc>); +impl Output for StringBuffer { + fn locked_write(&self, data: &[u8]) { + self.0.lock().unwrap().push_str(&String::from_utf8(data.to_vec()).unwrap()); + } +} +impl StringBuffer { + pub fn new() -> Self { + Self(Arc::new(Mutex::new(String::new()))) + } + pub fn into_string(self) -> String { + Arc::try_unwrap(self.0).unwrap().into_inner().unwrap() + } +} + +pub struct TestLogger { id: String, + out: Out, +} +impl TestLogger { + pub fn new(id: String, out: Out) -> TestLogger { + TestLogger { id, out } + } } -impl TestLogger { - pub fn new(_id: String) -> TestLogger { - TestLogger { - #[cfg(test)] - id: _id - } +struct LockedWriteAdapter<'a, Out: Output>(&'a Out); +impl<'a, Out: Output> Write for LockedWriteAdapter<'a, Out> { + fn write(&mut self, data: &[u8]) -> Result { + self.0.locked_write(data); + Ok(data.len()) } + fn flush(&mut self) -> Result<(), std::io::Error> { Ok(()) } } -impl Logger for TestLogger { +impl Logger for TestLogger { fn log(&self, record: &Record) { - #[cfg(test)] - println!("{:<5} {} [{} : {}, {}] {}", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args); - #[cfg(not(test))] - let _ = format!("{}", record.args); + write!(LockedWriteAdapter(&self.out), + "{:<5} {} [{} : {}, {}] {}\n", record.level.to_string(), self.id, record.module_path, record.file, record.line, record.args) + .unwrap(); } } -- 2.39.5