A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
allow recursive Wasm invocation from concurrent host functions (#12152)
Joel Dicecb97ae85b574
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex b62ed4687e..d8c6f4c5f3 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -51,10 +51,15 @@//! in host functions.use crate::component::func::{self, Func};-use crate::component::{HasData, HasSelf, Instance, Resource, ResourceTable, ResourceTableError};+use crate::component::store::StoreComponentInstanceId;+use crate::component::{+ ComponentInstanceId, HasData, HasSelf, Instance, Resource, ResourceTable, ResourceTableError,+};use crate::fiber::{self, StoreFiber, StoreFiberYield};use crate::store::{Store, StoreId, StoreInner, StoreOpaque, StoreToken};-use crate::vm::component::{CallContext, ComponentInstance, InstanceFlags, ResourceTables};+use crate::vm::component::{+ CallContext, ComponentInstance, HandleTable, InstanceFlags, ResourceTables,+};use crate::vm::{AlwaysMut, SendSyncPtr, VMFuncRef, VMMemoryDefinition, VMStore};use crate::{AsContext, AsContextMut, FuncType, StoreContext, StoreContextMut, ValRaw, ValType};use anyhow::{Context as _, Result, anyhow, bail};@@ -67,7 +72,7 @@ use std::any::Any;use std::borrow::ToOwned;use std::boxed::Box;use std::cell::UnsafeCell;-use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};+use std::collections::{BTreeMap, BTreeSet, HashSet};use std::fmt;use std::future::Future;use std::marker::PhantomData;@@ -642,9 +647,12 @@ impl GuestCall {////// - the call is for a not-yet started task and the (sub-)component/// instance to be called has backpressure enabled- fn is_ready(&self, state: &mut ConcurrentState) -> Result<bool> {- let task_instance = state.get_mut(self.thread.task)?.instance;- let state = state.instance_state(task_instance);+ fn is_ready(&self, store: &mut StoreOpaque) -> Result<bool> {+ let instance = store+ .concurrent_state_mut()+ .get_mut(self.thread.task)?+ .instance;+ let state = store.instance_state(instance);let ready = match &self.kind {GuestCallKind::DeliverEvent { .. } => !state.do_not_enter,@@ -851,21 +859,32 @@ fn handle_guest_call(store: &mut dyn VMStore, call: GuestCall) -> Result<()> {store.maybe_push_call_context(call.thread.task)?;- let state = store.concurrent_state_mut();- state.enter_instance(runtime_instance);+ store.enter_instance(runtime_instance);- let callback = state.get_mut(call.thread.task)?.callback.take().unwrap();+ let callback = store+ .concurrent_state_mut()+ .get_mut(call.thread.task)?+ .callback+ .take()+ .unwrap();- let code = callback(store, runtime_instance, event, handle)?;+ let code = callback(store, runtime_instance.index, event, handle)?;- let state = store.concurrent_state_mut();+ store+ .concurrent_state_mut()+ .get_mut(call.thread.task)?+ .callback = Some(callback);- state.get_mut(call.thread.task)?.callback = Some(callback);- state.exit_instance(runtime_instance)?;+ store.exit_instance(runtime_instance)?;store.maybe_pop_call_context(call.thread.task)?;- next = instance.handle_callback_code(store, call.thread, runtime_instance, code)?;+ next = instance.handle_callback_code(+ store,+ call.thread,+ runtime_instance.index,+ code,+ )?;store.concurrent_state_mut().guest_thread = old_thread;log::trace!(@@ -922,7 +941,7 @@ impl<T> StoreContextMut<'_, T> {store.store_data_mut().components- .assert_guest_tables_empty();+ .assert_instance_states_empty();let state = store.concurrent_state_mut();assert!(state.table.get_mut().is_empty(),@@ -933,12 +952,6 @@ impl<T> StoreContextMut<'_, T> {assert!(state.low_priority.is_empty());assert!(state.guest_thread.is_none());assert!(state.futures.get_mut().as_ref().unwrap().is_empty());- assert!(- state- .instance_states- .iter()- .all(|(_, state)| state.pending.is_empty())- );assert!(state.global_error_context_ref_counts.is_empty());}@@ -1308,10 +1321,10 @@ impl<T> StoreContextMut<'_, T> {self.0.resume_fiber(fiber).await?;}WorkItem::GuestCall(call) => {- let state = self.0.concurrent_state_mut();- if call.is_ready(state)? {+ if call.is_ready(self.0)? {self.run_on_worker(WorkerItem::GuestCall(call)).await?;} else {+ let state = self.0.concurrent_state_mut();let task = state.get_mut(call.thread.task)?;if !task.starting_sent {task.starting_sent = true;@@ -1325,9 +1338,9 @@ impl<T> StoreContextMut<'_, T> {}}- let runtime_instance = state.get_mut(call.thread.task)?.instance;- state- .instance_state(runtime_instance)+ let instance = state.get_mut(call.thread.task)?.instance;+ self.0+ .instance_state(instance).pending.insert(call.thread, call.kind);}@@ -1415,7 +1428,90 @@ impl<T> StoreContextMut<'_, T> {}}+#[derive(Debug, Copy, Clone)]+pub struct RuntimeInstance {+ pub instance: ComponentInstanceId,+ pub index: RuntimeComponentInstanceIndex,+}+impl StoreOpaque {+ /// Helper function to retrieve the `ConcurrentInstanceState` for the+ /// specified instance.+ fn instance_state(&mut self, instance: RuntimeInstance) -> &mut ConcurrentInstanceState {+ StoreComponentInstanceId::new(self.id(), instance.instance)+ .get_mut(self)+ .instance_state(instance.index)+ .unwrap()+ .concurrent_state()+ }++ /// Helper function to retrieve the `HandleTable` for the specified+ /// instance.+ fn handle_table(&mut self, instance: RuntimeInstance) -> &mut HandleTable {+ StoreComponentInstanceId::new(self.id(), instance.instance)+ .get_mut(self)+ .instance_state(instance.index)+ .unwrap()+ .handle_table()+ }++ /// Record that we're about to enter a (sub-)component instance which does+ /// not support more than one concurrent, stackful activation, meaning it+ /// cannot be entered again until the next call returns.+ fn enter_instance(&mut self, instance: RuntimeInstance) {+ log::trace!("enter {instance:?}");+ self.instance_state(instance).do_not_enter = true;+ }++ /// Record that we've exited a (sub-)component instance previously entered+ /// with `Self::enter_instance` and then calls `Self::partition_pending`.+ /// See the documentation for the latter for details.+ fn exit_instance(&mut self, instance: RuntimeInstance) -> Result<()> {+ log::trace!("exit {instance:?}");+ self.instance_state(instance).do_not_enter = false;+ self.partition_pending(instance)+ }++ /// Iterate over `InstanceState::pending`, moving any ready items into the+ /// "high priority" work item queue.+ ///+ /// See `GuestCall::is_ready` for details.+ fn partition_pending(&mut self, instance: RuntimeInstance) -> Result<()> {+ for (thread, kind) in mem::take(&mut self.instance_state(instance).pending).into_iter() {+ let call = GuestCall { thread, kind };+ if call.is_ready(self)? {+ self.concurrent_state_mut()+ .push_high_priority(WorkItem::GuestCall(call));+ } else {+ self.instance_state(instance)+ .pending+ .insert(call.thread, call.kind);+ }+ }++ Ok(())+ }++ /// Implements the `backpressure.{inc,dec}` intrinsics.+ pub(crate) fn backpressure_modify(+ &mut self,+ caller_instance: RuntimeInstance,+ modify: impl FnOnce(u16) -> Option<u16>,+ ) -> Result<()> {+ let state = self.instance_state(caller_instance);+ let old = state.backpressure;+ let new = modify(old).ok_or_else(|| anyhow!("backpressure counter overflow"))?;+ state.backpressure = new;++ if old > 0 && new == 0 {+ // Backpressure was previously enabled and is now disabled; move any+ // newly-eligible guest calls to the "high priority" queue.+ self.partition_pending(caller_instance)?;+ }++ Ok(())+ }+/// Resume the specified fiber, giving it exclusive access to the specified/// store.async fn resume_fiber(&mut self, fiber: StoreFiber<'static>) -> Result<()> {@@ -1639,12 +1735,16 @@ impl Instance {let state = store.concurrent_state_mut();- let get_set = |store, handle| {+ let get_set = |store: &mut StoreOpaque, handle| {if handle == 0 {bail!("invalid waitable-set handle");}- let set = self.id().get_mut(store).guest_tables().0[runtime_instance]+ let set = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: runtime_instance,+ }).waitable_set_rep(handle)?;Ok(TableId::<WaitableSet>::new(set))@@ -1763,7 +1863,11 @@ impl Instance {.concurrent_state_mut().get_mut(guest_thread.thread)?.instance_rep;- self.id().get_mut(store).guest_tables().0[runtime_instance]+ store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: runtime_instance,+ }).guest_thread_remove(guest_id.unwrap())?;store.concurrent_state_mut().delete(guest_thread.thread)?;@@ -1875,6 +1979,7 @@ impl Instance {.concurrent_state_mut().get_mut(guest_thread.task)?.instance;+let fun = if callback.is_some() {assert!(async_);@@ -1882,7 +1987,7 @@ impl Instance {self.add_guest_thread_to_instance_table(guest_thread.thread,store,- callee_instance,+ callee_instance.index,)?;let old_thread = store.concurrent_state_mut()@@ -1894,7 +1999,7 @@ impl Instance {store.maybe_push_call_context(guest_thread.task)?;- store.concurrent_state_mut().enter_instance(callee_instance);+ store.enter_instance(callee_instance);// SAFETY: See the documentation for `make_call` to review the// contract we must uphold for `call` here.@@ -1904,9 +2009,7 @@ impl Instance {// over must be valid.let storage = call(store)?;- store- .concurrent_state_mut()- .exit_instance(callee_instance)?;+ store.exit_instance(callee_instance)?;store.maybe_pop_call_context(guest_thread.task)?;@@ -1920,7 +2023,7 @@ impl Instance {// function returns a `i32` result.let code = unsafe { storage[0].assume_init() }.get_i32() as u32;- self.handle_callback_code(store, guest_thread, callee_instance, code)+ self.handle_callback_code(store, guest_thread, callee_instance.index, code)})as Box<dyn FnOnce(&mut dyn VMStore) -> Result<Option<GuestCall>> + Send + Sync>} else {@@ -1929,7 +2032,7 @@ impl Instance {self.add_guest_thread_to_instance_table(guest_thread.thread,store,- callee_instance,+ callee_instance.index,)?;let old_thread = store.concurrent_state_mut()@@ -1938,7 +2041,7 @@ impl Instance {log::trace!("sync/async-stackful call: replaced {old_thread:?} with {guest_thread:?} as current thread",);- let mut flags = self.id().get(store).instance_flags(callee_instance);+ let mut flags = self.id().get(store).instance_flags(callee_instance.index);store.maybe_push_call_context(guest_thread.task)?;@@ -1946,7 +2049,7 @@ impl Instance {// async-lifted export, we need to record that the instance// cannot be entered until the call returns.if !async_ {- store.concurrent_state_mut().enter_instance(callee_instance);+ store.enter_instance(callee_instance);}// SAFETY: See the documentation for `make_call` to review the@@ -1958,7 +2061,7 @@ impl Instance {let storage = call(store)?;// This is a callback-less call, so the implicit thread has now completed- self.cleanup_thread(store, guest_thread, callee_instance)?;+ self.cleanup_thread(store, guest_thread, callee_instance.index)?;if async_ {let task = store.concurrent_state_mut().get_mut(guest_thread.task)?;@@ -1972,9 +2075,9 @@ impl Instance {// subtask has returned.let lift = {- let state = store.concurrent_state_mut();- state.exit_instance(callee_instance)?;+ store.exit_instance(callee_instance)?;+ let state = store.concurrent_state_mut();assert!(state.get_mut(guest_thread.task)?.result.is_none());state@@ -2244,6 +2347,7 @@ impl Instance {instance: caller_instance,},None,+ self,callee_instance,callee_async,)?;@@ -2387,7 +2491,7 @@ impl Instance {let instance_flags = if callback.is_null() {None} else {- Some(self.id().get(store.0).instance_flags(callee_instance))+ Some(self.id().get(store.0).instance_flags(callee_instance.index))};// Queue the call as a "high priority" work item.@@ -2460,7 +2564,12 @@ impl Instance {// It hasn't returned yet, but the caller is calling via an// async-lowered import, so we generate a handle for the task// waitable and return the status.- let handle = self.id().get_mut(store.0).guest_tables().0[caller_instance]+ let handle = store+ .0+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ }).subtask_insert_guest(guest_thread.task.rep())?;store.0@@ -2637,7 +2746,12 @@ impl Instance {});store.0.concurrent_state_mut().push_future(future);- let handle = self.id().get_mut(store.0).guest_tables().0[caller_instance]+ let handle = store+ .0+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ }).subtask_insert_host(task.rep())?;store.0.concurrent_state_mut().get_mut(task)?.common.handle = Some(handle);log::trace!(@@ -2762,7 +2876,7 @@ impl Instance {ResourceTables {calls,host_table: Some(host_table),- guest: Some(instance.guest_tables()),+ guest: Some(instance.instance_states()),}.exit_call()?;} else {@@ -2803,7 +2917,11 @@ impl Instance {) -> Result<u32> {self.id().get_mut(store).check_may_leave(caller_instance)?;let set = store.concurrent_state_mut().push(WaitableSet::default())?;- let handle = self.id().get_mut(store).guest_tables().0[caller_instance]+ let handle = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ }).waitable_set_insert(set.rep())?;log::trace!("new waitable set {set:?} (handle {handle})");Ok(handle)@@ -2817,8 +2935,12 @@ impl Instance {set: u32,) -> Result<()> {self.id().get_mut(store).check_may_leave(caller_instance)?;- let rep =- self.id().get_mut(store).guest_tables().0[caller_instance].waitable_set_remove(set)?;+ let rep = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ })+ .waitable_set_remove(set)?;log::trace!("drop waitable set {rep} (handle {set})");@@ -2849,7 +2971,9 @@ impl Instance {let set = if set_handle == 0 {None} else {- let set = instance.guest_tables().0[caller_instance].waitable_set_rep(set_handle)?;+ let set = instance.instance_states().0[caller_instance]+ .handle_table()+ .waitable_set_rep(set_handle)?;Some(TableId::<WaitableSet>::new(set))};@@ -2871,8 +2995,12 @@ impl Instance {self.id().get_mut(store).check_may_leave(caller_instance)?;self.waitable_join(store, caller_instance, task_id, 0)?;- let (rep, is_host) =- self.id().get_mut(store).guest_tables().0[caller_instance].subtask_remove(task_id)?;+ let (rep, is_host) = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ })+ .subtask_remove(task_id)?;let concurrent_state = store.concurrent_state_mut();let (waitable, expected_caller_instance, delete) = if is_host {@@ -2936,8 +3064,12 @@ impl Instance {instance: caller_instance,..} = &self.id().get(store).component().env_component().options[options];- let rep =- self.id().get_mut(store).guest_tables().0[caller_instance].waitable_set_rep(set)?;+ let rep = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ })+ .waitable_set_rep(set)?;self.waitable_check(store,@@ -2973,8 +3105,12 @@ impl Instance {instance: caller_instance,..} = &self.id().get(store).component().env_component().options[options];- let rep =- self.id().get_mut(store).guest_tables().0[caller_instance].waitable_set_rep(set)?;+ let rep = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ })+ .waitable_set_rep(set)?;self.waitable_check(store,@@ -3127,7 +3263,11 @@ impl Instance {store: &mut StoreOpaque,runtime_instance: RuntimeComponentInstanceIndex,) -> Result<u32> {- let guest_id = self.id().get_mut(store).guest_tables().0[runtime_instance]+ let guest_id = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: runtime_instance,+ }).guest_thread_insert(thread_id.rep())?;store.concurrent_state_mut()@@ -3310,8 +3450,12 @@ impl Instance {store.concurrent_state_mut().check_blocking()?;}- let (rep, is_host) =- self.id().get_mut(store).guest_tables().0[caller_instance].subtask_rep(task_id)?;+ let (rep, is_host) = store+ .handle_table(RuntimeInstance {+ instance: self.id().instance(),+ index: caller_instance,+ })+ .subtask_rep(task_id)?;let (waitable, expected_caller_instance) = if is_host {let id = TableId::<HostTask>::new(rep);(@@ -3350,9 +3494,8 @@ impl Instance {task.lift_result = None;// Not yet started; cancel and remove from pending- let callee_instance = task.instance;-- let pending = &mut concurrent_state.instance_state(callee_instance).pending;+ let instance = task.instance;+ let pending = &mut store.instance_state(instance).pending;let pending_count = pending.len();pending.retain(|thread, _| thread.task != guest_task);// If there were no pending threads for this task, we're in an error state@@ -4081,7 +4224,9 @@ impl GuestThread {caller_instance: RuntimeComponentInstanceIndex,guest_thread: u32,) -> Result<TableId<Self>> {- let rep = state.guest_tables().0[caller_instance].guest_thread_rep(guest_thread)?;+ let rep = state.instance_states().0[caller_instance]+ .handle_table()+ .guest_thread_rep(guest_thread)?;Ok(TableId::new(rep))}@@ -4177,12 +4322,13 @@ pub(crate) struct GuestTask {subtasks: HashSet<TableId<GuestTask>>,/// Scratch waitable set used to watch subtasks during synchronous calls.sync_call_set: TableId<WaitableSet>,- /// The instance to which the exported function for this guest task belongs.+ /// The runtime instance to which the exported function for this guest task+ /// belongs.////// Note that the task may do a sync->sync call via a fused adapter which/// results in that task executing code in a different instance, and it may/// call host functions and intrinsics from that other instance.- instance: RuntimeComponentInstanceIndex,+ instance: RuntimeInstance,/// If present, a pending `Event::None` or `Event::Cancelled` to be/// delivered to this task.event: Option<Event>,@@ -4205,10 +4351,12 @@ impl GuestTask {// We reset `lower_params` after we lower the parametersself.lower_params.is_none()}+fn returned_or_cancelled(&self) -> bool {// We reset `lift_result` after we return or exitself.lift_result.is_none()}+fn ready_to_delete(&self) -> bool {let threads_completed = self.threads.is_empty();let has_sync_result = matches!(self.sync_result, SyncResult::Produced(_));@@ -4231,13 +4379,15 @@ impl GuestTask {);ready}+fn new(state: &mut ConcurrentState,lower_params: RawLower,lift_result: LiftResult,caller: Caller,callback: Option<CallbackFn>,- component_instance: RuntimeComponentInstanceIndex,+ component_instance: Instance,+ instance: RuntimeComponentInstanceIndex,async_function: bool,) -> Result<Self> {let sync_call_set = state.push(WaitableSet::default())?;@@ -4267,7 +4417,10 @@ impl GuestTask {starting_sent: false,subtasks: HashSet::new(),sync_call_set,- instance: component_instance,+ instance: RuntimeInstance {+ instance: component_instance.id().instance(),+ index: instance,+ },event: None,function_index: None,exited: false,@@ -4390,7 +4543,9 @@ impl Waitable {) -> Result<Self> {use crate::runtime::vm::component::Waitable;- let (waitable, kind) = state.guest_tables().0[caller_instance].waitable_rep(waitable)?;+ let (waitable, kind) = state.instance_states().0[caller_instance]+ .handle_table()+ .waitable_rep(waitable)?;Ok(match kind {Waitable::Subtask { is_host: true } => Self::Host(TableId::new(waitable)),@@ -4561,7 +4716,7 @@ struct DummyResult;/// Represents the Component Model Async state of a (sub-)component instance.#[derive(Default)]-struct InstanceState {+pub struct ConcurrentInstanceState {/// Whether backpressure is set for this instance (enabled if >0)backpressure: u16,/// Whether this instance can be entered@@ -4571,6 +4726,12 @@ struct InstanceState {pending: BTreeMap<QualifiedThreadId, GuestCallKind>,}+impl ConcurrentInstanceState {+ pub fn pending_is_empty(&self) -> bool {+ self.pending.is_empty()+ }+}+/// Represents the Component Model Async state of a store.pub struct ConcurrentState {/// The currently running guest thread, if any.@@ -4583,12 +4744,6 @@ pub struct ConcurrentState {futures: AlwaysMut<Option<FuturesUnordered<HostTaskFuture>>>,/// The table of waitables, waitable sets, etc.table: AlwaysMut<ResourceTable>,- /// Per (sub-)component instance states.- ///- /// See `InstanceState` for details and note that this map is lazily- /// populated as needed.- // TODO: this can and should be a `PrimaryMap`- instance_states: HashMap<RuntimeComponentInstanceIndex, InstanceState>,/// The "high priority" work queue for this store's event loop.high_priority: Vec<WorkItem>,/// The "low priority" work queue for this store's event loop.@@ -4626,7 +4781,6 @@ impl Default for ConcurrentState {guest_thread: None,table: AlwaysMut::new(ResourceTable::new()),futures: AlwaysMut::new(Some(FuturesUnordered::new())),- instance_states: HashMap::new(),high_priority: Vec::new(),low_priority: Vec::new(),suspend_reason: None,@@ -4705,10 +4859,6 @@ impl ConcurrentState {}}- fn instance_state(&mut self, instance: RuntimeComponentInstanceIndex) -> &mut InstanceState {- self.instance_states.entry(instance).or_default()- }-fn push<V: Send + Sync + 'static>(&mut self,value: V,@@ -4795,7 +4945,7 @@ impl ConcurrentState {let next_thread = match &self.get_mut(guest_task).unwrap().caller {Caller::Host { .. } => break true,Caller::Guest { thread, instance } => {- if *instance == guest_instance {+ if *instance == guest_instance.index {break false;} else {*thread@@ -4806,60 +4956,6 @@ impl ConcurrentState {}}- /// Record that we're about to enter a (sub-)component instance which does- /// not support more than one concurrent, stackful activation, meaning it- /// cannot be entered again until the next call returns.- fn enter_instance(&mut self, instance: RuntimeComponentInstanceIndex) {- self.instance_state(instance).do_not_enter = true;- }-- /// Record that we've exited a (sub-)component instance previously entered- /// with `Self::enter_instance` and then calls `Self::partition_pending`.- /// See the documentation for the latter for details.- fn exit_instance(&mut self, instance: RuntimeComponentInstanceIndex) -> Result<()> {- self.instance_state(instance).do_not_enter = false;- self.partition_pending(instance)- }-- /// Iterate over `InstanceState::pending`, moving any ready items into the- /// "high priority" work item queue.- ///- /// See `GuestCall::is_ready` for details.- fn partition_pending(&mut self, instance: RuntimeComponentInstanceIndex) -> Result<()> {- for (thread, kind) in mem::take(&mut self.instance_state(instance).pending).into_iter() {- let call = GuestCall { thread, kind };- if call.is_ready(self)? {- self.push_high_priority(WorkItem::GuestCall(call));- } else {- self.instance_state(instance)- .pending- .insert(call.thread, call.kind);- }- }-- Ok(())- }-- /// Implements the `backpressure.{inc,dec}` intrinsics.- pub(crate) fn backpressure_modify(- &mut self,- caller_instance: RuntimeComponentInstanceIndex,- modify: impl FnOnce(u16) -> Option<u16>,- ) -> Result<()> {- let state = self.instance_state(caller_instance);- let old = state.backpressure;- let new = modify(old).ok_or_else(|| anyhow!("backpressure counter overflow"))?;- state.backpressure = new;-- if old > 0 && new == 0 {- // Backpressure was previously enabled and is now disabled; move any- // newly-eligible guest calls to the "high priority" queue.- self.partition_pending(caller_instance)?;- }-- Ok(())- }-/// Implements the `context.get` intrinsic.pub(crate) fn context_get(&mut self, slot: u32) -> Result<u32> {let thread = self.guest_thread.unwrap();@@ -5076,8 +5172,6 @@ pub(crate) fn prepare_call<T, R>(let token = StoreToken::new(store.as_context_mut());let state = store.0.concurrent_state_mut();- assert!(state.guest_thread.is_none());-let (tx, rx) = oneshot::channel();let (exit_tx, exit_rx) = oneshot::channel();@@ -5121,6 +5215,7 @@ pub(crate) fn prepare_call<T, R>(},) as CallbackFn}),+ handle.instance(),component_instance,async_function,)?;diff --git a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rsindex 42290a8d2e..9210914a18 100644--- a/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/futures_and_streams.rs@@ -2492,7 +2492,7 @@ impl<T> StoreContextMut<'_, T> {*producer.lock().unwrap() = Some((mine, buffer));if write_buffer {- write( token, id, producer.clone(), kind).await?;+ write(token, id, producer.clone(), kind).await?;}Ok(if dropped {@@ -4261,21 +4261,21 @@ impl Instance {impl ComponentInstance {fn table_for_transmit(self: Pin<&mut Self>, ty: TransmitIndex) -> &mut HandleTable {- let (tables, types) = self.guest_tables();+ let (states, types) = self.instance_states();let runtime_instance = match ty {TransmitIndex::Stream(ty) => types[ty].instance,TransmitIndex::Future(ty) => types[ty].instance,};- &mut tables[runtime_instance]+ states[runtime_instance].handle_table()}fn table_for_error_context(self: Pin<&mut Self>,ty: TypeComponentLocalErrorContextTableIndex,) -> &mut HandleTable {- let (tables, types) = self.guest_tables();+ let (states, types) = self.instance_states();let runtime_instance = types[ty].instance;- &mut tables[runtime_instance]+ states[runtime_instance].handle_table()}fn get_mut_by_index(@@ -4456,7 +4456,8 @@ impl Waitable {} => {let instance = instance.id().get_mut(store);let runtime_instance = instance.component().types()[ty].instance;- let (rep, state) = instance.guest_tables().0[runtime_instance]+ let (rep, state) = instance.instance_states().0[runtime_instance]+ .handle_table().future_rep(ty, handle).unwrap();assert_eq!(rep, self.rep());@@ -4477,7 +4478,8 @@ impl Waitable {} => {let instance = instance.id().get_mut(store);let runtime_instance = instance.component().types()[ty].instance;- let (rep, state) = instance.guest_tables().0[runtime_instance]+ let (rep, state) = instance.instance_states().0[runtime_instance]+ .handle_table().stream_rep(ty, handle).unwrap();assert_eq!(rep, self.rep());diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rsindex 366ffd7165..4b2ead4645 100644--- a/crates/wasmtime/src/runtime/component/func.rs+++ b/crates/wasmtime/src/runtime/component/func.rs@@ -794,7 +794,7 @@ impl Func {ResourceTables {host_table: Some(host_table),calls,- guest: Some(instance.guest_tables()),+ guest: Some(instance.instance_states()),}.exit_call()?;}diff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rsindex 4c4ec2588c..02a04df69a 100644--- a/crates/wasmtime/src/runtime/component/func/options.rs+++ b/crates/wasmtime/src/runtime/component/func/options.rs@@ -286,7 +286,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {ResourceTables {host_table: Some(host_table),calls,- guest: Some(instance.guest_tables()),+ guest: Some(instance.instance_states()),},host_resource_data,)@@ -473,7 +473,7 @@ impl<'a> LiftContext<'a> {calls: self.calls,// Note that the unsafety here should be valid given the contract of// `LiftContext::new`.- guest: Some(self.instance.as_mut().guest_tables()),+ guest: Some(self.instance.as_mut().instance_states()),},self.host_resource_data,)diff --git a/crates/wasmtime/src/runtime/component/instance.rs b/crates/wasmtime/src/runtime/component/instance.rsindex a803e6040c..caa75ae503 100644--- a/crates/wasmtime/src/runtime/component/instance.rs+++ b/crates/wasmtime/src/runtime/component/instance.rs@@ -659,7 +659,7 @@ fn resource_tables<'a>(ResourceTables {host_table: None,calls,- guest: Some(instance.guest_tables()),+ guest: Some(instance.instance_states()),}}diff --git a/crates/wasmtime/src/runtime/component/store.rs b/crates/wasmtime/src/runtime/component/store.rsindex 94817a52c8..c24070c7ee 100644--- a/crates/wasmtime/src/runtime/component/store.rs+++ b/crates/wasmtime/src/runtime/component/store.rs@@ -49,7 +49,7 @@ impl ComponentStoreData {}#[cfg(feature = "component-model-async")]- pub(crate) fn assert_guest_tables_empty(&mut self) {+ pub(crate) fn assert_instance_states_empty(&mut self) {for (_, instance) in self.instances.iter_mut() {let Some(instance) = instance.as_mut() else {continue;@@ -58,10 +58,11 @@ impl ComponentStoreData {assert!(instance.get_mut()- .guest_tables()+ .instance_states().0- .iter()- .all(|(_, table)| table.is_empty())+ .iter_mut()+ .all(|(_, state)| state.handle_table().is_empty()+ && state.concurrent_state().pending_is_empty()));}}diff --git a/crates/wasmtime/src/runtime/vm/component.rs b/crates/wasmtime/src/runtime/vm/component.rsindex 4d7c388b60..a75bbb3036 100644--- a/crates/wasmtime/src/runtime/vm/component.rs+++ b/crates/wasmtime/src/runtime/vm/component.rs@@ -9,6 +9,8 @@use crate::component::{Component, Instance, InstancePre, ResourceType, RuntimeImport};use crate::module::ModuleRegistry;use crate::runtime::component::ComponentInstanceId;+#[cfg(feature = "component-model-async")]+use crate::runtime::component::concurrent::ConcurrentInstanceState;use crate::runtime::vm::instance::{InstanceLayout, OwnedInstance, OwnedVMContext};use crate::runtime::vm::vmcontext::VMFunctionBody;use crate::runtime::vm::{@@ -45,6 +47,35 @@ pub use self::handle_table::{TransmitLocalState, Waitable};pub use self::resources::CallContext;pub use self::resources::{CallContexts, ResourceTables, TypedResource, TypedResourceIndex};+/// Represents the state of a (sub-)component instance.+#[derive(Default)]+pub struct InstanceState {+ /// Represents the Component Model Async state of a (sub-)component instance.+ #[cfg(feature = "component-model-async")]+ concurrent_state: ConcurrentInstanceState,++ /// State of handles (e.g. resources, waitables, etc.) for this instance.+ ///+ /// For resource handles, this is paired with other information to create a+ /// `ResourceTables` and manipulated through that. For other handles, this+ /// is used directly to translate guest handles to host representations and+ /// vice-versa.+ handle_table: HandleTable,+}++impl InstanceState {+ /// Represents the Component Model Async state of a (sub-)component instance.+ #[cfg(feature = "component-model-async")]+ pub fn concurrent_state(&mut self) -> &mut ConcurrentInstanceState {+ &mut self.concurrent_state+ }++ /// State of handles (e.g. resources, waitables, etc.) for this instance.+ pub fn handle_table(&mut self) -> &mut HandleTable {+ &mut self.handle_table+ }+}+/// Runtime representation of a component instance and all state necessary for/// the instance itself.///@@ -86,13 +117,9 @@ pub struct ComponentInstance {// borrowing a store mutably at the same time as a contained instance.component: Component,- /// State of handles (e.g. resources, waitables, etc.) for this component.- ///- /// For resource handles, this is paired with other information to create a- /// `ResourceTables` and manipulated through that. For other handles, this- /// is used directly to translate guest handles to host representations and- /// vice-versa.- instance_handle_tables: PrimaryMap<RuntimeComponentInstanceIndex, HandleTable>,+ /// Contains state specific to each (sub-)component instance within this+ /// top-level instance.+ instance_states: PrimaryMap<RuntimeComponentInstanceIndex, InstanceState>,/// What all compile-time-identified core instances are mapped to within the/// `Store` that this component belongs to.@@ -293,16 +320,15 @@ impl ComponentInstance {) -> OwnedComponentInstance {let offsets = VMComponentOffsets::new(HostPtr, component.env_component());let num_instances = component.env_component().num_runtime_component_instances;- let mut instance_handle_tables =- PrimaryMap::with_capacity(num_instances.try_into().unwrap());+ let mut instance_states = PrimaryMap::with_capacity(num_instances.try_into().unwrap());for _ in 0..num_instances {- instance_handle_tables.push(HandleTable::default());+ instance_states.push(InstanceState::default());}let mut ret = OwnedInstance::new(ComponentInstance {id,offsets,- instance_handle_tables,+ instance_states,instances: PrimaryMap::with_capacity(component.env_component()@@ -825,22 +851,30 @@ impl ComponentInstance {resource_instance == component.defined_resource_instances[idx]}- /// Returns the runtime state of resources associated with this component.+ /// Returns the runtime state of resources and concurrency associated with+ /// this component.#[inline]- pub fn guest_tables(+ pub fn instance_states(self: Pin<&mut Self>,) -> (- &mut PrimaryMap<RuntimeComponentInstanceIndex, HandleTable>,+ &mut PrimaryMap<RuntimeComponentInstanceIndex, InstanceState>,&ComponentTypes,) {// safety: we've chosen the `pin` guarantee of `self` to not apply to// the map returned.unsafe {let me = self.get_unchecked_mut();- (&mut me.instance_handle_tables, me.component.types())+ (&mut me.instance_states, me.component.types())}}+ pub fn instance_state(+ self: Pin<&mut Self>,+ instance: RuntimeComponentInstanceIndex,+ ) -> Option<&mut InstanceState> {+ self.instance_states().0.get_mut(instance)+ }+/// Returns the destructor and instance flags for the specified resource/// table type.///diff --git a/crates/wasmtime/src/runtime/vm/component/libcalls.rs b/crates/wasmtime/src/runtime/vm/component/libcalls.rsindex 02f21b5833..0825ae2009 100644--- a/crates/wasmtime/src/runtime/vm/component/libcalls.rs+++ b/crates/wasmtime/src/runtime/vm/component/libcalls.rs@@ -5,7 +5,7 @@ use crate::component::Instance;use crate::component::concurrent::WaitResult;use crate::prelude::*;#[cfg(feature = "component-model-async")]-use crate::runtime::component::concurrent::ResourcePair;+use crate::runtime::component::concurrent::{ResourcePair, RuntimeInstance};use crate::runtime::vm::component::{ComponentInstance, VMComponentContext};use crate::runtime::vm::{HostResultHasUnwindSentinel, VMStore, VmSafe};use core::cell::Cell;@@ -672,12 +672,15 @@ fn trap(_store: &mut dyn VMStore, _instance: Instance, code: u8) -> Result<()> {#[cfg(feature = "component-model-async")]fn backpressure_modify(store: &mut dyn VMStore,- _instance: Instance,+ instance: Instance,caller_instance: u32,increment: u8,) -> Result<()> {- store.concurrent_state_mut().backpressure_modify(- RuntimeComponentInstanceIndex::from_u32(caller_instance),+ store.backpressure_modify(+ RuntimeInstance {+ instance: instance.id().instance(),+ index: RuntimeComponentInstanceIndex::from_u32(caller_instance),+ },|old| {if increment != 0 {old.checked_add(1)diff --git a/crates/wasmtime/src/runtime/vm/component/resources.rs b/crates/wasmtime/src/runtime/vm/component/resources.rsindex 06fc507ab1..11473f45bf 100644--- a/crates/wasmtime/src/runtime/vm/component/resources.rs+++ b/crates/wasmtime/src/runtime/vm/component/resources.rs@@ -20,7 +20,7 @@//! about ABI details can be found in lifting/lowering throughout Wasmtime,//! namely in the `Resource<T>` and `ResourceAny` types.-use super::{HandleTable, RemovedResource};+use super::{HandleTable, InstanceState, RemovedResource};use crate::prelude::*;use core::error::Error;use core::fmt;@@ -53,7 +53,7 @@ pub struct ResourceTables<'a> {/// `ResourceAny::resource_drop` which won't consult this table as it's/// only operating over the host table.pub guest: Option<(- &'a mut PrimaryMap<RuntimeComponentInstanceIndex, HandleTable>,+ &'a mut PrimaryMap<RuntimeComponentInstanceIndex, InstanceState>,&'a ComponentTypes,)>,@@ -196,8 +196,8 @@ impl ResourceTables<'_> {match resource {TypedResource::Host(_) => self.host_table.as_mut().unwrap(),TypedResource::Component { ty, .. } => {- let (tables, types) = self.guest.as_mut().unwrap();- &mut tables[types[*ty].unwrap_concrete_instance()]+ let (states, types) = self.guest.as_mut().unwrap();+ states[types[*ty].unwrap_concrete_instance()].handle_table()}}}@@ -206,8 +206,8 @@ impl ResourceTables<'_> {match index {TypedResourceIndex::Host(_) => self.host_table.as_mut().unwrap(),TypedResourceIndex::Component { ty, .. } => {- let (tables, types) = self.guest.as_mut().unwrap();- &mut tables[types[*ty].unwrap_concrete_instance()]+ let (states, types) = self.guest.as_mut().unwrap();+ states[types[*ty].unwrap_concrete_instance()].handle_table()}}}diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex 0bac8c0dca..075938e429 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -767,3 +767,73 @@ async fn cancel_host_future() -> Result<()> {}}}++#[tokio::test]+#[cfg_attr(miri, ignore)]+async fn run_wasm_in_call_async() -> Result<()> {+ _ = env_logger::try_init();++ let mut config = Config::new();+ config.async_support(true);+ config.wasm_component_model_async(true);+ let engine = Engine::new(&config)?;++ let a = Component::new(+ &engine,+ r#"+(component+ (type $t (func async))+ (import "a" (func $f (type $t)))+ (core func $f (canon lower (func $f)))+ (core module $a+ (import "" "f" (func $f))+ (func (export "run") call $f)+ )+ (core instance $a (instantiate $a+ (with "" (instance (export "f" (func $f))))+ ))+ (func (export "run") (type $t)+ (canon lift (core func $a "run")))+)+ "#,+ )?;+ let b = Component::new(+ &engine,+ r#"+(component+ (type $t (func async))+ (core module $a+ (func (export "run"))+ )+ (core instance $a (instantiate $a))+ (func (export "run") (type $t)+ (canon lift (core func $a "run")))+)+ "#,+ )?;++ type State = Option<Instance>;++ let mut linker = Linker::new(&engine);+ linker+ .root()+ .func_wrap_concurrent("a", |accessor: &Accessor<State>, (): ()| {+ Box::pin(async move {+ let func = accessor.with(|mut access| {+ access+ .get()+ .unwrap()+ .get_typed_func::<(), ()>(&mut access, "run")+ })?;+ func.call_concurrent(accessor, ()).await?;+ Ok(())+ })+ })?;+ let mut store = Store::new(&engine, None);+ let instance_a = linker.instantiate_async(&mut store, &a).await?;+ let instance_b = linker.instantiate_async(&mut store, &b).await?;+ *store.data_mut() = Some(instance_b);+ let run = instance_a.get_typed_func::<(), ()>(&mut store, "run")?;+ run.call_async(&mut store, ()).await?;+ Ok(())+}