Skip to content

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

Trap calls to `task.{return,cancel}` for sync lifts (#14560)

Alex Crichton8b9c868efb91
diff --git a/crates/c-api/include/wasmtime/trap.h b/crates/c-api/include/wasmtime/trap.h
index f2903fc307..3d6c29db5f 100644
--- a/crates/c-api/include/wasmtime/trap.h
+++ b/crates/c-api/include/wasmtime/trap.h
@@ -156,6 +156,9 @@ enum wasmtime_trap_code_enum {
/// Guest attempted to lift a stream after being notified that the writable
/// end was dropped.
WASMTIME_TRAP_LIFT_DROPPED_STREAM = 53,
+ /// `task.return` or `task.cancel` was called by a task which was not
+ /// created by an `async`-lifted export.
+ WASMTIME_TRAP_CODE_TASK_RETURN_OR_CANCEL_SYNC_LIFTED = 54,
};
/**
diff --git a/crates/c-api/src/trap.rs b/crates/c-api/src/trap.rs
index 2582efd5f9..9dd577506b 100644
--- a/crates/c-api/src/trap.rs
+++ b/crates/c-api/src/trap.rs
@@ -60,6 +60,7 @@ const _: () = {
assert!(Trap::WriteToDroppedStream as u8 == 51);
assert!(Trap::WriteToDroppedFuture as u8 == 52);
assert!(Trap::LiftDroppedStream as u8 == 53);
+ assert!(Trap::TaskReturnOrCancelSyncLifted as u8 == 54);
};
#[repr(C)]
diff --git a/crates/environ/src/trap_encoding.rs b/crates/environ/src/trap_encoding.rs
index feae63c0f8..a72ef5b4bb 100644
--- a/crates/environ/src/trap_encoding.rs
+++ b/crates/environ/src/trap_encoding.rs
@@ -295,6 +295,10 @@ generate_trap_type! {
/// writable end was dropped.
LiftDroppedStream = "cannot lift stream after being notified that the writable end dropped",
+ /// `task.return` or `task.cancel` was called by a task which was not
+ /// created by an `async`-lifted export.
+ TaskReturnOrCancelSyncLifted = "`task.return` or `task.cancel` called by a task which was not lifted with `async`",
+
// if adding a variant here be sure to update `trap.rs` and `trap.h` as
// mentioned above
}
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 033c9fc2b7..a5d5b9ced1 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -1968,7 +1968,7 @@ impl StoreOpaque {
None,
callee,
callee_async_typed,
- true,
+ false,
)?;
Instance::from_wasmtime(self, callee.instance).add_guest_thread_to_instance_table(
@@ -3720,6 +3720,9 @@ impl Instance {
) -> Result<()> {
let guest_thread = store.current_guest_thread()?;
let state = store.concurrent_state_mut()?;
+ if !state.get_mut(guest_thread.task)?.async_lifted {
+ bail!(Trap::TaskReturnOrCancelSyncLifted);
+ }
let lift = state
.get_mut(guest_thread.task)?
.lift_result
@@ -3767,6 +3770,9 @@ impl Instance {
let guest_thread = store.current_guest_thread()?;
let state = store.concurrent_state_mut()?;
let task = state.get_mut(guest_thread.task)?;
+ if !task.async_lifted {
+ bail!(Trap::TaskReturnOrCancelSyncLifted);
+ }
if !task.cancel_request_delivered {
bail!(Trap::TaskCancelNotCancelled);
}
diff --git a/tests/misc_testsuite/component-model/async/task-return-from-sync-lift.wast b/tests/misc_testsuite/component-model/async/task-return-from-sync-lift.wast
new file mode 100644
index 0000000000..046d7c0352
--- /dev/null
+++ b/tests/misc_testsuite/component-model/async/task-return-from-sync-lift.wast
@@ -0,0 +1,90 @@
+;;! component_model_async = true
+
+;; `task.return` and `task.cancel` may only be called from tasks created by an
+;; `async`-lifted export; calling them from a sync-lifted export traps.
+
+;; A sync-lifted export calls `task.return`, called from the host.
+(component
+ (core func $task.return (canon task.return))
+ (core module $m
+ (import "" "task.return" (func $task.return))
+ (func (export "f")
+ (call $task.return)))
+ (core instance $i (instantiate $m
+ (with "" (instance (export "task.return" (func $task.return))))))
+ (func (export "f") async (canon lift (core func $i "f")))
+)
+(assert_trap (invoke "f") "not lifted with `async`")
+
+;; A sync-lifted export calls `task.return`, called through an async-lowered
+;; import.
+(component
+ (component $A
+ (core func $task.return (canon task.return))
+ (core module $a
+ (import "" "task.return" (func $task.return))
+ (func (export "f")
+ (call $task.return)))
+ (core instance $a (instantiate $a
+ (with "" (instance (export "task.return" (func $task.return))))))
+ (func (export "f") async (canon lift (core func $a "f")))
+ )
+ (component $B
+ (import "a" (instance $a (export "f" (func async))))
+ (core func $f (canon lower (func $a "f") async))
+ (core module $b
+ (import "" "f" (func $f (result i32)))
+ (func (export "run") (drop (call $f))))
+ (core instance $b (instantiate $b
+ (with "" (instance (export "f" (func $f))))))
+ (func (export "run") (canon lift (core func $b "run")))
+ )
+ (instance $a (instantiate $A))
+ (instance $b (instantiate $B (with "a" (instance $a))))
+ (export "run" (func $b "run"))
+)
+(assert_trap (invoke "run") "not lifted with `async`")
+
+;; A sync-lifted export calls `task.return`, called through a sync-lowered
+;; import from an async-lifted (callback) export.
+(component
+ (component $A
+ (core func $task.return (canon task.return))
+ (core module $a
+ (import "" "task.return" (func $task.return))
+ (func (export "f")
+ (call $task.return)))
+ (core instance $a (instantiate $a
+ (with "" (instance (export "task.return" (func $task.return))))))
+ (func (export "f") (canon lift (core func $a "f")))
+ )
+ (component $B
+ (import "a" (instance $a (export "f" (func))))
+ (core func $f (canon lower (func $a "f")))
+ (core module $b
+ (import "" "f" (func $f))
+ (func (export "run") (result i32) (call $f) (i32.const 0 (; EXIT ;)))
+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable))
+ (core instance $b (instantiate $b
+ (with "" (instance (export "f" (func $f))))))
+ (func (export "run") async
+ (canon lift (core func $b "run") async (callback (core func $b "cb"))))
+ )
+ (instance $a (instantiate $A))
+ (instance $b (instantiate $B (with "a" (instance $a))))
+ (export "run" (func $b "run"))
+)
+(assert_trap (invoke "run") "not lifted with `async`")
+
+;; A sync-lifted export calls `task.cancel`.
+(component
+ (core func $task.cancel (canon task.cancel))
+ (core module $m
+ (import "" "task.cancel" (func $task.cancel))
+ (func (export "f")
+ (call $task.cancel)))
+ (core instance $i (instantiate $m
+ (with "" (instance (export "task.cancel" (func $task.cancel))))))
+ (func (export "f") async (canon lift (core func $i "f")))
+)
+(assert_trap (invoke "f") "not lifted with `async`")