]> git.bitcoin.ninja Git - rust-lightning/blob - fuzz/src/bin/msg_funding_created_target.rs
Merge pull request #593 from TheBlueMatt/2020-04-par-fuzz-check
[rust-lightning] / fuzz / src / bin / msg_funding_created_target.rs
1 // This file is auto-generated by gen_target.sh based on target_template.txt
2 // To modify it, modify target_template.txt and run gen_target.sh instead.
3
4 #![cfg_attr(feature = "libfuzzer_fuzz", no_main)]
5
6 extern crate lightning_fuzz;
7 use lightning_fuzz::msg_targets::msg_funding_created::*;
8
9 use std::fs;
10 use std::io::Read;
11
12 #[cfg(feature = "afl")]
13 #[macro_use] extern crate afl;
14 #[cfg(feature = "afl")]
15 fn main() {
16         fuzz!(|data| {
17                 msg_funding_created_run(data.as_ptr(), data.len());
18         });
19 }
20
21 #[cfg(feature = "honggfuzz")]
22 #[macro_use] extern crate honggfuzz;
23 #[cfg(feature = "honggfuzz")]
24 fn main() {
25         loop {
26                 fuzz!(|data| {
27                         msg_funding_created_run(data.as_ptr(), data.len());
28                 });
29         }
30 }
31
32 #[cfg(feature = "libfuzzer_fuzz")]
33 #[macro_use] extern crate libfuzzer_sys;
34 #[cfg(feature = "libfuzzer_fuzz")]
35 fuzz_target!(|data: &[u8]| {
36         msg_funding_created_run(data.as_ptr(), data.len());
37 });
38
39 #[cfg(feature = "stdin_fuzz")]
40 fn main() {
41         let mut data = Vec::with_capacity(8192);
42         std::io::stdin().read_to_end(&mut data).unwrap();
43         msg_funding_created_run(data.as_ptr(), data.len());
44 }
45
46 #[test]
47 fn run_test_cases() {
48         use lightning_fuzz::utils::test_logger::StringBuffer;
49
50         use std::sync::{atomic, Arc};
51         {
52                 let data: Vec<u8> = vec![0];
53                 msg_funding_created_run(data.as_ptr(), data.len());
54         }
55         let mut threads = Vec::new();
56         let threads_running = Arc::new(atomic::AtomicUsize::new(0));
57         if let Ok(tests) = fs::read_dir("test_cases/msg_funding_created") {
58                 for test in tests {
59                         let mut data: Vec<u8> = Vec::new();
60                         let path = test.unwrap().path();
61                         fs::File::open(&path).unwrap().read_to_end(&mut data).unwrap();
62                         threads_running.fetch_add(1, atomic::Ordering::AcqRel);
63
64                         let thread_count_ref = Arc::clone(&threads_running);
65                         let main_thread_ref = std::thread::current();
66                         threads.push((path.file_name().unwrap().to_str().unwrap().to_string(),
67                                 std::thread::spawn(move || {
68                                         let string_logger = StringBuffer::new();
69
70                                         let panic_logger = string_logger.clone();
71                                         let res = if ::std::panic::catch_unwind(move || {
72                                                 msg_funding_created_test(&data, panic_logger);
73                                         }).is_err() {
74                                                 Some(string_logger.into_string())
75                                         } else { None };
76                                         thread_count_ref.fetch_sub(1, atomic::Ordering::AcqRel);
77                                         main_thread_ref.unpark();
78                                         res
79                                 })
80                         ));
81                         while threads_running.load(atomic::Ordering::Acquire) > 32 {
82                                 std::thread::park();
83                         }
84                 }
85         }
86         for (test, thread) in threads.drain(..) {
87                 if let Some(output) = thread.join().unwrap() {
88                         println!("Output of {}:\n{}", test, output);
89                         panic!();
90                 }
91         }
92 }