A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
component: Lower without fiber (#14468)
Adam Bratschi-Kaye61096c594d43
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 0ec1699138..c0101473f1 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -3508,6 +3508,7 @@ impl Instance {self,mut store: StoreContextMut<'_, T>,host_task: EnteredHostTask,+ result_may_require_realloc: bool,future: impl Future<Output = Result<R>> + Send + 'static,lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>+ Send@@ -3600,17 +3601,23 @@ impl Instance {Ok(())};- // Here we schedule a task to run on a worker fiber to do the- // lowering since it may involve a call to the guest's realloc- // function. This is necessary because calling the guest while- // there are host embedder frames on the stack is unsound.tls::get(move |store| {- store- .concurrent_state_mut()?- .push_high_priority(WorkItem::WorkerFunction(AlwaysMut::new(Box::new(- on_complete,- ))));- Ok(())+ if result_may_require_realloc {+ // Lowering may call the guest's realloc function, so run+ // it on a worker fiber. This is required because the guest+ // might try to suspend the fiber which is unsound when it's+ // not actually on a fiber.+ store+ .concurrent_state_mut()?+ .push_high_priority(WorkItem::WorkerFunction(AlwaysMut::new(Box::new(+ on_complete,+ ))));+ Ok(())+ } else {+ // No guest code can be called while lowering this result,+ // so avoid scheduling a worker fiber.+ on_complete(store)+ }})});diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rsindex c5ffa359e1..0aee43d5b9 100644--- a/crates/wasmtime/src/runtime/component/func/host.rs+++ b/crates/wasmtime/src/runtime/component/func/host.rs@@ -74,6 +74,7 @@ trait HostOutput<R> {store: StoreContextMut<'_, T>,instance: Instance,host_task: EnteredHostTask,+ may_require_realloc: bool,lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>+ Send+ 'static,@@ -91,6 +92,7 @@ impl<R> HostOutput<R> for Result<R> {store: StoreContextMut<'_, T>,_instance: Instance,_host_task: EnteredHostTask,+ _may_require_realloc: bool,lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>+ Send+ 'static,@@ -113,11 +115,12 @@ impl<R: Send + Sync + 'static> HostOutput<R> for HostFuture<R> {store: StoreContextMut<'_, T>,instance: Instance,host_task: EnteredHostTask,+ may_require_realloc: bool,lower: impl FnOnce(StoreContextMut<T>, Option<R>, bool, Option<TableId<HostTask>>) -> Result<()>+ Send+ 'static,) -> Result<u32> {- instance.first_poll(store, host_task, self, lower)+ instance.first_poll(store, host_task, may_require_realloc, self, lower)}}@@ -328,6 +331,11 @@ whereT: 'static,R: Send + Sync + 'static,{+ /// How to determine whether lowering this function's result might call the+ /// guest's `realloc` function. True is the conservative choice.+ #[cfg(feature = "component-model-async")]+ const RESULT_MAY_REQUIRE_REALLOC: bool = true;+type Output: HostOutput<R>;/// Performs a type-check to ensure that this host function can be imported@@ -518,8 +526,13 @@ wherestore.as_context_mut(),instance,entered_host_task,+ Self::RESULT_MAY_REQUIRE_REALLOC,move |store, ret, immediate, materialized_host_task| {- let mut lower = LowerContext::new(store, options, instance);+ let mut lower = if Self::RESULT_MAY_REQUIRE_REALLOC {+ LowerContext::new(store, options, instance)+ } else {+ LowerContext::new_without_realloc(store, options, instance)+ };lower.validate_scope_exit()?;if immediate {lower@@ -661,6 +674,9 @@ whereP: ComponentNamedList + Lift + 'static,R: ComponentNamedList + Lower + 'static,{+ #[cfg(feature = "component-model-async")]+ const RESULT_MAY_REQUIRE_REALLOC: bool = R::MAY_REQUIRE_REALLOC;+type Output = O;fn typecheck(ty: TypeFuncIndex, types: &InstanceType<'_>) -> Result<()> {