Skip to content

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

[stack-switching] Tests (#13996)

Daniel Hillerström3a3f80887780
diff --git a/crates/wast/src/wast.rs b/crates/wast/src/wast.rs
index 6891956c4b..5e5fdeafc8 100644
--- a/crates/wast/src/wast.rs
+++ b/crates/wast/src/wast.rs
@@ -573,6 +573,14 @@ impl WastContext {
}
}
+ fn assert_suspension(&self, result: Outcome, _expected: &str) -> Result<()> {
+ match result {
+ Outcome::Ok(values) => bail!("expected suspension, got {values:?}"),
+ Outcome::Trap(err) if err.downcast_ref::<Trap>() == Some(&Trap::UnhandledTag) => Ok(()),
+ Outcome::Trap(err) => bail!("expected suspension, got {err:?}"),
+ }
+ }
+
/// Run a wast script from a byte buffer.
pub fn run_wast(&mut self, filename: &str, wast: &[u8]) -> Result<()> {
let wast = str::from_utf8(wast)?;
@@ -766,6 +774,14 @@ impl WastContext {
let result = self.perform_action(&action)?;
self.assert_exception(result)?;
}
+ AssertSuspension {
+ line: _,
+ action,
+ text,
+ } => {
+ let result = self.perform_action(&action)?;
+ self.assert_suspension(result, &text)?;
+ }
Thread {
name,
@@ -818,10 +834,6 @@ impl WastContext {
.unwrap()?;
}
- AssertSuspension { .. } => {
- bail!("unimplemented wast directive");
- }
-
AssertMalformedCustom {
file: _,
text: _,
diff --git a/tests/all/continuation_traps.rs b/tests/all/continuation_traps.rs
deleted file mode 100644
index b30517377f..0000000000
--- a/tests/all/continuation_traps.rs
+++ /dev/null
@@ -1,207 +0,0 @@
-use wasmtime::*;
-
-const MODULE: &str = r#"
- (module
- (import "host" "reenter" (func $host-reenter))
- (import "host" "trap" (func $host-trap))
-
- (type $ft (func))
- (type $ct (cont $ft))
-
- (global $stacks-left (mut i32) (i32.const 0))
- (global $frames-left (mut i32) (i32.const 0))
- (global $frames-per-stack (mut i32) (i32.const 0))
- (global $trap-in-host (mut i32) (i32.const 0))
- (global $g (export "g") (mut i32) (i32.const 0))
-
- (func $increment
- (global.set $g
- (i32.add (global.get $g) (i32.const 1))))
-
- (func $continue (export "continue")
- (if (i32.gt_u (global.get $frames-left) (i32.const 0))
- (then
- (global.set $frames-left
- (i32.sub (global.get $frames-left) (i32.const 1)))
- (call $host-reenter))
- (else
- (if (i32.gt_u (global.get $stacks-left) (i32.const 0))
- (then
- ;; Install a new child stack.
- (global.set $stacks-left
- (i32.sub (global.get $stacks-left) (i32.const 1)))
- (global.set $frames-left (global.get $frames-per-stack))
- (resume $ct (cont.new $ct (ref.func $continue))))
- (else
- ;; Call either the host provided trap, or the native Wasm trapping instruction `unreachable`.
- (if (global.get $trap-in-host)
- (then (call $host-trap))
- (else unreachable))))))
-
- ;; No frame, on any stack, may resume after the terminal trap.
- (call $increment))
-
- (func (export "run")
- (param $stacks i32)
- (param $frames-per-stack-arg i32)
- (param $trap-in-host-arg i32)
- (global.set $stacks-left (local.get $stacks))
- (global.set $frames-left (local.get $frames-per-stack-arg))
- (global.set $frames-per-stack (local.get $frames-per-stack-arg))
- (global.set $trap-in-host (local.get $trap-in-host-arg))
- (call $continue))
-
- (elem declare func $continue)
- )
-"#;
-
-fn increment_global<T>(caller: &mut Caller<'_, T>) -> Result<()> {
- let g = caller.get_export("g").unwrap().into_global().unwrap();
- let value = g.get(&mut *caller).unwrap_i32();
- g.set(caller, Val::I32(value + 1))
-}
-
-fn assert_expected_trap(error: &Error, trap_in_host: bool, stacks: i32, frames_per_stack: i32) {
- if trap_in_host {
- assert!(
- format!("{error:#}").contains("intentional host trap"),
- "unexpected error for stacks={stacks}, frames_per_stack={frames_per_stack}: {error:#}"
- );
- } else {
- assert_eq!(
- error.downcast_ref::<Trap>(),
- Some(&Trap::UnreachableCodeReached),
- "unexpected error for stacks={stacks}, frames_per_stack={frames_per_stack}: {error:#}"
- );
- }
-}
-
-fn host_trap() -> Result<()> {
- bail!("intentional host trap")
-}
-
-// TODO(dhil): Enable ASAN. ASAN produces a false positive here,
-// because ASAN thinks the thread is on the default stack. We need to
-// instrument the stack switching runtime to inform ASAN about the
-// switching of stacks.
-#[test]
-#[cfg_attr(any(asan, miri), ignore)]
-fn traps_cross_continuation_stacks_and_host_frames() -> Result<()> {
- let mut config = Config::new();
- config.wasm_stack_switching(true);
- let engine = Engine::new(&config)?;
- let module = Module::new(&engine, MODULE)?;
-
- // Exercise short continuation chains with different numbers of alternating
- // Wasm and host frames on each stack. Test both a Wasm trap and a host trap
- // as the youngest frame.
- for stacks in 0..=10 {
- for frames_per_stack in 0..=10 {
- for trap_in_host in [false, true] {
- let mut store = Store::new(&engine, ());
- let reenter = Func::wrap(&mut store, |mut caller: Caller<'_, ()>| -> Result<()> {
- let continue_ = caller
- .get_export("continue")
- .unwrap()
- .into_func()
- .unwrap()
- .typed::<(), ()>(&caller)?;
- continue_.call(&mut caller, ())?;
-
- // This must not execute: the recursive call always traps.
- increment_global(&mut caller)
- });
- let host_trap = Func::wrap(&mut store, host_trap);
- let instance =
- Instance::new(&mut store, &module, &[reenter.into(), host_trap.into()])?;
-
- let run = instance.get_typed_func::<(i32, i32, i32), ()>(&mut store, "run")?;
- let error = run
- .call(
- &mut store,
- (stacks, frames_per_stack, i32::from(trap_in_host)),
- )
- .unwrap_err();
-
- assert_expected_trap(&error, trap_in_host, stacks, frames_per_stack);
-
- let g = instance.get_global(&mut store, "g").unwrap();
- assert_eq!(g.get(&mut store).unwrap_i32(), 0);
- }
- }
- }
-
- Ok(())
-}
-
-#[derive(Default)]
-struct CatchState {
- error: Option<Error>,
-}
-
-// TODO(dhil): Enable ASAN.
-#[test]
-#[cfg_attr(any(asan, miri), ignore)]
-fn parent_frames_resume_after_host_catches_trap() -> Result<()> {
- let mut config = Config::new();
- config.wasm_stack_switching(true);
- let engine = Engine::new(&config)?;
- let module = Module::new(&engine, MODULE)?;
-
- for stacks in 0..=10 {
- // At least one host re-entry is needed to catch the terminal trap.
- for frames_per_stack in 1..=10 {
- for trap_in_host in [false, true] {
- let mut store = Store::new(&engine, CatchState::default());
- let reenter = Func::wrap(
- &mut store,
- |mut caller: Caller<'_, CatchState>| -> Result<()> {
- let continue_ = caller
- .get_export("continue")
- .unwrap()
- .into_func()
- .unwrap()
- .typed::<(), ()>(&caller)?;
-
- if let Err(error) = continue_.call(&mut caller, ()) {
- let previous = caller.data_mut().error.replace(error);
- assert!(previous.is_none(), "more than one host frame caught a trap");
- }
-
- increment_global(&mut caller)
- },
- );
- let host_trap = Func::wrap(&mut store, host_trap);
- let instance =
- Instance::new(&mut store, &module, &[reenter.into(), host_trap.into()])?;
-
- let run = instance.get_typed_func::<(i32, i32, i32), ()>(&mut store, "run")?;
- run.call(
- &mut store,
- (stacks, frames_per_stack, i32::from(trap_in_host)),
- )?;
-
- let error = store
- .data()
- .error
- .as_ref()
- .expect("a host frame should have caught the terminal trap");
- assert_expected_trap(error, trap_in_host, stacks, frames_per_stack);
-
- // `stacks` counts child stacks, so there are `stacks + 1`
- // stacks in total. Each has `frames_per_stack` host frames and
- // `frames_per_stack + 1` Wasm frames. All frames except the
- // terminal trapping Wasm frame return and increment `g`.
- let expected = (stacks + 1) * (2 * frames_per_stack + 1) - 1;
- let g = instance.get_global(&mut store, "g").unwrap();
- assert_eq!(
- g.get(&mut store).unwrap_i32(),
- expected,
- "wrong count for stacks={stacks}, frames_per_stack={frames_per_stack}"
- );
- }
- }
- }
-
- Ok(())
-}
diff --git a/tests/all/main.rs b/tests/all/main.rs
index 61fc209e60..c6d29c7ad5 100644
--- a/tests/all/main.rs
+++ b/tests/all/main.rs
@@ -8,8 +8,6 @@ mod call_hook;
mod cli_tests;
mod compile_time_builtins;
mod component_model;
-#[cfg(all(feature = "stack-switching", unix, target_arch = "x86_64"))]
-mod continuation_traps;
mod coredump;
mod custom_code_memory;
mod debug;
@@ -53,6 +51,12 @@ mod pulley;
mod relocs;
mod stack_creator;
mod stack_overflow;
+#[cfg(all(
+ feature = "stack-switching",
+ target_os = "linux",
+ target_arch = "x86_64"
+))]
+mod stack_switching;
mod store;
mod structs;
mod table;
diff --git a/tests/all/stack_switching.rs b/tests/all/stack_switching.rs
new file mode 100644
index 0000000000..dfc33714f8
--- /dev/null
+++ b/tests/all/stack_switching.rs
@@ -0,0 +1,1391 @@
+use wasmtime::*;
+
+mod test_utils {
+ use std::any::*;
+ use std::panic::AssertUnwindSafe;
+ use wasmtime::{bail, *};
+
+ pub struct Runner {
+ pub engine: Engine,
+ pub store: Store<()>,
+ }
+
+ impl Runner {
+ pub fn new() -> Runner {
+ let mut config = Config::default();
+ config.wasm_function_references(true);
+ config.wasm_exceptions(true);
+ config.wasm_stack_switching(true);
+ // Required in order to use recursive types.
+ config.wasm_gc(true);
+
+ let engine = Engine::new(&config).unwrap();
+
+ let store = Store::<()>::new(&engine, ());
+
+ Runner { engine, store }
+ }
+
+ /// Uses this `Runner` to run the module defined in `wat`, satisfying
+ /// its imports using `imports`. The module must export a function
+ /// `entry`, taking no parameters and returning `Results`.
+ pub fn run_test<Results: WasmResults>(
+ mut self,
+ wat: &str,
+ imports: &[Extern],
+ ) -> Result<Results> {
+ let module = Module::new(&self.engine, wat)?;
+
+ let instance = Instance::new(&mut self.store, &module, imports)?;
+ let entry = instance.get_typed_func::<(), Results>(&mut self.store, "entry")?;
+
+ entry.call(&mut self.store, ())
+ }
+
+ /// Uses this `Runner` to run the module defined in `wat`, satisfying
+ /// its imports using `imports`. The module must export a function
+ /// `entry`, taking no parameters and without return values. Execution
+ /// of `entry` is expected to cause a panic (and that this is panic is
+ /// not handled by wasmtime previously).
+ /// Returns the `Error` payload.
+ pub fn run_test_expect_panic(
+ mut self,
+ wat: &str,
+ imports: &[Extern],
+ ) -> Box<dyn Any + Send + 'static> {
+ let module = Module::new(&self.engine, wat).unwrap();
+
+ let instance = Instance::new(&mut self.store, &module, imports).unwrap();
+ let entry = instance.get_func(&mut self.store, "entry").unwrap();
+
+ std::panic::catch_unwind(AssertUnwindSafe(|| {
+ drop(entry.call(&mut self.store, &[], &mut []))
+ }))
+ .unwrap_err()
+ }
+ }
+
+ /// 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
+ /// turn increments the result before returning it as the overall result.
+ pub fn make_i32_inc_via_export_host_func(
+ runner: &mut Runner,
+ export_func: &'static str,
+ ) -> Func {
+ Func::new(
+ &mut runner.store,
+ FuncType::new(&runner.engine, vec![ValType::I32], vec![ValType::I32]),
+ |mut caller, args: &[Val], results: &mut [Val]| {
+ let export = caller
+ .get_export(export_func)
+ .ok_or_else(|| wasmtime::format_err!("could not get export"))?;
+ let func = export
+ .into_func()
+ .ok_or_else(|| wasmtime::format_err!("export is not a Func"))?;
+ let func_typed = func.typed::<i32, i32>(caller.as_context())?;
+ let arg = args[0].unwrap_i32();
+ let res = func_typed.call(caller.as_context_mut(), arg + 1)?;
+ results[0] = Val::I32(res + 1);
+ Ok(())
+ },
+ )
+ }
+
+ pub fn make_panicking_host_func(store: &mut Store<()>, msg: &'static str) -> Func {
+ Func::wrap(store, move || -> () { std::panic::panic_any(msg) })
+ }
+}
+
+mod wasi {
+ use wasmtime::{Config, Engine, Linker, Module, Result, Store};
+ use wasmtime_wasi::WasiCtxBuilder;
+ use wasmtime_wasi::p1::{self, WasiP1Ctx};
+
+ fn run_wasi_test(wat: &'static str) -> Result<i32> {
+ // Construct the wasm engine with async support disabled.
+ let mut config = Config::new();
+ config
+ .wasm_exceptions(true)
+ .wasm_function_references(true)
+ .wasm_stack_switching(true);
+ let engine = Engine::new(&config)?;
+
+ // Add the WASI preview1 API to the linker (will be implemented in terms of
+ // the preview2 API)
+ let mut linker: Linker<WasiP1Ctx> = Linker::new(&engine);
+ p1::add_to_linker_sync(&mut linker, |t| t)?;
+
+ // Add capabilities (e.g. filesystem access) to the WASI preview2 context here.
+ let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().build_p1();
+
+ let mut store: Store<WasiP1Ctx> = Store::new(&engine, wasi_ctx);
+
+ // Instantiate our wasm module.
+ let module = Module::new(&engine, wat)?;
+ let func = linker
+ .module(&mut store, "", &module)?
+ .get_default(&mut store, "")?
+ .typed::<(), i32>(&store)?;
+
+ // Invoke the WASI program default function.
+ func.call(&mut store, ())
+ }
+
+ async fn run_wasi_test_async(wat: &'static str) -> Result<i32> {
+ // Construct the wasm engine with async support enabled.
+ let mut config = Config::new();
+ config
+ .wasm_exceptions(true)
+ .wasm_function_references(true)
+ .wasm_stack_switching(true);
+ let engine = Engine::new(&config)?;
+
+ // Add the WASI preview1 API to the linker (will be implemented in terms of
+ // the preview2 API)
+ let mut linker: Linker<WasiP1Ctx> = Linker::new(&engine);
+ p1::add_to_linker_async(&mut linker, |t| t)?;
+
+ // Add capabilities (e.g. filesystem access) to the WASI preview2 context here.
+ let wasi_ctx = WasiCtxBuilder::new().inherit_stdio().build_p1();
+
+ let mut store: Store<WasiP1Ctx> = Store::new(&engine, wasi_ctx);
+
+ // Instantiate our wasm module.
+ let module = Module::new(&engine, wat)?;
+ let func = linker
+ .module_async(&mut store, "", &module)
+ .await?
+ .get_default(&mut store, "")?
+ .typed::<(), i32>(&store)?;
+
+ // Invoke the WASI program default function.
+ func.call_async(&mut store, ()).await
+ }
+
+ static WRITE_SOMETHING_WAT: &str = r#"
+(module
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+ (import "wasi_snapshot_preview1" "fd_write"
+ (func $print (param $fd i32)
+ (param $iovec i32)
+ (param $len i32)
+ (param $written i32) (result i32)))
+ (memory 1)
+ (export "memory" (memory 0))
+
+ ;; 9 is the offset to write to
+ (data (i32.const 9) "something\n")
+
+ (func $f (result i32)
+ (i32.const 0) ;; offset
+ (i32.const 9) ;; value start of the string
+ (i32.store)
+
+ (i32.const 4) ;; offset
+ (i32.const 10) ;; value, the length of the string
+ (i32.store offset=0 align=2) ;; size_buf_len
+
+ (i32.const 1) ;; 1 for stdout
+ (i32.const 0) ;; 0 as we stored the beginning of __wasi_ciovec_t
+ (i32.const 1) ;;
+ (i32.const 20) ;; nwritten
+ (call $print)
+ )
+ (elem declare func $f)
+
+ (func (export "_start") (result i32)
+ (ref.func $f)
+ (cont.new $ct)
+ (resume $ct)
+ )
+)"#;
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ fn write_something_test() -> Result<()> {
+ assert_eq!(run_wasi_test(WRITE_SOMETHING_WAT)?, 0);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[tokio::test]
+ async fn write_something_test_async() -> Result<()> {
+ assert_eq!(run_wasi_test_async(WRITE_SOMETHING_WAT).await?, 0);
+ Ok(())
+ }
+
+ static SCHED_YIELD_WAT: &'static str = r#"
+(module
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+ (import "wasi_snapshot_preview1" "sched_yield"
+ (func $sched_yield (result i32)))
+ (memory 1)
+ (export "memory" (memory 0))
+
+ (func $g (result i32)
+ (call $sched_yield))
+ (elem declare func $g)
+
+ (func (export "_start") (result i32)
+ (cont.new $ct (ref.func $g))
+ (resume $ct)
+ )
+)"#;
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ fn sched_yield_test() -> Result<()> {
+ assert_eq!(run_wasi_test(SCHED_YIELD_WAT)?, 0);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[tokio::test]
+ async fn sched_yield_test_async() -> Result<()> {
+ assert_eq!(run_wasi_test_async(SCHED_YIELD_WAT).await?, 0);
+ Ok(())
+ }
+}
+
+/// Test that two distinct instantiations of the same module yield
+/// different control tag identities.
+#[cfg_attr(any(asan, 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(())
+}
+
+/// Tests interaction with host functions. Note that the interaction with host
+/// functions and traps is covered by the module `traps` further down.
+mod host {
+ use super::test_utils::*;
+ use wasmtime::*;
+
+ const CONTINUATION_TRAP_MODULE: &str = r#"
+ (module
+ (import "host" "reenter" (func $host-reenter))
+ (import "host" "trap" (func $host-trap))
+
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (global $stacks-left (mut i32) (i32.const 0))
+ (global $frames-left (mut i32) (i32.const 0))
+ (global $frames-per-stack (mut i32) (i32.const 0))
+ (global $trap-in-host (mut i32) (i32.const 0))
+ (global $g (export "g") (mut i32) (i32.const 0))
+
+ (func $increment
+ (global.set $g
+ (i32.add (global.get $g) (i32.const 1))))
+
+ (func $continue (export "continue")
+ (if (i32.gt_u (global.get $frames-left) (i32.const 0))
+ (then
+ (global.set $frames-left
+ (i32.sub (global.get $frames-left) (i32.const 1)))
+ (call $host-reenter))
+ (else
+ (if (i32.gt_u (global.get $stacks-left) (i32.const 0))
+ (then
+ ;; Install a new child stack.
+ (global.set $stacks-left
+ (i32.sub (global.get $stacks-left) (i32.const 1)))
+ (global.set $frames-left (global.get $frames-per-stack))
+ (resume $ct (cont.new $ct (ref.func $continue))))
+ (else
+ ;; Call either the host provided trap, or the native Wasm trapping instruction `unreachable`.
+ (if (global.get $trap-in-host)
+ (then (call $host-trap))
+ (else unreachable))))))
+
+ ;; No frame, on any stack, may resume after the terminal trap.
+ (call $increment))
+
+ (func (export "run")
+ (param $stacks i32)
+ (param $frames-per-stack-arg i32)
+ (param $trap-in-host-arg i32)
+ (global.set $stacks-left (local.get $stacks))
+ (global.set $frames-left (local.get $frames-per-stack-arg))
+ (global.set $frames-per-stack (local.get $frames-per-stack-arg))
+ (global.set $trap-in-host (local.get $trap-in-host-arg))
+ (call $continue))
+
+ (elem declare func $continue)
+ )
+"#;
+
+ fn increment_global<T>(caller: &mut Caller<'_, T>) -> Result<()> {
+ let g = caller.get_export("g").unwrap().into_global().unwrap();
+ let value = g.get(&mut *caller).unwrap_i32();
+ g.set(caller, Val::I32(value + 1))
+ }
+
+ fn assert_expected_trap(error: &Error, trap_in_host: bool, stacks: i32, frames_per_stack: i32) {
+ if trap_in_host {
+ assert!(
+ format!("{error:#}").contains("intentional host trap"),
+ "unexpected error for stacks={stacks}, frames_per_stack={frames_per_stack}: {error:#}"
+ );
+ } else {
+ assert_eq!(
+ error.downcast_ref::<Trap>(),
+ Some(&Trap::UnreachableCodeReached),
+ "unexpected error for stacks={stacks}, frames_per_stack={frames_per_stack}: {error:#}"
+ );
+ }
+ }
+
+ fn host_trap() -> Result<()> {
+ bail!("intentional host trap")
+ }
+
+ // TODO(dhil): Enable ASAN. ASAN produces a false positive here,
+ // because ASAN thinks the thread is on the default stack. We need to
+ // instrument the stack switching runtime to inform ASAN about the
+ // switching of stacks.
+ #[test]
+ #[cfg_attr(any(asan, miri), ignore)]
+ fn traps_cross_continuation_stacks_and_host_frames() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_stack_switching(true);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, CONTINUATION_TRAP_MODULE)?;
+
+ // Exercise short continuation chains with different numbers of alternating
+ // Wasm and host frames on each stack. Test both a Wasm trap and a host trap
+ // as the youngest frame.
+ for stacks in 0..=10 {
+ for frames_per_stack in 0..=10 {
+ for trap_in_host in [false, true] {
+ let mut store = Store::new(&engine, ());
+ let reenter =
+ Func::wrap(&mut store, |mut caller: Caller<'_, ()>| -> Result<()> {
+ let continue_ = caller
+ .get_export("continue")
+ .unwrap()
+ .into_func()
+ .unwrap()
+ .typed::<(), ()>(&caller)?;
+ continue_.call(&mut caller, ())?;
+
+ // This must not execute: the recursive call always traps.
+ increment_global(&mut caller)
+ });
+ let host_trap = Func::wrap(&mut store, host_trap);
+ let instance =
+ Instance::new(&mut store, &module, &[reenter.into(), host_trap.into()])?;
+
+ let run = instance.get_typed_func::<(i32, i32, i32), ()>(&mut store, "run")?;
+ let error = run
+ .call(
+ &mut store,
+ (stacks, frames_per_stack, i32::from(trap_in_host)),
+ )
+ .unwrap_err();
+
+ assert_expected_trap(&error, trap_in_host, stacks, frames_per_stack);
+
+ let g = instance.get_global(&mut store, "g").unwrap();
+ assert_eq!(g.get(&mut store).unwrap_i32(), 0);
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ #[derive(Default)]
+ struct CatchState {
+ error: Option<Error>,
+ }
+
+ // TODO(dhil): Enable ASAN.
+ #[test]
+ #[cfg_attr(any(asan, miri), ignore)]
+ fn parent_frames_resume_after_host_catches_trap() -> Result<()> {
+ let mut config = Config::new();
+ config.wasm_stack_switching(true);
+ let engine = Engine::new(&config)?;
+ let module = Module::new(&engine, CONTINUATION_TRAP_MODULE)?;
+
+ for stacks in 0..=10 {
+ // At least one host re-entry is needed to catch the terminal trap.
+ for frames_per_stack in 1..=10 {
+ for trap_in_host in [false, true] {
+ let mut store = Store::new(&engine, CatchState::default());
+ let reenter = Func::wrap(
+ &mut store,
+ |mut caller: Caller<'_, CatchState>| -> Result<()> {
+ let continue_ = caller
+ .get_export("continue")
+ .unwrap()
+ .into_func()
+ .unwrap()
+ .typed::<(), ()>(&caller)?;
+
+ if let Err(error) = continue_.call(&mut caller, ()) {
+ let previous = caller.data_mut().error.replace(error);
+ assert!(
+ previous.is_none(),
+ "more than one host frame caught a trap"
+ );
+ }
+
+ increment_global(&mut caller)
+ },
+ );
+ let host_trap = Func::wrap(&mut store, host_trap);
+ let instance =
+ Instance::new(&mut store, &module, &[reenter.into(), host_trap.into()])?;
+
+ let run = instance.get_typed_func::<(i32, i32, i32), ()>(&mut store, "run")?;
+ run.call(
+ &mut store,
+ (stacks, frames_per_stack, i32::from(trap_in_host)),
+ )?;
+
+ let error = store
+ .data()
+ .error
+ .as_ref()
+ .expect("a host frame should have caught the terminal trap");
+ assert_expected_trap(error, trap_in_host, stacks, frames_per_stack);
+
+ // `stacks` counts child stacks, so there are `stacks + 1`
+ // stacks in total. Each has `frames_per_stack` host frames and
+ // `frames_per_stack + 1` Wasm frames. All frames except the
+ // terminal trapping Wasm frame return and increment `g`.
+ let expected = (stacks + 1) * (2 * frames_per_stack + 1) - 1;
+ let g = instance.get_global(&mut store, "g").unwrap();
+ assert_eq!(
+ g.get(&mut store).unwrap_i32(),
+ expected,
+ "wrong count for stacks={stacks}, frames_per_stack={frames_per_stack}"
+ );
+ }
+ }
+ }
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, 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(any(asan, miri), ignore)]
+ #[test]
+ /// We re-enter wasm from a host function and execute a continuation.
+ /// Call chain:
+ /// $entry -call-> $a -call-> $host_func_a -call-> $b -resume-> $c
+ fn re_enter_wasm_ok1() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+
+ (import "" "" (func $host_func_a (param i32) (result i32)))
+
+
+ (func $a (export "a") (param $x i32) (result i32)
+ (call $host_func_a (local.get $x))
+ )
+
+ (func $b (export "b") (param $x i32) (result i32)
+ (resume $ct (local.get $x) (cont.new $ct (ref.func $c)))
+ )
+
+ (func $c (export "c") (param $x i32) (result i32)
+ (return (i32.add (local.get $x) (i32.const 1)))
+ )
+
+
+ (func $entry (export "entry") (result i32)
+ (call $a (i32.const 120))
+ )
+ )
+ "#;
+
+ let mut runner = Runner::new();
+
+ let host_func_a = make_i32_inc_via_export_host_func(&mut runner, "b");
+
+ let result = runner.run_test::<i32>(wat, &[host_func_a.into()]).unwrap();
+ assert_eq!(result, 123);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Similar to `re_enter_wasm_ok2, but we run a continuation before the host call.
+ /// Call chain:
+ /// $entry -call-> $a -call-> $host_func_a -call-> $b -resume-> $c
+ fn re_enter_wasm_ok2() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+
+ (import "" "" (func $host_func_a (param i32) (result i32)))
+
+
+ (func $a (export "a") (param $x i32) (result i32)
+ ;; Running continuation before calling into host is fine
+ (resume $ct (local.get $x) (cont.new $ct (ref.func $c)))
+ (drop)
+
+ (call $host_func_a (local.get $x))
+ )
+
+ (func $b (export "b") (param $x i32) (result i32)
+ (resume $ct (local.get $x) (cont.new $ct (ref.func $c)))
+ )
+
+ (func $c (export "c") (param $x i32) (result i32)
+ (return (i32.add (local.get $x) (i32.const 1)))
+ )
+
+
+ (func $entry (export "entry") (result i32)
+ (call $a (i32.const 120))
+ )
+ )
+ "#;
+
+ let mut runner = Runner::new();
+
+ let host_func_a = make_i32_inc_via_export_host_func(&mut runner, "b");
+
+ let result = runner.run_test::<i32>(wat, &[host_func_a.into()]).unwrap();
+ assert_eq!(result, 123);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// We re-enter Wasm from a host function while already running on a
+ /// continuation stack.
+ /// Call chain:
+ /// $entry -resume-> $a -call-> $host_func_a -call-> $b
+ fn re_enter_wasm_from_continuation() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+
+ (import "" "" (func $host_func_a (param i32) (result i32)))
+
+
+ (func $a (export "a") (param $x i32) (result i32)
+ (call $host_func_a (local.get $x))
+ )
+
+
+ (func $b (export "b") (param $x i32) (result i32)
+ (return (i32.add (local.get $x) (i32.const 1)))
+ )
+
+
+ (func $entry (export "entry") (result i32)
+ (resume $ct (i32.const 120) (cont.new $ct (ref.func $a)))
+ )
+ )
+ "#;
+ let mut runner = Runner::new();
+
+ let host_func_a = make_i32_inc_via_export_host_func(&mut runner, "b");
+
+ let result = runner.run_test::<i32>(&wat, &[host_func_a.into()])?;
+ assert_eq!(result, 123);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// After crossing from the host back into wasm, we suspend to a tag that is
+ /// handled by the surrounding function (i.e., without needing to cross the
+ /// host frame to reach the handler).
+ /// Call chain:
+ /// $entry -resume-> $a -call-> $host_func_a -call-> $b -resume-> $c
+ fn call_host_from_continuation_nested_suspend_ok() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+ (tag $t (result i32))
+
+ (import "" "" (func $host_func_a (param i32) (result i32)))
+
+
+ (func $a (export "a") (param $x i32) (result i32)
+ (call $host_func_a (local.get $x))
+ )
+
+
+ (func $b (export "b") (param $x i32) (result i32)
+ (block $h (result (ref $ct))
+ (resume $ct (on $t $h) (local.get $x) (cont.new $ct (ref.func $c)))
+ (unreachable)
+ )
+ (drop)
+ ;; note that we do not run the continuation to completion
+ (i32.add (local.get $x) (i32.const 1))
+ )
+
+ (func $c (export "c") (param $x i32) (result i32)
+ (suspend $t)
+ )
+
+
+ (func $entry (export "entry") (result i32)
+ (resume $ct (i32.const 120) (cont.new $ct (ref.func $a)))
+ )
+ )
+ "#;
+
+ let mut runner = Runner::new();
+
+ let host_func_a = make_i32_inc_via_export_host_func(&mut runner, "b");
+
+ let result = runner.run_test::<i32>(&wat, &[host_func_a.into()])?;
+ assert_eq!(result, 123);
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Similar to `call_host_from_continuation_nested_suspend_ok`. However,
+ /// we suspend to a tag that is only handled if we were to cross a host function
+ /// boundary.
+ /// Host calls act as barriers for suspensions, so the inner `resume`
+ /// observes an unhandled tag.
+ ///
+ /// Call chain:
+ /// $entry -resume-> $a -call-> $host_func_a -call-> $b -resume-> $c
+ fn call_host_from_continuation_nested_suspend_unhandled() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+ (tag $t (result i32))
+
+ (import "" "" (func $host_func_a (param i32) (result i32)))
+
+
+ (func $a (export "a") (param $x i32) (result i32)
+ (call $host_func_a (local.get $x))
+ )
+
+
+ (func $b (export "b") (param $x i32) (result i32)
+ (resume $ct (local.get $x) (cont.new $ct (ref.func $c)))
+ )
+
+ (func $c (export "c") (param $x i32) (result i32)
+ (suspend $t)
+ )
+
+
+ (func $entry (export "entry")
+ (block $h (result (ref $ct))
+ (return
+ (resume $ct
+ (on $t $h)
+ (i32.const 123)
+ (cont.new $ct (ref.func $a))))
+ )
+ (drop)
+ )
+ )
+ "#;
+
+ let mut runner = Runner::new();
+
+ let host_func_a = make_i32_inc_via_export_host_func(&mut runner, "b");
+
+ let error = runner
+ .run_test::<()>(&wat, &[host_func_a.into()])
+ .unwrap_err();
+ assert_eq!(
+ error.downcast_ref::<Trap>(),
+ Some(&Trap::UnhandledTag),
+ "unexpected error: {error:#}"
+ );
+ Ok(())
+ }
+}
+
+mod traps {
+ use super::test_utils::*;
+ use wasmtime::*;
+
+ /// Runs the module given as `wat`. We expect execution to cause the
+ /// `expected_trap` and a backtrace containing exactly the function names
+ /// given by `expected_backtrace`.
+ fn run_test_expect_trap_backtrace(wat: &str, expected_trap: Trap, expected_backtrace: &[&str]) {
+ let runner = Runner::new();
+ let result = runner.run_test::<()>(wat, &[]);
+
+ let err = result.expect_err("Was expecting wasm execution to yield error");
+
+ assert!(err.root_cause().is::<Trap>());
+ assert_eq!(*err.downcast_ref::<Trap>().unwrap(), expected_trap);
+
+ let trace = err.downcast_ref::<WasmBacktrace>().unwrap();
+
+ let actual_func_name_it = trace
+ .frames()
+ .iter()
+ .map(|frame| {
+ frame
+ .func_name()
+ .expect("Expecting all functions in actual backtrace to have names")
+ })
+ .rev();
+
+ let expected_func_name_it = expected_backtrace.iter().copied();
+
+ assert!(actual_func_name_it.eq(expected_func_name_it));
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces if we trap deep inside multiple continuations.
+ /// Call chain:
+ /// $entry -call-> $a -resume-> $b -call-> $c -resume-> $d -call-> $e -resume-> $f
+ /// Here, $f traps.
+ fn trap_in_continuation_nested() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e")
+ (resume $ct (cont.new $ct (ref.func $f)))
+ )
+
+ (func $f (export "f")
+ (unreachable)
+ )
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "a", "b", "c", "d", "e", "f"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces if we trap after returning from one
+ /// continuation to its parent.
+ fn trap_in_continuation_back_to_parent() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ (unreachable)
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e"))
+
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "a", "b", "c"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces if we trap after returning from
+ /// several continuations back to the main stack.
+ fn trap_in_continuation_back_to_main() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ (unreachable)
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e"))
+
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(wat, Trap::UnreachableCodeReached, &["entry", "a"]);
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces after suspending a continuation.
+ fn trap_in_continuation_suspend() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (tag $t)
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ (unreachable)
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (block $handler (result (ref $ct))
+ (resume $ct (on $t $handler) (cont.new $ct (ref.func $d)))
+ (return)
+ )
+ (unreachable)
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e")
+ (suspend $t)
+ )
+
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "a", "b", "c"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces after suspending a continuation and
+ /// then resuming it from a different stack frame.
+ fn trap_in_continuation_suspend_resume() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (tag $t)
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ )
+
+ (func $b (export "b")
+ (resume $ct (call $c))
+ )
+
+ (func $c (export "c") (result (ref $ct))
+ (block $handler (result (ref $ct))
+ (resume $ct (on $t $handler) (cont.new $ct (ref.func $d)))
+
+ ;; We never want to get here, but also don't want to use
+ ;; (unreachable), which is the trap we deliberately use in
+ ;; this test. Instead, we call a null function ref here,
+ ;; which is guaranteed to trap.
+ (call_ref $ft (ref.null $ft))
+
+ (return (cont.new $ct (ref.func $d)))
+ )
+ ;; implicitly returning the continuation here
+ )
+
+ (func $d (export "d")
+ (call $e)
+ (unreachable)
+ )
+
+ (func $e (export "e")
+ (suspend $t)
+ )
+
+ )
+ "#;
+
+ // Note that c does not appear in the stack trace:
+ // In $b, we resume the suspended computation, which started in $d,
+ // suspended in $e, and traps in $d
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "a", "b", "d"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces after suspending a continuation
+ /// where we need to forward to an outer handler.
+ fn trap_in_continuation_forward() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $t)
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (block $handler (result (ref $ct))
+ (resume $ct (on $t $handler) (cont.new $ct (ref.func $b)))
+ ;; We don't actually want to get here
+ (return)
+ )
+ (unreachable)
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e")
+ (suspend $t)
+ )
+
+ )
+ "#;
+
+ // Note that c does not appear in the stack trace:
+ // In $b, we resume the suspended computation, which started in $d,
+ // suspended in $e, and traps in $d
+ run_test_expect_trap_backtrace(wat, Trap::UnreachableCodeReached, &["entry", "a"]);
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces after suspending a continuation
+ /// where we need to forward to an outer handler. We then resume the
+ /// continuation from within another continuation.
+ fn trap_in_continuation_forward_resume() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $t)
+
+ (global $k (mut (ref null $ct)) (ref.null $ct))
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ )
+
+ (func $b (export "b")
+ (block $handler (result (ref $ct))
+ (resume $ct (on $t $handler) (cont.new $ct (ref.func $c)))
+ ;; We don't actually want to get here
+ (return)
+ )
+ (global.set $k)
+ ;; $f will resume $k
+ (resume $ct (cont.new $ct (ref.func $f)))
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e")
+ (suspend $t)
+ (unreachable)
+ )
+
+ (func $f (export "f")
+ (resume $ct (global.get $k))
+ )
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "a", "b", "f", "c", "d", "e"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct backtraces after switch.
+ /// We first create the a stack with the following shape:
+ /// entry -> a -> b, then switch, leading to
+ /// entry -> c -> d, at which point we resume the a -> b continuation:
+ /// entry -> c -> d -> a -> b
+ /// We trap at that point.
+ fn trap_switch_and_resume() -> Result<()> {
+ let wat = r#"
+ (module
+ (rec
+ (type $ft0 (func (param (ref null $ct0))))
+ (type $ct0 (cont $ft0)))
+
+ (type $ft1 (func))
+ (type $ct1 (cont $ft1))
+
+ (tag $t)
+
+ (func $a (type $ft1)
+ (cont.new $ct1 (ref.func $b))
+ (resume $ct1)
+ )
+ (elem declare func $a)
+
+ (func $b (type $ft1)
+ (cont.new $ct0 (ref.func $c))
+ (switch $ct0 $t)
+
+ ;; we want a backtrace here
+ (unreachable)
+ )
+ (elem declare func $b)
+
+ (func $c (type $ft0)
+ (local.get 0)
+ (cont.new $ct0 (ref.func $d))
+ (resume $ct0)
+ )
+ (elem declare func $c)
+
+ (func $d (type $ft0)
+ (block $handler (result (ref $ct1))
+ (ref.null $ct0) ;; passed as payload
+ (local.get 0) ;; resumed
+ (resume $ct0 (on $t $handler))
+ (unreachable) ;; f1 will suspend after the switch
+ )
+ (resume $ct1)
+ )
+ (elem declare func $d)
+
+ (func $entry (export "entry")
+ (cont.new $ct1 (ref.func $a))
+ (resume $ct1 (on $t switch))
+ )
+ )
+ "#;
+
+ run_test_expect_trap_backtrace(
+ wat,
+ Trap::UnreachableCodeReached,
+ &["entry", "c", "d", "a", "b"],
+ );
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, miri), ignore)]
+ #[test]
+ /// Tests that we get correct panic payloads if we panic deep inside multiple
+ /// continuations. Note that wasmtime does not create its own backtraces for panics.
+ fn panic_in_continuation() -> Result<()> {
+ let wat = r#"
+ (module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (import "" "" (func $f))
+
+ (func $entry (export "entry")
+ (call $a)
+ )
+
+ (func $a (export "a")
+ (resume $ct (cont.new $ct (ref.func $b)))
+ )
+
+ (func $b (export "b")
+ (call $c)
+ )
+
+ (func $c (export "c")
+ (resume $ct (cont.new $ct (ref.func $d)))
+ )
+
+ (func $d (export "d")
+ (call $e)
+ )
+
+ (func $e (export "e")
+ (call $f)
+ )
+
+ )
+ "#;
+
+ let mut runner = Runner::new();
+
+ let msg = "Host function f panics";
+
+ let f = make_panicking_host_func(&mut runner.store, msg);
+ let error = runner.run_test_expect_panic(wat, &[f.into()]);
+ assert_eq!(error.downcast_ref::<&'static str>(), Some(&msg));
+
+ Ok(())
+ }
+
+ #[cfg_attr(any(asan, 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(())
+ }
+}
diff --git a/tests/misc_testsuite/stack-switching/cont_args.wast b/tests/misc_testsuite/stack-switching/cont_args.wast
new file mode 100644
index 0000000000..4808d45cf2
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_args.wast
@@ -0,0 +1,109 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; This file tests passing arguments to functions used has continuations and
+;; returning values from such continuations on ordinary (i.e., non-suspend) exit
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $unit_to_int (func (result i32)))
+ (type $int_to_unit (func (param i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+
+
+ (type $f1_t (func (param i32) (result i32)))
+ (type $f1_ct (cont $f1_t))
+
+ (type $f2_t (func (param i32) (result i32)))
+ (type $f2_ct (cont $f2_t))
+
+ (type $f3_t (func (param i32) (result i32)))
+ (type $f3_ct (cont $f3_t))
+
+ (type $res_unit_to_unit (cont $unit_to_unit))
+ (type $res_int_to_unit (cont $int_to_unit))
+ (type $res_int_to_int (cont $int_to_int))
+ (type $res_unit_to_int (cont $unit_to_int))
+
+ (tag $e1_unit_to_unit)
+ (tag $e2_int_to_unit (param i32))
+ (tag $e3_int_to_int (param i32) (result i32))
+
+ (global $i (mut i32) (i32.const 0))
+
+
+ ;; Used for testing the passing of arguments to continuation function and returning values out of them
+ (func $f1 (export "f1") (param $x i32) (result i32)
+ (global.set $i (i32.add (global.get $i) (local.get $x)))
+ (suspend $e1_unit_to_unit)
+ (i32.add (i32.const 2) (local.get $x)))
+
+ ;; Used for testing case where no suspend happens at all
+ (func $f2 (export "f2") (param $x i32) (result i32)
+ (global.set $i (i32.add (global.get $i) (local.get $x)))
+ (i32.add (i32.const 2) (local.get $x)))
+
+ ;; Same as $f1, but additionally passes payloads to and from handler
+ (func $f3 (export "f3") (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e3_int_to_int)
+ ;; return x + value returned received back from $e3
+ (i32.add (local.get $x)))
+
+
+ (func $test_case_1 (export "test_case_1") (result i32)
+ ;; remove this eventually
+ (global.set $i (i32.const 0))
+ (block $on_e1 (result (ref $res_unit_to_int))
+ (resume $f1_ct (on $e1_unit_to_unit $on_e1) (i32.const 100) (cont.new $f1_ct (ref.func $f1)))
+ ;; unreachable: we never intend to invoke the resumption when handling
+ ;; $e1 invoked from $f2
+ (unreachable))
+ ;; after on_e1, stack: [resumption]
+ (drop) ;; drop resumption
+ (global.get $i))
+
+ (func $test_case_2 (export "test_case_2") (result i32)
+ ;; remove this eventually
+ (global.set $i (i32.const 0))
+ ;;(local $finish_f3 (ref $res_unit_to_unit))
+ (block $on_e1 (result (ref $res_unit_to_int))
+ (resume $f1_ct (on $e1_unit_to_unit $on_e1) (i32.const 49) (cont.new $f1_ct (ref.func $f1)))
+ (unreachable))
+ ;; after on_e1, stack: [resumption]
+ ;;(local.set $finish_f2)
+ (resume $res_unit_to_int)
+ ;; the resume above resumes execution of f2, which finishes without further suspends
+ (i32.add (global.get $i)))
+
+ (func $test_case_3 (export "test_case_3") (result i32)
+ ;; remove this eventually
+ (global.set $i (i32.const 0))
+ (resume $f2_ct (i32.const 49) (cont.new $f2_ct (ref.func $f2)))
+ (i32.add (global.get $i)))
+
+
+ (func $test_case_4 (export "test_case_4") (result i32)
+ (local $k (ref $res_int_to_int))
+
+ (block $on_e3 (result i32 (ref $res_int_to_int))
+ (resume $f3_ct (on $e3_int_to_int $on_e3) (i32.const 49) (cont.new $f3_ct (ref.func $f3)))
+ (unreachable))
+ ;; after on_e3, expected stack: [50 resumption]
+ (local.set $k)
+
+ ;; add 1 to value 50 received from f6 via tag e3, thus passing 51 back to it
+ (i32.add (i32.const 1))
+ (resume $res_int_to_int (local.get $k))
+ ;; expecting to get 49 (original argument to function) + 51 (passed above) back
+ )
+
+)
+
+(assert_return (invoke "test_case_1") (i32.const 100))
+(assert_return (invoke "test_case_2") (i32.const 100))
+(assert_return (invoke "test_case_3") (i32.const 100))
+(assert_return (invoke "test_case_4") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_bind1.wast b/tests/misc_testsuite/stack-switching/cont_bind1.wast
new file mode 100644
index 0000000000..392c5f712f
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_bind1.wast
@@ -0,0 +1,33 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Simple test for cont.bind: cont.bind supplies 0 arguments
+
+(module
+ (type $unit_to_int (func (result i32)))
+ (type $int_int_to_int (func (param i32 i32) (result i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+
+ (type $ct0 (cont $unit_to_int))
+ (type $ct1 (cont $int_int_to_int))
+
+ (tag $e)
+
+ (func $g (param $x i32) (param $y i32) (result i32)
+ (suspend $e)
+ (i32.add (local.get $x) (local.get $y)))
+ (elem declare func $g)
+
+ (func $test (export "test") (result i32)
+ (block $on_e (result (ref $ct0))
+ (i32.const 49) ;; consumed by resume
+ (i32.const 51) ;; consumed by resume
+ (cont.new $ct1 (ref.func $g))
+ (cont.bind $ct1 $ct1)
+ (resume $ct1 (on $e $on_e))
+ (unreachable))
+ ;; on_e
+ (resume $ct0))
+)
+
+(assert_return (invoke "test") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_bind2.wast b/tests/misc_testsuite/stack-switching/cont_bind2.wast
new file mode 100644
index 0000000000..b65df95984
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_bind2.wast
@@ -0,0 +1,34 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Simple test for cont.bind: cont.bind turns 2-arg continution into 1-arg one before calling resume
+
+(module
+ (type $unit_to_int (func (result i32)))
+ (type $int_int_to_int (func (param i32 i32) (result i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+
+ (type $ct0 (cont $unit_to_int))
+ (type $ct1 (cont $int_to_int))
+ (type $ct2 (cont $int_int_to_int))
+
+ (tag $e)
+
+ (func $g (param $x i32) (param $y i32) (result i32)
+ (suspend $e)
+ (i32.add (local.get $x) (local.get $y)))
+ (elem declare func $g)
+
+ (func $test (export "test") (result i32)
+ (block $on_e (result (ref $ct0))
+ (i32.const 49) ;; consumed by resume
+ (i32.const 51) ;; consumed by cont.bind
+ (cont.new $ct2 (ref.func $g))
+ (cont.bind $ct2 $ct1)
+ (resume $ct1 (on $e $on_e))
+ (unreachable))
+ ;; on_e
+ (resume $ct0))
+)
+
+(assert_return (invoke "test") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_bind3.wast b/tests/misc_testsuite/stack-switching/cont_bind3.wast
new file mode 100644
index 0000000000..a24e1a841f
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_bind3.wast
@@ -0,0 +1,43 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Testing cont.bind on continuations received from suspending rather than cont.new.
+
+(module
+ (type $unit_to_int (func (result i32)))
+ (type $int_to_unit (func (param i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $2int_to_int (func (param i32 i32) (result i32)))
+ (type $3int_to_int (func (param i32 i32 i32) (result i32)))
+
+ (type $ct0 (cont $unit_to_int))
+ (type $ct1 (cont $3int_to_int))
+ (type $ct2 (cont $2int_to_int))
+ (type $ct3 (cont $int_to_int))
+
+ (tag $e (param i32 i32) (result i32 i32 i32))
+
+ (func $g (result i32)
+ (suspend $e (i32.const 5) (i32.const 15))
+ (i32.add)
+ (i32.add))
+ (elem declare func $g)
+
+ (func $test (export "test") (result i32)
+ (local $k (ref $ct1))
+ (i32.const 35) ;; to be consumed by second call to cont.resume
+ (i32.const 45) ;; to be consumed by second call to cont.bind
+ (block $on_e (result i32 i32 (ref $ct1))
+ (resume $ct0 (on $e $on_e) (cont.new $ct0 (ref.func $g)))
+ (unreachable))
+ ;; on_e:
+ (local.set $k)
+ (i32.add) ;; add two values received from $e, leave on stack to be consumed by first call to cont.bind
+ (local.get $k)
+ (cont.bind $ct1 $ct2) ;; consumes the result (= 20) of the addition two lines earlier
+ (cont.bind $ct2 $ct3) ;; consumes the constant value 45 put on stack earlier
+ (resume $ct3) ;; consumes the constant value 35 put on stack earlier
+ )
+)
+
+(assert_return (invoke "test") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_bind4.wast b/tests/misc_testsuite/stack-switching/cont_bind4.wast
new file mode 100644
index 0000000000..bd05c46c77
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_bind4.wast
@@ -0,0 +1,106 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Testing that the creation of the necessary payload buffers works as expect,
+;; even when the same continuation reference is suspended multiple times
+
+(module
+ (type $unit_to_int (func (result i32)))
+ (type $int_to_unit (func (param i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $2int_to_int (func (param i32 i32) (result i32)))
+ (type $3int_to_int (func (param i32 i32 i32) (result i32)))
+
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+ (type $ct2 (cont $2int_to_int))
+
+ (global $checker (mut i32) (i32.const 0))
+
+ (func $check_stack (param $expected i32) (param $actual i32) (result i32)
+ (if (result i32)
+ (i32.xor (local.get $expected) (local.get $actual))
+ (then (unreachable))
+ (else (local.get $actual))))
+
+ (func $check_stack2
+ (param $expected1 i32)
+ (param $expected2 i32)
+ (param $actual1 i32)
+ (param $actual2 i32)
+ (result i32 i32)
+ (if
+ (i32.xor (local.get $expected1) (local.get $actual1))
+ (then (unreachable))
+ (else))
+ (if
+ (i32.xor (local.get $expected2) (local.get $actual2))
+ (then (unreachable))
+ (else))
+ (local.get $actual1)
+ (local.get $actual2))
+
+
+ (tag $e (param i32) (result i32))
+ (tag $f (param i32 i32) (result i32 i32))
+
+ (func $g (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (call $check_stack (i32.const 10))
+ (suspend $e)
+ (call $check_stack (i32.const 15))
+ (i32.add (i32.const 5))
+ (call $check_stack (i32.const 20))
+ (suspend $e)
+ (call $check_stack (i32.const 25))
+ (i32.const 30)
+ (suspend $f)
+ (call $check_stack2 (i32.const 35) (i32.const 40))
+ (i32.add))
+ (elem declare func $g)
+
+ (func $test (export "test") (result i32)
+ (local $k1 (ref $ct0))
+ (local $k2 (ref $ct1))
+ (local $k3 (ref $ct2))
+ (local $i i32)
+
+ (block $on_e1 (result i32 (ref $ct0))
+ (i32.const 9)
+ (cont.new $ct0 (ref.func $g))
+ (cont.bind $ct0 $ct1) ;; binding 9 here as value of parameter $x of $g
+ (resume $ct1 (on $e $on_e1))
+ (unreachable))
+ (local.set $k1)
+ (call $check_stack (i32.const 10))
+ (i32.add (i32.const 5))
+ (call $check_stack (i32.const 15))
+ (cont.bind $ct0 $ct1 (local.get $k1)) ;; binding 15
+ (local.set $k2)
+
+
+ (block $on_e2 (result i32 (ref $ct0))
+ (resume $ct1 (on $e $on_e2) (local.get $k2))
+ (unreachable))
+ (local.set $k1)
+ (call $check_stack (i32.const 20))
+ (i32.add (i32.const 5))
+ (call $check_stack (i32.const 25))
+ (cont.bind $ct0 $ct1 (local.get $k1)) ;; binding 25
+ (local.set $k2)
+ (block $on_f (result i32 i32 (ref $ct2))
+ (resume $ct1 (on $f $on_f) (local.get $k2))
+ (unreachable))
+ (local.set $k3)
+ (call $check_stack2 (i32.const 25) (i32.const 30))
+ (i32.add (i32.const 10))
+ (local.set $i)
+ (i32.add (i32.const 10))
+ (local.get $i)
+ (call $check_stack2 (i32.const 35) (i32.const 40))
+ (local.get $k3)
+ (cont.bind $ct2 $ct1) ;; binding 35, 40
+ (resume $ct1))
+)
+
+(assert_return (invoke "test") (i32.const 75))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding1.wast b/tests/misc_testsuite/stack-switching/cont_forwarding1.wast
new file mode 100644
index 0000000000..b7dd4462e7
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding1.wast
@@ -0,0 +1,52 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Simple forwarding, no payloads, no param or return values on
+;; function, immediately resumed by handler
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ (type $g2_res_type (func (result (ref $ct))))
+ (type $g2_res_type_ct (cont $g2_res_type))
+
+ (tag $e1)
+ (tag $e2)
+
+ (global $marker (mut i32) (i32.const 0))
+
+ (func $update_marker (param $x i32)
+ (i32.add (global.get $marker) (i32.const 1))
+ (i32.mul (local.get $x))
+ (global.set $marker))
+
+ (func $g1
+ (call $update_marker (i32.const 2))
+ (suspend $e1)
+ (call $update_marker (i32.const 3)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2
+ (block $on_e2 (result (ref $ct))
+ (call $update_marker (i32.const 5))
+ (resume $ct (on $e2 $on_e2) (cont.new $ct (ref.func $g1)))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3
+ (block $on_e1 (result (ref $ct))
+ (call $update_marker (i32.const 7))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g2)))
+ (unreachable))
+ (call $update_marker (i32.const 11))
+ (resume $ct))
+
+ (func $test (export "test") (result i32)
+ (call $g3)
+ (global.get $marker)))
+
+(assert_return (invoke "test") (i32.const 2742))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding2.wast b/tests/misc_testsuite/stack-switching/cont_forwarding2.wast
new file mode 100644
index 0000000000..cc48ae155c
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding2.wast
@@ -0,0 +1,42 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Like previous test, but with param and return values on functions (but no tag payloads)
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (tag $e1)
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (suspend $e1)
+ (i32.add (local.get $x) (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (block $on_e1 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e1 $on_e1) (cont.new $ct0 (ref.func $g2)))
+ (unreachable))
+ (resume $ct1)
+ (i32.add (i32.const 1)))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 6))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding3.wast b/tests/misc_testsuite/stack-switching/cont_forwarding3.wast
new file mode 100644
index 0000000000..843d682cba
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding3.wast
@@ -0,0 +1,47 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Like previous test, but tag has payloads (param and return values)
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (local $k (ref $ct0))
+ (block $on_e1 (result i32 (ref $ct0))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e1 $on_e1) (cont.new $ct0 (ref.func $g2)))
+ (unreachable))
+ (local.set $k)
+ (i32.add (i32.const 1))
+ (local.get $k)
+ (resume $ct0)
+ (i32.add (i32.const 1)))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 8))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding4.wast b/tests/misc_testsuite/stack-switching/cont_forwarding4.wast
new file mode 100644
index 0000000000..de193999dc
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding4.wast
@@ -0,0 +1,58 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Continuation is not immediately resumed, instead we run a different continuation in the meantime.
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (local $k2 (ref $ct0))
+ (block $on_e1 (result i32 (ref $ct0))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e1 $on_e1) (cont.new $ct0 (ref.func $g2)))
+ (unreachable))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+
+ ;; We run another continuation before resuming $k1
+ (block $on_e1_2 (param i32) (result i32 (ref $ct0))
+ (resume $ct0 (on $e1 $on_e1_2) (cont.new $ct0 (ref.func $g1)))
+ (unreachable))
+ (local.set $k2)
+ (i32.add (i32.const 1))
+ (resume $ct0 (local.get $k2))
+ (i32.add (i32.const 1))
+
+ ;; Now finally resume $k1
+ (resume $ct0 (local.get $k1))
+ (i32.add (i32.const 1)))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 12))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding5.wast b/tests/misc_testsuite/stack-switching/cont_forwarding5.wast
new file mode 100644
index 0000000000..9c7af62f85
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding5.wast
@@ -0,0 +1,61 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+;; Continuation is not immediately resumed, we pass it to a different one as an
+;; argument, increasing the length of the chain by adding yet another useless
+;; handler.
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (type $ct1_to_int (func (param (ref $ct1)) (result i32)))
+ (type $ct2 (cont $ct1_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $k (ref $ct1)) (result i32)
+ (resume $ct1 (local.get $k)))
+ (elem declare func $g3)
+
+ (func $g4 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (local $k2 (ref $ct1))
+ (block $on_e1 (result i32 (ref $ct0))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e1 $on_e1) (cont.new $ct0 (ref.func $g2)))
+ (unreachable))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+ (cont.bind $ct0 $ct1 (local.get $k1))
+ (local.set $k2)
+
+ ;; We resume $k1 by running it from within another continuation
+ (resume $ct2 (local.get $k2) (cont.new $ct2 (ref.func $g3)))
+ (i32.add (i32.const 1)))
+
+ (func $test (export "test") (result i32)
+ (call $g4 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 8))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding6.wast b/tests/misc_testsuite/stack-switching/cont_forwarding6.wast
new file mode 100644
index 0000000000..9fe38e74df
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding6.wast
@@ -0,0 +1,58 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; The resumed continuation suspends again (from the same inner function), using the same tag.
+;; We install a new handler at the outermost level, which should be forwarded to correctly.
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ ;;(call $update_marker (i32.const 5))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (block $on_e1 (result i32 (ref $ct0))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e1 $on_e1) (cont.new $ct0 (ref.func $g2)))
+ (unreachable))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+
+ ;; g1 will suspend again. We install a new handler for $e1
+ (block $on_e1_2 (param i32) (result i32 (ref $ct0))
+ (resume $ct0 (on $e1 $on_e1_2) (local.get $k1))
+ (unreachable))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+
+ (resume $ct0 (local.get $k1))
+ (i32.add (i32.const 1)))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 10))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding7.wast b/tests/misc_testsuite/stack-switching/cont_forwarding7.wast
new file mode 100644
index 0000000000..54b1a56add
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding7.wast
@@ -0,0 +1,56 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; The resumed continuation suspends again (but not from the same function), using the same tag.
+;; We use the same outer handler in a loop
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+ (type $ct1 (cont $unit_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2)
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (block $on_e2 (result (ref $ct1))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (i32.add (i32.const 1))
+ ;; suspend to $e1 again, this time from the direct child of the handler of $e1
+ (suspend $e1)
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (local.get $x)
+ (cont.new $ct0 (ref.func $g2))
+
+ (loop $loop (param i32 (ref $ct0))
+ (block $on_e1 (param i32 (ref $ct0)) (result i32 (ref $ct0))
+ (resume $ct0 (on $e1 $on_e1))
+ (return))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+ (local.get $k1)
+ (br $loop))
+ (unreachable))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 10))
diff --git a/tests/misc_testsuite/stack-switching/cont_forwarding8.wast b/tests/misc_testsuite/stack-switching/cont_forwarding8.wast
new file mode 100644
index 0000000000..4766fc6e19
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_forwarding8.wast
@@ -0,0 +1,65 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; The resumed continuation suspends again, to the same and different tags.
+;; We use the same outer handler in a loop
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $unit_to_int (func (result i32)))
+ (type $ct0 (cont $int_to_int))
+
+ (tag $e1 (param i32) (result i32))
+ (tag $e2 (param i32) (result i32))
+
+ (func $g1 (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1))
+ (suspend $e2)
+ (i32.add (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1)))
+ (elem declare func $g1)
+
+ ;; Calls $g1 as continuation, but only handles e2 rather than e1
+ (func $g2 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (block $on_e2 (result i32 (ref $ct0))
+ (i32.add (local.get $x) (i32.const 1))
+ (resume $ct0 (on $e2 $on_e2) (cont.new $ct0 (ref.func $g1)))
+ (unreachable))
+ (local.set $k1)
+
+ ;; we expect to suspend to $e2 eventually, at which point we install a new handler, while keeping the
+ ;; "outer" one for $e1 intact.
+ (i32.add (i32.const 1))
+ (block $on_e2_2 (param i32) (result i32 (ref $ct0))
+ (resume $ct0 (on $e2 $on_e2_2) (local.get $k1))
+ (i32.add (i32.const 1))
+ (suspend $e1)
+ (i32.add (i32.const 1))
+ (return))
+ (unreachable))
+ (elem declare func $g2)
+
+ (func $g3 (param $x i32) (result i32)
+ (local $k1 (ref $ct0))
+ (local.get $x)
+ (cont.new $ct0 (ref.func $g2))
+
+ (loop $loop (param i32 (ref $ct0))
+ (block $on_e1 (param i32 (ref $ct0)) (result i32 (ref $ct0))
+ (resume $ct0 (on $e1 $on_e1))
+ (return))
+ (local.set $k1)
+ (i32.add (i32.const 1))
+ (local.get $k1)
+ (br $loop))
+ (unreachable))
+
+ (func $test (export "test") (result i32)
+ (call $g3 (i32.const 1))))
+
+(assert_return (invoke "test") (i32.const 12))
diff --git a/tests/misc_testsuite/stack-switching/cont_nary.wast b/tests/misc_testsuite/stack-switching/cont_nary.wast
new file mode 100644
index 0000000000..feed5c3dd7
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nary.wast
@@ -0,0 +1,92 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Tests using support for n-ary continuations
+;; Uses a function as continuation that has 3 param and 5 return values
+;; Uses tag that has 4 elements of payloads
+;; All of these mix i32 and i64 values
+
+
+(module
+ (type $unit_to_unit (func))
+ (type $unit_to_int (func (result i32)))
+ (type $int_to_unit (func (param i32)))
+ (type $int_to_int (func (param i32) (result i32)))
+
+
+ ;; type of function f
+ (type $f_t
+ (func
+ (param i64 i32 i64)
+ (result i32 i64 i32 i64 i32)))
+ (type $f_ct (cont $f_t))
+
+
+ ;; type of the resumption we expect to see in our handler for $e
+ (type $res_ft
+ (func
+ (param i64 i32 i64 i32)
+ (result i32 i64 i32 i64 i32)))
+ (type $res (cont $res_ft))
+
+ ;; This is 10^10, which exceeds what can be stored in any 32 bit type
+ (global $big i64 (i64.const 10_000_000_000))
+
+ (tag $e
+ (param i32 i64 i32 i64)
+ (result i64 i32 i64 i32))
+
+
+
+ (func $f (export "f")
+ (param $x i64) (param $y i32) (param $z i64)
+ (result i32 i64 i32 i64 i32)
+
+ ;; value to stay on the stack as part of return values
+ (i32.const 1)
+
+ ;; values to be passed to $e
+ (i32.const 10)
+ (local.get $z)
+ (local.get $y)
+ (local.get $x)
+ (suspend $e)
+ )
+
+ (func $test (export "test") (result i32 i64)
+ (local $i64_acc i64)
+ (local $i32_acc i32)
+ (local $k (ref $res))
+ (local.set $i64_acc (i64.const 0))
+ (local.set $i32_acc (i32.const 0))
+
+
+ (block $on_e (result i32 i64 i32 i64 (ref $res)) ;; lets call these values v1 v2 v3 v4 k
+ (global.get $big)
+ (i32.const 100)
+ (i64.mul (global.get $big) (i64.const 10))
+ (resume $f_ct (on $e $on_e) (cont.new $f_ct (ref.func $f)))
+ (unreachable))
+ ;; after on_e
+ (local.set $k)
+ (i32.const 1000)
+ ;; We pass v2 v3 v4 123 as arguments to the continuation, leaving v1 on the stack
+ (resume $res (local.get $k))
+ ;; We now have v1 and the five return values of $f on the stack, i.e. [i32 i32 i64 i32 i64 i32]
+ ;; Lets accumulate them
+ ;;
+
+ (local.set $i32_acc (i32.add (local.get $i32_acc)))
+ (local.set $i64_acc (i64.add (local.get $i64_acc)))
+ (local.set $i32_acc (i32.add (local.get $i32_acc)))
+ (local.set $i64_acc (i64.add (local.get $i64_acc)))
+ (local.set $i32_acc (i32.add (local.get $i32_acc)))
+ (local.set $i32_acc (i32.add (local.get $i32_acc)))
+
+ ;; ;; Set up return values
+ (local.get $i32_acc)
+ (local.get $i64_acc))
+
+)
+
+(assert_return (invoke "test") (i32.const 1111) (i64.const 110_000_000_000))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested1.wast b/tests/misc_testsuite/stack-switching/cont_nested1.wast
new file mode 100644
index 0000000000..fe6d37a13f
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested1.wast
@@ -0,0 +1,55 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; test using continuations from within a continuation
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ (type $g2_res_type (func (result (ref $ct))))
+ (type $g2_res_type_ct (cont $g2_res_type))
+
+ (tag $e1)
+ (tag $e2 (param (ref $ct)))
+
+ (global $marker (mut i32) (i32.const 0))
+
+ (func $update_marker (param $x i32)
+ (i32.add (global.get $marker) (i32.const 1))
+ (i32.mul (local.get $x))
+ (global.set $marker))
+
+ (func $g1
+ (call $update_marker (i32.const 2))
+ (suspend $e1)
+ (call $update_marker (i32.const 3))
+ )
+ (elem declare func $g1)
+
+ (func $g2
+ (local $k1 (ref $ct))
+ (local $k2 (ref $ct))
+ (call $update_marker (i32.const 5))
+
+ (block $on_e1 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (local.set $k1)
+ (call $update_marker (i32.const 7))
+ (block $on_e1_2 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1_2) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (local.set $k2)
+ (call $update_marker (i32.const 11))
+ (resume $ct (local.get $k1)))
+ (elem declare func $g2)
+
+
+
+ (func $test (export "test") (result i32)
+ (resume $ct (cont.new $ct (ref.func $g2)))
+ (global.get $marker)))
+
+(assert_return (invoke "test") (i32.const 6_108))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested2.wast b/tests/misc_testsuite/stack-switching/cont_nested2.wast
new file mode 100644
index 0000000000..c5785dd1e8
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested2.wast
@@ -0,0 +1,67 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Similar to cont_nested1, but with payloads
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+ (type $ct (cont $int_to_int))
+
+ (type $unit_to_int (func (result i32)))
+ (type $ct_unit_to_int (cont $unit_to_int))
+
+ (tag $e1 (param i32) (result i32))
+
+ (global $marker (mut i32) (i32.const 0))
+
+ ;; (func $update_marker (param $x i32) (result i32)
+ ;; (i32.add (global.get $marker) (i32.const 1))
+ ;; (i32.mul (local.get $x))
+ ;; (global.set $marker)
+ ;; (global.get $marker))
+
+ (func $scramble (param $x i32) (param $y i32) (result i32)
+ (i32.add (local.get $y) (i32.const 1))
+ (i32.mul (local.get $x))
+ )
+
+ (func $g1 (param $x i32) (result i32)
+ (call $scramble (i32.const 3) (local.get $x))
+ (suspend $e1)
+ (call $scramble (i32.const 5))
+ (i32.add (local.get $x))
+ (global.set $marker)
+ (global.get $marker))
+ (elem declare func $g1)
+
+ (func $g2 (result i32)
+ (local $k1 (ref $ct))
+ (local $v i32)
+
+ (block $on_e1 (result i32 (ref $ct))
+ (resume $ct (on $e1 $on_e1) (i32.const 7) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (local.set $k1)
+ (call $scramble (i32.const 11)) ;; scramble the value received via $e1 from $g1
+ (local.set $v)
+
+ (block $on_e1_2 (result i32 (ref $ct))
+ (resume $ct (on $e1 $on_e1_2) (local.get $v) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (drop) ;; drop continuation, we don't intend to resume the second invocation of g1
+ (call $scramble (i32.const 13))
+
+ (resume $ct (local.get $k1))
+ (i32.add (global.get $marker)))
+ (elem declare func $g2)
+
+
+
+ (func $test (export "test") (result i32)
+ (resume $ct_unit_to_int (cont.new $ct_unit_to_int (ref.func $g2)))
+
+ )
+ )
+
+(assert_return (invoke "test") (i32.const 145_670))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested3.wast b/tests/misc_testsuite/stack-switching/cont_nested3.wast
new file mode 100644
index 0000000000..3180d47e21
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested3.wast
@@ -0,0 +1,43 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Minimal test for resuming continuation after its original parent is gone
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ (type $g2 (func (result (ref $ct))))
+ (type $g2_ct (cont $g2))
+
+ (tag $e1)
+ ;;(tag $e2 (param (ref $ct)))
+
+ (global $marker (mut i32) (i32.const 0))
+
+ ;;(global $orphan (mut (ref $ct)))
+
+ (func $g1
+ (suspend $e1)
+ (global.set $marker (i32.const 100))
+ )
+ (elem declare func $g1)
+
+ (func $g2 (result (ref $ct))
+ (block $on_e1 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ ;; continuation becomes return value
+ )
+
+ (elem declare func $g2)
+
+
+ (func $test (export "test") (result i32)
+ (resume $g2_ct (cont.new $g2_ct (ref.func $g2)))
+ (resume $ct) ;; resume return value of $g2
+ (global.get $marker))
+ )
+
+(assert_return (invoke "test") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested4.wast b/tests/misc_testsuite/stack-switching/cont_nested4.wast
new file mode 100644
index 0000000000..83a87cb4ee
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested4.wast
@@ -0,0 +1,47 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Minimal test for resuming continuation after its original parent is suspended
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ ;;(type $g2 (func (result (ref $ct))))
+ ;;(type $g2_ct (cont $g2))
+
+ (tag $e1)
+ (tag $e2 (param (ref $ct)))
+
+ (global $marker (mut i32) (i32.const 0))
+
+ ;;(global $orphan (mut (ref $ct)))
+
+ (func $g1
+ (suspend $e1)
+ (global.set $marker (i32.const 100))
+ )
+ (elem declare func $g1)
+
+ (func $g2
+ (block $on_e1 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (suspend $e2)
+ ;; continuation becomes return value
+ (unreachable))
+
+ (elem declare func $g2)
+
+
+ (func $test (export "test") (result i32)
+ (block $on_e2 (result (ref $ct) (ref $ct))
+ (resume $ct (on $e2 $on_e2) (cont.new $ct (ref.func $g2)))
+ (unreachable))
+ (drop) ;; drop the continuation (i.e., for resuming g2)
+ (resume $ct) ;; resume continuation received as payload of $e2 (i.e., continuing execution of $g1)
+ (global.get $marker))
+)
+
+(assert_return (invoke "test") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested5.wast b/tests/misc_testsuite/stack-switching/cont_nested5.wast
new file mode 100644
index 0000000000..9a8f188a70
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested5.wast
@@ -0,0 +1,71 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; test using continuations from within a continuation
+
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ (type $g2_res_type (func (result (ref $ct))))
+ (type $g2_res_type_ct (cont $g2_res_type))
+
+ (tag $e1)
+ (tag $e2 (param (ref $ct)))
+
+ (global $marker (mut i32) (i32.const 0))
+
+ (func $update_marker (param $x i32)
+ (i32.add (global.get $marker) (i32.const 1))
+ (i32.mul (local.get $x))
+ (global.set $marker))
+
+ (func $g1
+ (call $update_marker (i32.const 2))
+ (suspend $e1)
+ (call $update_marker (i32.const 3))
+ )
+ (elem declare func $g1)
+
+ (func $g2 (result (ref $ct))
+ (local $k1 (ref $ct))
+ (local $k2 (ref $ct))
+ (call $update_marker (i32.const 5))
+
+ (block $on_e1 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (local.set $k1)
+ (call $update_marker (i32.const 7))
+ (block $on_e1_2 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1_2) (cont.new $ct (ref.func $g1)))
+ (unreachable))
+ (local.set $k2)
+ (call $update_marker (i32.const 11))
+ (resume $ct (local.get $k1))
+ (call $update_marker (i32.const 13))
+ (local.get $k2)
+ )
+ (elem declare func $g2)
+
+ (func $g3
+ (call $update_marker (i32.const 17))
+ (resume $g2_res_type_ct (cont.new $g2_res_type_ct (ref.func $g2)))
+ (call $update_marker (i32.const 19))
+ (suspend $e2))
+ (elem declare func $g3)
+
+
+ (func $test (export "test") (result i32)
+ (call $update_marker (i32.const 23))
+ (block $on_e2 (result (ref $ct) (ref $ct))
+ (resume $ct (on $e2 $on_e2) (cont.new $ct (ref.func $g3)))
+ (unreachable))
+ (drop) ;; we won't resume g3, but want the payload
+ (call $update_marker (i32.const 31))
+ (resume $ct)
+ (global.get $marker)))
+
+(assert_return (invoke "test") (i32.const 490_074_902))
diff --git a/tests/misc_testsuite/stack-switching/cont_nested6.wast b/tests/misc_testsuite/stack-switching/cont_nested6.wast
new file mode 100644
index 0000000000..0aead698c5
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_nested6.wast
@@ -0,0 +1,47 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; test proper handling of TSP pointer after a continuation returns normally
+
+(module
+
+ (type $unit_to_unit (func))
+ (type $ct (cont $unit_to_unit))
+
+ (type $g2_res_type (func (result (ref $ct))))
+ (type $g2_res_type_ct (cont $g2_res_type))
+
+ (tag $e1)
+
+ (global $marker (mut i32) (i32.const 0))
+
+ (func $update_marker (param $x i32)
+ (i32.add (global.get $marker) (i32.const 1))
+ (i32.mul (local.get $x))
+ (global.set $marker))
+
+ (func $g1
+ (call $update_marker (i32.const 2)))
+ (elem declare func $g1)
+
+ (func $g2
+ (call $update_marker (i32.const 3))
+
+ (resume $ct (cont.new $ct (ref.func $g1)))
+ (call $update_marker (i32.const 5))
+
+ ;; This suspend only works correctly if we reset the TSP
+ ;; pointer after the g1 continuation returned.
+ (suspend $e1))
+
+ (elem declare func $g2)
+
+
+ (func $test (export "test") (result i32)
+ (block $on_e1 (result (ref $ct))
+ (resume $ct (on $e1 $on_e1) (cont.new $ct (ref.func $g2)))
+ (unreachable))
+ (drop)
+ (global.get $marker)))
+
+(assert_return (invoke "test") (i32.const 45))
diff --git a/tests/misc_testsuite/stack-switching/cont_new.wast b/tests/misc_testsuite/stack-switching/cont_new.wast
new file mode 100644
index 0000000000..b7de60100f
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_new.wast
@@ -0,0 +1,19 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (func $noop)
+ (elem declare func $noop)
+
+ (func $make-cont (result (ref $ct))
+ (cont.new $ct (ref.func $noop)))
+
+ (func $f (export "f") (result i32)
+ (call $make-cont)
+ (ref.is_null))
+)
+
+(assert_return (invoke "f") (i32.const 0))
diff --git a/tests/misc_testsuite/stack-switching/cont_resume.wast b/tests/misc_testsuite/stack-switching/cont_resume.wast
new file mode 100644
index 0000000000..2932dd6430
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_resume.wast
@@ -0,0 +1,20 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (global $i (mut i32) (i32.const 0))
+
+ (func $g
+ (global.set $i (i32.const 42)))
+ (elem declare func $g)
+
+ (func $f (export "f") (result i32)
+ (global.set $i (i32.const 99))
+ (resume $ct (cont.new $ct (ref.func $g)))
+ (global.get $i))
+)
+
+(assert_return (invoke "f") (i32.const 42))
diff --git a/tests/misc_testsuite/stack-switching/cont_resume1.wast b/tests/misc_testsuite/stack-switching/cont_resume1.wast
new file mode 100644
index 0000000000..5ff1d93388
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_resume1.wast
@@ -0,0 +1,30 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft_init (func))
+ (type $ct_init (cont $ft_init))
+ (type $ft (func (param i32)))
+ (type $ct (cont $ft))
+ (tag $yield (result i32))
+
+ (global $i (mut i32) (i32.const 0))
+
+ (func $g
+ (suspend $yield)
+ (global.set $i))
+ (elem declare func $g)
+
+ (func $f (export "f") (result i32)
+ (local $k (ref null $ct))
+ (global.set $i (i32.const 99))
+ (block $on_yield (result (ref $ct))
+ (resume $ct_init (on $yield $on_yield) (cont.new $ct_init (ref.func $g)))
+ (unreachable))
+ ;; on_yield
+ (local.set $k)
+ (resume $ct (i32.const 42) (local.get $k))
+ (global.get $i))
+)
+
+(assert_return (invoke "f") (i32.const 42))
diff --git a/tests/misc_testsuite/stack-switching/cont_resume2.wast b/tests/misc_testsuite/stack-switching/cont_resume2.wast
new file mode 100644
index 0000000000..01430fb78c
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_resume2.wast
@@ -0,0 +1,67 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; This test requires the following to work:
+;; 1. Passing arguments to tags and receiving values back at suspend sites
+;; 2. Passing values to a continuation obtained from a handler
+;; 3. Receving values once a resumed continuation returns oridinarly
+
+;; Not tested: Passing values when resuming a continuation obtained from
+;; cont.new rather than a handler
+
+;; TODO(frank-emrich) Replace this with more fine-grained tests in the future
+
+(module
+
+ (type $int_to_int (func (param i32) (result i32)))
+
+ (type $cont_int_to_int (cont $int_to_int))
+
+
+ (type $g_type (func (result i32)))
+ (type $g_ct (cont $g_type))
+
+
+ (tag $e0 (param i32) (result i32)) ;; never actually invoked
+ (tag $e1 (param i32) (result i32))
+ (tag $e2 (param i32) (result i32))
+ (tag $e3 (param i32) (result i32)) ;; never actually invoked
+
+
+ (func $g (result i32)
+ (suspend $e1 (i32.const 42))
+ (suspend $e2) ;; passes value obtained from doing $e on to $f
+ (i32.add (i32.const 21)))
+ (elem declare func $g)
+
+ (func $f (export "f") (result i32)
+ (local $c (ref $cont_int_to_int))
+ (block $on_e0_e2_e3 (result i32 (ref $cont_int_to_int))
+ (block $on_e1 (result i32 (ref $cont_int_to_int))
+ ;; We know that $e0, e2, e3 won't actually be performed here, but add a handler
+ ;; to test the switching logic
+ (resume $g_ct
+ (on $e0 $on_e0_e2_e3)
+ (on $e1 $on_e1)
+ (on $e2 $on_e0_e2_e3)
+ (on $e3 $on_e0_e2_e3)
+ (cont.new $g_ct (ref.func $g)))
+ (unreachable))
+ ;; after $on_e1
+ (local.set $c)
+ ;; stack now contains the value that $g passed to $e1, we manipulate it
+ (i32.add (i32.const 13))
+ (local.get $c)
+ (resume $cont_int_to_int (on $e2 $on_e0_e2_e3))
+ (unreachable))
+ ;; after $on_e0_e2_e3
+ ;; stack contains value that $g passed to $e2 and continuation
+ ;; We manipulate the value again before resuming the continuation
+ (local.set $c)
+ (i32.add (i32.const 24))
+ (local.get $c)
+ (resume $cont_int_to_int)
+ ))
+
+
+(assert_return (invoke "f") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_resume_return.wast b/tests/misc_testsuite/stack-switching/cont_resume_return.wast
new file mode 100644
index 0000000000..72791a5457
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_resume_return.wast
@@ -0,0 +1,16 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+
+ (func $g (result i32)
+ (i32.const 42))
+ (elem declare func $g)
+
+ (func $f (export "f") (result i32)
+ (resume $ct (cont.new $ct (ref.func $g))))
+)
+
+(assert_return (invoke "f") (i32.const 42))
diff --git a/tests/misc_testsuite/stack-switching/cont_return.wast b/tests/misc_testsuite/stack-switching/cont_return.wast
new file mode 100644
index 0000000000..f7f5da38c9
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_return.wast
@@ -0,0 +1,19 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Test returning value from continuation function without any suspending
+
+(module
+
+ (type $g_type (func (result i32)))
+ (type $g_ct (cont $g_type))
+
+ (func $g (result i32)
+ (i32.const 100))
+ (elem declare func $g)
+
+ (func $f (export "f") (result i32)
+ (resume $g_ct
+ (cont.new $g_ct (ref.func $g)))))
+
+(assert_return (invoke "f") (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/cont_suspend.wast b/tests/misc_testsuite/stack-switching/cont_suspend.wast
new file mode 100644
index 0000000000..9618844b5f
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_suspend.wast
@@ -0,0 +1,30 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; Small continuation resume test
+;; expected output:
+;; 1 : i32
+;; 2 : i32
+;; 3 : i32
+(module
+ (func $print (import "spectest" "print_i32") (param i32) (result))
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $h)
+ (func $f (export "f")
+ (suspend $h)
+ (call $print (i32.const 2)))
+ (func (export "run") (result i32)
+ (call $print (i32.const 1))
+ (block $on_h (result (ref $ct))
+ (resume $ct (on $h $on_h)
+ (cont.new $ct (ref.func $f)))
+ (unreachable))
+ (drop)
+ (call $print (i32.const 3))
+ (return (i32.const 42)))
+)
+
+(assert_return (invoke "run") (i32.const 42))
diff --git a/tests/misc_testsuite/stack-switching/cont_table.wast b/tests/misc_testsuite/stack-switching/cont_table.wast
new file mode 100644
index 0000000000..ed0acf03d4
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_table.wast
@@ -0,0 +1,89 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func (param i32) (result i32)))
+ (type $ct (cont $ft))
+
+ (table $conts0 0 (ref null $ct))
+ (table $conts1 0 (ref null $ct))
+
+ (func $f (param $x i32) (result i32)
+ (i32.add (local.get $x) (i32.const 1))
+ )
+ (elem declare func $f)
+
+ (func (export "table0_size") (result i32)
+ (table.size $conts0)
+ )
+
+ (func (export "table0_grow_f") (result i32)
+ (table.grow $conts0 (cont.new $ct (ref.func $f)) (i32.const 10))
+ )
+ (func (export "table1_grow_f") (result i32)
+ (table.grow $conts1 (cont.new $ct (ref.func $f)) (i32.const 10))
+ )
+
+ (func (export "table_copy_0_to_1")
+ (table.copy $conts1 $conts0 (i32.const 0) (i32.const 0) (i32.const 10))
+ )
+
+ (func (export "table0_fill_f")
+ (table.fill $conts0 (i32.const 0) (cont.new $ct (ref.func $f)) (i32.const 10))
+ )
+
+ (func (export "table0_null_at") (param $i i32) (result i32)
+ (ref.is_null (table.get $conts0 (local.get $i)))
+ )
+ (func (export "table1_null_at") (param $i i32) (result i32)
+ (ref.is_null (table.get $conts1 (local.get $i)))
+ )
+
+ (func (export "table0_set_f") (param $i i32)
+ (table.set $conts0 (local.get $i) (cont.new $ct (ref.func $f)))
+ )
+
+ (func (export "table0_set_null") (param $i i32)
+ (table.set $conts0 (local.get $i) (ref.null $ct))
+ )
+
+ (func (export "table0_run") (param $i i32) (result i32)
+ (resume $ct (i32.const 99) (table.get $conts0 (local.get $i)))
+ )
+ (func (export "table1_run") (param $i i32) (result i32)
+ (resume $ct (i32.const 99) (table.get $conts1 (local.get $i)))
+ )
+)
+
+(assert_return (invoke "table0_size") (i32.const 0))
+(assert_return (invoke "table0_grow_f") (i32.const 0))
+(assert_return (invoke "table0_size") (i32.const 10))
+;; At this point table 0 contains reference to the same, resumeable continuation
+;; at all indices
+
+
+;; We now consume the continuation, do table0[0] := null and write a fresh
+;; continuation to table0[9], which we then consume
+(assert_return (invoke "table0_run" (i32.const 0)) (i32.const 100))
+(assert_return (invoke "table0_null_at" (i32.const 9)) (i32.const 0))
+(assert_return (invoke "table0_set_f" (i32.const 9)))
+(assert_return (invoke "table0_run" (i32.const 9)) (i32.const 100))
+
+
+;; Refill table with references to the same continuation, consume it, and do table0[9] := null
+(invoke "table0_fill_f")
+(assert_return (invoke "table0_run" (i32.const 0)) (i32.const 100))
+(assert_return (invoke "table0_set_f" (i32.const 0)))
+(assert_return (invoke "table0_set_null" (i32.const 9)))
+
+
+;; fill table1 with references to the same continuation running f and consume it
+(invoke "table1_grow_f")
+(assert_return (invoke "table1_run" (i32.const 0)) (i32.const 100))
+
+;; We copy table0 to table1, meaning that table1[0] should contain an available
+;; continuation, indices 1 to 8 contain the same consumed one, and table1[9]
+;; contains null.
+(invoke "table_copy_0_to_1")
+(assert_return (invoke "table1_run" (i32.const 0)) (i32.const 100))
+(assert_return (invoke "table1_null_at" (i32.const 9)) (i32.const 1))
diff --git a/tests/misc_testsuite/stack-switching/cont_twice.wast b/tests/misc_testsuite/stack-switching/cont_twice.wast
new file mode 100644
index 0000000000..8bddf4d0b2
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/cont_twice.wast
@@ -0,0 +1,18 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (func $f)
+ (func (export "resume_twice")
+ (local $k (ref $ct))
+ (local.set $k (cont.new $ct (ref.func $f)))
+ (resume $ct (local.get $k))
+ (resume $ct (local.get $k))
+ )
+ (elem declare func $f)
+)
+
+(assert_trap (invoke "resume_twice") "continuation already consumed")
diff --git a/tests/misc_testsuite/stack-switching/dup.wast b/tests/misc_testsuite/stack-switching/dup.wast
new file mode 100644
index 0000000000..189af93fa5
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/dup.wast
@@ -0,0 +1,29 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (tag $t)
+
+ (func $f
+ (suspend $t))
+ (elem declare func $f)
+
+ (func $dup (export "dup") (result i32)
+ (block $on_t-1 (result (ref $ct))
+ (block $on_t-2 (result (ref $ct))
+ (resume $ct (on $t $on_t-1)
+ ;;(on $t $on_t-2)
+ (cont.new $ct (ref.func $f)))
+ (return (i32.const 128))
+ ) ;; on_t-2 [ (ref $ct) ]
+ (drop)
+ (return (i32.const 256))
+ ) ;; on_t-1 [ (ref $ct) ]
+ (drop)
+ (return (i32.const 512)))
+)
+
+(assert_return (invoke "dup") (i32.const 512))
diff --git a/tests/misc_testsuite/stack-switching/linking_tags1.wast b/tests/misc_testsuite/stack-switching/linking_tags1.wast
new file mode 100644
index 0000000000..d551464950
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/linking_tags1.wast
@@ -0,0 +1,40 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+(module $alien
+ (tag $alien_tag (export "alien_tag"))
+)
+(register "alien")
+
+(module $mine
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $alien_tag (import "alien" "alien_tag"))
+ (tag $my_tag)
+ (func $do_alien_tag
+ (suspend $alien_tag))
+
+ ;; Don't handle the imported alien.
+ (func (export "main-1")
+ (block $on_my_tag (result (ref $ct))
+ (resume $ct (on $my_tag $on_my_tag) (cont.new $ct (ref.func $do_alien_tag)))
+ (unreachable)
+ )
+ (unreachable))
+
+ ;; Handle the imported alien.
+ (func (export "main-2")
+ (block $on_alien_tag (result (ref $ct))
+ (resume $ct (on $alien_tag $on_alien_tag) (cont.new $ct (ref.func $do_alien_tag)))
+ (unreachable)
+ )
+ (drop))
+
+ (elem declare func $do_alien_tag)
+)
+(register "mine")
+(assert_return (invoke "main-2"))
+;; Due to issue #253, we need to make sure that nothing happens afterwards in
+;; the test:
+(assert_suspension (invoke "main-1") "unhandled")
diff --git a/tests/misc_testsuite/stack-switching/linking_tags2.wast b/tests/misc_testsuite/stack-switching/linking_tags2.wast
new file mode 100644
index 0000000000..e898ed2a71
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/linking_tags2.wast
@@ -0,0 +1,140 @@
+;;! bulk_memory = true
+;;! exceptions = true
+;;! function_references = true
+;;! stack_switching = true
+
+(module $foo
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+ (type $ft-2 (func (param i32) (result i32)))
+ (type $ct-2 (cont $ft-2))
+
+ (tag $foo (export "foo") (result i32)) ;; occupies defined tag entry 0
+
+ (func $do_foo (export "do_foo") (result i32)
+ (suspend $foo))
+ (func $handle_foo (export "handle_foo") (param $f (ref $ft)) (result i32)
+ (block $on_foo (result (ref $ct-2))
+ (resume $ct (on $foo $on_foo) (cont.new $ct (local.get $f)))
+ (return)
+ ) ;; on_foo
+ (drop)
+ (return (i32.const 1))
+ )
+ (func (export "test_foo") (result i32)
+ (call $handle_foo (ref.func $do_foo)))
+ (elem declare func $do_foo)
+)
+(register "foo")
+(assert_return (invoke "test_foo") (i32.const 1))
+
+(module $baz
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+
+ (type $ft-2 (func (param i32) (result i32)))
+ (type $ct-2 (cont $ft-2))
+
+ (func $handle_foo (import "foo" "handle_foo") (param (ref $ft)) (result i32))
+ (func $do_foo (import "foo" "do_foo") (result i32))
+
+ (tag $baz (result i32)) ;; unused, but occupies defined tag entry 0
+
+ (func $handle_baz (param $f (ref $ft)) (result i32)
+ (block $on_baz (result (ref $ct-2))
+ (resume $ct (on $baz $on_baz) (cont.new $ct (local.get $f)))
+ (return)
+ ) ;; on_baz
+ (drop)
+ (return (i32.const 3))
+ )
+
+ (func $inner-baz (result i32)
+ (call $handle_baz (ref.func $do_foo)))
+ (func (export "compose-handle-foo-baz") (result i32)
+ (call $handle_foo (ref.func $inner-baz)))
+
+ (func $inner-foo (result i32)
+ (call $handle_foo (ref.func $do_foo)))
+ (func (export "compose-handle-baz-foo") (result i32)
+ (call $handle_baz (ref.func $inner-foo)))
+ (elem declare func $do_foo $inner-baz $inner-foo)
+)
+(register "baz")
+(assert_return (invoke "compose-handle-baz-foo") (i32.const 1))
+(assert_return (invoke "compose-handle-foo-baz") (i32.const 1))
+
+(module $quux
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+
+ (type $ft-2 (func (param i32) (result i32)))
+ (type $ct-2 (cont $ft-2))
+
+ (func $handle_foo (import "foo" "handle_foo") (param (ref $ft)) (result i32))
+ (tag $foo (import "foo" "foo") (result i32))
+
+ (func $do_foo (result i32)
+ (suspend $foo))
+
+ (func $my_handle_foo (param $f (ref $ft)) (result i32)
+ (block $on_foo (result (ref $ct-2))
+ (resume $ct (on $foo $on_foo) (cont.new $ct (local.get $f)))
+ (return)
+ ) ;; on_foo
+ (drop)
+ (return (i32.const 4))
+ )
+
+ (func $inner-my-foo (result i32)
+ (call $my_handle_foo (ref.func $do_foo)))
+ (func (export "compose-handle-foo-my-foo") (result i32)
+ (call $handle_foo (ref.func $inner-my-foo)))
+
+ (func $inner-foo (result i32)
+ (call $handle_foo (ref.func $do_foo)))
+ (func (export "compose-handle-my-foo-foo") (result i32)
+ (call $my_handle_foo (ref.func $inner-foo)))
+ (elem declare func $do_foo $inner-my-foo $inner-foo)
+)
+(register "quux")
+(assert_return (invoke "compose-handle-foo-my-foo") (i32.const 4))
+(assert_return (invoke "compose-handle-my-foo-foo") (i32.const 1))
+
+(module $bar
+ (type $ft (func (result i32)))
+ (type $ct (cont $ft))
+
+ (type $ft-2 (func (param i32) (result i32)))
+ (type $ct-2 (cont $ft-2))
+
+ (tag $foo (import "foo" "foo") (result i32))
+ (tag $bar (result i32))
+ (func $do_foo (result i32)
+ (suspend $foo))
+
+ ;; Don't handle the imported foo.
+ (func (export "skip-imported-foo") (result i32)
+ (block $on_bar (result (ref $ct-2))
+ (resume $ct (on $bar $on_bar) (cont.new $ct (ref.func $do_foo)))
+ (unreachable)
+ )
+ (unreachable))
+
+ ;; Handle the imported foo.
+ (func (export "handle-imported-foo") (result i32)
+ (block $on_foo (result (ref $ct-2))
+ (resume $ct (on $foo $on_foo) (cont.new $ct (ref.func $do_foo)))
+ (unreachable)
+ )
+ (drop)
+ (return (i32.const 2))
+ )
+
+ (elem declare func $do_foo)
+)
+(register "bar")
+(assert_return (invoke "handle-imported-foo") (i32.const 2))
+;; Due to issue #253, we need to make sure that nothing happens afterwards in
+;; the test:
+(assert_suspension (invoke "skip-imported-foo") "unhandled")
diff --git a/tests/misc_testsuite/stack-switching/proc_exit.wat b/tests/misc_testsuite/stack-switching/proc_exit.wat
new file mode 100644
index 0000000000..cf18820731
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/proc_exit.wat
@@ -0,0 +1,24 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; Regression test from https://github.com/bytecodealliance/wasmtime/issues/13584
+(module
+ (import "wasi_snapshot_preview1" "proc_exit" (func $proc_exit (param i32)))
+ (memory (export "memory") 1)
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (func $body)
+ (elem declare func $body)
+ (func (export "start") $start (local $k (ref null $ct))
+ ref.func $body
+ cont.new $ct
+ local.set $k
+ block $done
+ (resume $ct (local.get $k))
+ br $done
+ end
+ i32.const 0
+ call $proc_exit)
+)
+(assert_return (invoke "start"))
\ No newline at end of file
diff --git a/tests/misc_testsuite/stack-switching/switch1.wast b/tests/misc_testsuite/stack-switching/switch1.wast
new file mode 100644
index 0000000000..3671cf603a
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch1.wast
@@ -0,0 +1,37 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; smoke test for switching: Only a single switch to a cotinuation created with
+;; cont.new.
+(module
+
+ (type $ft0 (func))
+ (type $ct0 (cont $ft0))
+
+ (type $ft1 (func (param (ref $ct0))))
+ (type $ct1 (cont $ft1))
+
+ (func $print (import "spectest" "print_i32") (param i32))
+ (tag $t)
+
+
+ (func $f
+ (cont.new $ct1 (ref.func $g))
+ (switch $ct1 $t)
+ )
+ (elem declare func $f)
+
+ (func $g (type $ft1)
+ (call $print (i32.const 123))
+ )
+ (elem declare func $g)
+
+ (func $entry (export "entry") (result i32)
+ (cont.new $ct0 (ref.func $f))
+ (resume $ct0 (on $t switch))
+ (i32.const 0)
+ )
+)
+
+(assert_return (invoke "entry" ) (i32.const 0))
diff --git a/tests/misc_testsuite/stack-switching/switch2.wast b/tests/misc_testsuite/stack-switching/switch2.wast
new file mode 100644
index 0000000000..40c44f4fba
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch2.wast
@@ -0,0 +1,73 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! gc = true
+;;! stack_switching = true
+
+;; switch to continuation created by switch
+(module
+
+ (rec
+ (type $ft (func (param i32 (ref null $ct)) (result i32)))
+ (type $ct (cont $ft)))
+
+ (tag $t (result i32))
+
+ (func $f0 (type $ft)
+ ;; Just a wrapper around $f1 to make sure that the involved continuation
+ ;; chains consist of more than one element.
+
+ (local.get 0)
+ (local.get 1)
+ (cont.new $ct (ref.func $f1))
+ (resume $ct)
+ )
+ (elem declare func $f0)
+
+ (func $f1 (type $ft)
+ ;; add 1 to argument and pass to $g0 on switch
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ ;; prepare continuation
+ (cont.new $ct (ref.func $g0))
+ (switch $ct $t)
+ (drop) ;; we won't run $g to completion
+
+ ;; add 1 to payload received from $g1
+ (i32.const 1)
+ (i32.add)
+ )
+ (elem declare func $f1)
+
+ (func $g0 (type $ft)
+ ;; Just a wrapper around $g1 to make sure that the involved continuation
+ ;; chains consist of more than one element.
+
+ (local.get 0)
+ (local.get 1)
+ (cont.new $ct (ref.func $g1))
+ (resume $ct)
+ )
+ (elem declare func $g0)
+
+ (func $g1 (type $ft)
+ ;; add 1 to argument received from $f1
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ (local.get 1)
+ (switch $ct $t)
+
+ ;; $f never switches back to us
+ (unreachable)
+ )
+ (elem declare func $g1)
+
+ (func $entry (export "entry") (result i32)
+ (i32.const 100)
+ (ref.null $ct)
+ (cont.new $ct (ref.func $f0))
+ (resume $ct (on $t switch))
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 103))
diff --git a/tests/misc_testsuite/stack-switching/switch3.wast b/tests/misc_testsuite/stack-switching/switch3.wast
new file mode 100644
index 0000000000..2bc89d83c1
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch3.wast
@@ -0,0 +1,97 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! gc = true
+;;! stack_switching = true
+
+;; resume continuation created by switch
+(module
+
+ (rec
+ (type $ft0 (func (param i32 (ref null $ct0)) (result i32)))
+ (type $ct0 (cont $ft0)))
+
+ (type $ft1 (func (param i32) (result i32)))
+ (type $ct1 (cont $ft1))
+
+ (tag $t_switch (result i32))
+ (tag $t_suspend (param i32) (result i32))
+
+ (func $f0 (type $ft0)
+ ;; Just a wrapper around $f1 to make sure that the involved continuation
+ ;; chains consist of more than one element.
+
+ (local.get 0)
+ (local.get 1)
+ (cont.new $ct0 (ref.func $f1))
+ (resume $ct0)
+ )
+ (elem declare func $f0)
+
+ (func $f1 (type $ft0)
+ ;; add 1 to argument and pass to $g on switch
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ ;; prepare continuation
+ (cont.new $ct0 (ref.func $g0))
+ ;; switch to $g0
+ (switch $ct0 $t_switch)
+ ;; g1 resumed us, installed suspend handler for t_suspend)
+ ;; drop null continuation and increment argument.
+ (drop)
+ (i32.const 1)
+ (i32.add)
+ (suspend $t_suspend)
+
+ ;; add 1 to tag return value
+ (i32.const 1)
+ (i32.add)
+ )
+ (elem declare func $f1)
+
+ (func $g0 (type $ft0)
+ ;; Just a wrapper around $g1 to make sure that the involved continuation
+ ;; chains consist of more than one element.
+
+ (local.get 0)
+ (local.get 1)
+ (cont.new $ct0 (ref.func $g1))
+ (resume $ct0)
+ )
+ (elem declare func $g0)
+
+ (func $g1 (type $ft0)
+ (local $c (ref $ct1))
+
+ (block $handler (result i32 (ref $ct1))
+ ;; add 1 to argument received from f1 on switch
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ (ref.null $ct0) ;; passed as payload
+ (local.get 1) ;; resumed
+ (resume $ct0 (on $t_suspend $handler))
+ (unreachable) ;; f1 will suspend after the switch
+ )
+ ;; stash continuation created by suspend in $f1 aside
+ (local.set $c)
+ ;; increment value received from suspend in $f1
+ (i32.const 1)
+ (i32.add)
+ ;; ... and pass back to $f1
+ (local.get $c)
+ (resume $ct1)
+ ;; increment $f1's return value
+ (i32.const 1)
+ (i32.add)
+ )
+ (elem declare func $g1)
+
+ (func $entry (export "entry") (result i32)
+ (i32.const 100)
+ (ref.null $ct0)
+ (cont.new $ct0 (ref.func $f0))
+ (resume $ct0 (on $t_switch switch))
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 106))
diff --git a/tests/misc_testsuite/stack-switching/switch4.wast b/tests/misc_testsuite/stack-switching/switch4.wast
new file mode 100644
index 0000000000..c190542759
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch4.wast
@@ -0,0 +1,54 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! gc = true
+;;! stack_switching = true
+
+;; use cont.bind on continuation created by switch
+(module
+
+ (rec
+ (type $ft0 (func (param (ref null $ct1)) (result i32)))
+ (type $ct0 (cont $ft0))
+ (type $ft1 (func (param i32 (ref null $ct1)) (result i32)))
+ (type $ct1 (cont $ft1)))
+
+ (tag $t (result i32))
+
+ (func $f (type $ft1)
+ ;; add 1 to argument and pass to $g on switch
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ ;; prepare continuation
+ (cont.new $ct1 (ref.func $g))
+ (cont.bind $ct1 $ct0)
+ (switch $ct0 $t)
+ (drop) ;; we won't run $g to completion
+
+ ;; add 1 to payload received from $g
+ (i32.const 1)
+ (i32.add)
+ )
+ (elem declare func $f)
+
+ (func $g (type $ft1)
+ ;; add 1 to argument received from $f
+ (local.get 0)
+ (i32.const 1)
+ (i32.add)
+ (local.get 1)
+ (cont.bind $ct1 $ct0)
+ (switch $ct0 $t)
+ ;; $f never switches back to us
+ (unreachable)
+ )
+ (elem declare func $g)
+
+ (func $entry (export "entry") (result i32)
+ (i32.const 100)
+ (ref.null $ct1)
+ (cont.new $ct1 (ref.func $f))
+ (resume $ct1 (on $t switch))
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 103))
diff --git a/tests/misc_testsuite/stack-switching/switch5.wast b/tests/misc_testsuite/stack-switching/switch5.wast
new file mode 100644
index 0000000000..15660ea782
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch5.wast
@@ -0,0 +1,56 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; switch to continuation created by suspend
+(module
+
+ (type $ft0 (func (result i32)))
+ (type $ct0 (cont $ft0))
+
+ (type $ft1 (func (result i32)))
+ (type $ct1 (cont $ft1))
+
+ (type $ft2 (func (param i32 (ref $ct0)) (result i32)))
+ (type $ct2 (cont $ft2))
+
+ (tag $t_suspend (result i32 (ref $ct0)))
+ (tag $t_switch (result i32))
+
+ (global $c (mut (ref null $ct2)) (ref.null $ct2))
+
+
+ (func $f (result i32)
+ (suspend $t_suspend)
+ (drop) ;; drops continuation created by switch without running to completion
+
+ ;; We increment the switch payload and return it to our handler
+ (i32.const 1)
+ (i32.add)
+ )
+ (elem declare func $f)
+
+ (func $g (result i32)
+ (i32.const 100)
+ (global.get $c)
+ (switch $ct2 $t_switch)
+ ;; we never switch back here
+ (unreachable)
+ )
+
+ (elem declare func $g)
+
+ (func $entry (export "entry") (result i32)
+ (block $handler (result (ref $ct2))
+ (cont.new $ct0 (ref.func $f))
+ (resume $ct0 (on $t_suspend $handler))
+ (unreachable)
+ )
+ (global.set $c)
+
+ (cont.new $ct1 (ref.func $g))
+ (resume $ct1 (on $t_switch switch))
+
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 101))
diff --git a/tests/misc_testsuite/stack-switching/switch6.wast b/tests/misc_testsuite/stack-switching/switch6.wast
new file mode 100644
index 0000000000..b533f3dc50
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch6.wast
@@ -0,0 +1,39 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; suspend past a switch handler for same tag
+(module
+
+ (type $ft (func))
+ (type $ct (cont $ft))
+
+ (tag $t)
+
+ (func $f
+ (cont.new $ct (ref.func $g))
+ (resume $ct (on $t switch))
+ ;; $g will suspend and we will not come back here
+ (unreachable)
+ )
+ (elem declare func $f)
+
+ (func $g (type $ft)
+ (suspend $t)
+ ;; we will not come back here
+ (unreachable)
+ )
+ (elem declare func $g)
+
+ (func $entry (export "entry") (result i32)
+ (block $handler (result (ref $ct))
+ (cont.new $ct (ref.func $f))
+ (resume $ct (on $t switch) (on $t $handler))
+ ;; we will have a suspension
+ (unreachable)
+ )
+ (drop)
+ (i32.const 100)
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/switch7.wast b/tests/misc_testsuite/stack-switching/switch7.wast
new file mode 100644
index 0000000000..58a800d090
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch7.wast
@@ -0,0 +1,54 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; switch past a suspend handler for same tag
+(module
+
+ (type $ft0 (func (result i32)))
+ (type $ct0 (cont $ft0))
+
+ (type $ft1 (func (param i32) (result i32)))
+ (type $ct1 (cont $ft1))
+
+ (type $ft2 (func (param (ref $ct0)) (result i32)))
+ (type $ct2 (cont $ft2))
+
+ (tag $t (result i32))
+
+ (func $f (result i32)
+ (block $handler (result (ref $ct1))
+ (cont.new $ct0 (ref.func $g))
+ (resume $ct0 (on $t $handler))
+ ;; $g will switch, we won't come back here
+ (unreachable)
+ )
+ ;; we will not suspend
+ (unreachable)
+ )
+ (elem declare func $f)
+
+ (func $g (result i32)
+ (cont.new $ct2 (ref.func $h))
+ (switch $ct2 $t)
+ ;; we won't come back here
+ (unreachable)
+ )
+ (elem declare func $g)
+
+ (func $h (type $ft2)
+ (i32.const 100)
+ )
+ (elem declare func $h)
+
+ (func $entry (export "entry") (result i32)
+ (block $handler (result (ref $ct1))
+ (cont.new $ct0 (ref.func $f))
+ (resume $ct0 (on $t switch) (on $t $handler))
+ (return)
+ )
+ ;; we will not suspend
+ (unreachable)
+ )
+)
+(assert_return (invoke "entry" ) (i32.const 100))
diff --git a/tests/misc_testsuite/stack-switching/switch8.wast b/tests/misc_testsuite/stack-switching/switch8.wast
new file mode 100644
index 0000000000..329a759a03
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/switch8.wast
@@ -0,0 +1,38 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+
+;; try to switch to an already consumed continuation
+(module
+ (type $ft0 (func))
+ (type $ct0 (cont $ft0))
+
+ (type $ft1 (func (param (ref null $ct0))))
+ (type $ct1 (cont $ft1))
+
+ (func $print (import "spectest" "print_i32") (param i32))
+ (tag $t)
+
+
+ (func $f
+ (local $c (ref $ct1))
+ (ref.null $ct0) ;; argument to $g
+ (cont.new $ct1 (ref.func $g))
+ (local.tee $c)
+ (resume $ct1)
+
+ ;; this should fail, we already used the continuation
+ (local.get $c)
+ (switch $ct1 $t)
+ )
+ (elem declare func $f)
+
+ (func $g (type $ft1))
+ (elem declare func $g)
+
+ (func $entry (export "entry")
+ (cont.new $ct0 (ref.func $f))
+ (resume $ct0 (on $t switch))
+ )
+)
+(assert_trap (invoke "entry") "continuation already consumed")
diff --git a/tests/misc_testsuite/stack-switching/unhandled.wast b/tests/misc_testsuite/stack-switching/unhandled.wast
new file mode 100644
index 0000000000..63a9b92673
--- /dev/null
+++ b/tests/misc_testsuite/stack-switching/unhandled.wast
@@ -0,0 +1,26 @@
+;;! bulk_memory = true
+;;! function_references = true
+;;! stack_switching = true
+;; Test unhandled suspension
+
+(module
+ (type $ft (func))
+ (type $ct (cont $ft))
+ (tag $t)
+
+ (func $suspend
+ (suspend $t))
+ (elem declare func $suspend)
+
+ (func $unhandled-0 (export "unhandled-0")
+ (call $suspend))
+
+ (func $unhandled-1 (export "unhandled-1")
+ (resume $ct (cont.new $ct (ref.func $suspend))))
+)
+
+;; TODO(dhil): Suspending on the main thread currently causes an
+;; unrecoverable panic. Instead we should emit the UnhandledTrap trap
+;; code; once this has been implemented the below test should pass.
+;;(assert_suspension (invoke "unhandled-0") "unhandled")
+(assert_suspension (invoke "unhandled-1") "unhandled")
diff --git a/tests/wast.rs b/tests/wast.rs
index 62df646cd6..b12d41aa90 100644
--- a/tests/wast.rs
+++ b/tests/wast.rs
@@ -70,6 +70,10 @@ fn main() {
// leave the full combinatorial matrix and such to fuzz testing which
// configures many more settings than those configured here.
for test in tests {
+ if test.config.stack_switching() && !cfg!(all(target_arch = "x86_64", target_os = "linux"))
+ {
+ continue;
+ }
let collector = if test.test_uses_gc_types() {
Collector::DeferredReferenceCounting
} else {