Skip to content

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

Fix caller context on fused component<->component returns (#12737)

Alex Crichton039ae2af85ed
diff --git a/crates/wasmtime/src/runtime/component/concurrent.rs b/crates/wasmtime/src/runtime/component/concurrent.rs
index bdd3579ee4..fd73e5622e 100644
--- a/crates/wasmtime/src/runtime/component/concurrent.rs
+++ b/crates/wasmtime/src/runtime/component/concurrent.rs
@@ -2507,6 +2507,14 @@ impl Instance {
if let ResultInfo::Heap { results } = &result_info {
my_src.push(ValRaw::u32(*results));
}
+
+ // Execute the `return_` hook, generated by Wasmtime's FACT
+ // compiler, in the context of the old thread. The old
+ // thread, this thread's caller, may have `realloc`
+ // callbacks invoked for example and those need the correct
+ // context set for the current thread.
+ let prev = store.0.set_thread(old_thread)?;
+
// SAFETY: `return_` is a valid `*mut VMFuncRef` from
// `wasmtime-cranelift`-generated fused adapter code. Based
// on how it was constructed (see
@@ -2520,6 +2528,11 @@ impl Instance {
my_src.as_mut_slice().into(),
)?;
}
+
+ // Restore the previous current thread after the
+ // lifting/lowering has returned.
+ store.0.set_thread(prev)?;
+
let state = store.0.concurrent_state_mut();
let thread = state.current_guest_thread()?;
if sync_caller {
diff --git a/tests/misc_testsuite/component-model/async/task-builtins.wast b/tests/misc_testsuite/component-model/async/task-builtins.wast
index 9fec3e1bf7..5476fb8222 100644
--- a/tests/misc_testsuite/component-model/async/task-builtins.wast
+++ b/tests/misc_testsuite/component-model/async/task-builtins.wast
@@ -381,9 +381,15 @@
(instance $a (instantiate $A))
(instance $b (instantiate $B (with "a" (instance $a))))
(export "sync-to-sync" (func $b "sync-to-sync"))
+ (export "sync-to-async" (func $b "sync-to-async"))
+ (export "async-to-sync" (func $b "async-to-sync"))
+ (export "async-to-async" (func $b "async-to-async"))
)
(assert_return (invoke "sync-to-sync"))
+(assert_return (invoke "sync-to-async"))
+(assert_return (invoke "async-to-sync"))
+(assert_return (invoke "async-to-async"))
;; Same as above, but when calling the host.
(component