Skip to content

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

Trap when a sync task blocks on a waitable set (#14606)

Alex Crichtond5d09ed095d4
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index 395b771f12..3bac73a46a 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -996,8 +996,11 @@ fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {
"event for {:?} on {set:?} no longer present; waiting again",
call.thread
);
- let state = store.concurrent_state_mut()?;
- return instance.wait_with_callback(state, call.thread, set);
+ return instance.wait_with_callback(
+ store.store_opaque_mut(),
+ call.thread,
+ set,
+ );
}
// When a thread yields it doesn't wait on a set and the
// event to be delivered isn't stored anywhere, and this is
@@ -2784,8 +2787,7 @@ impl Instance {
}
callback_code::WAIT => {
let set = get_set(store, set)?;
- let state = store.concurrent_state_mut()?;
- self.wait_with_callback(state, guest_thread, set)?;
+ self.wait_with_callback(store, guest_thread, set)?;
}
_ => bail!(Trap::UnsupportedCallbackCode),
}
@@ -2800,10 +2802,11 @@ impl Instance {
/// already available, or once one is published.
fn wait_with_callback(
self,
- state: &mut ConcurrentState,
+ store: &mut StoreOpaque,
guest_thread: QualifiedThreadId,
set: TableId<WaitableSet>,
) -> Result<()> {
+ let state = store.concurrent_state_mut()?;
// This thread's wait ends when that work item is handled in
// `handle_guest_call`.
state.get_mut(set)?.num_waiting += 1;
@@ -2826,11 +2829,23 @@ impl Instance {
return Ok(());
}
- // No event is immediately available, so register to be woken up when
- // one is published for this waitable set.
+ // No event is immediately available, so this thread is about to block.
+ // Like any other suspension point (e.g. the callback `EXIT` path), if
+ // a sync-typed call is in progress for this instance we must first
+ // switch to another ready thread of that instance, or trap if there is
+ // none.
+ let instance = store
+ .concurrent_state_mut()?
+ .get_mut(guest_thread.task)?
+ .instance;
+ store.switch_or_trap_if_may_not_suspend(instance)?;
+
+ // Register to be woken up when an event is published for this waitable
+ // set.
//
// Here we also set `GuestTask::wake_on_cancel` which allows
// `subtask.cancel` to interrupt the wait.
+ let state = store.concurrent_state_mut()?;
let old = state
.get_mut(guest_thread.thread)?
.wake_on_cancel
diff --git a/tests/misc_testsuite/component-model/async/sync-task-callback-wait-traps.wast b/tests/misc_testsuite/component-model/async/sync-task-callback-wait-traps.wast
new file mode 100644
index 0000000000..2aa21dbf3a
--- /dev/null
+++ b/tests/misc_testsuite/component-model/async/sync-task-callback-wait-traps.wast
@@ -0,0 +1,240 @@
+;;! component_model_async = true
+
+;; A sync-typed task `s` in instance $A blocks on a slow (async) host import.
+;; Here the only other thread of $A is the implicit thread of callback-lifted
+;; task `t`, which is ready (it returned YIELD). It is resumed, and its
+;; callback then returns WAIT on an empty waitable set, so it blocks. No thread
+;; of $A is ready anymore, so this must trap.
+(component
+ (import "host" (instance $host
+ (export "return-two-slowly" (func async (result s32)))
+ ))
+
+ (component $A
+ (import "host" (instance $host
+ (export "return-two-slowly" (func async (result s32)))
+ ))
+ (core func $slow (canon lower (func $host "return-two-slowly")))
+ (core func $task.cancel (canon task.cancel))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core module $m
+ (import "" "slow" (func $slow (result i32)))
+ (import "" "task.cancel" (func $task.cancel))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (global $ws (mut i32) (i32.const 0))
+
+ ;; callback-lifted: first YIELD, then WAIT on an empty set until
+ ;; cancelled.
+ (func (export "t") (result i32) (i32.const 1 (; YIELD ;)))
+ (func (export "t-cb") (param $ev i32) (param i32 i32) (result i32)
+ (if (i32.eq (local.get $ev) (i32.const 6 (; TASK_CANCELLED ;)))
+ (then (call $task.cancel) (return (i32.const 0 (; EXIT ;)))))
+ (if (i32.eqz (global.get $ws))
+ (then (global.set $ws (call $waitable-set.new))))
+ (i32.or (i32.const 2 (; WAIT ;))
+ (i32.shl (global.get $ws) (i32.const 4))))
+
+ ;; sync-typed, sync-lifted: blocks on the slow host import
+ (func (export "s") (result i32) (call $slow))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "slow" (func $slow))
+ (export "task.cancel" (func $task.cancel))
+ (export "waitable-set.new" (func $waitable-set.new))
+ ))
+ ))
+ (func (export "t") async
+ (canon lift (core func $i "t") async (callback (core func $i "t-cb"))))
+ (func (export "s") (result s32) (canon lift (core func $i "s")))
+ )
+ (instance $a (instantiate $A (with "host" (instance $host))))
+
+ (component $B
+ (import "a" (instance $a
+ (export "t" (func async))
+ (export "s" (func (result s32)))
+ ))
+ (core func $t (canon lower (func $a "t") async))
+ (core func $s (canon lower (func $a "s")))
+ (core func $subtask.cancel (canon subtask.cancel))
+ (core func $subtask.drop (canon subtask.drop))
+ (core module $m
+ (import "" "t" (func $t (result i32)))
+ (import "" "s" (func $s (result i32)))
+ (import "" "subtask.cancel"
+ (func $subtask.cancel (param i32) (result i32)))
+ (import "" "subtask.drop" (func $subtask.drop (param i32)))
+ (func (export "run") (result i32)
+ (local $rc i32)
+ ;; start `t`; it yields, so it's STARTED
+ (local.set $rc (call $t))
+ (if (i32.ne (i32.and (local.get $rc) (i32.const 0xf))
+ (i32.const 1 (; STARTED ;)))
+ (then unreachable))
+
+ ;; this should trap
+ call $s
+
+ ;; .. so shouldn't get there
+ unreachable
+ )
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "t" (func $t))
+ (export "s" (func $s))
+ (export "subtask.cancel" (func $subtask.cancel))
+ (export "subtask.drop" (func $subtask.drop))
+ ))
+ ))
+ (func (export "run") async (result s32) (canon lift (core func $i "run")))
+ )
+ (instance $b (instantiate $B (with "a" (instance $a))))
+ (export "run" (func $b "run"))
+)
+
+(assert_trap (invoke "run") "cannot block a synchronous task")
+
+
+;; Cross-instance variant: sync-typed `$A.s` sync-calls `$C.w`, which
+;; itself waits on the slow async host import. While `s` is blocked the
+;; only other thread of `$A` is callback task `t`, which returns WAIT on an
+;; empty set and so is not ready. This should trap.
+(component
+ (import "host" (instance $host
+ (export "return-two-slowly" (func async (result s32)))))
+
+ ;; $C: async-typed callback-lifted export `w` which waits for the async
+ ;; host import `slow` and then returns its result.
+ (component $C
+ (import "slow" (func $slow async (result s32)))
+ (core module $libc (memory (export "mem") 1))
+ (core instance $libc (instantiate $libc))
+ (core func $slow
+ (canon lower (func $slow) async (memory (core memory $libc "mem"))))
+ (core func $task.return (canon task.return (result u32)))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core func $waitable.join (canon waitable.join))
+ (core func $subtask.drop (canon subtask.drop))
+ (core module $m
+ (import "libc" "mem" (memory 1))
+ (import "" "slow" (func $slow (param i32) (result i32)))
+ (import "" "task.return" (func $task.return (param i32)))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))
+ (import "" "subtask.drop" (func $subtask.drop (param i32)))
+ (func (export "w") (result i32)
+ (local $rc i32) (local $s i32)
+ (local.set $rc (call $slow (i32.const 100)))
+ (if (i32.ne (i32.and (local.get $rc) (i32.const 0xf))
+ (i32.const 1 (; STARTED ;)))
+ (then unreachable))
+ (local.set $s (call $waitable-set.new))
+ (call $waitable.join
+ (i32.shr_u (local.get $rc) (i32.const 4)) (local.get $s))
+ (i32.or (i32.const 2 (; WAIT ;))
+ (i32.shl (local.get $s) (i32.const 4))))
+ (func (export "w-cb")
+ (param $ev i32) (param $h i32) (param $st i32) (result i32)
+ (if (i32.ne (local.get $ev) (i32.const 1 (; SUBTASK ;)))
+ (then unreachable))
+ (if (i32.ne (local.get $st) (i32.const 2 (; RETURNED ;)))
+ (then unreachable))
+ (call $waitable.join (local.get $h) (i32.const 0))
+ (call $subtask.drop (local.get $h))
+ (call $task.return (i32.load (i32.const 100)))
+ (i32.const 0 (; EXIT ;)))
+ )
+ (core instance $i (instantiate $m
+ (with "libc" (instance $libc))
+ (with "" (instance
+ (export "slow" (func $slow))
+ (export "task.return" (func $task.return))
+ (export "waitable-set.new" (func $waitable-set.new))
+ (export "waitable.join" (func $waitable.join))
+ (export "subtask.drop" (func $subtask.drop))
+ ))
+ ))
+ (func (export "w") async (result u32)
+ (canon lift (core func $i "w") async (callback (core func $i "w-cb"))))
+ )
+ (instance $c (instantiate $C
+ (with "slow" (func $host "return-two-slowly"))))
+
+ ;; $A: callback-lifted `t` (YIELD, then WAIT on an empty set until
+ ;; cancelled) and sync-typed `s` which sync-calls $C's `w`.
+ (component $A
+ (import "w" (func $w async (result u32)))
+ (core func $w (canon lower (func $w)))
+ (core func $task.cancel (canon task.cancel))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core module $m
+ (import "" "w" (func $w (result i32)))
+ (import "" "task.cancel" (func $task.cancel))
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (global $ws (mut i32) (i32.const 0))
+ (func (export "t") (result i32) (i32.const 1 (; YIELD ;)))
+ (func (export "t-cb") (param $ev i32) (param i32 i32) (result i32)
+ (if (i32.eq (local.get $ev) (i32.const 6 (; TASK_CANCELLED ;)))
+ (then (call $task.cancel) (return (i32.const 0 (; EXIT ;)))))
+ (if (i32.eqz (global.get $ws))
+ (then (global.set $ws (call $waitable-set.new))))
+ (i32.or (i32.const 2 (; WAIT ;))
+ (i32.shl (global.get $ws) (i32.const 4))))
+ (func (export "s") (result i32) (call $w))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "w" (func $w))
+ (export "task.cancel" (func $task.cancel))
+ (export "waitable-set.new" (func $waitable-set.new))
+ ))
+ ))
+ (func (export "t") async
+ (canon lift (core func $i "t") async (callback (core func $i "t-cb"))))
+ (func (export "s") (result u32) (canon lift (core func $i "s")))
+ )
+ (instance $a (instantiate $A (with "w" (func $c "w"))))
+
+ (component $B
+ (import "t" (func $t async))
+ (import "s" (func $s (result u32)))
+ (core func $t (canon lower (func $t) async))
+ (core func $s (canon lower (func $s)))
+ (core func $subtask.cancel (canon subtask.cancel))
+ (core func $subtask.drop (canon subtask.drop))
+ (core module $m
+ (import "" "t" (func $t (result i32)))
+ (import "" "s" (func $s (result i32)))
+ (import "" "subtask.cancel"
+ (func $subtask.cancel (param i32) (result i32)))
+ (import "" "subtask.drop" (func $subtask.drop (param i32)))
+ (func (export "run") (result i32)
+ (local $rc i32) (local $sub i32) (local $r i32)
+ (local.set $rc (call $t))
+ (if (i32.ne (i32.and (local.get $rc) (i32.const 0xf))
+ (i32.const 1 (; STARTED ;)))
+ (then unreachable))
+ (local.set $sub (i32.shr_u (local.get $rc) (i32.const 4)))
+ (local.set $r (call $s))
+ (drop (call $subtask.cancel (local.get $sub)))
+ (call $subtask.drop (local.get $sub))
+ (local.get $r))
+ )
+ (core instance $i (instantiate $m
+ (with "" (instance
+ (export "t" (func $t))
+ (export "s" (func $s))
+ (export "subtask.cancel" (func $subtask.cancel))
+ (export "subtask.drop" (func $subtask.drop))
+ ))
+ ))
+ (func (export "run") async (result u32) (canon lift (core func $i "run")))
+ )
+ (instance $b (instantiate $B
+ (with "t" (func $a "t"))
+ (with "s" (func $a "s"))))
+ (export "run" (func $b "run"))
+)
+(assert_trap (invoke "run") "cannot block a synchronous task")