Path: blob/main/crates/fuzzing/src/generators/module.rs
1693 views
//! Generate a Wasm module and the configuration for generating it.12use arbitrary::{Arbitrary, Unstructured};3use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};45/// Default module-level configuration for fuzzing Wasmtime.6///7/// Internally this uses `wasm-smith`'s own `Config` but we further refine8/// the defaults here as well.9#[derive(Debug, Clone)]10#[expect(missing_docs, reason = "self-describing fields")]11pub struct ModuleConfig {12pub config: wasm_smith::Config,1314// These knobs aren't exposed in `wasm-smith` at this time but are exposed15// in our `*.wast` testing so keep knobs here so they can be read during16// config-to-`wasmtime::Config` translation.17pub function_references_enabled: bool,18pub component_model_async: bool,19pub component_model_async_builtins: bool,20pub component_model_async_stackful: bool,21pub component_model_error_context: bool,22pub component_model_gc: bool,23pub legacy_exceptions: bool,24}2526impl<'a> Arbitrary<'a> for ModuleConfig {27fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<ModuleConfig> {28let mut config = wasm_smith::Config::arbitrary(u)?;2930// This list is intended to be the definitive source of truth for31// what's at least possible to fuzz within Wasmtime. This is a32// combination of features in `wasm-smith` where some proposals are33// on-by-default (as determined by fuzz input) and others are34// off-by-default (as they aren't stage4+). Wasmtime will default-fuzz35// proposals that a pre-stage-4 to test our own implementation. Wasmtime36// might also unconditionally disable proposals that it doesn't37// implement yet which are stage4+. This is intended to be an exhaustive38// list of all the wasm proposals that `wasm-smith` supports and the39// fuzzing status within Wasmtime too.40let _ = config.multi_value_enabled;41let _ = config.saturating_float_to_int_enabled;42let _ = config.sign_extension_ops_enabled;43let _ = config.bulk_memory_enabled;44let _ = config.reference_types_enabled;45let _ = config.simd_enabled;46let _ = config.relaxed_simd_enabled;47let _ = config.tail_call_enabled;48let _ = config.extended_const_enabled;49let _ = config.gc_enabled;50let _ = config.exceptions_enabled;51config.custom_page_sizes_enabled = u.arbitrary()?;52config.wide_arithmetic_enabled = u.arbitrary()?;53config.memory64_enabled = u.ratio(1, 20)?;54config.threads_enabled = u.ratio(1, 20)?;55// Allow multi-memory but make it unlikely56if u.ratio(1, 20)? {57config.max_memories = config.max_memories.max(2);58} else {59config.max_memories = 1;60}61// ... NB: if you add something above this line please be sure to update62// `docs/stability-wasm-proposals.md`6364// We get better differential execution when we disallow traps, so we'll65// do that most of the time.66config.disallow_traps = u.ratio(9, 10)?;6768Ok(ModuleConfig {69component_model_async: false,70component_model_async_builtins: false,71component_model_async_stackful: false,72component_model_error_context: false,73component_model_gc: false,74legacy_exceptions: false,75function_references_enabled: config.gc_enabled,76config,77})78}79}8081impl ModuleConfig {82/// Uses this configuration and the supplied source of data to generate a83/// Wasm module.84///85/// If a `default_fuel` is provided, the resulting module will be configured86/// to ensure termination; as doing so will add an additional global to the87/// module, the pooling allocator, if configured, must also have its globals88/// limit updated.89pub fn generate(90&self,91input: &mut Unstructured<'_>,92default_fuel: Option<u32>,93) -> arbitrary::Result<wasm_smith::Module> {94crate::init_fuzzing();9596// If requested, save `*.{dna,json}` files for recreating this module97// in wasm-tools alone.98let input_before = if log::log_enabled!(log::Level::Debug) {99let len = input.len();100Some(input.peek_bytes(len).unwrap().to_vec())101} else {102None103};104105let mut module = wasm_smith::Module::new(self.config.clone(), input)?;106107if let Some(before) = input_before {108static GEN_CNT: AtomicUsize = AtomicUsize::new(0);109let used = before.len() - input.len();110let i = GEN_CNT.fetch_add(1, Relaxed);111let dna = format!("testcase{i}.dna");112let config = format!("testcase{i}.json");113log::debug!("writing `{dna}` and `{config}`");114std::fs::write(&dna, &before[..used]).unwrap();115std::fs::write(&config, serde_json::to_string_pretty(&self.config).unwrap()).unwrap();116}117118if let Some(default_fuel) = default_fuel {119module.ensure_termination(default_fuel).unwrap();120}121122Ok(module)123}124}125126127