Skip to content

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

Migrate some tests out of `exceptions.rs` to `*.wast` (#15039)

Alex Crichton9927ce1a05b1
diff --git a/tests/all/exceptions.rs b/tests/all/exceptions.rs
index 812ecdddce..6d500271a7 100644
--- a/tests/all/exceptions.rs
+++ b/tests/all/exceptions.rs
@@ -3,200 +3,6 @@ use std::sync::atomic::{AtomicBool, Ordering::Relaxed};
use wasmtime::*;
use wasmtime_test_macros::wasmtime_test;
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn basic_throw(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e0 (param i32 i64))
-
- (func $throw (param i32 i64)
- (throw $e0 (local.get 0) (local.get 1)))
-
- (func $catch (export "catch") (param i32 i64) (result i32 i64)
-
- (block $b (result i32 i64)
- (try_table (result i32 i64)
- (catch $e0 $b)
- (call $throw (local.get 0) (local.get 1))
- (i32.const 42)
- (i64.const 100)))))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let func = instance.get_func(&mut store, "catch").unwrap();
- let mut results = [Val::I32(0), Val::I64(0)];
- func.call(&mut store, &[Val::I32(1), Val::I64(2)], &mut results[..])?;
- assert!(matches!(results[0], Val::I32(1)));
- assert!(matches!(results[1], Val::I64(2)));
-
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn try_table_fallthrough_with_multi_value_results(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param i32))
- (func $callee (result i32) (i32.const 9))
- (func (export "f") (result i32)
- (block $h (result i32)
- (try_table (result i32 i32 i32 i32 i32 i32) (catch $e $h)
- (i32.const 1)
- (i32.const 2)
- (i32.const 3)
- (i32.const 4)
- (i32.const 5)
- (call $callee))
- drop drop drop drop drop)))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let f = instance.get_typed_func::<(), i32>(&mut store, "f")?;
- assert_eq!(f.call(&mut store, ())?, 1);
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn try_table_exception_with_multi_value_payload(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param i32 i32 i32 i32 i32 i32))
-
- (func $throw (result i32 i32 i32 i32 i32 i32)
- (throw $e
- (i32.const 1)
- (i32.const 2)
- (i32.const 3)
- (i32.const 4)
- (i32.const 5)
- (i32.const 6)))
-
- (func (export "f") (result i32)
- (block $handler (result i32 i32 i32 i32 i32 i32)
- (try_table
- (result i32 i32 i32 i32 i32 i32)
- (catch $e $handler)
- (call $throw)))
- drop drop drop drop drop))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let f = instance.get_typed_func::<(), i32>(&mut store, "f")?;
- assert_eq!(f.call(&mut store, ())?, 1);
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn try_table_branch_and_fallthrough_with_multi_value_results(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param i32))
-
- (func (export "f") (param $branch i32) (result i32)
- (block $handler (result i32)
- (try_table
- (result i32 i32 i32 i32 i32 i32)
- (catch $e $handler)
- (i32.const 1)
- (i32.const 2)
- (i32.const 3)
- (i32.const 4)
- (i32.const 5)
- (i32.const 6)
- (local.get $branch)
- br_if 0)
- drop drop drop drop drop)))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let f = instance.get_typed_func::<i32, i32>(&mut store, "f")?;
- assert_eq!(f.call(&mut store, 0)?, 1);
- assert_eq!(f.call(&mut store, 1)?, 1);
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn try_table_unreachable_fallthrough_with_multi_value_results(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param i32))
- (func $callee)
-
- (func (export "f") (result i32)
- (block $handler (result i32)
- (try_table
- (result i32 i32 i32 i32 i32 i32)
- (catch $e $handler)
- (call $callee)
- (i32.const 1)
- (i32.const 2)
- (i32.const 3)
- (i32.const 4)
- (i32.const 5)
- (i32.const 6)
- br 0)
- drop drop drop drop drop)))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let f = instance.get_typed_func::<(), i32>(&mut store, "f")?;
- assert_eq!(f.call(&mut store, ())?, 1);
- Ok(())
-}
-
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn try_table_after_return(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
- let module = Module::new(
- &engine,
- r#"(module
- (func (export "f")
- (return)
- (try_table (catch_all 0))))"#,
- )?;
- let instance = Instance::new(&mut store, &module, &[])?;
- let f = instance.get_typed_func::<(), ()>(&mut store, "f")?;
- f.call(&mut store, ())?;
- Ok(())
-}
-
#[wasmtime_test(wasm_features(exceptions))]
#[cfg_attr(miri, ignore)]
fn dynamic_tags(config: &mut Config) -> Result<()> {
@@ -257,58 +63,6 @@ fn dynamic_tags(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test(wasm_features(exceptions))]
-#[cfg_attr(miri, ignore)]
-fn nested_handler_scopes(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $outer)
- (tag $inner)
-
- (func $throw_outer
- (throw $outer))
-
- (func $throw_inner
- (throw $inner))
-
- ;; While both handlers are active, the inner tag does not match and
- ;; lookup continues to the outer handler.
- (func (export "nested") (result i32)
- (block $outer_handler
- (try_table (catch $outer $outer_handler)
- (block $inner_handler
- (try_table (catch $inner $inner_handler)
- (call $throw_outer)))))
- (i32.const 1))
-
- ;; After the inner try_table ends, its handler is no longer active.
- ;; The outer catch_all handles the throw instead.
- (func (export "after") (result i32)
- (block $stale_inner_handler
- (block $outer_handler
- (try_table (catch_all $outer_handler)
- (try_table (catch $inner $stale_inner_handler)
- (nop))
- (call $throw_inner)))
- (return (i32.const 1)))
- (i32.const 2)))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let nested = instance.get_typed_func::<(), i32>(&mut store, "nested")?;
- let after = instance.get_typed_func::<(), i32>(&mut store, "after")?;
- assert_eq!(nested.call(&mut store, ())?, 1);
- assert_eq!(after.call(&mut store, ())?, 1);
-
- Ok(())
-}
-
#[wasmtime_test(wasm_features(exceptions))]
#[cfg_attr(miri, ignore)]
fn exception_escape_to_host(config: &mut Config) -> Result<()> {
@@ -401,44 +155,6 @@ fn funcref_exception_payload_escape_to_host(config: &mut Config) -> Result<()> {
Ok(())
}
-#[wasmtime_test(collectors(All), wasm_features(exceptions, reference_types))]
-#[cfg_attr(miri, ignore)]
-fn caught_funcref_payload(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let mut store = Store::new(&engine, ());
-
- let module = Module::new(
- &engine,
- r#"
- (module
- (tag $e (param funcref i32))
-
- (func $throw (param funcref)
- (throw $e (local.get 0) (i32.const 42)))
-
- (func (export "catch") (param funcref) (result funcref i32)
- (block $handler (result funcref i32)
- (try_table (result funcref i32) (catch $e $handler)
- (call $throw (local.get 0))
- (ref.null func)
- (i32.const 0)))))
- "#,
- )?;
-
- let instance = Instance::new(&mut store, &module, &[])?;
- let catch = instance.get_func(&mut store, "catch").unwrap();
- let expected = Func::wrap(&mut store, || 126_i32);
- let mut results = [Val::null_func_ref(), Val::I32(0)];
- catch.call(&mut store, &[Val::FuncRef(Some(expected))], &mut results)?;
-
- let actual = results[0].unwrap_funcref().unwrap();
- let actual = actual.typed::<(), i32>(&store)?;
- assert_eq!(actual.call(&mut store, ())?, 126);
- assert_eq!(results[1].unwrap_i32(), 42);
-
- Ok(())
-}
-
#[wasmtime_test(
collectors(Copying, DeferredReferenceCounting),
wasm_features(exceptions, reference_types)
@@ -972,24 +688,3 @@ fn store_pending_exnref_has_write_barrier(config: &mut Config) -> wasmtime::Resu
Ok(())
}
-
-#[wasmtime_test(wasm_features(exceptions, reference_types))]
-#[cfg_attr(miri, ignore)]
-fn exnref_local_defaults_to_null(config: &mut Config) -> Result<()> {
- let engine = Engine::new(config)?;
- let module = Module::new(
- &engine,
- r#"
- (module
- (func (export "run") (result i32)
- (local exnref)
- local.get 0
- ref.is_null))
- "#,
- )?;
- let mut store = Store::new(&engine, ());
- let instance = Instance::new(&mut store, &module, &[])?;
- let run = instance.get_typed_func::<(), i32>(&mut store, "run")?;
- assert_eq!(run.call(&mut store, ())?, 1);
- Ok(())
-}
diff --git a/tests/misc_testsuite/throw-payloads.wast b/tests/misc_testsuite/throw-payloads.wast
index f371510113..526b3a9295 100644
--- a/tests/misc_testsuite/throw-payloads.wast
+++ b/tests/misc_testsuite/throw-payloads.wast
@@ -44,3 +44,184 @@
(throw $fref (ref.func $f))))
(assert_exception (invoke "throw-funcref"))
+
+;; A caught payload is delivered to the handler's block.
+(module
+ (tag $e0 (param i32 i64))
+
+ (func $throw (param i32 i64)
+ (throw $e0 (local.get 0) (local.get 1)))
+
+ (func (export "catch") (param i32 i64) (result i32 i64)
+ (block $b (result i32 i64)
+ (try_table (result i32 i64) (catch $e0 $b)
+ (call $throw (local.get 0) (local.get 1))
+ (i32.const 42)
+ (i64.const 100)))))
+
+(assert_return (invoke "catch" (i32.const 1) (i64.const 2))
+ (i32.const 1) (i64.const 2))
+
+;; Fallthrough out of a `try_table` with many results.
+(module
+ (tag $e (param i32))
+ (func $callee (result i32) (i32.const 9))
+ (func (export "f") (result i32)
+ (block $h (result i32)
+ (try_table (result i32 i32 i32 i32 i32 i32) (catch $e $h)
+ (i32.const 1)
+ (i32.const 2)
+ (i32.const 3)
+ (i32.const 4)
+ (i32.const 5)
+ (call $callee))
+ drop drop drop drop drop)))
+
+(assert_return (invoke "f") (i32.const 1))
+
+;; Exception caught with a many-value payload.
+(module
+ (tag $e (param i32 i32 i32 i32 i32 i32))
+
+ (func $throw (result i32 i32 i32 i32 i32 i32)
+ (throw $e
+ (i32.const 1)
+ (i32.const 2)
+ (i32.const 3)
+ (i32.const 4)
+ (i32.const 5)
+ (i32.const 6)))
+
+ (func (export "f") (result i32)
+ (block $handler (result i32 i32 i32 i32 i32 i32)
+ (try_table (result i32 i32 i32 i32 i32 i32) (catch $e $handler)
+ (call $throw)))
+ drop drop drop drop drop))
+
+(assert_return (invoke "f") (i32.const 1))
+
+;; Both a branch and a fallthrough out of a many-result `try_table`.
+(module
+ (tag $e (param i32))
+
+ (func (export "f") (param $branch i32) (result i32)
+ (block $handler (result i32)
+ (try_table (result i32 i32 i32 i32 i32 i32) (catch $e $handler)
+ (i32.const 1)
+ (i32.const 2)
+ (i32.const 3)
+ (i32.const 4)
+ (i32.const 5)
+ (i32.const 6)
+ (local.get $branch)
+ br_if 0)
+ drop drop drop drop drop)))
+
+(assert_return (invoke "f" (i32.const 0)) (i32.const 1))
+(assert_return (invoke "f" (i32.const 1)) (i32.const 1))
+
+;; Unreachable fallthrough after an unconditional branch out of a many-result
+;; `try_table`.
+(module
+ (tag $e (param i32))
+ (func $callee)
+
+ (func (export "f") (result i32)
+ (block $handler (result i32)
+ (try_table (result i32 i32 i32 i32 i32 i32) (catch $e $handler)
+ (call $callee)
+ (i32.const 1)
+ (i32.const 2)
+ (i32.const 3)
+ (i32.const 4)
+ (i32.const 5)
+ (i32.const 6)
+ br 0)
+ drop drop drop drop drop)))
+
+(assert_return (invoke "f") (i32.const 1))
+
+;; A `try_table` in unreachable code after a `return`.
+(module
+ (func (export "f")
+ (return)
+ (try_table (catch_all 0))))
+
+(assert_return (invoke "f"))
+
+;; Handler lookup across nested and already-exited `try_table` scopes.
+(module
+ (tag $outer)
+ (tag $inner)
+
+ (func $throw_outer
+ (throw $outer))
+
+ (func $throw_inner
+ (throw $inner))
+
+ ;; While both handlers are active, the inner tag does not match and
+ ;; lookup continues to the outer handler.
+ (func (export "nested") (result i32)
+ (block $outer_handler
+ (try_table (catch $outer $outer_handler)
+ (block $inner_handler
+ (try_table (catch $inner $inner_handler)
+ (call $throw_outer)))))
+ (i32.const 1))
+
+ ;; After the inner try_table ends, its handler is no longer active.
+ ;; The outer catch_all handles the throw instead.
+ (func (export "after") (result i32)
+ (block $stale_inner_handler
+ (block $outer_handler
+ (try_table (catch_all $outer_handler)
+ (try_table (catch $inner $stale_inner_handler)
+ (nop))
+ (call $throw_inner)))
+ (return (i32.const 1)))
+ (i32.const 2)))
+
+(assert_return (invoke "nested") (i32.const 1))
+(assert_return (invoke "after") (i32.const 1))
+
+;; An `exnref` local is initialized to null.
+(module
+ (func (export "run") (result i32)
+ (local exnref)
+ local.get 0
+ ref.is_null))
+
+(assert_return (invoke "run") (i32.const 1))
+
+;; A caught funcref payload is still callable.
+(module
+ (type $ret_i32 (func (result i32)))
+ (tag $e (param funcref i32))
+ (table 1 funcref)
+
+ (func $payload (result i32) (i32.const 126))
+ (elem declare func $payload)
+
+ (func $throw (param funcref)
+ (throw $e (local.get 0) (i32.const 42)))
+
+ (func $catch (param funcref) (result funcref i32)
+ (block $handler (result funcref i32)
+ (try_table (result funcref i32) (catch $e $handler)
+ (call $throw (local.get 0))
+ (ref.null func)
+ (i32.const 0))))
+
+ ;; Catch a funcref payload, then call it to make sure it's still valid.
+ (func (export "run") (result i32 i32)
+ (local $f funcref)
+ (local $i i32)
+ (call $catch (ref.func $payload))
+ (local.set $i)
+ (local.set $f)
+ (table.set (i32.const 0) (local.get $f))
+ (call_indirect (type $ret_i32) (i32.const 0))
+ (local.get $i)))
+
+(assert_return (invoke "run") (i32.const 126) (i32.const 42))