A lightweight WebAssembly runtime that is fast, secure, and standards-compliant
Restore a trap in same-instance adapter calls (#14730)
Alex Crichton3ec2054017a0
diff --git a/crates/environ/src/fact/trampoline.rs b/crates/environ/src/fact/trampoline.rsindex a46029d484..625dab433b 100644--- a/crates/environ/src/fact/trampoline.rs+++ b/crates/environ/src/fact/trampoline.rs@@ -114,6 +114,18 @@ pub(super) fn compile(module: &mut Module<'_>, adapter: &AdapterData) {)}+ // Disallow same-instance adapters. This is, as of the time of this writing,+ // technically not spec-conformant. This is something we'll want to+ // reconcile with the upstream specification because without this check the+ // precise order of lifts/lowers is visible to the guest which is generally+ // not something that the spec desires.+ if adapter.lift.instance == adapter.lower.instance {+ let (mut compiler, _, _) = compiler(module, adapter);+ compiler.trap(Trap::CannotEnterComponent);+ compiler.finish();+ return;+ }+// This closure compiles a function to be exported to the host which host to// lift the parameters from the caller and lower them to the callee.//diff --git a/tests/misc_testsuite/component-model/adapter.wast b/tests/misc_testsuite/component-model/adapter.wastindex b1b907aac5..79479c197d 100644--- a/tests/misc_testsuite/component-model/adapter.wast+++ b/tests/misc_testsuite/component-model/adapter.wast@@ -94,24 +94,25 @@(func $f1 (canon lift (core func $m "")))(core func $f2 (canon lower (func $f1))))+(assert_trap+ (component+ (core module $m (func (export "")))+ (core instance $m (instantiate $m))-(component- (core module $m (func (export "")))- (core instance $m (instantiate $m))-- (func $f1 (canon lift (core func $m "")))- (core func $f2 (canon lower (func $f1)))+ (func $f1 (canon lift (core func $m "")))+ (core func $f2 (canon lower (func $f1)))- (core module $m2- (import "" "" (func $f))- (func $start- call $f)- (start $start)+ (core module $m2+ (import "" "" (func $f))+ (func $start+ call $f)+ (start $start)+ )+ (core instance (instantiate $m2+ (with "" (instance (export "" (func $f2))))+ )))- (core instance (instantiate $m2- (with "" (instance (export "" (func $f2))))- ))-)+ "cannot enter component instance");; fiddling with 0-sized lists(component $cdiff --git a/tests/misc_testsuite/component-model/same-instance-call.wast b/tests/misc_testsuite/component-model/same-instance-call.wastnew file mode 100644index 0000000000..cd6fcfc8e7--- /dev/null+++ b/tests/misc_testsuite/component-model/same-instance-call.wast@@ -0,0 +1,104 @@+;;! multi_memory = true++;; A component instance calling a function that it itself lifted traps, as the+;; source and destination of transferred values may overlap in the same linear+;; memory.++;; string parameter, where `realloc` returns the source string's own address+(component+ (core module $M+ (memory (export "mem") 1)+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 100)+ (func (export "f") (param i32 i32) (result i32) local.get 1))+ (core instance $m (instantiate $M))+ (func $f (param "s" string) (result u32)+ (canon lift (core func $m "f") (memory (core memory $m "mem"))+ (realloc (core func $m "realloc")) string-encoding=utf8))+ (core func $f' (canon lower (func $f) (memory (core memory $m "mem"))+ (realloc (core func $m "realloc")) string-encoding=utf8))+ (core module $N+ (import "" "f" (func $f (param i32 i32) (result i32)))+ (import "" "mem" (memory 1))+ (data (i32.const 100) "hello world")+ (func (export "run") (result i32)+ (call $f (i32.const 100) (i32.const 11))))+ (core instance $n (instantiate $N (with "" (instance+ (export "f" (func $f'))+ (export "mem" (memory $m "mem"))))))+ (func (export "run") (result u32) (canon lift (core func $n "run")))+)+(assert_trap (invoke "run")+ "cannot enter component instance")++;; string nested in a result, with differing string encodings, and with the+;; caller referring to the memory through a re-exporting core instance+(component+ (core module $M+ (memory (export "mem") 1)+ (data (i32.const 200) "\01\00\00\00\2c\01\00\00\02\00\00\00")+ (data (i32.const 300) "h\00i\00")+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 100)+ (func (export "f") (result i32) i32.const 200))+ (core instance $m (instantiate $M))+ (core module $Reexport+ (import "" "mem" (memory 1))+ (export "mem" (memory 0)))+ (core instance $r (instantiate $Reexport (with "" (instance+ (export "mem" (memory $m "mem"))))))+ (func $f (result (option string))+ (canon lift (core func $m "f") (memory (core memory $m "mem"))+ string-encoding=utf16))+ (core func $f' (canon lower (func $f) (memory (core memory $r "mem"))+ (realloc (core func $m "realloc")) string-encoding=utf8))+ (core module $N+ (import "" "f" (func $f (param i32)))+ (func (export "run") (call $f (i32.const 0))))+ (core instance $n (instantiate $N (with "" (instance+ (export "f" (func $f'))))))+ (func (export "run") (canon lift (core func $n "run")))+)+(assert_trap (invoke "run")+ "cannot enter component instance")++;; lists also trap+(component+ (core module $M+ (memory (export "mem") 1)+ (data (i32.const 100) "\01\02\03")+ (func (export "realloc") (param i32 i32 i32 i32) (result i32)+ i32.const 100)+ (func (export "f") (param i32 i32) (result i32)+ (i32.add+ (i32.load8_u (local.get 0))+ (i32.load8_u offset=2 (local.get 0)))))+ (core instance $m (instantiate $M))+ (func $f (param "l" (list u8)) (result u32)+ (canon lift (core func $m "f") (memory (core memory $m "mem"))+ (realloc (core func $m "realloc"))))+ (core func $f' (canon lower (func $f) (memory (core memory $m "mem"))))+ (core module $N+ (import "" "f" (func $f (param i32 i32) (result i32)))+ (func (export "run") (result i32)+ (call $f (i32.const 100) (i32.const 3))))+ (core instance $n (instantiate $N (with "" (instance+ (export "f" (func $f'))))))+ (func (export "run") (result u32) (canon lift (core func $n "run")))+)+(assert_trap (invoke "run") "cannot enter component instance")++;; calls that don't involve linear memory at all also trap, at least for now+(component+ (core module $M (func (export "f") (result i32) i32.const 1))+ (core instance $m (instantiate $M))+ (func $f (result u32) (canon lift (core func $m "f")))+ (core func $f' (canon lower (func $f)))+ (core module $N+ (import "" "f" (func $f (result i32)))+ (func (export "run") (result i32) (call $f)))+ (core instance $n (instantiate $N (with "" (instance+ (export "f" (func $f'))))))+ (func (export "run") (result u32) (canon lift (core func $n "run")))+)+(assert_trap (invoke "run") "cannot enter component instance")