Skip to content

Commit 45494c4

Browse files
committed
Also reject outer aliases of resources that cross component types
Resolves #771
1 parent 232a4cb commit 45494c4

5 files changed

Lines changed: 271 additions & 18 deletions

File tree

‎design/mvp/Binary.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,10 @@ Notes:
129129
type, an `instance` type)
130130
* `i` is validated to be a valid index in the `s` index space of the target
131131
scope (counting outward, starting with `0` referring to the current scope)
132-
* if `ct` is greater than 0 and crosses a component (as opposed to type)
133-
boundary and `s` is `type`, the target type may not transitively refer to a
134-
resource type.
132+
* if `s` is `type` and at least one of the `ct` scopes crossed by the alias
133+
(starting with the current scope and stopping before the target scope) is a
134+
`component` definition or `component` type, the target type may not
135+
transitively refer to a resource type.
135136

136137

137138
## Type Definitions

‎design/mvp/Text.md‎

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -933,10 +933,9 @@ function parameter type:
933933
(import "fs-component" (component (type $CT)))
934934
)
935935
```
936-
Similarly, `outer` aliases are necessary to define an `instance` or `component`
937-
type that uses a `resource` type defined or imported by the containing
938-
component, since `instance` and `component` types start with empty type index
939-
spaces:
936+
Similarly, `outer` aliases are necessary to define an `instance` type that uses
937+
a `resource` type defined or imported by the containing component, since
938+
`instance` and `component` types start with empty type index spaces:
940939
```wat
941940
(component $C
942941
(import "r" (type $R (sub resource)))
@@ -951,21 +950,31 @@ In terms of validation, `alias` declarators inherit all the previously-mentioned
951950
validation rules of `alias` definitions and add the following rules:
952951
* `alias export` declarators may only alias the `instance` and `type` sorts
953952
* `alias outer` declarators may only alias the `core type` and `type` sorts
953+
* `alias outer` declarators that cross the boundary of a `component` *type*
954+
may not alias `type` definitions that transitively refer to a `resource` type
954955

956+
For example, in the following component, `$T2` and `$T3` can only be `outer`-
957+
aliased by the first `instance` type:
955958
```wat
956959
(component $C
957960
(component $D)
958961
(type $T1 (record (field "x" u32) (field "y" u32)))
959962
(type $T2 (resource (rep i32)))
960963
(type $T3 (borrow $T2))
961-
(type (component
964+
(type (instance
962965
;; (alias outer $C $D (component $D)) ❌ not allowed inside type
963966
(alias outer $C $T1 (type $T1))
964967
(alias outer $C $T2 (type $T2))
965968
(alias outer $C $T3 (type $T3))
966969
))
970+
(type (component
971+
;; (alias outer $C $D (component $D)) ❌ not allowed inside type
972+
(alias outer $C $T1 (type $T1))
973+
;; (alias outer $C $T2 (type $T2)) ❌ resource type
974+
;; (alias outer $C $T3 (type $T3)) ❌ refers to resource type
975+
))
967976
(component
968-
(type (component
977+
(type (instance
969978
(alias outer $C $T1 (type $T1))
970979
;; (alias outer $C $T2 (type $T2)) ❌ resource type
971980
;; (alias outer $C $T3 (type $T3)) ❌ refers to resource type

‎test/nyi.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
./async/big-interleaving-test.wast
55
./async/forward.wast
66
./async/exit-without-resolving.wast
7+
./validation/outer-alias-component-type.wast
Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,244 @@
1+
;; Validation of `outer` aliases that cross the boundary of a `component` type:
2+
;; like a `component` definition, a `component` type may not alias a type that
3+
;; transitively refers to a resource type, whereas `instance` type boundaries
4+
;; are transparent.
5+
6+
;; a `component` type may alias the resource-free types of its enclosing
7+
;; component...
8+
9+
(component definition $C
10+
(core type $CT (func (param i32) (result i32)))
11+
(type $T (tuple u32 string))
12+
(type $F (func (param "t" $T) (result $T)))
13+
(type $I (instance (export "f" (func (type $F)))))
14+
(type $U (component (import "i" (instance (type $I)))))
15+
(type (component
16+
(alias outer $C $CT (core type $ct))
17+
(alias outer $C $T (type $t))
18+
(alias outer $C $F (type $f))
19+
(alias outer $C $I (type $i))
20+
(alias outer $C $U (type $u))
21+
(import "f" (func (type $f)))
22+
(import "i" (instance (type $i)))
23+
(import "c" (component (type $u)))
24+
(export "g" (func (param "t" $t)))
25+
))
26+
)
27+
28+
;; ...including instance and component types that use resource types, as long
29+
;; as each resource type is imported or exported by the aliased type itself
30+
31+
(component definition $C
32+
(type $I (instance
33+
(export "r" (type $r (sub resource)))
34+
(export "f" (func (param "self" (borrow $r))))
35+
))
36+
(type $U (component
37+
(import "r" (type $r (sub resource)))
38+
(export "s" (type $s (sub resource)))
39+
(export "f" (func (param "r" (own $r)) (result (own $s))))
40+
))
41+
(type (component
42+
(alias outer $C $I (type $i))
43+
(alias outer $C $U (type $u))
44+
(import "i" (instance (type $i)))
45+
(export "c" (component (type $u)))
46+
))
47+
)
48+
49+
;; a `component` type may also alias the resource types that it imports or
50+
;; exports itself, either directly (ct=0) or from nested `instance` types
51+
;; (crossing only `instance` type boundaries)
52+
53+
(component definition
54+
(type $CT (component
55+
(import "r" (type $R (sub resource)))
56+
(alias outer $CT $R (type $r0))
57+
(import "f" (func (param "r" (borrow $r0))))
58+
(type $IT (instance
59+
(alias outer $CT $R (type $r1))
60+
(export "f" (func (param "r" (borrow $r1))))
61+
(type (instance
62+
(alias outer $CT $R (type $r2))
63+
(export "g" (func (result (own $r2))))
64+
))
65+
))
66+
(import "i" (instance (type $IT)))
67+
(export "j" (instance (type $IT)))
68+
(export "s" (type $S (sub resource)))
69+
(export "k" (instance
70+
(export "h" (func (param "r" (borrow $R)) (result (own $S))))
71+
))
72+
))
73+
)
74+
75+
;; (just as `instance` types nested in a component may alias the component's
76+
;; resource types)
77+
78+
(component definition $C
79+
(type $R (resource (rep i32)))
80+
(type (instance
81+
(alias outer $C $R (type $r))
82+
(type (instance
83+
(alias outer $C $R (type $r))
84+
(export "f" (func (param "r" (borrow $r))))
85+
))
86+
))
87+
)
88+
89+
;; however, a `component` type may not alias a resource type of its enclosing
90+
;; component, whether defined, imported or exported...
91+
92+
(assert_invalid
93+
(component $C
94+
(type $R (resource (rep i32)))
95+
(type (component (alias outer $C $R (type $r))))
96+
)
97+
"transitively refers to resources")
98+
99+
(assert_invalid
100+
(component $C
101+
(import "r" (type $R (sub resource)))
102+
(type (component (alias outer $C $R (type $r))))
103+
)
104+
"transitively refers to resources")
105+
106+
(assert_invalid
107+
(component $C
108+
(type $R (resource (rep i32)))
109+
(export $R' "r" (type $R))
110+
(type (component (alias outer $C $R' (type $r))))
111+
)
112+
"transitively refers to resources")
113+
114+
(assert_invalid
115+
(component
116+
(type (resource (rep i32)))
117+
(type (component (alias outer 1 0 (type))))
118+
)
119+
"transitively refers to resources")
120+
121+
;; ...nor any type that transitively refers to one
122+
123+
(assert_invalid
124+
(component $C
125+
(type $R (resource (rep i32)))
126+
(type $B (borrow $R))
127+
(type (component (alias outer $C $B (type $b))))
128+
)
129+
"transitively refers to resources")
130+
131+
(assert_invalid
132+
(component $C
133+
(type $R (resource (rep i32)))
134+
(type $L (list (own $R)))
135+
(type (component (alias outer $C $L (type $l))))
136+
)
137+
"transitively refers to resources")
138+
139+
(assert_invalid
140+
(component $C
141+
(type $R (resource (rep i32)))
142+
(type $F (func (param "r" (borrow $R))))
143+
(type (component (alias outer $C $F (type $f))))
144+
)
145+
"transitively refers to resources")
146+
147+
(assert_invalid
148+
(component $C
149+
(type $R (resource (rep i32)))
150+
(type $I (instance (export "r" (type (eq $R)))))
151+
(type (component (alias outer $C $I (type $i))))
152+
)
153+
"transitively refers to resources")
154+
155+
;; the implicit `outer` aliases injected by text-format name resolution are
156+
;; subject to the same rule
157+
158+
(assert_invalid
159+
(component
160+
(type $R (resource (rep i32)))
161+
(type (component (import "r" (type (eq $R)))))
162+
)
163+
"transitively refers to resources")
164+
165+
(assert_invalid
166+
(component
167+
(type $R (resource (rep i32)))
168+
(type (component (export "r" (type (eq $R)))))
169+
)
170+
"transitively refers to resources")
171+
172+
(assert_invalid
173+
(component
174+
(import "r" (type $R (sub resource)))
175+
(type $I (instance (export "r" (type (eq $R)))))
176+
(type (component (import "i" (instance (type $I)))))
177+
)
178+
"transitively refers to resources")
179+
180+
;; the rule applies wherever a `component` type is written, including inline
181+
;; as the type of an import, so that an `outer` alias of an imported component
182+
;; can always be replaced by a copy of the import
183+
184+
(assert_invalid
185+
(component $C
186+
(import "r" (type $R (sub resource)))
187+
(import "c" (component
188+
(alias outer $C $R (type $r))
189+
(export "r" (type (eq $r)))
190+
))
191+
)
192+
"transitively refers to resources")
193+
194+
;; an alias is rejected if any of the boundaries it crosses is a `component`
195+
;; type boundary: an `instance` type in a `component` type...
196+
197+
(assert_invalid
198+
(component $C
199+
(type $R (resource (rep i32)))
200+
(type (component
201+
(type (instance (alias outer $C $R (type $r))))
202+
))
203+
)
204+
"transitively refers to resources")
205+
206+
(assert_invalid
207+
(component
208+
(import "r" (type $R (sub resource)))
209+
(type (component
210+
(import "i" (instance (export "r" (type (eq $R)))))
211+
))
212+
)
213+
"transitively refers to resources")
214+
215+
;; ...a `component` type in an `instance` type...
216+
217+
(assert_invalid
218+
(component $C
219+
(type $R (resource (rep i32)))
220+
(type (instance
221+
(type (component (alias outer $C $R (type $r))))
222+
))
223+
)
224+
"transitively refers to resources")
225+
226+
(assert_invalid
227+
(component
228+
(import "r" (type $R (sub resource)))
229+
(import "i" (instance
230+
(export "c" (component (export "r" (type (eq $R)))))
231+
))
232+
)
233+
"transitively refers to resources")
234+
235+
;; ...and a `component` type in a `component` type
236+
237+
(assert_invalid
238+
(component $C
239+
(type $R (resource (rep i32)))
240+
(type (component
241+
(type (component (alias outer $C $R (type $r))))
242+
))
243+
)
244+
"transitively refers to resources")

‎test/validation/outer-alias.wast‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,22 @@
7575
)
7676
"transitively refers to resources")
7777

78-
;; the rule applies to the aliased type as a whole: a component type that
79-
;; transitively mentions a resource may not cross a `component` boundary either
78+
;; the rule applies to the aliased type as a whole: an instance or function
79+
;; type that transitively mentions a resource may not cross a `component`
80+
;; boundary either
8081

8182
(assert_invalid
8283
(component $C
8384
(type $R (resource (rep i32)))
84-
(type $U (component (export "a" (type (eq $R)))))
85+
(type $U (instance (export "a" (type (eq $R)))))
8586
(component (alias outer $C $U (type $a)))
8687
)
8788
"transitively refers to resources")
8889

8990
(assert_invalid
9091
(component $C
9192
(type $R (resource (rep i32)))
92-
(type $U (component (import "a" (type (eq $R)))))
93+
(type $U (func (param "a" (own $R))))
9394
(component (alias outer $C $U (type $a)))
9495
)
9596
"transitively refers to resources")
@@ -102,19 +103,16 @@
102103
(alias outer 0 1 (type $b))
103104
)
104105

105-
;; only a `type` boundary (component-type declarator)
106+
;; only an `instance` type boundary
106107
(component definition $C
107108
(type $R (resource (rep i32)))
108109
(type $B (borrow $R))
109110
(type $L (list (own $R)))
110-
(type (component
111+
(type (instance
111112
(alias outer $C $R (type $r))
112113
(alias outer $C $B (type $b))
113114
(alias outer $C $L (type $l))
114115
))
115-
(type (instance
116-
(alias outer $C $R (type $r))
117-
))
118116
)
119117

120118
;; mix of valid

0 commit comments

Comments
 (0)