1#[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
42pub 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 Err(_) => drop(std::fs::remove_file(&wat)),
62 }
63}
64
65#[derive(Clone)]
68pub struct StoreLimits(Arc<LimitsState>);
69
70struct LimitsState {
71 remaining_memory: AtomicUsize,
73 remaining_copy_allowance: AtomicUsize,
75 oom: AtomicBool,
77}
78
79const MAX_MEMORY: usize = 1 << 30;
82
83const MAX_MEMORY_MOVED: usize = 4 << 30;
86
87impl StoreLimits {
88 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 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 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#[derive(Clone, Debug)]
166pub enum Timeout {
167 None,
170 Fuel(u64),
173 Epoch(Duration),
176}
177
178pub 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 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#[derive(Arbitrary, Debug)]
220pub enum Command {
221 Instantiate(usize),
227 Terminate(usize),
236}
237
238pub 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 modules.is_empty() {
260 return;
261 }
262
263 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 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 let string = e.to_string();
322 if string.contains("minimum element size") {
323 return None;
324 }
325
326 if string.contains("instance allocation for this module requires") {
332 return None;
333 }
334
335 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
358pub fn instantiate_with_dummy(store: &mut Store<StoreLimits>, module: &Module) -> Option<Instance> {
369 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 store.data().is_oom() {
393 log::debug!("failed to instantiate: OOM");
394 return None;
395 }
396
397 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 if string.contains("incompatible import type") {
409 log::debug!("failed to instantiate: {}", string);
410 return None;
411 }
412
413 if e.is::<wasmtime::PoolConcurrencyLimitError>() {
415 log::debug!("failed to instantiate: {}", string);
416 return None;
417 }
418
419 panic!("failed to instantiate: {e:?}");
421}
422
423pub 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 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 .map(|results| results.unwrap());
455 log::debug!(" -> rhs results on {}: {:?}", rhs.name(), &rhs_results);
456
457 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
500pub enum DiffEqResult<T, U> {
503 Success(T, U),
505 Poisoned,
508 Failed,
511}
512
513fn wasmtime_trap_is_non_deterministic(trap: &Trap) -> bool {
514 match trap {
515 Trap::AllocationTooLarge |
518 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 None => true,
536 }
537}
538
539impl<T, U> DiffEqResult<T, U> {
540 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 (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 (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 (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
588pub 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 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, ¶ms, &mut results);
673 }
674 }
675 }
676 }
677}
678
679pub 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 if test.config.hogs_memory.unwrap_or(false) {
697 return Err(arbitrary::Error::IncorrectFormat);
698 }
699
700 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 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 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
753pub 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 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 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 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 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 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 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 *self.state.should_exit.lock().unwrap() = true;
983 self.state.should_exit_cvar.notify_one();
984
985 thread.join().unwrap();
988 }
989}
990
991pub 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, ¶ms, &mut actual)
1073 .unwrap();
1074 log::trace!("received results {actual:?}");
1075 assert_eq!(actual, results);
1076 }
1077
1078 Ok(())
1079}
1080
1081pub 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 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 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 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; }
1150 },
1151 Err(_) => {
1152 log::info!("instantiation failed to complete");
1153 return; }
1155 };
1156
1157 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, ¶ms, &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 Ok(_) | Err(Exhausted::Polls) => {}
1191
1192 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 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 struct Timeout<F> {
1231 future: F,
1232 end: Instant,
1235 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 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]
1330 fn table_ops_eventually_gcs() {
1331 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 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 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 Validator::new_with_features(WasmFeatures::all()).validate_all(&wasm)?;
1383
1384 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 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}