Skip to content

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

Handle `may_leave` in post-return in FACT adapters (#14670)

Alex Crichton65c3460aae07
diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rs
index 09ba83ec14..a46029d484 100644
--- a/crates/environ/src/fact/trampoline.rs
+++ b/crates/environ/src/fact/trampoline.rs
@@ -776,14 +776,17 @@ impl<'a, 'b> Compiler<'a, 'b> {
// are all translated.
//
// Note that for this call the callee's previous context is shuffled
- // in-and-then-back-out after the call.
+ // in-and-then-back-out after the call. Additionally the callee cannot
+ // leave its instance during post-return, so `may_leave` is cleared.
if let Some(func) = adapter.lift.post_return {
let caller_context = self.save_context();
self.restore_context(callee_context);
+ let saved_lift_may_leave = self.clear_may_leave(adapter.lift.flags);
for (result, _) in result_locals.iter() {
self.instruction(LocalGet(*result));
}
self.instruction(Call(func.as_u32()));
+ self.restore_may_leave(adapter.lift.flags, saved_lift_may_leave);
self.restore_context(caller_context);
} else {
assert!(callee_context.is_empty());
diff --git a/tests/misc_testsuite/component-model/async/lower-may-leave.wast b/tests/misc_testsuite/component-model/async/lower-may-leave.wast
index 610e0bcbbb..7c9d3602e2 100644
--- a/tests/misc_testsuite/component-model/async/lower-may-leave.wast
+++ b/tests/misc_testsuite/component-model/async/lower-may-leave.wast
@@ -169,3 +169,33 @@
(export "run" (func $a "run")))
(assert_trap (invoke "run") "cannot leave component instance")
+
+;; post-return reached through a guest-to-guest sync call uses a built-in and
+;; that should trap.
+(component
+ (component $C
+ (core module $m
+ (import "" "waitable-set.new" (func $waitable-set.new (result i32)))
+ (func (export "f") (result i32) i32.const 7)
+ (func (export "post") (param i32)
+ (drop (call $waitable-set.new))))
+ (core func $waitable-set.new (canon waitable-set.new))
+ (core instance $i (instantiate $m (with "" (instance
+ (export "waitable-set.new" (func $waitable-set.new))))))
+ (func (export "f") (result u32)
+ (canon lift (core func $i "f") (post-return (core func $i "post")))))
+ (instance $c (instantiate $C))
+
+ (component $B
+ (import "f" (func $f (result u32)))
+ (core func $f (canon lower (func $f)))
+ (core module $m
+ (import "" "f" (func $f (result i32)))
+ (func (export "run") (result i32) (call $f)))
+ (core instance $i (instantiate $m (with "" (instance
+ (export "f" (func $f))))))
+ (func (export "run") (result u32) (canon lift (core func $i "run"))))
+ (instance $b (instantiate $B (with "f" (func $c "f"))))
+ (export "run" (func $b "run")))
+
+(assert_trap (invoke "run") "cannot leave component instance")