A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Fix the current guest task when calling `realloc` (#12718)
Alex Crichton58877f2f175a
diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rsindex e729b1a032..8757bcb26a 100644--- a/crates/environ/src/fact/trampoline.rs+++ b/crates/environ/src/fact/trampoline.rs@@ -840,6 +840,25 @@ impl<'a, 'b> Compiler<'a, 'b> {}result_locals.reverse();+ // Handle a few things related to the concurrent task infrastructure+ // after the callee has finished, such as:+ //+ // * Validate that the callee dropped all its borrows+ // * Transition the current running task back to the caller.+ //+ // This is not necessary if there are no resources in this call, nor if+ // concurrency support is disabled (no tasks). Note that this must+ // happen before lowering below because semantically that's where the+ // "you forgot to drop borrows" trap shows up and additionally the+ // lowering below may call realloc which is in the context of the+ // caller's task, not the callee.+ //+ // FIXME: Apply the optimizations described in #12311.+ if self.emit_resource_call || self.module.tunables.concurrency_support {+ let exit_sync_call = self.module.import_exit_sync_call();+ self.instruction(Call(exit_sync_call.as_u32()));+ }+// Like above during the translation of results the caller cannot be// left (as we might invoke things like `realloc`). Again the precise// order of everything doesn't matter since intermediate states cannot@@ -865,14 +884,6 @@ impl<'a, 'b> Compiler<'a, 'b> {self.free_temp_local(tmp);}- if self.emit_resource_call || self.module.tunables.concurrency_support {- // Pop the task we pushed earlier off of the current task stack.- //- // FIXME: Apply the optimizations described in #12311.- let exit_sync_call = self.module.import_exit_sync_call();- self.instruction(Call(exit_sync_call.as_u32()));- }-if self.module.tunables.concurrency_support {// Restore old `may_block_field`if let Some(old_task_may_block) = old_task_may_block {diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rsindex 66c6c8458c..1740522c73 100644--- a/crates/wasmtime/src/runtime/component/concurrent.rs+++ b/crates/wasmtime/src/runtime/component/concurrent.rs@@ -1547,34 +1547,50 @@ impl StoreOpaque {/// relatively expensive table manipulations. This would ideally be/// optimized to avoid the full allocation of a `HostTask` in at least some/// situations.- pub fn enter_host_call(&mut self) -> Result<()> {+ pub(crate) fn host_task_create(&mut self) -> Result<Option<TableId<HostTask>>> {if !self.concurrency_support() {self.enter_call_not_concurrent();- return Ok(());+ return Ok(None);}let state = self.concurrent_state_mut();let caller = state.current_guest_thread()?;let task = state.push(HostTask::new(caller, HostTaskState::CalleeStarted))?;log::trace!("new host task {task:?}");self.set_thread(task)?;+ Ok(Some(task))+ }++ /// Invoked before lowering the results of a host task to the guest.+ ///+ /// This is used to update the current thread annotations within the store+ /// to ensure that it reflects the guest task, not the host task, since+ /// lowering may execute guest code.+ pub fn host_task_reenter_caller(&mut self) -> Result<()> {+ if !self.concurrency_support() {+ return Ok(());+ }+ let task = self.concurrent_state_mut().current_host_thread()?;+ let caller = self.concurrent_state_mut().get_mut(task)?.caller;+ self.set_thread(caller)?;Ok(())}- /// Dual of `enter_host_call` and signifies that the host has finished and+ /// Dual of `host_task_create` and signifies that the host has finished and/// will be cleaned up.////// Note that this isn't invoked when the host is invoked asynchronously and/// the host isn't complete yet. In that situation the host task persists- /// and will be cleaned up separately.- pub fn exit_host_call(&mut self) -> Result<()> {- if !self.concurrency_support() {- self.exit_call_not_concurrent();- return Ok(());+ /// and will be cleaned up separately in `subtask_drop`+ pub(crate) fn host_task_delete(&mut self, task: Option<TableId<HostTask>>) -> Result<()> {+ match task {+ Some(task) => {+ log::trace!("delete host task {task:?}");+ self.concurrent_state_mut().delete(task)?;+ }+ None => {+ self.exit_call_not_concurrent();+ }}- let task = self.concurrent_state_mut().current_host_thread()?;- log::trace!("delete host task {task:?}");- let task = self.concurrent_state_mut().delete(task)?;- self.set_thread(task.caller)?;Ok(())}@@ -4183,7 +4199,7 @@ type HostTaskFuture = Pin<Box<dyn Future<Output = Result<()>> + Send + 'static>>/// Represents the state of a pending host task.////// This is used to represent tasks when the guest calls into the host.-struct HostTask {+pub(crate) struct HostTask {common: WaitableCommon,/// Guest thread which called the host.diff --git a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs b/crates/wasmtime/src/runtime/component/concurrent_disabled.rsindex 0cd145fe11..1a61387df3 100644--- a/crates/wasmtime/src/runtime/component/concurrent_disabled.rs+++ b/crates/wasmtime/src/runtime/component/concurrent_disabled.rs@@ -175,11 +175,15 @@ impl StoreOpaque {Ok(self.exit_call_not_concurrent())}- pub(crate) fn enter_host_call(&mut self) -> Result<()> {+ pub(crate) fn host_task_create(&mut self) -> Result<()> {Ok(self.enter_call_not_concurrent())}- pub(crate) fn exit_host_call(&mut self) -> Result<()> {+ pub(crate) fn host_task_reenter_caller(&mut self) -> Result<()> {+ Ok(())+ }++ pub(crate) fn host_task_delete(&mut self, (): ()) -> Result<()> {Ok(self.exit_call_not_concurrent())}diff --git a/crates/wasmtime/src/runtime/component/func.rs b/crates/wasmtime/src/runtime/component/func.rsindex c22189c16b..c506d73e33 100644--- a/crates/wasmtime/src/runtime/component/func.rs+++ b/crates/wasmtime/src/runtime/component/func.rs@@ -649,6 +649,13 @@ impl Func {)?;}+ // Validate that the task, after returning, has no more active borrows+ // as they're required to have been dropped by this point.+ store+ .0+ .component_resource_tables(Some(self.instance))+ .validate_scope_exit()?;+// SAFETY: We're relying on the correctness of the structure of// `LowerReturn` and the type-checking performed to acquire the// `TypedFunc` to make this safe. It should be the case that@@ -706,11 +713,6 @@ impl Func {unsafe {call_post_return(&mut store, post_return, arg, flags)?;-- store- .0- .component_resource_tables(Some(self.instance))- .validate_scope_exit()?;store.0.exit_guest_sync_call()?;}Ok(())diff --git a/crates/wasmtime/src/runtime/component/func/host.rs b/crates/wasmtime/src/runtime/component/func/host.rsindex d0e5038c00..68e8b92014 100644--- a/crates/wasmtime/src/runtime/component/func/host.rs+++ b/crates/wasmtime/src/runtime/component/func/host.rs@@ -378,9 +378,9 @@ where}// Enter the host by pushing a `HostTask` into the concurrent state.- store.0.enter_host_call()?;+ let host_task = store.0.host_task_create()?;- let task_exited = if async_ {+ let host_task_complete = if async_ {#[cfg(feature = "component-model-async")]{self.call_async_lower(store.as_context_mut(), instance, ty, options, storage)?@@ -395,13 +395,13 @@ wheretrue};- // If the host task exited, then it's popped and deallocated.+ // If the host task completed, then it's deallocated.//// Note that if the host task did not exit then the `call_async_lower`// function transitively would have updated the current guest thread to// the caller of this host function.- if task_exited {- store.0.exit_host_call()?;+ if host_task_complete {+ store.0.host_task_delete(host_task)?;}Ok(())@@ -571,6 +571,16 @@ whereret: Option<R>,dst: Destination<'_>,) -> Result<()> {+ // Before lowering below semantically ensure that the caller has dropped+ // all of its borrows and such.+ lower.validate_scope_exit()?;++ // At this point we're transitioning back to the caller task which means+ // that the current task needs to be updated. This will restore the+ // currently running thread as the caller's thread, for example if+ // lowering below calls `realloc` it'll use the right context.+ lower.store.0.host_task_reenter_caller()?;+if let Some(ret) = ret {let caller_instance = lower.options().instance;let mut flags = lower.instance_mut().instance_flags(caller_instance);@@ -582,7 +592,6 @@ whereflags.set_may_leave(true);}}- lower.validate_scope_exit()?;Ok(())}}diff --git a/crates/wast/src/spectest.rs b/crates/wast/src/spectest.rsindex e3e57c98da..f535723951 100644--- a/crates/wast/src/spectest.rs+++ b/crates/wast/src/spectest.rs@@ -218,5 +218,6 @@ pub fn link_component_spectest<T>(linker: &mut component::Linker<T>) -> Result<(Box::pin(async move { std::future::pending::<Result<()>>().await })},)?;+ i.func_wrap("return-hi", |_cx, (): ()| Ok(("hi".to_string(),)))?;Ok(())}diff --git a/tests/all/component_model/resources.rs b/tests/all/component_model/resources.rsindex dd57c35618..9b99e0d766 100644--- a/tests/all/component_model/resources.rs+++ b/tests/all/component_model/resources.rs@@ -814,7 +814,7 @@ fn thread_through_borrow() -> Result<()> {}#[test]-fn cannot_use_borrow_for_own() -> Result<()> {+fn borrows_must_be_dropped_before_lifting() -> Result<()> {let engine = super::engine();let c = Component::new(&engine,@@ -824,6 +824,9 @@ fn cannot_use_borrow_for_own() -> Result<()> {(core module $m(func (export "f") (param i32) (result i32)+ ;; this is a `borrow $t`, and we're returning it as an+ ;; `own $t`. This should have an error about remaining+ ;; borrows rather than a mismatch of own/borrow.local.get 0))@@ -848,7 +851,63 @@ fn cannot_use_borrow_for_own() -> Result<()> {let resource = Resource::new_own(100);let err = f.call(&mut store, (&resource,)).unwrap_err();- assert_eq!(err.to_string(), "cannot lift own resource from a borrow");+ assert_eq!(+ err.to_string(),+ "borrow handles still remain at the end of the call"+ );+ Ok(())+}++#[test]+fn cannot_use_borrow_for_own() -> Result<()> {+ let engine = super::engine();+ let c = Component::new(+ &engine,+ r#"+ (component+ (import "t" (type $t (sub resource)))+ (import "f" (func $f (param "x" (own $t))))++ (core module $m+ (import "" "f" (func $f (param i32)))+ (func (export "f") (param i32)+ local.get 0+ call $f+ )+ )+ (core func $f (canon lower (func $f)))+ (core instance $i (instantiate $m+ (with "" (instance+ (export "f" (func $f))+ ))+ ))++ (func (export "f") (param "x" (borrow $t))+ (canon lift (core func $i "f")))+ )+ "#,+ )?;++ struct MyType;++ let mut store = Store::new(&engine, ());+ let mut linker = Linker::new(&engine);+ linker+ .root()+ .resource("t", ResourceType::host::<MyType>(), |_, _| Ok(()))?;+ linker+ .root()+ .func_wrap("f", |_, _: (Resource<MyType>,)| Ok(()))?;+ let i = linker.instantiate(&mut store, &c)?;++ let f = i.get_typed_func::<(&Resource<MyType>,), ()>(&mut store, "f")?;++ let resource = Resource::new_own(100);+ let err = f.call(&mut store, (&resource,)).unwrap_err();+ assert!(+ format!("{err:?}").contains("cannot lift own resource from a borrow"),+ "bad error: {err:?}",+ );Ok(())}diff --git a/tests/misc_testsuite/component-model/async/task-builtins.wast b/tests/misc_testsuite/component-model/async/task-builtins.wastindex 7dcf6e8cb5..9fec3e1bf7 100644--- a/tests/misc_testsuite/component-model/async/task-builtins.wast+++ b/tests/misc_testsuite/component-model/async/task-builtins.wast@@ -1,4 +1,6 @@;;! component_model_async = true+;;! multi_memory = true+;;! reference_types = true;; backpressure.inc(component@@ -119,3 +121,339 @@)(assert_return (invoke "run"))++;; Test calling intrinsics during `realloc` and their values should be set for+;; when main function is called+(component+ (core func $backpressure.inc (canon backpressure.inc))+ (core func $backpressure.dec (canon backpressure.dec))+ (core func $context.get (canon context.get i32 0))+ (core func $context.set (canon context.set i32 0))++ (core module $libc+ (import "" "backpressure.inc" (func $backpressure.inc))+ (import "" "backpressure.dec" (func $backpressure.dec))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "context.set" (func $context.set (param i32)))++ (memory (export "memory") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (if (i32.ne (local.get 0) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 1) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 2) (i32.const 1)) (then (unreachable)))+ (if (i32.ne (local.get 3) (i32.const 2)) (then (unreachable)))++ call $context.get+ i32.const 0+ i32.ne+ if unreachable end++ i32.const 100+ call $context.set++ call $backpressure.inc+ call $backpressure.dec++ i32.const 200+ )+ )++ (core instance $libc (instantiate $libc (with "" (instance+ (export "backpressure.inc" (func $backpressure.inc))+ (export "backpressure.dec" (func $backpressure.dec))+ (export "context.get" (func $context.get))+ (export "context.set" (func $context.set))+ ))))++ (core module $M+ (import "" "backpressure.inc" (func $backpressure.inc))+ (import "" "backpressure.dec" (func $backpressure.dec))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "context.set" (func $context.set (param i32)))++ (func (export "run") (param i32 i32)+ ;; ptr/len assertion+ (if (i32.ne (local.get 0) (i32.const 200)) (then (unreachable)))+ (if (i32.ne (local.get 1) (i32.const 2)) (then (unreachable)))++ call $backpressure.inc+ call $backpressure.dec++ ;; context.get should be what was set in `realloc`+ (if (i32.ne (call $context.get) (i32.const 100)) (then (unreachable)))+ )+ )+ (core instance $m (instantiate $M (with "" (instance+ (export "backpressure.inc" (func $backpressure.inc))+ (export "backpressure.dec" (func $backpressure.dec))+ (export "context.get" (func $context.get))+ (export "context.set" (func $context.set))+ ))))+ (func (export "run") (param "x" string)+ (canon lift (core func $m "run")+ (realloc (func $libc "realloc"))+ (memory $libc "memory")+ )+ )+)++(assert_return (invoke "run" (str.const "hi")))++;; Test when realloc is called to communicate return values that various+;; intrinsics work and have their expected values.+(component+ (component $A+ (core module $libc (memory (export "memory") 1))+ (core instance $libc (instantiate $libc))++ (core func $task.return (canon task.return (result string) (memory $libc "memory")))++ (core module $m+ (import "" "task.return" (func $task.return (param i32 i32)))+ (import "" "memory" (memory 1))+ (func (export "run-sync") (result i32)+ i32.const 40+ i32.const 100+ i32.store offset=0++ i32.const 40+ i32.const 2+ i32.store offset=4++ i32.const 40+ )+ (func (export "run-async") (result i32)+ i32.const 100+ i32.const 2+ call $task.return+ i32.const 0 ;; CALLBACK_CODE_EXIT+ )++ (func (export "run-async-cb") (param i32 i32 i32) (result i32) unreachable)+ (data (i32.const 100) "hi")+ )+ (core instance $m (instantiate $m+ (with "" (instance+ (export "task.return" (func $task.return))+ (export "memory" (memory $libc "memory"))+ ))+ ))+ (func (export "run-sync") (result string)+ (canon lift (core func $m "run-sync") (memory $libc "memory"))+ )+ (func (export "run-async") async (result string)+ (canon lift (core func $m "run-async") (memory $libc "memory") async+ (callback (func $m "run-async-cb")))+ )+ )++ (component $B+ (import "a" (instance $a+ (export "run-sync" (func (result string)))+ (export "run-async" (func async (result string)))+ ))++ (core func $backpressure.inc (canon backpressure.inc))+ (core func $backpressure.dec (canon backpressure.dec))+ (core func $context.get (canon context.get i32 0))+ (core func $context.set (canon context.set i32 0))++ (core module $libc+ (import "" "backpressure.inc" (func $backpressure.inc))+ (import "" "backpressure.dec" (func $backpressure.dec))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "context.set" (func $context.set (param i32)))++ (memory (export "memory") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (if (i32.ne (local.get 0) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 1) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 2) (i32.const 1)) (then (unreachable)))+ (if (i32.ne (local.get 3) (i32.const 2)) (then (unreachable)))++ (if (i32.ne (call $context.get) (i32.const 400)) (then (unreachable)))+ (call $context.set (i32.const 500))++ call $backpressure.inc+ call $backpressure.dec++ i32.const 200+ )+ )++ (core instance $libc (instantiate $libc (with "" (instance+ (export "backpressure.inc" (func $backpressure.inc))+ (export "backpressure.dec" (func $backpressure.dec))+ (export "context.get" (func $context.get))+ (export "context.set" (func $context.set))+ ))))++ (core func $sync-to-sync+ (canon lower (func $a "run-sync")+ (memory $libc "memory")+ (realloc (func $libc "realloc"))+ )+ )+ (core func $async-to-sync+ (canon lower (func $a "run-sync")+ async+ (memory $libc "memory")+ (realloc (func $libc "realloc"))+ )+ )+ (core func $sync-to-async+ (canon lower (func $a "run-async")+ (memory $libc "memory")+ (realloc (func $libc "realloc"))+ )+ )+ (core func $async-to-async+ (canon lower (func $a "run-async")+ async+ (memory $libc "memory")+ (realloc (func $libc "realloc"))+ )+ )++ (core module $M+ (import "" "context.set" (func $context.set (param i32)))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "sync-to-sync" (func $sync-to-sync (param i32)))+ (import "" "sync-to-async" (func $sync-to-async (param i32)))+ (import "" "async-to-sync" (func $async-to-sync (param i32) (result i32)))+ (import "" "async-to-async" (func $async-to-async (param i32) (result i32)))++ ;; set this tasks's context before calling $run, in calling $run the+ ;; runtime will then call `realloc` above for the string return value+ ;; which should see our 400 value. That will then set 500 which we should+ ;; then see after the return.++ (func (export "sync-to-sync")+ (call $context.set (i32.const 400))+ (call $sync-to-sync (i32.const 20))+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))+ )++ (func (export "sync-to-async")+ (call $context.set (i32.const 400))+ (call $sync-to-async (i32.const 20))+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))+ )++ (func (export "async-to-sync")+ (call $context.set (i32.const 400))+ (if+ (i32.ne+ (call $async-to-sync (i32.const 20))+ (i32.const 2) ;; RETURNED+ )+ (then (unreachable))+ )+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))+ )++ (func (export "async-to-async")+ (call $context.set (i32.const 400))+ (if+ (i32.ne+ (call $async-to-async (i32.const 20))+ (i32.const 2) ;; RETURNED+ )+ (then (unreachable))+ )+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))+ )+ )+ (core instance $m (instantiate $M (with "" (instance+ (export "context.set" (func $context.set))+ (export "context.get" (func $context.get))+ (export "sync-to-sync" (func $sync-to-sync))+ (export "sync-to-async" (func $sync-to-async))+ (export "async-to-sync" (func $async-to-sync))+ (export "async-to-async" (func $async-to-async))+ ))))+ (func (export "sync-to-sync") async (canon lift (core func $m "sync-to-sync")))+ (func (export "sync-to-async") async (canon lift (core func $m "sync-to-async")))+ (func (export "async-to-sync") async (canon lift (core func $m "async-to-sync")))+ (func (export "async-to-async") async (canon lift (core func $m "async-to-async")))+ )++ (instance $a (instantiate $A))+ (instance $b (instantiate $B (with "a" (instance $a))))+ (export "sync-to-sync" (func $b "sync-to-sync"))+)++(assert_return (invoke "sync-to-sync"))++;; Same as above, but when calling the host.+(component+ (import "host" (instance $host+ (export "return-hi" (func (result string)))+ ))++ (core func $backpressure.inc (canon backpressure.inc))+ (core func $backpressure.dec (canon backpressure.dec))+ (core func $context.get (canon context.get i32 0))+ (core func $context.set (canon context.set i32 0))++ (core module $libc+ (import "" "backpressure.inc" (func $backpressure.inc))+ (import "" "backpressure.dec" (func $backpressure.dec))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "context.set" (func $context.set (param i32)))++ (memory (export "memory") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ (if (i32.ne (local.get 0) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 1) (i32.const 0)) (then (unreachable)))+ (if (i32.ne (local.get 2) (i32.const 1)) (then (unreachable)))+ (if (i32.ne (local.get 3) (i32.const 2)) (then (unreachable)))++ (if (i32.ne (call $context.get) (i32.const 400)) (then (unreachable)))+ (call $context.set (i32.const 500))++ call $backpressure.inc+ call $backpressure.dec++ i32.const 200+ )+ )++ (core instance $libc (instantiate $libc (with "" (instance+ (export "backpressure.inc" (func $backpressure.inc))+ (export "backpressure.dec" (func $backpressure.dec))+ (export "context.get" (func $context.get))+ (export "context.set" (func $context.set))+ ))))++ (core func $run+ (canon lower (func $host "return-hi")+ (memory $libc "memory")+ (realloc (func $libc "realloc"))+ )+ )++ (core module $M+ (import "" "context.set" (func $context.set (param i32)))+ (import "" "context.get" (func $context.get (result i32)))+ (import "" "run" (func $run (param i32)))++ (func (export "run")+ ;; set this tasks's context before calling $run, in calling $run the+ ;; runtime will then call `realloc` above for the string return value+ ;; which should see our 400 value. That will then set 500 which we+ ;; should then see after the return.+ (call $context.set (i32.const 400))+ (call $run (i32.const 20))+ (if (i32.ne (call $context.get) (i32.const 500)) (then (unreachable)))+ )+ )+ (core instance $m (instantiate $M (with "" (instance+ (export "context.set" (func $context.set))+ (export "context.get" (func $context.get))+ (export "run" (func $run))+ ))))+ (func (export "run") (canon lift (core func $m "run")))+)++(assert_return (invoke "run"))