wasmtime_fuzzing/
oracles.rs

1//! Oracles.
2//!
3//! Oracles take a test case and determine whether we have a bug. For example,
4//! one of the simplest oracles is to take a Wasm binary as our input test case,
5//! validate and instantiate it, and (implicitly) check that no assertions
6//! failed or segfaults happened. A more complicated oracle might compare the
7//! result of executing a Wasm file with and without optimizations enabled, and
8//! make sure that the two executions are observably identical.
9//!
10//! When an oracle finds a bug, it should report it to the fuzzing engine by
11//! panicking.
12
13#[cfg(feature = "fuzz-spec-interpreter")]
14pub mod diff_spec;
15pub mod diff_wasmi;
16pub mod diff_wasmtime;
17pub mod dummy;
18pub mod engine;
19pub mod memory;
20mod stacks;
21
22use self::diff_wasmtime::WasmtimeInstance;
23use self::engine::{DiffEngine, DiffInstance};
24use crate::generators::{self, CompilerStrategy, DiffValue, DiffValueType};
25use crate::single_module_fuzzer::KnownValid;
26use arbitrary::Arbitrary;
27pub use stacks::check_stacks;
28use std::future::Future;
29use std::pin::Pin;
30use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
31use std::sync::{Arc, Condvar, Mutex};
32use std::task::{Context, Poll};
33use std::time::{Duration, Instant};
34use wasmtime::*;
35use wasmtime_wast::WastContext;
36
37#[cfg(not(any(windows, target_arch = "s390x", target_arch = "riscv64")))]
38mod diff_v8;
39
40static CNT: AtomicUsize = AtomicUsize::new(0);
41
42/// Logs a wasm file to the filesystem to make it easy to figure out what wasm
43/// was used when debugging.
44pub fn log_wasm(wasm: &[u8]) {
45    super::init_fuzzing();
46
47    if !log::log_enabled!(log::Level::Debug) {
48        return;
49    }
50
51    let i = CNT.fetch_add(1, SeqCst);
52    let name = format!("testcase{i}.wasm");
53    std::fs::write(&name, wasm).expect("failed to write wasm file");
54    log::debug!("wrote wasm file to `{}`", name);
55    let wat = format!("testcase{i}.wat");
56    match wasmprinter::print_bytes(wasm) {
57        Ok(s) => std::fs::write(&wat, s).expect("failed to write wat file"),
58        // If wasmprinter failed remove a `*.wat` file, if any, to avoid
59        // confusing a preexisting one with this wasm which failed to get
60        // printed.
61        Err(_) => drop(std::fs::remove_file(&wat)),
62    }
63}
64
65/// The `T` in `Store<T>` for fuzzing stores, used to limit resource
66/// consumption during fuzzing.
67#[derive(Clone)]
68pub struct StoreLimits(Arc<LimitsState>);
69
70struct LimitsState {
71    /// Remaining memory, in bytes, left to allocate
72    remaining_memory: AtomicUsize,
73    /// Remaining amount of memory that's allowed to be copied via a growth.
74    remaining_copy_allowance: AtomicUsize,
75    /// Whether or not an allocation request has been denied
76    oom: AtomicBool,
77}
78
79/// Allow up to 1G which is well below the 2G limit on OSS-Fuzz and should allow
80/// most interesting behavior.
81const MAX_MEMORY: usize = 1 << 30;
82
83/// Allow up to 4G of bytes to be copied (conservatively) which should enable
84/// growth up to `MAX_MEMORY` or at least up to a relatively large amount.
85const MAX_MEMORY_MOVED: usize = 4 << 30;
86
87impl StoreLimits {
88    /// Creates the default set of limits for all fuzzing stores.
89    pub fn new() -> StoreLimits {
90        StoreLimits(Arc::new(LimitsState {
91            remaining_memory: AtomicUsize::new(MAX_MEMORY),
92            remaining_copy_allowance: AtomicUsize::new(MAX_MEMORY_MOVED),
93            oom: AtomicBool::new(false),
94        }))
95    }
96
97    fn alloc(&mut self, amt: usize) -> bool {
98        log::trace!("alloc {amt:#x} bytes");
99
100        // Assume that on each allocation of memory that all previous
101        // allocations of memory are moved. This is pretty coarse but is used to
102        // help prevent against fuzz test cases that just move tons of bytes
103        // around continuously. This assumes that all previous memory was
104        // allocated in a single linear memory and growing by `amt` will require
105        // moving all the bytes to a new location. This isn't actually required
106        // all the time nor does it accurately reflect what happens all the
107        // time, but it's a coarse approximation that should be "good enough"
108        // for allowing interesting fuzz behaviors to happen while not timing
109        // out just copying bytes around.
110        let prev_size = MAX_MEMORY - self.0.remaining_memory.load(SeqCst);
111        if self
112            .0
113            .remaining_copy_allowance
114            .fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(prev_size))
115            .is_err()
116        {
117            self.0.oom.store(true, SeqCst);
118            log::debug!("-> too many bytes moved, rejecting allocation");
119            return false;
120        }
121
122        // If we're allowed to move the bytes, then also check if we're allowed
123        // to actually have this much residence at once.
124        match self
125            .0
126            .remaining_memory
127            .fetch_update(SeqCst, SeqCst, |remaining| remaining.checked_sub(amt))
128        {
129            Ok(_) => true,
130            Err(_) => {
131                self.0.oom.store(true, SeqCst);
132                log::debug!("-> OOM hit");
133                false
134            }
135        }
136    }
137
138    fn is_oom(&self) -> bool {
139        self.0.oom.load(SeqCst)
140    }
141}
142
143impl ResourceLimiter for StoreLimits {
144    fn memory_growing(
145        &mut self,
146        current: usize,
147        desired: usize,
148        _maximum: Option<usize>,
149    ) -> Result<bool> {
150        Ok(self.alloc(desired - current))
151    }
152
153    fn table_growing(
154        &mut self,
155        current: usize,
156        desired: usize,
157        _maximum: Option<usize>,
158    ) -> Result<bool> {
159        let delta = (desired - current).saturating_mul(std::mem::size_of::<usize>());
160        Ok(self.alloc(delta))
161    }
162}
163
164/// Methods of timing out execution of a WebAssembly module
165#[derive(Clone, Debug)]
166pub enum Timeout {
167    /// No timeout is used, it should be guaranteed via some other means that
168    /// the input does not infinite loop.
169    None,
170    /// Fuel-based timeouts are used where the specified fuel is all that the
171    /// provided wasm module is allowed to consume.
172    Fuel(u64),
173    /// An epoch-interruption-based timeout is used with a sleeping
174    /// thread bumping the epoch counter after the specified duration.
175    Epoch(Duration),
176}
177
178/// Instantiate the Wasm buffer, and implicitly fail if we have an unexpected
179/// panic or segfault or anything else that can be detected "passively".
180///
181/// The engine will be configured using provided config.
182pub fn instantiate(
183    wasm: &[u8],
184    known_valid: KnownValid,
185    config: &generators::Config,
186    timeout: Timeout,
187) {
188    let mut store = config.to_store();
189
190    let module = match compile_module(store.engine(), wasm, known_valid, config) {
191        Some(module) => module,
192        None => return,
193    };
194
195    let mut timeout_state = HelperThread::default();
196    match timeout {
197        Timeout::Fuel(fuel) => store.set_fuel(fuel).unwrap(),
198
199        // If a timeout is requested then we spawn a helper thread to wait for
200        // the requested time and then send us a signal to get interrupted. We
201        // also arrange for the thread's sleep to get interrupted if we return
202        // early (or the wasm returns within the time limit), which allows the
203        // thread to get torn down.
204        //
205        // This prevents us from creating a huge number of sleeping threads if
206        // this function is executed in a loop, like it does on nightly fuzzing
207        // infrastructure.
208        Timeout::Epoch(timeout) => {
209            let engine = store.engine().clone();
210            timeout_state.run_periodically(timeout, move || engine.increment_epoch());
211        }
212        Timeout::None => {}
213    }
214
215    instantiate_with_dummy(&mut store, &module);
216}
217
218/// Represents supported commands to the `instantiate_many` function.
219#[derive(Arbitrary, Debug)]
220pub enum Command {
221    /// Instantiates a module.
222    ///
223    /// The value is the index of the module to instantiate.
224    ///
225    /// The module instantiated will be this value modulo the number of modules provided to `instantiate_many`.
226    Instantiate(usize),
227    /// Terminates a "running" instance.
228    ///
229    /// The value is the index of the instance to terminate.
230    ///
231    /// The instance terminated will be this value modulo the number of currently running
232    /// instances.
233    ///
234    /// If no instances are running, the command will be ignored.
235    Terminate(usize),
236}
237
238/// Instantiates many instances from the given modules.
239///
240/// The engine will be configured using the provided config.
241///
242/// The modules are expected to *not* have start functions as no timeouts are configured.
243pub fn instantiate_many(
244    modules: &[Vec<u8>],
245    known_valid: KnownValid,
246    config: &generators::Config,
247    commands: &[Command],
248) {
249    assert!(!config.module_config.config.allow_start_export);
250
251    let engine = Engine::new(&config.to_wasmtime()).unwrap();
252
253    let modules = modules
254        .iter()
255        .filter_map(|bytes| compile_module(&engine, bytes, known_valid, config))
256        .collect::<Vec<_>>();
257
258    // If no modules were valid, we're done
259    if modules.is_empty() {
260        return;
261    }
262
263    // This stores every `Store` where a successful instantiation takes place
264    let mut stores = Vec::new();
265    let limits = StoreLimits::new();
266
267    for command in commands {
268        match command {
269            Command::Instantiate(index) => {
270                let index = *index % modules.len();
271                log::info!("instantiating {}", index);
272                let module = &modules[index];
273                let mut store = Store::new(&engine, limits.clone());
274                config.configure_store(&mut store);
275
276                if instantiate_with_dummy(&mut store, module).is_some() {
277                    stores.push(Some(store));
278                } else {
279                    log::warn!("instantiation failed");
280                }
281            }
282            Command::Terminate(index) => {
283                if stores.is_empty() {
284                    continue;
285                }
286                let index = *index % stores.len();
287
288                log::info!("dropping {}", index);
289                stores.swap_remove(index);
290            }
291        }
292    }
293}
294
295fn compile_module(
296    engine: &Engine,
297    bytes: &[u8],
298    known_valid: KnownValid,
299    config: &generators::Config,
300) -> Option<Module> {
301    log_wasm(bytes);
302
303    fn is_pcc_error(e: &anyhow::Error) -> bool {
304        // NOTE: please keep this predicate in sync with the display format of CodegenError,
305        // defined in `wasmtime/cranelift/codegen/src/result.rs`
306        e.to_string().to_lowercase().contains("proof-carrying-code")
307    }
308
309    match config.compile(engine, bytes) {
310        Ok(module) => Some(module),
311        Err(e) if is_pcc_error(&e) => {
312            panic!("pcc error in input: {e:#?}");
313        }
314        Err(_) if known_valid == KnownValid::No => None,
315        Err(e) => {
316            if let generators::InstanceAllocationStrategy::Pooling(c) = &config.wasmtime.strategy {
317                // When using the pooling allocator, accept failures to compile
318                // when arbitrary table element limits have been exceeded as
319                // there is currently no way to constrain the generated module
320                // table types.
321                let string = e.to_string();
322                if string.contains("minimum element size") {
323                    return None;
324                }
325
326                // Allow modules-failing-to-compile which exceed the requested
327                // size for each instance. This is something that is difficult
328                // to control and ensure it always succeeds, so we simply have a
329                // "random" instance size limit and if a module doesn't fit we
330                // move on to the next fuzz input.
331                if string.contains("instance allocation for this module requires") {
332                    return None;
333                }
334
335                // If the pooling allocator is more restrictive on the number of
336                // tables and memories than we allowed wasm-smith to generate
337                // then allow compilation errors along those lines.
338                if c.max_tables_per_module < (config.module_config.config.max_tables as u32)
339                    && string.contains("defined tables count")
340                    && string.contains("exceeds the per-instance limit")
341                {
342                    return None;
343                }
344
345                if c.max_memories_per_module < (config.module_config.config.max_memories as u32)
346                    && string.contains("defined memories count")
347                    && string.contains("exceeds the per-instance limit")
348                {
349                    return None;
350                }
351            }
352
353            panic!("failed to compile module: {e:?}");
354        }
355    }
356}
357
358/// Create a Wasmtime [`Instance`] from a [`Module`] and fill in all imports
359/// with dummy values (e.g., zeroed values, immediately-trapping functions).
360/// Also, this function catches certain fuzz-related instantiation failures and
361/// returns `None` instead of panicking.
362///
363/// TODO: we should implement tracing versions of these dummy imports that
364/// record a trace of the order that imported functions were called in and with
365/// what values. Like the results of exported functions, calls to imports should
366/// also yield the same values for each configuration, and we should assert
367/// that.
368pub fn instantiate_with_dummy(store: &mut Store<StoreLimits>, module: &Module) -> Option<Instance> {
369    // Creation of imports can fail due to resource limit constraints, and then
370    // instantiation can naturally fail for a number of reasons as well. Bundle
371    // the two steps together to match on the error below.
372    let linker = dummy::dummy_linker(store, module);
373    if let Err(e) = &linker {
374        log::warn!("failed to create dummy linker: {e:?}");
375    }
376    let instance = linker.and_then(|l| l.instantiate(&mut *store, module));
377    unwrap_instance(store, instance)
378}
379
380fn unwrap_instance(
381    store: &Store<StoreLimits>,
382    instance: anyhow::Result<Instance>,
383) -> Option<Instance> {
384    let e = match instance {
385        Ok(i) => return Some(i),
386        Err(e) => e,
387    };
388
389    // If the instantiation hit OOM for some reason then that's ok, it's
390    // expected that fuzz-generated programs try to allocate lots of
391    // stuff.
392    if store.data().is_oom() {
393        log::debug!("failed to instantiate: OOM");
394        return None;
395    }
396
397    // Allow traps which can happen normally with `unreachable` or a
398    // timeout or such
399    if let Some(trap) = e.downcast_ref::<Trap>() {
400        log::debug!("failed to instantiate: {}", trap);
401        return None;
402    }
403
404    let string = e.to_string();
405    // Currently we instantiate with a `Linker` which can't instantiate
406    // every single module under the sun due to using name-based resolution
407    // rather than positional-based resolution
408    if string.contains("incompatible import type") {
409        log::debug!("failed to instantiate: {}", string);
410        return None;
411    }
412
413    // Also allow failures to instantiate as a result of hitting pooling limits.
414    if e.is::<wasmtime::PoolConcurrencyLimitError>() {
415        log::debug!("failed to instantiate: {}", string);
416        return None;
417    }
418
419    // Everything else should be a bug in the fuzzer or a bug in wasmtime
420    panic!("failed to instantiate: {e:?}");
421}
422
423/// Evaluate the function identified by `name` in two different engine
424/// instances--`lhs` and `rhs`.
425///
426/// Returns `Ok(true)` if more evaluations can happen or `Ok(false)` if the
427/// instances may have drifted apart and no more evaluations can happen.
428///
429/// # Panics
430///
431/// This will panic if the evaluation is different between engines (e.g.,
432/// results are different, hashed instance is different, one side traps, etc.).
433pub fn differential(
434    lhs: &mut dyn DiffInstance,
435    lhs_engine: &dyn DiffEngine,
436    rhs: &mut WasmtimeInstance,
437    name: &str,
438    args: &[DiffValue],
439    result_tys: &[DiffValueType],
440) -> anyhow::Result<bool> {
441    log::debug!("Evaluating: `{}` with {:?}", name, args);
442    let lhs_results = match lhs.evaluate(name, args, result_tys) {
443        Ok(Some(results)) => Ok(results),
444        Err(e) => Err(e),
445        // this engine couldn't execute this type signature, so discard this
446        // execution by returning success.
447        Ok(None) => return Ok(true),
448    };
449    log::debug!(" -> lhs results on {}: {:?}", lhs.name(), &lhs_results);
450
451    let rhs_results = rhs
452        .evaluate(name, args, result_tys)
453        // wasmtime should be able to invoke any signature, so unwrap this result
454        .map(|results| results.unwrap());
455    log::debug!(" -> rhs results on {}: {:?}", rhs.name(), &rhs_results);
456
457    // If Wasmtime hit its OOM condition, which is possible since it's set
458    // somewhat low while fuzzing, then don't return an error but return
459    // `false` indicating that differential fuzzing must stop. There's no
460    // guarantee the other engine has the same OOM limits as Wasmtime, and
461    // it's assumed that Wasmtime is configured to have a more conservative
462    // limit than the other engine.
463    if rhs.is_oom() {
464        return Ok(false);
465    }
466
467    match DiffEqResult::new(lhs_engine, lhs_results, rhs_results) {
468        DiffEqResult::Success(lhs, rhs) => assert_eq!(lhs, rhs),
469        DiffEqResult::Poisoned => return Ok(false),
470        DiffEqResult::Failed => {}
471    }
472
473    for (global, ty) in rhs.exported_globals() {
474        log::debug!("Comparing global `{global}`");
475        let lhs = match lhs.get_global(&global, ty) {
476            Some(val) => val,
477            None => continue,
478        };
479        let rhs = rhs.get_global(&global, ty).unwrap();
480        assert_eq!(lhs, rhs);
481    }
482    for (memory, shared) in rhs.exported_memories() {
483        log::debug!("Comparing memory `{memory}`");
484        let lhs = match lhs.get_memory(&memory, shared) {
485            Some(val) => val,
486            None => continue,
487        };
488        let rhs = rhs.get_memory(&memory, shared).unwrap();
489        if lhs == rhs {
490            continue;
491        }
492        eprintln!("differential memory is {} bytes long", lhs.len());
493        eprintln!("wasmtime memory is     {} bytes long", rhs.len());
494        panic!("memories have differing values");
495    }
496
497    Ok(true)
498}
499
500/// Result of comparing the result of two operations during differential
501/// execution.
502pub enum DiffEqResult<T, U> {
503    /// Both engines succeeded.
504    Success(T, U),
505    /// The result has reached the state where engines may have diverged and
506    /// results can no longer be compared.
507    Poisoned,
508    /// Both engines failed with the same error message, and internal state
509    /// should still match between the two engines.
510    Failed,
511}
512
513fn wasmtime_trap_is_non_deterministic(trap: &Trap) -> bool {
514    match trap {
515        // Allocations being too large for the GC are
516        // implementation-defined.
517        Trap::AllocationTooLarge |
518        // Stack size, and therefore when overflow happens, is
519        // implementation-defined.
520        Trap::StackOverflow => true,
521        _ => false,
522    }
523}
524
525fn wasmtime_error_is_non_deterministic(error: &wasmtime::Error) -> bool {
526    match error.downcast_ref::<Trap>() {
527        Some(trap) => wasmtime_trap_is_non_deterministic(trap),
528
529        // For general, unknown errors, we can't rely on this being
530        // a deterministic Wasm failure that both engines handled
531        // identically, leaving Wasm in identical states. We could
532        // just as easily be hitting engine-specific failures, like
533        // different implementation-defined limits. So simply poison
534        // this execution and move on to the next test.
535        None => true,
536    }
537}
538
539impl<T, U> DiffEqResult<T, U> {
540    /// Computes the differential result from executing in two different
541    /// engines.
542    pub fn new(
543        lhs_engine: &dyn DiffEngine,
544        lhs_result: Result<T>,
545        rhs_result: Result<U>,
546    ) -> DiffEqResult<T, U> {
547        match (lhs_result, rhs_result) {
548            (Ok(lhs_result), Ok(rhs_result)) => DiffEqResult::Success(lhs_result, rhs_result),
549
550            // Handle all non-deterministic errors by poisoning this execution's
551            // state, so that we simply move on to the next test.
552            (Err(lhs), _) if lhs_engine.is_non_deterministic_error(&lhs) => {
553                log::debug!("lhs failed non-deterministically: {lhs:?}");
554                DiffEqResult::Poisoned
555            }
556            (_, Err(rhs)) if wasmtime_error_is_non_deterministic(&rhs) => {
557                log::debug!("rhs failed non-deterministically: {rhs:?}");
558                DiffEqResult::Poisoned
559            }
560
561            // Both sides failed deterministically. Check that the trap and
562            // state at the time of failure is the same.
563            (Err(lhs), Err(rhs)) => {
564                let rhs = rhs
565                    .downcast::<Trap>()
566                    .expect("non-traps handled in earlier match arm");
567
568                debug_assert!(
569                    !lhs_engine.is_non_deterministic_error(&lhs),
570                    "non-deterministic traps handled in earlier match arm",
571                );
572                debug_assert!(
573                    !wasmtime_trap_is_non_deterministic(&rhs),
574                    "non-deterministic traps handled in earlier match arm",
575                );
576
577                lhs_engine.assert_error_match(&lhs, &rhs);
578                DiffEqResult::Failed
579            }
580
581            // A real bug is found if only one side fails.
582            (Ok(_), Err(err)) => panic!("only the `rhs` failed for this input: {err:?}"),
583            (Err(err), Ok(_)) => panic!("only the `lhs` failed for this input: {err:?}"),
584        }
585    }
586}
587
588/// Invoke the given API calls.
589pub fn make_api_calls(api: generators::api::ApiCalls) {
590    use crate::generators::api::ApiCall;
591    use std::collections::HashMap;
592
593    let mut store: Option<Store<StoreLimits>> = None;
594    let mut modules: HashMap<usize, Module> = Default::default();
595    let mut instances: HashMap<usize, Instance> = Default::default();
596
597    for call in api.calls {
598        match call {
599            ApiCall::StoreNew(config) => {
600                log::trace!("creating store");
601                assert!(store.is_none());
602                store = Some(config.to_store());
603            }
604
605            ApiCall::ModuleNew { id, wasm } => {
606                log::debug!("creating module: {}", id);
607                log_wasm(&wasm);
608                let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
609                    Ok(m) => m,
610                    Err(_) => continue,
611                };
612                let old = modules.insert(id, module);
613                assert!(old.is_none());
614            }
615
616            ApiCall::ModuleDrop { id } => {
617                log::trace!("dropping module: {}", id);
618                drop(modules.remove(&id));
619            }
620
621            ApiCall::InstanceNew { id, module } => {
622                log::trace!("instantiating module {} as {}", module, id);
623                let module = match modules.get(&module) {
624                    Some(m) => m,
625                    None => continue,
626                };
627
628                let store = store.as_mut().unwrap();
629                if let Some(instance) = instantiate_with_dummy(store, module) {
630                    instances.insert(id, instance);
631                }
632            }
633
634            ApiCall::InstanceDrop { id } => {
635                log::trace!("dropping instance {}", id);
636                instances.remove(&id);
637            }
638
639            ApiCall::CallExportedFunc { instance, nth } => {
640                log::trace!("calling instance export {} / {}", instance, nth);
641                let instance = match instances.get(&instance) {
642                    Some(i) => i,
643                    None => {
644                        // Note that we aren't guaranteed to instantiate valid
645                        // modules, see comments in `InstanceNew` for details on
646                        // that. But the API call generator can't know if
647                        // instantiation failed, so we might not actually have
648                        // this instance. When that's the case, just skip the
649                        // API call and keep going.
650                        continue;
651                    }
652                };
653                let store = store.as_mut().unwrap();
654
655                let funcs = instance
656                    .exports(&mut *store)
657                    .filter_map(|e| match e.into_extern() {
658                        Extern::Func(f) => Some(f),
659                        _ => None,
660                    })
661                    .collect::<Vec<_>>();
662
663                if funcs.is_empty() {
664                    continue;
665                }
666
667                let nth = nth % funcs.len();
668                let f = &funcs[nth];
669                let ty = f.ty(&store);
670                if let Ok(params) = dummy::dummy_values(ty.params()) {
671                    let mut results = vec![Val::I32(0); ty.results().len()];
672                    let _ = f.call(store, &params, &mut results);
673                }
674            }
675        }
676    }
677}
678
679/// Executes the wast `test` with the `config` specified.
680///
681/// Ensures that wast tests pass regardless of the `Config`.
682pub fn wast_test(u: &mut arbitrary::Unstructured<'_>) -> arbitrary::Result<()> {
683    crate::init_fuzzing();
684
685    let mut fuzz_config: generators::Config = u.arbitrary()?;
686    let test: generators::WastTest = u.arbitrary()?;
687    if u.arbitrary()? {
688        fuzz_config.enable_async(u)?;
689    }
690
691    let test = &test.test;
692
693    // Discard tests that allocate a lot of memory as we don't want to OOM the
694    // fuzzer and we also limit memory growth which would cause the test to
695    // fail.
696    if test.config.hogs_memory.unwrap_or(false) {
697        return Err(arbitrary::Error::IncorrectFormat);
698    }
699
700    // Transform `fuzz_config` to be valid for `test` and make sure that this
701    // test is supposed to pass.
702    let wast_config = fuzz_config.make_wast_test_compliant(test);
703    if test.should_fail(&wast_config) {
704        return Err(arbitrary::Error::IncorrectFormat);
705    }
706
707    // Winch requires AVX and AVX2 for SIMD tests to pass so don't run the test
708    // if either isn't enabled.
709    if fuzz_config.wasmtime.compiler_strategy == CompilerStrategy::Winch
710        && test.config.simd()
711        && (fuzz_config
712            .wasmtime
713            .codegen_flag("has_avx")
714            .is_some_and(|value| value == "false")
715            || fuzz_config
716                .wasmtime
717                .codegen_flag("has_avx2")
718                .is_some_and(|value| value == "false"))
719    {
720        log::warn!(
721            "Skipping Wast test because Winch doesn't support SIMD tests with AVX or AVX2 disabled"
722        );
723        return Err(arbitrary::Error::IncorrectFormat);
724    }
725
726    // Fuel and epochs don't play well with threads right now, so exclude any
727    // thread-spawning test if it looks like threads are spawned in that case.
728    if fuzz_config.wasmtime.consume_fuel || fuzz_config.wasmtime.epoch_interruption {
729        if test.contents.contains("(thread") {
730            return Err(arbitrary::Error::IncorrectFormat);
731        }
732    }
733
734    log::debug!("running {:?}", test.path);
735    let async_ = if fuzz_config.wasmtime.async_config == generators::AsyncConfig::Disabled {
736        wasmtime_wast::Async::No
737    } else {
738        wasmtime_wast::Async::Yes
739    };
740    let mut wast_context = WastContext::new(fuzz_config.to_store(), async_);
741    wast_context
742        .register_spectest(&wasmtime_wast::SpectestConfig {
743            use_shared_memory: true,
744            suppress_prints: true,
745        })
746        .unwrap();
747    wast_context
748        .run_buffer(test.path.to_str().unwrap(), test.contents.as_bytes())
749        .unwrap();
750    Ok(())
751}
752
753/// Execute a series of `table.get` and `table.set` operations.
754///
755/// Returns the number of `gc` operations which occurred throughout the test
756/// case -- used to test below that gc happens reasonably soon and eventually.
757pub fn table_ops(
758    mut fuzz_config: generators::Config,
759    ops: generators::table_ops::TableOps,
760) -> Result<usize> {
761    let expected_drops = Arc::new(AtomicUsize::new(ops.num_params as usize));
762    let num_dropped = Arc::new(AtomicUsize::new(0));
763
764    let num_gcs = Arc::new(AtomicUsize::new(0));
765    {
766        fuzz_config.wasmtime.consume_fuel = true;
767        let mut store = fuzz_config.to_store();
768        store.set_fuel(1_000).unwrap();
769
770        let wasm = ops.to_wasm_binary();
771        log_wasm(&wasm);
772        let module = match compile_module(store.engine(), &wasm, KnownValid::No, &fuzz_config) {
773            Some(m) => m,
774            None => return Ok(0),
775        };
776
777        let mut linker = Linker::new(store.engine());
778
779        // To avoid timeouts, limit the number of explicit GCs we perform per
780        // test case.
781        const MAX_GCS: usize = 5;
782
783        let func_ty = FuncType::new(
784            store.engine(),
785            vec![],
786            vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
787        );
788        let func = Func::new(&mut store, func_ty, {
789            let num_dropped = num_dropped.clone();
790            let expected_drops = expected_drops.clone();
791            let num_gcs = num_gcs.clone();
792            move |mut caller: Caller<'_, StoreLimits>, _params, results| {
793                log::info!("table_ops: GC");
794                if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
795                    caller.gc();
796                }
797
798                let a = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
799                let b = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
800                let c = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
801
802                log::info!("table_ops: gc() -> ({:?}, {:?}, {:?})", a, b, c);
803
804                expected_drops.fetch_add(3, SeqCst);
805                results[0] = Some(a).into();
806                results[1] = Some(b).into();
807                results[2] = Some(c).into();
808                Ok(())
809            }
810        });
811        linker.define(&store, "", "gc", func).unwrap();
812
813        linker
814            .func_wrap("", "take_refs", {
815                let expected_drops = expected_drops.clone();
816                move |caller: Caller<'_, StoreLimits>,
817                      a: Option<Rooted<ExternRef>>,
818                      b: Option<Rooted<ExternRef>>,
819                      c: Option<Rooted<ExternRef>>|
820                      -> Result<()> {
821                    log::info!("table_ops: take_refs({a:?}, {b:?}, {c:?})",);
822
823                    // Do the assertion on each ref's inner data, even though it
824                    // all points to the same atomic, so that if we happen to
825                    // run into a use-after-free bug with one of these refs we
826                    // are more likely to trigger a segfault.
827                    if let Some(a) = a {
828                        let a = a
829                            .data(&caller)?
830                            .unwrap()
831                            .downcast_ref::<CountDrops>()
832                            .unwrap();
833                        assert!(a.0.load(SeqCst) <= expected_drops.load(SeqCst));
834                    }
835                    if let Some(b) = b {
836                        let b = b
837                            .data(&caller)?
838                            .unwrap()
839                            .downcast_ref::<CountDrops>()
840                            .unwrap();
841                        assert!(b.0.load(SeqCst) <= expected_drops.load(SeqCst));
842                    }
843                    if let Some(c) = c {
844                        let c = c
845                            .data(&caller)?
846                            .unwrap()
847                            .downcast_ref::<CountDrops>()
848                            .unwrap();
849                        assert!(c.0.load(SeqCst) <= expected_drops.load(SeqCst));
850                    }
851                    Ok(())
852                }
853            })
854            .unwrap();
855
856        let func_ty = FuncType::new(
857            store.engine(),
858            vec![],
859            vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
860        );
861        let func = Func::new(&mut store, func_ty, {
862            let num_dropped = num_dropped.clone();
863            let expected_drops = expected_drops.clone();
864            move |mut caller, _params, results| {
865                log::info!("table_ops: make_refs");
866
867                let a = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
868                let b = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
869                let c = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
870                expected_drops.fetch_add(3, SeqCst);
871
872                log::info!("table_ops: make_refs() -> ({:?}, {:?}, {:?})", a, b, c);
873
874                results[0] = Some(a).into();
875                results[1] = Some(b).into();
876                results[2] = Some(c).into();
877
878                Ok(())
879            }
880        });
881        linker.define(&store, "", "make_refs", func).unwrap();
882
883        let instance = linker.instantiate(&mut store, &module).unwrap();
884        let run = instance.get_func(&mut store, "run").unwrap();
885
886        {
887            let mut scope = RootScope::new(&mut store);
888
889            log::info!(
890                "table_ops: begin allocating {} externref arguments",
891                ops.num_globals
892            );
893            let args: Vec<_> = (0..ops.num_params)
894                .map(|_| {
895                    Ok(Val::ExternRef(Some(ExternRef::new(
896                        &mut scope,
897                        CountDrops(num_dropped.clone()),
898                    )?)))
899                })
900                .collect::<Result<_>>()?;
901            log::info!(
902                "table_ops: end allocating {} externref arguments",
903                ops.num_globals
904            );
905
906            // The generated function should always return a trap. The only two
907            // valid traps are table-out-of-bounds which happens through `table.get`
908            // and `table.set` generated or an out-of-fuel trap. Otherwise any other
909            // error is unexpected and should fail fuzzing.
910            log::info!("table_ops: calling into Wasm `run` function");
911            let trap = run
912                .call(&mut scope, &args, &mut [])
913                .unwrap_err()
914                .downcast::<Trap>()
915                .unwrap();
916
917            match trap {
918                Trap::TableOutOfBounds | Trap::OutOfFuel => {}
919                _ => panic!("unexpected trap: {trap}"),
920            }
921        }
922
923        // Do a final GC after running the Wasm.
924        store.gc();
925    }
926
927    assert_eq!(num_dropped.load(SeqCst), expected_drops.load(SeqCst));
928    return Ok(num_gcs.load(SeqCst));
929
930    struct CountDrops(Arc<AtomicUsize>);
931
932    impl Drop for CountDrops {
933        fn drop(&mut self) {
934            self.0.fetch_add(1, SeqCst);
935        }
936    }
937}
938
939#[derive(Default)]
940struct HelperThread {
941    state: Arc<HelperThreadState>,
942    thread: Option<std::thread::JoinHandle<()>>,
943}
944
945#[derive(Default)]
946struct HelperThreadState {
947    should_exit: Mutex<bool>,
948    should_exit_cvar: Condvar,
949}
950
951impl HelperThread {
952    fn run_periodically(&mut self, dur: Duration, mut closure: impl FnMut() + Send + 'static) {
953        let state = self.state.clone();
954        self.thread = Some(std::thread::spawn(move || {
955            // Using our mutex/condvar we wait here for the first of `dur` to
956            // pass or the `HelperThread` instance to get dropped.
957            let mut should_exit = state.should_exit.lock().unwrap();
958            while !*should_exit {
959                let (lock, result) = state
960                    .should_exit_cvar
961                    .wait_timeout(should_exit, dur)
962                    .unwrap();
963                should_exit = lock;
964                // If we timed out for sure then there's no need to continue
965                // since we'll just abort on the next `checked_sub` anyway.
966                if result.timed_out() {
967                    closure();
968                }
969            }
970        }));
971    }
972}
973
974impl Drop for HelperThread {
975    fn drop(&mut self) {
976        let thread = match self.thread.take() {
977            Some(thread) => thread,
978            None => return,
979        };
980        // Signal our thread that it should exit and wake it up in case it's
981        // sleeping.
982        *self.state.should_exit.lock().unwrap() = true;
983        self.state.should_exit_cvar.notify_one();
984
985        // ... and then wait for the thread to exit to ensure we clean up
986        // after ourselves.
987        thread.join().unwrap();
988    }
989}
990
991/// Generate and execute a `crate::generators::component_types::TestCase` using the specified `input` to create
992/// arbitrary types and values.
993pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
994    use crate::generators::component_types;
995    use component_fuzz_util::{TestCase, Type, EXPORT_FUNCTION, IMPORT_FUNCTION, MAX_TYPE_DEPTH};
996    use component_test_util::FuncExt;
997    use wasmtime::component::{Component, Linker, Val};
998
999    crate::init_fuzzing();
1000
1001    let mut types = Vec::new();
1002    let mut type_fuel = 500;
1003
1004    for _ in 0..5 {
1005        types.push(Type::generate(input, MAX_TYPE_DEPTH, &mut type_fuel)?);
1006    }
1007    let params = (0..input.int_in_range(0..=5)?)
1008        .map(|_| input.choose(&types))
1009        .collect::<arbitrary::Result<Vec<_>>>()?;
1010    let result = if input.arbitrary()? {
1011        Some(input.choose(&types)?)
1012    } else {
1013        None
1014    };
1015
1016    let case = TestCase {
1017        params,
1018        result,
1019        encoding1: input.arbitrary()?,
1020        encoding2: input.arbitrary()?,
1021    };
1022
1023    let mut config = component_test_util::config();
1024    config.debug_adapter_modules(input.arbitrary()?);
1025    let engine = Engine::new(&config).unwrap();
1026    let mut store = Store::new(&engine, (Vec::new(), None));
1027    let wat = case.declarations().make_component();
1028    let wat = wat.as_bytes();
1029    log_wasm(wat);
1030    let component = Component::new(&engine, wat).unwrap();
1031    let mut linker = Linker::new(&engine);
1032
1033    linker
1034        .root()
1035        .func_new(IMPORT_FUNCTION, {
1036            move |mut cx: StoreContextMut<'_, (Vec<Val>, Option<Vec<Val>>)>,
1037                  params: &[Val],
1038                  results: &mut [Val]|
1039                  -> Result<()> {
1040                log::trace!("received params {params:?}");
1041                let (expected_args, expected_results) = cx.data_mut();
1042                assert_eq!(params.len(), expected_args.len());
1043                for (expected, actual) in expected_args.iter().zip(params) {
1044                    assert_eq!(expected, actual);
1045                }
1046                results.clone_from_slice(&expected_results.take().unwrap());
1047                log::trace!("returning results {results:?}");
1048                Ok(())
1049            }
1050        })
1051        .unwrap();
1052
1053    let instance = linker.instantiate(&mut store, &component).unwrap();
1054    let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
1055    let param_tys = func.params(&store);
1056    let result_tys = func.results(&store);
1057
1058    while input.arbitrary()? {
1059        let params = param_tys
1060            .iter()
1061            .map(|(_, ty)| component_types::arbitrary_val(ty, input))
1062            .collect::<arbitrary::Result<Vec<_>>>()?;
1063        let results = result_tys
1064            .iter()
1065            .map(|ty| component_types::arbitrary_val(ty, input))
1066            .collect::<arbitrary::Result<Vec<_>>>()?;
1067
1068        *store.data_mut() = (params.clone(), Some(results.clone()));
1069
1070        log::trace!("passing params {params:?}");
1071        let mut actual = vec![Val::Bool(false); results.len()];
1072        func.call_and_post_return(&mut store, &params, &mut actual)
1073            .unwrap();
1074        log::trace!("received results {actual:?}");
1075        assert_eq!(actual, results);
1076    }
1077
1078    Ok(())
1079}
1080
1081/// Instantiates a wasm module and runs its exports with dummy values, all in
1082/// an async fashion.
1083///
1084/// Attempts to stress yields in host functions to ensure that exiting and
1085/// resuming a wasm function call works.
1086pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32]) {
1087    let mut store = config.to_store();
1088    let module = match compile_module(store.engine(), wasm, KnownValid::Yes, config) {
1089        Some(module) => module,
1090        None => return,
1091    };
1092
1093    // Configure a helper thread to periodically increment the epoch to
1094    // forcibly enable yields-via-epochs if epochs are in use. Note that this
1095    // is required because the wasm isn't otherwise guaranteed to necessarily
1096    // call any imports which will also increment the epoch.
1097    let mut helper_thread = HelperThread::default();
1098    if let generators::AsyncConfig::YieldWithEpochs { dur, .. } = &config.wasmtime.async_config {
1099        let engine = store.engine().clone();
1100        helper_thread.run_periodically(*dur, move || engine.increment_epoch());
1101    }
1102
1103    // Generate a `Linker` where all function imports are custom-built to yield
1104    // periodically and additionally increment the epoch.
1105    let mut imports = Vec::new();
1106    for import in module.imports() {
1107        let item = match import.ty() {
1108            ExternType::Func(ty) => {
1109                let poll_amt = take_poll_amt(&mut poll_amts);
1110                Func::new_async(&mut store, ty.clone(), move |caller, _, results| {
1111                    let ty = ty.clone();
1112                    Box::new(async move {
1113                        caller.engine().increment_epoch();
1114                        log::info!("yielding {} times in import", poll_amt);
1115                        YieldN(poll_amt).await;
1116                        for (ret_ty, result) in ty.results().zip(results) {
1117                            *result = dummy::dummy_value(ret_ty)?;
1118                        }
1119                        Ok(())
1120                    })
1121                })
1122                .into()
1123            }
1124            other_ty => match dummy::dummy_extern(&mut store, other_ty) {
1125                Ok(item) => item,
1126                Err(e) => {
1127                    log::warn!("couldn't create import: {}", e);
1128                    return;
1129                }
1130            },
1131        };
1132        imports.push(item);
1133    }
1134
1135    // Run the instantiation process, asynchronously, and if everything
1136    // succeeds then pull out the instance.
1137    // log::info!("starting instantiation");
1138    let instance = run(Timeout {
1139        future: Instance::new_async(&mut store, &module, &imports),
1140        polls: take_poll_amt(&mut poll_amts),
1141        end: Instant::now() + Duration::from_millis(2_000),
1142    });
1143    let instance = match instance {
1144        Ok(instantiation_result) => match unwrap_instance(&store, instantiation_result) {
1145            Some(instance) => instance,
1146            None => {
1147                log::info!("instantiation hit a nominal error");
1148                return; // resource exhaustion or limits met
1149            }
1150        },
1151        Err(_) => {
1152            log::info!("instantiation failed to complete");
1153            return; // Timed out or ran out of polls
1154        }
1155    };
1156
1157    // Run each export of the instance in the same manner as instantiation
1158    // above. Dummy values are passed in for argument values here:
1159    //
1160    // TODO: this should probably be more clever about passing in arguments for
1161    // example they might be used as pointers or something and always using 0
1162    // isn't too interesting.
1163    let funcs = instance
1164        .exports(&mut store)
1165        .filter_map(|e| {
1166            let name = e.name().to_string();
1167            let func = e.into_extern().into_func()?;
1168            Some((name, func))
1169        })
1170        .collect::<Vec<_>>();
1171    for (name, func) in funcs {
1172        let ty = func.ty(&store);
1173        let params = ty
1174            .params()
1175            .map(|ty| dummy::dummy_value(ty).unwrap())
1176            .collect::<Vec<_>>();
1177        let mut results = ty
1178            .results()
1179            .map(|ty| dummy::dummy_value(ty).unwrap())
1180            .collect::<Vec<_>>();
1181
1182        log::info!("invoking export {:?}", name);
1183        let future = func.call_async(&mut store, &params, &mut results);
1184        match run(Timeout {
1185            future,
1186            polls: take_poll_amt(&mut poll_amts),
1187            end: Instant::now() + Duration::from_millis(2_000),
1188        }) {
1189            // On success or too many polls, try the next export.
1190            Ok(_) | Err(Exhausted::Polls) => {}
1191
1192            // If time ran out then stop the current test case as we might have
1193            // already sucked up a lot of time for this fuzz test case so don't
1194            // keep it going.
1195            Err(Exhausted::Time) => return,
1196        }
1197    }
1198
1199    fn take_poll_amt(polls: &mut &[u32]) -> u32 {
1200        match polls.split_first() {
1201            Some((a, rest)) => {
1202                *polls = rest;
1203                *a
1204            }
1205            None => 0,
1206        }
1207    }
1208
1209    /// Helper future to yield N times before resolving.
1210    struct YieldN(u32);
1211
1212    impl Future for YieldN {
1213        type Output = ();
1214
1215        fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
1216            if self.0 == 0 {
1217                Poll::Ready(())
1218            } else {
1219                self.0 -= 1;
1220                cx.waker().wake_by_ref();
1221                Poll::Pending
1222            }
1223        }
1224    }
1225
1226    /// Helper future for applying a timeout to `future` up to either when `end`
1227    /// is the current time or `polls` polls happen.
1228    ///
1229    /// Note that this helps to time out infinite loops in wasm, for example.
1230    struct Timeout<F> {
1231        future: F,
1232        /// If the future isn't ready by this time then the `Timeout<F>` future
1233        /// will return `None`.
1234        end: Instant,
1235        /// If the future doesn't resolve itself in this many calls to `poll`
1236        /// then the `Timeout<F>` future will return `None`.
1237        polls: u32,
1238    }
1239
1240    enum Exhausted {
1241        Time,
1242        Polls,
1243    }
1244
1245    impl<F: Future> Future for Timeout<F> {
1246        type Output = Result<F::Output, Exhausted>;
1247
1248        fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1249            let (end, polls, future) = unsafe {
1250                let me = self.get_unchecked_mut();
1251                (me.end, &mut me.polls, Pin::new_unchecked(&mut me.future))
1252            };
1253            match future.poll(cx) {
1254                Poll::Ready(val) => Poll::Ready(Ok(val)),
1255                Poll::Pending => {
1256                    if Instant::now() >= end {
1257                        log::warn!("future operation timed out");
1258                        return Poll::Ready(Err(Exhausted::Time));
1259                    }
1260                    if *polls == 0 {
1261                        log::warn!("future operation ran out of polls");
1262                        return Poll::Ready(Err(Exhausted::Polls));
1263                    }
1264                    *polls -= 1;
1265                    Poll::Pending
1266                }
1267            }
1268        }
1269    }
1270
1271    fn run<F: Future>(future: F) -> F::Output {
1272        let mut f = Box::pin(future);
1273        let mut cx = Context::from_waker(futures::task::noop_waker_ref());
1274        loop {
1275            match f.as_mut().poll(&mut cx) {
1276                Poll::Ready(val) => break val,
1277                Poll::Pending => {}
1278            }
1279        }
1280    }
1281}
1282
1283#[cfg(test)]
1284mod tests {
1285    use super::*;
1286    use arbitrary::Unstructured;
1287    use rand::prelude::*;
1288    use wasmparser::{Validator, WasmFeatures};
1289
1290    fn gen_until_pass<T: for<'a> Arbitrary<'a>>(
1291        mut f: impl FnMut(T, &mut Unstructured<'_>) -> Result<bool>,
1292    ) -> bool {
1293        let mut rng = SmallRng::seed_from_u64(0);
1294        let mut buf = vec![0; 2048];
1295        let n = 3000;
1296        for _ in 0..n {
1297            rng.fill_bytes(&mut buf);
1298            let mut u = Unstructured::new(&buf);
1299
1300            if let Ok(config) = u.arbitrary() {
1301                if f(config, &mut u).unwrap() {
1302                    return true;
1303                }
1304            }
1305        }
1306        false
1307    }
1308
1309    /// Runs `f` with random data until it returns `Ok(())` `iters` times.
1310    fn test_n_times<T: for<'a> Arbitrary<'a>>(
1311        iters: u32,
1312        mut f: impl FnMut(T, &mut Unstructured<'_>) -> arbitrary::Result<()>,
1313    ) {
1314        let mut to_test = 0..iters;
1315        let ok = gen_until_pass(|a, b| {
1316            if f(a, b).is_ok() {
1317                Ok(to_test.next().is_none())
1318            } else {
1319                Ok(false)
1320            }
1321        });
1322        assert!(ok);
1323    }
1324
1325    // Test that the `table_ops` fuzzer eventually runs the gc function in the host.
1326    // We've historically had issues where this fuzzer accidentally wasn't fuzzing
1327    // anything for a long time so this is an attempt to prevent that from happening
1328    // again.
1329    #[test]
1330    fn table_ops_eventually_gcs() {
1331        // Skip if we're under emulation because some fuzz configurations will do
1332        // large address space reservations that QEMU doesn't handle well.
1333        if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
1334            return;
1335        }
1336
1337        let ok = gen_until_pass(|(config, test), _| {
1338            let result = table_ops(config, test)?;
1339            Ok(result > 0)
1340        });
1341
1342        if !ok {
1343            panic!("gc was never found");
1344        }
1345    }
1346
1347    #[test]
1348    fn module_generation_uses_expected_proposals() {
1349        // Proposals that Wasmtime supports. Eventually a module should be
1350        // generated that needs these proposals.
1351        let mut expected = WasmFeatures::MUTABLE_GLOBAL
1352            | WasmFeatures::FLOATS
1353            | WasmFeatures::SIGN_EXTENSION
1354            | WasmFeatures::SATURATING_FLOAT_TO_INT
1355            | WasmFeatures::MULTI_VALUE
1356            | WasmFeatures::BULK_MEMORY
1357            | WasmFeatures::REFERENCE_TYPES
1358            | WasmFeatures::SIMD
1359            | WasmFeatures::MULTI_MEMORY
1360            | WasmFeatures::RELAXED_SIMD
1361            | WasmFeatures::THREADS
1362            | WasmFeatures::TAIL_CALL
1363            | WasmFeatures::WIDE_ARITHMETIC
1364            | WasmFeatures::MEMORY64
1365            | WasmFeatures::FUNCTION_REFERENCES
1366            | WasmFeatures::GC
1367            | WasmFeatures::GC_TYPES
1368            | WasmFeatures::CUSTOM_PAGE_SIZES
1369            | WasmFeatures::EXTENDED_CONST;
1370
1371        // All other features that wasmparser supports, which is presumably a
1372        // superset of the features that wasm-smith supports, are listed here as
1373        // unexpected. This means, for example, that if wasm-smith updates to
1374        // include a new proposal by default that wasmtime implements then it
1375        // will be required to be listed above.
1376        let unexpected = WasmFeatures::all() ^ expected;
1377
1378        let ok = gen_until_pass(|config: generators::Config, u| {
1379            let wasm = config.generate(u, None)?.to_bytes();
1380
1381            // Double-check the module is valid
1382            Validator::new_with_features(WasmFeatures::all()).validate_all(&wasm)?;
1383
1384            // If any of the unexpected features are removed then this module
1385            // should always be valid, otherwise something went wrong.
1386            for feature in unexpected.iter() {
1387                let ok =
1388                    Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1389                if ok.is_err() {
1390                    anyhow::bail!("generated a module with {feature:?} but that wasn't expected");
1391                }
1392            }
1393
1394            // If any of `expected` is removed and the module fails to validate,
1395            // then that means the module requires that feature. Remove that
1396            // from the set of features we're then expecting.
1397            for feature in expected.iter() {
1398                let ok =
1399                    Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1400                if ok.is_err() {
1401                    expected ^= feature;
1402                }
1403            }
1404
1405            Ok(expected.is_empty())
1406        });
1407
1408        if !ok {
1409            panic!("never generated wasm module using {expected:?}");
1410        }
1411    }
1412
1413    #[test]
1414    fn wast_smoke_test() {
1415        test_n_times(50, |(), u| super::wast_test(u));
1416    }
1417}