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")]
14 pub mod diff_spec;
15 pub mod diff_wasmi;
16 pub mod diff_wasmtime;
17 pub mod dummy;
18 pub mod engine;
19 pub mod memory;
20 mod stacks;
21 
22 use self::diff_wasmtime::WasmtimeInstance;
23 use self::engine::{DiffEngine, DiffInstance};
24 use crate::generators::{self, DiffValue, DiffValueType};
25 use crate::single_module_fuzzer::KnownValid;
26 use arbitrary::Arbitrary;
27 pub use stacks::check_stacks;
28 use std::future::Future;
29 use std::pin::Pin;
30 use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering::SeqCst};
31 use std::sync::{Arc, Condvar, Mutex};
32 use std::task::{Context, Poll};
33 use std::time::{Duration, Instant};
34 use wasmtime::*;
35 use wasmtime_wast::WastContext;
36 
37 #[cfg(not(any(windows, target_arch = "s390x", target_arch = "riscv64")))]
38 mod diff_v8;
39 
40 static 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.
44 pub 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)]
68 pub struct StoreLimits(Arc<LimitsState>);
69 
70 struct 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.
81 const 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.
85 const MAX_MEMORY_MOVED: usize = 4 << 30;
86 
87 impl 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 
143 impl 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)]
166 pub 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.
182 pub 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)]
220 pub 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.
243 pub 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 
295 fn 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.
368 pub 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 instance =
373         dummy::dummy_linker(store, module).and_then(|l| l.instantiate(&mut *store, module));
374     unwrap_instance(store, instance)
375 }
376 
377 fn unwrap_instance(
378     store: &Store<StoreLimits>,
379     instance: anyhow::Result<Instance>,
380 ) -> Option<Instance> {
381     let e = match instance {
382         Ok(i) => return Some(i),
383         Err(e) => e,
384     };
385 
386     // If the instantiation hit OOM for some reason then that's ok, it's
387     // expected that fuzz-generated programs try to allocate lots of
388     // stuff.
389     if store.data().is_oom() {
390         log::debug!("failed to instantiate: OOM");
391         return None;
392     }
393 
394     // Allow traps which can happen normally with `unreachable` or a
395     // timeout or such
396     if let Some(trap) = e.downcast_ref::<Trap>() {
397         log::debug!("failed to instantiate: {}", trap);
398         return None;
399     }
400 
401     let string = e.to_string();
402     // Currently we instantiate with a `Linker` which can't instantiate
403     // every single module under the sun due to using name-based resolution
404     // rather than positional-based resolution
405     if string.contains("incompatible import type") {
406         log::debug!("failed to instantiate: {}", string);
407         return None;
408     }
409 
410     // Also allow failures to instantiate as a result of hitting pooling limits.
411     if e.is::<wasmtime::PoolConcurrencyLimitError>() {
412         log::debug!("failed to instantiate: {}", string);
413         return None;
414     }
415 
416     // Everything else should be a bug in the fuzzer or a bug in wasmtime
417     panic!("failed to instantiate: {e:?}");
418 }
419 
420 /// Evaluate the function identified by `name` in two different engine
421 /// instances--`lhs` and `rhs`.
422 ///
423 /// Returns `Ok(true)` if more evaluations can happen or `Ok(false)` if the
424 /// instances may have drifted apart and no more evaluations can happen.
425 ///
426 /// # Panics
427 ///
428 /// This will panic if the evaluation is different between engines (e.g.,
429 /// results are different, hashed instance is different, one side traps, etc.).
430 pub fn differential(
431     lhs: &mut dyn DiffInstance,
432     lhs_engine: &dyn DiffEngine,
433     rhs: &mut WasmtimeInstance,
434     name: &str,
435     args: &[DiffValue],
436     result_tys: &[DiffValueType],
437 ) -> anyhow::Result<bool> {
438     log::debug!("Evaluating: `{}` with {:?}", name, args);
439     let lhs_results = match lhs.evaluate(name, args, result_tys) {
440         Ok(Some(results)) => Ok(results),
441         Err(e) => Err(e),
442         // this engine couldn't execute this type signature, so discard this
443         // execution by returning success.
444         Ok(None) => return Ok(true),
445     };
446     log::debug!(" -> lhs results on {}: {:?}", lhs.name(), &lhs_results);
447 
448     let rhs_results = rhs
449         .evaluate(name, args, result_tys)
450         // wasmtime should be able to invoke any signature, so unwrap this result
451         .map(|results| results.unwrap());
452     log::debug!(" -> rhs results on {}: {:?}", rhs.name(), &rhs_results);
453 
454     // If Wasmtime hit its OOM condition, which is possible since it's set
455     // somewhat low while fuzzing, then don't return an error but return
456     // `false` indicating that differential fuzzing must stop. There's no
457     // guarantee the other engine has the same OOM limits as Wasmtime, and
458     // it's assumed that Wasmtime is configured to have a more conservative
459     // limit than the other engine.
460     if rhs.is_oom() {
461         return Ok(false);
462     }
463 
464     match DiffEqResult::new(lhs_engine, lhs_results, rhs_results) {
465         DiffEqResult::Success(lhs, rhs) => assert_eq!(lhs, rhs),
466         DiffEqResult::Poisoned => return Ok(false),
467         DiffEqResult::Failed => {}
468     }
469 
470     for (global, ty) in rhs.exported_globals() {
471         log::debug!("Comparing global `{global}`");
472         let lhs = match lhs.get_global(&global, ty) {
473             Some(val) => val,
474             None => continue,
475         };
476         let rhs = rhs.get_global(&global, ty).unwrap();
477         assert_eq!(lhs, rhs);
478     }
479     for (memory, shared) in rhs.exported_memories() {
480         log::debug!("Comparing memory `{memory}`");
481         let lhs = match lhs.get_memory(&memory, shared) {
482             Some(val) => val,
483             None => continue,
484         };
485         let rhs = rhs.get_memory(&memory, shared).unwrap();
486         if lhs == rhs {
487             continue;
488         }
489         eprintln!("differential memory is {} bytes long", lhs.len());
490         eprintln!("wasmtime memory is     {} bytes long", rhs.len());
491         panic!("memories have differing values");
492     }
493 
494     Ok(true)
495 }
496 
497 /// Result of comparing the result of two operations during differential
498 /// execution.
499 pub enum DiffEqResult<T, U> {
500     /// Both engines succeeded.
501     Success(T, U),
502     /// The result has reached the state where engines may have diverged and
503     /// results can no longer be compared.
504     Poisoned,
505     /// Both engines failed with the same error message, and internal state
506     /// should still match between the two engines.
507     Failed,
508 }
509 
510 impl<T, U> DiffEqResult<T, U> {
511     /// Computes the differential result from executing in two different
512     /// engines.
513     pub fn new(
514         lhs_engine: &dyn DiffEngine,
515         lhs_result: Result<T>,
516         rhs_result: Result<U>,
517     ) -> DiffEqResult<T, U> {
518         match (lhs_result, rhs_result) {
519             (Ok(lhs_result), Ok(rhs_result)) => DiffEqResult::Success(lhs_result, rhs_result),
520 
521             // Both sides failed. Check that the trap and state at the time of
522             // failure is the same, when possible.
523             (Err(lhs), Err(rhs)) => {
524                 let rhs = match rhs.downcast::<Trap>() {
525                     Ok(trap) => trap,
526 
527                     // For general, unknown errors, we can't rely on this being
528                     // a deterministic Wasm failure that both engines handled
529                     // identically, leaving Wasm in identical states. We could
530                     // just as easily be hitting engine-specific failures, like
531                     // different implementation-defined limits. So simply report
532                     // failure and move on to the next test.
533                     Err(err) => {
534                         log::debug!("rhs failed: {err:?}");
535                         return DiffEqResult::Failed;
536                     }
537                 };
538 
539                 // Even some traps are nondeterministic, and we can't rely on
540                 // the errors matching or leaving Wasm in the same state.
541                 let poisoned =
542                     // Allocations being too large for the GC are
543                     // implementation-defined.
544                     rhs == Trap::AllocationTooLarge
545                     // Stack size, and therefore when overflow happens, is
546                     // implementation-defined.
547                     || rhs == Trap::StackOverflow
548                     || lhs_engine.is_stack_overflow(&lhs);
549                 if poisoned {
550                     return DiffEqResult::Poisoned;
551                 }
552 
553                 lhs_engine.assert_error_match(&lhs, &rhs);
554                 DiffEqResult::Failed
555             }
556             // A real bug is found if only one side fails.
557             (Ok(_), Err(err)) => panic!("only the `rhs` failed for this input: {err:?}"),
558             (Err(err), Ok(_)) => panic!("only the `lhs` failed for this input: {err:?}"),
559         }
560     }
561 }
562 
563 /// Invoke the given API calls.
564 pub fn make_api_calls(api: generators::api::ApiCalls) {
565     use crate::generators::api::ApiCall;
566     use std::collections::HashMap;
567 
568     let mut store: Option<Store<StoreLimits>> = None;
569     let mut modules: HashMap<usize, Module> = Default::default();
570     let mut instances: HashMap<usize, Instance> = Default::default();
571 
572     for call in api.calls {
573         match call {
574             ApiCall::StoreNew(config) => {
575                 log::trace!("creating store");
576                 assert!(store.is_none());
577                 store = Some(config.to_store());
578             }
579 
580             ApiCall::ModuleNew { id, wasm } => {
581                 log::debug!("creating module: {}", id);
582                 log_wasm(&wasm);
583                 let module = match Module::new(store.as_ref().unwrap().engine(), &wasm) {
584                     Ok(m) => m,
585                     Err(_) => continue,
586                 };
587                 let old = modules.insert(id, module);
588                 assert!(old.is_none());
589             }
590 
591             ApiCall::ModuleDrop { id } => {
592                 log::trace!("dropping module: {}", id);
593                 drop(modules.remove(&id));
594             }
595 
596             ApiCall::InstanceNew { id, module } => {
597                 log::trace!("instantiating module {} as {}", module, id);
598                 let module = match modules.get(&module) {
599                     Some(m) => m,
600                     None => continue,
601                 };
602 
603                 let store = store.as_mut().unwrap();
604                 if let Some(instance) = instantiate_with_dummy(store, module) {
605                     instances.insert(id, instance);
606                 }
607             }
608 
609             ApiCall::InstanceDrop { id } => {
610                 log::trace!("dropping instance {}", id);
611                 instances.remove(&id);
612             }
613 
614             ApiCall::CallExportedFunc { instance, nth } => {
615                 log::trace!("calling instance export {} / {}", instance, nth);
616                 let instance = match instances.get(&instance) {
617                     Some(i) => i,
618                     None => {
619                         // Note that we aren't guaranteed to instantiate valid
620                         // modules, see comments in `InstanceNew` for details on
621                         // that. But the API call generator can't know if
622                         // instantiation failed, so we might not actually have
623                         // this instance. When that's the case, just skip the
624                         // API call and keep going.
625                         continue;
626                     }
627                 };
628                 let store = store.as_mut().unwrap();
629 
630                 let funcs = instance
631                     .exports(&mut *store)
632                     .filter_map(|e| match e.into_extern() {
633                         Extern::Func(f) => Some(f),
634                         _ => None,
635                     })
636                     .collect::<Vec<_>>();
637 
638                 if funcs.is_empty() {
639                     continue;
640                 }
641 
642                 let nth = nth % funcs.len();
643                 let f = &funcs[nth];
644                 let ty = f.ty(&store);
645                 if let Ok(params) = dummy::dummy_values(ty.params()) {
646                     let mut results = vec![Val::I32(0); ty.results().len()];
647                     let _ = f.call(store, &params, &mut results);
648                 }
649             }
650         }
651     }
652 }
653 
654 /// Executes the wast `test` with the `config` specified.
655 ///
656 /// Ensures that wast tests pass regardless of the `Config`.
657 pub fn wast_test(mut fuzz_config: generators::Config, test: generators::WastTest) {
658     crate::init_fuzzing();
659     let test = &test.test;
660 
661     // Discard tests that allocate a lot of memory as we don't want to OOM the
662     // fuzzer and we also limit memory growth which would cause the test to
663     // fail.
664     if test.config.hogs_memory.unwrap_or(false) {
665         return;
666     }
667 
668     // Transform `fuzz_config` to be valid for `test` and make sure that this
669     // test is supposed to pass.
670     let wast_config = fuzz_config.make_wast_test_compliant(test);
671     if test.should_fail(&wast_config) {
672         return;
673     }
674 
675     // Fuel and epochs don't play well with threads right now, so exclude any
676     // thread-spawning test if it looks like threads are spawned in that case.
677     if fuzz_config.wasmtime.consume_fuel || fuzz_config.wasmtime.epoch_interruption {
678         if test.contents.contains("(thread") {
679             return;
680         }
681     }
682 
683     log::debug!("running {:?}", test.path);
684     let mut wast_context = WastContext::new(fuzz_config.to_store());
685     wast_context
686         .register_spectest(&wasmtime_wast::SpectestConfig {
687             use_shared_memory: true,
688             suppress_prints: true,
689         })
690         .unwrap();
691     wast_context
692         .run_buffer(test.path.to_str().unwrap(), test.contents.as_bytes())
693         .unwrap();
694 }
695 
696 /// Execute a series of `table.get` and `table.set` operations.
697 ///
698 /// Returns the number of `gc` operations which occurred throughout the test
699 /// case -- used to test below that gc happens reasonably soon and eventually.
700 pub fn table_ops(
701     mut fuzz_config: generators::Config,
702     ops: generators::table_ops::TableOps,
703 ) -> Result<usize> {
704     let expected_drops = Arc::new(AtomicUsize::new(ops.num_params as usize));
705     let num_dropped = Arc::new(AtomicUsize::new(0));
706 
707     let num_gcs = Arc::new(AtomicUsize::new(0));
708     {
709         fuzz_config.wasmtime.consume_fuel = true;
710         let mut store = fuzz_config.to_store();
711         store.set_fuel(1_000).unwrap();
712 
713         let wasm = ops.to_wasm_binary();
714         log_wasm(&wasm);
715         let module = match compile_module(store.engine(), &wasm, KnownValid::No, &fuzz_config) {
716             Some(m) => m,
717             None => return Ok(0),
718         };
719 
720         let mut linker = Linker::new(store.engine());
721 
722         // To avoid timeouts, limit the number of explicit GCs we perform per
723         // test case.
724         const MAX_GCS: usize = 5;
725 
726         let func_ty = FuncType::new(
727             store.engine(),
728             vec![],
729             vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
730         );
731         let func = Func::new(&mut store, func_ty, {
732             let num_dropped = num_dropped.clone();
733             let expected_drops = expected_drops.clone();
734             let num_gcs = num_gcs.clone();
735             move |mut caller: Caller<'_, StoreLimits>, _params, results| {
736                 log::info!("table_ops: GC");
737                 if num_gcs.fetch_add(1, SeqCst) < MAX_GCS {
738                     caller.gc();
739                 }
740 
741                 let a = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
742                 let b = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
743                 let c = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
744 
745                 log::info!("table_ops: gc() -> ({:?}, {:?}, {:?})", a, b, c);
746 
747                 expected_drops.fetch_add(3, SeqCst);
748                 results[0] = Some(a).into();
749                 results[1] = Some(b).into();
750                 results[2] = Some(c).into();
751                 Ok(())
752             }
753         });
754         linker.define(&store, "", "gc", func).unwrap();
755 
756         linker
757             .func_wrap("", "take_refs", {
758                 let expected_drops = expected_drops.clone();
759                 move |caller: Caller<'_, StoreLimits>,
760                       a: Option<Rooted<ExternRef>>,
761                       b: Option<Rooted<ExternRef>>,
762                       c: Option<Rooted<ExternRef>>|
763                       -> Result<()> {
764                     log::info!("table_ops: take_refs({a:?}, {b:?}, {c:?})",);
765 
766                     // Do the assertion on each ref's inner data, even though it
767                     // all points to the same atomic, so that if we happen to
768                     // run into a use-after-free bug with one of these refs we
769                     // are more likely to trigger a segfault.
770                     if let Some(a) = a {
771                         let a = a
772                             .data(&caller)?
773                             .unwrap()
774                             .downcast_ref::<CountDrops>()
775                             .unwrap();
776                         assert!(a.0.load(SeqCst) <= expected_drops.load(SeqCst));
777                     }
778                     if let Some(b) = b {
779                         let b = b
780                             .data(&caller)?
781                             .unwrap()
782                             .downcast_ref::<CountDrops>()
783                             .unwrap();
784                         assert!(b.0.load(SeqCst) <= expected_drops.load(SeqCst));
785                     }
786                     if let Some(c) = c {
787                         let c = c
788                             .data(&caller)?
789                             .unwrap()
790                             .downcast_ref::<CountDrops>()
791                             .unwrap();
792                         assert!(c.0.load(SeqCst) <= expected_drops.load(SeqCst));
793                     }
794                     Ok(())
795                 }
796             })
797             .unwrap();
798 
799         let func_ty = FuncType::new(
800             store.engine(),
801             vec![],
802             vec![ValType::EXTERNREF, ValType::EXTERNREF, ValType::EXTERNREF],
803         );
804         let func = Func::new(&mut store, func_ty, {
805             let num_dropped = num_dropped.clone();
806             let expected_drops = expected_drops.clone();
807             move |mut caller, _params, results| {
808                 log::info!("table_ops: make_refs");
809 
810                 let a = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
811                 let b = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
812                 let c = ExternRef::new(&mut caller, CountDrops(num_dropped.clone()))?;
813                 expected_drops.fetch_add(3, SeqCst);
814 
815                 log::info!("table_ops: make_refs() -> ({:?}, {:?}, {:?})", a, b, c);
816 
817                 results[0] = Some(a).into();
818                 results[1] = Some(b).into();
819                 results[2] = Some(c).into();
820 
821                 Ok(())
822             }
823         });
824         linker.define(&store, "", "make_refs", func).unwrap();
825 
826         let instance = linker.instantiate(&mut store, &module).unwrap();
827         let run = instance.get_func(&mut store, "run").unwrap();
828 
829         {
830             let mut scope = RootScope::new(&mut store);
831 
832             log::info!(
833                 "table_ops: begin allocating {} externref arguments",
834                 ops.num_globals
835             );
836             let args: Vec<_> = (0..ops.num_params)
837                 .map(|_| {
838                     Ok(Val::ExternRef(Some(ExternRef::new(
839                         &mut scope,
840                         CountDrops(num_dropped.clone()),
841                     )?)))
842                 })
843                 .collect::<Result<_>>()?;
844             log::info!(
845                 "table_ops: end allocating {} externref arguments",
846                 ops.num_globals
847             );
848 
849             // The generated function should always return a trap. The only two
850             // valid traps are table-out-of-bounds which happens through `table.get`
851             // and `table.set` generated or an out-of-fuel trap. Otherwise any other
852             // error is unexpected and should fail fuzzing.
853             log::info!("table_ops: calling into Wasm `run` function");
854             let trap = run
855                 .call(&mut scope, &args, &mut [])
856                 .unwrap_err()
857                 .downcast::<Trap>()
858                 .unwrap();
859 
860             match trap {
861                 Trap::TableOutOfBounds | Trap::OutOfFuel => {}
862                 _ => panic!("unexpected trap: {trap}"),
863             }
864         }
865 
866         // Do a final GC after running the Wasm.
867         store.gc();
868     }
869 
870     assert_eq!(num_dropped.load(SeqCst), expected_drops.load(SeqCst));
871     return Ok(num_gcs.load(SeqCst));
872 
873     struct CountDrops(Arc<AtomicUsize>);
874 
875     impl Drop for CountDrops {
876         fn drop(&mut self) {
877             self.0.fetch_add(1, SeqCst);
878         }
879     }
880 }
881 
882 #[derive(Default)]
883 struct HelperThread {
884     state: Arc<HelperThreadState>,
885     thread: Option<std::thread::JoinHandle<()>>,
886 }
887 
888 #[derive(Default)]
889 struct HelperThreadState {
890     should_exit: Mutex<bool>,
891     should_exit_cvar: Condvar,
892 }
893 
894 impl HelperThread {
895     fn run_periodically(&mut self, dur: Duration, mut closure: impl FnMut() + Send + 'static) {
896         let state = self.state.clone();
897         self.thread = Some(std::thread::spawn(move || {
898             // Using our mutex/condvar we wait here for the first of `dur` to
899             // pass or the `HelperThread` instance to get dropped.
900             let mut should_exit = state.should_exit.lock().unwrap();
901             while !*should_exit {
902                 let (lock, result) = state
903                     .should_exit_cvar
904                     .wait_timeout(should_exit, dur)
905                     .unwrap();
906                 should_exit = lock;
907                 // If we timed out for sure then there's no need to continue
908                 // since we'll just abort on the next `checked_sub` anyway.
909                 if result.timed_out() {
910                     closure();
911                 }
912             }
913         }));
914     }
915 }
916 
917 impl Drop for HelperThread {
918     fn drop(&mut self) {
919         let thread = match self.thread.take() {
920             Some(thread) => thread,
921             None => return,
922         };
923         // Signal our thread that it should exit and wake it up in case it's
924         // sleeping.
925         *self.state.should_exit.lock().unwrap() = true;
926         self.state.should_exit_cvar.notify_one();
927 
928         // ... and then wait for the thread to exit to ensure we clean up
929         // after ourselves.
930         thread.join().unwrap();
931     }
932 }
933 
934 /// Generate and execute a `crate::generators::component_types::TestCase` using the specified `input` to create
935 /// arbitrary types and values.
936 pub fn dynamic_component_api_target(input: &mut arbitrary::Unstructured) -> arbitrary::Result<()> {
937     use crate::generators::component_types;
938     use component_fuzz_util::{TestCase, Type, EXPORT_FUNCTION, IMPORT_FUNCTION, MAX_TYPE_DEPTH};
939     use component_test_util::FuncExt;
940     use wasmtime::component::{Component, Linker, Val};
941 
942     crate::init_fuzzing();
943 
944     let mut types = Vec::new();
945     let mut type_fuel = 500;
946 
947     for _ in 0..5 {
948         types.push(Type::generate(input, MAX_TYPE_DEPTH, &mut type_fuel)?);
949     }
950     let params = (0..input.int_in_range(0..=5)?)
951         .map(|_| input.choose(&types))
952         .collect::<arbitrary::Result<Vec<_>>>()?;
953     let results = (0..input.int_in_range(0..=5)?)
954         .map(|_| input.choose(&types))
955         .collect::<arbitrary::Result<Vec<_>>>()?;
956 
957     let case = TestCase {
958         params,
959         results,
960         encoding1: input.arbitrary()?,
961         encoding2: input.arbitrary()?,
962     };
963 
964     let mut config = component_test_util::config();
965     if case.results.len() > 1 {
966         config.wasm_component_model_multiple_returns(true);
967     }
968     config.debug_adapter_modules(input.arbitrary()?);
969     let engine = Engine::new(&config).unwrap();
970     let mut store = Store::new(&engine, (Vec::new(), None));
971     let wat = case.declarations().make_component();
972     let wat = wat.as_bytes();
973     log_wasm(wat);
974     let component = Component::new(&engine, wat).unwrap();
975     let mut linker = Linker::new(&engine);
976 
977     linker
978         .root()
979         .func_new(IMPORT_FUNCTION, {
980             move |mut cx: StoreContextMut<'_, (Vec<Val>, Option<Vec<Val>>)>,
981                   params: &[Val],
982                   results: &mut [Val]|
983                   -> Result<()> {
984                 log::trace!("received params {params:?}");
985                 let (expected_args, expected_results) = cx.data_mut();
986                 assert_eq!(params.len(), expected_args.len());
987                 for (expected, actual) in expected_args.iter().zip(params) {
988                     assert_eq!(expected, actual);
989                 }
990                 results.clone_from_slice(&expected_results.take().unwrap());
991                 log::trace!("returning results {results:?}");
992                 Ok(())
993             }
994         })
995         .unwrap();
996 
997     let instance = linker.instantiate(&mut store, &component).unwrap();
998     let func = instance.get_func(&mut store, EXPORT_FUNCTION).unwrap();
999     let param_tys = func.params(&store);
1000     let result_tys = func.results(&store);
1001 
1002     while input.arbitrary()? {
1003         let params = param_tys
1004             .iter()
1005             .map(|(_, ty)| component_types::arbitrary_val(ty, input))
1006             .collect::<arbitrary::Result<Vec<_>>>()?;
1007         let results = result_tys
1008             .iter()
1009             .map(|ty| component_types::arbitrary_val(ty, input))
1010             .collect::<arbitrary::Result<Vec<_>>>()?;
1011 
1012         *store.data_mut() = (params.clone(), Some(results.clone()));
1013 
1014         log::trace!("passing params {params:?}");
1015         let mut actual = vec![Val::Bool(false); results.len()];
1016         func.call_and_post_return(&mut store, &params, &mut actual)
1017             .unwrap();
1018         log::trace!("received results {actual:?}");
1019         assert_eq!(actual, results);
1020     }
1021 
1022     Ok(())
1023 }
1024 
1025 /// Instantiates a wasm module and runs its exports with dummy values, all in
1026 /// an async fashion.
1027 ///
1028 /// Attempts to stress yields in host functions to ensure that exiting and
1029 /// resuming a wasm function call works.
1030 pub fn call_async(wasm: &[u8], config: &generators::Config, mut poll_amts: &[u32]) {
1031     let mut store = config.to_store();
1032     let module = match compile_module(store.engine(), wasm, KnownValid::Yes, config) {
1033         Some(module) => module,
1034         None => return,
1035     };
1036 
1037     // Configure a helper thread to periodically increment the epoch to
1038     // forcibly enable yields-via-epochs if epochs are in use. Note that this
1039     // is required because the wasm isn't otherwise guaranteed to necessarily
1040     // call any imports which will also increment the epoch.
1041     let mut helper_thread = HelperThread::default();
1042     if let generators::AsyncConfig::YieldWithEpochs { dur, .. } = &config.wasmtime.async_config {
1043         let engine = store.engine().clone();
1044         helper_thread.run_periodically(*dur, move || engine.increment_epoch());
1045     }
1046 
1047     // Generate a `Linker` where all function imports are custom-built to yield
1048     // periodically and additionally increment the epoch.
1049     let mut imports = Vec::new();
1050     for import in module.imports() {
1051         let item = match import.ty() {
1052             ExternType::Func(ty) => {
1053                 let poll_amt = take_poll_amt(&mut poll_amts);
1054                 Func::new_async(&mut store, ty.clone(), move |caller, _, results| {
1055                     let ty = ty.clone();
1056                     Box::new(async move {
1057                         caller.engine().increment_epoch();
1058                         log::info!("yielding {} times in import", poll_amt);
1059                         YieldN(poll_amt).await;
1060                         for (ret_ty, result) in ty.results().zip(results) {
1061                             *result = dummy::dummy_value(ret_ty)?;
1062                         }
1063                         Ok(())
1064                     })
1065                 })
1066                 .into()
1067             }
1068             other_ty => match dummy::dummy_extern(&mut store, other_ty) {
1069                 Ok(item) => item,
1070                 Err(e) => {
1071                     log::warn!("couldn't create import: {}", e);
1072                     return;
1073                 }
1074             },
1075         };
1076         imports.push(item);
1077     }
1078 
1079     // Run the instantiation process, asynchronously, and if everything
1080     // succeeds then pull out the instance.
1081     // log::info!("starting instantiation");
1082     let instance = run(Timeout {
1083         future: Instance::new_async(&mut store, &module, &imports),
1084         polls: take_poll_amt(&mut poll_amts),
1085         end: Instant::now() + Duration::from_millis(2_000),
1086     });
1087     let instance = match instance {
1088         Ok(instantiation_result) => match unwrap_instance(&store, instantiation_result) {
1089             Some(instance) => instance,
1090             None => {
1091                 log::info!("instantiation hit a nominal error");
1092                 return; // resource exhaustion or limits met
1093             }
1094         },
1095         Err(_) => {
1096             log::info!("instantiation failed to complete");
1097             return; // Timed out or ran out of polls
1098         }
1099     };
1100 
1101     // Run each export of the instance in the same manner as instantiation
1102     // above. Dummy values are passed in for argument values here:
1103     //
1104     // TODO: this should probably be more clever about passing in arguments for
1105     // example they might be used as pointers or something and always using 0
1106     // isn't too interesting.
1107     let funcs = instance
1108         .exports(&mut store)
1109         .filter_map(|e| {
1110             let name = e.name().to_string();
1111             let func = e.into_extern().into_func()?;
1112             Some((name, func))
1113         })
1114         .collect::<Vec<_>>();
1115     for (name, func) in funcs {
1116         let ty = func.ty(&store);
1117         let params = ty
1118             .params()
1119             .map(|ty| dummy::dummy_value(ty).unwrap())
1120             .collect::<Vec<_>>();
1121         let mut results = ty
1122             .results()
1123             .map(|ty| dummy::dummy_value(ty).unwrap())
1124             .collect::<Vec<_>>();
1125 
1126         log::info!("invoking export {:?}", name);
1127         let future = func.call_async(&mut store, &params, &mut results);
1128         match run(Timeout {
1129             future,
1130             polls: take_poll_amt(&mut poll_amts),
1131             end: Instant::now() + Duration::from_millis(2_000),
1132         }) {
1133             // On success or too many polls, try the next export.
1134             Ok(_) | Err(Exhausted::Polls) => {}
1135 
1136             // If time ran out then stop the current test case as we might have
1137             // already sucked up a lot of time for this fuzz test case so don't
1138             // keep it going.
1139             Err(Exhausted::Time) => return,
1140         }
1141     }
1142 
1143     fn take_poll_amt(polls: &mut &[u32]) -> u32 {
1144         match polls.split_first() {
1145             Some((a, rest)) => {
1146                 *polls = rest;
1147                 *a
1148             }
1149             None => 0,
1150         }
1151     }
1152 
1153     /// Helper future to yield N times before resolving.
1154     struct YieldN(u32);
1155 
1156     impl Future for YieldN {
1157         type Output = ();
1158 
1159         fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<()> {
1160             if self.0 == 0 {
1161                 Poll::Ready(())
1162             } else {
1163                 self.0 -= 1;
1164                 cx.waker().wake_by_ref();
1165                 Poll::Pending
1166             }
1167         }
1168     }
1169 
1170     /// Helper future for applying a timeout to `future` up to either when `end`
1171     /// is the current time or `polls` polls happen.
1172     ///
1173     /// Note that this helps to time out infinite loops in wasm, for example.
1174     struct Timeout<F> {
1175         future: F,
1176         /// If the future isn't ready by this time then the `Timeout<F>` future
1177         /// will return `None`.
1178         end: Instant,
1179         /// If the future doesn't resolve itself in this many calls to `poll`
1180         /// then the `Timeout<F>` future will return `None`.
1181         polls: u32,
1182     }
1183 
1184     enum Exhausted {
1185         Time,
1186         Polls,
1187     }
1188 
1189     impl<F: Future> Future for Timeout<F> {
1190         type Output = Result<F::Output, Exhausted>;
1191 
1192         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
1193             let (end, polls, future) = unsafe {
1194                 let me = self.get_unchecked_mut();
1195                 (me.end, &mut me.polls, Pin::new_unchecked(&mut me.future))
1196             };
1197             match future.poll(cx) {
1198                 Poll::Ready(val) => Poll::Ready(Ok(val)),
1199                 Poll::Pending => {
1200                     if Instant::now() >= end {
1201                         log::warn!("future operation timed out");
1202                         return Poll::Ready(Err(Exhausted::Time));
1203                     }
1204                     if *polls == 0 {
1205                         log::warn!("future operation ran out of polls");
1206                         return Poll::Ready(Err(Exhausted::Polls));
1207                     }
1208                     *polls -= 1;
1209                     Poll::Pending
1210                 }
1211             }
1212         }
1213     }
1214 
1215     fn run<F: Future>(future: F) -> F::Output {
1216         let mut f = Box::pin(future);
1217         let mut cx = Context::from_waker(futures::task::noop_waker_ref());
1218         loop {
1219             match f.as_mut().poll(&mut cx) {
1220                 Poll::Ready(val) => break val,
1221                 Poll::Pending => {}
1222             }
1223         }
1224     }
1225 }
1226 
1227 #[cfg(test)]
1228 mod tests {
1229     use super::*;
1230     use arbitrary::Unstructured;
1231     use rand::prelude::*;
1232     use wasmparser::{Validator, WasmFeatures};
1233 
1234     fn gen_until_pass<T: for<'a> Arbitrary<'a>>(
1235         mut f: impl FnMut(T, &mut Unstructured<'_>) -> Result<bool>,
1236     ) -> bool {
1237         let mut rng = SmallRng::seed_from_u64(0);
1238         let mut buf = vec![0; 2048];
1239         let n = 3000;
1240         for _ in 0..n {
1241             rng.fill_bytes(&mut buf);
1242             let mut u = Unstructured::new(&buf);
1243 
1244             if let Ok(config) = u.arbitrary() {
1245                 if f(config, &mut u).unwrap() {
1246                     return true;
1247                 }
1248             }
1249         }
1250         false
1251     }
1252 
1253     // Test that the `table_ops` fuzzer eventually runs the gc function in the host.
1254     // We've historically had issues where this fuzzer accidentally wasn't fuzzing
1255     // anything for a long time so this is an attempt to prevent that from happening
1256     // again.
1257     #[test]
1258     fn table_ops_eventually_gcs() {
1259         // Skip if we're under emulation because some fuzz configurations will do
1260         // large address space reservations that QEMU doesn't handle well.
1261         if std::env::var("WASMTIME_TEST_NO_HOG_MEMORY").is_ok() {
1262             return;
1263         }
1264 
1265         let ok = gen_until_pass(|(config, test), _| {
1266             let result = table_ops(config, test)?;
1267             Ok(result > 0)
1268         });
1269 
1270         if !ok {
1271             panic!("gc was never found");
1272         }
1273     }
1274 
1275     #[test]
1276     fn module_generation_uses_expected_proposals() {
1277         // Proposals that Wasmtime supports. Eventually a module should be
1278         // generated that needs these proposals.
1279         let mut expected = WasmFeatures::MUTABLE_GLOBAL
1280             | WasmFeatures::FLOATS
1281             | WasmFeatures::SIGN_EXTENSION
1282             | WasmFeatures::SATURATING_FLOAT_TO_INT
1283             | WasmFeatures::MULTI_VALUE
1284             | WasmFeatures::BULK_MEMORY
1285             | WasmFeatures::REFERENCE_TYPES
1286             | WasmFeatures::SIMD
1287             | WasmFeatures::MULTI_MEMORY
1288             | WasmFeatures::RELAXED_SIMD
1289             | WasmFeatures::THREADS
1290             | WasmFeatures::TAIL_CALL
1291             | WasmFeatures::WIDE_ARITHMETIC
1292             | WasmFeatures::MEMORY64
1293             | WasmFeatures::GC_TYPES
1294             | WasmFeatures::CUSTOM_PAGE_SIZES
1295             | WasmFeatures::EXTENDED_CONST;
1296 
1297         // All other features that wasmparser supports, which is presumably a
1298         // superset of the features that wasm-smith supports, are listed here as
1299         // unexpected. This means, for example, that if wasm-smith updates to
1300         // include a new proposal by default that wasmtime implements then it
1301         // will be required to be listed above.
1302         let unexpected = WasmFeatures::all() ^ expected;
1303 
1304         let ok = gen_until_pass(|config: generators::Config, u| {
1305             let wasm = config.generate(u, None)?.to_bytes();
1306 
1307             // Double-check the module is valid
1308             Validator::new_with_features(WasmFeatures::all()).validate_all(&wasm)?;
1309 
1310             // If any of the unexpected features are removed then this module
1311             // should always be valid, otherwise something went wrong.
1312             for feature in unexpected.iter() {
1313                 let ok =
1314                     Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1315                 if ok.is_err() {
1316                     anyhow::bail!("generated a module with {feature:?} but that wasn't expected");
1317                 }
1318             }
1319 
1320             // If any of `expected` is removed and the module fails to validate,
1321             // then that means the module requires that feature. Remove that
1322             // from the set of features we're then expecting.
1323             for feature in expected.iter() {
1324                 let ok =
1325                     Validator::new_with_features(WasmFeatures::all() ^ feature).validate_all(&wasm);
1326                 if ok.is_err() {
1327                     expected ^= feature;
1328                 }
1329             }
1330 
1331             Ok(expected.is_empty())
1332         });
1333 
1334         if !ok {
1335             panic!("never generated wasm module using {expected:?}");
1336         }
1337     }
1338 }
1339