Path: blob/main/fuzz/fuzz_targets/instantiate-many.rs
1690 views
//! This fuzz target is used to test multiple concurrent instantiations from1//! multiple modules.23#![no_main]45use libfuzzer_sys::arbitrary::{Result, Unstructured};6use libfuzzer_sys::fuzz_target;7use wasmtime_fuzzing::single_module_fuzzer::KnownValid;8use wasmtime_fuzzing::{generators, oracles};910const MAX_MODULES: usize = 5;1112fuzz_target!(|data: &[u8]| {13// errors in `run` have to do with not enough input in `data`, which we14// ignore here since it doesn't affect how we'd like to fuzz.15let _ = execute_one(data);16});1718fn execute_one(data: &[u8]) -> Result<()> {19let mut u = Unstructured::new(data);20let mut config: generators::Config = u.arbitrary()?;2122// Don't generate start functions23// No wasm code execution is necessary for this fuzz target and thus we don't24// use timeouts or ensure that the generated wasm code will terminate.25config.module_config.config.allow_start_export = false;2627// Wasm linear memories take roughly ~8gb of virtual address space. Down28// below we could instantiate up to 300 modules. Conservatively estimating29// that we have 46 bits of address space to work with (technically 48 on30// x86_64, but take some out for kernel stuff and some for asan stuff) that31// gives us a budget of ~27 memories per instance. Reduce that a bit further32// and make sure that no instance has more than 10 linear memories to ensure33// that even if the maximum were created it should still fit in the linear34// address space.35config.module_config.config.max_memories = config.module_config.config.max_memories.min(10);3637// Create the modules to instantiate38let modules = (0..u.int_in_range(1..=MAX_MODULES)?)39.map(|_| Ok(config.generate(&mut u, None)?.to_bytes()))40.collect::<Result<Vec<_>>>()?;4142let max_instances = match &config.wasmtime.strategy {43generators::InstanceAllocationStrategy::OnDemand => u.int_in_range(1..=100)?,44generators::InstanceAllocationStrategy::Pooling(config) => config.total_core_instances,45};4647// Front-load with instantiation commands48let mut commands: Vec<oracles::Command> = (0..u.int_in_range(1..=max_instances)?)49.map(|_| Ok(oracles::Command::Instantiate(u.arbitrary()?)))50.collect::<Result<_>>()?;5152// Then add some more arbitrary commands53commands.extend(54(0..u.int_in_range(0..=2 * max_instances)?)55.map(|_| u.arbitrary())56.collect::<Result<Vec<_>>>()?,57);5859oracles::instantiate_many(&modules, KnownValid::Yes, &config, &commands);6061Ok(())62}636465