2021-12-24 18:39:58 -06:00
|
|
|
#![no_main]
|
|
|
|
use libfuzzer_sys::{arbitrary, fuzz_target};
|
2022-11-29 19:32:38 -06:00
|
|
|
use std::sync::atomic::{AtomicU64, Ordering};
|
2021-12-24 18:39:58 -06:00
|
|
|
|
2023-02-16 18:20:54 -06:00
|
|
|
use waffle::{FrontendOptions, Module};
|
2021-12-24 18:39:58 -06:00
|
|
|
|
2021-12-25 01:56:21 -06:00
|
|
|
fn reject(bytes: &[u8]) -> bool {
|
2021-12-24 18:39:58 -06:00
|
|
|
let parser = wasmparser::Parser::new(0);
|
2021-12-24 19:03:13 -06:00
|
|
|
let mut has_start = false;
|
2021-12-25 01:56:21 -06:00
|
|
|
let mut has_global_set = false;
|
|
|
|
let mut num_globals = 0;
|
2021-12-24 18:39:58 -06:00
|
|
|
for payload in parser.parse_all(bytes) {
|
|
|
|
match payload.unwrap() {
|
|
|
|
wasmparser::Payload::CodeSectionEntry(body) => {
|
|
|
|
for op in body.get_operators_reader().unwrap() {
|
|
|
|
let op = op.unwrap();
|
|
|
|
match op {
|
2021-12-25 01:56:21 -06:00
|
|
|
wasmparser::Operator::GlobalSet { .. } => {
|
|
|
|
has_global_set = true;
|
|
|
|
}
|
2021-12-24 18:39:58 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 19:03:13 -06:00
|
|
|
wasmparser::Payload::StartSection { .. } => {
|
|
|
|
has_start = true;
|
|
|
|
}
|
2021-12-25 01:56:21 -06:00
|
|
|
wasmparser::Payload::ExportSection(mut reader) => {
|
|
|
|
for _ in 0..reader.get_count() {
|
|
|
|
let e = reader.read().unwrap();
|
|
|
|
match &e.kind {
|
|
|
|
&wasmparser::ExternalKind::Global => {
|
|
|
|
num_globals += 1;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 18:39:58 -06:00
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 01:56:21 -06:00
|
|
|
|
|
|
|
if !has_start || !has_global_set || num_globals < 1 {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
false
|
2021-12-24 18:39:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Config;
|
|
|
|
|
|
|
|
impl<'a> arbitrary::Arbitrary<'a> for Config {
|
|
|
|
fn arbitrary(_u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
|
|
|
|
Ok(Config)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl wasm_smith::Config for Config {
|
|
|
|
fn min_funcs(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
fn max_funcs(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
fn min_memories(&self) -> u32 {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
fn max_memories(&self) -> usize {
|
|
|
|
1
|
|
|
|
}
|
|
|
|
fn min_globals(&self) -> usize {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
fn max_globals(&self) -> usize {
|
|
|
|
10
|
|
|
|
}
|
|
|
|
fn min_tables(&self) -> u32 {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
fn max_tables(&self) -> usize {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
fn min_imports(&self) -> usize {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
fn max_imports(&self) -> usize {
|
|
|
|
0
|
|
|
|
}
|
|
|
|
fn min_exports(&self) -> usize {
|
|
|
|
12
|
|
|
|
}
|
|
|
|
fn max_exports(&self) -> usize {
|
|
|
|
12
|
|
|
|
}
|
|
|
|
fn allow_start_export(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
fn canonicalize_nans(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2021-12-24 19:03:13 -06:00
|
|
|
fn max_memory_pages(&self, _is_64: bool) -> u64 {
|
|
|
|
1
|
|
|
|
}
|
2021-12-24 18:39:58 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fuzz_target!(|module: wasm_smith::ConfiguredModule<Config>| {
|
|
|
|
let _ = env_logger::try_init();
|
|
|
|
log::debug!("original module: {:?}", module.module);
|
|
|
|
|
|
|
|
let orig_bytes = module.module.to_bytes();
|
|
|
|
|
2021-12-25 01:56:21 -06:00
|
|
|
if reject(&orig_bytes[..]) {
|
|
|
|
log::debug!("Discarding fuzz run. Body:\n{:?}", module);
|
2021-12-24 18:39:58 -06:00
|
|
|
return;
|
2021-12-25 01:56:21 -06:00
|
|
|
} else {
|
|
|
|
log::info!("body: {:?}", module);
|
2021-12-24 18:39:58 -06:00
|
|
|
}
|
|
|
|
|
2022-11-29 23:15:02 -06:00
|
|
|
let mut config = wasmtime::Config::default();
|
|
|
|
config.consume_fuel(true);
|
|
|
|
let engine = wasmtime::Engine::new(&config).unwrap();
|
2021-12-24 19:03:13 -06:00
|
|
|
let orig_module =
|
|
|
|
wasmtime::Module::new(&engine, &orig_bytes[..]).expect("failed to parse original wasm");
|
2022-11-29 23:15:02 -06:00
|
|
|
let mut orig_store = wasmtime::Store::new(&engine, ());
|
|
|
|
orig_store.out_of_fuel_trap();
|
|
|
|
orig_store.add_fuel(10000).unwrap();
|
|
|
|
let orig_instance = wasmtime::Instance::new(&mut orig_store, &orig_module, &[]);
|
2021-12-24 18:39:58 -06:00
|
|
|
let orig_instance = match orig_instance {
|
|
|
|
Ok(orig_instance) => orig_instance,
|
|
|
|
Err(e) => {
|
2021-12-25 01:56:21 -06:00
|
|
|
log::info!("cannot run start on orig intsance ({:?}); discarding", e);
|
2021-12-24 18:39:58 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-02-16 18:20:54 -06:00
|
|
|
let mut parsed_module =
|
|
|
|
Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap();
|
|
|
|
parsed_module.per_func_body(|body| body.optimize());
|
2022-11-29 19:32:38 -06:00
|
|
|
let roundtrip_bytes = parsed_module.to_wasm_bytes().unwrap();
|
2021-12-24 18:39:58 -06:00
|
|
|
|
2021-12-24 19:13:23 -06:00
|
|
|
if let Ok(filename) = std::env::var("FUZZ_DUMP_WASM") {
|
|
|
|
std::fs::write(format!("{}_orig.wasm", filename), &orig_bytes[..]).unwrap();
|
|
|
|
std::fs::write(format!("{}_roundtrip.wasm", filename), &roundtrip_bytes[..]).unwrap();
|
|
|
|
}
|
|
|
|
|
2022-11-29 19:32:38 -06:00
|
|
|
let total = TOTAL.fetch_add(1, Ordering::Relaxed);
|
|
|
|
|
2021-12-24 19:03:13 -06:00
|
|
|
let roundtrip_module = wasmtime::Module::new(&engine, &roundtrip_bytes[..])
|
|
|
|
.expect("failed to parse roundtripped wasm");
|
2022-11-29 23:15:02 -06:00
|
|
|
let mut roundtrip_store = wasmtime::Store::new(&engine, ());
|
|
|
|
roundtrip_store.out_of_fuel_trap();
|
|
|
|
// After roundtrip, fuel consumption rate may differ. That's fine;
|
|
|
|
// what matters is that it terminated above without a trap (hence
|
|
|
|
// halts in a reasonable time).
|
|
|
|
roundtrip_store.add_fuel(u64::MAX).unwrap();
|
|
|
|
let roundtrip_instance = wasmtime::Instance::new(&mut roundtrip_store, &roundtrip_module, &[])
|
2021-12-24 19:03:13 -06:00
|
|
|
.expect("cannot instantiate roundtripped wasm");
|
|
|
|
|
|
|
|
// Ensure exports are equal.
|
|
|
|
|
|
|
|
let a_globals: Vec<_> = orig_instance
|
2022-11-29 23:15:02 -06:00
|
|
|
.exports(&mut orig_store)
|
2021-12-24 19:03:13 -06:00
|
|
|
.filter_map(|e| e.into_global())
|
|
|
|
.collect();
|
2022-11-29 23:15:02 -06:00
|
|
|
let a_globals: Vec<wasmtime::Val> = a_globals
|
|
|
|
.into_iter()
|
|
|
|
.map(|g| g.get(&mut orig_store))
|
|
|
|
.collect();
|
2021-12-24 19:03:13 -06:00
|
|
|
let a_mems: Vec<wasmtime::Memory> = orig_instance
|
2022-11-29 23:15:02 -06:00
|
|
|
.exports(&mut orig_store)
|
2021-12-24 19:03:13 -06:00
|
|
|
.filter_map(|e| e.into_memory())
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
let b_globals: Vec<_> = roundtrip_instance
|
2022-11-29 23:15:02 -06:00
|
|
|
.exports(&mut roundtrip_store)
|
2021-12-24 19:03:13 -06:00
|
|
|
.filter_map(|e| e.into_global())
|
|
|
|
.collect();
|
2022-11-29 23:15:02 -06:00
|
|
|
let b_globals: Vec<wasmtime::Val> = b_globals
|
|
|
|
.into_iter()
|
|
|
|
.map(|g| g.get(&mut roundtrip_store))
|
|
|
|
.collect();
|
2021-12-24 19:03:13 -06:00
|
|
|
let b_mems: Vec<wasmtime::Memory> = roundtrip_instance
|
2022-11-29 23:15:02 -06:00
|
|
|
.exports(&mut roundtrip_store)
|
2021-12-24 19:03:13 -06:00
|
|
|
.filter_map(|e| e.into_memory())
|
|
|
|
.collect();
|
|
|
|
|
2021-12-24 19:06:59 -06:00
|
|
|
log::info!("a_globals = {:?}", a_globals);
|
|
|
|
log::info!("b_globals = {:?}", b_globals);
|
|
|
|
|
2021-12-24 19:03:13 -06:00
|
|
|
assert_eq!(a_globals.len(), b_globals.len());
|
|
|
|
for (a, b) in a_globals.into_iter().zip(b_globals.into_iter()) {
|
|
|
|
match (a, b) {
|
|
|
|
(wasmtime::Val::I32(a), wasmtime::Val::I32(b)) => assert_eq!(a, b),
|
|
|
|
(wasmtime::Val::I64(a), wasmtime::Val::I64(b)) => assert_eq!(a, b),
|
|
|
|
(wasmtime::Val::F32(a), wasmtime::Val::F32(b)) => assert_eq!(a, b),
|
|
|
|
(wasmtime::Val::F64(a), wasmtime::Val::F64(b)) => assert_eq!(a, b),
|
|
|
|
_ => panic!("mismatching types"),
|
2021-12-24 18:39:58 -06:00
|
|
|
}
|
|
|
|
}
|
2021-12-24 19:03:13 -06:00
|
|
|
|
|
|
|
assert_eq!(a_mems.len(), b_mems.len());
|
|
|
|
for (a, b) in a_mems.into_iter().zip(b_mems.into_iter()) {
|
2022-11-29 23:15:02 -06:00
|
|
|
let a_data = a.data(&orig_store);
|
|
|
|
let b_data = b.data(&roundtrip_store);
|
2021-12-24 19:03:13 -06:00
|
|
|
assert_eq!(a_data, b_data);
|
|
|
|
}
|
2022-11-29 19:32:38 -06:00
|
|
|
|
|
|
|
success(total);
|
2021-12-24 18:39:58 -06:00
|
|
|
});
|
2022-11-29 19:32:38 -06:00
|
|
|
|
|
|
|
static TOTAL: AtomicU64 = AtomicU64::new(0);
|
|
|
|
static SUCCESS: AtomicU64 = AtomicU64::new(0);
|
|
|
|
|
|
|
|
fn success(total: u64) {
|
|
|
|
let value = SUCCESS.fetch_add(1, Ordering::Relaxed);
|
2022-11-29 20:23:50 -06:00
|
|
|
if value % 100 == 0 {
|
2022-11-29 19:32:38 -06:00
|
|
|
eprintln!("SUCCESS: {} / TOTAL: {}", value, total);
|
|
|
|
}
|
|
|
|
}
|