| 1 |
use arbitrary::{Result, Unstructured}; |
| 2 |
use wasmparser::WasmFeatures; |
| 3 |
#[cfg(feature = "wasmtime")] |
| 4 |
use wasmtime::*; |
| 5 |
|
| 6 |
#[cfg(feature = "wasmtime")] |
| 7 |
#[path = "../../crates/fuzz-stats/src/lib.rs"] |
| 8 |
pub mod fuzz_stats; |
| 9 |
|
| 10 |
// Define a fuzz target that accepts arbitrary |
| 11 |
// `Module`s as input. |
| 12 |
pub fn run(u: &mut Unstructured<'_>) -> Result<()> { |
| 13 |
// Use data to generate a random wasm module |
| 14 |
let (wasm_bytes, config) = crate::generate_valid_module(u, |config, _| { |
| 15 |
config.disallow_traps = true; |
| 16 |
config.threads_enabled = false; |
| 17 |
config.exceptions_enabled = false; |
| 18 |
config.gc_enabled = false; |
| 19 |
config.max_memory32_bytes = config.max_memory32_bytes.min(1 << 18); |
| 20 |
config.max_memory64_bytes = config.max_memory64_bytes.min(1 << 18); |
| 21 |
|
| 22 |
// NB: should re-enable once wasmtime implements the table64 extension |
| 23 |
// to the memory64 proposal. |
| 24 |
config.memory64_enabled = false; |
| 25 |
Ok(()) |
| 26 |
})?; |
| 27 |
validate_module(&wasm_bytes); |
| 28 |
|
| 29 |
// Don't try to run these modules until we update to Wasmtime >=23. |
| 30 |
if config.custom_page_sizes_enabled { |
| 31 |
return Ok(()); |
| 32 |
} |
| 33 |
// Not implemented in wasmtime at this time. |
| 34 |
if config.wide_arithmetic_enabled { |
| 35 |
return Ok(()); |
| 36 |
} |
| 37 |
|
| 38 |
#[cfg(feature = "wasmtime")] |
| 39 |
{ |
| 40 |
// Configure the engine, module, and store |
| 41 |
let mut eng_conf = wasmtime::Config::new(); |
| 42 |
eng_conf.wasm_memory64(true); |
| 43 |
eng_conf.wasm_multi_memory(true); |
| 44 |
eng_conf.wasm_tail_call(true); |
| 45 |
eng_conf.consume_fuel(true); |
| 46 |
let engine = Engine::new(&eng_conf).unwrap(); |
| 47 |
let module = match Module::from_binary(&engine, &wasm_bytes) { |
| 48 |
Ok(m) => m, |
| 49 |
// Waiting on wasmtime to get published to fix this. |
| 50 |
Err(e) if format!("{e:?}").contains("unsupported init expr in element section") => { |
| 51 |
return Ok(()); |
| 52 |
} |
| 53 |
Err(e) => panic!("failed to compile module {e:?}"), |
| 54 |
}; |
| 55 |
|
| 56 |
// Call all exported functions |
| 57 |
for export in module.exports() { |
| 58 |
match export.ty() { |
| 59 |
ExternType::Func(func_ty) => { |
| 60 |
let mut store = Store::new( |
| 61 |
&engine, |
| 62 |
fuzz_stats::limits::StoreLimits { |
| 63 |
remaining_memory: 1 << 30, |
| 64 |
oom: false, |
| 65 |
}, |
| 66 |
); |
| 67 |
store.limiter(|s| s as &mut dyn ResourceLimiter); |
| 68 |
store.set_fuel(1_000).unwrap(); |
| 69 |
|
| 70 |
// Instantiate the module |
| 71 |
let inst_result = fuzz_stats::dummy::dummy_imports(&mut store, &module) |
| 72 |
.and_then(|imports| Instance::new(&mut store, &module, &imports)); |
| 73 |
let instance = match inst_result { |
| 74 |
Ok(r) => r, |
| 75 |
Err(err) => return Ok(check_err(err)), |
| 76 |
}; |
| 77 |
|
| 78 |
let args = fuzz_stats::dummy::dummy_values(func_ty.params()); |
| 79 |
let mut results = fuzz_stats::dummy::dummy_values(func_ty.results()); |
| 80 |
let func = instance.get_func(&mut store, export.name()).unwrap(); |
| 81 |
match func.call(&mut store, &args, &mut results) { |
| 82 |
Ok(_) => {} |
| 83 |
Err(err) => check_err(err), |
| 84 |
} |
| 85 |
} |
| 86 |
_ => continue, |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
fn check_err(err: anyhow::Error) { |
| 91 |
// Allow stack overflow since this generally can't be protected |
| 92 |
// against as it's an implementation detail of cranelift we could |
| 93 |
// expose regardless of the limits placed on the function. |
| 94 |
if let Some(wasmtime::Trap::StackOverflow) = err.downcast_ref::<wasmtime::Trap>() { |
| 95 |
return; |
| 96 |
} |
| 97 |
|
| 98 |
// Allow out of fuel on module instantiation |
| 99 |
if let Some(wasmtime::Trap::OutOfFuel) = err.downcast_ref::<wasmtime::Trap>() { |
| 100 |
return; |
| 101 |
} |
| 102 |
|
| 103 |
let s = err.to_string(); |
| 104 |
// Allow "nominal" traps such as running out of fuel and the |
| 105 |
// module trying to allocate more resources than we'd like to |
| 106 |
// allow it (e.g. lots of memories or lots of tables). |
| 107 |
if s.contains("all fuel consumed") || s.contains("Insufficient resources") { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// Otherwise though this is a bug. |
| 112 |
panic!("generated wasm trapped in non-trapping mode: {}", err) |
| 113 |
} |
| 114 |
} |
| 115 |
Ok(()) |
| 116 |
} |
| 117 |
|
| 118 |
fn validate_module(wasm_bytes: &Vec<u8>) { |
| 119 |
// Validate the module or component and assert that it passes validation. |
| 120 |
let mut validator = wasmparser::Validator::new_with_features(WasmFeatures::all()); |
| 121 |
if let Err(e) = validator.validate_all(wasm_bytes) { |
| 122 |
panic!("Invalid module: {e}"); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
#[test] |
| 127 |
fn smoke() { |
| 128 |
super::test::test_n_times(100, run); |
| 129 |
} |