A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Reject async-typed functions in `Func::call` (#14954)
Alex Crichton7a3435824cc3
diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rsindex c22d37b00f..8d4e9cdb80 100644--- a/crates/wasmtime/src/runtime/component/func.rs+++ b/crates/wasmtime/src/runtime/component/func.rs@@ -245,7 +245,7 @@ impl Func {/// * The `store` used requires the use of [`Func::call_async`] instead. See/// [store documentation](crate#async) for more information./// * The call is recursive from within e.g. a `call_async` invocation.- /// * If the function is async-lifted, in which case either+ /// * If the function is async-typed or async-lifted, in which case either/// [`Self::call_async`] or [`Self::call_concurrent`] must be used.////// See [`TypedFunc::call`] for more information in addition to@@ -340,9 +340,9 @@ impl Func {self.check_params_results(store.as_context_mut(), params, results)?;- if self.abi_async(store.0) {+ if self.is_async(store.0) {bail!(- "cannot synchronously call an async-lifted export; \+ "cannot synchronously call an async function; \use `call_async` or `call_concurrent` instead");}@@ -398,11 +398,13 @@ impl Func {self.unsafe_func_ref.as_non_null()}- pub(crate) fn abi_async(&self, store: &StoreOpaque) -> bool {+ /// Returns whether this function is async-typed or async-lifted, either of+ /// which means it can't be called synchronously.+ pub(crate) fn is_async(&self, store: &StoreOpaque) -> bool {let instance = self.instance.id().get(store);let component = instance.component();- let (_ty, _def, options) = component.export_lifted_function(self.index);- component.env_component().options[options].async_+ let (ty, _def, options) = component.export_lifted_function(self.index);+ component.types()[ty].async_ || component.env_component().options[options].async_}pub(crate) fn abi_info<'a>(diff --git a/crates/wasmtime/src/runtime/component/func/typed.rs b/crates/wasmtime/src/runtime/component/func/typed.rsindex 56c8b30184..7957e1fbbc 100644--- a/crates/wasmtime/src/runtime/component/func/typed.rs+++ b/crates/wasmtime/src/runtime/component/func/typed.rs@@ -129,7 +129,7 @@ where/// * If `store` requires using [`Self::call_async`] instead, see/// [crate documentation](crate#async) for more info./// * The call is recursive from within e.g. a `call_async` invocation.- /// * If the function is async-lifted, in which case either+ /// * If the function is async-typed or async-lifted, in which case either/// [`Self::call_async`] or [`Self::call_concurrent`] must be used.////// In general there are many ways that things could go wrong when copying@@ -212,9 +212,9 @@ wherefn call_impl(&self, mut store: impl AsContextMut, params: Params) -> Result<Return> {let mut store = store.as_context_mut();- if self.func.abi_async(store.0) {+ if self.func.is_async(store.0) {bail!(- "cannot synchronously call an async-lifted export; \+ "cannot synchronously call an async function; \use `call_async` or `call_concurrent` instead");}diff --git a/tests/all/component_model/async.rs b/tests/all/component_model/async.rsindex e8085ab4c6..882a4ef95d 100644--- a/tests/all/component_model/async.rs+++ b/tests/all/component_model/async.rs@@ -2769,11 +2769,15 @@ fn sync_call_async_typed() -> Result<()> {(call $task.return (i32.const 42))(i32.const 0 (; EXIT ;)))(func (export "run-cb") (param i32 i32 i32) (result i32)- unreachable))+ unreachable)+ (func (export "run-sync-lifted") (result i32)+ (i32.const 42)))(core instance $i (instantiate $m(with "" (instance (export "task.return" (func $task.return))))))(func (export "run") async (result u32)(canon lift (core func $i "run") async (callback (core func $i "run-cb"))))+ (func (export "run-sync-lifted") async (result u32)+ (canon lift (core func $i "run-sync-lifted"))))"#;@@ -2782,26 +2786,26 @@ fn sync_call_async_typed() -> Result<()> {let mut store = Store::new(&engine, ());let instance = Linker::new(&engine).instantiate(&mut store, &component)?;- let func = instance.get_func(&mut store, "run").unwrap();- let mut results = [Val::U32(0)];- let error = func.call(&mut store, &[], &mut results).unwrap_err();- assert!(- error- .to_string()- .contains("cannot synchronously call an async-lifted export"),- "{error}"- );+ for name in ["run", "run-sync-lifted"] {+ let func = instance.get_func(&mut store, name).unwrap();+ let mut results = [Val::U32(0)];+ let error = func.call(&mut store, &[], &mut results).unwrap_err();+ assert!(+ error+ .to_string()+ .contains("cannot synchronously call an async function"),+ "{error}"+ );- let func = instance- .get_typed_func::<(), (u32,)>(&mut store, "run")- .unwrap();- let error = func.call(&mut store, ()).unwrap_err();- assert!(- error- .to_string()- .contains("cannot synchronously call an async-lifted export"),- "{error}"- );+ let func = instance.get_typed_func::<(), (u32,)>(&mut store, name)?;+ let error = func.call(&mut store, ()).unwrap_err();+ assert!(+ error+ .to_string()+ .contains("cannot synchronously call an async function"),+ "{error}"+ );+ }Ok(())}