From 45494c4c49ef46c69036bb79c2ada220313590f6 Mon Sep 17 00:00:00 2001 From: Luke Wagner Date: Tue, 1 Dec 2026 17:54:58 -0600 Subject: [PATCH] Also reject outer aliases of resources that cross component types Resolves #771 --- design/mvp/Binary.md | 7 +- design/mvp/Text.md | 21 +- test/nyi.txt | 1 + .../outer-alias-component-type.wast | 244 ++++++++++++++++++ test/validation/outer-alias.wast | 16 +- 5 files changed, 271 insertions(+), 18 deletions(-) create mode 100644 test/validation/outer-alias-component-type.wast diff --git a/design/mvp/Binary.md b/design/mvp/Binary.md index c4663bc8f..e196d4798 100644 --- a/design/mvp/Binary.md +++ b/design/mvp/Binary.md @@ -129,9 +129,10 @@ Notes: type, an `instance` type) * `i` is validated to be a valid index in the `s` index space of the target scope (counting outward, starting with `0` referring to the current scope) - * if `ct` is greater than 0 and crosses a component (as opposed to type) - boundary and `s` is `type`, the target type may not transitively refer to a - resource type. + * if `s` is `type` and at least one of the `ct` scopes crossed by the alias + (starting with the current scope and stopping before the target scope) is a + `component` definition or `component` type, the target type may not + transitively refer to a resource type. ## Type Definitions diff --git a/design/mvp/Text.md b/design/mvp/Text.md index fdfa18be6..40106158d 100644 --- a/design/mvp/Text.md +++ b/design/mvp/Text.md @@ -933,10 +933,9 @@ function parameter type: (import "fs-component" (component (type $CT))) ) ``` -Similarly, `outer` aliases are necessary to define an `instance` or `component` -type that uses a `resource` type defined or imported by the containing -component, since `instance` and `component` types start with empty type index -spaces: +Similarly, `outer` aliases are necessary to define an `instance` type that uses +a `resource` type defined or imported by the containing component, since +`instance` and `component` types start with empty type index spaces: ```wat (component $C (import "r" (type $R (sub resource))) @@ -951,21 +950,31 @@ In terms of validation, `alias` declarators inherit all the previously-mentioned validation rules of `alias` definitions and add the following rules: * `alias export` declarators may only alias the `instance` and `type` sorts * `alias outer` declarators may only alias the `core type` and `type` sorts +* `alias outer` declarators that cross the boundary of a `component` *type* + may not alias `type` definitions that transitively refer to a `resource` type +For example, in the following component, `$T2` and `$T3` can only be `outer`- +aliased by the first `instance` type: ```wat (component $C (component $D) (type $T1 (record (field "x" u32) (field "y" u32))) (type $T2 (resource (rep i32))) (type $T3 (borrow $T2)) - (type (component + (type (instance ;; (alias outer $C $D (component $D)) ❌ not allowed inside type (alias outer $C $T1 (type $T1)) (alias outer $C $T2 (type $T2)) (alias outer $C $T3 (type $T3)) )) + (type (component + ;; (alias outer $C $D (component $D)) ❌ not allowed inside type + (alias outer $C $T1 (type $T1)) + ;; (alias outer $C $T2 (type $T2)) ❌ resource type + ;; (alias outer $C $T3 (type $T3)) ❌ refers to resource type + )) (component - (type (component + (type (instance (alias outer $C $T1 (type $T1)) ;; (alias outer $C $T2 (type $T2)) ❌ resource type ;; (alias outer $C $T3 (type $T3)) ❌ refers to resource type diff --git a/test/nyi.txt b/test/nyi.txt index ec1ec1d78..1518b7f33 100644 --- a/test/nyi.txt +++ b/test/nyi.txt @@ -4,3 +4,4 @@ ./async/big-interleaving-test.wast ./async/forward.wast ./async/exit-without-resolving.wast +./validation/outer-alias-component-type.wast diff --git a/test/validation/outer-alias-component-type.wast b/test/validation/outer-alias-component-type.wast new file mode 100644 index 000000000..6fd5a3de7 --- /dev/null +++ b/test/validation/outer-alias-component-type.wast @@ -0,0 +1,244 @@ +;; Validation of `outer` aliases that cross the boundary of a `component` type: +;; like a `component` definition, a `component` type may not alias a type that +;; transitively refers to a resource type, whereas `instance` type boundaries +;; are transparent. + +;; a `component` type may alias the resource-free types of its enclosing +;; component... + +(component definition $C + (core type $CT (func (param i32) (result i32))) + (type $T (tuple u32 string)) + (type $F (func (param "t" $T) (result $T))) + (type $I (instance (export "f" (func (type $F))))) + (type $U (component (import "i" (instance (type $I))))) + (type (component + (alias outer $C $CT (core type $ct)) + (alias outer $C $T (type $t)) + (alias outer $C $F (type $f)) + (alias outer $C $I (type $i)) + (alias outer $C $U (type $u)) + (import "f" (func (type $f))) + (import "i" (instance (type $i))) + (import "c" (component (type $u))) + (export "g" (func (param "t" $t))) + )) +) + +;; ...including instance and component types that use resource types, as long +;; as each resource type is imported or exported by the aliased type itself + +(component definition $C + (type $I (instance + (export "r" (type $r (sub resource))) + (export "f" (func (param "self" (borrow $r)))) + )) + (type $U (component + (import "r" (type $r (sub resource))) + (export "s" (type $s (sub resource))) + (export "f" (func (param "r" (own $r)) (result (own $s)))) + )) + (type (component + (alias outer $C $I (type $i)) + (alias outer $C $U (type $u)) + (import "i" (instance (type $i))) + (export "c" (component (type $u))) + )) +) + +;; a `component` type may also alias the resource types that it imports or +;; exports itself, either directly (ct=0) or from nested `instance` types +;; (crossing only `instance` type boundaries) + +(component definition + (type $CT (component + (import "r" (type $R (sub resource))) + (alias outer $CT $R (type $r0)) + (import "f" (func (param "r" (borrow $r0)))) + (type $IT (instance + (alias outer $CT $R (type $r1)) + (export "f" (func (param "r" (borrow $r1)))) + (type (instance + (alias outer $CT $R (type $r2)) + (export "g" (func (result (own $r2)))) + )) + )) + (import "i" (instance (type $IT))) + (export "j" (instance (type $IT))) + (export "s" (type $S (sub resource))) + (export "k" (instance + (export "h" (func (param "r" (borrow $R)) (result (own $S)))) + )) + )) +) + +;; (just as `instance` types nested in a component may alias the component's +;; resource types) + +(component definition $C + (type $R (resource (rep i32))) + (type (instance + (alias outer $C $R (type $r)) + (type (instance + (alias outer $C $R (type $r)) + (export "f" (func (param "r" (borrow $r)))) + )) + )) +) + +;; however, a `component` type may not alias a resource type of its enclosing +;; component, whether defined, imported or exported... + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type (component (alias outer $C $R (type $r)))) + ) + "transitively refers to resources") + +(assert_invalid + (component $C + (import "r" (type $R (sub resource))) + (type (component (alias outer $C $R (type $r)))) + ) + "transitively refers to resources") + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (export $R' "r" (type $R)) + (type (component (alias outer $C $R' (type $r)))) + ) + "transitively refers to resources") + +(assert_invalid + (component + (type (resource (rep i32))) + (type (component (alias outer 1 0 (type)))) + ) + "transitively refers to resources") + +;; ...nor any type that transitively refers to one + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type $B (borrow $R)) + (type (component (alias outer $C $B (type $b)))) + ) + "transitively refers to resources") + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type $L (list (own $R))) + (type (component (alias outer $C $L (type $l)))) + ) + "transitively refers to resources") + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type $F (func (param "r" (borrow $R)))) + (type (component (alias outer $C $F (type $f)))) + ) + "transitively refers to resources") + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type $I (instance (export "r" (type (eq $R))))) + (type (component (alias outer $C $I (type $i)))) + ) + "transitively refers to resources") + +;; the implicit `outer` aliases injected by text-format name resolution are +;; subject to the same rule + +(assert_invalid + (component + (type $R (resource (rep i32))) + (type (component (import "r" (type (eq $R))))) + ) + "transitively refers to resources") + +(assert_invalid + (component + (type $R (resource (rep i32))) + (type (component (export "r" (type (eq $R))))) + ) + "transitively refers to resources") + +(assert_invalid + (component + (import "r" (type $R (sub resource))) + (type $I (instance (export "r" (type (eq $R))))) + (type (component (import "i" (instance (type $I))))) + ) + "transitively refers to resources") + +;; the rule applies wherever a `component` type is written, including inline +;; as the type of an import, so that an `outer` alias of an imported component +;; can always be replaced by a copy of the import + +(assert_invalid + (component $C + (import "r" (type $R (sub resource))) + (import "c" (component + (alias outer $C $R (type $r)) + (export "r" (type (eq $r))) + )) + ) + "transitively refers to resources") + +;; an alias is rejected if any of the boundaries it crosses is a `component` +;; type boundary: an `instance` type in a `component` type... + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type (component + (type (instance (alias outer $C $R (type $r)))) + )) + ) + "transitively refers to resources") + +(assert_invalid + (component + (import "r" (type $R (sub resource))) + (type (component + (import "i" (instance (export "r" (type (eq $R))))) + )) + ) + "transitively refers to resources") + +;; ...a `component` type in an `instance` type... + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type (instance + (type (component (alias outer $C $R (type $r)))) + )) + ) + "transitively refers to resources") + +(assert_invalid + (component + (import "r" (type $R (sub resource))) + (import "i" (instance + (export "c" (component (export "r" (type (eq $R))))) + )) + ) + "transitively refers to resources") + +;; ...and a `component` type in a `component` type + +(assert_invalid + (component $C + (type $R (resource (rep i32))) + (type (component + (type (component (alias outer $C $R (type $r)))) + )) + ) + "transitively refers to resources") diff --git a/test/validation/outer-alias.wast b/test/validation/outer-alias.wast index 2fe3496c2..e8c804a1f 100644 --- a/test/validation/outer-alias.wast +++ b/test/validation/outer-alias.wast @@ -75,13 +75,14 @@ ) "transitively refers to resources") -;; the rule applies to the aliased type as a whole: a component type that -;; transitively mentions a resource may not cross a `component` boundary either +;; the rule applies to the aliased type as a whole: an instance or function +;; type that transitively mentions a resource may not cross a `component` +;; boundary either (assert_invalid (component $C (type $R (resource (rep i32))) - (type $U (component (export "a" (type (eq $R))))) + (type $U (instance (export "a" (type (eq $R))))) (component (alias outer $C $U (type $a))) ) "transitively refers to resources") @@ -89,7 +90,7 @@ (assert_invalid (component $C (type $R (resource (rep i32))) - (type $U (component (import "a" (type (eq $R))))) + (type $U (func (param "a" (own $R)))) (component (alias outer $C $U (type $a))) ) "transitively refers to resources") @@ -102,19 +103,16 @@ (alias outer 0 1 (type $b)) ) -;; only a `type` boundary (component-type declarator) +;; only an `instance` type boundary (component definition $C (type $R (resource (rep i32))) (type $B (borrow $R)) (type $L (list (own $R))) - (type (component + (type (instance (alias outer $C $R (type $r)) (alias outer $C $B (type $b)) (alias outer $C $L (type $l)) )) - (type (instance - (alias outer $C $R (type $r)) - )) ) ;; mix of valid