A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix some issues with waitable sets and stealing events (#14558)
Alex Crichtonbb433ff09464
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 7ddeaf13bf..033c9fc2b7 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -980,9 +980,32 @@ pub(crate) fn poll_and_block<R: Send + Sync + 'static>(fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {match call.kind {GuestCallKind::DeliverEvent { instance, set } => {+ // This thread's wait on `set`, if any, which started in+ // `Instance::wait_with_callback`, ends here.+ if let Some(set) = set {+ store.concurrent_state_mut()?.get_mut(set)?.stop_waiting()?;+ }let (event, waitable) = match instance.get_event(store, call.thread.task, set, true)? {Some(pair) => pair,- None => bail_bug!("delivering non-present event"),+ None => match set {+ // The event which woke this thread up was taken by another+ // thread (e.g. via `waitable-set.poll`) or its waitable was+ // removed from the set before this thread had a chance to+ // run. Regardless, keep waiting.+ Some(set) => {+ log::trace!(+ "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);+ }+ // When a thread yields it doesn't wait on a set and the+ // event to be delivered isn't stored anywhere, and this is+ // just handling that case so indicate that the yield is+ // complete.+ None => (Event::None, None),+ },};let state = store.concurrent_state_mut()?;let task = state.get_mut(call.thread.task)?;@@ -2358,6 +2381,11 @@ impl StoreOpaque {CurrentThread::None};+ let waiting_set = match &reason {+ SuspendReason::Waiting { set, .. } => Some(*set),+ _ => None,+ };+let suspend_reason = &mut self.concurrent_state_mut()?.suspend_reason;assert!(suspend_reason.is_none());*suspend_reason = Some(reason);@@ -2368,8 +2396,18 @@ impl StoreOpaque {return Err(format_err!("future dropped"));}+ // Count this thread as waiting on `waiting_set`, if any, for as long as+ // it's suspended.+ if let Some(set) = waiting_set {+ self.concurrent_state_mut()?.get_mut(set)?.num_waiting += 1;+ }+self.with_blocking(|_, cx| cx.suspend(StoreFiberYield::ReleaseStore))?;+ if let Some(set) = waiting_set {+ self.concurrent_state_mut()?.get_mut(set)?.stop_waiting()?;+ }+if save_and_restore_thread {self.set_thread(old_guest_thread)?;}@@ -2725,16 +2763,12 @@ impl Instance {bail_bug!("thread unexpectedly had wake_on_cancel set");}- let task = state.get_mut(guest_thread.task)?;- // If an `Event::Cancelled` is pending, we'll deliver that;- // otherwise, we'll deliver `Event::None`. Note that- // `GuestTask::event` is only ever set to one of those two- // `Event` variants.- if let Some(event) = task.event {- assert!(matches!(event, Event::None | Event::Cancelled));- } else {- task.event = Some(Event::None);- }+ // If an `Event::Cancelled` is pending in `GuestTask::event`+ // when this call is delivered, we'll deliver that; otherwise,+ // we'll deliver `Event::None`. Note that the `Event::None` is+ // deliberately not stored in `GuestTask::event` since that's+ // shared by all threads of the task and the `Event::None` is+ // only for this thread.let call = GuestCall {thread: guest_thread,kind: GuestCallKind::DeliverEvent {@@ -2752,44 +2786,7 @@ impl Instance {callback_code::WAIT => {let set = get_set(store, set)?;let state = store.concurrent_state_mut()?;-- if state.get_mut(guest_thread.task)?.event.is_some()- || !state.get_mut(set)?.ready.is_empty()- {- // An event is immediately available; deliver it ASAP.- state.push_high_priority(WorkItem::GuestCall {- instance: self.runtime_instance(runtime_instance),- call: GuestCall {- thread: guest_thread,- kind: GuestCallKind::DeliverEvent {- instance: self,- set: Some(set),- },- },- });- } else {- // No event is immediately available.- //- // We're waiting, so 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 old = state- .get_mut(guest_thread.thread)?- .wake_on_cancel- .replace(WakeOnCancel::Waiting(set));- if !old.is_none() {- bail_bug!("thread unexpectedly had wake_on_cancel set");- }- let old = state- .get_mut(set)?- .waiting- .insert(guest_thread, WaitMode::Callback(self));- if !old.is_none() {- bail_bug!("set's waiting set already had this thread registered");- }- }+ self.wait_with_callback(state, guest_thread, set)?;}_ => bail!(Trap::UnsupportedCallbackCode),}@@ -2797,6 +2794,61 @@ impl Instance {Ok(())}+ /// Start waiting on `set` with `guest_thread`, which uses a callback.+ ///+ /// The thread will receive an event from `set` via a+ /// `GuestCallKind::DeliverEvent` work item, either immediately if one is+ /// already available, or once one is published.+ fn wait_with_callback(+ self,+ state: &mut ConcurrentState,+ guest_thread: QualifiedThreadId,+ set: TableId<WaitableSet>,+ ) -> Result<()> {+ // This thread's wait ends when that work item is handled in+ // `handle_guest_call`.+ state.get_mut(set)?.num_waiting += 1;++ if state.get_mut(guest_thread.task)?.event.is_some()+ || !state.get_mut(set)?.ready.is_empty()+ {+ // An event is immediately available; deliver it ASAP.+ let instance = state.get_mut(guest_thread.task)?.instance;+ state.push_high_priority(WorkItem::GuestCall {+ instance,+ call: GuestCall {+ thread: guest_thread,+ kind: GuestCallKind::DeliverEvent {+ instance: self,+ set: Some(set),+ },+ },+ });+ return Ok(());+ }++ // No event is immediately available, so register to be woken up when+ // one is published for this waitable set.+ //+ // Here we also set `GuestTask::wake_on_cancel` which allows+ // `subtask.cancel` to interrupt the wait.+ let old = state+ .get_mut(guest_thread.thread)?+ .wake_on_cancel+ .replace(WakeOnCancel::Waiting(set));+ if !old.is_none() {+ bail_bug!("thread unexpectedly had wake_on_cancel set");+ }+ let old = state+ .get_mut(set)?+ .waiting+ .insert(guest_thread, WaitMode::Callback(self));+ if !old.is_none() {+ bail_bug!("set's waiting set already had this thread registered");+ }+ Ok(())+ }+/// Stage the specified guest call as the work item to run next, to be/// started as soon as backpressure and/or reentrance rules allow.///@@ -3800,12 +3852,10 @@ impl Instance {// Note that we're careful to check for waiters _before_ deleting the// set to avoid dropping any waiters in `WaitMode::Fiber(_)`, which// would panic. See `drop-waitable-set-with-waiters.wast` for details.- if !store+ let set = store.concurrent_state_mut()?- .get_mut(TableId::<WaitableSet>::new(rep))?- .waiting- .is_empty()- {+ .get_mut(TableId::<WaitableSet>::new(rep))?;+ if set.num_waiting > 0 {bail!(Trap::WaitableSetDropHasWaiters);}@@ -4217,18 +4267,25 @@ impl Instance {log::trace!("waitable check for {guest_thread:?}; set {:?}", params.set);- let state = store.concurrent_state_mut()?;- let task = state.get_mut(guest_thread.task)?;-// If we're waiting, and there are no events immediately available,// suspend the fiber until that changes.match &check {WaitableCheck::Wait => {let set = params.set;- if (task.event.is_none() || matches!(task.event, Some(Event::Cancelled)))- && state.get_mut(set)?.ready.is_empty()- {+ // Note that this is a loop since, by the time this fiber is+ // resumed after being woken up, another thread may have already+ // taken the event (e.g. via `waitable-set.poll`), in which case+ // we keep waiting.+ loop {+ let state = store.concurrent_state_mut()?;+ let task = state.get_mut(guest_thread.task)?;+ if !(task.event.is_none() || matches!(task.event, Some(Event::Cancelled)))+ || !state.get_mut(set)?.ready.is_empty()+ {+ break;+ }+store.switch_or_trap_if_may_not_suspend(caller)?;store.suspend(SuspendReason::Waiting {@@ -4412,7 +4469,7 @@ impl Instance {thread,kind: GuestCallKind::DeliverEvent {instance,- set: None,+ set: Some(set),},},},@@ -5332,8 +5389,7 @@ pub(crate) struct GuestTask {/// results in that task executing code in a different instance, and it may/// call host functions and intrinsics from that other instance.instance: RuntimeInstance,- /// If present, a pending `Event::None` or `Event::Cancelled` to be- /// delivered to this task.+ /// If present, a pending `Event::Cancelled` to be delivered to this task.event: Option<Event>,/// Whether or not the task has exited.exited: bool,@@ -5673,11 +5729,25 @@ struct WaitableSet {ready: BTreeSet<Waitable>,/// Which guest threads are currently waiting on this set, if any.waiting: BTreeMap<QualifiedThreadId, WaitMode>,+ /// How many guest threads are waiting on this set, mirroring the spec's+ /// `WaitableSet.num_waiting`.+ num_waiting: usize,/// Whether this set is a synthetic, internal one meant for handling/// synchronous calls.is_sync_call_set: bool,}+impl WaitableSet {+ /// Records that a thread counted in `num_waiting` is no longer waiting.+ fn stop_waiting(&mut self) -> Result<()> {+ self.num_waiting = match self.num_waiting.checked_sub(1) {+ Some(n) => n,+ None => bail_bug!("waiter not accounted for in waitable set"),+ };+ Ok(())+ }+}+impl TableDebug for WaitableSet {fn type_name() -> &'static str {"WaitableSet"diff --git a/tests/misc_testsuite/component-model-threading/waitable-event-stolen.wast b/tests/misc_testsuite/component-model-threading/waitable-event-stolen.wastnew file mode 100644index 0000000000..e3857c7e25--- /dev/null+++ b/tests/misc_testsuite/component-model-threading/waitable-event-stolen.wast@@ -0,0 +1,337 @@+;;! component_model_async = true+;;! component_model_threading = true++;; The implicit thread of a callback-lifted task waits on set S (returning+;; `WAIT`). An explicit thread of the same task then completes a read whose+;; readable end is in S (waking the implicit thread up) and immediately takes+;; that event with `waitable-set.poll`. The implicit thread should keep waiting+;; and deadlock here.+(component+ (core module $libc+ (table (export "t") 1 funcref)+ (memory (export "m") 1))+ (core instance $libc (instantiate $libc))+ (core type $start-func-ty (func (param i32)))++ (type $f (future))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f async))+ (core func $future.write (canon future.write $f async))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $waitable-set.poll+ (canon waitable-set.poll (memory (core memory $libc "m"))))+ (core func $thread.new-indirect+ (canon thread.new-indirect $start-func-ty (core table $libc "t")))+ (core func $thread.resume-later (canon thread.resume-later))+ (core func $task.return (canon task.return))++ (core module $m+ (import "" "m" (memory 1))+ (import "" "t" (table 1 funcref))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable.join" (func $join (param i32 i32)))+ (import "" "waitable-set.poll" (func $poll (param i32 i32) (result i32)))+ (import "" "thread.new-indirect"+ (func $thread.new-indirect (param i32 i32) (result i32)))+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))+ (import "" "task.return" (func $task.return))++ (global $w (mut i32) (i32.const 0))+ (global $s (mut i32) (i32.const 0))++ (func $thread (param i32)+ ;; complete the pending read, waking the implicit thread+ (if (i32.ne (call $future.write (global.get $w) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ ;; ... and take the event before the implicit thread runs+ (if (i32.ne (call $poll (global.get $s) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable)))+ (elem (i32.const 0) func $thread)++ (func (export "run") (result i32)+ (local $tmp i64) (local $r i32)+ (local.set $tmp (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $tmp)))+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (global.set $s (call $ws.new))+ (call $join (local.get $r) (global.get $s))+ (call $thread.resume-later+ (call $thread.new-indirect (i32.const 0) (i32.const 0)))+ ;; CALLBACK_CODE_WAIT | (set << 4)+ (i32.or (i32.const 2) (i32.shl (global.get $s) (i32.const 4))))+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)+ )++ (core instance $i (instantiate $m (with "" (instance+ (export "m" (memory $libc "m"))+ (export "t" (table $libc "t"))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "waitable-set.poll" (func $waitable-set.poll))+ (export "thread.new-indirect" (func $thread.new-indirect))+ (export "thread.resume-later" (func $thread.resume-later))+ (export "task.return" (func $task.return))))))++ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+)+(assert_trap (invoke "run") "deadlock detected")++;; Same as above, but the explicit thread drops S after moving the readable end+;; out of it. This should trap since the implicit thread is still waiting.+(component+ (core module $libc+ (table (export "t") 1 funcref))+ (core instance $libc (instantiate $libc))+ (core type $start-func-ty (func (param i32)))++ (type $f (future))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f async))+ (core func $future.write (canon future.write $f async))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.drop (canon waitable-set.drop))+ (core func $waitable.join (canon waitable.join))+ (core func $thread.new-indirect+ (canon thread.new-indirect $start-func-ty (core table $libc "t")))+ (core func $thread.resume-later (canon thread.resume-later))+ (core func $task.return (canon task.return))++ (core module $m+ (import "" "t" (table 1 funcref))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable-set.drop" (func $ws.drop (param i32)))+ (import "" "waitable.join" (func $join (param i32 i32)))+ (import "" "thread.new-indirect"+ (func $thread.new-indirect (param i32 i32) (result i32)))+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))+ (import "" "task.return" (func $task.return))++ (global $r (mut i32) (i32.const 0))+ (global $w (mut i32) (i32.const 0))+ (global $s (mut i32) (i32.const 0))++ (func $thread (param i32)+ ;; complete the pending read, waking the implicit thread+ (if (i32.ne (call $future.write (global.get $w) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ ;; the implicit thread is still waiting on S, so it can't be dropped+ (call $join (global.get $r) (i32.const 0))+ (call $ws.drop (global.get $s)))+ (elem (i32.const 0) func $thread)++ (func (export "run") (result i32)+ (local $tmp i64)+ (local.set $tmp (call $future.new))+ (global.set $r (i32.wrap_i64 (local.get $tmp)))+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (if (i32.ne (call $future.read (global.get $r) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (global.set $s (call $ws.new))+ (call $join (global.get $r) (global.get $s))+ (call $thread.resume-later+ (call $thread.new-indirect (i32.const 0) (i32.const 0)))+ ;; CALLBACK_CODE_WAIT | (set << 4)+ (i32.or (i32.const 2) (i32.shl (global.get $s) (i32.const 4))))+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable)+ )++ (core instance $i (instantiate $m (with "" (instance+ (export "t" (table $libc "t"))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.drop" (func $waitable-set.drop))+ (export "waitable.join" (func $waitable.join))+ (export "thread.new-indirect" (func $thread.new-indirect))+ (export "thread.resume-later" (func $thread.resume-later))+ (export "task.return" (func $task.return))))))++ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+)+(assert_trap (invoke "run") "cannot drop waitable set with waiters")++;; The reverse: an explicit thread blocks in `waitable-set.wait` on S, and the+;; implicit thread (using a callback) completes a read in S, waking the explicit+;; thread up, and then takes the event itself with `waitable-set.poll`. The+;; explicit thread should just keep waiting.+(component+ (core module $libc+ (table (export "t") 1 funcref)+ (memory (export "m") 1))+ (core instance $libc (instantiate $libc))+ (core type $start-func-ty (func (param i32)))++ (type $f (future))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f async))+ (core func $future.write (canon future.write $f async))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $waitable-set.wait+ (canon waitable-set.wait (memory (core memory $libc "m"))))+ (core func $waitable-set.poll+ (canon waitable-set.poll (memory (core memory $libc "m"))))+ (core func $thread.new-indirect+ (canon thread.new-indirect $start-func-ty (core table $libc "t")))+ (core func $thread.resume-later (canon thread.resume-later))+ (core func $task.return (canon task.return))++ (core module $m+ (import "" "m" (memory 1))+ (import "" "t" (table 1 funcref))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable.join" (func $join (param i32 i32)))+ (import "" "waitable-set.wait" (func $wait (param i32 i32) (result i32)))+ (import "" "waitable-set.poll" (func $poll (param i32 i32) (result i32)))+ (import "" "thread.new-indirect"+ (func $thread.new-indirect (param i32 i32) (result i32)))+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))+ (import "" "task.return" (func $task.return))++ (global $w (mut i32) (i32.const 0))+ (global $s (mut i32) (i32.const 0))+ (global $state (mut i32) (i32.const 0))+ (global $woke (mut i32) (i32.const 0))++ (func $thread (param i32)+ (drop (call $wait (global.get $s) (i32.const 16)))+ (global.set $woke (i32.const 1)))+ (elem (i32.const 0) func $thread)++ (func (export "run") (result i32)+ (local $tmp i64) (local $r i32)+ (local.set $tmp (call $future.new))+ (local.set $r (i32.wrap_i64 (local.get $tmp)))+ (global.set $w (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (if (i32.ne (call $future.read (local.get $r) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (global.set $s (call $ws.new))+ (call $join (local.get $r) (global.get $s))+ ;; let the explicit thread block in `waitable-set.wait`+ (call $thread.resume-later+ (call $thread.new-indirect (i32.const 0) (i32.const 0)))+ (i32.const 1 (; YIELD ;)))+ (func (export "cb") (param i32 i32 i32) (result i32)+ (if (i32.eqz (global.get $state))+ (then+ (global.set $state (i32.const 1))+ ;; complete the read; this wakes the explicit thread+ (if (i32.ne (call $future.write (global.get $w) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ ;; ... but take the event before it runs+ (if (i32.ne (call $poll (global.get $s) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ ;; let the explicit thread run+ (return (i32.const 1 (; YIELD ;)))))+ ;; the explicit thread must still be waiting+ (if (global.get $woke) (then unreachable))+ (call $task.return)+ (i32.const 0 (; EXIT ;)))+ )++ (core instance $i (instantiate $m (with "" (instance+ (export "m" (memory $libc "m"))+ (export "t" (table $libc "t"))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "waitable-set.wait" (func $waitable-set.wait))+ (export "waitable-set.poll" (func $waitable-set.poll))+ (export "thread.new-indirect" (func $thread.new-indirect))+ (export "thread.resume-later" (func $thread.resume-later))+ (export "task.return" (func $task.return))))))++ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+)+(assert_return (invoke "run"))++;; A callback returning `YIELD` must not leave an event behind which another+;; thread of the same task could take: here an explicit thread polls an empty+;; set (which must return `EVENT_NONE` without affecting anything else) before+;; the callback receives its `EVENT_NONE`.+(component+ (core module $libc+ (table (export "t") 1 funcref)+ (memory (export "m") 1))+ (core instance $libc (instantiate $libc))+ (core type $start-func-ty (func (param i32)))++ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.poll+ (canon waitable-set.poll (memory (core memory $libc "m"))))+ (core func $thread.new-indirect+ (canon thread.new-indirect $start-func-ty (core table $libc "t")))+ (core func $thread.resume-later (canon thread.resume-later))+ (core func $task.return (canon task.return))++ (core module $m+ (import "" "t" (table 1 funcref))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable-set.poll" (func $poll (param i32 i32) (result i32)))+ (import "" "thread.new-indirect"+ (func $thread.new-indirect (param i32 i32) (result i32)))+ (import "" "thread.resume-later" (func $thread.resume-later (param i32)))+ (import "" "task.return" (func $task.return))++ (global $polled (mut i32) (i32.const 0))++ (func $thread (param i32)+ (if (i32.ne (call $poll (call $ws.new) (i32.const 0))+ (i32.const 0 (; NONE ;)))+ (then unreachable))+ (global.set $polled (i32.const 1)))+ (elem (i32.const 0) func $thread)++ (func (export "run") (result i32)+ (call $thread.resume-later+ (call $thread.new-indirect (i32.const 0) (i32.const 0)))+ (i32.const 1 (; YIELD ;)))+ (func (export "cb") (param $event i32) (param i32 i32) (result i32)+ (if (i32.ne (local.get $event) (i32.const 0 (; NONE ;)))+ (then unreachable))+ (if (i32.eqz (global.get $polled)) (then unreachable))+ (call $task.return)+ (i32.const 0 (; EXIT ;)))+ )++ (core instance $i (instantiate $m (with "" (instance+ (export "t" (table $libc "t"))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.poll" (func $waitable-set.poll))+ (export "thread.new-indirect" (func $thread.new-indirect))+ (export "thread.resume-later" (func $thread.resume-later))+ (export "task.return" (func $task.return))))))++ (func (export "run") async+ (canon lift (core func $i "run") async (callback (core func $i "cb"))))+)+(assert_return (invoke "run"))diff --git a/tests/misc_testsuite/component-model/async/waitable-event-stolen.wast b/tests/misc_testsuite/component-model/async/waitable-event-stolen.wastnew file mode 100644index 0000000000..6e87f00623--- /dev/null+++ b/tests/misc_testsuite/component-model/async/waitable-event-stolen.wast@@ -0,0 +1,239 @@+;;! component_model_async = true++;; Two callback-lifted tasks, "a" and "b", in the same instance share a+;; waitable set. "a" waits on the set (returning `WAIT`), "b" completes a read+;; that's in the set (waking "a" up), and then, before "a" gets to run, "b"+;; takes the event itself or moves the waitable out of the set. Here "a" should+;; keep waiting on the set.+;;+;; The `mode` parameter to `run` selects what "b" does:+;;+;; * 0: takes the event with `waitable-set.poll`+;; * 1: takes the event with `waitable-set.poll`, then completes a second read+;; in the set, which "a" receives+;; * 2: removes the waitable from the set with `waitable.join(w, 0)`+;; * 3: moves the waitable to another set and polls that set+;; * 4: removes all waitables from the set and tries to drop it, which traps+;; since "a" is still waiting on it+;;+;; If bit 3 (8) of `mode` is set, "a" completes the first read itself before+;; returning `WAIT`, so "a" is scheduled to receive the event immediately+;; without first registering as a waiter.+(component definition $T+ (component $J+ (core module $libc (memory (export "m") 1))+ (core instance $libc (instantiate $libc))+ (type $f (future))+ (core func $future.new (canon future.new $f))+ (core func $future.read (canon future.read $f async))+ (core func $future.write (canon future.write $f async))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable-set.drop (canon waitable-set.drop))+ (core func $waitable.join (canon waitable.join))+ (core func $waitable-set.poll+ (canon waitable-set.poll (memory (core memory $libc "m"))))+ (core func $task.return (canon task.return))+ (core module $m+ (import "" "m" (memory 1))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (import "" "future.write"+ (func $future.write (param i32 i32) (result i32)))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable-set.drop" (func $ws.drop (param i32)))+ (import "" "waitable.join" (func $join (param i32 i32)))+ (import "" "waitable-set.poll" (func $poll (param i32 i32) (result i32)))+ (import "" "task.return" (func $ret))+ (global $set (mut i32) (i32.const 0))+ (global $r1 (mut i32) (i32.const 0))+ (global $w1 (mut i32) (i32.const 0))+ (global $r2 (mut i32) (i32.const 0))+ (global $w2 (mut i32) (i32.const 0))++ (func (export "a") (param $mode i32) (result i32)+ (local $tmp i64)+ (local.set $tmp (call $future.new))+ (global.set $r1 (i32.wrap_i64 (local.get $tmp)))+ (global.set $w1+ (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (local.set $tmp (call $future.new))+ (global.set $r2 (i32.wrap_i64 (local.get $tmp)))+ (global.set $w2+ (i32.wrap_i64 (i64.shr_u (local.get $tmp) (i64.const 32))))+ (if (i32.ne (call $future.read (global.get $r1) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (if (i32.ne (call $future.read (global.get $r2) (i32.const 0))+ (i32.const -1 (; BLOCKED ;)))+ (then unreachable))+ (global.set $set (call $ws.new))+ (call $join (global.get $r1) (global.get $set))+ (call $join (global.get $r2) (global.get $set))+ (if (i32.and (local.get $mode) (i32.const 8))+ (then+ (if (i32.ne (call $future.write (global.get $w1) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))))+ ;; CALLBACK_CODE_WAIT | (set << 4)+ (i32.or (i32.const 2) (i32.shl (global.get $set) (i32.const 4))))+ (func (export "a-cb")+ (param $event i32) (param $handle i32) (param i32) (result i32)+ ;; the only event "a" may receive is the completion of the second read+ (if (i32.ne (local.get $event) (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ (if (i32.ne (local.get $handle) (global.get $r2))+ (then unreachable))+ (call $ret)+ (i32.const 0 (; EXIT ;)))++ (func (export "b") (param $mode i32) (result i32)+ (local $set2 i32)+ (if (i32.eqz (i32.and (local.get $mode) (i32.const 8)))+ (then+ (if (i32.ne (call $future.write (global.get $w1) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))))+ (block $done+ (block $drop+ (block $join-other+ (block $join-none+ (block $poll-then-wake+ (block $poll+ (br_table $poll $poll-then-wake $join-none $join-other $drop+ (i32.and (local.get $mode) (i32.const 7))))+ ;; mode 0+ (if (i32.ne (call $poll (global.get $set) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ (if (i32.ne (i32.load (i32.const 0)) (global.get $r1))+ (then unreachable))+ (br $done))+ ;; mode 1+ (if (i32.ne (call $poll (global.get $set) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ (if (i32.ne (i32.load (i32.const 0)) (global.get $r1))+ (then unreachable))+ (if (i32.ne (call $future.write (global.get $w2) (i32.const 0))+ (i32.const 0 (; COMPLETED ;)))+ (then unreachable))+ (br $done))+ ;; mode 2+ (call $join (global.get $r1) (i32.const 0))+ (br $done))+ ;; mode 3+ (local.set $set2 (call $ws.new))+ (call $join (global.get $r1) (local.get $set2))+ (if (i32.ne (call $poll (local.get $set2) (i32.const 0))+ (i32.const 4 (; FUTURE_READ ;)))+ (then unreachable))+ (br $done))+ ;; mode 4+ (call $join (global.get $r1) (i32.const 0))+ (call $join (global.get $r2) (i32.const 0))+ (call $ws.drop (global.get $set)))+ (call $ret)+ (i32.const 0 (; EXIT ;)))+ (func (export "b-cb") (param i32 i32 i32) (result i32) unreachable)+ )+ (core instance $i (instantiate $m (with "" (instance+ (export "m" (memory $libc "m"))+ (export "future.new" (func $future.new))+ (export "future.read" (func $future.read))+ (export "future.write" (func $future.write))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable-set.drop" (func $waitable-set.drop))+ (export "waitable.join" (func $waitable.join))+ (export "waitable-set.poll" (func $waitable-set.poll))+ (export "task.return" (func $task.return))))))+ (func (export "a") async (param "mode" u32)+ (canon lift (core func $i "a") async (callback (core func $i "a-cb"))))+ (func (export "b") async (param "mode" u32)+ (canon lift (core func $i "b") async (callback (core func $i "b-cb"))))+ )+ (component $C+ (import "a" (func $a async (param "mode" u32)))+ (import "b" (func $b async (param "mode" u32)))+ (core func $a (canon lower (func $a) async))+ (core func $b (canon lower (func $b) async))+ (core func $waitable-set.new (canon waitable-set.new))+ (core func $waitable.join (canon waitable.join))+ (core func $subtask.drop (canon subtask.drop))+ (core func $task.return (canon task.return (result u32)))+ (core module $m+ (import "" "a" (func $a (param i32) (result i32)))+ (import "" "b" (func $b (param i32) (result i32)))+ (import "" "waitable-set.new" (func $ws.new (result i32)))+ (import "" "waitable.join" (func $join (param i32 i32)))+ (import "" "subtask.drop" (func $subtask.drop (param i32)))+ (import "" "task.return" (func $task.return (param i32)))+ (global $sa (mut i32) (i32.const 0))+ (func (export "run") (param $mode i32) (result i32)+ (local $set i32)+ ;; start "a"; it blocks, so we get a subtask handle (STARTED=1)+ (global.set $sa (call $a (local.get $mode)))+ (if (i32.ne (i32.and (global.get $sa) (i32.const 0xf)) (i32.const 1))+ (then unreachable))+ (global.set $sa (i32.shr_u (global.get $sa) (i32.const 4)))+ ;; "b" runs to completion (RETURNED=2)+ (if (i32.ne (call $b (local.get $mode)) (i32.const 2 (; RETURNED ;)))+ (then unreachable))+ ;; now wait for "a"+ (local.set $set (call $ws.new))+ (call $join (global.get $sa) (local.get $set))+ ;; CALLBACK_CODE_WAIT | (set << 4)+ (i32.or (i32.const 2) (i32.shl (local.get $set) (i32.const 4))))+ (func (export "run-cb")+ (param $event i32) (param $handle i32) (param $status i32)+ (result i32)+ (if (i32.ne (local.get $event) (i32.const 1 (; SUBTASK ;)))+ (then unreachable))+ (if (i32.ne (local.get $handle) (global.get $sa))+ (then unreachable))+ (if (i32.ne (local.get $status) (i32.const 2 (; RETURNED ;)))+ (then unreachable))+ (call $subtask.drop (global.get $sa))+ (call $task.return (i32.const 7))+ (i32.const 0 (; EXIT ;)))+ )+ (core instance $i (instantiate $m (with "" (instance+ (export "a" (func $a))+ (export "b" (func $b))+ (export "waitable-set.new" (func $waitable-set.new))+ (export "waitable.join" (func $waitable.join))+ (export "subtask.drop" (func $subtask.drop))+ (export "task.return" (func $task.return))))))+ (func (export "run") async (param "mode" u32) (result u32)+ (canon lift (core func $i "run") async+ (callback (core func $i "run-cb"))))+ )+ (instance $j (instantiate $J))+ (instance $c (instantiate $C+ (with "a" (func $j "a"))+ (with "b" (func $j "b"))))+ (func (export "run") (alias export $c "run"))+)++(component instance $T $T)+(assert_trap (invoke "run" (u32.const 0)) "deadlock detected")+(component instance $T $T)+(assert_return (invoke "run" (u32.const 1)) (u32.const 7))+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 2)) "deadlock detected")+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 3)) "deadlock detected")+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 4))+ "cannot drop waitable set with waiters")++(component instance $T $T)+(assert_trap (invoke "run" (u32.const 8)) "deadlock detected")+(component instance $T $T)+(assert_return (invoke "run" (u32.const 9)) (u32.const 7))+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 10)) "deadlock detected")+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 11)) "deadlock detected")+(component instance $T $T)+(assert_trap (invoke "run" (u32.const 12))+ "cannot drop waitable set with waiters")