Skip to content

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

Don't instantiate concurrent hostcall glue for non-concurrent host functions (#14517)

Kyle Kovalchick2574af09bcd7
diff --git a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
index 7ba9630203..cbdcfcd475 100644
--- a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs
@@ -13,6 +13,8 @@ use wasmtime_environ::component::{CanonicalAbiInfo, InterfaceType};
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
pub struct TaskGroupId;
+pub(crate) type EnteredHostTask = ();
+
fn should_have_failed_validation<T>(what: &str) -> Result<T> {
// This should be unreachable; if we trap here, it indicates a
// bug in Wasmtime rather than in the guest.
diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rs
index fd889f9d8a..c5ffa359e1 100644
--- a/crates/wasmtime/src/runtime/component/func/host.rs
+++ b/crates/wasmtime/src/runtime/component/func/host.rs
@@ -1,7 +1,8 @@
//! Implementation of calling Rust-defined functions from components.
+use crate::component::concurrent::EnteredHostTask;
#[cfg(feature = "component-model-async")]
-use crate::component::concurrent::{self, Accessor, Status};
+use crate::component::concurrent::{self, Accessor, HostTask, Status, table::TableId};
use crate::component::func::{LiftContext, LowerContext};
use crate::component::matching::InstanceType;
use crate::component::storage::{slice_to_storage, slice_to_storage_mut};
@@ -59,10 +60,65 @@ impl core::fmt::Debug for HostFunc {
}
}
-enum HostResult<T> {
- Done(Result<T>),
+#[cfg(feature = "component-model-async")]
+type HostFuture<R> = Pin<Box<dyn Future<Output = Result<R>> + Send>>;
+
+/// Only the future impl references `poll_and_block`/`first_poll`, so
+/// non-concurrent host functions don't instantiate them.
+trait HostOutput<R> {
+ fn finish_sync(self, store: &mut dyn VMStore, host_task: EnteredHostTask) -> Result<R>;
+
+ #[cfg(feature = "component-model-async")]
+ fn finish_async<T: 'static>(
+ self,
+ store: StoreContextMut<'_, T>,
+ instance: Instance,
+ host_task: EnteredHostTask,
+ lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>
+ + Send
+ + 'static,
+ ) -> Result<u32>;
+}
+
+impl<R> HostOutput<R> for Result<R> {
+ fn finish_sync(self, _store: &mut dyn VMStore, _host_task: EnteredHostTask) -> Result<R> {
+ self
+ }
+
#[cfg(feature = "component-model-async")]
- Future(Pin<Box<dyn Future<Output = Result<T>> + Send>>),
+ fn finish_async<T: 'static>(
+ self,
+ store: StoreContextMut<'_, T>,
+ _instance: Instance,
+ _host_task: EnteredHostTask,
+ lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>
+ + Send
+ + 'static,
+ ) -> Result<u32> {
+ let result = self?;
+ let task = store.0.current_materialized_host_task()?;
+ lower(store, Some(result), true, task)?;
+ Ok(Status::Returned.pack(None))
+ }
+}
+
+#[cfg(feature = "component-model-async")]
+impl<R: Send + Sync + 'static> HostOutput<R> for HostFuture<R> {
+ fn finish_sync(self, store: &mut dyn VMStore, host_task: EnteredHostTask) -> Result<R> {
+ concurrent::poll_and_block(store, host_task, self)
+ }
+
+ fn finish_async<T: 'static>(
+ self,
+ store: StoreContextMut<'_, T>,
+ instance: Instance,
+ host_task: EnteredHostTask,
+ lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>
+ + Send
+ + 'static,
+ ) -> Result<u32> {
+ instance.first_poll(store, host_task, self, lower)
+ }
}
impl HostFunc {
@@ -96,12 +152,7 @@ impl HostFunc {
P: ComponentNamedList + Lift + 'static,
R: ComponentNamedList + Lower + 'static,
{
- Self::new(
- Asyncness::No,
- StaticHostFn::<_, false>::new(move |store, params| {
- HostResult::Done(func(store, params))
- }),
- )
+ Self::new(Asyncness::No, StaticHostFn::<_, false>::new(func))
}
/// Equivalent for `Linker::func_wrap_async`
@@ -118,12 +169,10 @@ impl HostFunc {
{
Self::new(
Asyncness::Yes,
- StaticHostFn::<_, false>::new(move |store, params| {
- HostResult::Done(
- store
- .block_on(|store| Pin::from(func(store, params)))
- .and_then(|r| r),
- )
+ StaticHostFn::<_, false>::new(move |store: StoreContextMut<'_, T>, params| {
+ store
+ .block_on(|store| Pin::from(func(store, params)))
+ .and_then(|r| r)
}),
)
}
@@ -143,11 +192,9 @@ impl HostFunc {
let func = Arc::new(func);
Self::new(
Asyncness::Yes,
- StaticHostFn::<_, true>::new(move |store, params| {
+ StaticHostFn::<_, true>::new(move |store: StoreContextMut<'_, T>, params| {
let func = func.clone();
- HostResult::Future(Box::pin(
- store.wrap_call(move |accessor| func(accessor, params)),
- ))
+ Box::pin(store.wrap_call(move |accessor| func(accessor, params))) as HostFuture<R>
}),
)
}
@@ -166,8 +213,7 @@ impl HostFunc {
DynamicHostFn::<_, false>::new(
move |store, ty, mut params_and_results, result_start| {
let (params, results) = params_and_results.split_at_mut(result_start);
- let result = func(store, ty, params, results).map(move |()| params_and_results);
- HostResult::Done(result)
+ func(store, ty, params, results).map(move |()| params_and_results)
},
),
)
@@ -198,8 +244,7 @@ impl HostFunc {
cx.block_on(Pin::from(func(store, ty, params, results)))
})
.and_then(|r| r);
- let result = result.map(move |()| params_and_results);
- HostResult::Done(result)
+ result.map(move |()| params_and_results)
},
),
)
@@ -226,13 +271,13 @@ impl HostFunc {
DynamicHostFn::<_, true>::new(
move |store, ty, mut params_and_results, result_start| {
let func = func.clone();
- HostResult::Future(Box::pin(store.wrap_call(move |accessor| {
+ Box::pin(store.wrap_call(move |accessor| {
Box::pin(async move {
let (params, results) = params_and_results.split_at_mut(result_start);
func(accessor, ty, params, results).await?;
Ok(params_and_results)
})
- })))
+ })) as HostFuture<Vec<Val>>
},
),
)
@@ -283,12 +328,14 @@ where
T: 'static,
R: Send + Sync + 'static,
{
+ type Output: HostOutput<R>;
+
/// Performs a type-check to ensure that this host function can be imported
/// with the provided signature that a component is using.
fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()>;
/// Execute this host function.
- fn run(&self, store: StoreContextMut<'_, T>, params: P) -> HostResult<R>;
+ fn run(&self, store: StoreContextMut<'_, T>, params: P) -> Self::Output;
/// Performs the lifting operation to convert arguments from the canonical
/// ABI in wasm memory/arguments into their Rust representation.
@@ -394,13 +441,9 @@ where
let mut lift = LiftContext::new(store.0.store_opaque_mut(), options, instance)?;
let (params, rest) = self.load_params(&mut lift, ty, MAX_FLAT_PARAMS, storage)?;
- let ret = match self.run(store.as_context_mut(), params) {
- HostResult::Done(result) => result?,
- #[cfg(feature = "component-model-async")]
- HostResult::Future(future) => {
- concurrent::poll_and_block(store.0, entered_host_task, future)?
- }
- };
+ let ret = self
+ .run(store.as_context_mut(), params)
+ .finish_sync(store.0, entered_host_task)?;
let mut lower = LowerContext::new(store, options, instance);
let fty = &lower.types[ty];
@@ -471,47 +514,30 @@ where
0
};
- let host_result = self.run(store.as_context_mut(), params);
-
- let rc = match host_result {
- HostResult::Done(result) => {
- let result = result?;
+ let rc = self.run(store.as_context_mut(), params).finish_async(
+ store.as_context_mut(),
+ instance,
+ entered_host_task,
+ move |store, ret, immediate, materialized_host_task| {
let mut lower = LowerContext::new(store, options, instance);
lower.validate_scope_exit()?;
- // Check if running the future created an actual host task in the store.
- let materialized_host_task = lower.store.0.current_materialized_host_task()?;
- lower
- .store
- .0
- .host_task_delete(entered_host_task, materialized_host_task)?;
- Self::lower_raw(&mut lower, ty, result, Destination::Memory(retptr))?;
- Status::Returned.pack(None)
- }
- HostResult::Future(future) => instance.first_poll(
- store.as_context_mut(),
- entered_host_task,
- future,
- move |store, ret, immediate, materialized_host_task| {
- let mut lower = LowerContext::new(store, options, instance);
- lower.validate_scope_exit()?;
- if immediate {
- lower
- .store
- .0
- .host_task_delete(entered_host_task, materialized_host_task)?;
- }
- // FIXME(WebAssembly/component-model#678) the currently
- // running thread for this exit lower is wrong. This happens
- // to pick whatever's in the store at the time of a
- // non-immediate exit which is not correct. There's no real
- // right answer here, hence the upstream issue.
- if let Some(result) = ret {
- Self::lower_raw(&mut lower, ty, result, Destination::Memory(retptr))?;
- }
- Ok(())
- },
- )?,
- };
+ if immediate {
+ lower
+ .store
+ .0
+ .host_task_delete(entered_host_task, materialized_host_task)?;
+ }
+ // FIXME(WebAssembly/component-model#678) the currently
+ // running thread for this exit lower is wrong. This happens
+ // to pick whatever's in the store at the time of a
+ // non-immediate exit which is not correct. There's no real
+ // right answer here, hence the upstream issue.
+ if let Some(result) = ret {
+ Self::lower_raw(&mut lower, ty, result, Destination::Memory(retptr))?;
+ }
+ Ok(())
+ },
+ )?;
storage[0].write(ValRaw::u32(rc));
@@ -615,24 +641,28 @@ fn typecheck_async(host_async: bool, wit_async: bool) -> Result<()> {
struct StaticHostFn<F, const ASYNC: bool>(F);
impl<F, const ASYNC: bool> StaticHostFn<F, ASYNC> {
- fn new<T, P, R>(func: F) -> Self
+ fn new<T, P, R, O>(func: F) -> Self
where
T: 'static,
P: ComponentNamedList + Lift + 'static,
R: ComponentNamedList + Lower + 'static,
- F: Fn(StoreContextMut<'_, T>, P) -> HostResult<R>,
+ F: Fn(StoreContextMut<'_, T>, P) -> O,
+ O: HostOutput<R>,
{
Self(func)
}
}
-impl<T, F, P, R, const ASYNC: bool> HostFn<T, P, R> for StaticHostFn<F, ASYNC>
+impl<T, F, P, R, O, const ASYNC: bool> HostFn<T, P, R> for StaticHostFn<F, ASYNC>
where
T: 'static,
- F: Fn(StoreContextMut<'_, T>, P) -> HostResult<R>,
+ F: Fn(StoreContextMut<'_, T>, P) -> O,
+ O: HostOutput<R>,
P: ComponentNamedList + Lift + 'static,
R: ComponentNamedList + Lower + 'static,
{
+ type Output = O;
+
fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {
let ty = &types.types[ty];
typecheck_async(ASYNC, ty.async_)?;
@@ -643,7 +673,7 @@ where
Ok(())
}
- fn run(&self, store: StoreContextMut<'_, T>, params: P) -> HostResult<R> {
+ fn run(&self, store: StoreContextMut<'_, T>, params: P) -> O {
(self.0)(store, params)
}
@@ -694,21 +724,25 @@ where
struct DynamicHostFn<F, const ASYNC: bool>(F);
impl<F, const ASYNC: bool> DynamicHostFn<F, ASYNC> {
- fn new<T>(func: F) -> Self
+ fn new<T, O>(func: F) -> Self
where
T: 'static,
- F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> HostResult<Vec<Val>>,
+ F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> O,
+ O: HostOutput<Vec<Val>>,
{
Self(func)
}
}
-impl<T, F, const ASYNC: bool> HostFn<T, (ComponentFunc, Vec<Val>), Vec<Val>>
+impl<T, F, O, const ASYNC: bool> HostFn<T, (ComponentFunc, Vec<Val>), Vec<Val>>
for DynamicHostFn<F, ASYNC>
where
T: 'static,
- F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> HostResult<Vec<Val>>,
+ F: Fn(StoreContextMut<'_, T>, ComponentFunc, Vec<Val>, usize) -> O,
+ O: HostOutput<Vec<Val>>,
{
+ type Output = O;
+
/// This function performs dynamic type checks on its parameters and
/// results and subsequently does not need to perform up-front type
/// checks. However, we _do_ verify async-ness here.
@@ -717,11 +751,7 @@ where
typecheck_async(ASYNC, ty.async_)
}
- fn run(
- &self,
- store: StoreContextMut<'_, T>,
- (ty, mut params): (ComponentFunc, Vec<Val>),
- ) -> HostResult<Vec<Val>> {
+ fn run(&self, store: StoreContextMut<'_, T>, (ty, mut params): (ComponentFunc, Vec<Val>)) -> O {
let offset = params.len();
for _ in 0..ty.results().len() {
params.push(Val::Bool(false));