|
20 | 20 | //! ```
|
21 | 21 | //!
|
22 | 22 | #![deny(missing_docs)]
|
| 23 | +#[macro_use] |
| 24 | +extern crate lazy_static; |
23 | 25 | extern crate quickcheck;
|
24 | 26 | extern crate rand;
|
25 | 27 | extern crate tempdir;
|
26 | 28 |
|
| 29 | +use std::sync::Mutex; |
| 30 | +use quickcheck::{QuickCheck, StdGen, TestResult}; |
| 31 | +use std::fs::File; |
| 32 | +use std::io::Write; |
| 33 | +use tempdir::TempDir; |
| 34 | +use std::process::{Command, Output}; |
| 35 | +use std::path::PathBuf; |
| 36 | +use std::error::Error; |
| 37 | +use rand::thread_rng; |
| 38 | + |
27 | 39 | /// Contains definitions of and impls for types used to fuzz C declarations.
|
28 | 40 | pub mod fuzzers;
|
| 41 | + |
| 42 | +// Global singleton, manages context across tests. For now that context is |
| 43 | +// only the output_path for inspecting fuzzed headers (if specified). |
| 44 | +struct Context { |
| 45 | + output_path: Option<String>, |
| 46 | +} |
| 47 | + |
| 48 | +// Initialize global context. |
| 49 | +lazy_static! { |
| 50 | + static ref CONTEXT: Mutex<Context> = Mutex::new(Context { output_path: None }); |
| 51 | +} |
| 52 | + |
| 53 | +// Passes fuzzed header to the `csmith-fuzzing/predicate.py` script, returns |
| 54 | +// output of the associated command. |
| 55 | +fn run_predicate_script(header: fuzzers::HeaderC) -> Result<Output, Box<Error>> { |
| 56 | + let dir = TempDir::new("bindgen_prop")?; |
| 57 | + let header_path = dir.path().join("prop_test.h"); |
| 58 | + |
| 59 | + let mut header_file = File::create(&header_path)?; |
| 60 | + header_file.write_all(header.to_string().as_bytes())?; |
| 61 | + header_file.sync_all()?; |
| 62 | + |
| 63 | + let header_path_string; |
| 64 | + match header_path.into_os_string().into_string() { |
| 65 | + Ok(s) => header_path_string = s, |
| 66 | + Err(_) => return Err(From::from("error converting path into String")), |
| 67 | + } |
| 68 | + |
| 69 | + let mut predicate_script_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
| 70 | + predicate_script_path.push("../../csmith-fuzzing/predicate.py"); |
| 71 | + |
| 72 | + let predicate_script_path_string; |
| 73 | + match predicate_script_path.into_os_string().into_string() { |
| 74 | + Ok(s) => predicate_script_path_string = s, |
| 75 | + Err(_) => return Err(From::from("error converting path into String")), |
| 76 | + } |
| 77 | + |
| 78 | + // Copy generated temp files to output_path directory for inspection. |
| 79 | + // If `None`, output path not specified, don't copy. |
| 80 | + match CONTEXT.lock().unwrap().output_path { |
| 81 | + Some(ref path) => { |
| 82 | + Command::new("cp") |
| 83 | + .arg("-a") |
| 84 | + .arg(&dir.path().to_str().unwrap()) |
| 85 | + .arg(&path) |
| 86 | + .output()?; |
| 87 | + } |
| 88 | + None => {} |
| 89 | + } |
| 90 | + |
| 91 | + Ok(Command::new(&predicate_script_path_string) |
| 92 | + .arg(&header_path_string) |
| 93 | + .output()?) |
| 94 | +} |
| 95 | + |
| 96 | +// Generatable property. Pass generated headers off to run through the |
| 97 | +// `csmith-fuzzing/predicate.py` script. Success is measured by the success |
| 98 | +// status of that command. |
| 99 | +fn bindgen_prop(header: fuzzers::HeaderC) -> TestResult { |
| 100 | + match run_predicate_script(header) { |
| 101 | + Ok(o) => return TestResult::from_bool(o.status.success()), |
| 102 | + Err(e) => { |
| 103 | + println!("{:?}", e); |
| 104 | + return TestResult::from_bool(false); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/// Instantiate a Quickcheck object and use it to run property tests using |
| 110 | +/// fuzzed C headers generated with types defined in the `fuzzers` module. |
| 111 | +/// Success/Failure is dictated by the result of passing the fuzzed headers |
| 112 | +/// to the `csmith-fuzzing/predicate.py` script. |
| 113 | +pub fn test_bindgen(generate_range: usize, tests: usize, output_path: Option<&str>) { |
| 114 | + match output_path { |
| 115 | + Some(path) => { |
| 116 | + CONTEXT.lock().unwrap().output_path = |
| 117 | + Some(String::from(PathBuf::from(path).to_str().unwrap())); |
| 118 | + } |
| 119 | + None => {} // Path not specified, don't provide output. |
| 120 | + } |
| 121 | + |
| 122 | + QuickCheck::new() |
| 123 | + .tests(tests) |
| 124 | + .gen(StdGen::new(thread_rng(), generate_range)) |
| 125 | + .quickcheck(bindgen_prop as fn(fuzzers::HeaderC) -> TestResult) |
| 126 | +} |
0 commit comments