A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Deliver pending cancellation of callback tasks more eagerly (#14708)
Alex Crichtonf438df1665c8
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 7a6c7556a4..fdd88816d5 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -737,9 +737,6 @@ enum GuestCallKind {},/// Indicates that a new guest task call is pending and may be executed/// using the specified closure.- ///- /// If the closure returns `Ok(Some(call))`, the `call` should be run- /// immediately using `handle_guest_call`.StartImplicit(Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>),StartExplicit(Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>),}@@ -1046,37 +1043,9 @@ fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {call.thread,);- let old_thread = store.set_thread(call.thread)?;- log::trace!(- "GuestCallKind::DeliverEvent: replaced {old_thread:?} with {:?} as current thread",- call.thread- );-- store.enter_instance(runtime_instance);-- let Some(callback) = store- .concurrent_state_mut()?- .get_mut(call.thread.task)?- .callback- .take()- else {- bail_bug!("guest task callback field not present")- };-- let code = callback(store, event, handle)?;-- store- .concurrent_state_mut()?- .get_mut(call.thread.task)?- .callback = Some(callback);-- store.exit_instance(runtime_instance)?;-- store.set_thread(old_thread)?;-- instance.handle_callback_code(store, call.thread, runtime_instance.index, code)?;-- log::trace!("GuestCallKind::DeliverEvent: restored {old_thread:?} as current thread");+ instance.handle_callback_code(store, call.thread, runtime_instance, |store| {+ invoke_callback(store, call.thread.task, event, handle)+ })?;}GuestCallKind::StartImplicit(fun) => {fun(store)?;@@ -1089,6 +1058,25 @@ fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {Ok(())}+/// Invoke the callback of the callback-lifted `task` with `event` and+/// `handle`, returning the callback code it produces.+fn invoke_callback(+ store: &mut dyn VMStore,+ task: TableId<GuestTask>,+ event: Event,+ handle: u32,+) -> Result<u32> {+ let Some(callback) = store.concurrent_state_mut()?.get_mut(task)?.callback.take() else {+ bail_bug!("guest task callback field not present")+ };++ let code = callback(store, event, handle)?;++ store.concurrent_state_mut()?.get_mut(task)?.callback = Some(callback);++ Ok(code)+}+impl<T> Store<T> {/// Convenience wrapper for [`StoreContextMut::run_concurrent`].pub async fn run_concurrent<R>(&mut self, fun: impl AsyncFnOnce(&Accessor<T>) -> R) -> Result<R>@@ -2793,18 +2781,52 @@ impl Instance {Ok(Some((event, Some((waitable, handle)))))}- /// Handle the `CallbackCode` returned from an async-lifted export or its- /// callback.+ /// Run `call` for the callback-lifted `guest_thread` and then handle the+ /// `CallbackCode` it returns.///- /// If this returns `Ok(Some(call))`, then `call` should be run immediately- /// using `handle_guest_call`.+ /// `call` runs with `guest_thread` as the current thread and with+ /// `runtime_instance` entered; both are restored before the final code is+ /// handled.fn handle_callback_code(self,- store: &mut StoreOpaque,+ store: &mut dyn VMStore,guest_thread: QualifiedThreadId,- runtime_instance: RuntimeComponentInstanceIndex,- code: u32,+ runtime_instance: RuntimeInstance,+ call: impl FnOnce(&mut dyn VMStore) -> Result<u32>,) -> Result<()> {+ let old_thread = store.set_thread(guest_thread)?;+ log::trace!(+ "callback-lifted call: replaced {old_thread:?} with {guest_thread:?} as current thread"+ );++ store.enter_instance(runtime_instance);++ let mut code = call(store)?;++ // Implement the spec behavior here of effectively ignoring the return+ // code if there's a pending cancellation for this task available.+ //+ // TODO: the spec doesn't validate anything here, but seems like the+ // waitable set may want to be validated?+ if matches!(+ unpack_callback_code(code).0,+ callback_code::YIELD | callback_code::WAIT+ ) && let Some((event, _)) = self.get_event(store, guest_thread.task, None, true)?+ {+ let (unpacked, set) = unpack_callback_code(code);+ log::trace!(+ "{guest_thread:?} returned callback code {unpacked} (set: {set}) with a pending \+ cancellation; delivering it via callback"+ );+ code = invoke_callback(store, guest_thread.task, event, 0)?;+ }++ store.exit_instance(runtime_instance)?;++ store.set_thread(old_thread)?;+ log::trace!("callback-lifted call: restored {old_thread:?} as current thread");++ let store = store.store_opaque_mut();let (code, set) = unpack_callback_code(code);log::trace!("received callback code from {guest_thread:?}: {code} (set: {set})");@@ -2815,7 +2837,7 @@ impl Instance {let get_set = |store: &mut StoreOpaque, handle| -> Result<_> {let set = store- .instance_state(self.runtime_instance(runtime_instance))+ .instance_state(runtime_instance).handle_table().waitable_set_rep(handle)?;@@ -2829,8 +2851,6 @@ impl Instance {task.exited = true;task.callback = None;- let runtime_instance = self.runtime_instance(runtime_instance);-// Since we're async-typed, returning control to our caller// won't help resolve any outstanding sync-typed call which// might be in progress, so we may need to switch or trap before@@ -2866,7 +2886,7 @@ impl Instance {// Push this thread onto the "low priority" queue so it runs// after any other threads have had a chance to run.state.push_low_priority(WorkItem::GuestCall {- instance: self.runtime_instance(runtime_instance),+ instance: runtime_instance,call,});}@@ -3055,35 +3075,13 @@ impl Instance {store,callee_instance.index,)?;- let old_thread = store.set_thread(guest_thread)?;- log::trace!(- "stackless call: replaced {old_thread:?} with {guest_thread:?} as current thread"- );-- store.enter_instance(callee_instance);+ self.handle_callback_code(store, guest_thread, callee_instance, |store| {+ let storage = call(store)?;- // SAFETY: See the documentation for `make_call` to review the- // contract we must uphold for `call` here.- //- // Per the contract described in the `stage_call`- // documentation, the `callee` pointer which `call` closes- // over must be valid.- let storage = call(store)?;-- store.exit_instance(callee_instance)?;-- store.set_thread(old_thread)?;- let state = store.concurrent_state_mut()?;- if let Some(t) = old_thread.guest() {- state.set_thread_running(t.thread)?;- }- log::trace!("stackless call: restored {old_thread:?} as current thread");-- // SAFETY: `wasmparser` will have validated that the callback- // function returns a `i32` result.- let code = unsafe { storage[0].assume_init() }.get_i32() as u32;-- self.handle_callback_code(store, guest_thread, callee_instance.index, code)+ // SAFETY: `wasmparser` will have validated that the+ // callback function returns a `i32` result.+ Ok(unsafe { storage[0].assume_init() }.get_i32() as u32)+ })}) as Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>} else {let token = StoreToken::new(store.as_context_mut());diff --git a/tests/misc_testsuite/component-model/async/cancel-delivered-before-exit.wast b/tests/misc_testsuite/component-model/async/cancel-delivered-before-exit.wastnew file mode 100644index 0000000000..c195ebcf8f--- /dev/null+++ b/tests/misc_testsuite/component-model/async/cancel-delivered-before-exit.wast@@ -0,0 +1,359 @@+;;! component_model_async = true++;; Per the spec's `canon_lift` callback loop:+;;+;; while code != CallbackCode.EXIT:+;; if thread.task.deliver_pending_cancel():+;; event = (EventCode.TASK_CANCELLED, 0, 0)+;; else:+;; ...+;; inst.exclusive_thread = None+;; ...+;;+;; when a callback-lifted task returns WAIT or YIELD while a cancellation is+;; pending, the callback is immediately re-invoked with TASK_CANCELLED without+;; releasing the instance's exclusive lock, so no other task may enter the+;; instance in between.++;; The start function of `$C.f` blocks in a synchronous `waitable-set.wait`,+;; holding the exclusive lock. Meanwhile `$A` starts `$C.g` (which must wait+;; for the lock, so is STARTING), unblocks `f`'s wait and synchronously cancels+;; `f`. When `f`'s start function then returns WAIT, TASK_CANCELLED must be+;; delivered to `f` before `g` starts.+(component+ (component $C+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (type $fut (future u32))+ (core func $future.read (canon future.read $fut async (memory (core memory $libc "mem"))))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "mem"))))+ (core func $waitable.join (canon waitable.join))+ (core func $task.cancel (canon task.cancel))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "task.cancel" (func $task.cancel))+ (import "" "task.return" (func $task.return))+ (global $g_ran (mut i32) (i32.const 0))++ (func (export "f") (param $fut i32) (result i32)+ (local $s i32)+ ;; async read (BLOCKED), then a synchronous `waitable-set.wait` from+ ;; within the start function: blocks while holding the exclusive lock+ (if (i32.ne (call $future.read (local.get $fut) (i32.const 0)) (i32.const -1))+ (then unreachable))+ (local.set $s (call $waitable-set.new))+ (call $waitable.join (local.get $fut) (local.get $s))+ (drop (call $waitable-set.wait (local.get $s) (i32.const 8)))+ (call $waitable.join (local.get $fut) (i32.const 0))+ ;; return WAIT on a fresh (empty) set+ (i32.or (i32.shl (call $waitable-set.new) (i32.const 4)) (i32.const 2)))+ (func (export "f-cb") (param $ev i32) (param i32 i32) (result i32)+ (if (i32.ne (local.get $ev) (i32.const 6)) (then unreachable))+ ;; cancellation is delivered before `g` can enter+ (if (global.get $g_ran) (then unreachable))+ (call $task.cancel)+ (i32.const 0))++ (func (export "g") (result i32)+ (global.set $g_ran (i32.const 1))+ (call $task.return)+ (i32.const 0))+ (func (export "g-cb") (param i32 i32 i32) (result i32) unreachable)+ )+ (core instance $i (instantiate $m+ (with "" (instance+ (export "future.read" (func $future.read))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.wait" (func $waitable-set.wait))+ (export "waitable.join" (func $waitable.join))+ (export "task.cancel" (func $task.cancel))+ (export "task.return" (func $task.return))+ ))+ ))+ (func (export "f") async (param "x" $fut)+ (canon lift (core func $i "f") async (callback (core func $i "f-cb"))))+ (func (export "g") async+ (canon lift (core func $i "g") async (callback (core func $i "g-cb"))))+ )+ (instance $c (instantiate $C))++ (component $A+ (type $fut (future u32))+ (import "f" (func $f async (param "x" $fut)))+ (import "g" (func $g async))+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (core func $f (canon lower (func $f) async (memory (core memory $libc "mem"))))+ (core func $g (canon lower (func $g) async (memory (core memory $libc "mem"))))+ (core func $future.new (canon future.new $fut))+ (core func $future.write (canon future.write $fut async (memory (core memory $libc "mem"))))+ (core func $subtask.cancel (canon subtask.cancel))+ (core func $subtask.drop (canon subtask.drop))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $task.return (canon task.return))+ (core module $m+ (import "libc" "mem" (memory 1))+ (import "" "f" (func $f (param i32) (result i32)))+ (import "" "g" (func $g (result i32)))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "subtask.cancel" (func $subtask.cancel (param i32) (result i32)))+ (import "" "subtask.drop" (func $subtask.drop (param i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "task.return" (func $task.return))+ (global $set (mut i32) (i32.const 0))++ (func (export "run") (result i32)+ (local $tmp i64) (local $r i32) (local $w i32)+ (local $st i32) (local $h1 i32) (local $h2 i32)+ (local.set $tmp (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $tmp)))+ (local.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))++ ;; f blocks in its start function => STARTED+ (local.set $st (call $f (local.get $r)))+ (if (i32.ne (i32.and (local.get $st) (i32.const 0xf)) (i32.const 1))+ (then unreachable))+ (local.set $h1 (i32.shr_u (local.get $st) (i32.const 4)))++ ;; g can't enter $C (f holds the exclusive lock) => STARTING+ (local.set $st (call $g))+ (if (i32.ne (i32.and (local.get $st) (i32.const 0xf)) (i32.const 0))+ (then unreachable))+ (local.set $h2 (i32.shr_u (local.get $st) (i32.const 4)))++ ;; unblock f's wait+ (i32.store (i32.const 0) (i32.const 42))+ (drop (call $future.write (local.get $w) (i32.const 0)))++ ;; synchronously cancel f => RETURN_CANCELLED (4)+ (if (i32.ne (call $subtask.cancel (local.get $h1)) (i32.const 4))+ (then unreachable))+ (call $subtask.drop (local.get $h1))++ (global.set $set (call $waitable-set.new))+ (call $waitable.join (local.get $h2) (global.get $set))+ (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))++ (func (export "cb") (param $ev i32) (param $h i32) (param $status i32) (result i32)+ (if (i32.ne (local.get $ev) (i32.const 1)) (then unreachable))+ ;; RETURNED (2) is terminal+ (if (i32.eq (local.get $status) (i32.const 2))+ (then+ (call $subtask.drop (local.get $h))+ (call $task.return)+ (return (i32.const 0))))+ (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))+ )+ (core instance $i (instantiate $m+ (with "libc" (instance $libc))+ (with "" (instance+ (export "f" (func $f))+ (export "g" (func $g))+ (export "future.new" (func $future.new))+ (export "future.write" (func $future.write))+ (export "subtask.cancel" (func $subtask.cancel))+ (export "subtask.drop" (func $subtask.drop))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "task.return" (func $task.return))+ ))+ ))+ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+ )+ (instance $a (instantiate $A+ (with "f" (func $c "f"))+ (with "g" (func $c "g"))))+ (export "run" (func $a "run"))+)++(assert_return (invoke "run"))++;; Same as above, but `f` blocks in its callback rather than its start+;; function, and the callback then returns YIELD.+(component+ (component $C+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (type $fut (future u32))+ (core func $future.read (canon future.read $fut async (memory (core memory $libc "mem"))))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.wait (canon waitable-set.wait (memory (core memory $libc "mem"))))+ (core func $waitable.join (canon waitable.join))+ (core func $task.cancel (canon task.cancel))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable-set.wait" (func $waitable-set.wait (param i32 i32) (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "task.cancel" (func $task.cancel))+ (import "" "task.return" (func $task.return))+ (global $g_ran (mut i32) (i32.const 0))+ (global $fut (mut i32) (i32.const 0))+ (global $f_state (mut i32) (i32.const 0))++ (func (export "f") (param $fut i32) (result i32)+ (global.set $fut (local.get $fut))+ ;; YIELD+ (i32.const 1))+ (func (export "f-cb") (param $ev i32) (param i32 i32) (result i32)+ (local $s i32)+ (if (i32.eqz (global.get $f_state))+ (then+ (global.set $f_state (i32.const 1))+ (if (i32.ne (local.get $ev) (i32.const 0)) (then unreachable))+ ;; async read (BLOCKED), then a synchronous `waitable-set.wait`+ ;; from within the callback: blocks while holding the exclusive+ ;; lock+ (if (i32.ne (call $future.read (global.get $fut) (i32.const 0)) (i32.const -1))+ (then unreachable))+ (local.set $s (call $waitable-set.new))+ (call $waitable.join (global.get $fut) (local.get $s))+ (drop (call $waitable-set.wait (local.get $s) (i32.const 8)))+ (call $waitable.join (global.get $fut) (i32.const 0))+ ;; YIELD+ (return (i32.const 1))))+ (if (i32.ne (local.get $ev) (i32.const 6)) (then unreachable))+ ;; cancellation is delivered before `g` can enter+ (if (global.get $g_ran) (then unreachable))+ (call $task.cancel)+ (i32.const 0))++ (func (export "g") (result i32)+ (global.set $g_ran (i32.const 1))+ (call $task.return)+ (i32.const 0))+ (func (export "g-cb") (param i32 i32 i32) (result i32) unreachable)+ )+ (core instance $i (instantiate $m+ (with "" (instance+ (export "future.read" (func $future.read))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.wait" (func $waitable-set.wait))+ (export "waitable.join" (func $waitable.join))+ (export "task.cancel" (func $task.cancel))+ (export "task.return" (func $task.return))+ ))+ ))+ (func (export "f") async (param "x" $fut)+ (canon lift (core func $i "f") async (callback (core func $i "f-cb"))))+ (func (export "g") async+ (canon lift (core func $i "g") async (callback (core func $i "g-cb"))))+ )+ (instance $c (instantiate $C))++ (component $A+ (type $fut (future u32))+ (import "f" (func $f async (param "x" $fut)))+ (import "g" (func $g async))+ (core module $libc (memory (export "mem") 1))+ (core instance $libc (instantiate $libc))+ (core func $f (canon lower (func $f) async (memory (core memory $libc "mem"))))+ (core func $g (canon lower (func $g) async (memory (core memory $libc "mem"))))+ (core func $future.new (canon future.new $fut))+ (core func $future.write (canon future.write $fut async (memory (core memory $libc "mem"))))+ (core func $subtask.cancel (canon subtask.cancel))+ (core func $subtask.drop (canon subtask.drop))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $task.return (canon task.return))+ (core module $m+ (import "libc" "mem" (memory 1))+ (import "" "f" (func $f (param i32) (result i32)))+ (import "" "g" (func $g (result i32)))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "subtask.cancel" (func $subtask.cancel (param i32) (result i32)))+ (import "" "subtask.drop" (func $subtask.drop (param i32)))+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))+ (import "" "waitable.join" (func $waitable.join (param i32 i32)))+ (import "" "task.return" (func $task.return))+ (global $set (mut i32) (i32.const 0))+ (global $w (mut i32) (i32.const 0))+ (global $h1 (mut i32) (i32.const 0))+ (global $state (mut i32) (i32.const 0))++ (func (export "run") (result i32)+ (local $tmp i64) (local $st i32)+ (local.set $tmp (call $future.new))+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))++ ;; f yields from its start function => STARTED+ (local.set $st (call $f (i32.wrap_i64 (local.get $tmp))))+ (if (i32.ne (i32.and (local.get $st) (i32.const 0xf)) (i32.const 1))+ (then unreachable))+ (global.set $h1 (i32.shr_u (local.get $st) (i32.const 4)))++ ;; YIELD so that f's callback runs and blocks. The spec lets either+ ;; yielding task resume first; this test expects f's, which yielded+ ;; first, to run before this callback is called again.+ (i32.const 1))++ (func (export "cb") (param $ev i32) (param $h i32) (param $status i32) (result i32)+ (local $st i32) (local $h2 i32)+ (if (i32.eqz (global.get $state))+ (then+ (global.set $state (i32.const 1))+ (if (i32.ne (local.get $ev) (i32.const 0)) (then unreachable))++ ;; g can't enter $C (f holds the exclusive lock) => STARTING+ (local.set $st (call $g))+ (if (i32.ne (i32.and (local.get $st) (i32.const 0xf)) (i32.const 0))+ (then unreachable))+ (local.set $h2 (i32.shr_u (local.get $st) (i32.const 4)))++ ;; unblock f's wait+ (i32.store (i32.const 0) (i32.const 42))+ (drop (call $future.write (global.get $w) (i32.const 0)))++ ;; synchronously cancel f => RETURN_CANCELLED (4)+ (if (i32.ne (call $subtask.cancel (global.get $h1)) (i32.const 4))+ (then unreachable))+ (call $subtask.drop (global.get $h1))++ (global.set $set (call $waitable-set.new))+ (call $waitable.join (local.get $h2) (global.get $set))+ (return (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))))++ (if (i32.ne (local.get $ev) (i32.const 1)) (then unreachable))+ ;; RETURNED (2) is terminal+ (if (i32.eq (local.get $status) (i32.const 2))+ (then+ (call $subtask.drop (local.get $h))+ (call $task.return)+ (return (i32.const 0))))+ (i32.or (i32.shl (global.get $set) (i32.const 4)) (i32.const 2)))+ )+ (core instance $i (instantiate $m+ (with "libc" (instance $libc))+ (with "" (instance+ (export "f" (func $f))+ (export "g" (func $g))+ (export "future.new" (func $future.new))+ (export "future.write" (func $future.write))+ (export "subtask.cancel" (func $subtask.cancel))+ (export "subtask.drop" (func $subtask.drop))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "task.return" (func $task.return))+ ))+ ))+ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+ )+ (instance $a (instantiate $A+ (with "f" (func $c "f"))+ (with "g" (func $c "g"))))+ (export "run" (func $a "run"))+)++(assert_return (invoke "run"))