Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Migrate core wasm tests to `*.wast` (#15090)

Alex Crichtond698ecb592e5
diff --git a/tests/all/component_model.rs b/tests/all/component_model.rs
index a73e33c281..83cb5877dd 100644
--- a/tests/all/component_model.rs
+++ b/tests/all/component_model.rs
@@ -109,66 +109,6 @@ impl ApiStyle {
}
}
-#[test]
-#[cfg_attr(miri, ignore)]
-fn components_importing_modules() -> Result<()> {
- let engine = engine();
-
- // FIXME: these components should actually get instantiated in `*.wast`
- // tests once supplying imports has actually been implemented.
-
- Component::new(
- &engine,
- r#"
- (component
- (import "a" (core module))
- )
- "#,
- )?;
-
- Component::new(
- &engine,
- r#"
- (component
- (import "a" (core module $m1
- (import "" "" (func))
- (import "" "x" (global i32))
-
- (export "a" (table 1 funcref))
- (export "b" (memory 1))
- (export "c" (func (result f32)))
- (export "d" (global i64))
- ))
-
- (core module $m2
- (func (export ""))
- (global (export "x") i32 i32.const 0)
- )
- (core instance $i2 (instantiate (module $m2)))
- (core instance $i1 (instantiate (module $m1) (with "" (instance $i2))))
-
- (core module $m3
- (import "mod" "1" (memory 1))
- (import "mod" "2" (table 1 funcref))
- (import "mod" "3" (global i64))
- (import "mod" "4" (func (result f32)))
- )
-
- (core instance $i3 (instantiate (module $m3)
- (with "mod" (instance
- (export "1" (memory $i1 "b"))
- (export "2" (table $i1 "a"))
- (export "3" (global $i1 "d"))
- (export "4" (func $i1 "c"))
- ))
- ))
- )
- "#,
- )?;
-
- Ok(())
-}
-
#[derive(Copy, Clone, PartialEq, Eq)]
enum Type {
S8,
diff --git a/tests/all/gc.rs b/tests/all/gc.rs
index 0cc08f7c02..db3424b982 100644
--- a/tests/all/gc.rs
+++ b/tests/all/gc.rs
@@ -1808,74 +1808,6 @@ fn elem_const_eval_oom() -> Result<()> {
Ok(())
}
-// The result of a `select` instruction with a GC reference type should be
-// declared as needing a stack map.
-#[test]
-#[cfg_attr(miri, ignore)]
-fn select_gc_ref_stack_map() -> Result<()> {
- let _ = env_logger::try_init();
-
- let mut config = Config::new();
- config.wasm_function_references(true);
- config.wasm_gc(true);
- // Use pooling allocator with a tiny GC heap so that GC is triggered
- // frequently and freed memory is reused quickly.
- let mut pool = crate::small_pool_config();
- pool.max_memory_size(1 << 16); // 64 KiB
- config.allocation_strategy(pool);
-
- let engine = Engine::new(&config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (type $pair (struct (field (mut i32))))
- (type $arr (array (mut i8)))
-
- (import "" "" (func $force_gc))
-
- (func (export "test") (param $cond i32) (result i32)
- ;; The select result stays on the Wasm operand stack (never
- ;; stored in a local variable). If the new SSA value created
- ;; by select is not declared for stack maps, the GC will
- ;; free it.
- (select (result (ref null $pair))
- (struct.new $pair (i32.const 111))
- (ref.null $pair)
- (local.get $cond)
- )
-
- ;; This call is a safepoint. The select result is live
- ;; across it. The called function triggers GC which will
- ;; free the struct if it is incorrectly omitted from the
- ;; safepoint's stack map, and the new allocations would
- ;; overwrite the freed memory.
- (call $force_gc)
-
- (drop (struct.new $pair (i32.const 222)))
-
- ;; Use the select result. If it was incorrectly freed, then
- ;; this will have the wrong value.
- (struct.get $pair 0)
- )
- )
- "#,
- )?;
-
- let mut store = Store::new(&engine, ());
- let force_gc = Func::wrap(&mut store, |mut caller: Caller<'_, _>| {
- caller.gc(None)?;
- Ok(())
- });
- let instance = Instance::new(&mut store, &module, &[force_gc.into()])?;
- let test = instance.get_typed_func::<(i32,), i32>(&mut store, "test")?;
-
- let result = test.call(&mut store, (1,))?;
- assert_eq!(result, 111);
-
- Ok(())
-}
-
#[test]
#[cfg_attr(miri, ignore)]
fn dropped_passive_elems_do_not_leak() -> Result<()> {
diff --git a/tests/all/linker.rs b/tests/all/linker.rs
index 55673d6786..882bbba2e8 100644
--- a/tests/all/linker.rs
+++ b/tests/all/linker.rs
@@ -137,40 +137,6 @@ fn function_interposition_renamed() -> Result<()> {
Ok(())
}
-// Similar to `function_interposition`, but use `Linker::instance` instead of
-// `Linker::define`.
-#[test]
-#[cfg_attr(miri, ignore)]
-fn module_interposition() -> Result<()> {
- let mut store = Store::<()>::default();
- let mut linker = Linker::new(store.engine());
- linker.allow_shadowing(true);
- let mut module = Module::new(
- store.engine(),
- r#"(module (func (export "export") (result i32) (i32.const 7)))"#,
- )?;
- for _ in 0..4 {
- let instance = linker.instantiate(&mut store, &module)?;
- linker.instance(&mut store, "instance", instance)?;
- module = Module::new(
- store.engine(),
- r#"(module
- (import "instance" "export" (func (result i32)))
- (func (export "export") (result i32) (i32.mul (call 0) (i32.const 2)))
- )"#,
- )?;
- }
- let instance = linker.instantiate(&mut store, &module)?;
- let func = instance
- .get_export(&mut store, "export")
- .unwrap()
- .into_func()
- .unwrap();
- let func = func.typed::<(), i32>(&store)?;
- assert_eq!(func.call(&mut store, ())?, 112);
- Ok(())
-}
-
#[test]
#[cfg_attr(miri, ignore)]
fn allow_unknown_exports() -> Result<()> {
diff --git a/tests/all/memory.rs b/tests/all/memory.rs
index 462310dbf1..2ab911a9f1 100644
--- a/tests/all/memory.rs
+++ b/tests/all/memory.rs
@@ -764,25 +764,6 @@ fn custom_1byte_page_cant_grow() -> Result<()> {
Ok(())
}
-#[wasmtime_test]
-#[cfg_attr(miri, ignore)]
-#[cfg(target_pointer_width = "64")] // requires large VM reservation
-fn init_with_negative_segment(cfg: &mut Config) -> Result<()> {
- let engine = Engine::new(cfg)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (memory 65536)
- (data (i32.const 0x8000_0000) "x")
- )
- "#,
- )?;
- let mut store = Store::new(&engine, ());
- Instance::new(&mut store, &module, &[])?;
- Ok(())
-}
-
#[test]
fn non_page_aligned_static_memory() -> Result<()> {
let mut config = Config::new();
diff --git a/tests/all/stack_switching.rs b/tests/all/stack_switching.rs
index 9345aadbc1..0cc7c317c9 100644
--- a/tests/all/stack_switching.rs
+++ b/tests/all/stack_switching.rs
@@ -3,7 +3,7 @@ use wasmtime::*;
mod test_utils {
use std::any::*;
use std::panic::AssertUnwindSafe;
- use wasmtime::{bail, *};
+ use wasmtime::*;
pub struct Runner {
pub engine: Engine,
@@ -65,22 +65,6 @@ mod test_utils {
}
}
- /// Creates a simple Host function that increments an i32
- pub fn make_i32_inc_host_func(runner: &mut Runner) -> Func {
- Func::new(
- &mut runner.store,
- FuncType::new(&runner.engine, vec![ValType::I32], vec![ValType::I32]),
- |mut _caller, args: &[Val], results: &mut [Val]| {
- let res = match args {
- [Val::I32(i)] => i + 1,
- _ => bail!("Error: Received illegal argument (should be single i32)"),
- };
- results[0] = Val::I32(res);
- Ok(())
- },
- )
- }
-
/// Creates a host function of type i32 -> i32. `export_func` must denote an
/// exported function of type i32 -> i32. The created host function
/// increments its argument by 1, passes it to the exported function, and in
@@ -266,79 +250,6 @@ mod wasi {
}
}
-/// Test that two distinct instantiations of the same module yield
-/// different control tag identities.
-#[cfg_attr(miri, ignore)]
-#[test]
-fn inter_instance_suspend() -> Result<()> {
- let mut config = Config::default();
- config.wasm_function_references(true);
- config.wasm_exceptions(true);
- config.wasm_stack_switching(true);
-
- let engine = Engine::new(&config)?;
-
- let mut store = Store::<()>::new(&engine, ());
-
- let wat_other = r#"
- (module
-
- (type $ft (func))
- (type $ct (cont $ft))
- (tag $tag)
-
-
- (func $suspend (export "suspend")
- (suspend $tag)
- )
-
- (func $resume (export "resume") (param $f (ref $ct))
- (block $handler (result (ref $ct))
- (resume $ct (on $tag $handler) (local.get $f))
- (unreachable)
- )
- (drop)
- )
- )
- "#;
-
- let wat_main = r#"
- (module
-
- (type $ft (func))
- (type $ct (cont $ft))
-
- (import "other" "suspend" (func $suspend))
- (import "other" "resume" (func $resume (param (ref $ct))))
-
- (elem declare func $suspend)
-
-
- (func $entry (export "entry")
- (call $resume (cont.new $ct (ref.func $suspend)))
- )
- )
- "#;
-
- let module_other = Module::new(&engine, wat_other)?;
-
- let other_inst1 = Instance::new(&mut store, &module_other, &[])?;
- let other_inst2 = Instance::new(&mut store, &module_other, &[])?;
-
- // Crucially, suspend and resume are from two separate instances
- // of the same module.
- let suspend = other_inst1.get_func(&mut store, "suspend").unwrap();
- let resume = other_inst2.get_func(&mut store, "resume").unwrap();
-
- let module_main = Module::new(&engine, wat_main)?;
- let main_instance = Instance::new(&mut store, &module_main, &[suspend.into(), resume.into()])?;
- let entry_func = main_instance.get_func(&mut store, "entry").unwrap();
-
- let result = entry_func.call(&mut store, &[], &mut []);
- assert!(result.is_err());
- Ok(())
-}
-
/// Regression test for https://github.com/bytecodealliance/wasmtime/issues/13750.
///
/// A GC reference returned through a continuation payload must retain
@@ -404,48 +315,6 @@ fn continuation_result_gc_ref_on_operand_stack() -> Result<()> {
Ok(())
}
-/// https://github.com/bytecodealliance/wasmtime/issues/14239.
-///
-/// GC should trace the suspended continuation and entry should
-/// complete normally.
-#[cfg_attr(miri, ignore)]
-#[test]
-fn gc_traces_a_suspended_continuation() -> Result<()> {
- let wat = r#"
- (module
- (type $ft (func))
- (type $ct (cont $ft))
- (type $st (struct (field i32)))
- (tag $t)
-
- (func $suspend
- (suspend $t)
- )
- (elem declare func $suspend)
-
- (func (export "entry")
- (local $continuation (ref null $ct))
- (local $i i32)
- (block $handler (result (ref $ct))
- (resume $ct
- (on $t $handler)
- (cont.new $ct (ref.func $suspend)))
- (return)
- )
- (local.set $continuation)
- (loop $allocate
- (drop (struct.new $st (i32.const 7)))
- (local.set $i (i32.add (local.get $i) (i32.const 1)))
- (br_if $allocate (i32.lt_u (local.get $i) (i32.const 20000)))
- )
- (resume $ct (local.get $continuation))
- )
- )
- "#;
-
- test_utils::Runner::new().run_test::<()>(wat, &[])
-}
-
/// Tests interaction with host functions. Note that the interaction with host
/// functions and traps is covered by the module `traps` further down.
mod host {
@@ -659,37 +528,6 @@ mod host {
Ok(())
}
- #[cfg_attr(miri, ignore)]
- #[test]
- /// Tests calling a host function from within a wasm function running inside a continuation.
- /// Call chain:
- /// $entry -resume-> a -call-> host_func_a
- fn call_host_from_continuation() -> Result<()> {
- let wat = r#"
- (module
- (type $ft (func (result i32)))
- (type $ct (cont $ft))
-
- (import "" "" (func $host_func_a (param i32) (result i32)))
-
- (func $a (export "a") (result i32)
- (call $host_func_a (i32.const 122))
- )
- (func $entry (export "entry") (result i32)
- (resume $ct (cont.new $ct (ref.func $a)))
- )
- )
- "#;
-
- let mut runner = Runner::new();
-
- let host_func_a = make_i32_inc_host_func(&mut runner);
-
- let result = runner.run_test::<i32>(wat, &[host_func_a.into()]).unwrap();
- assert_eq!(result, 123);
- Ok(())
- }
-
#[cfg_attr(miri, ignore)]
#[test]
/// We re-enter wasm from a host function and execute a continuation.
@@ -1447,51 +1285,6 @@ mod traps {
Ok(())
}
-
- #[cfg_attr(miri, ignore)]
- #[test]
- fn stack_overflow_in_continuation() -> Result<()> {
- let wat = r#"
- (module
- (type $ft (func (param i32)))
- (type $ct (cont $ft))
-
- (func $entry (export "entry")
- (call $a)
- )
-
- (func $a (export "a")
- ;; We ask for a billion recursive calls
- (i32.const 1_000_000_000)
-
- (resume $ct (cont.new $ct (ref.func $overflow)))
- )
-
- (func $overflow (export "overflow") (param $i i32)
- (block $continue
- (local.get $i)
- ;; return if $i == 0
- (br_if $continue)
- (return)
- )
- (i32.sub (local.get $i) (i32.const 1))
- (call $overflow)
- )
-
- )
- "#;
-
- let runner = Runner::new();
-
- let error = runner
- .run_test::<()>(wat, &[])
- .expect_err("Expecting execution to yield error");
-
- assert!(error.root_cause().is::<Trap>());
- assert_eq!(*error.downcast_ref::<Trap>().unwrap(), Trap::StackOverflow);
-
- Ok(())
- }
}
/// Regression test for https://github.com/bytecodealliance/wasmtime/issues/13322
diff --git a/tests/all/structs.rs b/tests/all/structs.rs
index e5d3b1edfb..cafbc87dfd 100644
--- a/tests/all/structs.rs
+++ b/tests/all/structs.rs
@@ -730,29 +730,6 @@ fn can_put_funcrefs_in_structs() -> Result<()> {
Ok(())
}
-#[test]
-#[cfg_attr(miri, ignore)]
-fn struct_ref_struct_in_same_rec_group_in_global() -> Result<()> {
- let mut store = gc_store()?;
- let module = Module::new(
- store.engine(),
- r#"
- (module
- (rec
- (type $b (struct))
- (type $a (struct (field (ref $b))))
- )
- (global (ref $a)
- struct.new_default $b
- struct.new $a
- )
- )
- "#,
- )?;
- let _instance = Instance::new(&mut store, &module, &[])?;
- Ok(())
-}
-
#[test]
fn structref_to_anyref_rooted() -> Result<()> {
let mut store = gc_store()?;
diff --git a/tests/all/table.rs b/tests/all/table.rs
index b47670546b..1b0084f716 100644
--- a/tests/all/table.rs
+++ b/tests/all/table.rs
@@ -377,24 +377,3 @@ fn host_table_keep_type_registration() -> Result<()> {
Ok(())
}
-
-#[test]
-#[cfg_attr(miri, ignore)]
-fn gc_store_with_table_initializers() -> Result<()> {
- let mut config = Config::new();
- config.wasm_gc(true);
- config.wasm_function_references(true);
- let engine = Engine::new(&config)?;
-
- let test = |wat: &str| -> Result<()> {
- let module = Module::new(&engine, wat)?;
- Instance::new(&mut Store::new(&engine, ()), &module, &[])?;
- Ok(())
- };
-
- test("(module (table 1 i31ref))")?;
- test("(module (table 1 i31ref (ref.i31 (i32.const 1))))")?;
- test("(module (table 1 i31ref (ref.null i31)))")?;
-
- Ok(())
-}
diff --git a/tests/all/threads.rs b/tests/all/threads.rs
index a8113876f4..66566308fe 100644
--- a/tests/all/threads.rs
+++ b/tests/all/threads.rs
@@ -1,10 +1,3 @@
-use std::{
- collections::{HashSet, hash_map::RandomState},
- sync::{
- Arc, RwLock,
- atomic::{AtomicBool, Ordering},
- },
-};
use wasmtime::*;
pub fn engine() -> Option<Engine> {
@@ -59,50 +52,6 @@ fn test_export_shared_memory() -> Result<()> {
Ok(())
}
-#[test]
-#[cfg_attr(miri, ignore)]
-fn test_sharing_of_shared_memory() -> Result<()> {
- let wat = r#"(module
- (import "env" "memory" (memory 1 5 shared))
- (func (export "first_word") (result i32) (i32.load (i32.const 0)))
- )"#;
- let Some(engine) = engine() else {
- return Ok(());
- };
- let module = Module::new(&engine, wat)?;
- let mut store = Store::new(&engine, ());
- let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 5)?)?;
- let instance1 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?;
- let instance2 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?;
- let data = shared_memory.data();
-
- // Modify the memory in one place.
- unsafe {
- *data[0].get() = 42;
- }
-
- // Verify that the memory is the same in all shared locations.
- let shared_memory_first_word = i32::from_le_bytes(unsafe {
- [
- *data[0].get(),
- *data[1].get(),
- *data[2].get(),
- *data[3].get(),
- ]
- });
- let instance1_first_word = instance1
- .get_typed_func::<(), i32>(&mut store, "first_word")?
- .call(&mut store, ())?;
- let instance2_first_word = instance2
- .get_typed_func::<(), i32>(&mut store, "first_word")?
- .call(&mut store, ())?;
- assert_eq!(shared_memory_first_word, 42);
- assert_eq!(instance1_first_word, 42);
- assert_eq!(instance2_first_word, 42);
-
- Ok(())
-}
-
#[test]
#[cfg_attr(miri, ignore)]
fn test_probe_shared_memory_size() -> Result<()> {
@@ -166,127 +115,6 @@ fn test_multi_memory() -> Result<()> {
Ok(())
}
-#[test]
-#[cfg_attr(miri, ignore)]
-fn test_grow_memory_in_multiple_threads() -> Result<()> {
- const NUM_THREADS: usize = 4;
- const NUM_GROW_OPS: usize = 1000;
-
- let wat = r#"(module
- (import "env" "memory" (memory 1 4000 shared))
- (func (export "grow") (param $delta i32) (result i32) (memory.grow (local.get $delta)))
- )"#;
-
- let Some(engine) = engine() else {
- return Ok(());
- };
- let module = Module::new(&engine, wat)?;
- let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, NUM_GROW_OPS as u32)?)?;
- let mut threads = vec![];
- let observed_sizes = Arc::new(RwLock::new(vec![]));
-
- // Spawn several threads using a single shared memory and grow the memory
- // concurrently on all threads.
- for _ in 0..NUM_THREADS {
- let engine = engine.clone();
- let module = module.clone();
- let observed_sizes = observed_sizes.clone();
- let shared_memory = shared_memory.clone();
- let thread = std::thread::spawn(move || {
- let mut store = Store::new(&engine, ());
- let instance = Instance::new(&mut store, &module, &[shared_memory.into()]).unwrap();
- let grow_fn = instance
- .get_typed_func::<i32, i32>(&mut store, "grow")
- .unwrap();
- let mut thread_local_observed_sizes: Vec<_> = (0..NUM_GROW_OPS / NUM_THREADS)
- .map(|_| grow_fn.call(&mut store, 1).unwrap() as u32)
- .collect();
- println!(
- "Returned memory sizes for {:?}: {:?}",
- std::thread::current().id(),
- thread_local_observed_sizes
- );
- assert!(is_sorted(thread_local_observed_sizes.as_slice()));
- observed_sizes
- .write()
- .unwrap()
- .append(&mut thread_local_observed_sizes);
- });
- threads.push(thread);
- }
-
- // Wait for all threads to finish.
- for t in threads {
- t.join().unwrap()
- }
-
- // Ensure the returned "old memory sizes" are all unique--i.e., we have not
- // observed the same growth twice.
- let unique_observed_sizes: HashSet<u32, RandomState> =
- HashSet::from_iter(observed_sizes.read().unwrap().iter().cloned());
- assert_eq!(
- observed_sizes.read().unwrap().len(),
- unique_observed_sizes.len()
- );
-
- Ok(())
-}
-
-fn is_sorted(data: &[u32]) -> bool {
- data.array_windows().all(|[a, b]| a <= b)
-}
-
-#[test]
-#[cfg_attr(miri, ignore)]
-fn test_memory_size_accessibility() -> Result<()> {
- const NUM_GROW_OPS: usize = 1000;
- let wat = r#"(module
- (import "env" "memory" (memory $memory 1 1000 shared))
- (func (export "probe_last_available") (result i32)
- (local $last_address i32)
- (local.set $last_address (i32.sub (i32.mul (memory.size) (i32.const 0x10000)) (i32.const 4)))
- (i32.load $memory (local.get $last_address))
- )
- )"#;
-
- let Some(engine) = engine() else {
- return Ok(());
- };
- let module = Module::new(&engine, wat)?;
- let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, NUM_GROW_OPS as u32)?)?;
- let done = Arc::new(AtomicBool::new(false));
-
- let grow_memory = shared_memory.clone();
- let grow_thread = std::thread::spawn(move || {
- for i in 0..NUM_GROW_OPS {
- if grow_memory.grow(1).is_err() {
- println!("stopping at grow operation #{i}");
- break;
- }
- }
- });
-
- let probe_memory = shared_memory.clone();
- let probe_done = done.clone();
- let probe_thread = std::thread::spawn(move || {
- let mut store = Store::new(&engine, ());
- let instance = Instance::new(&mut store, &module, &[probe_memory.into()]).unwrap();
- let probe_fn = instance
- .get_typed_func::<(), i32>(&mut store, "probe_last_available")
- .unwrap();
- while !probe_done.load(Ordering::SeqCst) {
- let value = probe_fn.call(&mut store, ()).unwrap() as u32;
- assert_eq!(value, 0);
- }
- });
-
- grow_thread.join().unwrap();
- done.store(true, Ordering::SeqCst);
- probe_thread.join().unwrap();
-
- Ok(())
-}
-
#[test]
fn create_shared_memory_through_memory() -> Result<()> {
let engine = Engine::default();
diff --git a/tests/all/traps.rs b/tests/all/traps.rs
index d52d4cfc31..9e67a124a7 100644
--- a/tests/all/traps.rs
+++ b/tests/all/traps.rs
@@ -600,31 +600,6 @@ fn mismatched_arguments() -> Result<()> {
Ok(())
}
-#[test]
-fn call_signature_mismatch() -> Result<()> {
- let mut store = Store::<()>::default();
- let binary = wat::parse_str(
- r#"
- (module $a
- (func $foo
- i32.const 0
- call_indirect)
- (func $bar (param i32))
- (start $foo)
-
- (table 1 funcref)
- (elem (i32.const 0) 1)
- )
- "#,
- )?;
-
- let module = Module::new(store.engine(), &binary)?;
- let err = Instance::new(&mut store, &module, &[]).err().unwrap();
- err.downcast_ref::<Trap>().unwrap();
- err.assert_contains("wasm trap: indirect call type mismatch");
- Ok(())
-}
-
#[test]
fn start_trap_pretty() -> Result<()> {
let mut store = Store::<()>::default();
@@ -1341,100 +1316,6 @@ fn host_return_error_no_backtrace() -> Result<()> {
Ok(())
}
-#[test]
-fn div_plus_load_reported_right() -> Result<()> {
- let engine = Engine::default();
- let mut store = Store::new(&engine, ());
- let module = Module::new(
- &engine,
- r#"
- (module
- (memory (export "memory") 1)
- (func (export "i32.div_s") (param i32 i32) (result i32)
- (i32.div_s (local.get 0) (i32.load (local.get 1))))
- (func (export "i32.div_u") (param i32 i32) (result i32)
- (i32.div_u (local.get 0) (i32.load (local.get 1))))
- (func (export "i32.rem_s") (param i32 i32) (result i32)
- (i32.rem_s (local.get 0) (i32.load (local.get 1))))
- (func (export "i32.rem_u") (param i32 i32) (result i32)
- (i32.rem_u (local.get 0) (i32.load (local.get 1))))
- )
- "#,
- )?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let memory = instance.get_memory(&mut store, "memory").unwrap();
- let i32_div_s = instance.get_typed_func::<(i32, i32), i32>(&mut store, "i32.div_s")?;
- let i32_div_u = instance.get_typed_func::<(u32, u32), u32>(&mut store, "i32.div_u")?;
- let i32_rem_s = instance.get_typed_func::<(i32, i32), i32>(&mut store, "i32.rem_s")?;
- let i32_rem_u = instance.get_typed_func::<(u32, u32), u32>(&mut store, "i32.rem_u")?;
-
- memory.write(&mut store, 0, &1i32.to_le_bytes()).unwrap();
- memory.write(&mut store, 4, &0i32.to_le_bytes()).unwrap();
- memory.write(&mut store, 8, &(-1i32).to_le_bytes()).unwrap();
-
- assert_eq!(i32_div_s.call(&mut store, (100, 0))?, 100);
- assert_eq!(i32_div_u.call(&mut store, (101, 0))?, 101);
- assert_eq!(i32_rem_s.call(&mut store, (102, 0))?, 0);
- assert_eq!(i32_rem_u.call(&mut store, (103, 0))?, 0);
-
- assert_trap(
- i32_div_s.call(&mut store, (100, 4)),
- Trap::IntegerDivisionByZero,
- );
- assert_trap(
- i32_div_u.call(&mut store, (100, 4)),
- Trap::IntegerDivisionByZero,
- );
- assert_trap(
- i32_rem_s.call(&mut store, (100, 4)),
- Trap::IntegerDivisionByZero,
- );
- assert_trap(
- i32_rem_u.call(&mut store, (100, 4)),
- Trap::IntegerDivisionByZero,
- );
-
- assert_trap(
- i32_div_s.call(&mut store, (i32::MIN, 8)),
- Trap::IntegerOverflow,
- );
- assert_eq!(i32_rem_s.call(&mut store, (i32::MIN, 8))?, 0);
-
- assert_trap(
- i32_div_s.call(&mut store, (100, 100_000)),
- Trap::MemoryOutOfBounds,
- );
- assert_trap(
- i32_div_u.call(&mut store, (100, 100_000)),
- Trap::MemoryOutOfBounds,
- );
- assert_trap(
- i32_rem_s.call(&mut store, (100, 100_000)),
- Trap::MemoryOutOfBounds,
- );
- assert_trap(
- i32_rem_u.call(&mut store, (100, 100_000)),
- Trap::MemoryOutOfBounds,
- );
-
- return Ok(());
-
- #[track_caller]
- fn assert_trap<T>(result: Result<T>, expected: Trap) {
- match result {
- Ok(_) => panic!("expected failure"),
- Err(e) => {
- if let Some(code) = e.downcast_ref::<Trap>() {
- if *code == expected {
- return;
- }
- }
- panic!("unexpected error {e:?}");
- }
- }
- }
-}
-
#[test]
fn wasm_fault_address_reported_by_default() -> Result<()> {
let mut config = Config::new();
diff --git a/tests/all/wait_notify.rs b/tests/all/wait_notify.rs
index e154bef382..045bed764b 100644
--- a/tests/all/wait_notify.rs
+++ b/tests/all/wait_notify.rs
@@ -39,84 +39,3 @@ fn atomic_wait_timeout_length() -> Result<()> {
assert_eq!(func_ret, 2);
Ok(())
}
-
-#[test]
-fn atomic_wait_notify_basic() -> Result<()> {
- let wat = r#"(module
- (import "env" "memory" (memory 1 1 shared))
-
- (func (export "first_thread") (result i32)
- (drop (memory.atomic.wait32 (i32.const 4) (i32.const 0) (i64.const -1)))
- (i32.atomic.store (i32.const 0) (i32.const 42))
- (drop (memory.atomic.notify (i32.const 0) (i32.const -1)))
- (i32.atomic.load (i32.const 0))
- )
-
- (func (export "second_thread") (result i32)
- (i32.atomic.store (i32.const 4) (i32.const 21))
- (drop (memory.atomic.notify (i32.const 4) (i32.const -1)))
- (drop (memory.atomic.wait32 (i32.const 0) (i32.const 0) (i64.const -1)))
- (i32.atomic.load (i32.const 0))
- )
-
- (data (i32.const 0) "\00\00\00\00")
- (data (i32.const 4) "\00\00\00\00")
- )"#;
- let Some(engine) = engine() else {
- return Ok(());
- };
- let module = Module::new(&engine, wat)?;
- let mut store = Store::new(&engine, ());
- let shared_memory = SharedMemory::new(&engine, MemoryType::shared(1, 1)?)?;
- let instance1 = Instance::new(&mut store, &module, &[shared_memory.clone().into()])?;
-
- let thread = {
- let engine = engine.clone();
- let module = module.clone();
- let shared_memory = shared_memory.clone();
- std::thread::spawn(move || {
- let mut store = Store::new(&engine, ());
- let instance2 = Instance::new(&mut store, &module, &[shared_memory.into()]).unwrap();
-
- let instance2_first_word = instance2
- .get_typed_func::<(), i32>(&mut store, "second_thread")
- .unwrap()
- .call(&mut store, ())
- .unwrap();
-
- assert_eq!(instance2_first_word, 42);
- })
- };
-
- let instance1_first_word = instance1
- .get_typed_func::<(), i32>(&mut store, "first_thread")
- .unwrap()
- .call(&mut store, ())
- .unwrap();
- assert_eq!(instance1_first_word, 42);
-
- thread.join().unwrap();
-
- let data = shared_memory.data();
- // Verify that the memory is the same in all shared locations.
- let shared_memory_first_word = i32::from_le_bytes(unsafe {
- [
- *data[0].get(),
- *data[1].get(),
- *data[2].get(),
- *data[3].get(),
- ]
- });
- assert_eq!(shared_memory_first_word, 42);
-
- let shared_memory_second_word = i32::from_le_bytes(unsafe {
- [
- *data[4].get(),
- *data[5].get(),
- *data[6].get(),
- *data[7].get(),
- ]
- });
- assert_eq!(shared_memory_second_word, 21);
- Ok(())
-}
diff --git a/tests/misc_testsuite/big-memory-behavior.wast b/tests/misc_testsuite/big-memory-behavior.wast
index d336193451..0229ce9075 100644
--- a/tests/misc_testsuite/big-memory-behavior.wast
+++ b/tests/misc_testsuite/big-memory-behavior.wast
@@ -17,3 +17,9 @@
(assert_return (invoke "size") (i32.const 0x10000))
(assert_return (invoke "grow" (i32.const 0)) (i32.const 0x10000))
(assert_return (invoke "grow" (i32.const 1)) (i32.const -1))
+
+;; A data segment at a "negative" `i32` offset is still in bounds of a 4GiB
+;; memory.
+(module
+ (memory 65536)
+ (data (i32.const 0x8000_0000) "x"))
diff --git a/tests/misc_testsuite/call_indirect.wast b/tests/misc_testsuite/call_indirect.wast
index aa242d6222..d639408584 100644
--- a/tests/misc_testsuite/call_indirect.wast
+++ b/tests/misc_testsuite/call_indirect.wast
@@ -185,3 +185,15 @@
(assert_return (invoke "type-second-i64") (i64.const 64))
(assert_return (invoke "type-second-f32") (f32.const 32))
(assert_return (invoke "type-second-f64") (f64.const 64.1))
+
+;; A signature mismatch in a start function traps during instantiation.
+(assert_trap
+ (module
+ (func $foo
+ i32.const 0
+ call_indirect)
+ (func $bar (param i32))
+ (start $foo)
+ (table 1 funcref)
+ (elem (i32.const 0) $bar))
+ "indirect call type mismatch")
diff --git a/tests/misc_testsuite/component-model/async/sync-call-context.wast b/tests/misc_testsuite/component-model/async/sync-call-context.wast
index 3ca8d855a1..b92aea406e 100644
--- a/tests/misc_testsuite/component-model/async/sync-call-context.wast
+++ b/tests/misc_testsuite/component-model/async/sync-call-context.wast
@@ -566,7 +566,7 @@
(func (export "f") (param i32) (result i32)
(if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
(call $context.set (i32.const 0x5678))
- (drop (call $host))
+ (if (i32.ne (call $host) (i32.const 2)) (then unreachable))
(if (i32.ne (call $context.get) (i32.const 0x5678))
(then unreachable))
(i32.add (local.get 0) (i32.const 42))
@@ -635,7 +635,7 @@
(func (export "f") (param i32) (result i32)
(if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
(call $context.set (local.get 0))
- (drop (call $host))
+ (if (i32.ne (call $host) (i32.const 2)) (then unreachable))
(if (i32.ne (call $context.get) (local.get 0)) (then unreachable))
(i32.add (local.get 0) (i32.const 42))
)
@@ -706,7 +706,7 @@
(func (export "leaf") (param i32) (result i32)
(if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
(call $context.set (i32.const 0x0c0ffee0))
- (drop (call $host))
+ (if (i32.ne (call $host) (i32.const 2)) (then unreachable))
(if (i32.ne (call $context.get) (i32.const 0x0c0ffee0))
(then unreachable))
(i32.add (local.get 0) (i32.const 1))
@@ -823,7 +823,7 @@
(if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
(call $context.set (i32.const 0x0d00d100))
(local.set $r (call $leaf (local.get 0)))
- (drop (call $host))
+ (if (i32.ne (call $host) (i32.const 2)) (then unreachable))
(if (i32.ne (call $context.get) (i32.const 0x0d00d100))
(then unreachable))
(i32.add (local.get $r) (i32.const 10))
@@ -901,7 +901,7 @@
(func (export "leaf") (param i32) (result i32)
(if (i32.ne (call $context.get) (i32.const 0)) (then unreachable))
(call $context.set (i32.const 0x0feed000))
- (drop (call $host))
+ (if (i32.ne (call $host) (i32.const 2)) (then unreachable))
(if (i32.ne (call $context.get) (i32.const 0x0feed000))
(then unreachable))
(i32.add (local.get 0) (i32.const 1))
diff --git a/tests/misc_testsuite/component-model/modules.wast b/tests/misc_testsuite/component-model/modules.wast
index aa0be31966..1bc008755a 100644
--- a/tests/misc_testsuite/component-model/modules.wast
+++ b/tests/misc_testsuite/component-model/modules.wast
@@ -526,3 +526,43 @@
))
)
"export `g` has the wrong type")
+
+;; Components can import core modules directly.
+(component definition
+ (import "a" (core module))
+)
+
+(component definition
+ (import "a" (core module $m1
+ (import "" "" (func))
+ (import "" "x" (global i32))
+
+ (export "a" (table 1 funcref))
+ (export "b" (memory 1))
+ (export "c" (func (result f32)))
+ (export "d" (global i64))
+ ))
+
+ (core module $m2
+ (func (export ""))
+ (global (export "x") i32 i32.const 0)
+ )
+ (core instance $i2 (instantiate (module $m2)))
+ (core instance $i1 (instantiate (module $m1) (with "" (instance $i2))))
+
+ (core module $m3
+ (import "mod" "1" (memory 1))
+ (import "mod" "2" (table 1 funcref))
+ (import "mod" "3" (global i64))
+ (import "mod" "4" (func (result f32)))
+ )
+
+ (core instance $i3 (instantiate (module $m3)
+ (with "mod" (instance
+ (export "1" (memory $i1 "b"))
+ (export "2" (table $i1 "a"))
+ (export "3" (global $i1 "d"))
+ (export "4" (func $i1 "c"))
+ ))
+ ))
+)
diff --git a/tests/misc_testsuite/div-rem.wast b/tests/misc_testsuite/div-rem.wast
index 3bcfae85d6..5003528708 100644
--- a/tests/misc_testsuite/div-rem.wast
+++ b/tests/misc_testsuite/div-rem.wast
@@ -29,3 +29,56 @@
)
(assert_return (invoke "i64.rem_s" (i64.const 123121) (i64.const -1)) (i64.const 0))
+
+;; The divisor is loaded from memory, so a trap may come from the load or from
+;; the division, and each must be reported correctly.
+(module
+ (memory 1)
+ (data (i32.const 0) "\01\00\00\00")
+ (data (i32.const 4) "\00\00\00\00")
+ (data (i32.const 8) "\ff\ff\ff\ff")
+ (func (export "i32.div_s") (param i32 i32) (result i32)
+ (i32.div_s (local.get 0) (i32.load (local.get 1))))
+ (func (export "i32.div_u") (param i32 i32) (result i32)
+ (i32.div_u (local.get 0) (i32.load (local.get 1))))
+ (func (export "i32.rem_s") (param i32 i32) (result i32)
+ (i32.rem_s (local.get 0) (i32.load (local.get 1))))
+ (func (export "i32.rem_u") (param i32 i32) (result i32)
+ (i32.rem_u (local.get 0) (i32.load (local.get 1))))
+)
+
+;; Divide by 1.
+(assert_return (invoke "i32.div_s" (i32.const 100) (i32.const 0))
+ (i32.const 100))
+(assert_return (invoke "i32.div_u" (i32.const 101) (i32.const 0))
+ (i32.const 101))
+(assert_return (invoke "i32.rem_s" (i32.const 102) (i32.const 0))
+ (i32.const 0))
+(assert_return (invoke "i32.rem_u" (i32.const 103) (i32.const 0))
+ (i32.const 0))
+
+;; Divide by 0.
+(assert_trap (invoke "i32.div_s" (i32.const 100) (i32.const 4))
+ "integer divide by zero")
+(assert_trap (invoke "i32.div_u" (i32.const 100) (i32.const 4))
+ "integer divide by zero")
+(assert_trap (invoke "i32.rem_s" (i32.const 100) (i32.const 4))
+ "integer divide by zero")
+(assert_trap (invoke "i32.rem_u" (i32.const 100) (i32.const 4))
+ "integer divide by zero")
+
+;; Divide by -1.
+(assert_trap (invoke "i32.div_s" (i32.const 0x80000000) (i32.const 8))
+ "integer overflow")
+(assert_return (invoke "i32.rem_s" (i32.const 0x80000000) (i32.const 8))
+ (i32.const 0))
+
+;; The load is out of bounds.
+(assert_trap (invoke "i32.div_s" (i32.const 100) (i32.const 100000))
+ "out of bounds memory access")
+(assert_trap (invoke "i32.div_u" (i32.const 100) (i32.const 100000))
+ "out of bounds memory access")
+(assert_trap (invoke "i32.rem_s" (i32.const 100) (i32.const 100000))
+ "out of bounds memory access")
+(assert_trap (invoke "i32.rem_u" (i32.const 100) (i32.const 100000))
+ "out of bounds memory access")
diff --git a/tests/misc_testsuite/gc/const-expr-gc.wast b/tests/misc_testsuite/gc/const-expr-gc.wast
index 0b8e7e6db3..f5f84167f8 100644
--- a/tests/misc_testsuite/gc/const-expr-gc.wast
+++ b/tests/misc_testsuite/gc/const-expr-gc.wast
@@ -171,3 +171,13 @@
)
)
(assert_return (invoke "ref-i31-const") (i32.const 42))
+
+;; A global initialized with a struct whose field refers to another type in
+;; the same rec group.
+(module
+ (rec
+ (type $b (struct))
+ (type $a (struct (field (ref $b)))))
+ (global (ref $a)
+ (struct.new $a (struct.new_default $b)))
+)
diff --git a/tests/misc_testsuite/gc/i31ref-tables.wast b/tests/misc_testsuite/gc/i31ref-tables.wast
index 0430d7706f..cb864163f7 100644
--- a/tests/misc_testsuite/gc/i31ref-tables.wast
+++ b/tests/misc_testsuite/gc/i31ref-tables.wast
@@ -62,3 +62,8 @@
(assert_return (invoke "get" (i32.const 1)) (i32.const 123))
(assert_return (invoke "get" (i32.const 2)) (i32.const 456))
(assert_return (invoke "get" (i32.const 3)) (i32.const 789))
+
+;; i31ref tables with no initializer, a non-null initializer, and a null one.
+(module (table 1 i31ref))
+(module (table 1 i31ref (ref.i31 (i32.const 1))))
+(module (table 1 i31ref (ref.null i31)))
diff --git a/tests/misc_testsuite/gc/select-stack-map.wast b/tests/misc_testsuite/gc/select-stack-map.wast
new file mode 100644
index 0000000000..4215b05cf1
--- /dev/null
+++ b/tests/misc_testsuite/gc/select-stack-map.wast
@@ -0,0 +1,25 @@
+;;! gc = true
+
+;; The result of a `select` with a GC reference type must be in stack maps.
+(module
+ (type $pair (struct (field (mut i32))))
+
+ (import "wasmtime" "gc" (func $gc))
+
+ (func (export "test") (param $cond i32) (result i32)
+ ;; The select result stays on the operand stack, never in a local.
+ (select (result (ref null $pair))
+ (struct.new $pair (i32.const 111))
+ (ref.null $pair)
+ (local.get $cond))
+
+ ;; This call is a safepoint, and the select result is live across it. If
+ ;; it's missing from the stack map, the GC frees the struct and the next
+ ;; allocation may reuse its memory.
+ (call $gc)
+ (drop (struct.new $pair (i32.const 222)))
+
+ (struct.get $pair 0))
+)
+
+(assert_return (invoke "test" (i32.const 1)) (i32.const 111))
diff --git a/tests/misc_testsuite/module-interposition.wast b/tests/misc_testsuite/module-interposition.wast
new file mode 100644
index 0000000000..589960cebb
--- /dev/null
+++ b/tests/misc_testsuite/module-interposition.wast
@@ -0,0 +1,27 @@
+;; Each module imports `"instance" "export"` and is then registered under the
+;; same name, shadowing the previous one, so every call goes through the whole
+;; chain: 7 * 2^4 = 112.
+
+(module (func (export "export") (result i32) (i32.const 7)))
+(register "instance")
+
+(module
+ (import "instance" "export" (func (result i32)))
+ (func (export "export") (result i32) (i32.mul (call 0) (i32.const 2))))
+(register "instance")
+
+(module
+ (import "instance" "export" (func (result i32)))
+ (func (export "export") (result i32) (i32.mul (call 0) (i32.const 2))))
+(register "instance")
+
+(module
+ (import "instance" "export" (func (result i32)))
+ (func (export "export") (result i32) (i32.mul (call 0) (i32.const 2))))
+(register "instance")
+
+(module
+ (import "instance" "export" (func (result i32)))
+ (func (export "export") (result i32) (i32.mul (call 0) (i32.const 2))))
+
+(assert_return (invoke "export") (i32.const 112))
diff --git a/tests/misc_testsuite/stack-switching/gc.wast b/tests/misc_testsuite/stack-switching/gc.wast
index 5cbe6af868..288641a9f8 100644
--- a/tests/misc_testsuite/stack-switching/gc.wast
+++ b/tests/misc_testsuite/stack-switching/gc.wast
@@ -361,3 +361,33 @@
(assert_return
(invoke "gc-ref-in-resume-throw-payload")
(i32.const 202))
+
+;; Regression test for #13750: a suspended continuation held in a local must be
+;; traced by GCs that happen before it is resumed.
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (type $st (struct (field i32)))
+ (tag $t)
+
+ (func $suspend
+ (suspend $t))
+ (elem declare func $suspend)
+
+ (func (export "entry")
+ (local $continuation (ref null $ct))
+ (local $i i32)
+ (block $handler (result (ref $ct))
+ (resume $ct
+ (on $t $handler)
+ (cont.new $ct (ref.func $suspend)))
+ (return))
+ (local.set $continuation)
+ (loop $allocate
+ (drop (struct.new $st (i32.const 7)))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $allocate (i32.lt_u (local.get $i) (i32.const 20000))))
+ (resume $ct (local.get $continuation)))
+)
+
+(assert_return (invoke "entry"))
diff --git a/tests/misc_testsuite/stack-switching/host-call.wast b/tests/misc_testsuite/stack-switching/host-call.wast
new file mode 100644
index 0000000000..6de2e1d447
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/host-call.wast
@@ -0,0 +1,22 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; Wasm running inside a continuation can call a host function.
+(module
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+
+ (import "spectest" "print_i32" (func $print_i32 (param i32)))
+
+ (func $a (result i32)
+ (call $print_i32 (i32.const 122))
+ (i32.const 123))
+ (elem declare func $a)
+
+ (func (export "entry") (result i32)
+ (resume $ct (cont.new $ct (ref.func $a))))
+)
+
+(assert_return (invoke "entry") (i32.const 123))
diff --git a/tests/misc_testsuite/stack-switching/linking_tags3.wast b/tests/misc_testsuite/stack-switching/linking_tags3.wast
new file mode 100644
index 0000000000..36a8a8b488
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/linking_tags3.wast
@@ -0,0 +1,42 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; Two instances of the same module have distinct tags, so a handler installed
+;; by one instance doesn't handle a suspension to the other's tag.
+(module definition $other
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $tag)
+
+ (func (export "suspend")
+ (suspend $tag))
+
+ (func (export "resume") (param $f (ref $ct))
+ (block $handler (result (ref $ct))
+ (resume $ct (on $tag $handler) (local.get $f))
+ (unreachable))
+ (drop))
+)
+(module instance $other1 $other)
+(module instance $other2 $other)
+(register "other1" $other1)
+(register "other2" $other2)
+
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (import "other1" "suspend" (func $suspend))
+ (import "other2" "resume" (func $resume (param (ref $ct))))
+
+ (elem declare func $suspend)
+
+ (func (export "entry")
+ (call $resume (cont.new $ct (ref.func $suspend))))
+)
+
+;; Due to issue #253, we need to make sure that nothing happens afterwards in
+;; the test:
+(assert_suspension (invoke "entry") "unhandled")
diff --git a/tests/misc_testsuite/stack-switching/trap.wast b/tests/misc_testsuite/stack-switching/trap.wast
index 83b8a70e55..5623171d6b 100644
--- a/tests/misc_testsuite/stack-switching/trap.wast
+++ b/tests/misc_testsuite/stack-switching/trap.wast
@@ -14,4 +14,20 @@
(elem declare func $fn)
)
-(assert_trap (invoke "run_fn") "unreachable")
\ No newline at end of file
+(assert_trap (invoke "run_fn") "unreachable")
+
+;; Unbounded recursion inside a continuation exhausts the stack.
+(module
+ (type $ft (func (param i32)))
+ (type $ct (cont $ft))
+
+ (func (export "entry")
+ (resume $ct (i32.const 1_000_000_000) (cont.new $ct (ref.func $overflow))))
+
+ (func $overflow (param $i i32)
+ (if (i32.eqz (local.get $i)) (then (return)))
+ (call $overflow (i32.sub (local.get $i) (i32.const 1))))
+
+ (elem declare func $overflow)
+)
+(assert_exhaustion (invoke "entry") "call stack exhausted")
diff --git a/tests/misc_testsuite/threads/shared-memory-grow.wast b/tests/misc_testsuite/threads/shared-memory-grow.wast
new file mode 100644
index 0000000000..0f75aac1a6
--- /dev/null
+++ b/tests/misc_testsuite/threads/shared-memory-grow.wast
@@ -0,0 +1,119 @@
+;;! threads = true
+;;! hogs_memory = true
+
+;; Concurrent `memory.grow` on one shared memory from four threads.
+;; - Each thread grows by one page 250 times. The memory starts at 1 page with
+;; a maximum of 1000, so exactly one of the 1000 grows fails.
+;; - The old sizes a thread sees must increase.
+;; - No old size may be returned twice. Each grow marks byte `old size` of a
+;; bitmap at offset 0, and traps if that byte was already marked.
+;; - Failed grows are counted at offset 2000.
+(module $GrowMem
+ (memory (export "shared") 1 1000 shared)
+)
+
+(module definition $Grower
+ (import "GrowMem" "shared" (memory 1 1000 shared))
+ (func (export "run")
+ (local $i i32) (local $old i32) (local $last i32)
+ (local.set $last (i32.const -1))
+ (loop $loop
+ (local.set $old (memory.grow (i32.const 1)))
+ (if (i32.eq (local.get $old) (i32.const -1))
+ (then
+ (drop (i32.atomic.rmw.add (i32.const 2000) (i32.const 1))))
+ (else
+ ;; Old sizes from one thread must increase.
+ (if (i32.and
+ (i32.ne (local.get $last) (i32.const -1))
+ (i32.le_u (local.get $old) (local.get $last)))
+ (then unreachable))
+ (local.set $last (local.get $old))
+ ;; No other grow may have returned this size.
+ (if (i32.atomic.rmw8.xchg_u (local.get $old) (i32.const 1))
+ (then unreachable))))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $loop (i32.lt_u (local.get $i) (i32.const 250)))
+ )
+ )
+)
+
+(thread $T1 (shared (module $GrowMem))
+ (module instance $G $Grower)
+ (assert_return (invoke $G "run"))
+)
+(thread $T2 (shared (module $GrowMem))
+ (module instance $G $Grower)
+ (assert_return (invoke $G "run"))
+)
+(thread $T3 (shared (module $GrowMem))
+ (module instance $G $Grower)
+ (assert_return (invoke $G "run"))
+)
+(thread $T4 (shared (module $GrowMem))
+ (module instance $G $Grower)
+ (assert_return (invoke $G "run"))
+)
+(wait $T1)
+(wait $T2)
+(wait $T3)
+(wait $T4)
+
+(module $GrowCheck
+ (import "GrowMem" "shared" (memory 1 1000 shared))
+ (func (export "size") (result i32) (memory.size))
+ (func (export "failures") (result i32) (i32.atomic.load (i32.const 2000)))
+ ;; Returns how many of the old sizes 1..999 were seen.
+ (func (export "seen") (result i32)
+ (local $i i32) (local $n i32)
+ (local.set $i (i32.const 1))
+ (loop $loop
+ (local.set $n
+ (i32.add (local.get $n) (i32.load8_u (local.get $i))))
+ (local.set $i (i32.add (local.get $i) (i32.const 1)))
+ (br_if $loop (i32.lt_u (local.get $i) (i32.const 1000)))
+ )
+ (local.get $n)
+ )
+)
+(assert_return (invoke $GrowCheck "size") (i32.const 1000))
+(assert_return (invoke $GrowCheck "failures") (i32.const 1))
+(assert_return (invoke $GrowCheck "seen") (i32.const 999))
+
+;; One thread grows a shared memory while another repeatedly loads from the
+;; last four bytes of the current size. Every load must be in bounds and read
+;; zero. The grower sets a flag at offset 0 when it is done.
+(module $ProbeMem
+ (memory (export "shared") 1 1000 shared)
+)
+
+(thread $Grow (shared (module $ProbeMem))
+ (module
+ (import "ProbeMem" "shared" (memory 1 1000 shared))
+ (func (export "run")
+ (loop $loop
+ (br_if $loop (i32.ne (memory.grow (i32.const 1)) (i32.const -1)))
+ )
+ (i32.atomic.store (i32.const 0) (i32.const 1))
+ )
+ )
+ (assert_return (invoke "run"))
+)
+(thread $Probe (shared (module $ProbeMem))
+ (module
+ (import "ProbeMem" "shared" (memory 1 1000 shared))
+ (func (export "run")
+ (loop $loop
+ (if (i32.load
+ (i32.sub
+ (i32.mul (memory.size) (i32.const 0x10000))
+ (i32.const 4)))
+ (then unreachable))
+ (br_if $loop (i32.eqz (i32.atomic.load (i32.const 0))))
+ )
+ )
+ )
+ (assert_return (invoke "run"))
+)
+(wait $Grow)
+(wait $Probe)
diff --git a/tests/misc_testsuite/threads/shared-memory.wast b/tests/misc_testsuite/threads/shared-memory.wast
new file mode 100644
index 0000000000..2e01b6d4c8
--- /dev/null
+++ b/tests/misc_testsuite/threads/shared-memory.wast
@@ -0,0 +1,22 @@
+;;! threads = true
+
+;; Two instances of one module importing the same shared memory see a write
+;; made through a third instance.
+(module $Mem
+ (memory (export "shared") 1 5 shared)
+)
+
+(module $Writer
+ (import "Mem" "shared" (memory 1 5 shared))
+ (func (export "write") (i32.store (i32.const 0) (i32.const 42)))
+)
+(invoke $Writer "write")
+
+(module definition $Reader
+ (import "Mem" "shared" (memory 1 5 shared))
+ (func (export "first-word") (result i32) (i32.load (i32.const 0)))
+)
+(module instance $Reader1 $Reader)
+(module instance $Reader2 $Reader)
+(assert_return (invoke $Reader1 "first-word") (i32.const 42))
+(assert_return (invoke $Reader2 "first-word") (i32.const 42))
diff --git a/tests/misc_testsuite/threads/wait_notify.wast b/tests/misc_testsuite/threads/wait_notify.wast
index b80aa001b1..b8a50c18fd 100644
--- a/tests/misc_testsuite/threads/wait_notify.wast
+++ b/tests/misc_testsuite/threads/wait_notify.wast
@@ -41,3 +41,48 @@
(wait $T1)
(wait $T2)
+
+;; Two threads hand off to each other with wait and notify.
+;; - $T1 waits on offset 4 until $T2 stores 21 there and notifies.
+;; - $T1 then stores 42 at offset 0 and notifies.
+;; - $T2 waits on offset 0 until it sees the 42.
+;; If a wait starts after the store it waits for, it returns at once.
+(module $Mem2
+ (memory (export "shared") 1 1 shared)
+)
+
+(thread $T1 (shared (module $Mem2))
+ (module
+ (memory (import "Mem2" "shared") 1 1 shared)
+ (func (export "run") (result i32)
+ (drop (memory.atomic.wait32 (i32.const 4) (i32.const 0) (i64.const -1)))
+ (i32.atomic.store (i32.const 0) (i32.const 42))
+ (drop (memory.atomic.notify (i32.const 0) (i32.const -1)))
+ (i32.atomic.load (i32.const 0))
+ )
+ )
+ (assert_return (invoke "run") (i32.const 42))
+)
+
+(thread $T2 (shared (module $Mem2))
+ (module
+ (memory (import "Mem2" "shared") 1 1 shared)
+ (func (export "run") (result i32)
+ (i32.atomic.store (i32.const 4) (i32.const 21))
+ (drop (memory.atomic.notify (i32.const 4) (i32.const -1)))
+ (drop (memory.atomic.wait32 (i32.const 0) (i32.const 0) (i64.const -1)))
+ (i32.atomic.load (i32.const 0))
+ )
+ )
+ (assert_return (invoke "run") (i32.const 42))
+)
+
+(wait $T1)
+(wait $T2)
+
+(module $Check2
+ (memory (import "Mem2" "shared") 1 1 shared)
+ (func (export "words") (result i32 i32)
+ (i32.load (i32.const 0)) (i32.load (i32.const 4)))
+)
+(assert_return (invoke $Check2 "words") (i32.const 42) (i32.const 21))