wasmi/fuzz/fuzz_targets/load_spec.rs

54 lines
1.2 KiB
Rust
Raw Normal View History

2018-02-15 22:57:40 +00:00
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;
extern crate wabt;
extern crate wasmi;
extern crate tempdir;
use std::fs::File;
use std::io::Write;
2018-02-15 23:09:03 +00:00
use std::process::{Command, Stdio};
2018-02-15 22:57:40 +00:00
2018-02-17 11:26:52 +00:00
fn run_spec(data: &[u8]) -> Result<(), ()> {
2018-02-19 16:05:15 +00:00
let temp_dir = tempdir::TempDir::new("spec").unwrap();
let mut seed_path = temp_dir.path().to_path_buf();
seed_path.push("test.wasm");
2018-02-15 22:57:40 +00:00
{
let mut seedfile =
2018-02-19 16:05:15 +00:00
File::create(&seed_path).expect("open temporary file for writing to store fuzzer input");
2018-02-15 22:57:40 +00:00
seedfile.write_all(data).expect(
"write fuzzer input to temporary file",
);
seedfile.flush().expect(
"flush fuzzer input to temporary file before starting wasm-opt",
);
}
2018-02-17 11:26:52 +00:00
let exit_status = Command::new("wasm")
.arg("-d")
2018-02-19 16:05:15 +00:00
.arg(&seed_path)
2018-02-15 23:09:03 +00:00
.stdout(Stdio::null())
2018-02-15 23:10:02 +00:00
.stderr(Stdio::null())
2018-02-15 22:57:40 +00:00
.status()
.expect("failed to execute `wasm`");
2018-02-17 11:26:52 +00:00
if exit_status.success() {
Ok(())
} else {
Err(())
}
}
fn run_wasmi(data: &[u8]) -> Result<(), ()> {
let _ = wasmi::Module::from_buffer(data).map_err(|_| ())?;
Ok(())
}
fuzz_target!(|data: &[u8]| {
let wasmi_result = run_wasmi(data);
let wasm_result = run_spec(data);
assert_eq!(wasmi_result.is_ok(), wasm_result.is_ok());
2018-02-15 22:57:40 +00:00
});