Skip to main content

wasmtime/runtime/component/
concurrent.rs

1//! Runtime support for the Component Model Async ABI.
2//!
3//! This module and its submodules provide host runtime support for Component
4//! Model Async features such as async-lifted exports, async-lowered imports,
5//! streams, futures, and related intrinsics.  See [the Async
6//! Explainer](https://github.com/WebAssembly/component-model/blob/main/design/mvp/Concurrency.md)
7//! for a high-level overview.
8//!
9//! At the core of this support is an event loop which schedules and switches
10//! between guest tasks and any host tasks they create.  Each
11//! `Store` will have at most one event loop running at any given
12//! time, and that loop may be suspended and resumed by the host embedder using
13//! e.g. `StoreContextMut::run_concurrent`.  The `StoreContextMut::poll_until`
14//! function contains the loop itself, while the
15//! `StoreOpaque::concurrent_state` field holds its state.
16//!
17//! # Public API Overview
18//!
19//! ## Top-level API (e.g. kicking off host->guest calls and driving the event loop)
20//!
21//! - `[Typed]Func::call_concurrent`: Start a host->guest call to an
22//! async-lifted or sync-lifted import, creating a guest task.
23//!
24//! - `StoreContextMut::run_concurrent`: Run the event loop for the specified
25//! instance, allowing any and all tasks belonging to that instance to make
26//! progress.
27//!
28//! - `StoreContextMut::spawn`: Run a background task as part of the event loop
29//! for the specified instance.
30//!
31//! - `{Future,Stream}Reader::new`: Create a new Component Model `future` or
32//! `stream` which may be passed to the guest.  This takes a
33//! `{Future,Stream}Producer` implementation which will be polled for items when
34//! the consumer requests them.
35//!
36//! - `{Future,Stream}Reader::pipe`: Consume a `future` or `stream` by
37//! connecting it to a `{Future,Stream}Consumer` which will consume any items
38//! produced by the write end.
39//!
40//! ## Host Task API (e.g. implementing concurrent host functions and background tasks)
41//!
42//! - `LinkerInstance::func_wrap_concurrent`: Register a concurrent host
43//! function with the linker.  That function will take an `Accessor` as its
44//! first parameter, which provides access to the store between (but not across)
45//! await points.
46//!
47//! - `Accessor::with`: Access the store and its associated data.
48//!
49//! - `Accessor::spawn`: Run a background task as part of the event loop for the
50//! store.  This is equivalent to `StoreContextMut::spawn` but more convenient to use
51//! in host functions.
52
53use self::error_contexts::GlobalErrorContextRefCount;
54use crate::component::func::{Func, call_post_return};
55use crate::component::{
56    HasData, HasSelf, Instance, Resource, ResourceTable, ResourceTableError, RuntimeInstance,
57};
58use crate::fiber::{self, StoreFiber, StoreFiberYield};
59use crate::hash_set::HashSet;
60#[cfg(feature = "gc")]
61use crate::module::ModuleRegistry;
62use crate::prelude::*;
63use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};
64#[cfg(feature = "gc")]
65use crate::vm::GcRootsList;
66use crate::vm::component::{CallContext, ComponentInstance, CurrentScope, InstanceState, Scope};
67use crate::vm::{AlwaysMut, SendSyncPtr, VMFuncRef, VMLazyThread, VMMemoryDefinition, VMStore};
68use crate::{
69    AsContext, AsContextMut, FuncType, Result, StoreContext, StoreContextMut, ValRaw, ValType, bail,
70};
71use crate::{Instance as ModuleInstance, bail_bug};
72use alloc::borrow::ToOwned;
73use alloc::collections::{BTreeMap, BTreeSet, VecDeque};
74use core::any::Any;
75use core::cell::UnsafeCell;
76use core::fmt;
77use core::future;
78use core::future::Future;
79use core::marker::PhantomData;
80use core::mem::{self, ManuallyDrop, MaybeUninit};
81use core::ops::DerefMut;
82use core::pin::{Pin, pin};
83use core::ptr::{self, NonNull};
84use core::task::{Context, Poll, Waker};
85use futures::channel::oneshot;
86use futures::stream::{FuturesUnordered, StreamExt};
87use futures_and_streams::{FlatAbi, ReturnCode, TransmitHandle, TransmitIndex};
88use table::{TableDebug, TableId};
89use wasmtime_environ::component::{
90    CanonicalAbiInfo, CanonicalOptions, CanonicalOptionsDataModel, MAX_FLAT_PARAMS,
91    MAX_FLAT_RESULTS, OptionsIndex, PREPARE_ASYNC_NO_RESULT, PREPARE_ASYNC_WITH_RESULT,
92    RuntimeComponentInstanceIndex, RuntimeTableIndex, StringEncoding,
93    TypeComponentGlobalErrorContextTableIndex, TypeComponentLocalErrorContextTableIndex,
94    TypeFuncIndex, TypeFutureTableIndex, TypeStreamTableIndex, TypeTupleIndex,
95};
96use wasmtime_environ::packed_option::ReservedValue;
97use wasmtime_environ::{NUM_COMPONENT_CONTEXT_SLOTS, Trap};
98#[cfg(feature = "gc")]
99use wasmtime_unwinder::Unwind;
100
101pub use abort::JoinHandle;
102pub use func::{FuncCallConcurrent, TypedFuncCallConcurrent};
103pub use future_stream_any::{FutureAny, StreamAny};
104pub use futures_and_streams::{
105    Destination, DirectDestination, DirectSource, ErrorContext, FutureConsumer, FutureProducer,
106    FutureReader, GuardedFutureReader, GuardedStreamReader, ReadBuffer, Source, StreamConsumer,
107    StreamProducer, StreamReader, StreamResult, VecBuffer, WriteBuffer,
108};
109pub(crate) use futures_and_streams::{ResourcePair, lower_error_context_to_index};
110#[cfg(feature = "task-group-hook")]
111pub use task_group_hook::TaskGroupHook;
112pub use task_group_hook::TaskGroupId;
113
114mod abort;
115mod error_contexts;
116mod func;
117mod future_stream_any;
118mod futures_and_streams;
119pub(crate) mod table;
120#[cfg(feature = "task-group-hook")]
121mod task_group_hook;
122#[cfg(not(feature = "task-group-hook"))]
123mod task_group_hook_disabled;
124#[cfg(not(feature = "task-group-hook"))]
125use task_group_hook_disabled as task_group_hook;
126pub(crate) mod tls;
127
128/// Constant defined in the Component Model spec to indicate that the async
129/// intrinsic (e.g. `future.write`) has not yet completed.
130const BLOCKED: u32 = 0xffff_ffff;
131
132/// Corresponds to `CallState` in the upstream spec.
133#[derive(Clone, Copy, Eq, PartialEq, Debug)]
134pub enum Status {
135    Starting = 0,
136    Started = 1,
137    Returned = 2,
138    StartCancelled = 3,
139    ReturnCancelled = 4,
140}
141
142impl Status {
143    /// Packs this status and the optional `waitable` provided into a 32-bit
144    /// result that the canonical ABI requires.
145    ///
146    /// The low 4 bits are reserved for the status while the upper 28 bits are
147    /// the waitable, if present.
148    pub fn pack(self, waitable: Option<u32>) -> u32 {
149        assert!(matches!(self, Status::Returned) == waitable.is_none());
150        let waitable = waitable.unwrap_or(0);
151        assert!(waitable < (1 << 28));
152        (waitable << 4) | (self as u32)
153    }
154}
155
156/// Corresponds to `EventCode` in the Component Model spec, plus related payload
157/// data.
158#[derive(Clone, Copy, Debug)]
159enum Event {
160    None,
161    Subtask {
162        status: Status,
163    },
164    StreamRead {
165        code: ReturnCode,
166        pending: Option<(TypeStreamTableIndex, u32)>,
167    },
168    StreamWrite {
169        code: ReturnCode,
170        pending: Option<(TypeStreamTableIndex, u32)>,
171    },
172    FutureRead {
173        code: ReturnCode,
174        pending: Option<(TypeFutureTableIndex, u32)>,
175    },
176    FutureWrite {
177        code: ReturnCode,
178        pending: Option<(TypeFutureTableIndex, u32)>,
179    },
180    Cancelled,
181}
182
183impl Event {
184    /// Lower this event to core Wasm integers for delivery to the guest.
185    ///
186    /// Note that the waitable handle, if any, is assumed to be lowered
187    /// separately.
188    fn parts(self) -> (u32, u32) {
189        const EVENT_NONE: u32 = 0;
190        const EVENT_SUBTASK: u32 = 1;
191        const EVENT_STREAM_READ: u32 = 2;
192        const EVENT_STREAM_WRITE: u32 = 3;
193        const EVENT_FUTURE_READ: u32 = 4;
194        const EVENT_FUTURE_WRITE: u32 = 5;
195        const EVENT_CANCELLED: u32 = 6;
196        match self {
197            Event::None => (EVENT_NONE, 0),
198            Event::Cancelled => (EVENT_CANCELLED, 0),
199            Event::Subtask { status } => (EVENT_SUBTASK, status as u32),
200            Event::StreamRead { code, .. } => (EVENT_STREAM_READ, code.encode()),
201            Event::StreamWrite { code, .. } => (EVENT_STREAM_WRITE, code.encode()),
202            Event::FutureRead { code, .. } => (EVENT_FUTURE_READ, code.encode()),
203            Event::FutureWrite { code, .. } => (EVENT_FUTURE_WRITE, code.encode()),
204        }
205    }
206}
207
208/// Corresponds to `CallbackCode` in the spec.
209mod callback_code {
210    pub const EXIT: u32 = 0;
211    pub const YIELD: u32 = 1;
212    pub const WAIT: u32 = 2;
213}
214
215/// A flag indicating that the callee is an async-lowered export.
216///
217/// This may be passed to the `async-start` intrinsic from a fused adapter.
218const START_FLAG_ASYNC_CALLEE: u32 = wasmtime_environ::component::START_FLAG_ASYNC_CALLEE as u32;
219
220/// Provides access to either store data (via the `get` method) or the store
221/// itself (via [`AsContext`]/[`AsContextMut`]), as well as the component
222/// instance to which the current host task belongs.
223///
224/// See [`Accessor::with`] for details.
225pub struct Access<'a, T: 'static, D: HasData + ?Sized = HasSelf<T>> {
226    store: StoreContextMut<'a, T>,
227    get_data: fn(&mut T) -> D::Data<'_>,
228}
229
230impl<'a, T, D> Access<'a, T, D>
231where
232    D: HasData + ?Sized,
233    T: 'static,
234{
235    /// Creates a new [`Access`] from its component parts.
236    pub fn new(store: StoreContextMut<'a, T>, get_data: fn(&mut T) -> D::Data<'_>) -> Self {
237        Self { store, get_data }
238    }
239
240    /// Get mutable access to the store data.
241    pub fn data_mut(&mut self) -> &mut T {
242        self.store.data_mut()
243    }
244
245    /// Get mutable access to the store data.
246    pub fn get(&mut self) -> D::Data<'_> {
247        (self.get_data)(self.data_mut())
248    }
249
250    /// Spawn a background task.
251    ///
252    /// See [`Accessor::spawn`] for details.
253    pub fn spawn(&mut self, task: impl for<'fut> AccessorTask<'fut, T, D>) -> Result<JoinHandle>
254    where
255        T: 'static,
256    {
257        let accessor = Accessor {
258            get_data: self.get_data,
259            token: StoreToken::new(self.store.as_context_mut()),
260        };
261        self.store
262            .as_context_mut()
263            .spawn_with_accessor(accessor, task)
264    }
265
266    /// Returns the getter this accessor is using to project from `T` into
267    /// `D::Data`.
268    pub fn getter(&self) -> fn(&mut T) -> D::Data<'_> {
269        self.get_data
270    }
271}
272
273impl<'a, T, D> AsContext for Access<'a, T, D>
274where
275    D: HasData + ?Sized,
276    T: 'static,
277{
278    type Data = T;
279
280    fn as_context(&self) -> StoreContext<'_, T> {
281        self.store.as_context()
282    }
283}
284
285impl<'a, T, D> AsContextMut for Access<'a, T, D>
286where
287    D: HasData + ?Sized,
288    T: 'static,
289{
290    fn as_context_mut(&mut self) -> StoreContextMut<'_, T> {
291        self.store.as_context_mut()
292    }
293}
294
295/// Provides scoped mutable access to store data in the context of a concurrent
296/// host task future.
297///
298/// This allows multiple host task futures to execute concurrently and access
299/// the store between (but not across) `await` points.
300///
301/// # Rationale
302///
303/// This structure is sort of like `&mut T` plus a projection from `&mut T` to
304/// `D::Data<'_>`. The problem this is solving, however, is that it does not
305/// literally store these values. The basic problem is that when a concurrent
306/// host future is being polled it has access to `&mut T` (and the whole
307/// `Store`) but when it's not being polled it does not have access to these
308/// values. This reflects how the store is only ever polling one future at a
309/// time so the store is effectively being passed between futures.
310///
311/// Rust's `Future` trait, however, has no means of passing a `Store`
312/// temporarily between futures. The [`Context`](core::task::Context) type does
313/// not have the ability to attach arbitrary information to it at this time.
314/// This type, [`Accessor`], is used to bridge this expressivity gap.
315///
316/// The [`Accessor`] type here represents the ability to acquire, temporarily in
317/// a synchronous manner, the current store. The [`Accessor::with`] function
318/// yields an [`Access`] which can be used to access [`StoreContextMut`], `&mut
319/// T`, or `D::Data<'_>`. Note though that [`Accessor::with`] intentionally does
320/// not take an `async` closure as its argument, instead it's a synchronous
321/// closure which must complete during on run of `Future::poll`. This reflects
322/// how the store is temporarily made available while a host future is being
323/// polled.
324///
325/// # Implementation
326///
327/// This type does not actually store `&mut T` nor `StoreContextMut<T>`, and
328/// this type additionally doesn't even have a lifetime parameter. This is
329/// instead a representation of proof of the ability to acquire these while a
330/// future is being polled. Wasmtime will, when it polls a host future,
331/// configure ambient state such that the `Accessor` that a future closes over
332/// will work and be able to access the store.
333///
334/// This has a number of implications for users such as:
335///
336/// * It's intentional that `Accessor` cannot be cloned, it needs to stay within
337///   the lifetime of a single future.
338/// * A future is expected to, however, close over an `Accessor` and keep it
339///   alive probably for the duration of the entire future.
340/// * Different host futures will be given different `Accessor`s, and that's
341///   intentional.
342/// * The `Accessor` type is `Send` and `Sync` irrespective of `T` which
343///   alleviates some otherwise required bounds to be written down.
344///
345/// # Using `Accessor` in `Drop`
346///
347/// The methods on `Accessor` are only expected to work in the context of
348/// `Future::poll` and are not guaranteed to work in `Drop`. This is because a
349/// host future can be dropped at any time throughout the system and Wasmtime
350/// store context is not necessarily available at that time. It's recommended to
351/// not use `Accessor` methods in anything connected to a `Drop` implementation
352/// as they will panic and have unintended results. If you run into this though
353/// feel free to file an issue on the Wasmtime repository.
354pub struct Accessor<T: 'static, D = HasSelf<T>>
355where
356    D: HasData + ?Sized,
357{
358    token: StoreToken<T>,
359    get_data: fn(&mut T) -> D::Data<'_>,
360}
361
362/// A helper trait to take any type of accessor-with-data in functions.
363///
364/// This trait is similar to [`AsContextMut`] except that it's used when
365/// working with an [`Accessor`] instead of a [`StoreContextMut`]. The
366/// [`Accessor`] is the main type used in concurrent settings and is passed to
367/// functions such as [`Func::call_concurrent`].
368///
369/// This trait is implemented for [`Accessor`] and `&T` where `T` implements
370/// this trait. This effectively means that regardless of the `D` in
371/// `Accessor<T, D>` it can still be passed to a function which just needs a
372/// store accessor.
373///
374/// Acquiring an [`Accessor`] can be done through
375/// [`StoreContextMut::run_concurrent`] for example or in a host function
376/// through
377/// [`Linker::func_wrap_concurrent`](crate::component::LinkerInstance::func_wrap_concurrent).
378pub trait AsAccessor {
379    /// The `T` in `Store<T>` that this accessor refers to.
380    type Data: 'static;
381
382    /// The `D` in `Accessor<T, D>`, or the projection out of
383    /// `Self::Data`.
384    type AccessorData: HasData + ?Sized;
385
386    /// Returns the accessor that this is referring to.
387    fn as_accessor(&self) -> &Accessor<Self::Data, Self::AccessorData>;
388}
389
390impl<T: AsAccessor + ?Sized> AsAccessor for &T {
391    type Data = T::Data;
392    type AccessorData = T::AccessorData;
393
394    fn as_accessor(&self) -> &Accessor<Self::Data, Self::AccessorData> {
395        T::as_accessor(self)
396    }
397}
398
399impl<T, D: HasData + ?Sized> AsAccessor for Accessor<T, D> {
400    type Data = T;
401    type AccessorData = D;
402
403    fn as_accessor(&self) -> &Accessor<T, D> {
404        self
405    }
406}
407
408// Note that it is intentional at this time that `Accessor` does not actually
409// store `&mut T` or anything similar. This distinctly enables the `Accessor`
410// structure to be both `Send` and `Sync` regardless of what `T` is (or `D` for
411// that matter). This is used to ergonomically simplify bindings where the
412// majority of the time `Accessor` is closed over in a future which then needs
413// to be `Send` and `Sync`. To avoid needing to write `T: Send` everywhere (as
414// you already have to write `T: 'static`...) it helps to avoid this.
415//
416// Note as well that `Accessor` doesn't actually store its data at all. Instead
417// it's more of a "proof" of what can be accessed from TLS. API design around
418// `Accessor` and functions like `Linker::func_wrap_concurrent` are
419// intentionally made to ensure that `Accessor` is ideally only used in the
420// context that TLS variables are actually set. For example host functions are
421// given `&Accessor`, not `Accessor`, and this prevents them from persisting
422// the value outside of a future. Within the future the TLS variables are all
423// guaranteed to be set while the future is being polled.
424//
425// Finally though this is not an ironclad guarantee, but nor does it need to be.
426// The TLS APIs are designed to panic or otherwise model usage where they're
427// called recursively or similar. It's hoped that code cannot be constructed to
428// actually hit this at runtime but this is not a safety requirement at this
429// time.
430const _: () = {
431    const fn assert<T: Send + Sync>() {}
432    assert::<Accessor<UnsafeCell<u32>>>();
433};
434
435impl<T> Accessor<T> {
436    /// Creates a new `Accessor` backed by the specified functions.
437    ///
438    /// - `get`: used to retrieve the store
439    ///
440    /// - `get_data`: used to "project" from the store's associated data to
441    /// another type (e.g. a field of that data or a wrapper around it).
442    ///
443    /// - `spawn`: used to queue spawned background tasks to be run later
444    pub(crate) fn new(token: StoreToken<T>) -> Self {
445        Self {
446            token,
447            get_data: |x| x,
448        }
449    }
450}
451
452impl<T, D> Accessor<T, D>
453where
454    D: HasData + ?Sized,
455{
456    /// Run the specified closure, passing it mutable access to the store.
457    ///
458    /// This function is one of the main building blocks of the [`Accessor`]
459    /// type. This yields synchronous, blocking, access to the store via an
460    /// [`Access`]. The [`Access`] implements [`AsContextMut`] in addition to
461    /// providing the ability to access `D` via [`Access::get`]. Note that the
462    /// `fun` here is given only temporary access to the store and `T`/`D`
463    /// meaning that the return value `R` here is not allowed to capture borrows
464    /// into the two. If access is needed to data within `T` or `D` outside of
465    /// this closure then it must be `clone`d out, for example.
466    ///
467    /// # Panics
468    ///
469    /// This function will panic if it is call recursively with any other
470    /// accessor already in scope. For example if `with` is called within `fun`,
471    /// then this function will panic. It is up to the embedder to ensure that
472    /// this does not happen.
473    pub fn with<R>(&self, fun: impl FnOnce(Access<'_, T, D>) -> R) -> R {
474        tls::get(|vmstore| {
475            fun(Access {
476                store: self.token.as_context_mut(vmstore),
477                get_data: self.get_data,
478            })
479        })
480    }
481
482    /// Returns the getter this accessor is using to project from `T` into
483    /// `D::Data`.
484    pub fn getter(&self) -> fn(&mut T) -> D::Data<'_> {
485        self.get_data
486    }
487
488    /// Changes this accessor to access `D2` instead of the current type
489    /// parameter `D`.
490    ///
491    /// This changes the underlying data access from `T` to `D2::Data<'_>`.
492    ///
493    /// # Panics
494    ///
495    /// When using this API the returned value is disconnected from `&self` and
496    /// the lifetime binding the `self` argument. An `Accessor` only works
497    /// within the context of the closure or async closure that it was
498    /// originally given to, however. This means that due to the fact that the
499    /// returned value has no lifetime connection it's possible to use the
500    /// accessor outside of `&self`, the original accessor, and panic.
501    ///
502    /// The returned value should only be used within the scope of the original
503    /// `Accessor` that `self` refers to.
504    pub fn with_getter<D2: HasData>(
505        &self,
506        get_data: fn(&mut T) -> D2::Data<'_>,
507    ) -> Accessor<T, D2> {
508        Accessor {
509            token: self.token,
510            get_data,
511        }
512    }
513
514    /// Spawn a background task which will receive an `&Accessor<T, D>` and
515    /// run concurrently with any other tasks in progress for the current
516    /// store.
517    ///
518    /// This is particularly useful for host functions which return a `stream`
519    /// or `future` such that the code to write to the write end of that
520    /// `stream` or `future` must run after the function returns.
521    ///
522    /// The returned [`JoinHandle`] may be used to cancel the task.
523    ///
524    /// # Panics
525    ///
526    /// Panics if called within a closure provided to the [`Accessor::with`]
527    /// function. This can only be called outside an active invocation of
528    /// [`Accessor::with`].
529    pub fn spawn(&self, task: impl for<'fut> AccessorTask<'fut, T, D>) -> Result<JoinHandle>
530    where
531        T: 'static,
532    {
533        let accessor = self.clone_for_spawn();
534        self.with(|mut access| access.as_context_mut().spawn_with_accessor(accessor, task))
535    }
536
537    fn clone_for_spawn(&self) -> Self {
538        Self {
539            token: self.token,
540            get_data: self.get_data,
541        }
542    }
543
544    /// Polls to see if this store contains any "interesting" tasks still within
545    /// it.
546    ///
547    /// Returns `Poll::Ready(())` if there are no more interesting tasks, and
548    /// otherwise returns `Poll::Pending`. If pending is returned then whenever
549    /// the last remaining "interesting" task has exited the provided context's
550    /// waker will be notified. Note that only the waker passed to the last call
551    /// to `poll_no_interesting_tasks` for the store will be notified, so this
552    /// is only appropriate to use once-at-a-time per store.
553    ///
554    /// The component model specification, as of this current date, does not
555    /// have a distinction between "interesting" tasks and not. The current
556    /// intention is that in a future revision of the component model this will
557    /// be distinguished at the component ABI level where tasks will be able to
558    /// flag themselves as "interesting" optionally. Additionally extra work can
559    /// be opted-in to being "interesting".
560    ///
561    /// For now what this means is that all component model tasks within this
562    /// store are considered interesting. This specifically includes the entire
563    /// duration of a task, so even all of the time after a task has returned
564    /// but before it has exited. This means that this function is, today,
565    /// effectively a proxy for "are there any more tasks still running in this
566    /// store". This can be used by embedders to determine whether there's any
567    /// more work going on, even in the background, for a particular guest.
568    /// Hosts can use this as a signal that the guest wants to stay alive a
569    /// little longer, even after a task has returned.
570    ///
571    /// In the future this predicate won't include all tasks in this store. Some
572    /// tasks will be able to flag themselves as not interesting, meaning that
573    /// when this returns ready it'd be possible that there are still tasks
574    /// remaining in the store.
575    ///
576    /// Note that at this time spawned threads within a task are always
577    /// considered uninteresting. If this function returns ready, then spawned
578    /// threads may still be in the store.
579    pub fn poll_no_interesting_tasks(&self, cx: &mut Context<'_>) -> Poll<()> {
580        self.with(|mut access| {
581            let store = access.as_context_mut().0;
582            let state = store.concurrent_state_mut_without_forcing_current_thread();
583            if state.interesting_tasks == 0 {
584                Poll::Ready(())
585            } else {
586                state.interesting_tasks_empty_waker = Some(cx.waker().clone());
587                Poll::Pending
588            }
589        })
590    }
591
592    /// Poll to see if the component instance corresponding to the specified
593    /// function is ready to run a concurrent call without queuing it (i.e. does
594    /// not have backpressure enabled and does not have a sync call in
595    /// progress).
596    ///
597    /// Returns `Poll::Ready(())` if the component instance is ready to run a
598    /// concurrent call, and otherwise returns `Poll::Pending`.  If pending is
599    /// returned then whenever the instance becomes ready for a call the
600    /// provided context's waker will be notified.  Note that only the waker
601    /// passed to the last call to `poll_ready_for_concurrent_call` for the
602    /// store will be notified (regardless of whether the same or different
603    /// `Func` is specified relative to earlier calls), so this is only
604    /// appropriate to use once-at-a-time per store.  Also note that the waker
605    /// may be notified when _any_ instance becomes callable (i.e. not
606    /// necessarily the last one polled), so this function must be called again
607    /// to determine if the instance of interest is ready.
608    pub fn poll_ready_for_concurrent_call(&self, func: Func, cx: &mut Context<'_>) -> Poll<()> {
609        self.with(|mut access| {
610            let store = access.as_context_mut().0;
611            let (_, _, _, raw_options) = func.abi_info(store);
612            let instance = func.instance().runtime_instance(raw_options.instance);
613            let state = store.instance_state(instance).concurrent_state();
614            if state.backpressure == 0 {
615                Poll::Ready(())
616            } else {
617                store
618                    .concurrent_state_mut_without_forcing_current_thread()
619                    .ready_for_concurrent_call_waker = Some(cx.waker().clone());
620                Poll::Pending
621            }
622        })
623    }
624}
625
626/// Represents an async closure which may be provided to `Accessor::spawn`,
627/// `Accessor::forward`, or `StoreContextMut::spawn`.
628// TODO: Replace this with `core::ops::AsyncFnOnce` when we are able to put `Send`
629// bound on the unnamed `Future` directly.
630//
631// As of this writing, it's not possible to specify e.g. `Send` and `Sync`
632// bounds on the `Future` type returned by an `AsyncFnOnce`.  Also, using `F:
633// Future<Output = Result<()>> + Send + Sync, FN: FnOnce(&Accessor<T>) -> F +
634// Send + Sync + 'static` fails with a type mismatch error as we cannot describe
635// that `F` should have `&Accessor<T>`'s unnamed lifetime
636//
637// Instead, this trait is used as a workaround for this limitation, the bound on `Self`
638// implementing `AsyncFnOnce()` is required for Rust to automatically infer that an async
639// closure is to be provided wherever we are accepting `impl for<'fut> AccessorTask<'fut, T, D>`
640// as argument. Otherwise, users will have to fully qualify the closure types before it
641// is accepted as a valid value. This also means that it is not intended for a user
642// to manually implement this trait for any arbitrary type, since they would first
643// have to implement `AsyncFnOnce`, which is unstable.
644//
645// The blanket implementation for this trait will ensure that `Self` is an async closure
646// that returns a `Future` that is `Send`, and lives as long as `&Accessor<T, D>`
647pub trait AccessorTask<'fut, T, D = HasSelf<T>>:
648    AsyncFnOnce(&Accessor<T, D>) -> Result<()> + Send + 'static
649where
650    D: HasData + ?Sized,
651{
652    /// Run the task.
653    fn run(self, accessor: &'fut Accessor<T, D>) -> impl Future<Output = Result<()>> + Send + 'fut;
654}
655
656impl<'fut, F, Fut, T, D> AccessorTask<'fut, T, D> for F
657where
658    T: 'static,
659    F: AsyncFnOnce(&Accessor<T, D>) -> Result<()>,
660    F: FnOnce(&'fut Accessor<T, D>) -> Fut + Send + 'static,
661    Fut: Future<Output = Result<()>> + Send + 'fut,
662    D: HasData,
663{
664    fn run(self, accessor: &'fut Accessor<T, D>) -> impl Future<Output = Result<()>> + Send + 'fut {
665        (self)(accessor)
666    }
667}
668
669/// Represents parameter and result metadata for the caller side of a
670/// guest->guest call orchestrated by a fused adapter.
671enum CallerInfo {
672    /// Metadata for a call to an async-lowered import
673    Async {
674        params: Vec<ValRaw>,
675        has_result: bool,
676    },
677    /// Metadata for a call to an sync-lowered import
678    Sync {
679        params: Vec<ValRaw>,
680        result_count: u32,
681    },
682}
683
684/// Indicates how a guest task is waiting on a waitable set.
685enum WaitMode {
686    /// The guest task is waiting using `task.wait`
687    Fiber(StoreFiber<'static>),
688    /// The guest task is waiting via a callback declared as part of an
689    /// async-lifted export.
690    Callback(Instance),
691}
692
693impl fmt::Debug for WaitMode {
694    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
695        match self {
696            Self::Fiber(_) => f.debug_tuple("Fiber").finish(),
697            Self::Callback(instance) => f.debug_tuple("Callback").field(instance).finish(),
698        }
699    }
700}
701
702/// Represents the reason a fiber is suspending itself.
703#[derive(Debug)]
704enum SuspendReason {
705    /// The fiber is waiting for an event to be delivered to the specified
706    /// waitable set or task.
707    Waiting {
708        set: TableId<WaitableSet>,
709        thread: QualifiedThreadId,
710    },
711    /// The fiber is waiting for a subtask to suspend or exit, e.g. for a
712    /// guest-to-guest call or a `subtask.cancel`.
713    YieldingToSubtask { thread: QualifiedThreadId },
714    /// The fiber has finished handling its most recent work item and is waiting
715    /// for another (or to be dropped if it is no longer needed).
716    NeedWork,
717    /// The fiber is yielding and should be resumed once other tasks have had a
718    /// chance to run.
719    Yielding { thread: QualifiedThreadId },
720    /// The fiber was explicitly suspended with a call to `thread.suspend` or
721    /// `thread.switch-to`.
722    ExplicitlySuspending { thread: QualifiedThreadId },
723}
724
725/// Represents a pending call into guest code for a given guest task.
726enum GuestCallKind {
727    /// Indicates there's an event to deliver to the task, possibly related to a
728    /// waitable set the task has been waiting on or polling.
729    DeliverEvent {
730        /// The instance to which the task belongs.
731        instance: Instance,
732        /// The waitable set the event belongs to, if any.
733        ///
734        /// If this is `None` the event will be waiting in the
735        /// `GuestTask::event` field for the task.
736        set: Option<TableId<WaitableSet>>,
737    },
738    /// Indicates that a new guest task call is pending and may be executed
739    /// using the specified closure.
740    ///
741    /// If the closure returns `Ok(Some(call))`, the `call` should be run
742    /// immediately using `handle_guest_call`.
743    StartImplicit(Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>),
744    StartExplicit(Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>),
745}
746
747impl fmt::Debug for GuestCallKind {
748    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
749        match self {
750            Self::DeliverEvent { instance, set } => f
751                .debug_struct("DeliverEvent")
752                .field("instance", instance)
753                .field("set", set)
754                .finish(),
755            Self::StartImplicit(_) => f.debug_tuple("StartImplicit").finish(),
756            Self::StartExplicit(_) => f.debug_tuple("StartExplicit").finish(),
757        }
758    }
759}
760
761/// The target of a suspension intrinsic.
762#[derive(Copy, Clone, Debug)]
763pub enum SuspensionTarget {
764    Resume(u32),
765    Promote(u32),
766    None,
767}
768
769/// Behavior for `resume_thread`.
770#[derive(Copy, Clone, Debug)]
771pub enum ResumeThread {
772    Promote,
773    Resume,
774    ResumeLater,
775}
776
777/// Represents a pending call into guest code for a given guest thread.
778#[derive(Debug)]
779struct GuestCall {
780    thread: QualifiedThreadId,
781    kind: GuestCallKind,
782}
783
784impl GuestCall {
785    /// Returns whether or not the call is ready to run.
786    ///
787    /// A call will not be ready to run if either:
788    ///
789    /// - the (sub-)component instance to be called has already been entered and
790    /// cannot be reentered until an in-progress call completes
791    ///
792    /// - the call is for a not-yet started task and the (sub-)component
793    /// instance to be called has backpressure enabled
794    fn is_ready(&self, store: &mut StoreOpaque) -> Result<bool> {
795        let task = store.concurrent_state_mut()?.get_mut(self.thread.task)?;
796        let async_typed = task.async_typed;
797        let instance = task.instance;
798        let state = store.instance_state(instance).concurrent_state();
799
800        let ready = match &self.kind {
801            GuestCallKind::DeliverEvent { .. } => !state.do_not_enter,
802            GuestCallKind::StartImplicit(_) => {
803                !async_typed || !(state.do_not_enter || state.backpressure > 0)
804            }
805            GuestCallKind::StartExplicit(_) => true,
806        };
807        log::trace!(
808            "call {self:?} ready? {ready} (do_not_enter: {}; backpressure: {})",
809            state.do_not_enter,
810            state.backpressure
811        );
812        Ok(ready)
813    }
814}
815
816/// Job to be run on a worker fiber.
817enum WorkerItem {
818    GuestCall(GuestCall),
819    Function(AlwaysMut<Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send>>),
820}
821
822/// Represents a pending work item to be handled by the event loop for a given
823/// component instance.
824enum WorkItem {
825    /// A host task to be pushed to `ConcurrentState::futures`.
826    PushFuture(AlwaysMut<HostTaskFuture>),
827    /// A fiber to resume.
828    ResumeFiber {
829        instance: RuntimeInstance,
830        thread: QualifiedThreadId,
831        fiber: StoreFiber<'static>,
832    },
833    /// A thread to resume.
834    ResumeThread {
835        instance: RuntimeInstance,
836        thread: QualifiedThreadId,
837    },
838    /// A pending call into guest code for a given guest task.
839    GuestCall {
840        instance: RuntimeInstance,
841        call: GuestCall,
842    },
843    /// A job to run on a worker fiber.
844    WorkerFunction(AlwaysMut<Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send>>),
845}
846
847impl fmt::Debug for WorkItem {
848    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
849        match self {
850            Self::PushFuture(_) => f.debug_tuple("PushFuture").finish(),
851            Self::ResumeFiber {
852                instance, thread, ..
853            } => f
854                .debug_struct("ResumeFiber")
855                .field("instance", instance)
856                .field("thread", thread)
857                .finish(),
858            Self::ResumeThread { instance, thread } => f
859                .debug_struct("ResumeThread")
860                .field("instance", instance)
861                .field("thread", thread)
862                .finish(),
863            Self::GuestCall { instance, call } => f
864                .debug_struct("GuestCall")
865                .field("instance", instance)
866                .field("call", call)
867                .finish(),
868            Self::WorkerFunction(_) => f.debug_tuple("WorkerFunction").finish(),
869        }
870    }
871}
872
873/// Whether a suspension intrinsic was cancelled or completed
874#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
875pub(crate) enum WaitResult {
876    Cancelled,
877    Completed,
878}
879
880/// Poll the specified future until it completes on behalf of a guest->host call
881/// using a sync-lowered import.
882///
883/// This is similar to `Instance::first_poll` except it's for sync-lowered
884/// imports, meaning we don't need to handle cancellation and we can block the
885/// caller until the task completes, at which point the caller can handle
886/// lowering the result to the guest's stack and linear memory.
887pub(crate) fn poll_and_block<R: Send + Sync + 'static>(
888    store: &mut dyn VMStore,
889    host_task: EnteredHostTask,
890    future: impl Future<Output = Result<R>> + Send + 'static,
891) -> Result<R> {
892    // Poll the future once before creating a host task. The host task will be
893    // created lazily if it's needed during the poll and otherwise will be
894    // created if the future suspends.  We can use a dummy `Waker` here because
895    // we'll add the future to `ConcurrentState::futures` and poll it
896    // automatically from the event loop if it doesn't complete immediately
897    // here.
898    let mut future = Box::pin(future);
899    let poll = tls::set(store, || {
900        future
901            .as_mut()
902            .poll(&mut Context::from_waker(&Waker::noop()))
903    });
904
905    let caller = match host_task {
906        Some(caller) => caller,
907        None => bail_bug!("host task wasn't created but should have been"),
908    };
909
910    let task = match poll {
911        // It completed immediately, so no persistent host task is needed.
912        Poll::Ready(result) => return result,
913
914        // It did not complete immediately; create the host task and add it to
915        // `ConcurrentState::futures` so it will be polled via the event loop;
916        // then use `GuestThread::sync_call_set` to wait for the task to
917        // complete, suspending the current fiber until it does so.
918        Poll::Pending => {
919            let Some(task) = store.materialize_host_task_id()? else {
920                bail_bug!("current thread is not a host thread")
921            };
922
923            // Wrap the future in a closure which will stash its result in the
924            // host task and resume this fiber when it completes.
925            let future = Box::pin(async move {
926                let result = run_with_host_task_set(task, future).await??;
927                tls::get(move |store| {
928                    let state = store.concurrent_state_mut()?;
929                    let host_state = &mut state.get_mut(task)?.state;
930                    assert!(matches!(host_state, HostTaskState::CalleeStarted));
931                    *host_state = HostTaskState::CalleeFinished(Box::new(result));
932
933                    Waitable::Host(task).set_event(
934                        state,
935                        Some(Event::Subtask {
936                            status: Status::Returned,
937                        }),
938                    )?;
939
940                    Ok(())
941                })
942            }) as HostTaskFuture;
943
944            let caller_instance = store.concurrent_state_mut()?.get_mut(caller.task)?.instance;
945            store.switch_or_trap_if_may_not_suspend(caller_instance)?;
946
947            let state = store.concurrent_state_mut()?;
948            state.push_future(future);
949
950            let set = state.get_mut(caller.thread)?.sync_call_set;
951            Waitable::Host(task).join(state, Some(set))?;
952
953            store.suspend(SuspendReason::Waiting {
954                set,
955                thread: caller,
956            })?;
957
958            // Remove the `task` from the `sync_call_set` to ensure that when
959            // this function returns and the task is deleted that there are no
960            // more lingering references to this host task.
961            Waitable::Host(task).join(store.concurrent_state_mut()?, None)?;
962            task
963        }
964    };
965
966    // Retrieve and return the result.
967    let host_state = &mut store.concurrent_state_mut()?.get_mut(task)?.state;
968    match mem::replace(host_state, HostTaskState::CalleeDone { cancelled: false }) {
969        HostTaskState::CalleeFinished(result) => Ok(match result.downcast() {
970            Ok(result) => *result,
971            Err(_) => bail_bug!("host task finished with wrong type of result"),
972        }),
973        _ => bail_bug!("unexpected host task state after completion"),
974    }
975}
976
977/// Execute the specified guest call.
978fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {
979    match call.kind {
980        GuestCallKind::DeliverEvent { instance, set } => {
981            let (event, waitable) = match instance.get_event(store, call.thread.task, set, true)? {
982                Some(pair) => pair,
983                None => bail_bug!("delivering non-present event"),
984            };
985            let state = store.concurrent_state_mut()?;
986            let task = state.get_mut(call.thread.task)?;
987            let runtime_instance = task.instance;
988            let handle = waitable.map(|(_, v)| v).unwrap_or(0);
989
990            log::trace!(
991                "use callback to deliver event {event:?} to {:?} for {waitable:?}",
992                call.thread,
993            );
994
995            let old_thread = store.set_thread(call.thread)?;
996            log::trace!(
997                "GuestCallKind::DeliverEvent: replaced {old_thread:?} with {:?} as current thread",
998                call.thread
999            );
1000
1001            store.enter_instance(runtime_instance);
1002
1003            let Some(callback) = store
1004                .concurrent_state_mut()?
1005                .get_mut(call.thread.task)?
1006                .callback
1007                .take()
1008            else {
1009                bail_bug!("guest task callback field not present")
1010            };
1011
1012            let code = callback(store, event, handle)?;
1013
1014            store
1015                .concurrent_state_mut()?
1016                .get_mut(call.thread.task)?
1017                .callback = Some(callback);
1018
1019            store.exit_instance(runtime_instance)?;
1020
1021            store.set_thread(old_thread)?;
1022
1023            instance.handle_callback_code(store, call.thread, runtime_instance.index, code)?;
1024
1025            log::trace!("GuestCallKind::DeliverEvent: restored {old_thread:?} as current thread");
1026        }
1027        GuestCallKind::StartImplicit(fun) => {
1028            fun(store)?;
1029        }
1030        GuestCallKind::StartExplicit(fun) => {
1031            fun(store)?;
1032        }
1033    }
1034
1035    Ok(())
1036}
1037
1038impl<T> Store<T> {
1039    /// Convenience wrapper for [`StoreContextMut::run_concurrent`].
1040    pub async fn run_concurrent<R>(&mut self, fun: impl AsyncFnOnce(&Accessor<T>) -> R) -> Result<R>
1041    where
1042        T: Send + 'static,
1043    {
1044        ensure!(
1045            self.as_context().0.concurrency_support(),
1046            "cannot use `run_concurrent` when Config::concurrency_support disabled",
1047        );
1048        self.as_context_mut().run_concurrent(fun).await
1049    }
1050
1051    #[doc(hidden)]
1052    pub fn assert_concurrent_state_empty(&mut self) {
1053        self.as_context_mut().assert_concurrent_state_empty();
1054    }
1055
1056    #[doc(hidden)]
1057    pub fn concurrent_state_table_size(&mut self) -> usize {
1058        self.as_context_mut().concurrent_state_table_size()
1059    }
1060
1061    /// Convenience wrapper for [`StoreContextMut::spawn`].
1062    pub fn spawn(
1063        &mut self,
1064        task: impl for<'fut> AccessorTask<'fut, T, HasSelf<T>>,
1065    ) -> Result<JoinHandle>
1066    where
1067        T: 'static,
1068    {
1069        self.as_context_mut().spawn(task)
1070    }
1071}
1072
1073impl<T> StoreContextMut<'_, T> {
1074    /// Assert that all the relevant tables and queues in the concurrent state
1075    /// for this store are empty.
1076    ///
1077    /// This is for sanity checking in integration tests
1078    /// (e.g. `component-async-tests`) that the relevant state has been cleared
1079    /// after each test concludes.  This should help us catch leaks, e.g. guest
1080    /// tasks which haven't been deleted despite having completed and having
1081    /// been dropped by their supertasks.
1082    ///
1083    /// Only intended for use in Wasmtime's own testing.
1084    #[doc(hidden)]
1085    pub fn assert_concurrent_state_empty(self) {
1086        let store = self.0;
1087        store
1088            .store_data_mut()
1089            .components
1090            .assert_instance_states_empty();
1091        let state = store.concurrent_state_mut().unwrap();
1092        assert!(
1093            state.table.get_mut().is_empty(),
1094            "non-empty table: {:?}",
1095            state.table.get_mut()
1096        );
1097        assert!(state.switch_item.is_none());
1098        assert!(state.next_switch_item.is_none());
1099        assert!(state.high_priority.is_empty());
1100        assert!(state.low_priority.is_empty());
1101        assert!(state.unforced_current_thread.is_none());
1102        assert!(state.deferred_host_call_context.is_none());
1103        assert!(state.futures_mut().unwrap().is_empty());
1104        assert!(state.global_error_context_ref_counts.is_empty());
1105    }
1106
1107    /// Helper function to perform tests over the size of the concurrent state
1108    /// table which can be useful for detecting leaks.
1109    ///
1110    /// Only intended for use in Wasmtime's own testing.
1111    #[doc(hidden)]
1112    pub fn concurrent_state_table_size(&mut self) -> usize {
1113        self.0
1114            .concurrent_state_mut()
1115            .unwrap()
1116            .table
1117            .get_mut()
1118            .iter_mut()
1119            .count()
1120    }
1121
1122    /// Spawn a background task to run as part of this instance's event loop.
1123    ///
1124    /// The task will receive an `&Accessor<U>` and run concurrently with
1125    /// any other tasks in progress for the instance.
1126    ///
1127    /// Note that the task will only make progress if and when the event loop
1128    /// for this instance is run.
1129    ///
1130    /// The returned [`JoinHandle`] may be used to cancel the task.
1131    pub fn spawn(mut self, task: impl for<'fut> AccessorTask<'fut, T>) -> Result<JoinHandle>
1132    where
1133        T: 'static,
1134    {
1135        let accessor = Accessor::new(StoreToken::new(self.as_context_mut()));
1136        self.spawn_with_accessor(accessor, task)
1137    }
1138
1139    /// Internal implementation of `spawn` functions where a `store` is
1140    /// available along with an `Accessor`.
1141    fn spawn_with_accessor<D>(
1142        self,
1143        accessor: Accessor<T, D>,
1144        task: impl for<'fut> AccessorTask<'fut, T, D>,
1145    ) -> Result<JoinHandle>
1146    where
1147        T: 'static,
1148        D: HasData + ?Sized,
1149    {
1150        // Create an "abortable future" here where internally the future will
1151        // hook calls to poll and possibly spawn more background tasks on each
1152        // iteration.
1153        let (handle, future) = JoinHandle::run(async move { task.run(&accessor).await });
1154        self.0
1155            .concurrent_state_mut()?
1156            .push_future(Box::pin(async move { future.await.unwrap_or(Ok(())) }));
1157        Ok(handle)
1158    }
1159
1160    /// Run the specified closure `fun` to completion as part of this store's
1161    /// event loop.
1162    ///
1163    /// This will run `fun` as part of this store's event loop until it
1164    /// yields a result.  `fun` is provided an [`Accessor`], which provides
1165    /// controlled access to the store and its data.
1166    ///
1167    /// This function can be used to invoke [`Func::call_concurrent`] for
1168    /// example within the async closure provided here.
1169    ///
1170    /// This function will unconditionally return an error if
1171    /// [`Config::concurrency_support`] is disabled.
1172    ///
1173    /// [`Config::concurrency_support`]: crate::Config::concurrency_support
1174    ///
1175    /// # Store-blocking behavior
1176    ///
1177    /// At this time there are certain situations in which the `Future` returned
1178    /// by the `AsyncFnOnce` passed to this function will not be polled for an
1179    /// extended period of time, despite one or more `Waker::wake` events having
1180    /// occurred for the task to which it belongs.  This can manifest as the
1181    /// `Future` seeming to be "blocked" or "locked up", but is actually due to
1182    /// the `Store` being held by e.g. a blocking host function, preventing the
1183    /// `Future` from being polled. A canonical example of this is when the
1184    /// `fun` provided to this function attempts to set a timeout for an
1185    /// invocation of a wasm function. In this situation the async closure is
1186    /// waiting both on (a) the wasm computation to finish, and (b) the timeout
1187    /// to elapse. At this time this setup will not always work and the timeout
1188    /// may not reliably fire.
1189    ///
1190    /// This function will not block the current thread and as such is always
1191    /// suitable to run in an `async` context, but the current implementation of
1192    /// Wasmtime can lead to situations where a certain wasm computation is
1193    /// required to make progress the closure to make progress. This is an
1194    /// artifact of Wasmtime's historical implementation of `async` functions
1195    /// and is the topic of [#11869] and [#11870]. In the timeout example from
1196    /// above it means that Wasmtime can get "wedged" for a bit where (a) must
1197    /// progress for a readiness notification of (b) to get delivered.
1198    ///
1199    /// This effectively means that it's not possible to reliably perform a
1200    /// "select" operation within the `fun` closure, which timeouts for example
1201    /// are based on. Fixing this requires some relatively major refactoring
1202    /// work within Wasmtime itself. This is a known pitfall otherwise and one
1203    /// that is intended to be fixed one day. In the meantime it's recommended
1204    /// to apply timeouts or such to the entire `run_concurrent` call itself
1205    /// rather than internally.
1206    ///
1207    /// [#11869]: https://github.com/bytecodealliance/wasmtime/issues/11869
1208    /// [#11870]: https://github.com/bytecodealliance/wasmtime/issues/11870
1209    ///
1210    /// # Example
1211    ///
1212    /// ```
1213    /// # use {
1214    /// #   wasmtime::{
1215    /// #     error::{Result},
1216    /// #     component::{ Component, Linker, Resource, ResourceTable},
1217    /// #     Config, Engine, Store
1218    /// #   },
1219    /// # };
1220    /// #
1221    /// # struct MyResource(u32);
1222    /// # struct Ctx { table: ResourceTable }
1223    /// #
1224    /// # async fn foo() -> Result<()> {
1225    /// # let mut config = Config::new();
1226    /// # let engine = Engine::new(&config)?;
1227    /// # let mut store = Store::new(&engine, Ctx { table: ResourceTable::new() });
1228    /// # let mut linker = Linker::new(&engine);
1229    /// # let component = Component::new(&engine, "")?;
1230    /// # let instance = linker.instantiate_async(&mut store, &component).await?;
1231    /// # let foo = instance.get_typed_func::<(Resource<MyResource>,), (Resource<MyResource>,)>(&mut store, "foo")?;
1232    /// # let bar = instance.get_typed_func::<(u32,), ()>(&mut store, "bar")?;
1233    /// store.run_concurrent(async |accessor| -> wasmtime::Result<_> {
1234    ///    let resource = accessor.with(|mut access| access.get().table.push(MyResource(42)))?;
1235    ///    let (another_resource,) = foo.call_concurrent(accessor, (resource,)).await?;
1236    ///    let value = accessor.with(|mut access| access.get().table.delete(another_resource))?;
1237    ///    bar.call_concurrent(accessor, (value.0,)).await?;
1238    ///    Ok(())
1239    /// }).await??;
1240    /// # Ok(())
1241    /// # }
1242    /// ```
1243    pub async fn run_concurrent<R>(self, fun: impl AsyncFnOnce(&Accessor<T>) -> R) -> Result<R>
1244    where
1245        T: Send + 'static,
1246    {
1247        ensure!(
1248            self.0.concurrency_support(),
1249            "cannot use `run_concurrent` when Config::concurrency_support disabled",
1250        );
1251        self.do_run_concurrent(fun, false).await
1252    }
1253
1254    pub(super) async fn run_concurrent_trap_on_idle<R>(
1255        self,
1256        fun: impl AsyncFnOnce(&Accessor<T>) -> R,
1257    ) -> Result<R> {
1258        self.do_run_concurrent(fun, true).await
1259    }
1260
1261    async fn do_run_concurrent<R>(
1262        mut self,
1263        fun: impl AsyncFnOnce(&Accessor<T>) -> R,
1264        trap_on_idle: bool,
1265    ) -> Result<R> {
1266        debug_assert!(self.0.concurrency_support());
1267        let already_running = self
1268            .0
1269            .concurrent_state_mut_already_forced_current_thread()
1270            .event_loop_running;
1271        if already_running {
1272            bail!("Recursive `StoreContextMut::run_concurrent` calls not supported")
1273        }
1274        let token = StoreToken::new(self.as_context_mut());
1275
1276        struct Dropper<'a, T: 'static, V> {
1277            store: StoreContextMut<'a, T>,
1278            value: ManuallyDrop<V>,
1279        }
1280
1281        impl<'a, T, V> Drop for Dropper<'a, T, V> {
1282            fn drop(&mut self) {
1283                self.store
1284                    .0
1285                    .concurrent_state_mut_already_forced_current_thread()
1286                    .event_loop_running = false;
1287
1288                tls::set(self.store.0, || {
1289                    // SAFETY: Here we drop the value without moving it for the
1290                    // first and only time -- per the contract for `Drop::drop`,
1291                    // this code won't run again, and the `value` field will no
1292                    // longer be accessible.
1293                    unsafe { ManuallyDrop::drop(&mut self.value) }
1294                });
1295            }
1296        }
1297
1298        let accessor = &Accessor::new(token);
1299        self.0
1300            .concurrent_state_mut_already_forced_current_thread()
1301            .event_loop_running = true;
1302        let dropper = &mut Dropper {
1303            store: self,
1304            value: ManuallyDrop::new(fun(accessor)),
1305        };
1306        // SAFETY: We never move `dropper` nor its `value` field.
1307        let future = unsafe { Pin::new_unchecked(dropper.value.deref_mut()) };
1308
1309        let result = dropper
1310            .store
1311            .as_context_mut()
1312            .poll_until(future, trap_on_idle)
1313            .await;
1314
1315        if result.is_err() {
1316            dropper.store.0.set_trapped();
1317        }
1318
1319        result
1320    }
1321
1322    /// Run this store's event loop.
1323    ///
1324    /// The returned future will resolve when the specified future completes or,
1325    /// if `trap_on_idle` is true, when the event loop can't make further
1326    /// progress.
1327    async fn poll_until<R>(
1328        mut self,
1329        mut future: Pin<&mut impl Future<Output = R>>,
1330        trap_on_idle: bool,
1331    ) -> Result<R> {
1332        struct Reset<'a, T: 'static> {
1333            store: StoreContextMut<'a, T>,
1334            futures: Option<FuturesUnordered<HostTaskFuture>>,
1335        }
1336
1337        impl<'a, T> Drop for Reset<'a, T> {
1338            fn drop(&mut self) {
1339                if let Some(futures) = self.futures.take() {
1340                    *self
1341                        .store
1342                        .0
1343                        .concurrent_state_mut_already_forced_current_thread()
1344                        .futures
1345                        .get_mut() = Some(futures);
1346                }
1347            }
1348        }
1349
1350        // Keep a continuously busy event loop from holding one executor poll
1351        // indefinitely.  In particular, Tokio's cooperative budget is only
1352        // replenished when the future returns `Pending` to the executor.
1353        const MAX_TURNS_WITHOUT_YIELD: usize = 128;
1354        let mut turns_without_yield = 0;
1355
1356        loop {
1357            // Take `ConcurrentState::futures` out of the store so we can poll
1358            // it while also safely giving any of the futures inside access to
1359            // `self`.
1360            let futures = self.0.concurrent_state_mut()?.futures.get_mut().take();
1361            let mut reset = Reset {
1362                store: self.as_context_mut(),
1363                futures,
1364            };
1365            let mut next = match reset.futures.as_mut() {
1366                Some(f) => pin!(f.next()),
1367                None => bail_bug!("concurrent state missing futures field"),
1368            };
1369
1370            enum PollResult<R> {
1371                Complete(R),
1372                ProcessWork {
1373                    ready: Option<WorkItem>,
1374                    low_priority: bool,
1375                },
1376            }
1377
1378            let result = future::poll_fn(|cx| {
1379                // First, poll the future we were passed as an argument and
1380                // return immediately if it's ready.
1381                if let Poll::Ready(value) = tls::set(reset.store.0, || future.as_mut().poll(cx)) {
1382                    return Poll::Ready(Ok(PollResult::Complete(value)));
1383                }
1384
1385                // Next, poll `ConcurrentState::futures` (which includes any
1386                // pending host tasks and/or background tasks), returning
1387                // immediately if one of them fails.
1388                let next = match tls::set(reset.store.0, || next.as_mut().poll(cx)) {
1389                    Poll::Ready(Some(output)) => {
1390                        match output {
1391                            Err(e) => return Poll::Ready(Err(e)),
1392                            Ok(()) => {}
1393                        }
1394                        Poll::Ready(true)
1395                    }
1396                    Poll::Ready(None) => Poll::Ready(false),
1397                    Poll::Pending => Poll::Pending,
1398                };
1399
1400                // Next, identify the next work item to process, if any, using
1401                // the following priority order:
1402                //
1403                // - `switch_item`: Represents the guest thread we _must_ switch
1404                // to before any other thread runs per the determinism
1405                // requirements in the Component Model spec.
1406                //
1407                // - `high_priority`: "Urgent" work items, e.g. async calls have
1408                // become freshly unblocked due to backpressure clearing or
1409                // similar, stream or future state updates, etc.
1410                //
1411                // - `low_priority`: Work items such as resuming a fiber after
1412                // it yields, in which case the point is to let other items run
1413                // first.
1414                let state = reset.store.0.concurrent_state_mut()?;
1415                let mut ready = state.switch_item.take();
1416                let mut low_priority = false;
1417                if ready.is_none() {
1418                    ready = state.high_priority.pop_back();
1419                    if ready.is_none() {
1420                        ready = state.low_priority.pop_back();
1421                        low_priority = true;
1422                    }
1423                }
1424                if ready.is_some() {
1425                    return Poll::Ready(Ok(PollResult::ProcessWork {
1426                        ready,
1427                        low_priority,
1428                    }));
1429                }
1430
1431                // Finally, if we have nothing else to do right now, determine what to do
1432                // based on whether there are any pending futures in
1433                // `ConcurrentState::futures`.
1434                return match next {
1435                    Poll::Ready(true) => {
1436                        // In this case, one of the futures in
1437                        // `ConcurrentState::futures` completed
1438                        // successfully, so we return now and continue
1439                        // the outer loop in case there is another one
1440                        // ready to complete.
1441                        Poll::Ready(Ok(PollResult::ProcessWork {
1442                            ready: None,
1443                            low_priority: false,
1444                        }))
1445                    }
1446                    Poll::Ready(false) => {
1447                        // Poll the future we were passed one last time
1448                        // in case one of `ConcurrentState::futures` had
1449                        // the side effect of unblocking it.
1450                        if let Poll::Ready(value) =
1451                            tls::set(reset.store.0, || future.as_mut().poll(cx))
1452                        {
1453                            Poll::Ready(Ok(PollResult::Complete(value)))
1454                        } else {
1455                            // In this case, there are no more pending
1456                            // futures in `ConcurrentState::futures`,
1457                            // there are no remaining work items, _and_
1458                            // the future we were passed as an argument
1459                            // still hasn't completed.
1460                            if trap_on_idle {
1461                                // `trap_on_idle` is true, so we exit
1462                                // immediately.
1463
1464                                // If there are any tasks belonging to
1465                                // an instance which may not suspend, trap with
1466                                // `CannotBlockSyncTask`:
1467                                Poll::Ready(Err(if reset.store.0.any_may_not_suspend()? {
1468                                    Trap::CannotBlockSyncTask.into()
1469                                } else {
1470                                    // Otherwise, trap with `AsyncDeadlock`:
1471                                    Trap::AsyncDeadlock.into()
1472                                }))
1473                            } else {
1474                                // `trap_on_idle` is false, so we assume
1475                                // that future will wake up and give us
1476                                // more work to do when it's ready to.
1477                                Poll::Pending
1478                            }
1479                        }
1480                    }
1481                    // There is at least one pending future in
1482                    // `ConcurrentState::futures` and we have nothing
1483                    // else to do but wait for now, so we return
1484                    // `Pending`.
1485                    Poll::Pending => Poll::Pending,
1486                };
1487            })
1488            .await;
1489
1490            // Put the `ConcurrentState::futures` back into the store before we
1491            // return or handle any work items since one or more of those items
1492            // might append more futures.
1493            drop(reset);
1494
1495            match result? {
1496                // The future we were passed as an argument completed, so we
1497                // return the result.
1498                PollResult::Complete(value) => break Ok(value),
1499                // The future we were passed has not yet completed, so handle
1500                // any work items and then loop again.
1501                PollResult::ProcessWork {
1502                    ready,
1503                    low_priority,
1504                } => {
1505                    struct Dispose<'a, T: 'static> {
1506                        store: StoreContextMut<'a, T>,
1507                        ready: Option<WorkItem>,
1508                    }
1509
1510                    impl<'a, T> Drop for Dispose<'a, T> {
1511                        fn drop(&mut self) {
1512                            if let Some(item) = self.ready.take() {
1513                                match item {
1514                                    WorkItem::ResumeFiber { mut fiber, .. } => {
1515                                        fiber.dispose(self.store.0)
1516                                    }
1517                                    WorkItem::PushFuture(future) => {
1518                                        tls::set(self.store.0, move || drop(future))
1519                                    }
1520                                    _ => {}
1521                                }
1522                            }
1523                        }
1524                    }
1525
1526                    let mut dispose = Dispose {
1527                        store: self.as_context_mut(),
1528                        ready,
1529                    };
1530
1531                    // If we're about to run a low-priority task, first yield to
1532                    // the executor.  This ensures that it won't be starved of
1533                    // the ability to e.g. update the readiness of sockets,
1534                    // etc. which the guest may be using `thread.yield` along
1535                    // with `waitable-set.poll` to monitor in a CPU-heavy loop.
1536                    //
1537                    // This works for e.g. `thread.yield` and callbacks which
1538                    // return `CALLBACK_CODE_YIELD` because we queue a low
1539                    // priority item to resume the task (i.e. resume the thread
1540                    // or call the callback, respectively) just prior to
1541                    // suspending it.  Indeed, as of this writing those are the
1542                    // _only_ situations we queue low-priority tasks.
1543                    // Therefore, we interpret the guest's request to yield as
1544                    // meaning "yield to other guest tasks _and_/_or_ host
1545                    // operations such as updating socket readiness", the latter
1546                    // being the async runtime's responsibility.
1547                    //
1548                    // In the future, if this ends up causing measurable
1549                    // performance issues, this could be optimized such that we
1550                    // only yield periodically (e.g. for batches of low priority
1551                    // items) and not for each and every individual item.
1552                    if low_priority {
1553                        dispose.store.0.yield_now().await;
1554                        turns_without_yield = 0;
1555                    }
1556
1557                    if let Some(item) = dispose.ready.take() {
1558                        dispose
1559                            .store
1560                            .as_context_mut()
1561                            .handle_work_item(item)
1562                            .await?;
1563                    }
1564
1565                    turns_without_yield += 1;
1566                    if turns_without_yield == MAX_TURNS_WITHOUT_YIELD {
1567                        turns_without_yield = 0;
1568                        dispose.store.0.yield_now().await;
1569                    }
1570                }
1571            }
1572        }
1573    }
1574
1575    /// Handle the specified work item, possibly resuming a fiber if applicable.
1576    async fn handle_work_item(self, item: WorkItem) -> Result<()> {
1577        log::trace!("handle work item {item:?}");
1578        match item {
1579            WorkItem::PushFuture(future) => {
1580                self.0
1581                    .concurrent_state_mut()?
1582                    .futures_mut()?
1583                    .push(future.into_inner());
1584            }
1585            WorkItem::ResumeFiber { fiber, .. } => {
1586                self.0.resume_fiber(fiber).await?;
1587            }
1588            WorkItem::ResumeThread { thread, .. } => {
1589                if let GuestThreadState::Ready { fiber, .. } = mem::replace(
1590                    &mut self.0.concurrent_state_mut()?.get_mut(thread.thread)?.state,
1591                    GuestThreadState::Running,
1592                ) {
1593                    self.0.resume_fiber(fiber).await?;
1594                } else {
1595                    bail_bug!("cannot resume non-pending thread {thread:?}");
1596                }
1597            }
1598            WorkItem::GuestCall { call, .. } => {
1599                if call.is_ready(self.0)? {
1600                    self.0
1601                        .concurrent_state_mut()?
1602                        .get_mut(call.thread.thread)?
1603                        .wake_on_cancel = WakeOnCancel::None;
1604                    self.run_on_worker(WorkerItem::GuestCall(call)).await?;
1605                } else {
1606                    let state = self.0.concurrent_state_mut()?;
1607                    let task = state.get_mut(call.thread.task)?;
1608                    if !task.starting_sent {
1609                        task.starting_sent = true;
1610                        if let GuestCallKind::StartImplicit(_) = &call.kind {
1611                            Waitable::Guest(call.thread.task).set_event(
1612                                state,
1613                                Some(Event::Subtask {
1614                                    status: Status::Starting,
1615                                }),
1616                            )?;
1617                        }
1618                    }
1619
1620                    let instance = state.get_mut(call.thread.task)?.instance;
1621                    self.0
1622                        .instance_state(instance)
1623                        .concurrent_state()
1624                        .pending
1625                        .insert(call.thread, call.kind);
1626
1627                    // Switch back to the caller (or canceller) immediately if
1628                    // applicable since we aren't yet able to run the subtask it
1629                    // yielded to.
1630                    self.0.concurrent_state_mut()?.take_next_switch_item()?;
1631                }
1632            }
1633            WorkItem::WorkerFunction(fun) => {
1634                self.run_on_worker(WorkerItem::Function(fun)).await?;
1635            }
1636        }
1637
1638        Ok(())
1639    }
1640
1641    /// Execute the specified guest call on a worker fiber.
1642    async fn run_on_worker(self, item: WorkerItem) -> Result<()> {
1643        let worker = if let Some(fiber) = self.0.concurrent_state_mut()?.worker.take() {
1644            fiber
1645        } else {
1646            // SAFETY: the `make_fiber_unchecked` function is unsafe because the
1647            // returned fiber is unconditionally `Send` as opposed to being
1648            // conditionally send depending on the argument (in this case
1649            // `self.0`). This `async` function, however, is conditionally
1650            // `Send` depending on `self`, in this case `StoreContextMut<T>`,
1651            // which is already going to be conditionally `Send` depending on
1652            // `T`.
1653            //
1654            // The returned fiber is possibly stored within the `Store<T>` as
1655            // well. If `T: Send` then that's fine and everything's dandy. If
1656            // `T: !Send`, however, then the store is already not-`Send` meaning
1657            // that putting more actually-not-`Send` things inside of it isn't
1658            // an issue.
1659            //
1660            // The main issue here is that the returned fiber effectively can't
1661            // get transferred outside the context of the store. That's an
1662            // implementation detail we'll have to rely on, but is currently
1663            // true.
1664            unsafe {
1665                fiber::make_fiber_unchecked(self.0, move |store| {
1666                    loop {
1667                        let Some(item) = store.concurrent_state_mut()?.worker_item.take() else {
1668                            bail_bug!("worker_item not present when resuming fiber")
1669                        };
1670                        match item {
1671                            WorkerItem::GuestCall(call) => handle_guest_call(store, call)?,
1672                            WorkerItem::Function(fun) => fun.into_inner()(store)?,
1673                        }
1674
1675                        store.suspend(SuspendReason::NeedWork)?;
1676                    }
1677                })?
1678            }
1679        };
1680
1681        let worker_item = &mut self.0.concurrent_state_mut()?.worker_item;
1682        assert!(worker_item.is_none());
1683        *worker_item = Some(item);
1684
1685        self.0.resume_fiber(worker).await
1686    }
1687
1688    /// Wrap the specified host function in a future which will call it, passing
1689    /// it an `&Accessor<T>`.
1690    ///
1691    /// See the `Accessor` documentation for details.
1692    pub(crate) fn wrap_call<F, R>(self, closure: F) -> impl Future<Output = Result<R>> + 'static
1693    where
1694        T: 'static,
1695        F: FnOnce(&Accessor<T>) -> Pin<Box<dyn Future<Output = Result<R>> + Send + '_>>
1696            + Send
1697            + Sync
1698            + 'static,
1699        R: Send + Sync + 'static,
1700    {
1701        let token = StoreToken::new(self);
1702        async move {
1703            let mut accessor = Accessor::new(token);
1704            closure(&mut accessor).await
1705        }
1706    }
1707
1708    pub(crate) async fn start_instance(
1709        &mut self,
1710        instance: ModuleInstance,
1711    ) -> Result<ModuleInstance> {
1712        let (tx, rx) = oneshot::channel();
1713        let token = StoreToken::new(self.as_context_mut());
1714        self.0.queue_task(move |store| {
1715            _ = tx.send(
1716                instance
1717                    .start_raw(&mut token.as_context_mut(store))
1718                    .map(|()| instance),
1719            );
1720            Ok(())
1721        })?;
1722        self.as_context_mut()
1723            .run_concurrent_trap_on_idle(async |_| {
1724                rx.await
1725                    .map_err(|_| format_err!("oneshot channel canceled"))
1726            })
1727            .await??
1728    }
1729}
1730
1731/// Return value of [`StoreOpaque::host_task_create`].
1732///
1733/// This is an `Option` to handle the dynamic `store.concurrency_support()`
1734/// property. When present this records the guest thread to restore when the
1735/// host call exits. The corresponding [`HostTask`] will need to be lazily
1736/// created if needed via [`StoreOpaque::materialize_host_task_id`].
1737pub type EnteredHostTask = Option<QualifiedThreadId>;
1738
1739impl StoreOpaque {
1740    /// Returns the currently-running thread, promoting any deferred lazy guest
1741    /// thread into a fully-materialized `CurrentThread`. Deferred [`HostTask`]s
1742    /// are not materialized.
1743    #[inline]
1744    pub(crate) fn current_thread(&mut self) -> Result<CurrentThread> {
1745        // Without concurrency support there is nothing to force.
1746        if !self.concurrency_support() {
1747            return Ok(CurrentThread::None);
1748        }
1749
1750        // If the JIT-visible current thread isn't a deferred thread then
1751        // `ConcurrentState` is already up to date.
1752        if !self
1753            .vm_store_context_mut()
1754            .current_thread_mut()
1755            .is_deferred()
1756        {
1757            return Ok(self
1758                .concurrent_state_mut_already_forced_current_thread()
1759                .unforced_current_thread);
1760        }
1761
1762        self.force_deferred_current_thread()
1763    }
1764
1765    /// Slow path of [`Self::current_thread`]: promote the deferred lazy
1766    /// thread into a fully-materialized `CurrentThread`.
1767    #[cold]
1768    fn force_deferred_current_thread(&mut self) -> Result<CurrentThread> {
1769        // The component instance whose adapters pushed the deferred frames; all
1770        // frames in a guest-to-guest, sync-to-sync call chain of fused adapters
1771        // live within a single `wasmtime::component::Instance` (because
1772        // cross-`wasmtime::component::Instance` calls don't go through fused
1773        // adapters), and guest code only ever runs as a guest thread, so the
1774        // chain's base thread is already materialized in `ConcurrentState` and
1775        // we can get the `ComponentInstanceId` shared by the whole chain from
1776        // here.
1777        let state = self.concurrent_state_mut_without_forcing_current_thread();
1778        let id = match state.unforced_current_thread.guest_task() {
1779            Some(task) => state.get_mut(task)?.instance.instance,
1780            None => bail_bug!("deferred component-model thread with non-guest base"),
1781        };
1782
1783        // Collect the deferred frames pushed inline by fused adapters, walking
1784        // the `parent` chain from innermost to the base.
1785        let mut frames = Vec::new();
1786        let mut cur = *self.vm_store_context_mut().current_thread_mut();
1787        while let Some(ptr) = cur.as_deferred() {
1788            // SAFETY: `ptr` points at a `VMDeferredThread` living in a fused
1789            // adapter's stack frame that is suspended below us on the stack
1790            // (mid-call, waiting for this nested call to return), so the
1791            // referent is still valid and exclusively ours to read.
1792            let deferred = unsafe { ptr.as_non_null().as_ref() };
1793            frames.push((
1794                deferred.callee_async != 0,
1795                deferred.callee_instance,
1796                deferred.saved_context,
1797            ));
1798            cur = deferred.parent;
1799        }
1800
1801        // Mark the current thread forced *before* replaying so that any
1802        // reentrant `force_current_thread` call short-circuits via the
1803        // non-deferred path above.
1804        *self.vm_store_context_mut().current_thread_mut() = VMLazyThread::forced();
1805
1806        // Save the current context, as we need to overwrite it while replaying
1807        // below.
1808        let current_context = *self.vm_store_context_mut().component_context_mut();
1809
1810        // Replay the deferred `enter_guest_sync_call`s outermost-first so that
1811        // the resulting `ConcurrentState` matches what the non-deferred path
1812        // would have otherwise produced.
1813        for (callee_async, callee_instance, saved_context) in frames.into_iter().rev() {
1814            // Restore the caller's context slots so that we save the correct
1815            // values into the caller's thread, exactly as the non-deferred path
1816            // would have on entry.
1817            *self.vm_store_context_mut().component_context_mut() = saved_context;
1818            let callee = RuntimeInstance {
1819                instance: id,
1820                index: RuntimeComponentInstanceIndex::from_u32(callee_instance),
1821            };
1822            self.enter_guest_sync_call(callee_async, callee)?;
1823        }
1824
1825        // Replaying done; restore the current context.
1826        *self.vm_store_context_mut().component_context_mut() = current_context;
1827
1828        Ok(self
1829            .concurrent_state_mut_without_forcing_current_thread()
1830            .unforced_current_thread)
1831    }
1832
1833    fn current_guest_thread(&mut self) -> Result<QualifiedThreadId> {
1834        match self.current_thread()?.guest() {
1835            Some(id) => Ok(*id),
1836            None => bail_bug!("current thread is not a guest thread"),
1837        }
1838    }
1839
1840    // A result of `None` may indicate that this is either the top-level event
1841    // loop, a deferred host task, or concurrency support is disabled. In all
1842    // cases we don't have an ID for the task.
1843    pub(crate) fn current_materialized_host_task(&mut self) -> Result<Option<TableId<HostTask>>> {
1844        match self.current_thread()? {
1845            CurrentThread::Host(id) => Ok(Some(id)),
1846            CurrentThread::DeferredHost(_) | CurrentThread::None => Ok(None),
1847            _ => bail_bug!("current thread is not a host thread"),
1848        }
1849    }
1850
1851    /// Returns the current host task ID, materializing a deferred host task if
1852    /// one is active. `None` represents a call from the top-level host.
1853    fn materialize_host_task_id(&mut self) -> Result<Option<TableId<HostTask>>> {
1854        Ok(self
1855            .concurrent_state_mut()?
1856            .materialize_current_host_task_id()?)
1857    }
1858
1859    fn enter_sync_call(&mut self, callee: RuntimeInstance) -> Result<()> {
1860        log::trace!("enter sync-typed call {callee:?}");
1861        let state = self.instance_state(callee).concurrent_state();
1862        let old_do_not_suspend = state.do_not_suspend;
1863        state.do_not_suspend = true;
1864
1865        let thread = self.current_guest_thread()?;
1866        let thread = self.concurrent_state_mut()?.get_mut(thread.thread)?;
1867        if thread.old_do_not_suspend.is_some() {
1868            bail_bug!("current thread already has `old_do_not_suspend` value");
1869        }
1870
1871        thread.old_do_not_suspend = Some(old_do_not_suspend);
1872
1873        Ok(())
1874    }
1875
1876    fn exit_sync_call(&mut self, callee: RuntimeInstance) -> Result<()> {
1877        log::trace!("exit sync-typed call {callee:?}");
1878        let thread = self.current_guest_thread()?;
1879        let thread = self.concurrent_state_mut()?.get_mut(thread.thread)?;
1880        let Some(old_do_not_suspend) = thread.old_do_not_suspend.take() else {
1881            bail_bug!("current thread missing `old_do_not_suspend` value");
1882        };
1883        let state = self.instance_state(callee).concurrent_state();
1884        state.do_not_suspend = old_do_not_suspend;
1885        Ok(())
1886    }
1887
1888    /// Push a `GuestTask` onto the task stack for either a sync-to-sync,
1889    /// guest-to-guest call or a sync host-to-guest call.
1890    ///
1891    /// This task will only be used for the purpose of handling calls to
1892    /// intrinsic functions; both parameter lowering and result lifting are
1893    /// assumed to be taken care of elsewhere.
1894    ///
1895    /// NB: for sync-to-sync, guest-to-guest calls we delay task construction in
1896    /// fused adapters, see `StoreOpaque::current_thread`, `VMDeferredThread`,
1897    /// and `lower_fact_enter_sync_call`. Make sure all this stuff stays in
1898    /// sync!
1899    pub(crate) fn enter_guest_sync_call(
1900        &mut self,
1901        callee_async_typed: bool,
1902        callee: RuntimeInstance,
1903    ) -> Result<()> {
1904        log::trace!("enter sync-lifted call {callee:?}");
1905        if !self.concurrency_support() {
1906            return self.enter_call_not_concurrent();
1907        }
1908
1909        let thread = self.current_thread()?;
1910        let caller = if let Some(thread) = thread.guest() {
1911            Caller::Guest { thread: *thread }
1912        } else {
1913            Caller::Host {
1914                tx: None,
1915                host_future_present: false,
1916                caller: self.materialize_host_task_id()?,
1917            }
1918        };
1919
1920        let state = self.concurrent_state_mut()?;
1921        let guest_thread = GuestTask::new(
1922            state,
1923            Box::new(move |_, _| bail_bug!("cannot lower params in sync call")),
1924            LiftResult {
1925                lift: Box::new(move |_, _| bail_bug!("cannot lift result in sync call")),
1926                ty: TypeTupleIndex::reserved_value(),
1927                memory: None,
1928                string_encoding: StringEncoding::Utf8,
1929            },
1930            caller,
1931            None,
1932            callee,
1933            callee_async_typed,
1934            true,
1935        )?;
1936
1937        Instance::from_wasmtime(self, callee.instance).add_guest_thread_to_instance_table(
1938            guest_thread.thread,
1939            self,
1940            callee.index,
1941        )?;
1942        self.set_thread(guest_thread)?;
1943
1944        if !callee_async_typed {
1945            self.enter_sync_call(callee)?;
1946        }
1947
1948        Ok(())
1949    }
1950
1951    /// Pop a `GuestTask` previously pushed using `enter_guest_sync_call`.
1952    ///
1953    /// NB: for sync-to-sync, guest-to-guest calls we delay task construction in
1954    /// fused adapters and then when the call returns we check to see if the
1955    /// task's contruction was forced and if not avoid calling out of the JIT
1956    /// code to this function. See `lower_fact_exit_sync_call`. Make sure all
1957    /// this stuff stays in sync!
1958    pub(crate) fn exit_guest_sync_call(&mut self) -> Result<()> {
1959        if !self.concurrency_support() {
1960            return Ok(self.exit_call_not_concurrent());
1961        }
1962
1963        let thread = match self.current_thread()?.guest() {
1964            Some(t) => *t,
1965            None => bail_bug!("expected task when exiting"),
1966        };
1967        let task = self.concurrent_state_mut()?.get_mut(thread.task)?;
1968        let instance = task.instance;
1969
1970        let caller = match &task.caller {
1971            &Caller::Guest { thread } => thread.into(),
1972            &Caller::Host { caller, .. } => caller
1973                .map(CurrentThread::Host)
1974                .unwrap_or(CurrentThread::None),
1975        };
1976        task.lift_result = None;
1977        task.exited = true;
1978        let async_typed = task.async_typed;
1979
1980        if !async_typed {
1981            self.exit_sync_call(instance)?;
1982        }
1983
1984        self.set_thread(caller)?;
1985
1986        log::trace!("exit sync-lifted call {instance:?}");
1987
1988        if async_typed {
1989            // If we're async-typed, returning control to our caller won't help
1990            // resolve any outstanding sync-typed call which might be in
1991            // progress, so we may need to switch or trap before exiting this
1992            // thread:
1993            self.switch_or_trap_if_may_not_suspend(instance)?;
1994        }
1995
1996        self.cleanup_thread(thread, instance, CleanupTask::Yes)?;
1997
1998        Ok(())
1999    }
2000
2001    /// Similar to `enter_guest_sync_call` except for when the guest makes a
2002    /// transition to the host.
2003    ///
2004    /// This initially records a deferred host call. A full [`HostTask`] should
2005    /// be allocated later if needed via
2006    /// [`StoreOpaque::materialize_host_task_id`].
2007    pub(crate) fn host_task_create(&mut self) -> Result<EnteredHostTask> {
2008        if !self.concurrency_support() {
2009            self.enter_call_not_concurrent()?;
2010            return Ok(None);
2011        }
2012        let caller = self.current_guest_thread()?;
2013        log::trace!("new deferred host task with caller {caller:?}");
2014
2015        self.set_thread(CurrentThread::DeferredHost(caller))?;
2016        let state = self.concurrent_state_mut()?;
2017        debug_assert!(state.deferred_host_call_context.is_none());
2018        state.deferred_host_call_context = Some(CallContext::default());
2019        state.debug_assert_deferred_host_invariant();
2020        Ok(Some(caller))
2021    }
2022
2023    /// Dual of `host_task_create` and signifies that the host has finished and
2024    /// will be cleaned up.
2025    ///
2026    /// Note that this isn't invoked when the host is invoked asynchronously and
2027    /// the host isn't complete yet. In that situation the host task persists
2028    /// and will be cleaned up separately in `subtask_drop`
2029    pub(crate) fn host_task_delete(
2030        &mut self,
2031        original_task: EnteredHostTask,
2032        materialized_task: Option<TableId<HostTask>>,
2033    ) -> Result<()> {
2034        match original_task {
2035            Some(caller) => {
2036                self.set_thread(caller)?;
2037                if materialized_task.is_none() {
2038                    let state = self.concurrent_state_mut()?;
2039                    let context = state
2040                        .deferred_host_call_context
2041                        .take()
2042                        .expect("deferred host call context should be present");
2043                    debug_assert!(context.is_empty());
2044                    state.debug_assert_deferred_host_invariant();
2045                }
2046                log::trace!(
2047                    "delete host task with caller {original_task:?} and materialized as {materialized_task:?}"
2048                );
2049                if let Some(task) = materialized_task {
2050                    Waitable::Host(task).delete_from(self)?;
2051                }
2052            }
2053            None => {
2054                debug_assert!(materialized_task.is_none());
2055                self.exit_call_not_concurrent();
2056            }
2057        }
2058        Ok(())
2059    }
2060
2061    /// Helper function to retrieve the `InstanceState` for the
2062    /// specified instance.
2063    fn instance_state(&mut self, instance: RuntimeInstance) -> &mut InstanceState {
2064        self.component_instance_mut(instance.instance)
2065            .instance_state(instance.index)
2066    }
2067
2068    /// Configure the currently running `thread`.
2069    ///
2070    /// This will save off any state necessary for the previous thread, if
2071    /// applicable, and then it'll additionally update state for `thread` if
2072    /// needed too.
2073    pub(crate) fn set_thread(&mut self, thread: impl Into<CurrentThread>) -> Result<CurrentThread> {
2074        let thread = thread.into();
2075        let state = self.concurrent_state_mut()?;
2076        state.debug_assert_deferred_host_invariant();
2077        let old_thread = mem::replace(&mut state.unforced_current_thread, thread);
2078
2079        state.handle_thread_switch(old_thread, thread)?;
2080
2081        // First thing to do after swapping threads is updating the context
2082        // slots for this thread within the store. This restores the behavior of
2083        // `context.{get,set}`. This involves taking the old state out of the
2084        // store, saving it in the thread that's being swapped from, and doing
2085        // the inverse for the new thread. When debug assertions are enabled
2086        // this also leaves behind sentinel values to try to uncover bugs where
2087        // this may be forgotten.
2088        if let Some(old_thread) = old_thread.guest() {
2089            let old_context = *self.vm_store_context_mut().component_context_mut();
2090            self.concurrent_state_mut()?
2091                .get_mut(old_thread.thread)?
2092                .context = old_context;
2093        }
2094        if cfg!(debug_assertions) {
2095            *self.vm_store_context_mut().component_context_mut() =
2096                [u32::MAX; NUM_COMPONENT_CONTEXT_SLOTS];
2097        }
2098        if let Some(thread) = thread.guest() {
2099            let thread = self.concurrent_state_mut()?.get_mut(thread.thread)?;
2100            let context = thread.context;
2101            if cfg!(debug_assertions) {
2102                thread.context = [u32::MAX; NUM_COMPONENT_CONTEXT_SLOTS];
2103            }
2104            *self.vm_store_context_mut().component_context_mut() = context;
2105        }
2106
2107        // Keep the JIT-visible current-thread pointer in sync.
2108        *self.vm_store_context_mut().current_thread_mut() = if thread.is_none() {
2109            VMLazyThread::none()
2110        } else {
2111            VMLazyThread::forced()
2112        };
2113
2114        Ok(old_thread)
2115    }
2116
2117    /// Call `switch_if_may_not_suspend` and trap if it returns `false`.
2118    fn switch_or_trap_if_may_not_suspend(&mut self, instance: RuntimeInstance) -> Result<()> {
2119        if self.switch_if_may_not_suspend(instance)? {
2120            Ok(())
2121        } else {
2122            Err(Trap::CannotBlockSyncTask.into())
2123        }
2124    }
2125
2126    /// Check if the specified instance has a sync-typed call in progress; if so
2127    /// attempt to switch to another ready thread for that instance, and if no
2128    /// such thread exists, return false.
2129    fn switch_if_may_not_suspend(&mut self, instance: RuntimeInstance) -> Result<bool> {
2130        // Call this for the side effect of forcing any deferred task creation,
2131        // which may influence the value of `ConcurrentState::do_not_suspend`
2132        // below:
2133        self.concurrent_state_mut()?;
2134
2135        Ok(!self.concurrency_support()
2136            || !self
2137                .instance_state(instance)
2138                .concurrent_state()
2139                .do_not_suspend
2140            || self
2141                .concurrent_state_mut()?
2142                .promote_instance_local_thread_work_item(instance)?)
2143    }
2144
2145    /// Record that we're about to enter a (sub-)component instance which does
2146    /// not support more than one concurrent, stackful activation, meaning it
2147    /// cannot be entered again until the next call returns.
2148    fn enter_instance(&mut self, instance: RuntimeInstance) {
2149        log::trace!("enter {instance:?}");
2150        self.instance_state(instance)
2151            .concurrent_state()
2152            .do_not_enter = true;
2153    }
2154
2155    /// Record that we've exited a (sub-)component instance previously entered
2156    /// with `Self::enter_instance` and then calls `Self::partition_pending`.
2157    /// See the documentation for the latter for details.
2158    fn exit_instance(&mut self, instance: RuntimeInstance) -> Result<()> {
2159        log::trace!("exit {instance:?}");
2160        self.instance_state(instance)
2161            .concurrent_state()
2162            .do_not_enter = false;
2163        self.partition_pending(instance)
2164    }
2165
2166    /// Iterate over `InstanceState::pending`, moving any ready items into the
2167    /// "high priority" work item queue.
2168    ///
2169    /// Also, notify `ConcurrentState::ready_for_concurrent_call_waker` if
2170    /// present.
2171    ///
2172    /// See `GuestCall::is_ready` for details.
2173    fn partition_pending(&mut self, instance: RuntimeInstance) -> Result<()> {
2174        for (thread, kind) in
2175            mem::take(&mut self.instance_state(instance).concurrent_state().pending).into_iter()
2176        {
2177            let call = GuestCall { thread, kind };
2178            if call.is_ready(self)? {
2179                self.concurrent_state_mut()?
2180                    .push_high_priority(WorkItem::GuestCall { instance, call });
2181            } else {
2182                self.instance_state(instance)
2183                    .concurrent_state()
2184                    .pending
2185                    .insert(call.thread, call.kind);
2186            }
2187        }
2188
2189        if let Some(waker) = self
2190            .concurrent_state_mut()?
2191            .ready_for_concurrent_call_waker
2192            .take()
2193        {
2194            waker.wake();
2195        }
2196
2197        Ok(())
2198    }
2199
2200    /// Implements the `backpressure.{inc,dec}` intrinsics.
2201    pub(crate) fn backpressure_modify(
2202        &mut self,
2203        caller_instance: RuntimeInstance,
2204        modify: impl FnOnce(u16) -> Option<u16>,
2205    ) -> Result<()> {
2206        let state = self.instance_state(caller_instance).concurrent_state();
2207        let old = state.backpressure;
2208        let new = modify(old).ok_or_else(|| Trap::BackpressureOverflow)?;
2209        state.backpressure = new;
2210
2211        if old > 0 && new == 0 {
2212            // Backpressure was previously enabled and is now disabled; move any
2213            // newly-eligible guest calls to the "high priority" queue.
2214            self.partition_pending(caller_instance)?;
2215        }
2216
2217        Ok(())
2218    }
2219
2220    /// Resume the specified fiber, giving it exclusive access to the specified
2221    /// store.
2222    async fn resume_fiber(&mut self, fiber: StoreFiber<'static>) -> Result<()> {
2223        let old_thread = self.current_thread()?;
2224        log::trace!("resume_fiber: save current thread {old_thread:?}");
2225
2226        let fiber = fiber::resolve_or_release(self, fiber).await?;
2227
2228        self.set_thread(old_thread)?;
2229
2230        let state = self.concurrent_state_mut()?;
2231
2232        if let Some(ot) = old_thread.guest() {
2233            state.get_mut(ot.thread)?.state = GuestThreadState::Running;
2234        }
2235        log::trace!("resume_fiber: restore current thread {old_thread:?}");
2236
2237        if let Some(mut fiber) = fiber {
2238            log::trace!("resume_fiber: suspend reason {:?}", &state.suspend_reason);
2239            // See the `SuspendReason` documentation for what each case means.
2240            let reason = match state.suspend_reason.take() {
2241                Some(r) => r,
2242                None => bail_bug!("suspend reason missing when resuming fiber"),
2243            };
2244            match reason {
2245                SuspendReason::NeedWork => {
2246                    if state.worker.is_none() {
2247                        state.worker = Some(fiber);
2248                    } else {
2249                        fiber.dispose(self);
2250                    }
2251                }
2252                SuspendReason::Yielding { thread } => {
2253                    state.get_mut(thread.thread)?.state = GuestThreadState::Ready { fiber };
2254                    let instance = state.get_mut(thread.task)?.instance;
2255                    state.push_low_priority(WorkItem::ResumeThread { instance, thread });
2256                }
2257                SuspendReason::ExplicitlySuspending { thread } => {
2258                    state.get_mut(thread.thread)?.state = GuestThreadState::Suspended(fiber);
2259                }
2260                SuspendReason::Waiting { set, thread } => {
2261                    let old = state
2262                        .get_mut(set)?
2263                        .waiting
2264                        .insert(thread, WaitMode::Fiber(fiber));
2265                    assert!(old.is_none());
2266                }
2267                SuspendReason::YieldingToSubtask { thread } => {
2268                    // In this case, the thread has either invoked or sent a
2269                    // cancel request to a subtask, and is now yielding to that
2270                    // subtask.  According to the CM spec, that subtask may only
2271                    // yield back to the original thread the first time it
2272                    // suspends or exits (or a thread that it has resumed
2273                    // suspends or exits, etc.), which we ensure by setting
2274                    // `ConcurrentState::next_switch_item` here.
2275
2276                    let item = WorkItem::ResumeFiber {
2277                        instance: state.get_mut(thread.task)?.instance,
2278                        thread,
2279                        fiber,
2280                    };
2281
2282                    if state.next_switch_item.replace(item).is_some() {
2283                        // This should be unreachable per the save/restore code
2284                        // in `Self::suspend`.
2285                        bail_bug!(
2286                            "`ConcurrentState::next_switch_item` was already `Some(_)` when \
2287                             a thread wanted to wait on a subtask"
2288                        );
2289                    }
2290                }
2291            };
2292        } else {
2293            log::trace!("resume_fiber: fiber has exited");
2294        }
2295
2296        Ok(())
2297    }
2298
2299    /// Suspend the current fiber, storing the reason in
2300    /// `ConcurrentState::suspend_reason` to indicate the conditions under which
2301    /// it should be resumed.
2302    ///
2303    /// See the `SuspendReason` documentation for details.
2304    fn suspend(&mut self, reason: SuspendReason) -> Result<()> {
2305        log::trace!("suspend fiber: {reason:?}");
2306
2307        let state = self.concurrent_state_mut()?;
2308
2309        // If we're yielding or waiting on behalf of a guest thread, we'll be
2310        // overwriting the current thread, so save it now and restore it once
2311        // we've resumed.
2312        //
2313        // Also, if we're yielding to a subtask, we're about to overwrite
2314        // `ConcurrentState::next_switch_item`, so also save and restore that.
2315        let (save_and_restore_thread, save_and_restore_next_switch_item) = match &reason {
2316            SuspendReason::Yielding { .. }
2317            | SuspendReason::Waiting { .. }
2318            | SuspendReason::ExplicitlySuspending { .. } => {
2319                // If there's a thread waiting for this subtask to suspend, this
2320                // is a good time to switch back to it.
2321                if state.switch_item.is_none() {
2322                    state.take_next_switch_item()?;
2323                }
2324
2325                (true, false)
2326            }
2327            SuspendReason::YieldingToSubtask { .. } => (true, true),
2328            SuspendReason::NeedWork => (false, false),
2329        };
2330
2331        let old_next_switch_item = if save_and_restore_next_switch_item {
2332            let item = state.next_switch_item.take();
2333            // Note that we store it in the table here rather than directly in a
2334            // local variable to ensure the fiber is disposed of properly if we
2335            // end up trapping or panicking.
2336            Some(state.push(item)?)
2337        } else {
2338            None
2339        };
2340
2341        let old_guest_thread = if save_and_restore_thread {
2342            self.current_thread()?
2343        } else {
2344            CurrentThread::None
2345        };
2346
2347        let suspend_reason = &mut self.concurrent_state_mut()?.suspend_reason;
2348        assert!(suspend_reason.is_none());
2349        *suspend_reason = Some(reason);
2350
2351        // We'll panic if we call `Self::with_blocking` when the fiber is being
2352        // disposed, so check for that and exit ASAP if appropriate.
2353        if !self.fiber_async_state_mut().can_block() {
2354            return Err(format_err!("future dropped"));
2355        }
2356
2357        self.with_blocking(|_, cx| cx.suspend(StoreFiberYield::ReleaseStore))?;
2358
2359        if save_and_restore_thread {
2360            self.set_thread(old_guest_thread)?;
2361        }
2362
2363        if let Some(item) = old_next_switch_item {
2364            let state = self.concurrent_state_mut()?;
2365            state.next_switch_item = state.delete(item)?;
2366        }
2367
2368        Ok(())
2369    }
2370
2371    fn wait_for_event(
2372        &mut self,
2373        caller_instance: RuntimeInstance,
2374        waitable: Waitable,
2375    ) -> Result<()> {
2376        let caller = self.current_guest_thread()?;
2377        let state = self.concurrent_state_mut()?;
2378
2379        waitable.trap_if_in_waitable_set(state)?;
2380
2381        let set = state.get_mut(caller.thread)?.sync_call_set;
2382        waitable.join(state, Some(set))?;
2383
2384        self.switch_or_trap_if_may_not_suspend(caller_instance)?;
2385
2386        self.suspend(SuspendReason::Waiting {
2387            set,
2388            thread: caller,
2389        })?;
2390        let state = self.concurrent_state_mut()?;
2391
2392        waitable.join(state, None)
2393    }
2394
2395    /// Cleans up the data structures backing the `guest_thread` specified,
2396    /// removing it from the internal tables of `runtime_instance` as well.
2397    ///
2398    /// This function is used whenever a guest thread has fully exited and
2399    /// completed. This'll clean up the associated `GuestThread` structure and
2400    /// related resources it contains.
2401    ///
2402    /// Other functionality that this implements is:
2403    ///
2404    /// * This will perform conditional cleanup of the `GuestTask` that owns
2405    ///   this thread if `cleanup_task` is `CleanupTask::Yes`.
2406    /// * If there are no more threads in the `GuestTask` that this thread is
2407    ///   associated with, and if the task hasn't produced a result (e.g. it's not
2408    ///   returned or cancelled), then a trap will be raised that a result
2409    ///   wasn't ever produced.
2410    /// * If this task is finished, meaning the top-level thread exited and
2411    ///   additionally it's been returned or cancelled, then this will handle
2412    ///   management of the store's "active interesting tasks" counter.
2413    ///
2414    /// Effectively this is intended to be a "narrow waist" through which many
2415    /// destruction operations are funneled through.
2416    fn cleanup_thread(
2417        &mut self,
2418        guest_thread: QualifiedThreadId,
2419        runtime_instance: RuntimeInstance,
2420        cleanup_task: CleanupTask,
2421    ) -> Result<()> {
2422        let state = self.concurrent_state_mut()?;
2423        // If we never suspended, we never had a chance to deliver a subtask
2424        // status update, if any, to our caller, so we do that here:
2425        state.take_next_switch_item()?;
2426        let thread_data = state.get_mut(guest_thread.thread)?;
2427        let sync_call_set = thread_data.sync_call_set;
2428        if let Some(guest_id) = thread_data.instance_rep {
2429            self.instance_state(runtime_instance)
2430                .thread_handle_table()
2431                .guest_thread_remove(guest_id)?;
2432        }
2433        let state = self.concurrent_state_mut()?;
2434
2435        // Clean up any pending subtasks in the sync_call_set
2436        for waitable in mem::take(&mut state.get_mut(sync_call_set)?.ready) {
2437            if let Some(Event::Subtask {
2438                status: Status::Returned | Status::ReturnCancelled,
2439            }) = waitable.common(self.concurrent_state_mut()?)?.event
2440            {
2441                waitable.delete_from(self)?;
2442            }
2443        }
2444
2445        let state = self.concurrent_state_mut()?;
2446        state.delete(guest_thread.thread)?;
2447        state.delete(sync_call_set)?;
2448        let task = state.get_mut(guest_thread.task)?;
2449        task.threads.remove(&guest_thread.thread);
2450
2451        if task.threads.is_empty() && !task.returned_or_cancelled() {
2452            bail!(Trap::NoAsyncResult);
2453        }
2454        let ready_to_delete = task.ready_to_delete();
2455
2456        if !task.decremented_interesting_task_count && task.exited && task.returned_or_cancelled() {
2457            task.decremented_interesting_task_count = true;
2458
2459            debug_assert!(state.interesting_tasks > 0);
2460            state.interesting_tasks -= 1;
2461            if state.interesting_tasks == 0
2462                && let Some(waker) = state.interesting_tasks_empty_waker.take()
2463            {
2464                waker.wake();
2465            }
2466        }
2467
2468        match cleanup_task {
2469            CleanupTask::Yes => {
2470                if ready_to_delete {
2471                    Waitable::Guest(guest_thread.task).delete_from(self)?;
2472                }
2473            }
2474            CleanupTask::No => {}
2475        }
2476
2477        Ok(())
2478    }
2479
2480    /// Performs cancellation of the `guest_task` specified with the
2481    /// precondition that the task hasn't lowered its parameters.
2482    ///
2483    /// In this situation the task hasn't ever been started meaning it hasn't
2484    /// actually run any wasm code yet. This requires cleaning up metadata such
2485    /// as thread information attached to the task.
2486    ///
2487    /// The main two entrypoints for this function are:
2488    ///
2489    /// * Task cancellation via `subtask.cancel`, the intrinsic.
2490    /// * Dropping a host `call_async` future which needs to cancel the task
2491    ///   because it cannot reference its parameters any more.
2492    fn cancel_guest_subtask_without_lowered_parameters(
2493        &mut self,
2494        caller_instance: RuntimeInstance,
2495        guest_task: TableId<GuestTask>,
2496    ) -> Result<()> {
2497        let concurrent_state = self.concurrent_state_mut()?;
2498        let task = concurrent_state.get_mut(guest_task)?;
2499        assert!(!task.already_lowered_parameters());
2500        // The task is in a `starting` state, meaning it hasn't run at
2501        // all yet.  Here we update its fields to indicate that it is
2502        // ready to delete immediately once `subtask.drop` is called.
2503        task.lower_params = None;
2504        task.lift_result = None;
2505        task.exited = true;
2506        let instance = task.instance;
2507
2508        // Clean up the thread within this task as it's now never going
2509        // to run.
2510        assert_eq!(1, task.threads.len());
2511        let thread = *task.threads.iter().next().unwrap();
2512        self.cleanup_thread(
2513            QualifiedThreadId {
2514                task: guest_task,
2515                thread,
2516            },
2517            caller_instance,
2518            CleanupTask::No,
2519        )?;
2520
2521        // Not yet started; cancel and remove from pending
2522        let pending = &mut self.instance_state(instance).concurrent_state().pending;
2523        let pending_count = pending.len();
2524        pending.retain(|thread, _| thread.task != guest_task);
2525        // If there were no pending threads for this task, we're in an error state
2526        if pending.len() == pending_count {
2527            bail!(Trap::SubtaskCancelAfterTerminal);
2528        }
2529        Ok(())
2530    }
2531
2532    /// Used by `ResourceTables` to record the scope of a borrow to get undone
2533    /// in the future.
2534    pub(crate) fn current_scope(&mut self) -> Result<Option<CurrentScope>> {
2535        if !self.concurrency_support() {
2536            return Ok(self
2537                .current_scope_id_not_concurrent()?
2538                .map(|id| CurrentScope::Id(Scope::Id(id))));
2539        }
2540
2541        Ok(match self.current_thread()? {
2542            CurrentThread::Guest(id) => Some(CurrentScope::Id(Scope::Id(id.task.rep()))),
2543            CurrentThread::Host(id) => Some(CurrentScope::Id(Scope::HostId(id.rep()))),
2544            CurrentThread::DeferredHost(_) => Some(CurrentScope::DeferredHost),
2545            CurrentThread::None => return Ok(None),
2546        })
2547    }
2548
2549    pub(crate) fn queue_task(
2550        &mut self,
2551        task: impl FnOnce(&mut dyn VMStore) -> Result<()> + Send + 'static,
2552    ) -> Result<()> {
2553        self.concurrent_state_mut()?
2554            .push_high_priority(WorkItem::WorkerFunction(AlwaysMut::new(Box::new(task))));
2555        Ok(())
2556    }
2557
2558    /// Used in `poll_until` just prior to trapping due to a "deadlock"
2559    /// condition.
2560    ///
2561    /// This helps us distinguish between a simple deadlock condition (where no
2562    /// work is available for the event loop to do, nor is there any way for new
2563    /// work to be added) and a "cannot block sync task" condition where at
2564    /// least once instance has an outstanding sync-typed task running, in which
2565    /// case we'll trap with a different error message.
2566    fn any_may_not_suspend(&mut self) -> Result<bool> {
2567        // Note that this currently requires a linear search across the whole
2568        // `ConcurrentState::table`.  We _could_ optimize that, but since (1)
2569        // this function is only used when trapping, (2) the only thing you can
2570        // really do with a store that's been poisoned by a trap is drop it, and
2571        // (3) we must do a linear search through the table when dropping a
2572        // store anyway to dispose of fibers, it's reasonable for us to also do
2573        // a linear search here.
2574        Ok(self
2575            .concurrent_state_mut()?
2576            .table
2577            .get_mut()
2578            .iter_mut()
2579            .filter_map(|(_, entry)| {
2580                if let Some(task) = entry.downcast_ref::<GuestTask>() {
2581                    Some(task.instance)
2582                } else {
2583                    None
2584                }
2585            })
2586            .collect::<Vec<_>>()
2587            .into_iter()
2588            .any(|instance| {
2589                self.instance_state(instance)
2590                    .concurrent_state()
2591                    .do_not_suspend
2592            }))
2593    }
2594}
2595
2596enum CleanupTask {
2597    Yes,
2598    No,
2599}
2600
2601impl Instance {
2602    /// Get the next pending event for the specified task and (optional)
2603    /// waitable set, along with the waitable handle if applicable.
2604    fn get_event(
2605        self,
2606        store: &mut StoreOpaque,
2607        guest_task: TableId<GuestTask>,
2608        set: Option<TableId<WaitableSet>>,
2609        cancellable: bool,
2610    ) -> Result<Option<(Event, Option<(Waitable, u32)>)>> {
2611        let state = store.concurrent_state_mut()?;
2612
2613        let task = state.get_mut(guest_task)?;
2614        let event = &mut task.event;
2615        if let Some(ev) = event
2616            && (cancellable || !matches!(ev, Event::Cancelled))
2617        {
2618            log::trace!("deliver event {ev:?} to {guest_task:?}");
2619
2620            if matches!(ev, Event::Cancelled) {
2621                task.cancel_request_delivered = true;
2622            }
2623
2624            let ev = *ev;
2625            *event = None;
2626            return Ok(Some((ev, None)));
2627        }
2628
2629        let set = match set {
2630            Some(set) => set,
2631            None => return Ok(None),
2632        };
2633        let waitable = match state.get_mut(set)?.ready.pop_first() {
2634            Some(v) => v,
2635            None => return Ok(None),
2636        };
2637
2638        let common = waitable.common(state)?;
2639        let handle = match common.handle {
2640            Some(h) => h,
2641            None => bail_bug!("handle not set when delivering event"),
2642        };
2643        let event = match common.event.take() {
2644            Some(e) => e,
2645            None => bail_bug!("event not set when delivering event"),
2646        };
2647
2648        log::trace!(
2649            "deliver event {event:?} to {guest_task:?} for {waitable:?} (handle {handle}); set {set:?}"
2650        );
2651
2652        waitable.on_delivery(store, self, event)?;
2653
2654        Ok(Some((event, Some((waitable, handle)))))
2655    }
2656
2657    /// Handle the `CallbackCode` returned from an async-lifted export or its
2658    /// callback.
2659    ///
2660    /// If this returns `Ok(Some(call))`, then `call` should be run immediately
2661    /// using `handle_guest_call`.
2662    fn handle_callback_code(
2663        self,
2664        store: &mut StoreOpaque,
2665        guest_thread: QualifiedThreadId,
2666        runtime_instance: RuntimeComponentInstanceIndex,
2667        code: u32,
2668    ) -> Result<()> {
2669        let (code, set) = unpack_callback_code(code);
2670
2671        log::trace!("received callback code from {guest_thread:?}: {code} (set: {set})");
2672
2673        let state = store.concurrent_state_mut()?;
2674
2675        state.take_next_switch_item()?;
2676
2677        let get_set = |store: &mut StoreOpaque, handle| -> Result<_> {
2678            let set = store
2679                .instance_state(self.runtime_instance(runtime_instance))
2680                .handle_table()
2681                .waitable_set_rep(handle)?;
2682
2683            Ok(TableId::<WaitableSet>::new(set))
2684        };
2685
2686        match code {
2687            callback_code::EXIT => {
2688                log::trace!("implicit thread {guest_thread:?} completed");
2689                let task = store.concurrent_state_mut()?.get_mut(guest_thread.task)?;
2690                task.exited = true;
2691                task.callback = None;
2692
2693                let runtime_instance = self.runtime_instance(runtime_instance);
2694
2695                // Since we're async-typed, returning control to our caller
2696                // won't help resolve any outstanding sync-typed call which
2697                // might be in progress, so we may need to switch or trap before
2698                // exiting this thread:
2699                store.switch_or_trap_if_may_not_suspend(runtime_instance)?;
2700
2701                store.cleanup_thread(guest_thread, runtime_instance, CleanupTask::Yes)?;
2702            }
2703            callback_code::YIELD => {
2704                // Set `GuestTask::wake_on_cancel` to allow `subtask.cancel` to
2705                // promote this thread if appropriate.
2706                let old = state
2707                    .get_mut(guest_thread.thread)?
2708                    .wake_on_cancel
2709                    .replace(WakeOnCancel::Yielding);
2710                if !old.is_none() {
2711                    bail_bug!("thread unexpectedly had wake_on_cancel set");
2712                }
2713
2714                let task = state.get_mut(guest_thread.task)?;
2715                // If an `Event::Cancelled` is pending, we'll deliver that;
2716                // otherwise, we'll deliver `Event::None`.  Note that
2717                // `GuestTask::event` is only ever set to one of those two
2718                // `Event` variants.
2719                if let Some(event) = task.event {
2720                    assert!(matches!(event, Event::None | Event::Cancelled));
2721                } else {
2722                    task.event = Some(Event::None);
2723                }
2724                let call = GuestCall {
2725                    thread: guest_thread,
2726                    kind: GuestCallKind::DeliverEvent {
2727                        instance: self,
2728                        set: None,
2729                    },
2730                };
2731                // Push this thread onto the "low priority" queue so it runs
2732                // after any other threads have had a chance to run.
2733                state.push_low_priority(WorkItem::GuestCall {
2734                    instance: self.runtime_instance(runtime_instance),
2735                    call,
2736                });
2737            }
2738            callback_code::WAIT => {
2739                let set = get_set(store, set)?;
2740                let state = store.concurrent_state_mut()?;
2741
2742                if state.get_mut(guest_thread.task)?.event.is_some()
2743                    || !state.get_mut(set)?.ready.is_empty()
2744                {
2745                    // An event is immediately available; deliver it ASAP.
2746                    state.push_high_priority(WorkItem::GuestCall {
2747                        instance: self.runtime_instance(runtime_instance),
2748                        call: GuestCall {
2749                            thread: guest_thread,
2750                            kind: GuestCallKind::DeliverEvent {
2751                                instance: self,
2752                                set: Some(set),
2753                            },
2754                        },
2755                    });
2756                } else {
2757                    // No event is immediately available.
2758                    //
2759                    // We're waiting, so register to be woken up when an event
2760                    // is published for this waitable set.
2761                    //
2762                    // Here we also set `GuestTask::wake_on_cancel` which allows
2763                    // `subtask.cancel` to interrupt the wait.
2764                    let old = state
2765                        .get_mut(guest_thread.thread)?
2766                        .wake_on_cancel
2767                        .replace(WakeOnCancel::Waiting(set));
2768                    if !old.is_none() {
2769                        bail_bug!("thread unexpectedly had wake_on_cancel set");
2770                    }
2771                    let old = state
2772                        .get_mut(set)?
2773                        .waiting
2774                        .insert(guest_thread, WaitMode::Callback(self));
2775                    if !old.is_none() {
2776                        bail_bug!("set's waiting set already had this thread registered");
2777                    }
2778                }
2779            }
2780            _ => bail!(Trap::UnsupportedCallbackCode),
2781        }
2782
2783        Ok(())
2784    }
2785
2786    /// Stage the specified guest call as the work item to run next, to be
2787    /// started as soon as backpressure and/or reentrance rules allow.
2788    ///
2789    /// SAFETY: The raw pointer arguments must be valid references to guest
2790    /// functions (with the appropriate signatures) when the closures staged by
2791    /// this function are called.
2792    unsafe fn stage_call<T: 'static>(
2793        self,
2794        mut store: StoreContextMut<T>,
2795        guest_thread: QualifiedThreadId,
2796        callee: SendSyncPtr<VMFuncRef>,
2797        param_count: usize,
2798        result_count: usize,
2799        async_: bool,
2800        callback: Option<SendSyncPtr<VMFuncRef>>,
2801        post_return: Option<SendSyncPtr<VMFuncRef>>,
2802        host_caller: bool,
2803    ) -> Result<()> {
2804        /// Return a closure which will call the specified function in the scope
2805        /// of the specified task.
2806        ///
2807        /// This will use `GuestTask::lower_params` to lower the parameters, but
2808        /// will not lift the result; instead, it returns a
2809        /// `[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]` from which the result, if
2810        /// any, may be lifted.  Note that an async-lifted export will have
2811        /// returned its result using the `task.return` intrinsic (or not
2812        /// returned a result at all, in the case of `task.cancel`), in which
2813        /// case the "result" of this call will either be a callback code or
2814        /// nothing.
2815        ///
2816        /// SAFETY: `callee` must be a valid `*mut VMFuncRef` at the time when
2817        /// the returned closure is called.
2818        unsafe fn make_call<T: 'static>(
2819            store: StoreContextMut<T>,
2820            guest_thread: QualifiedThreadId,
2821            callee: SendSyncPtr<VMFuncRef>,
2822            param_count: usize,
2823            result_count: usize,
2824        ) -> impl FnOnce(&mut dyn VMStore) -> Result<[MaybeUninit<ValRaw>; MAX_FLAT_PARAMS]>
2825        + Send
2826        + Sync
2827        + 'static
2828        + use<T> {
2829            let token = StoreToken::new(store);
2830            move |store: &mut dyn VMStore| {
2831                let mut storage = [MaybeUninit::uninit(); MAX_FLAT_PARAMS];
2832
2833                store
2834                    .concurrent_state_mut()?
2835                    .get_mut(guest_thread.thread)?
2836                    .state = GuestThreadState::Running;
2837                let task = store.concurrent_state_mut()?.get_mut(guest_thread.task)?;
2838                let lower = match task.lower_params.take() {
2839                    Some(l) => l,
2840                    None => bail_bug!("lower_params missing"),
2841                };
2842
2843                lower(store, &mut storage[..param_count])?;
2844
2845                let mut store = token.as_context_mut(store);
2846
2847                // SAFETY: Per the contract documented in `make_call's`
2848                // documentation, `callee` must be a valid pointer.
2849                unsafe {
2850                    crate::Func::call_unchecked_raw(
2851                        &mut store,
2852                        callee.as_non_null(),
2853                        NonNull::new(
2854                            &mut storage[..param_count.max(result_count)]
2855                                as *mut [MaybeUninit<ValRaw>] as _,
2856                        )
2857                        .unwrap(),
2858                    )?;
2859                }
2860
2861                Ok(storage)
2862            }
2863        }
2864
2865        // SAFETY: Per the contract described in this function documentation,
2866        // the `callee` pointer which `call` closes over must be valid when
2867        // called by the closure we queue below.
2868        let call = unsafe {
2869            make_call(
2870                store.as_context_mut(),
2871                guest_thread,
2872                callee,
2873                param_count,
2874                result_count,
2875            )
2876        };
2877
2878        let callee_instance = store
2879            .0
2880            .concurrent_state_mut()?
2881            .get_mut(guest_thread.task)?
2882            .instance;
2883
2884        let fun = if callback.is_some() {
2885            assert!(async_);
2886
2887            Box::new(move |store: &mut dyn VMStore| {
2888                self.add_guest_thread_to_instance_table(
2889                    guest_thread.thread,
2890                    store,
2891                    callee_instance.index,
2892                )?;
2893                let old_thread = store.set_thread(guest_thread)?;
2894                log::trace!(
2895                    "stackless call: replaced {old_thread:?} with {guest_thread:?} as current thread"
2896                );
2897
2898                store.enter_instance(callee_instance);
2899
2900                // SAFETY: See the documentation for `make_call` to review the
2901                // contract we must uphold for `call` here.
2902                //
2903                // Per the contract described in the `stage_call`
2904                // documentation, the `callee` pointer which `call` closes
2905                // over must be valid.
2906                let storage = call(store)?;
2907
2908                store.exit_instance(callee_instance)?;
2909
2910                store.set_thread(old_thread)?;
2911                let state = store.concurrent_state_mut()?;
2912                if let Some(t) = old_thread.guest() {
2913                    state.get_mut(t.thread)?.state = GuestThreadState::Running;
2914                }
2915                log::trace!("stackless call: restored {old_thread:?} as current thread");
2916
2917                // SAFETY: `wasmparser` will have validated that the callback
2918                // function returns a `i32` result.
2919                let code = unsafe { storage[0].assume_init() }.get_i32() as u32;
2920
2921                self.handle_callback_code(store, guest_thread, callee_instance.index, code)
2922            }) as Box<dyn FnOnce(&mut dyn VMStore) -> Result<()> + Send + Sync>
2923        } else {
2924            let token = StoreToken::new(store.as_context_mut());
2925            Box::new(move |store: &mut dyn VMStore| {
2926                self.add_guest_thread_to_instance_table(
2927                    guest_thread.thread,
2928                    store,
2929                    callee_instance.index,
2930                )?;
2931                let old_thread = store.set_thread(guest_thread)?;
2932                log::trace!(
2933                    "sync/async-stackful call: replaced {old_thread:?} with {guest_thread:?} as current thread",
2934                );
2935                let flags = self.id().get(store).instance_flags(callee_instance.index);
2936
2937                let callee_async_typed = store
2938                    .concurrent_state_mut()?
2939                    .get_mut(guest_thread.task)?
2940                    .async_typed;
2941
2942                // Unless this is a callback-less (i.e. stackful) async-lifted
2943                // or sync-typed export, we need to record that the instance
2944                // cannot be entered until the call returns.
2945                if !async_ && callee_async_typed {
2946                    store.enter_instance(callee_instance);
2947                }
2948
2949                if !callee_async_typed {
2950                    store.enter_sync_call(callee_instance)?;
2951                }
2952
2953                // SAFETY: See the documentation for `make_call` to review the
2954                // contract we must uphold for `call` here.
2955                //
2956                // Per the contract described in the `stage_call`
2957                // documentation, the `callee` pointer which `call` closes
2958                // over must be valid.
2959                let storage = call(store)?;
2960
2961                if !callee_async_typed {
2962                    store.exit_sync_call(callee_instance)?;
2963                }
2964
2965                if !async_ {
2966                    // This is a sync-lifted export, so now is when we lift the
2967                    // result, optionally call the post-return function, if any,
2968                    // and finally notify any current or future waiters that the
2969                    // subtask has returned.
2970
2971                    if callee_async_typed {
2972                        store.exit_instance(callee_instance)?;
2973                    }
2974
2975                    let lift = {
2976                        let state = store.concurrent_state_mut()?;
2977                        if !state.get_mut(guest_thread.task)?.result.is_none() {
2978                            bail_bug!("task has already produced a result");
2979                        }
2980
2981                        match state.get_mut(guest_thread.task)?.lift_result.take() {
2982                            Some(lift) => lift,
2983                            None => bail_bug!("lift_result field is missing"),
2984                        }
2985                    };
2986
2987                    // SAFETY: `result_count` represents the number of core Wasm
2988                    // results returned, per `wasmparser`.
2989                    let result = (lift.lift)(store, unsafe {
2990                        mem::transmute::<&[MaybeUninit<ValRaw>], &[ValRaw]>(
2991                            &storage[..result_count],
2992                        )
2993                    })?;
2994
2995                    let post_return_arg = match result_count {
2996                        0 => ValRaw::i32(0),
2997                        // SAFETY: `result_count` represents the number of
2998                        // core Wasm results returned, per `wasmparser`.
2999                        1 => unsafe { storage[0].assume_init() },
3000                        _ => unreachable!(),
3001                    };
3002
3003                    unsafe {
3004                        call_post_return(
3005                            token.as_context_mut(store),
3006                            post_return.map(|v| v.as_non_null()),
3007                            post_return_arg,
3008                            flags,
3009                        )?;
3010                    }
3011
3012                    self.task_complete(store, guest_thread.task, result, Status::Returned)?;
3013                }
3014
3015                store.set_thread(old_thread)?;
3016
3017                store
3018                    .concurrent_state_mut()?
3019                    .get_mut(guest_thread.task)?
3020                    .exited = true;
3021
3022                log::trace!(
3023                    "clean up thread; async lifted? {async_} async typed? {callee_async_typed}"
3024                );
3025
3026                if callee_async_typed {
3027                    // If we're async-typed, returning control to our caller
3028                    // won't help resolve any outstanding sync-typed call which
3029                    // might be in progress, so we may need to switch or trap
3030                    // before exiting this thread:
3031                    store.switch_or_trap_if_may_not_suspend(callee_instance)?;
3032                }
3033
3034                // This is a callback-less call, so the implicit thread has now completed
3035                store.cleanup_thread(guest_thread, callee_instance, CleanupTask::Yes)?;
3036                Ok(())
3037            })
3038        };
3039
3040        store.0.concurrent_state_mut()?.push_work_item(
3041            WorkItem::GuestCall {
3042                instance: callee_instance,
3043                call: GuestCall {
3044                    thread: guest_thread,
3045                    kind: GuestCallKind::StartImplicit(fun),
3046                },
3047            },
3048            if host_caller {
3049                Priority::High
3050            } else {
3051                Priority::Switch
3052            },
3053        )?;
3054
3055        Ok(())
3056    }
3057
3058    /// Prepare (but do not start) a guest->guest call.
3059    ///
3060    /// This is called from fused adapter code generated in
3061    /// `wasmtime_environ::fact::trampoline::Compiler`.  `start` and `return_`
3062    /// are synthesized Wasm functions which move the parameters from the caller
3063    /// to the callee and the result from the callee to the caller,
3064    /// respectively.  The adapter will call `Self::start_call` immediately
3065    /// after calling this function.
3066    ///
3067    /// SAFETY: All the pointer arguments must be valid pointers to guest
3068    /// entities (and with the expected signatures for the function references
3069    /// -- see `wasmtime_environ::fact::trampoline::Compiler` for details).
3070    unsafe fn prepare_call<T: 'static>(
3071        self,
3072        mut store: StoreContextMut<T>,
3073        start: NonNull<VMFuncRef>,
3074        return_: NonNull<VMFuncRef>,
3075        caller_instance: RuntimeComponentInstanceIndex,
3076        callee_instance: RuntimeComponentInstanceIndex,
3077        task_return_type: TypeTupleIndex,
3078        callee_async_typed: bool,
3079        memory: *mut VMMemoryDefinition,
3080        string_encoding: StringEncoding,
3081        caller_info: CallerInfo,
3082    ) -> Result<()> {
3083        enum ResultInfo {
3084            Heap { results: u32 },
3085            Stack { result_count: u32 },
3086        }
3087
3088        let result_info = match &caller_info {
3089            CallerInfo::Async {
3090                has_result: true,
3091                params,
3092            } => ResultInfo::Heap {
3093                results: match params.last() {
3094                    Some(r) => r.get_u32(),
3095                    None => bail_bug!("retptr missing"),
3096                },
3097            },
3098            CallerInfo::Async {
3099                has_result: false, ..
3100            } => ResultInfo::Stack { result_count: 0 },
3101            CallerInfo::Sync {
3102                result_count,
3103                params,
3104            } if *result_count > u32::try_from(MAX_FLAT_RESULTS)? => ResultInfo::Heap {
3105                results: match params.last() {
3106                    Some(r) => r.get_u32(),
3107                    None => bail_bug!("arg ptr missing"),
3108                },
3109            },
3110            CallerInfo::Sync { result_count, .. } => ResultInfo::Stack {
3111                result_count: *result_count,
3112            },
3113        };
3114
3115        let sync_caller = matches!(caller_info, CallerInfo::Sync { .. });
3116
3117        // Create a new guest task for the call, closing over the `start` and
3118        // `return_` functions to lift the parameters and lower the result,
3119        // respectively.
3120        let start = SendSyncPtr::new(start);
3121        let return_ = SendSyncPtr::new(return_);
3122        let token = StoreToken::new(store.as_context_mut());
3123        let old_thread = store.0.current_guest_thread()?;
3124
3125        let state = store.0.concurrent_state_mut()?;
3126
3127        debug_assert_eq!(
3128            state.get_mut(old_thread.task)?.instance,
3129            self.runtime_instance(caller_instance)
3130        );
3131
3132        let guest_thread = GuestTask::new(
3133            state,
3134            Box::new(move |store, dst| {
3135                let mut store = token.as_context_mut(store);
3136                assert!(dst.len() <= MAX_FLAT_PARAMS);
3137                // The `+ 1` here accounts for the return pointer, if any:
3138                let mut src = [MaybeUninit::uninit(); MAX_FLAT_PARAMS + 1];
3139                let count = match caller_info {
3140                    // Async callers, if they have a result, use the last
3141                    // parameter as a return pointer so chop that off if
3142                    // relevant here.
3143                    CallerInfo::Async { params, has_result } => {
3144                        let params = &params[..params.len() - usize::from(has_result)];
3145                        for (param, src) in params.iter().zip(&mut src) {
3146                            src.write(*param);
3147                        }
3148                        params.len()
3149                    }
3150
3151                    // Sync callers forward everything directly.
3152                    CallerInfo::Sync { params, .. } => {
3153                        for (param, src) in params.iter().zip(&mut src) {
3154                            src.write(*param);
3155                        }
3156                        params.len()
3157                    }
3158                };
3159                // SAFETY: `start` is a valid `*mut VMFuncRef` from
3160                // `wasmtime-cranelift`-generated fused adapter code.  Based on
3161                // how it was constructed (see
3162                // `wasmtime_environ::fact::trampoline::Compiler::compile_async_start_adapter`
3163                // for details) we know it takes count parameters and returns
3164                // `dst.len()` results.
3165                unsafe {
3166                    crate::Func::call_unchecked_raw(
3167                        &mut store,
3168                        start.as_non_null(),
3169                        NonNull::new(
3170                            &mut src[..count.max(dst.len())] as *mut [MaybeUninit<ValRaw>] as _,
3171                        )
3172                        .unwrap(),
3173                    )?;
3174                }
3175                dst.copy_from_slice(&src[..dst.len()]);
3176                let task = store.0.current_guest_thread()?.task;
3177                let state = store.0.concurrent_state_mut()?;
3178                Waitable::Guest(task).set_event(
3179                    state,
3180                    Some(Event::Subtask {
3181                        status: Status::Started,
3182                    }),
3183                )?;
3184                Ok(())
3185            }),
3186            LiftResult {
3187                lift: Box::new(move |store, src| {
3188                    // SAFETY: See comment in closure passed as `lower_params`
3189                    // parameter above.
3190                    let mut store = token.as_context_mut(store);
3191                    let mut my_src = src.to_owned(); // TODO: use stack to avoid allocation?
3192                    if let ResultInfo::Heap { results } = &result_info {
3193                        my_src.push(ValRaw::u32(*results));
3194                    }
3195
3196                    // SAFETY: `return_` is a valid `*mut VMFuncRef` from
3197                    // `wasmtime-cranelift`-generated fused adapter code.  Based
3198                    // on how it was constructed (see
3199                    // `wasmtime_environ::fact::trampoline::Compiler::compile_async_return_adapter`
3200                    // for details) we know it takes `src.len()` parameters and
3201                    // returns up to 1 result.
3202                    unsafe {
3203                        crate::Func::call_unchecked_raw(
3204                            &mut store,
3205                            return_.as_non_null(),
3206                            my_src.as_mut_slice().into(),
3207                        )?;
3208                    }
3209
3210                    let thread = store.0.current_guest_thread()?;
3211                    let state = store.0.concurrent_state_mut()?;
3212                    if sync_caller {
3213                        state.get_mut(thread.task)?.sync_result = SyncResult::Produced(
3214                            if let ResultInfo::Stack { result_count } = &result_info {
3215                                match result_count {
3216                                    0 => None,
3217                                    1 => Some(my_src[0]),
3218                                    _ => unreachable!(),
3219                                }
3220                            } else {
3221                                None
3222                            },
3223                        );
3224                    }
3225                    Ok(Box::new(DummyResult) as Box<dyn Any + Send + Sync>)
3226                }),
3227                ty: task_return_type,
3228                memory: NonNull::new(memory).map(SendSyncPtr::new),
3229                string_encoding,
3230            },
3231            Caller::Guest { thread: old_thread },
3232            None,
3233            self.runtime_instance(callee_instance),
3234            callee_async_typed,
3235            // We don't know whether the callee export was lifted sync or async
3236            // yet, but we'll update this in `start_call`:
3237            false,
3238        )?;
3239
3240        // Make the new thread the current one so that `Self::start_call` knows
3241        // which one to start.
3242        store.0.set_thread(guest_thread)?;
3243        log::trace!("pushed {guest_thread:?} as current thread; old thread was {old_thread:?}");
3244
3245        Ok(())
3246    }
3247
3248    /// Call the specified callback function for an async-lifted export.
3249    ///
3250    /// SAFETY: `function` must be a valid reference to a guest function of the
3251    /// correct signature for a callback.
3252    unsafe fn call_callback<T>(
3253        self,
3254        mut store: StoreContextMut<T>,
3255        function: SendSyncPtr<VMFuncRef>,
3256        event: Event,
3257        handle: u32,
3258    ) -> Result<u32> {
3259        let (ordinal, result) = event.parts();
3260        let params = &mut [
3261            ValRaw::u32(ordinal),
3262            ValRaw::u32(handle),
3263            ValRaw::u32(result),
3264        ];
3265        // SAFETY: `func` is a valid `*mut VMFuncRef` from either
3266        // `wasmtime-cranelift`-generated fused adapter code or
3267        // `component::Options`.  Per `wasmparser` callback signature
3268        // validation, we know it takes three parameters and returns one.
3269        unsafe {
3270            crate::Func::call_unchecked_raw(
3271                &mut store,
3272                function.as_non_null(),
3273                params.as_mut_slice().into(),
3274            )?;
3275        }
3276        Ok(params[0].get_u32())
3277    }
3278
3279    /// Start a guest->guest call previously prepared using
3280    /// `Self::prepare_call`.
3281    ///
3282    /// This is called from fused adapter code generated in
3283    /// `wasmtime_environ::fact::trampoline::Compiler`.  The adapter will call
3284    /// this function immediately after calling `Self::prepare_call`.
3285    ///
3286    /// SAFETY: The `*mut VMFuncRef` arguments must be valid pointers to guest
3287    /// functions with the appropriate signatures for the current guest task.
3288    /// If this is a call to an async-lowered import, the actual call may be
3289    /// deferred and run after this function returns, in which case the pointer
3290    /// arguments must also be valid when the call happens.
3291    unsafe fn start_call<T: 'static>(
3292        self,
3293        mut store: StoreContextMut<T>,
3294        callback: *mut VMFuncRef,
3295        post_return: *mut VMFuncRef,
3296        callee: NonNull<VMFuncRef>,
3297        param_count: u32,
3298        result_count: u32,
3299        flags: u32,
3300        storage: Option<&mut [MaybeUninit<ValRaw>]>,
3301    ) -> Result<u32> {
3302        let token = StoreToken::new(store.as_context_mut());
3303        let async_caller = storage.is_none();
3304        let guest_thread = store.0.current_guest_thread()?;
3305        let state = store.0.concurrent_state_mut()?;
3306
3307        if !state.event_loop_running {
3308            bail_bug!("Instance::start_call called without a running event loop");
3309        }
3310
3311        let callee = SendSyncPtr::new(callee);
3312        let param_count = usize::try_from(param_count)?;
3313        assert!(param_count <= MAX_FLAT_PARAMS);
3314        let result_count = usize::try_from(result_count)?;
3315        assert!(result_count <= MAX_FLAT_RESULTS);
3316
3317        let task = state.get_mut(guest_thread.task)?;
3318        let callee_async_typed = task.async_typed;
3319        let callee_instance = task.instance;
3320
3321        task.async_lifted = (flags & START_FLAG_ASYNC_CALLEE) != 0;
3322
3323        if let Some(callback) = NonNull::new(callback) {
3324            // We're calling an async-lifted export with a callback, so store
3325            // the callback and related context as part of the task so we can
3326            // call it later when needed.
3327            let callback = SendSyncPtr::new(callback);
3328            task.callback = Some(Box::new(move |store, event, handle| {
3329                let store = token.as_context_mut(store);
3330                unsafe { self.call_callback::<T>(store, callback, event, handle) }
3331            }));
3332        }
3333
3334        let Caller::Guest { thread: caller } = &task.caller else {
3335            // As of this writing, `start_call` is only used for guest->guest
3336            // calls.
3337            bail_bug!("start_call unexpectedly invoked for host->guest call");
3338        };
3339        let caller = *caller;
3340        let caller_instance = state.get_mut(caller.task)?.instance;
3341
3342        // Stage the call as the work item to run next, modulo backpressure, etc.
3343        unsafe {
3344            self.stage_call(
3345                store.as_context_mut(),
3346                guest_thread,
3347                callee,
3348                param_count,
3349                result_count,
3350                (flags & START_FLAG_ASYNC_CALLEE) != 0,
3351                NonNull::new(callback).map(SendSyncPtr::new),
3352                NonNull::new(post_return).map(SendSyncPtr::new),
3353                false,
3354            )?;
3355        }
3356
3357        let old_do_not_suspend = if callee_async_typed {
3358            // If we're starting an async-typed call, it is permitted to suspend
3359            // since that will just return control back to the caller, which may
3360            // be able to avoid blocking if needed.
3361            //
3362            // We'll restore the old value after the call either returns or
3363            // suspends.
3364            let state = store.0.instance_state(callee_instance).concurrent_state();
3365            let old_do_not_suspend = state.do_not_suspend;
3366            state.do_not_suspend = false;
3367            Some(old_do_not_suspend)
3368        } else {
3369            None
3370        };
3371
3372        let state = store.0.concurrent_state_mut()?;
3373
3374        // Use the caller's `GuestThread::sync_call_set` to register interest in
3375        // the subtask...
3376        let guest_waitable = Waitable::Guest(guest_thread.task);
3377        let old_set = guest_waitable.common(state)?.set;
3378        let set = state.get_mut(caller.thread)?.sync_call_set;
3379        guest_waitable.join(state, Some(set))?;
3380
3381        store.0.set_thread(CurrentThread::None)?;
3382
3383        // ... and suspend this fiber temporarily while we wait for it to start.
3384        //
3385        // Note that we _could_ call the callee directly using the current fiber
3386        // rather than suspend this one, but that would make reasoning about the
3387        // event loop more complicated and is probably only worth doing if
3388        // there's a measurable performance benefit.  In addition, it would mean
3389        // blocking the caller if the callee calls a blocking sync-lowered
3390        // import, and as of this writing the spec says we must not do that.
3391        //
3392        // Alternatively, the fused adapter code could be modified to call the
3393        // callee directly without calling a host-provided intrinsic at all (in
3394        // which case it would need to do its own, inline backpressure checks,
3395        // etc.).  Again, we'd want to see a measurable performance benefit
3396        // before committing to such an optimization.  And again, we'd need to
3397        // update the spec to allow that.
3398        let mut yielded = false;
3399        let (status, waitable) = loop {
3400            store.0.suspend(if yielded {
3401                SuspendReason::Waiting {
3402                    set,
3403                    thread: caller,
3404                }
3405            } else {
3406                yielded = true;
3407                SuspendReason::YieldingToSubtask { thread: caller }
3408            })?;
3409
3410            if let Some(old_do_not_suspend) = old_do_not_suspend {
3411                store
3412                    .0
3413                    .instance_state(callee_instance)
3414                    .concurrent_state()
3415                    .do_not_suspend = old_do_not_suspend;
3416            }
3417
3418            let state = store.0.concurrent_state_mut()?;
3419
3420            log::trace!("taking event for {:?}", guest_thread.task);
3421            let event = guest_waitable.take_event(state)?;
3422            let Some(Event::Subtask { status }) = event else {
3423                bail_bug!("subtasks should only get subtask events, got {event:?}")
3424            };
3425
3426            log::trace!("status {status:?} for {:?}", guest_thread.task);
3427
3428            if status == Status::Returned {
3429                // It returned, so we can stop waiting.
3430                break (status, None);
3431            } else if async_caller {
3432                // It hasn't returned yet, but the caller is calling via an
3433                // async-lowered import, so we generate a handle for the task
3434                // waitable and return the status.
3435                let handle = store
3436                    .0
3437                    .instance_state(caller_instance)
3438                    .handle_table()
3439                    .subtask_insert_guest(guest_thread.task.rep())?;
3440                store
3441                    .0
3442                    .concurrent_state_mut()?
3443                    .get_mut(guest_thread.task)?
3444                    .common
3445                    .handle = Some(handle);
3446                break (status, Some(handle));
3447            } else {
3448                // The callee hasn't returned yet, and the caller is calling via
3449                // a sync-lowered import, so we loop and keep waiting until the
3450                // callee returns.
3451                store.0.switch_or_trap_if_may_not_suspend(caller_instance)?;
3452            }
3453        };
3454
3455        guest_waitable.join(store.0.concurrent_state_mut()?, old_set)?;
3456
3457        // Reset the current thread to point to the caller as it resumes control.
3458        store.0.set_thread(caller)?;
3459        store
3460            .0
3461            .concurrent_state_mut()?
3462            .get_mut(caller.thread)?
3463            .state = GuestThreadState::Running;
3464        log::trace!("popped current thread {guest_thread:?}; new thread is {caller:?}");
3465
3466        if let Some(storage) = storage {
3467            // The caller used a sync-lowered import to call an async-lifted
3468            // export, in which case the result, if any, has been stashed in
3469            // `GuestTask::sync_result`.
3470            let state = store.0.concurrent_state_mut()?;
3471            let task = state.get_mut(guest_thread.task)?;
3472            if let Some(result) = task.sync_result.take()? {
3473                if let Some(result) = result {
3474                    storage[0] = MaybeUninit::new(result);
3475                }
3476
3477                if task.exited && task.ready_to_delete() {
3478                    Waitable::Guest(guest_thread.task).delete_from(store.0)?;
3479                }
3480            }
3481        }
3482
3483        Ok(status.pack(waitable))
3484    }
3485
3486    /// Poll the specified future once on behalf of a guest->host call using an
3487    /// async-lowered import.
3488    ///
3489    /// If it returns `Ready`, return `Ok(None)`.  Otherwise, if it returns
3490    /// `Pending`, add it to the set of futures to be polled as part of this
3491    /// instance's event loop until it completes, and then return
3492    /// `Ok(Some(handle))` where `handle` is the waitable handle to return.
3493    ///
3494    /// Whether the future returns `Ready` immediately or later, the `lower`
3495    /// function will be used to lower the result, if any, into the guest caller's
3496    /// stack and linear memory. The `lower` function is invoked with the
3497    /// `Option<R>` param `None` if the future is cancelled. The
3498    /// `Option<TableId<HostTask>>` is passed as `Some` if the host task was
3499    /// materialized during execution and allows `lower` to delete the task if
3500    /// needed.
3501    pub(crate) fn first_poll<T: 'static, R: Send + 'static>(
3502        self,
3503        mut store: StoreContextMut<'_, T>,
3504        host_task: EnteredHostTask,
3505        future: impl Future<Output = Result<R>> + Send + 'static,
3506        lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>
3507        + Send
3508        + 'static,
3509    ) -> Result<u32> {
3510        let token = StoreToken::new(store.as_context_mut());
3511
3512        // Create an abortable future which hooks calls to poll and manages call
3513        // context state for the future.
3514        let (join_handle, future) = JoinHandle::run(future);
3515        let mut future = Box::pin(future);
3516
3517        // Finally, poll the future.  We can use a dummy `Waker` here because
3518        // we'll add the future to `ConcurrentState::futures` and poll it
3519        // automatically from the event loop if it doesn't complete immediately
3520        // here.
3521        let poll = tls::set(store.0, || {
3522            future
3523                .as_mut()
3524                .poll(&mut Context::from_waker(&Waker::noop()))
3525        });
3526
3527        match poll {
3528            // It finished immediately; lower the result and delete the task.
3529            Poll::Ready(result) => {
3530                let result = result.transpose()?;
3531                // Check if the host task was materialized so that it can be
3532                // deleted in `lower`.
3533                let task = store.0.current_materialized_host_task()?;
3534                lower(store.as_context_mut(), result, true, task)?;
3535                return Ok(Status::Returned.pack(None));
3536            }
3537
3538            // Future isn't ready yet, so fall through.
3539            Poll::Pending => {}
3540        }
3541
3542        // The future will outlive this call frame, so materialize the deferred
3543        // host task and attach its cancellation handle before publishing it to
3544        // the event loop.
3545        let Some(task) = store.0.materialize_host_task_id()? else {
3546            bail_bug!("current thread is not a host thread")
3547        };
3548        {
3549            let state = &mut store.0.concurrent_state_mut()?.get_mut(task)?.state;
3550            assert!(matches!(state, HostTaskState::CalleeStarted));
3551            *state = HostTaskState::CalleeRunning(join_handle);
3552        }
3553
3554        // It hasn't finished yet; add the future to
3555        // `ConcurrentState::futures` so it will be polled by the event
3556        // loop and allocate a waitable handle to return to the guest.
3557
3558        // Wrap the future in a closure responsible for lowering the result into
3559        // the guest's stack and memory, as well as notifying any waiters that
3560        // the task returned.
3561        let future = Box::pin(async move {
3562            let result = match run_with_host_task_set(task, future).await? {
3563                Some(result) => Some(result?),
3564                None => None,
3565            };
3566            let on_complete = move |store: &mut dyn VMStore| {
3567                // Restore the `current_thread` to be the host so `lower` knows
3568                // how to manipulate borrows and knows which scope of borrows
3569                // to check.
3570                let mut store = token.as_context_mut(store);
3571                let old = store.0.set_thread(task)?;
3572
3573                let status = if result.is_some() {
3574                    Status::Returned
3575                } else {
3576                    Status::ReturnCancelled
3577                };
3578
3579                lower(store.as_context_mut(), result, false, Some(task))?;
3580                let state = store.0.concurrent_state_mut()?;
3581                match &mut state.get_mut(task)?.state {
3582                    // Cancellation is only complete once lowering has finished
3583                    // and the terminal event can be published.
3584                    pending @ HostTaskState::CalleeCancelling => {
3585                        *pending = HostTaskState::CalleeDone { cancelled: true };
3586                    }
3587
3588                    // Otherwise transition this task to the done state.
3589                    other => *other = HostTaskState::CalleeDone { cancelled: false },
3590                }
3591                Waitable::Host(task).set_event(state, Some(Event::Subtask { status }))?;
3592
3593                store.0.set_thread(old)?;
3594                Ok(())
3595            };
3596
3597            // Here we schedule a task to run on a worker fiber to do the
3598            // lowering since it may involve a call to the guest's realloc
3599            // function. This is necessary because calling the guest while
3600            // there are host embedder frames on the stack is unsound.
3601            tls::get(move |store| {
3602                store
3603                    .concurrent_state_mut()?
3604                    .push_high_priority(WorkItem::WorkerFunction(AlwaysMut::new(Box::new(
3605                        on_complete,
3606                    ))));
3607                Ok(())
3608            })
3609        });
3610
3611        // Make this task visible to the guest and then record what it
3612        // was made visible as.
3613        let caller = match host_task {
3614            Some(caller) => caller,
3615            None => bail_bug!("host task wasn't created but should have been"),
3616        };
3617        let state = store.0.concurrent_state_mut()?;
3618        state.push_future(future);
3619        let instance = state.get_mut(caller.task)?.instance;
3620        let handle = store
3621            .0
3622            .instance_state(instance)
3623            .handle_table()
3624            .subtask_insert_host(task.rep())?;
3625        store.0.concurrent_state_mut()?.get_mut(task)?.common.handle = Some(handle);
3626        log::trace!("assign {task:?} handle {handle} for {caller:?} instance {instance:?}");
3627
3628        // Restore the currently running thread to this host task's
3629        // caller. Note that the host task isn't deallocated as it's
3630        // within the store and will get deallocated later.
3631        store.0.set_thread(caller)?;
3632        Ok(Status::Started.pack(Some(handle)))
3633    }
3634
3635    /// Implements the `task.return` intrinsic, lifting the result for the
3636    /// current guest task.
3637    pub(crate) fn task_return(
3638        self,
3639        store: &mut dyn VMStore,
3640        ty: TypeTupleIndex,
3641        options: OptionsIndex,
3642        storage: &[ValRaw],
3643    ) -> Result<()> {
3644        let guest_thread = store.current_guest_thread()?;
3645        let state = store.concurrent_state_mut()?;
3646        let lift = state
3647            .get_mut(guest_thread.task)?
3648            .lift_result
3649            .take()
3650            .ok_or_else(|| Trap::TaskCancelOrReturnTwice)?;
3651        if !state.get_mut(guest_thread.task)?.result.is_none() {
3652            bail_bug!("task result unexpectedly already set");
3653        }
3654
3655        let CanonicalOptions {
3656            string_encoding,
3657            data_model,
3658            ..
3659        } = &self.id().get(store).component().env_component().options[options];
3660
3661        let invalid = ty != lift.ty
3662            || string_encoding != &lift.string_encoding
3663            || match data_model {
3664                CanonicalOptionsDataModel::LinearMemory(opts) => match opts.memory {
3665                    Some(memory) => {
3666                        let expected = lift.memory.map(|v| v.as_ptr()).unwrap_or(ptr::null_mut());
3667                        let actual = self.id().get(store).runtime_memory(memory);
3668                        expected != actual.as_ptr()
3669                    }
3670                    // Memory not specified, meaning it didn't need to be
3671                    // specified per validation, so not invalid.
3672                    None => false,
3673                },
3674                // Always invalid as this isn't supported.
3675                CanonicalOptionsDataModel::Gc { .. } => true,
3676            };
3677
3678        if invalid {
3679            bail!(Trap::TaskReturnInvalid);
3680        }
3681
3682        log::trace!("task.return for {guest_thread:?}");
3683
3684        let result = (lift.lift)(store, storage)?;
3685        self.task_complete(store, guest_thread.task, result, Status::Returned)
3686    }
3687
3688    /// Implements the `task.cancel` intrinsic.
3689    pub(crate) fn task_cancel(self, store: &mut StoreOpaque) -> Result<()> {
3690        let guest_thread = store.current_guest_thread()?;
3691        let state = store.concurrent_state_mut()?;
3692        let task = state.get_mut(guest_thread.task)?;
3693        if !task.cancel_request_delivered {
3694            bail!(Trap::TaskCancelNotCancelled);
3695        }
3696        _ = task
3697            .lift_result
3698            .take()
3699            .ok_or_else(|| Trap::TaskCancelOrReturnTwice)?;
3700
3701        if !task.result.is_none() {
3702            bail_bug!("task result should not bet set yet");
3703        }
3704
3705        log::trace!("task.cancel for {guest_thread:?}");
3706
3707        self.task_complete(
3708            store,
3709            guest_thread.task,
3710            Box::new(DummyResult),
3711            Status::ReturnCancelled,
3712        )
3713    }
3714
3715    /// Complete the specified guest task (i.e. indicate that it has either
3716    /// returned a (possibly empty) result or cancelled itself).
3717    ///
3718    /// This will return any resource borrows and notify any current or future
3719    /// waiters that the task has completed.
3720    fn task_complete(
3721        self,
3722        store: &mut StoreOpaque,
3723        guest_task: TableId<GuestTask>,
3724        result: Box<dyn Any + Send + Sync>,
3725        status: Status,
3726    ) -> Result<()> {
3727        store
3728            .component_resource_tables(Some(self))?
3729            .validate_scope_exit()?;
3730
3731        let state = store.concurrent_state_mut()?;
3732        let task = state.get_mut(guest_task)?;
3733
3734        if let Caller::Host { tx, .. } = &mut task.caller {
3735            if let Some(tx) = tx.take() {
3736                _ = tx.send(result);
3737            }
3738        } else {
3739            task.result = Some(result);
3740            Waitable::Guest(guest_task).set_event(state, Some(Event::Subtask { status }))?;
3741        }
3742
3743        Ok(())
3744    }
3745
3746    /// Implements the `waitable-set.new` intrinsic.
3747    pub(crate) fn waitable_set_new(
3748        self,
3749        store: &mut StoreOpaque,
3750        caller_instance: RuntimeComponentInstanceIndex,
3751    ) -> Result<u32> {
3752        let set = store.concurrent_state_mut()?.push(WaitableSet::default())?;
3753        let handle = store
3754            .instance_state(self.runtime_instance(caller_instance))
3755            .handle_table()
3756            .waitable_set_insert(set.rep())?;
3757        log::trace!("new waitable set {set:?} (handle {handle})");
3758        Ok(handle)
3759    }
3760
3761    /// Implements the `waitable-set.drop` intrinsic.
3762    pub(crate) fn waitable_set_drop(
3763        self,
3764        store: &mut StoreOpaque,
3765        caller_instance: RuntimeComponentInstanceIndex,
3766        set: u32,
3767    ) -> Result<()> {
3768        let rep = store
3769            .instance_state(self.runtime_instance(caller_instance))
3770            .handle_table()
3771            .waitable_set_remove(set)?;
3772
3773        log::trace!("drop waitable set {rep} (handle {set})");
3774
3775        // Note that we're careful to check for waiters _before_ deleting the
3776        // set to avoid dropping any waiters in `WaitMode::Fiber(_)`, which
3777        // would panic.  See `drop-waitable-set-with-waiters.wast` for details.
3778        if !store
3779            .concurrent_state_mut()?
3780            .get_mut(TableId::<WaitableSet>::new(rep))?
3781            .waiting
3782            .is_empty()
3783        {
3784            bail!(Trap::WaitableSetDropHasWaiters);
3785        }
3786
3787        store
3788            .concurrent_state_mut()?
3789            .delete(TableId::<WaitableSet>::new(rep))?;
3790
3791        Ok(())
3792    }
3793
3794    /// Implements the `waitable.join` intrinsic.
3795    pub(crate) fn waitable_join(
3796        self,
3797        store: &mut StoreOpaque,
3798        caller_instance: RuntimeComponentInstanceIndex,
3799        waitable_handle: u32,
3800        set_handle: u32,
3801    ) -> Result<()> {
3802        let mut instance = self.id().get_mut(store);
3803        let waitable =
3804            Waitable::from_instance(instance.as_mut(), caller_instance, waitable_handle)?;
3805
3806        let set = if set_handle == 0 {
3807            None
3808        } else {
3809            let set = instance.instance_states().0[caller_instance]
3810                .handle_table()
3811                .waitable_set_rep(set_handle)?;
3812
3813            let state = store.concurrent_state_mut()?;
3814            if let Some(old) = waitable.common(state)?.set
3815                && state.get_mut(old)?.is_sync_call_set
3816            {
3817                bail!(Trap::WaitableSyncAndAsync);
3818            }
3819
3820            Some(TableId::<WaitableSet>::new(set))
3821        };
3822
3823        log::trace!(
3824            "waitable {waitable:?} (handle {waitable_handle}) join set {set:?} (handle {set_handle})",
3825        );
3826
3827        waitable.join(store.concurrent_state_mut()?, set)
3828    }
3829
3830    /// Implements the `subtask.drop` intrinsic.
3831    pub(crate) fn subtask_drop(
3832        self,
3833        store: &mut StoreOpaque,
3834        caller_instance: RuntimeComponentInstanceIndex,
3835        task_id: u32,
3836    ) -> Result<()> {
3837        self.waitable_join(store, caller_instance, task_id, 0)?;
3838
3839        let (rep, is_host) = store
3840            .instance_state(self.runtime_instance(caller_instance))
3841            .handle_table()
3842            .subtask_remove(task_id)?;
3843
3844        let concurrent_state = store.concurrent_state_mut()?;
3845        let (waitable, delete) = if is_host {
3846            let id = TableId::<HostTask>::new(rep);
3847            let task = concurrent_state.get_mut(id)?;
3848            match &task.state {
3849                HostTaskState::CalleeRunning(_) | HostTaskState::CalleeCancelling => {
3850                    bail!(Trap::SubtaskDropNotResolved)
3851                }
3852                HostTaskState::CalleeDone { .. } => {}
3853                HostTaskState::CalleeStarted | HostTaskState::CalleeFinished(_) => {
3854                    bail_bug!("invalid state for callee in `subtask.drop`")
3855                }
3856            }
3857
3858            (Waitable::Host(id), true)
3859        } else {
3860            let id = TableId::<GuestTask>::new(rep);
3861            let task = concurrent_state.get_mut(id)?;
3862            if task.lift_result.is_some() {
3863                bail!(Trap::SubtaskDropNotResolved);
3864            }
3865            (
3866                Waitable::Guest(id),
3867                concurrent_state.get_mut(id)?.ready_to_delete(),
3868            )
3869        };
3870
3871        waitable.common(concurrent_state)?.handle = None;
3872
3873        // If this subtask has an event that means that the terminal status of
3874        // this subtask wasn't yet received so it can't be dropped yet.
3875        if waitable.take_event(concurrent_state)?.is_some() {
3876            bail!(Trap::SubtaskDropNotResolved);
3877        }
3878
3879        if delete {
3880            waitable.delete_from(store)?;
3881        }
3882
3883        log::trace!("subtask_drop {waitable:?} (handle {task_id})");
3884        Ok(())
3885    }
3886
3887    /// Implements the `waitable-set.wait` intrinsic.
3888    pub(crate) fn waitable_set_wait(
3889        self,
3890        store: &mut StoreOpaque,
3891        options: OptionsIndex,
3892        set: u32,
3893        payload: u32,
3894    ) -> Result<u32> {
3895        let &CanonicalOptions {
3896            instance: caller_instance,
3897            ..
3898        } = &self.id().get(store).component().env_component().options[options];
3899        let caller = self.runtime_instance(caller_instance);
3900        let rep = store
3901            .instance_state(self.runtime_instance(caller_instance))
3902            .handle_table()
3903            .waitable_set_rep(set)?;
3904
3905        self.waitable_check(
3906            store,
3907            caller,
3908            WaitableCheck::Wait,
3909            WaitableCheckParams {
3910                set: TableId::new(rep),
3911                options,
3912                payload,
3913            },
3914        )
3915    }
3916
3917    /// Implements the `waitable-set.poll` intrinsic.
3918    pub(crate) fn waitable_set_poll(
3919        self,
3920        store: &mut StoreOpaque,
3921        options: OptionsIndex,
3922        set: u32,
3923        payload: u32,
3924    ) -> Result<u32> {
3925        let &CanonicalOptions {
3926            instance: caller_instance,
3927            ..
3928        } = &self.id().get(store).component().env_component().options[options];
3929        let caller = self.runtime_instance(caller_instance);
3930        let rep = store
3931            .instance_state(caller)
3932            .handle_table()
3933            .waitable_set_rep(set)?;
3934
3935        self.waitable_check(
3936            store,
3937            caller,
3938            WaitableCheck::Poll,
3939            WaitableCheckParams {
3940                set: TableId::new(rep),
3941                options,
3942                payload,
3943            },
3944        )
3945    }
3946
3947    /// Implements the `thread.index` intrinsic.
3948    pub(crate) fn thread_index(&self, store: &mut dyn VMStore) -> Result<u32> {
3949        let thread_id = store.current_guest_thread()?.thread;
3950        match store
3951            .concurrent_state_mut()?
3952            .get_mut(thread_id)?
3953            .instance_rep
3954        {
3955            Some(r) => Ok(r),
3956            None => bail_bug!("thread should have instance_rep by now"),
3957        }
3958    }
3959
3960    /// Implements the `thread.new-indirect` intrinsic.
3961    pub(crate) fn thread_new_indirect<T: 'static>(
3962        self,
3963        mut store: StoreContextMut<T>,
3964        runtime_instance: RuntimeComponentInstanceIndex,
3965        _func_ty_idx: TypeFuncIndex, // currently unused
3966        start_func_table_idx: RuntimeTableIndex,
3967        start_func_idx: u32,
3968        context: i32,
3969    ) -> Result<u32> {
3970        log::trace!("creating new thread");
3971
3972        let start_func_ty = FuncType::new(store.engine(), [ValType::I32], []);
3973        let (instance, registry) = self.id().get_mut_and_registry(store.0);
3974        let callee = instance
3975            .index_runtime_func_table(registry, start_func_table_idx, start_func_idx as u64)?
3976            .ok_or_else(|| Trap::ThreadNewIndirectUninitialized)?;
3977        if callee.type_index(store.0) != start_func_ty.type_index() {
3978            bail!(Trap::ThreadNewIndirectInvalidType);
3979        }
3980
3981        let token = StoreToken::new(store.as_context_mut());
3982        let start_func = Box::new(
3983            move |store: &mut dyn VMStore, guest_thread: QualifiedThreadId| -> Result<()> {
3984                let old_thread = store.set_thread(guest_thread)?;
3985                log::trace!(
3986                    "thread start: replaced {old_thread:?} with {guest_thread:?} as current thread"
3987                );
3988
3989                let mut store = token.as_context_mut(store);
3990                let mut params = [ValRaw::i32(context)];
3991                // Use call_unchecked rather than call or call_async, as we don't want to run the function
3992                // on a separate fiber if we're running in an async store.
3993                unsafe { callee.call_unchecked(store.as_context_mut(), &mut params)? };
3994
3995                store.0.set_thread(old_thread)?;
3996
3997                let runtime_instance = self.runtime_instance(runtime_instance);
3998
3999                // We're not returning to any caller, so we may need to switch
4000                // or trap if the instance has an outstanding sync-typed call:
4001                store
4002                    .0
4003                    .switch_or_trap_if_may_not_suspend(runtime_instance)?;
4004
4005                store
4006                    .0
4007                    .cleanup_thread(guest_thread, runtime_instance, CleanupTask::Yes)?;
4008
4009                log::trace!("explicit thread {guest_thread:?} completed");
4010                let state = store.0.concurrent_state_mut()?;
4011                if let Some(t) = old_thread.guest() {
4012                    state.get_mut(t.thread)?.state = GuestThreadState::Running;
4013                }
4014                log::trace!("thread start: restored {old_thread:?} as current thread");
4015
4016                Ok(())
4017            },
4018        );
4019
4020        let current_thread = store.0.current_guest_thread()?;
4021        let state = store.0.concurrent_state_mut()?;
4022        let parent_task = current_thread.task;
4023
4024        let new_thread = GuestThread::new_explicit(state, parent_task, start_func)?;
4025        let thread_id = state.push(new_thread)?;
4026        state.get_mut(parent_task)?.threads.insert(thread_id);
4027
4028        log::trace!("new thread with id {thread_id:?} created");
4029
4030        self.add_guest_thread_to_instance_table(thread_id, store.0, runtime_instance)
4031    }
4032
4033    pub(crate) fn resume_thread(
4034        self,
4035        store: &mut StoreOpaque,
4036        runtime_instance: RuntimeComponentInstanceIndex,
4037        thread_idx: u32,
4038        how: ResumeThread,
4039    ) -> Result<bool> {
4040        let thread_id =
4041            GuestThread::from_instance(self.id().get_mut(store), runtime_instance, thread_idx)?;
4042        let state = store.concurrent_state_mut()?;
4043        let guest_thread = QualifiedThreadId::qualify(state, thread_id)?;
4044
4045        if store.current_guest_thread()? == guest_thread {
4046            bail!(Trap::CannotResumeThread);
4047        }
4048
4049        let state = store.concurrent_state_mut()?;
4050        let thread = state.get_mut(guest_thread.thread)?;
4051        let priority = match how {
4052            ResumeThread::Promote | ResumeThread::Resume => Priority::Switch,
4053            ResumeThread::ResumeLater => Priority::Low,
4054        };
4055
4056        match (&how, &thread.state) {
4057            // Promotion is a noop unless the thread is in a ready state.
4058            (ResumeThread::Promote, GuestThreadState::Ready { .. }) => {}
4059            (ResumeThread::Promote, _) => return Ok(false),
4060
4061            // When resuming a thread it must be in a suspended state otherwise
4062            // this operation is a trap.
4063            (
4064                ResumeThread::Resume | ResumeThread::ResumeLater,
4065                GuestThreadState::NotStartedExplicit(_) | GuestThreadState::Suspended(_),
4066            ) => {}
4067            (ResumeThread::Resume | ResumeThread::ResumeLater, _) => {
4068                bail!(Trap::CannotResumeThread)
4069            }
4070        }
4071
4072        match mem::replace(&mut thread.state, GuestThreadState::Running) {
4073            GuestThreadState::NotStartedExplicit(start_func) => {
4074                log::trace!("starting thread {guest_thread:?}");
4075                let guest_call = WorkItem::GuestCall {
4076                    instance: self.runtime_instance(runtime_instance),
4077                    call: GuestCall {
4078                        thread: guest_thread,
4079                        kind: GuestCallKind::StartExplicit(Box::new(move |store| {
4080                            start_func(store, guest_thread)
4081                        })),
4082                    },
4083                };
4084                store
4085                    .concurrent_state_mut()?
4086                    .push_work_item(guest_call, priority)?;
4087            }
4088            GuestThreadState::Suspended(fiber) => {
4089                log::trace!("resuming thread {thread_id:?} that was suspended");
4090                store.concurrent_state_mut()?.push_work_item(
4091                    WorkItem::ResumeFiber {
4092                        instance: self.runtime_instance(runtime_instance),
4093                        thread: guest_thread,
4094                        fiber,
4095                    },
4096                    priority,
4097                )?;
4098            }
4099            GuestThreadState::Ready { fiber } => {
4100                log::trace!("resuming thread {thread_id:?} that was ready");
4101                thread.state = GuestThreadState::Ready { fiber };
4102                store
4103                    .concurrent_state_mut()?
4104                    .promote_thread_work_item(guest_thread)?;
4105            }
4106            other @ (GuestThreadState::NotStartedImplicit
4107            | GuestThreadState::Running
4108            | GuestThreadState::Completed) => {
4109                thread.state = other;
4110            }
4111        }
4112        Ok(true)
4113    }
4114
4115    fn add_guest_thread_to_instance_table(
4116        self,
4117        thread_id: TableId<GuestThread>,
4118        store: &mut StoreOpaque,
4119        runtime_instance: RuntimeComponentInstanceIndex,
4120    ) -> Result<u32> {
4121        let guest_id = store
4122            .instance_state(self.runtime_instance(runtime_instance))
4123            .thread_handle_table()
4124            .guest_thread_insert(thread_id.rep())?;
4125        store
4126            .concurrent_state_mut()?
4127            .get_mut(thread_id)?
4128            .instance_rep = Some(guest_id);
4129        Ok(guest_id)
4130    }
4131
4132    /// Helper function for the `thread.yield`, thread.suspend`,
4133    /// `thread.suspend-then-resume`, `thread.suspend-then-promote`,
4134    /// `thread.yield-then-resume`, and `thread.yield-then-promote` intrinsics.
4135    pub(crate) fn suspension_intrinsic(
4136        self,
4137        store: &mut StoreOpaque,
4138        caller: RuntimeComponentInstanceIndex,
4139        yielding: bool,
4140        to_thread: SuspensionTarget,
4141    ) -> Result<WaitResult> {
4142        let check_suspend = match to_thread {
4143            SuspensionTarget::Promote(thread) => {
4144                !self.resume_thread(store, caller, thread, ResumeThread::Promote)?
4145            }
4146            SuspensionTarget::Resume(thread) => {
4147                if !self.resume_thread(store, caller, thread, ResumeThread::Resume)? {
4148                    bail_bug!(
4149                        "`resume_thread` should only ever return false \
4150                         when `ResumeThread::Promote` is passed to it"
4151                    );
4152                }
4153                false
4154            }
4155            SuspensionTarget::None => true,
4156        };
4157
4158        if check_suspend && !store.switch_if_may_not_suspend(self.runtime_instance(caller))? {
4159            return if yielding {
4160                Ok(WaitResult::Completed)
4161            } else {
4162                Err(Trap::CannotBlockSyncTask.into())
4163            };
4164        }
4165
4166        let guest_thread = store.current_guest_thread()?;
4167
4168        let reason = if yielding {
4169            SuspendReason::Yielding {
4170                thread: guest_thread,
4171            }
4172        } else {
4173            SuspendReason::ExplicitlySuspending {
4174                thread: guest_thread,
4175            }
4176        };
4177
4178        store.suspend(reason)?;
4179
4180        Ok(WaitResult::Completed)
4181    }
4182
4183    /// Helper function for the `waitable-set.wait` and `waitable-set.poll` intrinsics.
4184    fn waitable_check(
4185        self,
4186        store: &mut StoreOpaque,
4187        caller: RuntimeInstance,
4188        check: WaitableCheck,
4189        params: WaitableCheckParams,
4190    ) -> Result<u32> {
4191        let guest_thread = store.current_guest_thread()?;
4192
4193        log::trace!("waitable check for {guest_thread:?}; set {:?}", params.set);
4194
4195        let state = store.concurrent_state_mut()?;
4196        let task = state.get_mut(guest_thread.task)?;
4197
4198        // If we're waiting, and there are no events immediately available,
4199        // suspend the fiber until that changes.
4200        match &check {
4201            WaitableCheck::Wait => {
4202                let set = params.set;
4203
4204                if (task.event.is_none() || matches!(task.event, Some(Event::Cancelled)))
4205                    && state.get_mut(set)?.ready.is_empty()
4206                {
4207                    store.switch_or_trap_if_may_not_suspend(caller)?;
4208
4209                    store.suspend(SuspendReason::Waiting {
4210                        set,
4211                        thread: guest_thread,
4212                    })?;
4213                }
4214            }
4215            WaitableCheck::Poll => {}
4216        }
4217
4218        log::trace!(
4219            "waitable check for {guest_thread:?}; set {:?}, part two",
4220            params.set
4221        );
4222
4223        // Deliver any pending events to the guest and return.
4224        let event = self.get_event(store, guest_thread.task, Some(params.set), false)?;
4225
4226        let (ordinal, handle, result) = match &check {
4227            WaitableCheck::Wait => {
4228                let (event, waitable) = match event {
4229                    Some(p) => p,
4230                    None => bail_bug!("event expected to be present"),
4231                };
4232                let handle = waitable.map(|(_, v)| v).unwrap_or(0);
4233                let (ordinal, result) = event.parts();
4234                (ordinal, handle, result)
4235            }
4236            WaitableCheck::Poll => {
4237                if let Some((event, waitable)) = event {
4238                    let handle = waitable.map(|(_, v)| v).unwrap_or(0);
4239                    let (ordinal, result) = event.parts();
4240                    (ordinal, handle, result)
4241                } else {
4242                    log::trace!(
4243                        "no events ready to deliver via waitable-set.poll to {:?}; set {:?}",
4244                        guest_thread.task,
4245                        params.set
4246                    );
4247                    let (ordinal, result) = Event::None.parts();
4248                    (ordinal, 0, result)
4249                }
4250            }
4251        };
4252        let memory = self.options_memory_mut(store, params.options);
4253        let ptr = crate::component::func::validate_inbounds_dynamic(
4254            &CanonicalAbiInfo::POINTER_PAIR,
4255            memory,
4256            &ValRaw::u32(params.payload),
4257        )?;
4258        memory[ptr + 0..][..4].copy_from_slice(&handle.to_le_bytes());
4259        memory[ptr + 4..][..4].copy_from_slice(&result.to_le_bytes());
4260        Ok(ordinal)
4261    }
4262
4263    /// Implements the `subtask.cancel` intrinsic.
4264    pub(crate) fn subtask_cancel(
4265        self,
4266        store: &mut StoreOpaque,
4267        caller_instance: RuntimeComponentInstanceIndex,
4268        async_: bool,
4269        task_id: u32,
4270    ) -> Result<u32> {
4271        let (rep, is_host) = store
4272            .instance_state(self.runtime_instance(caller_instance))
4273            .handle_table()
4274            .subtask_rep(task_id)?;
4275        let waitable = if is_host {
4276            Waitable::Host(TableId::<HostTask>::new(rep))
4277        } else {
4278            Waitable::Guest(TableId::<GuestTask>::new(rep))
4279        };
4280        let concurrent_state = store.concurrent_state_mut()?;
4281
4282        log::trace!("subtask_cancel {waitable:?} (handle {task_id}; async {async_})");
4283
4284        waitable.trap_if_in_waitable_set(concurrent_state)?;
4285
4286        let needs_block;
4287        if let Waitable::Host(host_task) = waitable {
4288            let state = &mut concurrent_state.get_mut(host_task)?.state;
4289            match state {
4290                // If the callee is still running, signal an abort is requested.
4291                //
4292                // After cancelling this falls through to block waiting for the
4293                // host task to actually finish assuming that `async_` is false.
4294                // This blocking behavior resolves the race of `handle.abort()`
4295                // with the task actually getting cancelled or finishing.
4296                HostTaskState::CalleeRunning(handle) => {
4297                    handle.abort();
4298                    *state = HostTaskState::CalleeCancelling;
4299                    needs_block = true;
4300                }
4301
4302                // Cancellation was already requested, so fail as the task can't
4303                // be cancelled twice.
4304                HostTaskState::CalleeCancelling | HostTaskState::CalleeDone { cancelled: true } => {
4305                    bail!(Trap::SubtaskCancelAfterTerminal);
4306                }
4307                HostTaskState::CalleeDone { cancelled: false } => {
4308                    // The callee is already done so there's no need to
4309                    // block further for an event.
4310                    *state = HostTaskState::CalleeDone { cancelled: true };
4311                    needs_block = false;
4312                }
4313
4314                // These states should not be possible for a subtask that's
4315                // visible from the guest, so trap here.
4316                HostTaskState::CalleeStarted | HostTaskState::CalleeFinished(_) => {
4317                    bail_bug!("invalid states for host callee")
4318                }
4319            }
4320        } else {
4321            let guest_task = TableId::<GuestTask>::new(rep);
4322            let task = concurrent_state.get_mut(guest_task)?;
4323            if !task.already_lowered_parameters() {
4324                store.cancel_guest_subtask_without_lowered_parameters(
4325                    self.runtime_instance(caller_instance),
4326                    guest_task,
4327                )?;
4328                return Ok(Status::StartCancelled as u32);
4329            } else if !task.returned_or_cancelled() {
4330                // Started, but not yet returned or cancelled; send the
4331                // `CANCELLED` event
4332                //
4333                // Note that this might overwrite an event that was set earlier
4334                // (e.g. `Event::None` if the task is yielding, or
4335                // `Event::Cancelled` if it was already cancelled), but that's
4336                // okay -- this should supersede the previous state.
4337                task.event = Some(Event::Cancelled);
4338                let runtime_instance = task.instance;
4339                for thread in task.threads.clone() {
4340                    let thread = QualifiedThreadId {
4341                        task: guest_task,
4342                        thread,
4343                    };
4344                    let thread_mut = concurrent_state.get_mut(thread.thread)?;
4345
4346                    let yield_ = |store: &mut StoreOpaque| {
4347                        // While we're yielding, temporarily set
4348                        // `do_not_suspend` to false on the subtask's instance
4349                        // since we'll be getting control back if it does
4350                        // suspend.
4351                        let state = store.instance_state(runtime_instance).concurrent_state();
4352                        let old_do_not_suspend = state.do_not_suspend;
4353                        state.do_not_suspend = false;
4354
4355                        let caller = store.current_guest_thread()?;
4356
4357                        // Temporarily add the waitable to the caller's
4358                        // `sync_call_set` to ensure that (1) it isn't already
4359                        // part of a different set and (2) it can't be added to
4360                        // a different set while we yield to the subtask.
4361                        let state = store.concurrent_state_mut()?;
4362                        let set = state.get_mut(caller.thread)?.sync_call_set;
4363                        waitable.join(state, Some(set))?;
4364
4365                        store.suspend(SuspendReason::YieldingToSubtask { thread: caller })?;
4366
4367                        let state = store.concurrent_state_mut()?;
4368                        waitable.join(state, None)?;
4369
4370                        store
4371                            .instance_state(runtime_instance)
4372                            .concurrent_state()
4373                            .do_not_suspend = old_do_not_suspend;
4374
4375                        Ok::<(), crate::Error>(())
4376                    };
4377
4378                    match thread_mut.wake_on_cancel.take() {
4379                        WakeOnCancel::Waiting(set) => {
4380                            // The thread is in a cancellable wait, so wake it up:
4381                            let item = match concurrent_state.get_mut(set)?.waiting.remove(&thread)
4382                            {
4383                                Some(WaitMode::Callback(instance)) => WorkItem::GuestCall {
4384                                    instance: runtime_instance,
4385                                    call: GuestCall {
4386                                        thread,
4387                                        kind: GuestCallKind::DeliverEvent {
4388                                            instance,
4389                                            set: None,
4390                                        },
4391                                    },
4392                                },
4393                                other => bail_bug!(
4394                                    "expected `Some(WaitMode::Callback(_))`; got `{other:?}`"
4395                                ),
4396                            };
4397                            concurrent_state.set_switch_item(item)?;
4398
4399                            yield_(store)?;
4400
4401                            break;
4402                        }
4403                        WakeOnCancel::Yielding => {
4404                            if concurrent_state.promote_thread_work_item(thread)? {
4405                                yield_(store)?;
4406                                break;
4407                            } else {
4408                                bail_bug!("thread with `WakeOnCancel::Yielding` not promotable");
4409                            }
4410                        }
4411                        WakeOnCancel::None => {}
4412                    }
4413                }
4414
4415                // Guest tasks need to block if they have not yet returned or
4416                // cancelled, even as a result of the event delivery above.
4417                needs_block = !store
4418                    .concurrent_state_mut()?
4419                    .get_mut(guest_task)?
4420                    .returned_or_cancelled()
4421            } else {
4422                needs_block = false;
4423            }
4424        };
4425
4426        // If we need to block waiting on the terminal status of this subtask
4427        // then return immediately in `async` mode, or otherwise wait for the
4428        // event to get signaled through the store.
4429        if needs_block {
4430            if async_ {
4431                return Ok(BLOCKED);
4432            }
4433
4434            // Save and later restore `next_switch_item` during a sync cancel so
4435            // we don't try to switch to it while blocking.
4436            let old_next_switch_item = {
4437                let state = store.concurrent_state_mut()?;
4438                let item = state.next_switch_item.take();
4439                // Note that we store it in the table here rather than directly
4440                // in a local variable to ensure the fiber is disposed of
4441                // properly if we end up trapping or panicking.
4442                state.push(item)?
4443            };
4444
4445            // Wait for this waitable to get signaled with its terminal
4446            // status. Once that's done fall through to the shared code.
4447            store.wait_for_event(self.runtime_instance(caller_instance), waitable)?;
4448
4449            let state = store.concurrent_state_mut()?;
4450            state.next_switch_item = state.delete(old_next_switch_item)?;
4451
4452            // .. fall through to determine what event's in store for us.
4453        }
4454
4455        let event = waitable.take_event(store.concurrent_state_mut()?)?;
4456        if let Some(Event::Subtask {
4457            status: status @ (Status::Returned | Status::ReturnCancelled),
4458        }) = event
4459        {
4460            Ok(status as u32)
4461        } else {
4462            bail!(Trap::SubtaskCancelAfterTerminal);
4463        }
4464    }
4465}
4466
4467/// Trait representing component model ABI async intrinsics and fused adapter
4468/// helper functions.
4469///
4470/// SAFETY (callers): Most of the methods in this trait accept raw pointers,
4471/// which must be valid for at least the duration of the call (and possibly for
4472/// as long as the relevant guest task exists, in the case of `*mut VMFuncRef`
4473/// pointers used for async calls).
4474pub trait VMComponentAsyncStore {
4475    /// A helper function for fused adapter modules involving calls where the
4476    /// one of the caller or callee is async.
4477    ///
4478    /// This helper is not used when the caller and callee both use the sync
4479    /// ABI, only when at least one is async is this used.
4480    unsafe fn prepare_call(
4481        &mut self,
4482        instance: Instance,
4483        memory: *mut VMMemoryDefinition,
4484        start: NonNull<VMFuncRef>,
4485        return_: NonNull<VMFuncRef>,
4486        caller_instance: RuntimeComponentInstanceIndex,
4487        callee_instance: RuntimeComponentInstanceIndex,
4488        task_return_type: TypeTupleIndex,
4489        callee_async: bool,
4490        string_encoding: StringEncoding,
4491        result_count: u32,
4492        storage: *mut ValRaw,
4493        storage_len: usize,
4494    ) -> Result<()>;
4495
4496    /// A helper function for fused adapter modules involving calls where the
4497    /// caller is sync-lowered but the callee is async-lifted.
4498    unsafe fn sync_start(
4499        &mut self,
4500        instance: Instance,
4501        callback: *mut VMFuncRef,
4502        callee: NonNull<VMFuncRef>,
4503        param_count: u32,
4504        storage: *mut MaybeUninit<ValRaw>,
4505        storage_len: usize,
4506    ) -> Result<()>;
4507
4508    /// A helper function for fused adapter modules involving calls where the
4509    /// caller is async-lowered.
4510    unsafe fn async_start(
4511        &mut self,
4512        instance: Instance,
4513        callback: *mut VMFuncRef,
4514        post_return: *mut VMFuncRef,
4515        callee: NonNull<VMFuncRef>,
4516        param_count: u32,
4517        result_count: u32,
4518        flags: u32,
4519    ) -> Result<u32>;
4520
4521    /// The `future.write` intrinsic.
4522    fn future_write(
4523        &mut self,
4524        instance: Instance,
4525        caller: RuntimeComponentInstanceIndex,
4526        ty: TypeFutureTableIndex,
4527        options: OptionsIndex,
4528        future: u32,
4529        address: u32,
4530    ) -> Result<u32>;
4531
4532    /// The `future.read` intrinsic.
4533    fn future_read(
4534        &mut self,
4535        instance: Instance,
4536        caller: RuntimeComponentInstanceIndex,
4537        ty: TypeFutureTableIndex,
4538        options: OptionsIndex,
4539        future: u32,
4540        address: u32,
4541    ) -> Result<u32>;
4542
4543    /// The `future.drop-writable` intrinsic.
4544    fn future_drop_writable(
4545        &mut self,
4546        instance: Instance,
4547        ty: TypeFutureTableIndex,
4548        writer: u32,
4549    ) -> Result<()>;
4550
4551    /// The `stream.write` intrinsic.
4552    fn stream_write(
4553        &mut self,
4554        instance: Instance,
4555        caller: RuntimeComponentInstanceIndex,
4556        ty: TypeStreamTableIndex,
4557        options: OptionsIndex,
4558        stream: u32,
4559        address: u32,
4560        count: u32,
4561    ) -> Result<u32>;
4562
4563    /// The `stream.read` intrinsic.
4564    fn stream_read(
4565        &mut self,
4566        instance: Instance,
4567        caller: RuntimeComponentInstanceIndex,
4568        ty: TypeStreamTableIndex,
4569        options: OptionsIndex,
4570        stream: u32,
4571        address: u32,
4572        count: u32,
4573    ) -> Result<u32>;
4574
4575    /// The "fast-path" implementation of the `stream.write` intrinsic for
4576    /// "flat" (i.e. memcpy-able) payloads.
4577    fn flat_stream_write(
4578        &mut self,
4579        instance: Instance,
4580        caller: RuntimeComponentInstanceIndex,
4581        ty: TypeStreamTableIndex,
4582        options: OptionsIndex,
4583        payload_size: u32,
4584        payload_align: u32,
4585        stream: u32,
4586        address: u32,
4587        count: u32,
4588    ) -> Result<u32>;
4589
4590    /// The "fast-path" implementation of the `stream.read` intrinsic for "flat"
4591    /// (i.e. memcpy-able) payloads.
4592    fn flat_stream_read(
4593        &mut self,
4594        instance: Instance,
4595        caller: RuntimeComponentInstanceIndex,
4596        ty: TypeStreamTableIndex,
4597        options: OptionsIndex,
4598        payload_size: u32,
4599        payload_align: u32,
4600        stream: u32,
4601        address: u32,
4602        count: u32,
4603    ) -> Result<u32>;
4604
4605    /// The `stream.drop-writable` intrinsic.
4606    fn stream_drop_writable(
4607        &mut self,
4608        instance: Instance,
4609        ty: TypeStreamTableIndex,
4610        writer: u32,
4611    ) -> Result<()>;
4612
4613    /// The `error-context.debug-message` intrinsic.
4614    fn error_context_debug_message(
4615        &mut self,
4616        instance: Instance,
4617        ty: TypeComponentLocalErrorContextTableIndex,
4618        options: OptionsIndex,
4619        err_ctx_handle: u32,
4620        debug_msg_address: u32,
4621    ) -> Result<()>;
4622
4623    /// The `thread.new-indirect` intrinsic
4624    fn thread_new_indirect(
4625        &mut self,
4626        instance: Instance,
4627        caller: RuntimeComponentInstanceIndex,
4628        func_ty_idx: TypeFuncIndex,
4629        start_func_table_idx: RuntimeTableIndex,
4630        start_func_idx: u32,
4631        context: i32,
4632    ) -> Result<u32>;
4633}
4634
4635/// SAFETY: See trait docs.
4636impl<T: 'static> VMComponentAsyncStore for StoreInner<T> {
4637    unsafe fn prepare_call(
4638        &mut self,
4639        instance: Instance,
4640        memory: *mut VMMemoryDefinition,
4641        start: NonNull<VMFuncRef>,
4642        return_: NonNull<VMFuncRef>,
4643        caller_instance: RuntimeComponentInstanceIndex,
4644        callee_instance: RuntimeComponentInstanceIndex,
4645        task_return_type: TypeTupleIndex,
4646        callee_async: bool,
4647        string_encoding: StringEncoding,
4648        result_count_or_max_if_async: u32,
4649        storage: *mut ValRaw,
4650        storage_len: usize,
4651    ) -> Result<()> {
4652        // SAFETY: The `wasmtime_cranelift`-generated code that calls
4653        // this method will have ensured that `storage` is a valid
4654        // pointer containing at least `storage_len` items.
4655        let params = unsafe { core::slice::from_raw_parts(storage, storage_len) }.to_vec();
4656
4657        unsafe {
4658            instance.prepare_call(
4659                StoreContextMut(self),
4660                start,
4661                return_,
4662                caller_instance,
4663                callee_instance,
4664                task_return_type,
4665                callee_async,
4666                memory,
4667                string_encoding,
4668                match result_count_or_max_if_async {
4669                    PREPARE_ASYNC_NO_RESULT => CallerInfo::Async {
4670                        params,
4671                        has_result: false,
4672                    },
4673                    PREPARE_ASYNC_WITH_RESULT => CallerInfo::Async {
4674                        params,
4675                        has_result: true,
4676                    },
4677                    result_count => CallerInfo::Sync {
4678                        params,
4679                        result_count,
4680                    },
4681                },
4682            )
4683        }
4684    }
4685
4686    unsafe fn sync_start(
4687        &mut self,
4688        instance: Instance,
4689        callback: *mut VMFuncRef,
4690        callee: NonNull<VMFuncRef>,
4691        param_count: u32,
4692        storage: *mut MaybeUninit<ValRaw>,
4693        storage_len: usize,
4694    ) -> Result<()> {
4695        unsafe {
4696            instance
4697                .start_call(
4698                    StoreContextMut(self),
4699                    callback,
4700                    ptr::null_mut(),
4701                    callee,
4702                    param_count,
4703                    1,
4704                    START_FLAG_ASYNC_CALLEE,
4705                    // SAFETY: The `wasmtime_cranelift`-generated code that calls
4706                    // this method will have ensured that `storage` is a valid
4707                    // pointer containing at least `storage_len` items.
4708                    Some(core::slice::from_raw_parts_mut(storage, storage_len)),
4709                )
4710                .map(drop)
4711        }
4712    }
4713
4714    unsafe fn async_start(
4715        &mut self,
4716        instance: Instance,
4717        callback: *mut VMFuncRef,
4718        post_return: *mut VMFuncRef,
4719        callee: NonNull<VMFuncRef>,
4720        param_count: u32,
4721        result_count: u32,
4722        flags: u32,
4723    ) -> Result<u32> {
4724        unsafe {
4725            instance.start_call(
4726                StoreContextMut(self),
4727                callback,
4728                post_return,
4729                callee,
4730                param_count,
4731                result_count,
4732                flags,
4733                None,
4734            )
4735        }
4736    }
4737
4738    fn future_write(
4739        &mut self,
4740        instance: Instance,
4741        caller: RuntimeComponentInstanceIndex,
4742        ty: TypeFutureTableIndex,
4743        options: OptionsIndex,
4744        future: u32,
4745        address: u32,
4746    ) -> Result<u32> {
4747        instance
4748            .guest_write(
4749                StoreContextMut(self),
4750                caller,
4751                TransmitIndex::Future(ty),
4752                options,
4753                None,
4754                future,
4755                address,
4756                1,
4757            )
4758            .map(|result| result.encode())
4759    }
4760
4761    fn future_read(
4762        &mut self,
4763        instance: Instance,
4764        caller: RuntimeComponentInstanceIndex,
4765        ty: TypeFutureTableIndex,
4766        options: OptionsIndex,
4767        future: u32,
4768        address: u32,
4769    ) -> Result<u32> {
4770        instance
4771            .guest_read(
4772                StoreContextMut(self),
4773                caller,
4774                TransmitIndex::Future(ty),
4775                options,
4776                None,
4777                future,
4778                address,
4779                1,
4780            )
4781            .map(|result| result.encode())
4782    }
4783
4784    fn stream_write(
4785        &mut self,
4786        instance: Instance,
4787        caller: RuntimeComponentInstanceIndex,
4788        ty: TypeStreamTableIndex,
4789        options: OptionsIndex,
4790        stream: u32,
4791        address: u32,
4792        count: u32,
4793    ) -> Result<u32> {
4794        instance
4795            .guest_write(
4796                StoreContextMut(self),
4797                caller,
4798                TransmitIndex::Stream(ty),
4799                options,
4800                None,
4801                stream,
4802                address,
4803                count,
4804            )
4805            .map(|result| result.encode())
4806    }
4807
4808    fn stream_read(
4809        &mut self,
4810        instance: Instance,
4811        caller: RuntimeComponentInstanceIndex,
4812        ty: TypeStreamTableIndex,
4813        options: OptionsIndex,
4814        stream: u32,
4815        address: u32,
4816        count: u32,
4817    ) -> Result<u32> {
4818        instance
4819            .guest_read(
4820                StoreContextMut(self),
4821                caller,
4822                TransmitIndex::Stream(ty),
4823                options,
4824                None,
4825                stream,
4826                address,
4827                count,
4828            )
4829            .map(|result| result.encode())
4830    }
4831
4832    fn future_drop_writable(
4833        &mut self,
4834        instance: Instance,
4835        ty: TypeFutureTableIndex,
4836        writer: u32,
4837    ) -> Result<()> {
4838        instance.guest_drop_writable(self, TransmitIndex::Future(ty), writer)
4839    }
4840
4841    fn flat_stream_write(
4842        &mut self,
4843        instance: Instance,
4844        caller: RuntimeComponentInstanceIndex,
4845        ty: TypeStreamTableIndex,
4846        options: OptionsIndex,
4847        payload_size: u32,
4848        payload_align: u32,
4849        stream: u32,
4850        address: u32,
4851        count: u32,
4852    ) -> Result<u32> {
4853        instance
4854            .guest_write(
4855                StoreContextMut(self),
4856                caller,
4857                TransmitIndex::Stream(ty),
4858                options,
4859                Some(FlatAbi {
4860                    size: payload_size,
4861                    align: payload_align,
4862                }),
4863                stream,
4864                address,
4865                count,
4866            )
4867            .map(|result| result.encode())
4868    }
4869
4870    fn flat_stream_read(
4871        &mut self,
4872        instance: Instance,
4873        caller: RuntimeComponentInstanceIndex,
4874        ty: TypeStreamTableIndex,
4875        options: OptionsIndex,
4876        payload_size: u32,
4877        payload_align: u32,
4878        stream: u32,
4879        address: u32,
4880        count: u32,
4881    ) -> Result<u32> {
4882        instance
4883            .guest_read(
4884                StoreContextMut(self),
4885                caller,
4886                TransmitIndex::Stream(ty),
4887                options,
4888                Some(FlatAbi {
4889                    size: payload_size,
4890                    align: payload_align,
4891                }),
4892                stream,
4893                address,
4894                count,
4895            )
4896            .map(|result| result.encode())
4897    }
4898
4899    fn stream_drop_writable(
4900        &mut self,
4901        instance: Instance,
4902        ty: TypeStreamTableIndex,
4903        writer: u32,
4904    ) -> Result<()> {
4905        instance.guest_drop_writable(self, TransmitIndex::Stream(ty), writer)
4906    }
4907
4908    fn error_context_debug_message(
4909        &mut self,
4910        instance: Instance,
4911        ty: TypeComponentLocalErrorContextTableIndex,
4912        options: OptionsIndex,
4913        err_ctx_handle: u32,
4914        debug_msg_address: u32,
4915    ) -> Result<()> {
4916        instance.error_context_debug_message(
4917            StoreContextMut(self),
4918            ty,
4919            options,
4920            err_ctx_handle,
4921            debug_msg_address,
4922        )
4923    }
4924
4925    fn thread_new_indirect(
4926        &mut self,
4927        instance: Instance,
4928        caller: RuntimeComponentInstanceIndex,
4929        func_ty_idx: TypeFuncIndex,
4930        start_func_table_idx: RuntimeTableIndex,
4931        start_func_idx: u32,
4932        context: i32,
4933    ) -> Result<u32> {
4934        instance.thread_new_indirect(
4935            StoreContextMut(self),
4936            caller,
4937            func_ty_idx,
4938            start_func_table_idx,
4939            start_func_idx,
4940            context,
4941        )
4942    }
4943}
4944
4945type HostTaskFuture = Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>;
4946
4947/// Runs the given future with the current thread set to `task` each time it is
4948/// polled.
4949async fn run_with_host_task_set<F>(task: TableId<HostTask>, future: F) -> Result<F::Output>
4950where
4951    F: Future,
4952{
4953    let mut future = pin!(future);
4954    future::poll_fn(|cx| {
4955        let old_thread = match tls::get(|store| store.set_thread(task)) {
4956            Ok(thread) => thread,
4957            Err(error) => return Poll::Ready(Err(error)),
4958        };
4959        let result = future.as_mut().poll(cx);
4960        match tls::get(|store| store.set_thread(old_thread)) {
4961            Ok(_) => result.map(Ok),
4962            Err(error) => Poll::Ready(Err(error)),
4963        }
4964    })
4965    .await
4966}
4967
4968/// Represents the state of a pending host task.
4969///
4970/// This is used to represent tasks when the guest calls into the host.
4971pub(crate) struct HostTask {
4972    common: WaitableCommon,
4973
4974    /// State of borrows/etc the host needs to track. Used when the guest passes
4975    /// borrows to the host, for example.
4976    call_context: CallContext,
4977
4978    state: HostTaskState,
4979
4980    group: TaskGroupId,
4981}
4982
4983enum HostTaskState {
4984    /// A host task has been created and it's considered "started".
4985    ///
4986    /// The host task has yet to enter `first_poll` or `poll_and_block` which
4987    /// is where this will get updated further.
4988    CalleeStarted,
4989
4990    /// State used for tasks in `first_poll` meaning that the guest did an async
4991    /// lower of a host async function which is blocked. The specified handle is
4992    /// linked to the future in the main `FuturesUnordered` of a store which is
4993    /// used to cancel it if the guest requests cancellation.
4994    CalleeRunning(JoinHandle),
4995
4996    /// Cancellation was requested, but the completion worker has not finished
4997    /// lowering the result and publishing the terminal event. The task must
4998    /// remain in the table until that worker completes.
4999    CalleeCancelling,
5000
5001    /// Terminal state used for tasks in `poll_and_block` to store the result of
5002    /// their computation. Note that this state is not used for tasks in
5003    /// `first_poll`.
5004    CalleeFinished(LiftedResult),
5005
5006    /// Terminal state for host tasks meaning that the task was cancelled or the
5007    /// result was taken.
5008    CalleeDone { cancelled: bool },
5009}
5010
5011impl HostTask {
5012    fn new(
5013        concurrent_state: &mut ConcurrentState,
5014        state: HostTaskState,
5015        caller: QualifiedThreadId,
5016    ) -> Result<Self> {
5017        let group = concurrent_state.get_mut(caller.task)?.group;
5018        concurrent_state.increment_group_ref_count(group)?;
5019
5020        Ok(Self {
5021            common: WaitableCommon::default(),
5022            call_context: CallContext::default(),
5023            state,
5024            group,
5025        })
5026    }
5027}
5028
5029impl TableDebug for HostTask {
5030    fn type_name() -> &'static str {
5031        "HostTask"
5032    }
5033}
5034
5035type CallbackFn = Box<dyn Fn(&mut dyn VMStore, Event, u32) -> Result<u32> + Send + Sync + 'static>;
5036
5037/// Represents the caller of a given guest task.
5038enum Caller {
5039    /// The host called the guest task.
5040    Host {
5041        /// If present, may be used to deliver the result.
5042        tx: Option<oneshot::Sender<LiftedResult>>,
5043        /// If true, there's a host future that must be dropped before the task
5044        /// can be deleted.
5045        host_future_present: bool,
5046        /// The host task which called into the guest, or `None` for a call from
5047        /// the top-level host. The task may belong to an entirely unrelated
5048        /// top-level component instance than the one the host called into.
5049        caller: Option<TableId<HostTask>>,
5050    },
5051    /// Another guest thread called the guest task
5052    Guest {
5053        /// The id of the caller
5054        thread: QualifiedThreadId,
5055    },
5056}
5057
5058/// Represents a closure and related canonical ABI parameters required to
5059/// validate a `task.return` call at runtime and lift the result.
5060struct LiftResult {
5061    lift: RawLift,
5062    ty: TypeTupleIndex,
5063    memory: Option<SendSyncPtr<VMMemoryDefinition>>,
5064    string_encoding: StringEncoding,
5065}
5066
5067/// The table ID for a guest thread, qualified by the task to which it belongs.
5068///
5069/// This exists to minimize table lookups and the necessity to pass stores around mutably
5070/// for the common case of identifying the task to which a thread belongs.
5071#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
5072pub(crate) struct QualifiedThreadId {
5073    task: TableId<GuestTask>,
5074    thread: TableId<GuestThread>,
5075}
5076
5077impl QualifiedThreadId {
5078    fn qualify(
5079        state: &mut ConcurrentState,
5080        thread: TableId<GuestThread>,
5081    ) -> Result<QualifiedThreadId> {
5082        Ok(QualifiedThreadId {
5083            task: state.get_mut(thread)?.parent_task,
5084            thread,
5085        })
5086    }
5087}
5088
5089impl fmt::Debug for QualifiedThreadId {
5090    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5091        f.debug_tuple("QualifiedThreadId")
5092            .field(&self.task.rep())
5093            .field(&self.thread.rep())
5094            .finish()
5095    }
5096}
5097
5098enum GuestThreadState {
5099    NotStartedImplicit,
5100    NotStartedExplicit(
5101        Box<dyn FnOnce(&mut dyn VMStore, QualifiedThreadId) -> Result<()> + Send + Sync>,
5102    ),
5103    Running,
5104    Suspended(StoreFiber<'static>),
5105    Ready {
5106        fiber: StoreFiber<'static>,
5107    },
5108    Completed,
5109}
5110
5111impl fmt::Debug for GuestThreadState {
5112    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5113        match self {
5114            Self::NotStartedImplicit => f.debug_tuple("NotStartedImplicit").finish(),
5115            Self::NotStartedExplicit(_) => f.debug_tuple("NotStartedExplicit").finish(),
5116            Self::Running => f.debug_tuple("Running").finish(),
5117            Self::Suspended(_) => f.debug_tuple("Suspended").finish(),
5118            Self::Ready { .. } => f.debug_struct("Ready").finish(),
5119            Self::Completed => f.debug_tuple("Completed").finish(),
5120        }
5121    }
5122}
5123
5124#[derive(Copy, Clone, PartialEq, Eq, Debug)]
5125enum WakeOnCancel {
5126    None,
5127    Waiting(TableId<WaitableSet>),
5128    Yielding,
5129}
5130
5131impl WakeOnCancel {
5132    fn is_none(self) -> bool {
5133        matches!(self, WakeOnCancel::None)
5134    }
5135
5136    fn replace(&mut self, other: WakeOnCancel) -> Self {
5137        let old = *self;
5138        *self = other;
5139        old
5140    }
5141
5142    fn take(&mut self) -> Self {
5143        self.replace(WakeOnCancel::None)
5144    }
5145}
5146
5147pub struct GuestThread {
5148    /// Context-local state used to implement the `context.{get,set}`
5149    /// intrinsics.
5150    context: [u32; NUM_COMPONENT_CONTEXT_SLOTS],
5151    /// The owning guest task.
5152    parent_task: TableId<GuestTask>,
5153    /// If non-`None`, indicates that the thread is currently either waiting a
5154    /// waitable set or yielding but may be cancelled and woken immediately.
5155    wake_on_cancel: WakeOnCancel,
5156    /// The execution state of this guest thread
5157    state: GuestThreadState,
5158    /// The index of this thread in the component instance's handle table.
5159    /// This must always be `Some` after initialization.
5160    instance_rep: Option<u32>,
5161    /// Scratch waitable set used to watch subtasks during synchronous calls.
5162    sync_call_set: TableId<WaitableSet>,
5163    /// The old value of `do_not_suspend` prior to the sync-typed task for which
5164    /// this thread was created, if relevant.
5165    old_do_not_suspend: Option<bool>,
5166}
5167
5168impl GuestThread {
5169    /// Retrieve the `GuestThread` corresponding to the specified guest-visible
5170    /// handle.
5171    fn from_instance(
5172        state: Pin<&mut ComponentInstance>,
5173        caller_instance: RuntimeComponentInstanceIndex,
5174        guest_thread: u32,
5175    ) -> Result<TableId<Self>> {
5176        let rep = state.instance_states().0[caller_instance]
5177            .thread_handle_table()
5178            .guest_thread_rep(guest_thread)?;
5179        Ok(TableId::new(rep))
5180    }
5181
5182    fn new_implicit(state: &mut ConcurrentState, parent_task: TableId<GuestTask>) -> Result<Self> {
5183        let sync_call_set = state.push(WaitableSet {
5184            is_sync_call_set: true,
5185            ..WaitableSet::default()
5186        })?;
5187        Ok(Self {
5188            context: [0; NUM_COMPONENT_CONTEXT_SLOTS],
5189            parent_task,
5190            wake_on_cancel: WakeOnCancel::None,
5191            state: GuestThreadState::NotStartedImplicit,
5192            instance_rep: None,
5193            sync_call_set,
5194            old_do_not_suspend: None,
5195        })
5196    }
5197
5198    fn new_explicit(
5199        state: &mut ConcurrentState,
5200        parent_task: TableId<GuestTask>,
5201        start_func: Box<
5202            dyn FnOnce(&mut dyn VMStore, QualifiedThreadId) -> Result<()> + Send + Sync,
5203        >,
5204    ) -> Result<Self> {
5205        let sync_call_set = state.push(WaitableSet {
5206            is_sync_call_set: true,
5207            ..WaitableSet::default()
5208        })?;
5209        Ok(Self {
5210            context: [0; NUM_COMPONENT_CONTEXT_SLOTS],
5211            parent_task,
5212            wake_on_cancel: WakeOnCancel::None,
5213            state: GuestThreadState::NotStartedExplicit(start_func),
5214            instance_rep: None,
5215            sync_call_set,
5216            old_do_not_suspend: None,
5217        })
5218    }
5219}
5220
5221impl TableDebug for GuestThread {
5222    fn type_name() -> &'static str {
5223        "GuestThread"
5224    }
5225}
5226
5227enum SyncResult {
5228    NotProduced,
5229    Produced(Option<ValRaw>),
5230    Taken,
5231}
5232
5233impl SyncResult {
5234    fn take(&mut self) -> Result<Option<Option<ValRaw>>> {
5235        Ok(match mem::replace(self, SyncResult::Taken) {
5236            SyncResult::NotProduced => None,
5237            SyncResult::Produced(val) => Some(val),
5238            SyncResult::Taken => {
5239                bail_bug!("attempted to take a synchronous result that was already taken")
5240            }
5241        })
5242    }
5243}
5244
5245#[derive(Debug)]
5246enum HostFutureState {
5247    NotApplicable,
5248    Live,
5249    Dropped,
5250}
5251
5252/// Represents a pending guest task.
5253pub(crate) struct GuestTask {
5254    /// See `WaitableCommon`
5255    common: WaitableCommon,
5256    /// Closure to lower the parameters passed to this task.
5257    lower_params: Option<RawLower>,
5258    /// See `LiftResult`
5259    lift_result: Option<LiftResult>,
5260    /// A place to stash the type-erased lifted result if it can't be delivered
5261    /// immediately.
5262    result: Option<LiftedResult>,
5263    /// Closure to call the callback function for an async-lifted export, if
5264    /// provided.
5265    callback: Option<CallbackFn>,
5266    /// See `Caller`
5267    caller: Caller,
5268    /// Borrow state for this task.
5269    ///
5270    /// Keeps track of `borrow<T>` received to this task to ensure that
5271    /// everything is dropped by the time it exits.
5272    call_context: CallContext,
5273    /// A place to stash the lowered result for a sync-to-async call until it
5274    /// can be returned to the caller.
5275    sync_result: SyncResult,
5276    /// Whether or not the task has been cancelled (i.e. whether the
5277    /// cancellation request has been delivered to the task, and thus whether
5278    /// the task is permitted to call `task.cancel`).
5279    cancel_request_delivered: bool,
5280    /// Whether or not we've sent a `Status::Starting` event to any current or
5281    /// future waiters for this waitable.
5282    starting_sent: bool,
5283    /// The runtime instance to which the exported function for this guest task
5284    /// belongs.
5285    ///
5286    /// Note that the task may do a sync->sync call via a fused adapter which
5287    /// results in that task executing code in a different instance, and it may
5288    /// call host functions and intrinsics from that other instance.
5289    instance: RuntimeInstance,
5290    /// If present, a pending `Event::None` or `Event::Cancelled` to be
5291    /// delivered to this task.
5292    event: Option<Event>,
5293    /// Whether or not the task has exited.
5294    exited: bool,
5295    /// Threads belonging to this task
5296    threads: HashSet<TableId<GuestThread>>,
5297    /// The state of the host future that represents an async task, which must
5298    /// be dropped before we can delete the task.
5299    host_future_state: HostFutureState,
5300    /// Indicates whether this task was created for a call to an async-typed
5301    /// export.
5302    async_typed: bool,
5303    /// Indicates whether this task was created for a call to an async-lifted
5304    /// export.
5305    async_lifted: bool,
5306
5307    decremented_interesting_task_count: bool,
5308
5309    group: TaskGroupId,
5310}
5311
5312impl GuestTask {
5313    fn already_lowered_parameters(&self) -> bool {
5314        // We reset `lower_params` after we lower the parameters
5315        self.lower_params.is_none()
5316    }
5317
5318    fn returned_or_cancelled(&self) -> bool {
5319        // We reset `lift_result` after we return or exit
5320        self.lift_result.is_none()
5321    }
5322
5323    fn ready_to_delete(&self) -> bool {
5324        let threads_completed = self.threads.is_empty();
5325        let has_sync_result = matches!(self.sync_result, SyncResult::Produced(_));
5326        let pending_completion_event = matches!(
5327            self.common.event,
5328            Some(Event::Subtask {
5329                status: Status::Returned | Status::ReturnCancelled
5330            })
5331        );
5332        let ready = threads_completed
5333            && !has_sync_result
5334            && !pending_completion_event
5335            && !matches!(self.host_future_state, HostFutureState::Live);
5336        log::trace!(
5337            "ready to delete? {ready} (threads_completed: {}, has_sync_result: {}, pending_completion_event: {}, host_future_state: {:?})",
5338            threads_completed,
5339            has_sync_result,
5340            pending_completion_event,
5341            self.host_future_state
5342        );
5343        ready
5344    }
5345
5346    fn new(
5347        state: &mut ConcurrentState,
5348        lower_params: RawLower,
5349        lift_result: LiftResult,
5350        caller: Caller,
5351        callback: Option<CallbackFn>,
5352        instance: RuntimeInstance,
5353        async_typed: bool,
5354        async_lifted: bool,
5355    ) -> Result<QualifiedThreadId> {
5356        let host_future_state = match &caller {
5357            Caller::Guest { .. } => HostFutureState::NotApplicable,
5358            Caller::Host {
5359                host_future_present,
5360                ..
5361            } => {
5362                if *host_future_present {
5363                    HostFutureState::Live
5364                } else {
5365                    HostFutureState::NotApplicable
5366                }
5367            }
5368        };
5369
5370        let group = match caller {
5371            Caller::Guest { thread } => {
5372                let group = state.get_mut(thread.task)?.group;
5373                state.increment_group_ref_count(group)?;
5374                group
5375            }
5376            Caller::Host { .. } => state.make_task_group()?,
5377        };
5378
5379        let task = state.push(Self {
5380            common: WaitableCommon::default(),
5381            lower_params: Some(lower_params),
5382            lift_result: Some(lift_result),
5383            result: None,
5384            callback,
5385            caller,
5386            call_context: CallContext::default(),
5387            sync_result: SyncResult::NotProduced,
5388            cancel_request_delivered: false,
5389            starting_sent: false,
5390            instance,
5391            event: None,
5392            exited: false,
5393            threads: HashSet::new(),
5394            host_future_state,
5395            async_typed,
5396            async_lifted,
5397            decremented_interesting_task_count: false,
5398            group,
5399        })?;
5400        let new_thread = GuestThread::new_implicit(state, task)?;
5401        let thread = state.push(new_thread)?;
5402        state.get_mut(task)?.threads.insert(thread);
5403        state.interesting_tasks += 1;
5404        let thread = QualifiedThreadId { task, thread };
5405        log::trace!("new implicit thread {thread:?} for instance {instance:?}");
5406        Ok(thread)
5407    }
5408}
5409
5410impl TableDebug for GuestTask {
5411    fn type_name() -> &'static str {
5412        "GuestTask"
5413    }
5414}
5415
5416/// Represents state common to all kinds of waitables.
5417#[derive(Default)]
5418struct WaitableCommon {
5419    /// The currently pending event for this waitable, if any.
5420    event: Option<Event>,
5421    /// The set to which this waitable belongs, if any.
5422    set: Option<TableId<WaitableSet>>,
5423    /// The handle with which the guest refers to this waitable, if any.
5424    handle: Option<u32>,
5425}
5426
5427/// Represents a Component Model Async `waitable`.
5428#[derive(Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
5429enum Waitable {
5430    /// A host task
5431    Host(TableId<HostTask>),
5432    /// A guest task
5433    Guest(TableId<GuestTask>),
5434    /// The read or write end of a stream or future
5435    Transmit(TableId<TransmitHandle>),
5436}
5437
5438impl Waitable {
5439    /// Retrieve the `Waitable` corresponding to the specified guest-visible
5440    /// handle.
5441    fn from_instance(
5442        state: Pin<&mut ComponentInstance>,
5443        caller_instance: RuntimeComponentInstanceIndex,
5444        waitable: u32,
5445    ) -> Result<Self> {
5446        use crate::runtime::vm::component::Waitable;
5447
5448        let (waitable, kind) = state.instance_states().0[caller_instance]
5449            .handle_table()
5450            .waitable_rep(waitable)?;
5451
5452        Ok(match kind {
5453            Waitable::Subtask { is_host: true } => Self::Host(TableId::new(waitable)),
5454            Waitable::Subtask { is_host: false } => Self::Guest(TableId::new(waitable)),
5455            Waitable::Stream | Waitable::Future => Self::Transmit(TableId::new(waitable)),
5456        })
5457    }
5458
5459    /// Retrieve the host-visible identifier for this `Waitable`.
5460    fn rep(&self) -> u32 {
5461        match self {
5462            Self::Host(id) => id.rep(),
5463            Self::Guest(id) => id.rep(),
5464            Self::Transmit(id) => id.rep(),
5465        }
5466    }
5467
5468    /// Move this `Waitable` to the specified set (when `set` is `Some(_)`) or
5469    /// remove it from any set it may currently belong to (when `set` is
5470    /// `None`).
5471    fn join(&self, state: &mut ConcurrentState, set: Option<TableId<WaitableSet>>) -> Result<()> {
5472        log::trace!("waitable {self:?} join set {set:?}");
5473
5474        let old = mem::replace(&mut self.common(state)?.set, set);
5475
5476        if let Some(old) = old {
5477            match *self {
5478                Waitable::Host(id) => state.remove_child(id, old),
5479                Waitable::Guest(id) => state.remove_child(id, old),
5480                Waitable::Transmit(id) => state.remove_child(id, old),
5481            }?;
5482
5483            state.get_mut(old)?.ready.remove(self);
5484        }
5485
5486        if let Some(set) = set {
5487            match *self {
5488                Waitable::Host(id) => state.add_child(id, set),
5489                Waitable::Guest(id) => state.add_child(id, set),
5490                Waitable::Transmit(id) => state.add_child(id, set),
5491            }?;
5492
5493            if self.common(state)?.event.is_some() {
5494                self.mark_ready(state)?;
5495            }
5496        }
5497
5498        Ok(())
5499    }
5500
5501    /// Retrieve mutable access to the `WaitableCommon` for this `Waitable`.
5502    fn common<'a>(&self, state: &'a mut ConcurrentState) -> Result<&'a mut WaitableCommon> {
5503        Ok(match self {
5504            Self::Host(id) => &mut state.get_mut(*id)?.common,
5505            Self::Guest(id) => &mut state.get_mut(*id)?.common,
5506            Self::Transmit(id) => &mut state.get_mut(*id)?.common,
5507        })
5508    }
5509
5510    /// Trap if this waitable is currently a member of a waitable set.
5511    ///
5512    /// A synchronous stream/future/subtask operation may end up blocking on
5513    /// this waitable, so it is not allowed to run while the waitable is also
5514    /// being watched by a waitable set.
5515    fn trap_if_in_waitable_set(&self, state: &mut ConcurrentState) -> Result<()> {
5516        if self.common(state)?.set.is_some() {
5517            bail!(Trap::WaitableSyncAndAsync);
5518        }
5519        Ok(())
5520    }
5521
5522    /// Set or clear the pending event for this waitable and either deliver it
5523    /// to the first waiter, if any, or mark it as ready to be delivered to the
5524    /// next waiter that arrives.
5525    fn set_event(&self, state: &mut ConcurrentState, event: Option<Event>) -> Result<()> {
5526        log::trace!("set event for {self:?}: {event:?}");
5527        self.common(state)?.event = event;
5528        self.mark_ready(state)
5529    }
5530
5531    /// Take the pending event from this waitable, leaving `None` in its place.
5532    fn take_event(&self, state: &mut ConcurrentState) -> Result<Option<Event>> {
5533        let common = self.common(state)?;
5534        let event = common.event.take();
5535        if let Some(set) = self.common(state)?.set {
5536            state.get_mut(set)?.ready.remove(self);
5537        }
5538
5539        Ok(event)
5540    }
5541
5542    /// Deliver the current event for this waitable to the first waiter, if any,
5543    /// or else mark it as ready to be delivered to the next waiter that
5544    /// arrives.
5545    fn mark_ready(&self, state: &mut ConcurrentState) -> Result<()> {
5546        if let Some(set) = self.common(state)?.set {
5547            let set_state = state.get_mut(set)?;
5548            set_state.ready.insert(*self);
5549
5550            if let Some((thread, mode)) = set_state.waiting.pop_first() {
5551                let wake_on_cancel = state.get_mut(thread.thread)?.wake_on_cancel.take();
5552                assert!(wake_on_cancel.is_none() || wake_on_cancel == WakeOnCancel::Waiting(set));
5553
5554                let item = match mode {
5555                    WaitMode::Fiber(fiber) => Some(WorkItem::ResumeFiber {
5556                        instance: state.get_mut(thread.task)?.instance,
5557                        thread,
5558                        fiber,
5559                    }),
5560                    WaitMode::Callback(instance) => Some(WorkItem::GuestCall {
5561                        instance: state.get_mut(thread.task)?.instance,
5562                        call: GuestCall {
5563                            thread,
5564                            kind: GuestCallKind::DeliverEvent {
5565                                instance,
5566                                set: Some(set),
5567                            },
5568                        },
5569                    }),
5570                };
5571
5572                if let Some(item) = item {
5573                    state.push_high_priority(item);
5574                }
5575            }
5576        }
5577        Ok(())
5578    }
5579
5580    /// Remove this waitable from the store's rep table.
5581    fn delete_from(&self, store: &mut StoreOpaque) -> Result<()> {
5582        match self {
5583            Self::Host(task) => {
5584                log::trace!("delete host task {task:?}");
5585                let state = store.concurrent_state_mut()?;
5586                let task = state.delete(*task)?;
5587
5588                state.decrement_group_ref_count(task.group)?;
5589            }
5590            Self::Guest(task) => {
5591                log::trace!("delete guest task {task:?}");
5592                let state = store.concurrent_state_mut()?;
5593                let task = state.delete(*task)?;
5594
5595                state.decrement_group_ref_count(task.group)?;
5596
5597                // When a guest task is created it increments the
5598                // `ConcurrentState::interesting_tasks` counter, and that needs
5599                // to be paired with a decrement. There are a few situations in
5600                // which the decrement needs to happen which don't all funnel
5601                // through here, so in lieu of that at least try to catch issues
5602                // where we forgot to do a decrement.
5603                debug_assert!(task.decremented_interesting_task_count);
5604            }
5605            Self::Transmit(task) => {
5606                store.concurrent_state_mut()?.delete(*task)?;
5607            }
5608        }
5609
5610        Ok(())
5611    }
5612}
5613
5614impl fmt::Debug for Waitable {
5615    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
5616        match self {
5617            Self::Host(id) => write!(f, "{id:?}"),
5618            Self::Guest(id) => write!(f, "{id:?}"),
5619            Self::Transmit(id) => write!(f, "{id:?}"),
5620        }
5621    }
5622}
5623
5624/// Represents a Component Model Async `waitable-set`.
5625#[derive(Default)]
5626struct WaitableSet {
5627    /// Which waitables in this set have pending events, if any.
5628    ready: BTreeSet<Waitable>,
5629    /// Which guest threads are currently waiting on this set, if any.
5630    waiting: BTreeMap<QualifiedThreadId, WaitMode>,
5631    /// Whether this set is a synthetic, internal one meant for handling
5632    /// synchronous calls.
5633    is_sync_call_set: bool,
5634}
5635
5636impl TableDebug for WaitableSet {
5637    fn type_name() -> &'static str {
5638        "WaitableSet"
5639    }
5640}
5641
5642/// Type-erased closure to lower the parameters for a guest task.
5643type RawLower =
5644    Box<dyn FnOnce(&mut dyn VMStore, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync>;
5645
5646/// Type-erased closure to lift the result for a guest task.
5647type RawLift = Box<
5648    dyn FnOnce(&mut dyn VMStore, &[ValRaw]) -> Result<Box<dyn Any + Send + Sync>> + Send + Sync,
5649>;
5650
5651/// Type erased result of a guest task which may be downcast to the expected
5652/// type by a host caller (or simply ignored in the case of a guest caller; see
5653/// `DummyResult`).
5654type LiftedResult = Box<dyn Any + Send + Sync>;
5655
5656/// Used to return a result from a `LiftFn` when the actual result has already
5657/// been lowered to a guest task's stack and linear memory.
5658struct DummyResult;
5659
5660/// Represents the Component Model Async state of a (sub-)component instance.
5661#[derive(Default)]
5662pub struct ConcurrentInstanceState {
5663    /// Whether backpressure is set for this instance (enabled if >0)
5664    backpressure: u16,
5665    /// Whether this instance can be entered
5666    do_not_enter: bool,
5667    /// Whether this instance may suspend (i.e. whether this instance is
5668    /// currently running a sync-typed function).
5669    do_not_suspend: bool,
5670    /// Pending calls for this instance which require `Self::backpressure` to be
5671    /// zero and/or `Self::do_not_enter` to be false before they can proceed.
5672    pending: BTreeMap<QualifiedThreadId, GuestCallKind>,
5673}
5674
5675impl ConcurrentInstanceState {
5676    pub fn pending_is_empty(&self) -> bool {
5677        self.pending.is_empty()
5678    }
5679}
5680
5681#[derive(Debug, Copy, Clone)]
5682pub(crate) enum CurrentThread {
5683    /// The currently running thread is a guest, identified here with its
5684    /// task/thread id combo.
5685    Guest(QualifiedThreadId),
5686    /// The currently running thread is a host task.
5687    Host(TableId<HostTask>),
5688    /// The currently running thread is a host call whose task has not yet been
5689    /// materialized. The contained ID identifies its guest caller.
5690    DeferredHost(QualifiedThreadId),
5691    /// There is no currently running thread because we are in the main event
5692    /// loop or concurrency is disabled.
5693    None,
5694}
5695
5696impl CurrentThread {
5697    fn guest(&self) -> Option<&QualifiedThreadId> {
5698        match self {
5699            Self::Guest(id) => Some(id),
5700            _ => None,
5701        }
5702    }
5703
5704    fn guest_task(&self) -> Option<TableId<GuestTask>> {
5705        match self {
5706            Self::Guest(id) => Some(id.task),
5707            _ => None,
5708        }
5709    }
5710
5711    fn is_none(&self) -> bool {
5712        matches!(self, Self::None)
5713    }
5714}
5715
5716impl From<QualifiedThreadId> for CurrentThread {
5717    fn from(id: QualifiedThreadId) -> Self {
5718        Self::Guest(id)
5719    }
5720}
5721
5722impl From<TableId<HostTask>> for CurrentThread {
5723    fn from(id: TableId<HostTask>) -> Self {
5724        Self::Host(id)
5725    }
5726}
5727
5728enum Priority {
5729    Switch,
5730    High,
5731    Low,
5732}
5733
5734/// Represents the Component Model Async state of a store.
5735pub struct ConcurrentState {
5736    /// The currently running thread, if any.
5737    ///
5738    /// Note that we lazily materialize threads on-demand and this field is not
5739    /// necessarily up-to-date. The `StoreOpaque::current_thread` method should
5740    /// be preferred over directly accessing this field.
5741    unforced_current_thread: CurrentThread,
5742
5743    /// Borrow state for the deferred host call, if any.
5744    ///
5745    /// This is `Some` if and only if [`Self::unforced_current_thread`] is
5746    /// [`CurrentThread::DeferredHost`]. Materializing the host task moves this
5747    /// context into that task.
5748    deferred_host_call_context: Option<CallContext>,
5749
5750    /// The set of pending host and background tasks, if any.
5751    ///
5752    /// See `ComponentInstance::poll_until` for where we temporarily take this
5753    /// out, poll it, then put it back to avoid any mutable aliasing hazards.
5754    futures: AlwaysMut<Option<FuturesUnordered<HostTaskFuture>>>,
5755    /// The table of waitables, waitable sets, etc.
5756    table: AlwaysMut<ResourceTable>,
5757    /// The next item to switch to if any.
5758    ///
5759    /// This takes precedence over items in the `high_priority` queue below and
5760    /// should be used in cases such as subtask calls and thread resume/promote
5761    /// operations where we must switch to a specific thread at the next turn of
5762    /// the event loop regardless of what happens to be present in the
5763    /// `high_priority` queue.
5764    switch_item: Option<WorkItem>,
5765    /// The item to set `switch_item` to when the current thread suspends.
5766    ///
5767    /// This is used when ever an async-lowered import or `subtask.cancel` is
5768    /// called in order to track the thread to switch back to once the current
5769    /// subtask suspends, if any.
5770    next_switch_item: Option<WorkItem>,
5771    /// The "high priority" work queue for this store's event loop.
5772    high_priority: VecDeque<WorkItem>,
5773    /// The "low priority" work queue for this store's event loop.
5774    low_priority: VecDeque<WorkItem>,
5775    /// A place to stash the reason a fiber is suspending so that the code which
5776    /// resumed it will know under what conditions the fiber should be resumed
5777    /// again.
5778    suspend_reason: Option<SuspendReason>,
5779    /// A cached fiber which is waiting for work to do.
5780    ///
5781    /// This helps us avoid creating a new fiber for each `GuestCall` work item.
5782    worker: Option<StoreFiber<'static>>,
5783    /// A place to stash the work item for which we're resuming a worker fiber.
5784    worker_item: Option<WorkerItem>,
5785
5786    /// Reference counts for all component error contexts
5787    ///
5788    /// NOTE: it is possible the global ref count to be *greater* than the sum of
5789    /// (sub)component ref counts as tracked by `error_context_tables`, for
5790    /// example when the host holds one or more references to error contexts.
5791    ///
5792    /// The key of this primary map is often referred to as the "rep" (i.e. host-side
5793    /// component-wide representation) of the index into concurrent state for a given
5794    /// stored `ErrorContext`.
5795    ///
5796    /// Stated another way, `TypeComponentGlobalErrorContextTableIndex` is essentially the same
5797    /// as a `TableId<ErrorContextState>`.
5798    global_error_context_ref_counts:
5799        BTreeMap<TypeComponentGlobalErrorContextTableIndex, GlobalErrorContextRefCount>,
5800
5801    /// The number of "interesting tasks" currently executing in the store.
5802    ///
5803    /// This tracks the concept of a component instance lifetime as defined in
5804    /// https://github.com/WebAssembly/component-model/pull/643. Specifically
5805    /// all tasks currently increment this counter which then gets decremented
5806    /// when they exit. In the future some tasks might not increment this
5807    /// counter, but for now all do.
5808    ///
5809    /// This is used to implement `Accessor::poll_no_interesting_tasks` to
5810    /// inform the embedder when all tasks have completed. This is then
5811    /// used in wasmtime-wasi-http, for example, to know when an instance is
5812    /// idle.
5813    interesting_tasks: usize,
5814
5815    /// Single waker to notify when `interesting_tasks` reaches 0.
5816    ///
5817    /// Used in the implementation of `Accessor::poll_no_interesting_tasks`.
5818    interesting_tasks_empty_waker: Option<Waker>,
5819
5820    /// Single waker to notify when a component instance goes from
5821    /// not-concurrently-callable to concurrently-callable.
5822    ///
5823    /// Used in the implementation of `Accessor::poll_ready_for_concurrent_call`.
5824    ready_for_concurrent_call_waker: Option<Waker>,
5825
5826    /// Whether the `StoreContextMut::poll_until` event loop is running.
5827    event_loop_running: bool,
5828
5829    /// See [TaskGroupHook].
5830    #[cfg(feature = "task-group-hook")]
5831    task_group_hook: Option<Box<dyn TaskGroupHook>>,
5832}
5833
5834impl Default for ConcurrentState {
5835    fn default() -> Self {
5836        Self {
5837            unforced_current_thread: CurrentThread::None,
5838            deferred_host_call_context: None,
5839            table: AlwaysMut::new(ResourceTable::new()),
5840            futures: AlwaysMut::new(Some(FuturesUnordered::new())),
5841            switch_item: None,
5842            next_switch_item: None,
5843            high_priority: VecDeque::new(),
5844            low_priority: VecDeque::new(),
5845            suspend_reason: None,
5846            worker: None,
5847            worker_item: None,
5848            global_error_context_ref_counts: BTreeMap::new(),
5849            interesting_tasks: 0,
5850            interesting_tasks_empty_waker: None,
5851            ready_for_concurrent_call_waker: None,
5852            event_loop_running: false,
5853            #[cfg(feature = "task-group-hook")]
5854            task_group_hook: None,
5855        }
5856    }
5857}
5858
5859impl ConcurrentState {
5860    /// Take ownership of any fibers and futures owned by this object.
5861    ///
5862    /// This should be used when disposing of the `Store` containing this object
5863    /// in order to gracefully resolve any and all fibers using
5864    /// `StoreFiber::dispose`.  This is necessary to avoid possible
5865    /// use-after-free bugs due to fibers which may still have access to the
5866    /// `Store`.
5867    ///
5868    /// Additionally, the futures collected with this function should be dropped
5869    /// within a `tls::set` call, which will ensure than any futures closing
5870    /// over an `&Accessor` will have access to the store when dropped, allowing
5871    /// e.g. `WithAccessor[AndValue]` instances to be disposed of without
5872    /// panicking.
5873    ///
5874    /// Note that this will leave the object in an inconsistent and unusable
5875    /// state, so it should only be used just prior to dropping it.
5876    pub(crate) fn take_fibers_and_futures(
5877        &mut self,
5878        fibers: &mut Vec<StoreFiber<'static>>,
5879        futures: &mut Vec<FuturesUnordered<HostTaskFuture>>,
5880    ) {
5881        let mut items = Vec::new();
5882        for (_, entry) in self.table.get_mut().iter_mut() {
5883            if let Some(set) = entry.downcast_mut::<WaitableSet>() {
5884                for mode in mem::take(&mut set.waiting).into_values() {
5885                    match mode {
5886                        WaitMode::Fiber(fiber) => {
5887                            fibers.push(fiber);
5888                        }
5889                        WaitMode::Callback(_) => {}
5890                    }
5891                }
5892            } else if let Some(thread) = entry.downcast_mut::<GuestThread>() {
5893                if let GuestThreadState::Suspended(fiber) | GuestThreadState::Ready { fiber, .. } =
5894                    mem::replace(&mut thread.state, GuestThreadState::Completed)
5895                {
5896                    fibers.push(fiber);
5897                }
5898            } else if let Some(item) = entry.downcast_mut::<Option<WorkItem>>() {
5899                if let Some(item) = item.take() {
5900                    items.push(item);
5901                }
5902            }
5903        }
5904
5905        if let Some(fiber) = self.worker.take() {
5906            fibers.push(fiber);
5907        }
5908
5909        let mut handle_item = |item| match item {
5910            WorkItem::ResumeFiber { fiber, .. } => {
5911                fibers.push(fiber);
5912            }
5913            WorkItem::PushFuture(future) => {
5914                self.futures
5915                    .get_mut()
5916                    .as_mut()
5917                    .unwrap()
5918                    .push(future.into_inner());
5919            }
5920            WorkItem::ResumeThread { .. }
5921            | WorkItem::GuestCall { .. }
5922            | WorkItem::WorkerFunction(_) => {}
5923        };
5924
5925        for item in items {
5926            handle_item(item);
5927        }
5928        if let Some(item) = self.switch_item.take() {
5929            handle_item(item);
5930        }
5931        if let Some(item) = self.next_switch_item.take() {
5932            handle_item(item);
5933        }
5934        for item in mem::take(&mut self.high_priority) {
5935            handle_item(item);
5936        }
5937        for item in mem::take(&mut self.low_priority) {
5938            handle_item(item);
5939        }
5940
5941        if let Some(them) = self.futures.get_mut().take() {
5942            futures.push(them);
5943        }
5944    }
5945
5946    #[cfg(feature = "gc")]
5947    pub(crate) fn trace_fiber_roots(
5948        &mut self,
5949        modules: &ModuleRegistry,
5950        unwind: &dyn Unwind,
5951        gc_roots_list: &mut GcRootsList,
5952    ) {
5953        let ConcurrentState {
5954            table,
5955            worker,
5956            switch_item,
5957            next_switch_item,
5958            high_priority,
5959            low_priority,
5960
5961            // TODO(cm-gc): This field contains `ValRaw`s, but they are never GC
5962            // references because the component model doesn't support GC yet. We
5963            // will need to trace these somehow when it does.
5964            futures: _,
5965
5966            // These fields do not contain GC references.
5967            worker_item: _,
5968            unforced_current_thread: _,
5969            deferred_host_call_context: _,
5970            suspend_reason: _,
5971            global_error_context_ref_counts: _,
5972            interesting_tasks: _,
5973            interesting_tasks_empty_waker: _,
5974            ready_for_concurrent_call_waker: _,
5975            event_loop_running: _,
5976            #[cfg(feature = "task-group-hook")]
5977                task_group_hook: _,
5978        } = self;
5979
5980        for (_, entry) in table.get_mut().iter_mut() {
5981            if let Some(set) = entry.downcast_mut::<WaitableSet>() {
5982                for mode in set.waiting.values_mut() {
5983                    match mode {
5984                        WaitMode::Fiber(fiber) => {
5985                            fiber.trace_gc_roots(modules, unwind, gc_roots_list);
5986                        }
5987                        WaitMode::Callback(_) => {}
5988                    }
5989                }
5990            } else if let Some(thread) = entry.downcast_mut::<GuestThread>() {
5991                if let GuestThreadState::Suspended(fiber) | GuestThreadState::Ready { fiber, .. } =
5992                    &mut thread.state
5993                {
5994                    fiber.trace_gc_roots(modules, unwind, gc_roots_list);
5995                }
5996            } else if let Some(Some(WorkItem::ResumeFiber { fiber, .. })) =
5997                entry.downcast_mut::<Option<WorkItem>>()
5998            {
5999                fiber.trace_gc_roots(modules, unwind, gc_roots_list);
6000            }
6001        }
6002
6003        if let Some(fiber) = worker {
6004            fiber.trace_gc_roots(modules, unwind, gc_roots_list);
6005        }
6006
6007        let mut handle_item = |item: &mut WorkItem| match item {
6008            WorkItem::ResumeFiber { fiber, .. } => {
6009                fiber.trace_gc_roots(modules, unwind, gc_roots_list);
6010            }
6011            WorkItem::PushFuture(_future) => {
6012                // TODO(cm-gc): once futures can contain GC roots, we will need
6013                // to trace them.
6014            }
6015            WorkItem::ResumeThread { .. }
6016            | WorkItem::GuestCall { .. }
6017            | WorkItem::WorkerFunction(_) => {}
6018        };
6019
6020        if let Some(item) = switch_item {
6021            handle_item(item);
6022        }
6023        if let Some(item) = next_switch_item {
6024            handle_item(item);
6025        }
6026        for item in high_priority {
6027            handle_item(item);
6028        }
6029        for item in low_priority {
6030            handle_item(item);
6031        }
6032    }
6033
6034    fn push<V: Send + Sync + 'static>(
6035        &mut self,
6036        value: V,
6037    ) -> Result<TableId<V>, ResourceTableError> {
6038        self.table.get_mut().push(value).map(TableId::from)
6039    }
6040
6041    fn get_mut<V: 'static>(&mut self, id: TableId<V>) -> Result<&mut V, ResourceTableError> {
6042        self.table.get_mut().get_mut(&Resource::from(id))
6043    }
6044
6045    pub fn add_child<T: 'static, U: 'static>(
6046        &mut self,
6047        child: TableId<T>,
6048        parent: TableId<U>,
6049    ) -> Result<(), ResourceTableError> {
6050        self.table
6051            .get_mut()
6052            .add_child(Resource::from(child), Resource::from(parent))
6053    }
6054
6055    pub fn remove_child<T: 'static, U: 'static>(
6056        &mut self,
6057        child: TableId<T>,
6058        parent: TableId<U>,
6059    ) -> Result<(), ResourceTableError> {
6060        self.table
6061            .get_mut()
6062            .remove_child(Resource::from(child), Resource::from(parent))
6063    }
6064
6065    fn delete<V: 'static>(&mut self, id: TableId<V>) -> Result<V, ResourceTableError> {
6066        self.table.get_mut().delete(Resource::from(id))
6067    }
6068
6069    fn push_future(&mut self, future: HostTaskFuture) {
6070        // Note that we can't directly push to `ConcurrentState::futures` here
6071        // since this may be called from a future that's being polled inside
6072        // `Self::poll_until`, which temporarily removes the `FuturesUnordered`
6073        // so it has exclusive access while polling it.  Therefore, we push a
6074        // work item to the "high priority" queue, which will actually push to
6075        // `ConcurrentState::futures` later.
6076        self.push_high_priority(WorkItem::PushFuture(AlwaysMut::new(future)));
6077    }
6078
6079    fn set_switch_item(&mut self, item: WorkItem) -> Result<()> {
6080        log::trace!("set switch item: {item:?}");
6081
6082        if self.switch_item.is_some() {
6083            bail_bug!("switch item already set");
6084        }
6085
6086        self.switch_item = Some(item);
6087
6088        Ok(())
6089    }
6090
6091    fn take_next_switch_item(&mut self) -> Result<()> {
6092        if let Some(item) = self.next_switch_item.take() {
6093            self.set_switch_item(item)?;
6094        }
6095        Ok(())
6096    }
6097
6098    fn push_high_priority(&mut self, item: WorkItem) {
6099        log::trace!("push high priority: {item:?}");
6100        self.high_priority.push_front(item);
6101    }
6102
6103    fn push_low_priority(&mut self, item: WorkItem) {
6104        log::trace!("push low priority: {item:?}");
6105        self.low_priority.push_front(item);
6106    }
6107
6108    fn push_work_item(&mut self, item: WorkItem, priority: Priority) -> Result<()> {
6109        match priority {
6110            Priority::Switch => self.set_switch_item(item)?,
6111            Priority::High => self.push_high_priority(item),
6112            Priority::Low => self.push_low_priority(item),
6113        }
6114
6115        Ok(())
6116    }
6117
6118    fn promote_instance_local_thread_work_item(
6119        &mut self,
6120        current_instance: RuntimeInstance,
6121    ) -> Result<bool> {
6122        log::trace!("promote thread work items for {current_instance:?}");
6123
6124        self.promote_work_item_matching(|item: &WorkItem| {
6125            let result = match item {
6126                WorkItem::ResumeThread { instance, .. }
6127                | WorkItem::ResumeFiber { instance, .. }
6128                | WorkItem::GuestCall { instance, .. } => *instance == current_instance,
6129                _ => false,
6130            };
6131
6132            log::trace!("candidate {item:?}: {result}");
6133            result
6134        })
6135    }
6136
6137    fn promote_thread_work_item(&mut self, thread: QualifiedThreadId) -> Result<bool> {
6138        self.promote_work_item_matching(|item: &WorkItem| match item {
6139            WorkItem::ResumeThread {
6140                thread: item_thread,
6141                ..
6142            }
6143            | WorkItem::GuestCall {
6144                call:
6145                    GuestCall {
6146                        thread: item_thread,
6147                        ..
6148                    },
6149                ..
6150            } => *item_thread == thread,
6151            _ => false,
6152        })
6153    }
6154
6155    fn promote_work_item_matching<F>(&mut self, mut predicate: F) -> Result<bool>
6156    where
6157        F: FnMut(&WorkItem) -> bool,
6158    {
6159        // Note the use of `.rev()` below to preserve ordering given that items
6160        // are popped from the back of the `VecDeque`s by `poll_until` and
6161        // pushed to the front by `push_{high,low}_priority`.
6162
6163        for item in mem::take(&mut self.high_priority).into_iter().rev() {
6164            if self.switch_item.is_none() && predicate(&item) {
6165                self.set_switch_item(item)?;
6166            } else {
6167                self.push_high_priority(item);
6168            }
6169        }
6170
6171        if self.switch_item.is_none() {
6172            for item in mem::take(&mut self.low_priority).into_iter().rev() {
6173                if self.switch_item.is_none() && predicate(&item) {
6174                    self.set_switch_item(item)?;
6175                } else {
6176                    self.push_low_priority(item);
6177                }
6178            }
6179        }
6180
6181        Ok(self.switch_item.is_some())
6182    }
6183
6184    /// Used by `ResourceTables` to acquire the current `CallContext` for the
6185    /// specified task.
6186    pub fn call_context(&mut self, task: Scope) -> Result<&mut CallContext> {
6187        match task {
6188            Scope::HostId(task) => {
6189                let task: TableId<HostTask> = TableId::new(task);
6190                Ok(&mut self.get_mut(task)?.call_context)
6191            }
6192            Scope::Id(task) => {
6193                let task: TableId<GuestTask> = TableId::new(task);
6194                Ok(&mut self.get_mut(task)?.call_context)
6195            }
6196        }
6197    }
6198
6199    pub(crate) fn deferred_host_call_context(&mut self) -> Option<&mut CallContext> {
6200        self.deferred_host_call_context.as_mut()
6201    }
6202
6203    fn futures_mut(&mut self) -> Result<&mut FuturesUnordered<HostTaskFuture>> {
6204        match self.futures.get_mut().as_mut() {
6205            Some(f) => Ok(f),
6206            None => bail_bug!("futures field of concurrent state is currently taken"),
6207        }
6208    }
6209
6210    pub(crate) fn table(&mut self) -> &mut ResourceTable {
6211        self.table.get_mut()
6212    }
6213
6214    fn debug_assert_deferred_host_invariant(&self) {
6215        debug_assert_eq!(
6216            self.deferred_host_call_context.is_some(),
6217            matches!(self.unforced_current_thread, CurrentThread::DeferredHost(_)),
6218            "a deferred host thread and call context must exist together",
6219        );
6220    }
6221
6222    fn materialize_host_task(&mut self) -> Result<CurrentThread> {
6223        self.debug_assert_deferred_host_invariant();
6224        let caller = match self.unforced_current_thread {
6225            CurrentThread::DeferredHost(caller) => caller,
6226            thread => return Ok(thread),
6227        };
6228
6229        // Push first so allocation failure leaves the deferred state intact.
6230        let task = HostTask::new(self, HostTaskState::CalleeStarted, caller)?;
6231        let task = self.push(task)?;
6232        let call_context = self
6233            .deferred_host_call_context
6234            .take()
6235            .expect("deferred host call context should be present");
6236        self.get_mut(task)
6237            .expect("newly inserted host task should be present")
6238            .call_context = call_context;
6239        self.unforced_current_thread = CurrentThread::Host(task);
6240        self.debug_assert_deferred_host_invariant();
6241        log::trace!("new host task materialized {task:?}");
6242        Ok(CurrentThread::Host(task))
6243    }
6244
6245    fn materialize_current_host_task_id(&mut self) -> Result<Option<TableId<HostTask>>> {
6246        match self.materialize_host_task()? {
6247            CurrentThread::Host(id) => Ok(Some(id)),
6248            CurrentThread::None => Ok(None),
6249            CurrentThread::Guest(_) => {
6250                bail_bug!("tried to materialize a host task id from a guest thread")
6251            }
6252            CurrentThread::DeferredHost(_) => {
6253                bail_bug!(
6254                    "current thread is a deferred host thread which should have been materialized"
6255                )
6256            }
6257        }
6258    }
6259
6260    pub(crate) fn materialize_current_scope(&mut self) -> Result<Scope> {
6261        match self.materialize_host_task()? {
6262            CurrentThread::Host(id) => Ok(Scope::HostId(id.rep())),
6263            _ => bail_bug!("current scope is not a deferred host scope"),
6264        }
6265    }
6266}
6267
6268/// Provide a type hint to compiler about the shape of a parameter lower
6269/// closure.
6270fn for_any_lower<
6271    F: FnOnce(&mut dyn VMStore, &mut [MaybeUninit<ValRaw>]) -> Result<()> + Send + Sync,
6272>(
6273    fun: F,
6274) -> F {
6275    fun
6276}
6277
6278/// Provide a type hint to compiler about the shape of a result lift closure.
6279fn for_any_lift<
6280    F: FnOnce(&mut dyn VMStore, &[ValRaw]) -> Result<Box<dyn Any + Send + Sync>> + Send + Sync,
6281>(
6282    fun: F,
6283) -> F {
6284    fun
6285}
6286
6287fn check_ambient_store(id: StoreId) {
6288    let message = "\
6289        `Future`s which depend on asynchronous component tasks, streams, or \
6290        futures to complete may only be polled from the event loop of the \
6291        store to which they belong.  Please use \
6292        `StoreContextMut::{run_concurrent,spawn}` to poll or await them.\
6293    ";
6294    tls::try_get(|store| {
6295        let matched = match store {
6296            tls::TryGet::Some(store) => store.id() == id,
6297            tls::TryGet::Taken | tls::TryGet::None => false,
6298        };
6299
6300        if !matched {
6301            panic!("{message}")
6302        }
6303    });
6304}
6305
6306fn unpack_callback_code(code: u32) -> (u32, u32) {
6307    (code & 0xF, code >> 4)
6308}
6309
6310/// Helper struct for packaging parameters to be passed to
6311/// `ComponentInstance::waitable_check` for calls to `waitable-set.wait` or
6312/// `waitable-set.poll`.
6313struct WaitableCheckParams {
6314    set: TableId<WaitableSet>,
6315    options: OptionsIndex,
6316    payload: u32,
6317}
6318
6319/// Indicates whether `ComponentInstance::waitable_check` is being called for
6320/// `waitable-set.wait` or `waitable-set.poll`.
6321enum WaitableCheck {
6322    Wait,
6323    Poll,
6324}
6325
6326/// Represents a guest task called from the host, prepared using `prepare_call`.
6327pub(crate) struct PreparedCall<R> {
6328    /// The guest export to be called
6329    handle: Func,
6330    /// The guest thread created by `prepare_call`
6331    thread: QualifiedThreadId,
6332    /// The number of lowered core Wasm parameters to pass to the call.
6333    param_count: usize,
6334    /// The `oneshot::Receiver` to which the result of the call will be
6335    /// delivered when it is available.
6336    rx: oneshot::Receiver<LiftedResult>,
6337    /// The instance that this call is prepared for.
6338    runtime_instance: RuntimeInstance,
6339    _phantom: PhantomData<R>,
6340}
6341
6342impl<R> PreparedCall<R> {
6343    /// Get a copy of the `TaskId` for this `PreparedCall`.
6344    pub(crate) fn task_id(&self) -> TaskId {
6345        TaskId {
6346            task: self.thread.task,
6347            runtime_instance: self.runtime_instance,
6348        }
6349    }
6350}
6351
6352/// Represents a task created by `prepare_call`.
6353pub(crate) struct TaskId {
6354    task: TableId<GuestTask>,
6355    runtime_instance: RuntimeInstance,
6356}
6357
6358impl TaskId {
6359    /// The host future for an async task was dropped. If the parameters have not been lowered yet,
6360    /// it is no longer valid to do so, as the lowering closure would see a dangling pointer. In this case,
6361    /// we delete the task eagerly. Otherwise, there may be running threads, or ones that are suspended
6362    /// and can be resumed by other tasks for this component, so we mark the future as dropped
6363    /// and delete the task when all threads are done.
6364    pub(crate) fn host_future_dropped(&self, store: &mut StoreOpaque) -> Result<()> {
6365        let task = store.concurrent_state_mut()?.get_mut(self.task)?;
6366        let delete = if !task.already_lowered_parameters() {
6367            store.cancel_guest_subtask_without_lowered_parameters(
6368                self.runtime_instance,
6369                self.task,
6370            )?;
6371            true
6372        } else {
6373            task.host_future_state = HostFutureState::Dropped;
6374            task.ready_to_delete()
6375        };
6376        if delete {
6377            Waitable::Guest(self.task).delete_from(store)?
6378        }
6379        Ok(())
6380    }
6381}
6382
6383/// Prepare a call to the specified exported Wasm function, providing functions
6384/// for lowering the parameters and lifting the result.
6385///
6386/// To enqueue the returned `PreparedCall` in the `ComponentInstance`'s event
6387/// loop, use `stage_call`.
6388pub(crate) fn prepare_call<T, R>(
6389    mut store: StoreContextMut<T>,
6390    handle: Func,
6391    param_count: usize,
6392    host_future_present: bool,
6393    lower_params: impl FnOnce(StoreContextMut<T>, &mut [MaybeUninit<ValRaw>]) -> Result<()>
6394    + Send
6395    + Sync
6396    + 'static,
6397    lift_result: impl FnOnce(&mut StoreOpaque, &[ValRaw]) -> Result<Box<dyn Any + Send + Sync>>
6398    + Send
6399    + Sync
6400    + 'static,
6401) -> Result<PreparedCall<R>> {
6402    if !store.0.may_enter() {
6403        bail!(Trap::CannotEnterComponent);
6404    }
6405
6406    let (options, _flags, ty, raw_options) = handle.abi_info(store.0);
6407
6408    let instance = handle.instance().id().get(store.0);
6409    let options = &instance.component().env_component().options[options];
6410    let ty = &instance.component().types()[ty];
6411    let async_typed = ty.async_;
6412    let async_lifted = raw_options.async_;
6413    let task_return_type = ty.results;
6414    let component_instance = raw_options.instance;
6415    let callback = options.callback.map(|i| instance.runtime_callback(i));
6416    let memory = options
6417        .memory()
6418        .map(|i| instance.runtime_memory(i))
6419        .map(SendSyncPtr::new);
6420    let string_encoding = options.string_encoding;
6421    let token = StoreToken::new(store.as_context_mut());
6422    let caller = store.0.materialize_host_task_id()?;
6423    let state = store.0.concurrent_state_mut()?;
6424
6425    let (tx, rx) = oneshot::channel();
6426
6427    let instance = handle.instance().runtime_instance(component_instance);
6428    let thread = GuestTask::new(
6429        state,
6430        Box::new(for_any_lower(move |store, params| {
6431            lower_params(token.as_context_mut(store), params)
6432        })),
6433        LiftResult {
6434            lift: Box::new(for_any_lift(move |store, result| {
6435                lift_result(store, result)
6436            })),
6437            ty: task_return_type,
6438            memory,
6439            string_encoding,
6440        },
6441        Caller::Host {
6442            tx: Some(tx),
6443            host_future_present,
6444            caller,
6445        },
6446        callback.map(|callback| {
6447            let callback = SendSyncPtr::new(callback);
6448            let instance = handle.instance();
6449            Box::new(move |store: &mut dyn VMStore, event, handle| {
6450                let store = token.as_context_mut(store);
6451                // SAFETY: Per the contract of `prepare_call`, the callback
6452                // will remain valid at least as long is this task exists.
6453                unsafe { instance.call_callback(store, callback, event, handle) }
6454            }) as CallbackFn
6455        }),
6456        instance,
6457        async_typed,
6458        async_lifted,
6459    )?;
6460
6461    Ok(PreparedCall {
6462        handle,
6463        thread,
6464        param_count,
6465        runtime_instance: instance,
6466        rx,
6467        _phantom: PhantomData,
6468    })
6469}
6470
6471pub(crate) struct StagedCall<R> {
6472    store: StoreId,
6473    rx: oneshot::Receiver<LiftedResult>,
6474    _marker: PhantomData<fn() -> R>,
6475    group: TaskGroupId,
6476}
6477
6478impl<R> StagedCall<R> {
6479    /// Queue a call previously prepared using `prepare_call` to be run as part of
6480    /// the associated `ComponentInstance`'s event loop.
6481    ///
6482    /// The returned future will resolve to the result once it is available, but
6483    /// must only be polled via the instance's event loop. See
6484    /// `StoreContextMut::run_concurrent` for details.
6485    pub(crate) fn new<T: 'static>(
6486        mut store: StoreContextMut<T>,
6487        prepared: PreparedCall<R>,
6488    ) -> Result<StagedCall<R>> {
6489        let PreparedCall {
6490            handle,
6491            thread,
6492            param_count,
6493            rx,
6494            ..
6495        } = prepared;
6496
6497        stage_call0(store.as_context_mut(), handle, thread, param_count)?;
6498
6499        Ok(StagedCall {
6500            store: store.0.id(),
6501            rx,
6502            _marker: PhantomData,
6503            group: store.0.concurrent_state_mut()?.get_mut(thread.task)?.group,
6504        })
6505    }
6506}
6507
6508impl<R> Future for StagedCall<R>
6509where
6510    R: 'static,
6511{
6512    type Output = Result<R>;
6513
6514    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
6515        check_ambient_store(self.store);
6516        Pin::new(&mut self.rx).poll(cx).map(|result| match result {
6517            Ok(r) => match r.downcast() {
6518                Ok(r) => Ok(*r),
6519                Err(_) => bail_bug!("wrong type of value produced"),
6520            },
6521            Err(oneshot::Canceled) => bail_bug!("channel erroneously dropped"),
6522        })
6523    }
6524}
6525
6526/// Queue a call previously prepared using `prepare_call` to be run as part of
6527/// the associated `ComponentInstance`'s event loop.
6528fn stage_call0<T: 'static>(
6529    store: StoreContextMut<T>,
6530    handle: Func,
6531    guest_thread: QualifiedThreadId,
6532    param_count: usize,
6533) -> Result<()> {
6534    let (_options, _, _ty, raw_options) = handle.abi_info(store.0);
6535    let is_concurrent = raw_options.async_;
6536    let callback = raw_options.callback;
6537    let instance = handle.instance();
6538    let callee = handle.lifted_core_func(store.0);
6539    let post_return = raw_options
6540        .post_return
6541        .map(|i| instance.id().get(store.0).runtime_post_return(i));
6542    let callback = callback.map(|i| {
6543        let instance = instance.id().get(store.0);
6544        SendSyncPtr::new(instance.runtime_callback(i))
6545    });
6546
6547    log::trace!("queueing call {guest_thread:?}");
6548
6549    // SAFETY: `callee`, `callback`, and `post_return` are valid pointers
6550    // (with signatures appropriate for this call) and will remain valid as
6551    // long as this instance is valid.
6552    unsafe {
6553        instance.stage_call(
6554            store,
6555            guest_thread,
6556            SendSyncPtr::new(callee),
6557            param_count,
6558            1,
6559            is_concurrent,
6560            callback,
6561            post_return.map(SendSyncPtr::new),
6562            true,
6563        )
6564    }
6565}
6566
6567#[cfg(all(test, feature = "cranelift", feature = "wat"))]
6568mod tests {
6569    use super::*;
6570    use crate::component::{Component, Linker};
6571    use crate::store::AsStoreOpaque;
6572    use crate::{Config, Engine};
6573
6574    fn host_subtask(
6575        state: HostTaskState,
6576        event: Option<Event>,
6577    ) -> Result<(Store<()>, Instance, TableId<HostTask>, u32)> {
6578        let mut config = Config::new();
6579        config.wasm_component_model_async(true);
6580        let engine = Engine::new(&config)?;
6581        let component = Component::new(&engine, "(component)")?;
6582        let mut store = Store::new(&engine, ());
6583        let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
6584        let store_opaque = store.as_store_opaque();
6585        let concurrent_state = store_opaque.concurrent_state_mut()?;
6586        // These tests create an isolated host task without a guest caller, so
6587        // give it its own task group instead of inheriting the caller's group.
6588        let group = concurrent_state.make_task_group()?;
6589        let task = concurrent_state.push(HostTask {
6590            common: WaitableCommon::default(),
6591            call_context: CallContext::default(),
6592            state,
6593            group,
6594        })?;
6595        let handle = store_opaque
6596            .instance_state(instance.runtime_instance(RuntimeComponentInstanceIndex::from_u32(0)))
6597            .handle_table()
6598            .subtask_insert_host(task.rep())?;
6599        let common = &mut store_opaque.concurrent_state_mut()?.get_mut(task)?.common;
6600        common.handle = Some(handle);
6601        common.event = event;
6602        Ok((store, instance, task, handle))
6603    }
6604
6605    #[test]
6606    fn host_subtask_drop_during_cancellation() -> Result<()> {
6607        for abort_completed in [false, true] {
6608            let (handle, future) = JoinHandle::run(future::pending::<()>());
6609            let mut future = pin!(future);
6610            let (mut store, instance, task, handle) =
6611                host_subtask(HostTaskState::CalleeRunning(handle), None)?;
6612            let store = store.as_store_opaque();
6613            let caller = RuntimeComponentInstanceIndex::from_u32(0);
6614            assert_eq!(
6615                instance.subtask_cancel(store, caller, true, handle)?,
6616                BLOCKED
6617            );
6618            if abort_completed {
6619                // Even after the abort resolves, the completion worker still
6620                // needs the task entry to lower the result and publish an event.
6621                assert!(matches!(
6622                    future
6623                        .as_mut()
6624                        .poll(&mut Context::from_waker(Waker::noop())),
6625                    Poll::Ready(None),
6626                ));
6627            }
6628            for async_ in [false, true] {
6629                let err = instance
6630                    .subtask_cancel(store, caller, async_, handle)
6631                    .unwrap_err();
6632                assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskCancelAfterTerminal);
6633            }
6634            let err = instance.subtask_drop(store, caller, handle).unwrap_err();
6635            assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskDropNotResolved);
6636            assert!(store.concurrent_state_mut()?.get_mut(task).is_ok());
6637        }
6638        Ok(())
6639    }
6640
6641    #[test]
6642    fn host_subtask_cancel_after_completion() -> Result<()> {
6643        for async_ in [false, true] {
6644            let (mut store, instance, task, handle) = host_subtask(
6645                HostTaskState::CalleeDone { cancelled: false },
6646                Some(Event::Subtask {
6647                    status: Status::Returned,
6648                }),
6649            )?;
6650            let store = store.as_store_opaque();
6651            let caller = RuntimeComponentInstanceIndex::from_u32(0);
6652            assert_eq!(
6653                instance.subtask_cancel(store, caller, async_, handle)?,
6654                Status::Returned as u32,
6655            );
6656            let err = instance
6657                .subtask_cancel(store, caller, async_, handle)
6658                .unwrap_err();
6659            assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskCancelAfterTerminal);
6660            instance.subtask_drop(store, caller, handle)?;
6661            assert!(store.concurrent_state_mut()?.get_mut(task).is_err());
6662        }
6663        Ok(())
6664    }
6665
6666    #[test]
6667    fn host_subtask_drop_requires_terminal_event_delivery() -> Result<()> {
6668        for (cancelled, status) in [
6669            (false, Status::Returned),
6670            (true, Status::Returned),
6671            (true, Status::ReturnCancelled),
6672        ] {
6673            for delivered in [false, true] {
6674                let event = if delivered {
6675                    None
6676                } else {
6677                    Some(Event::Subtask { status })
6678                };
6679                let (mut store, instance, task, handle) =
6680                    host_subtask(HostTaskState::CalleeDone { cancelled }, event)?;
6681                let store = store.as_store_opaque();
6682                let result = instance.subtask_drop(
6683                    store,
6684                    RuntimeComponentInstanceIndex::from_u32(0),
6685                    handle,
6686                );
6687                if delivered {
6688                    result?;
6689                    assert!(store.concurrent_state_mut()?.get_mut(task).is_err());
6690                } else {
6691                    let err = result.unwrap_err();
6692                    assert_eq!(err.downcast::<Trap>()?, Trap::SubtaskDropNotResolved);
6693                    assert!(store.concurrent_state_mut()?.get_mut(task).is_ok());
6694                }
6695            }
6696        }
6697        Ok(())
6698    }
6699}