Skip to content

Commit 039ae2a

Browse files
authored
Fix caller context on fused component<->component returns (bytecodealliance#12737)
In bytecodealliance#12718 I added a test for more cases but forgot to update the `*.wast` to actually execute the test. Turns out all the cases were failing, and so this commit properly enables the tests and then fixes them.
1 parent fe6f7a4 commit 039ae2a

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

crates/wasmtime/src/runtime/component/concurrent.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,14 @@ impl Instance {
25072507
if let ResultInfo::Heap { results } = &result_info {
25082508
my_src.push(ValRaw::u32(*results));
25092509
}
2510+
2511+
// Execute the `return_` hook, generated by Wasmtime's FACT
2512+
// compiler, in the context of the old thread. The old
2513+
// thread, this thread's caller, may have `realloc`
2514+
// callbacks invoked for example and those need the correct
2515+
// context set for the current thread.
2516+
let prev = store.0.set_thread(old_thread)?;
2517+
25102518
// SAFETY: `return_` is a valid `*mut VMFuncRef` from
25112519
// `wasmtime-cranelift`-generated fused adapter code. Based
25122520
// on how it was constructed (see
@@ -2520,6 +2528,11 @@ impl Instance {
25202528
my_src.as_mut_slice().into(),
25212529
)?;
25222530
}
2531+
2532+
// Restore the previous current thread after the
2533+
// lifting/lowering has returned.
2534+
store.0.set_thread(prev)?;
2535+
25232536
let state = store.0.concurrent_state_mut();
25242537
let thread = state.current_guest_thread()?;
25252538
if sync_caller {

tests/misc_testsuite/component-model/async/task-builtins.wast

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,9 +381,15 @@
381381
(instance $a (instantiate $A))
382382
(instance $b (instantiate $B (with "a" (instance $a))))
383383
(export "sync-to-sync" (func $b "sync-to-sync"))
384+
(export "sync-to-async" (func $b "sync-to-async"))
385+
(export "async-to-sync" (func $b "async-to-sync"))
386+
(export "async-to-async" (func $b "async-to-async"))
384387
)
385388

386389
(assert_return (invoke "sync-to-sync"))
390+
(assert_return (invoke "sync-to-async"))
391+
(assert_return (invoke "async-to-sync"))
392+
(assert_return (invoke "async-to-async"))
387393

388394
;; Same as above, but when calling the host.
389395
(component

0 commit comments

Comments
 (0)