A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
concurrent: Clear may_leave in missing places (#14739)
Alex Crichtonc8a437d023fe
diff --git a/crates/wasmtime/src/runtime/component/concurrent/func.rs b/crates/wasmtime/src/runtime/component/concurrent/func.rsindex d5d58cd638..31285d1219 100644--- a/crates/wasmtime/src/runtime/component/concurrent/func.rs+++ b/crates/wasmtime/src/runtime/component/concurrent/func.rs@@ -185,7 +185,7 @@ impl Func {params: Vec<Val>,) -> Result<PreparedCall<Vec<Val>>> {let store = store.as_context_mut();- let (options, flags, ty, raw_options) = self.abi_info(store.0);+ let (options, _, ty, raw_options) = self.abi_info(store.0);let async_ = raw_options.async_;let instance = self.instance();@@ -194,7 +194,7 @@ impl Func {self,MAX_FLAT_PARAMS,move |store, params_out| {- Func::with_lower_context(instance, store, options, flags, ty, |cx, ty| {+ Func::with_lower_context(instance, store, options, ty, |cx, ty| {Self::lower_args(cx, ¶ms, ty, params_out)})},@@ -438,7 +438,7 @@ where} else {1};- let (options, flags, ty, raw_options) = self.func().abi_info(store.0);+ let (options, _, ty, raw_options) = self.func().abi_info(store.0);let instance = self.func().instance();let max_results = if raw_options.async_ {MAX_FLAT_PARAMS@@ -451,7 +451,7 @@ where*self.func(),param_count,move |store, params_out| {- Func::with_lower_context(instance, store, options, flags, ty, |cx, ty| {+ Func::with_lower_context(instance, store, options, ty, |cx, ty| {lower(cx, ty, params_out)})},diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rsindex 00f83dec9c..e758653311 100644--- a/crates/wasmtime/src/runtime/component/func.rs+++ b/crates/wasmtime/src/runtime/component/func.rs@@ -501,7 +501,6 @@ impl Func {self.instance,store.as_context_mut(),options_idx,- flags,ty,|cx, ty| lower(cx, ty, map_maybe_uninit!(space.params)),)?;@@ -684,21 +683,12 @@ impl Func {instance: Instance,mut store: StoreContextMut<T>,options: OptionsIndex,- mut flags: InstanceFlags,ty: TypeFuncIndex,lower: impl FnOnce(&mut LowerContext<T>, InterfaceType) -> Result<()>,) -> Result<()> {- // Perform the actual lowering, where while this is running the- // component is forbidden from calling imports.- unsafe {- debug_assert!(flags.may_leave());- flags.set_may_leave(false);- }let mut cx = LowerContext::new(store.as_context_mut(), options, instance);let param_ty = InterfaceType::Tuple(cx.types[ty].params);- let result = lower(&mut cx, param_ty);- unsafe { flags.set_may_leave(true) };- result+ lower(&mut cx, param_ty)}/// Creates a `LiftContext` using the provided configuration values and runsdiff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rsindex 0aee43d5b9..c893701b3b 100644--- a/crates/wasmtime/src/runtime/component/func/host.rs+++ b/crates/wasmtime/src/runtime/component/func/host.rs@@ -476,7 +476,7 @@ where.store.0.host_task_delete(entered_host_task, materialized_host_task)?;- Self::lower_raw(&mut lower, ty, ret, dst)+ Self::lower_result(&mut lower, ty, ret, dst)}/// Implementation of the "async" ABI of the component model.@@ -546,7 +546,7 @@ where// 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))?;+ Self::lower_result(&mut lower, ty, result, Destination::Memory(retptr))?;}Ok(())},@@ -594,24 +594,6 @@ wherelet params = Self::lift_params(lift, ty, src)?;Ok((params, &storage[param_flat_count.unwrap_or(1)..]))}-- fn lower_raw(- lower: &mut LowerContext<'_, T>,- ty: TypeFuncIndex,- ret: R,- dst: Destination<'_>,- ) -> Result<()> {- let caller_instance = lower.options().instance;- let mut flags = lower.instance_mut().instance_flags(caller_instance);- unsafe {- flags.set_may_leave(false);- }- Self::lower_result(lower, ty, ret, dst)?;- unsafe {- flags.set_may_leave(true);- }- Ok(())- }}/// Checks that a host function's `ASYNC`-ness (as encoded by which ofdiff --git a/crates/wasmtime/src/runtime/component/func/options.rs b/crates/wasmtime/src/runtime/component/func/options.rsindex de59c9f47c..c88481611c 100644--- a/crates/wasmtime/src/runtime/component/func/options.rs+++ b/crates/wasmtime/src/runtime/component/func/options.rs@@ -168,6 +168,7 @@ impl<'a, T: 'static> LowerContext<'a, T> {CanonicalOptionsDataModel::LinearMemory(m) => m.realloc.unwrap(),};let realloc = instance.runtime_realloc(realloc);+ let mut flags = instance.instance_flags(options.instance);let params = (u32::try_from(old)?,@@ -180,14 +181,21 @@ impl<'a, T: 'static> LowerContext<'a, T> {// Invoke the wasm malloc function using its raw and statically known// signature.+ //+ // Per the canonical ABI `realloc` always runs with `may_leave` cleared+ // so it can't call imports or intrinsics.let result = unsafe {- ReallocFunc::call_raw(+ let prev_may_leave = flags.may_leave();+ flags.set_may_leave(false);+ let result = ReallocFunc::call_raw(&mut StoreContextMut(store),&realloc_ty,realloc,params,UncaughtException::Trap,- )?+ );+ flags.set_may_leave(prev_may_leave);+ result?};if result % old_align != 0 {diff --git a/tests/misc_testsuite/component-model/async/realloc-may-leave.wast b/tests/misc_testsuite/component-model/async/realloc-may-leave.wastnew file mode 100644index 0000000000..b0ecc96049--- /dev/null+++ b/tests/misc_testsuite/component-model/async/realloc-may-leave.wast@@ -0,0 +1,202 @@+;;! component_model_async = true+;;! component_model_error_context = true+;;! multi_memory = true++;; Per the canonical ABI, `realloc` always runs with `may_leave` cleared, so it+;; can't call imports or intrinsics. Each component below reaches `realloc` via+;; a different path, and in every case `realloc` calls the host's+;; `host-return-two`, which must trap.++;; host -> guest: lowering params into a sync-lifted export.+(component+ (import "host-return-two" (func $h (result u32)))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core module $M (func (export "f") (param i32 i32)))+ (core instance $m (instantiate $M))+ (func (export "f") (param "s" string)+ (canon lift (core func $m "f")+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+)+(assert_trap (invoke "f" (str.const "x")) "cannot leave component instance")++;; host -> guest: lowering params into an async-lifted export.+(component+ (import "host-return-two" (func $h (result u32)))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core module $M+ (func (export "f") (param i32 i32) (result i32) unreachable)+ (func (export "cb") (param i32 i32 i32) (result i32) unreachable))+ (core instance $m (instantiate $M))+ (func (export "f") async (param "s" string)+ (canon lift (core func $m "f") async (callback (core func $m "cb"))+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+)+(assert_trap (invoke "f" (str.const "x")) "cannot leave component instance")++;; host import -> guest: lowering a host function's results.+(component+ (import "host-return-two" (func $h (result u32)))+ (import "host" (instance $host (export "return-hi" (func (result string)))))+ (alias export $host "return-hi" (func $hi))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core func $hi (canon lower (func $hi)+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+ (core module $M+ (import "" "hi" (func $hi (param i32)))+ (func (export "run") (call $hi (i32.const 8))))+ (core instance $m (instantiate $M (with "" (instance (export "hi" (func $hi))))))+ (func (export "run") (canon lift (core func $m "run")))+)+(assert_trap (invoke "run") "cannot leave component instance")++;; guest -> guest: copying a `future<string>` value into the reader. The writer+;; writes first, so the reader's `future.read` completes immediately.+(component+ (import "host-return-two" (func $h (result u32)))+ (component $C+ (import "host-return-two" (func $h (result u32)))+ (type $FT (future string))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core func $future.read (canon future.read $FT async+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+ (core module $M+ (import "" "future.read" (func $future.read (param i32 i32) (result i32)))+ (func (export "read") (param i32) (drop (call $future.read (local.get 0) (i32.const 8)))))+ (core instance $m (instantiate $M (with "" (instance+ (export "future.read" (func $future.read))))))+ (func (export "read") (param "f" (future string)) (canon lift (core func $m "read")))+ )+ (component $D+ (import "read" (func $read (param "f" (future string))))+ (type $FT (future string))+ (core module $Mem+ (memory (export "mem") 1)+ (data (i32.const 4) "\01")) ;; string at 0: ptr = 0, len = 1+ (core instance $mem (instantiate $Mem))+ (core func $read (canon lower (func $read)))+ (core func $future.new (canon future.new $FT))+ (core func $future.write (canon future.write $FT async (memory (core memory $mem "mem"))))+ (core module $M+ (import "" "read" (func $read (param i32)))+ (import "" "future.new" (func $future.new (result i64)))+ (import "" "future.write" (func $future.write (param i32 i32) (result i32)))+ (func (export "run")+ (drop (call $future.new)) ;; readable = 1, writable = 2+ (drop (call $future.write (i32.const 2) (i32.const 0)))+ (call $read (i32.const 1))))+ (core instance $m (instantiate $M (with "" (instance+ (export "read" (func $read))+ (export "future.new" (func $future.new))+ (export "future.write" (func $future.write))))))+ (func (export "run") (canon lift (core func $m "run")))+ )+ (instance $c (instantiate $C (with "host-return-two" (func $h))))+ (instance $d (instantiate $D (with "read" (func $c "read"))))+ (export "run" (func $d "run"))+)+(assert_trap (invoke "run") "cannot leave component instance")++;; guest -> guest: copying a `stream<string>` element into the reader. The+;; writer writes first, so the reader's `stream.read` completes immediately.+(component+ (import "host-return-two" (func $h (result u32)))+ (component $C+ (import "host-return-two" (func $h (result u32)))+ (type $ST (stream string))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core func $stream.read (canon stream.read $ST async+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+ (core module $M+ (import "" "stream.read" (func $stream.read (param i32 i32 i32) (result i32)))+ (func (export "read") (param i32)+ (drop (call $stream.read (local.get 0) (i32.const 8) (i32.const 1)))))+ (core instance $m (instantiate $M (with "" (instance+ (export "stream.read" (func $stream.read))))))+ (func (export "read") (param "s" (stream string)) (canon lift (core func $m "read")))+ )+ (component $D+ (import "read" (func $read (param "s" (stream string))))+ (type $ST (stream string))+ (core module $Mem+ (memory (export "mem") 1)+ (data (i32.const 4) "\01")) ;; string at 0: ptr = 0, len = 1+ (core instance $mem (instantiate $Mem))+ (core func $read (canon lower (func $read)))+ (core func $stream.new (canon stream.new $ST))+ (core func $stream.write (canon stream.write $ST async (memory (core memory $mem "mem"))))+ (core module $M+ (import "" "read" (func $read (param i32)))+ (import "" "stream.new" (func $stream.new (result i64)))+ (import "" "stream.write" (func $stream.write (param i32 i32 i32) (result i32)))+ (func (export "run")+ (drop (call $stream.new)) ;; readable = 1, writable = 2+ (drop (call $stream.write (i32.const 2) (i32.const 0) (i32.const 1)))+ (call $read (i32.const 1))))+ (core instance $m (instantiate $M (with "" (instance+ (export "read" (func $read))+ (export "stream.new" (func $stream.new))+ (export "stream.write" (func $stream.write))))))+ (func (export "run") (canon lift (core func $m "run")))+ )+ (instance $c (instantiate $C (with "host-return-two" (func $h))))+ (instance $d (instantiate $D (with "read" (func $c "read"))))+ (export "run" (func $d "run"))+)+(assert_trap (invoke "run") "cannot leave component instance")++;; `error-context.debug-message`: lowering the message string into the caller.+(component+ (import "host-return-two" (func $h (result u32)))+ (core func $h (canon lower (func $h)))+ (core module $Libc+ (import "" "h" (func $h (result i32)))+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (drop (call $h)) (i32.const 0)))+ (core instance $libc (instantiate $Libc (with "" (instance (export "h" (func $h))))))+ (core func $error-context.new (canon error-context.new (memory (core memory $libc "mem"))))+ (core func $error-context.debug-message (canon error-context.debug-message+ (memory (core memory $libc "mem")) (realloc (core func $libc "realloc"))))+ (core module $M+ (import "" "error-context.new" (func $error-context.new (param i32 i32) (result i32)))+ (import "" "error-context.debug-message" (func $error-context.debug-message (param i32 i32)))+ (func (export "run")+ (call $error-context.debug-message+ (call $error-context.new (i32.const 0) (i32.const 1))+ (i32.const 8))))+ (core instance $m (instantiate $M (with "" (instance+ (export "error-context.new" (func $error-context.new))+ (export "error-context.debug-message" (func $error-context.debug-message))))))+ (func (export "run") (canon lift (core func $m "run")))+)+(assert_trap (invoke "run") "cannot leave component instance")