From f3210799d5e1483cfa0543860f5afa6de9d0ff35 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 01:41:00 +0100 Subject: [PATCH 1/6] Fuzz: skip unsupported features in every target through one helper. `parse_ir`, `roundtrip_roundtrip`, `differential` and `opt_diff` unwrapped the result of `Module::from_wasm_bytes`, and `roundtrip`, `differential` and `opt_diff` unwrapped the result of `expand_all_funcs`. A wasm-smith module that uses a feature the frontend does not support, such as a GC type in the type section, made these targets panic with `FrontendError::UnsupportedFeature`. `roundtrip` skipped `UnsupportedFeature` and `TooLarge` when parsing, but that rule existed only there. Add `fuzzing::skip_unsupported`, which returns `None` for `UnsupportedFeature` and `TooLarge` errors and panics on every other error, and `fuzzing::parse`, which applies it to `Module::from_wasm_bytes`. Every target that parses wasm-smith output now uses them for parsing and for `expand_all_funcs`. --- fuzz/fuzz_targets/differential.rs | 11 ++++++---- fuzz/fuzz_targets/opt_diff.rs | 11 ++++++---- fuzz/fuzz_targets/parse_ir.rs | 5 +---- fuzz/fuzz_targets/roundtrip.rs | 27 ++++++------------------ fuzz/fuzz_targets/roundtrip_roundtrip.rs | 5 +++-- src/fuzzing.rs | 21 ++++++++++++++++++ 6 files changed, 46 insertions(+), 34 deletions(-) diff --git a/fuzz/fuzz_targets/differential.rs b/fuzz/fuzz_targets/differential.rs index ea903e8..3939acd 100644 --- a/fuzz/fuzz_targets/differential.rs +++ b/fuzz/fuzz_targets/differential.rs @@ -2,7 +2,7 @@ use libfuzzer_sys::fuzz_target; use std::sync::atomic::{AtomicU64, Ordering}; -use waffle::{FrontendOptions, Module, OptOptions}; +use waffle::OptOptions; fuzz_target!(|module: waffle::fuzzing::ArbitraryModule| { let module = module.0; @@ -34,9 +34,12 @@ fuzz_target!(|module: waffle::fuzzing::ArbitraryModule| { } }; - let mut parsed_module = - Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap(); - parsed_module.expand_all_funcs().unwrap(); + let Some(mut parsed_module) = waffle::fuzzing::parse(&orig_bytes[..]) else { + return; + }; + if waffle::fuzzing::skip_unsupported(parsed_module.expand_all_funcs()).is_none() { + return; + } parsed_module.per_func_body(|body| body.optimize(&OptOptions::default())); let roundtrip_bytes = parsed_module.to_wasm_bytes().unwrap(); diff --git a/fuzz/fuzz_targets/opt_diff.rs b/fuzz/fuzz_targets/opt_diff.rs index 364def2..d079fb1 100644 --- a/fuzz/fuzz_targets/opt_diff.rs +++ b/fuzz/fuzz_targets/opt_diff.rs @@ -1,7 +1,7 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use waffle::{FrontendOptions, InterpContext, InterpResult, Module, OptOptions}; +use waffle::{InterpContext, InterpResult, OptOptions}; fuzz_target!(|module: waffle::fuzzing::ArbitraryModule| { let module = module.0; @@ -17,9 +17,12 @@ fuzz_target!(|module: waffle::fuzzing::ArbitraryModule| { log::info!("body: {:?}", module); } - let mut parsed_module = - Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap(); - parsed_module.expand_all_funcs().unwrap(); + let Some(mut parsed_module) = waffle::fuzzing::parse(&orig_bytes[..]) else { + return; + }; + if waffle::fuzzing::skip_unsupported(parsed_module.expand_all_funcs()).is_none() { + return; + } let start = parsed_module.start_func.unwrap(); diff --git a/fuzz/fuzz_targets/parse_ir.rs b/fuzz/fuzz_targets/parse_ir.rs index 3c6e50c..508448f 100644 --- a/fuzz/fuzz_targets/parse_ir.rs +++ b/fuzz/fuzz_targets/parse_ir.rs @@ -1,10 +1,7 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use waffle::{FrontendOptions, Module}; - fuzz_target!(|module: wasm_smith::Module| { let _ = env_logger::try_init(); - let _parsed_module = - Module::from_wasm_bytes(&module.to_bytes()[..], &FrontendOptions::default()).unwrap(); + let _parsed_module = waffle::fuzzing::parse(&module.to_bytes()[..]); }); diff --git a/fuzz/fuzz_targets/roundtrip.rs b/fuzz/fuzz_targets/roundtrip.rs index d4959bf..17c3547 100644 --- a/fuzz/fuzz_targets/roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip.rs @@ -1,31 +1,18 @@ #![no_main] use libfuzzer_sys::fuzz_target; -use waffle::{FrontendError, FrontendOptions, Module, OptOptions}; +use waffle::OptOptions; fuzz_target!(|module: wasm_smith::Module| { let _ = env_logger::try_init(); log::debug!("original module: {:?}", module); let orig_bytes = module.to_bytes(); - let mut parsed_module = - match Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()) { - Ok(m) => m, - Err(e) => { - match e.downcast::() { - Ok(FrontendError::UnsupportedFeature(_)) | Ok(FrontendError::TooLarge(_)) => { - // Just skip this case. - return; - } - Ok(e) => { - panic!("Frontend error: {:?}", e); - } - Err(e) => { - panic!("Other error when parsing module: {:?}", e); - } - } - } - }; - parsed_module.expand_all_funcs().unwrap(); + let Some(mut parsed_module) = waffle::fuzzing::parse(&orig_bytes[..]) else { + return; + }; + if waffle::fuzzing::skip_unsupported(parsed_module.expand_all_funcs()).is_none() { + return; + } parsed_module.per_func_body(|body| body.optimize(&OptOptions::default())); let _ = parsed_module.to_wasm_bytes(); }); diff --git a/fuzz/fuzz_targets/roundtrip_roundtrip.rs b/fuzz/fuzz_targets/roundtrip_roundtrip.rs index 1bffc5b..59d581c 100644 --- a/fuzz/fuzz_targets/roundtrip_roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip_roundtrip.rs @@ -7,8 +7,9 @@ fuzz_target!(|module: wasm_smith::Module| { let _ = env_logger::try_init(); log::debug!("original module: {:?}", module); let orig_bytes = module.to_bytes(); - let parsed_module = - Module::from_wasm_bytes(&orig_bytes[..], &FrontendOptions::default()).unwrap(); + let Some(parsed_module) = waffle::fuzzing::parse(&orig_bytes[..]) else { + return; + }; let roundtrip_bytes = parsed_module.to_wasm_bytes().unwrap(); if let Ok(filename) = std::env::var("ROUNDTRIP_WASM_SAVE") { std::fs::write(filename, &roundtrip_bytes[..]).unwrap(); diff --git a/src/fuzzing.rs b/src/fuzzing.rs index 9346740..20905f1 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -1,7 +1,28 @@ //! Fuzzing-specific utilities. +use crate::{FrontendError, FrontendOptions, Module}; use libfuzzer_sys::arbitrary; +/// Returns the value in `result`, or `None` if `result` holds a +/// `FrontendError::UnsupportedFeature` or `FrontendError::TooLarge`. +/// Panics on every other error. +pub fn skip_unsupported(result: anyhow::Result) -> Option { + match result { + Ok(value) => Some(value), + Err(e) => match e.downcast::() { + Ok(FrontendError::UnsupportedFeature(_)) | Ok(FrontendError::TooLarge(_)) => None, + Ok(e) => panic!("Frontend error: {:?}", e), + Err(e) => panic!("Other error: {:?}", e), + }, + } +} + +/// Parses `bytes` with `Module::from_wasm_bytes` and default +/// `FrontendOptions`. Errors are handled as in `skip_unsupported`. +pub fn parse(bytes: &[u8]) -> Option> { + skip_unsupported(Module::from_wasm_bytes(bytes, &FrontendOptions::default())) +} + /// Should this module be rejected early during fuzzing due to an /// unsupported feature? /// From 74bc2fc6cc20db011ae38e581fac7d9a3783bccd Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 01:41:36 +0100 Subject: [PATCH 2/6] Fuzz: generate modules with proposals the frontend supports. `parse_ir`, `roundtrip` and `roundtrip_roundtrip` took `wasm_smith::Module` through its `Arbitrary` implementation, which uses wasm-smith's default configuration. That configuration enables GC, exceptions, threads, tail calls, memory64, relaxed SIMD and extended constant expressions. The frontend rejects GC types, the instructions of the other proposals, and constant expressions with more than one instruction, and it has no representation for memory64 or shared memories. Most inputs therefore ended in a rejected feature. Add `fuzzing::SupportedModule`, which generates a module from an arbitrary `wasm_smith::Config` with these proposals turned off. Limits and the remaining proposals, including bulk memory, reference types, SIMD, multi-value and multi-memory, stay random. The three targets now take `SupportedModule`. `skip_unsupported` still skips inputs that reach an unsupported construct, such as `global.get` in a constant expression or a bulk memory instruction the frontend does not translate. `differential` and `opt_diff` keep `ArbitraryModule`. --- fuzz/fuzz_targets/parse_ir.rs | 3 ++- fuzz/fuzz_targets/roundtrip.rs | 3 ++- fuzz/fuzz_targets/roundtrip_roundtrip.rs | 3 ++- src/fuzzing.rs | 25 ++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/fuzz/fuzz_targets/parse_ir.rs b/fuzz/fuzz_targets/parse_ir.rs index 508448f..5aef497 100644 --- a/fuzz/fuzz_targets/parse_ir.rs +++ b/fuzz/fuzz_targets/parse_ir.rs @@ -1,7 +1,8 @@ #![no_main] use libfuzzer_sys::fuzz_target; -fuzz_target!(|module: wasm_smith::Module| { +fuzz_target!(|module: waffle::fuzzing::SupportedModule| { + let module = module.0; let _ = env_logger::try_init(); let _parsed_module = waffle::fuzzing::parse(&module.to_bytes()[..]); }); diff --git a/fuzz/fuzz_targets/roundtrip.rs b/fuzz/fuzz_targets/roundtrip.rs index 17c3547..71fb46e 100644 --- a/fuzz/fuzz_targets/roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip.rs @@ -3,7 +3,8 @@ use libfuzzer_sys::fuzz_target; use waffle::OptOptions; -fuzz_target!(|module: wasm_smith::Module| { +fuzz_target!(|module: waffle::fuzzing::SupportedModule| { + let module = module.0; let _ = env_logger::try_init(); log::debug!("original module: {:?}", module); let orig_bytes = module.to_bytes(); diff --git a/fuzz/fuzz_targets/roundtrip_roundtrip.rs b/fuzz/fuzz_targets/roundtrip_roundtrip.rs index 59d581c..7d25892 100644 --- a/fuzz/fuzz_targets/roundtrip_roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip_roundtrip.rs @@ -3,7 +3,8 @@ use libfuzzer_sys::fuzz_target; use waffle::{FrontendOptions, Module}; -fuzz_target!(|module: wasm_smith::Module| { +fuzz_target!(|module: waffle::fuzzing::SupportedModule| { + let module = module.0; let _ = env_logger::try_init(); log::debug!("original module: {:?}", module); let orig_bytes = module.to_bytes(); diff --git a/src/fuzzing.rs b/src/fuzzing.rs index 20905f1..12e96eb 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -111,3 +111,28 @@ impl<'a> arbitrary::Arbitrary<'a> for ArbitraryModule { Ok(Self(wasm_smith::Module::new(fuzzing_config(), u)?)) } } + +/// Generates a `wasm_smith::Config` from `u` with the proposals turned +/// off that the frontend rejects or has no representation for. +fn supported_config(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { + let mut config: wasm_smith::Config = u.arbitrary()?; + config.gc_enabled = false; + config.exceptions_enabled = false; + config.threads_enabled = false; + config.tail_call_enabled = false; + config.memory64_enabled = false; + config.relaxed_simd_enabled = false; + config.extended_const_enabled = false; + Ok(config) +} + +/// A `wasm_smith::Module` generated with `supported_config`. +#[derive(Debug)] +pub struct SupportedModule(pub wasm_smith::Module); + +impl<'a> arbitrary::Arbitrary<'a> for SupportedModule { + fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result { + let config = supported_config(u)?; + Ok(Self(wasm_smith::Module::new(config, u)?)) + } +} From 7acbf5bb4b7629d5fbca327acb9598b843762552 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 01:43:07 +0100 Subject: [PATCH 3/6] Fuzz: validate the output of the roundtrip targets. `roundtrip` and `roundtrip_roundtrip` checked only that the backend returned bytes, and `roundtrip` discarded the result of `to_wasm_bytes`. `roundtrip_roundtrip` reparses its output with waffle, which does not validate. A valid input that became an invalid output therefore went unnoticed, for example a function body that refers to a data segment the backend did not emit. Add `fuzzing::validate_output`, which validates bytes with `wasmparser::Validator` and panics with the validator's message on failure. The feature set is the one wasm-smith reports for the configuration the input was generated with (`Config::features`), so the output may use any feature the input could have used and no other. Both targets now unwrap `to_wasm_bytes` and validate every module they serialize. The `fuzzing` feature now enables wasm-smith's `wasmparser` feature, which provides `Config::features`. --- Cargo.toml | 2 +- fuzz/fuzz_targets/roundtrip.rs | 3 ++- fuzz/fuzz_targets/roundtrip_roundtrip.rs | 4 +++- src/fuzzing.rs | 10 ++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index cec721e..a05f4ec 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ dwarf = ["dep:addr2line"] cli = ["dep:structopt", "dep:env_logger"] # Enable fuzzing entrypoints. -fuzzing = ["libfuzzer-sys", "wasm-smith"] +fuzzing = ["libfuzzer-sys", "wasm-smith", "wasm-smith/wasmparser"] [[bin]] name = "waffle-util" diff --git a/fuzz/fuzz_targets/roundtrip.rs b/fuzz/fuzz_targets/roundtrip.rs index 71fb46e..70d5c04 100644 --- a/fuzz/fuzz_targets/roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip.rs @@ -15,5 +15,6 @@ fuzz_target!(|module: waffle::fuzzing::SupportedModule| { return; } parsed_module.per_func_body(|body| body.optimize(&OptOptions::default())); - let _ = parsed_module.to_wasm_bytes(); + let bytes = parsed_module.to_wasm_bytes().unwrap(); + waffle::fuzzing::validate_output(&module, &bytes[..]); }); diff --git a/fuzz/fuzz_targets/roundtrip_roundtrip.rs b/fuzz/fuzz_targets/roundtrip_roundtrip.rs index 7d25892..3a69550 100644 --- a/fuzz/fuzz_targets/roundtrip_roundtrip.rs +++ b/fuzz/fuzz_targets/roundtrip_roundtrip.rs @@ -15,7 +15,9 @@ fuzz_target!(|module: waffle::fuzzing::SupportedModule| { if let Ok(filename) = std::env::var("ROUNDTRIP_WASM_SAVE") { std::fs::write(filename, &roundtrip_bytes[..]).unwrap(); } + waffle::fuzzing::validate_output(&module, &roundtrip_bytes[..]); let parsed_roundtrip_module = Module::from_wasm_bytes(&roundtrip_bytes[..], &FrontendOptions::default()).unwrap(); - let _ = parsed_roundtrip_module.to_wasm_bytes(); + let roundtrip_roundtrip_bytes = parsed_roundtrip_module.to_wasm_bytes().unwrap(); + waffle::fuzzing::validate_output(&module, &roundtrip_roundtrip_bytes[..]); }); diff --git a/src/fuzzing.rs b/src/fuzzing.rs index 12e96eb..f31fbf8 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -136,3 +136,13 @@ impl<'a> arbitrary::Arbitrary<'a> for SupportedModule { Ok(Self(wasm_smith::Module::new(config, u)?)) } } + +/// Validates `bytes` with `wasmparser::Validator` under the features of +/// the configuration `module` was generated with. Panics with the +/// validator's message if validation fails. +pub fn validate_output(module: &wasm_smith::Module, bytes: &[u8]) { + let mut validator = wasmparser::Validator::new_with_features(module.config().features()); + if let Err(e) = validator.validate_all(bytes) { + panic!("Invalid output: {}", e); + } +} From a30dbf0a39eec53655f85e529446ec4763104cd4 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 05:35:31 +0100 Subject: [PATCH 4/6] CI: run the parse_ir fuzz target for 30 seconds. The `check_fuzz` job ran `cargo fuzz check`, which compiles the fuzz targets and never runs them. Run `parse_ir` for 30 seconds after the check. The step uses cargo-fuzz's default release build with debug assertions, and `-s none` as the check does. `roundtrip` and `roundtrip_roundtrip` fail on existing bugs (dropped passive element and data segments, and functions declared only by an overwritten table entry). `differential` and `opt_diff` generate modules that almost never pass `fuzzing::reject`, so a 30-second run compares almost nothing. `irreducible` generates a CFG instead of a wasm module. --- .github/workflows/main.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index bcbef0b..21d22e2 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,6 +26,7 @@ jobs: - uses: actions/checkout@v2 - run: cargo install cargo-fuzz - run: cargo fuzz check --dev -s none + - run: cargo fuzz run -s none parse_ir -- -max_total_time=30 rustfmt: runs-on: ubuntu-latest From 1a138504f5515f9b0bbfe9e7584f19172290af70 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Wed, 11 Nov 2026 19:26:23 +0100 Subject: [PATCH 5/6] Fuzz: generate threads modules again. `supported_config` turned off threads because the frontend accepted shared memories and dropped `shared`. The frontend now returns `FrontendError::UnsupportedFeature` for shared memories, and for atomic instructions when it expands a function body. `skip_unsupported` discards those inputs. Turn threads on again. The targets now also cover atomic instructions on unshared memories in unexpanded function bodies, and the rejection of shared memories. memory64 stays off. The frontend rejects every 64-bit memory and table, so those inputs reach no code past that check. --- src/fuzzing.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/fuzzing.rs b/src/fuzzing.rs index f31fbf8..0799449 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -112,13 +112,13 @@ impl<'a> arbitrary::Arbitrary<'a> for ArbitraryModule { } } -/// Generates a `wasm_smith::Config` from `u` with the proposals turned -/// off that the frontend rejects or has no representation for. +/// Generates a `wasm_smith::Config` from `u` with GC, exceptions, tail +/// calls, memory64, relaxed SIMD and extended constant expressions turned +/// off. fn supported_config(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result { let mut config: wasm_smith::Config = u.arbitrary()?; config.gc_enabled = false; config.exceptions_enabled = false; - config.threads_enabled = false; config.tail_call_enabled = false; config.memory64_enabled = false; config.relaxed_simd_enabled = false; From ee50f7ea109ed9771caff16ed9c2572d02506071 Mon Sep 17 00:00:00 2001 From: Till Schneidereit Date: Tue, 17 Nov 2026 22:26:46 +0100 Subject: [PATCH 6/6] Fuzz: accept typed function references in roundtripped output. The backend declares a local that holds the result of `ref.func` with the type `(ref null $sig)`, which requires the function references proposal. `validate_output` validated under the features wasm-smith reports for the input's configuration. Those include function references only when GC is enabled, and `supported_config` turns GC off. `roundtrip` therefore failed on valid inputs whose expanded bodies hold a `ref.func` result in a local. `validate_output` now also enables function references. `differential` enables them in its Wasmtime configuration, because Wasmtime 44 leaves them off by default and rejects such a module. --- fuzz/fuzz_targets/differential.rs | 3 +++ src/fuzzing.rs | 8 +++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/fuzz/fuzz_targets/differential.rs b/fuzz/fuzz_targets/differential.rs index 3939acd..81bdcd3 100644 --- a/fuzz/fuzz_targets/differential.rs +++ b/fuzz/fuzz_targets/differential.rs @@ -20,6 +20,9 @@ fuzz_target!(|module: waffle::fuzzing::ArbitraryModule| { let mut config = wasmtime::Config::default(); config.consume_fuel(true); + // The roundtripped module can declare locals of a typed function + // reference type. + config.wasm_function_references(true); let engine = wasmtime::Engine::new(&config).unwrap(); let orig_module = wasmtime::Module::new(&engine, &orig_bytes[..]).expect("failed to parse original wasm"); diff --git a/src/fuzzing.rs b/src/fuzzing.rs index 0799449..de11f1f 100644 --- a/src/fuzzing.rs +++ b/src/fuzzing.rs @@ -138,10 +138,12 @@ impl<'a> arbitrary::Arbitrary<'a> for SupportedModule { } /// Validates `bytes` with `wasmparser::Validator` under the features of -/// the configuration `module` was generated with. Panics with the -/// validator's message if validation fails. +/// the configuration `module` was generated with and the function +/// references proposal. Panics with the validator's message if +/// validation fails. pub fn validate_output(module: &wasm_smith::Module, bytes: &[u8]) { - let mut validator = wasmparser::Validator::new_with_features(module.config().features()); + let features = module.config().features() | wasmparser::WasmFeatures::FUNCTION_REFERENCES; + let mut validator = wasmparser::Validator::new_with_features(features); if let Err(e) = validator.validate_all(bytes) { panic!("Invalid output: {}", e); }