Skip to content

Commit f78ac03

Browse files
authored
Sync with wasm-tools a bit more, clarify task.return & borrows (#737)
* Sync with wasm-tools a bit more, clarify task.return & borrows This is a few more minor edits to the spec from comparing the implementation in wasm-tools against the spec. The changes here are: * Encoding rules for types within a core module type have been updated to statically disallow core-modules-in-core-modules. This matches wasm-tools' behavior where within a core module the funky `0x00` prefix isn't needed for GC types, it's only within components that's needed. Minor `binary.wast` updates were made for this. * The `async` option on `canon lower` no longer unconditionally requires `memory`. This is already tested, wasm-tools already implements this behavior, so this syncs the explainer with the tests. * An explicit validation rule was added to disallow `borrow`s in the `$t` passed to the `task.return` intrinsic. This already wouldn't work anywhere because it couldn't match a function signature, but it seems reasonable to go ahead and statically disallow it here to avoid runtimes having to worry about it. * Add some task.return tests
1 parent 2126546 commit f78ac03

4 files changed

Lines changed: 49 additions & 8 deletions

File tree

‎design/mvp/Binary.md‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ core:deftype ::= rt:<core:rectype> => rt
145145
| mt:<core:moduletype> => mt
146146
core:moduletype ::= 0x50 md*:vec(<core:moduledecl>) => (module md*)
147147
core:moduledecl ::= 0x00 i:<core:import> => i
148-
| 0x01 t:<core:type> => t
148+
| 0x01 rt:<core:rectype> => (type rt)
149149
| 0x02 a:<core:alias> => a
150150
| 0x03 e:<core:exportdecl> => e
151151
core:alias ::= 0x10 0x01 ct:<u32> idx:<u32> => (alias outer ct idx (type))
@@ -163,10 +163,15 @@ Notes:
163163
component core type is `0x00 0x50`; elsewhere, `0x50`). By the v1.0 release of
164164
this specification, `core:moduletype` will receive a new, non-overlapping
165165
opcode.
166-
* Validation of `core:moduledecl` rejects `core:moduletype` definitions
167-
and `outer` aliases of `core:moduletype` definitions inside `type`
168-
declarators. Thus, as an invariant, when validating a `core:moduletype`, the
169-
core type index space will not contain any core module types.
166+
* The ambiguity above does not arise within a `core:moduletype`: the `type`
167+
declarators of `core:moduledecl` contain a `core:rectype` directly, using the
168+
same encoding as a Core WebAssembly type section. Thus, within a
169+
`core:moduletype`, a non-final `sub` is encoded as `0x50` without a `0x00`
170+
prefix, and a `core:moduletype` cannot be nested.
171+
* Validation of `core:moduledecl` rejects `outer` aliases of
172+
`core:moduletype` definitions. Thus, as an invariant, when validating a
173+
`core:moduletype`, the core type index space will not contain any core module
174+
types.
170175
* As described in the explainer, each module type is validated with an
171176
initially-empty type index space.
172177
* In `core:alias`, the first `0x10` is the opcode for `type` in `core:sort` and

‎design/mvp/CanonicalABI.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3604,7 +3604,6 @@ validation is performed where `$callee` has type `$ft`:
36043604
* requires options [based on `lift(result)`](#canonopt-validation) if `ft` has a result
36053605
* if `len(flatten_types(ft.param_types())) > max_flat_params`, `memory` is required
36063606
* if `len(flatten_types(ft.result_type())) > max_flat_results`, `memory` is required
3607-
* 🔀 if `async` is specified, `memory` must be present
36083607

36093608
When instantiating a component instance, the runtime calls `Store.lower` (defined
36103609
above) to capture the `$callee`, `$ft` and `$opts` immediates of `canon lower`
@@ -3928,6 +3927,7 @@ In addition to [general validation of `$opts`](#canonopt-validation) validation
39283927
specifies:
39293928

39303929
* `$f` is given type `flatten_functype($opts, (func (param $t)?), 'lower')`
3930+
* `$t` must not transitively contain `borrow`
39313931
* `$opts` may only contain `memory` and `string-encoding`
39323932
* [`lift($f.result)` above](#canonopt-validation) defines required options
39333933

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
;; the result of `task.return` cannot contain a `borrow`
3+
(assert_invalid
4+
(component
5+
(type $r (resource (rep i32)))
6+
(core func (canon task.return (result (borrow $r)))))
7+
"`task.return` result cannot contain a `borrow` type")
8+
(assert_invalid
9+
(component
10+
(type $r (resource (rep i32)))
11+
(type $t (tuple u32 (borrow $r)))
12+
(core func (canon task.return (result $t))))
13+
"`task.return` result cannot contain a `borrow` type")
14+
(component
15+
(type $r (resource (rep i32)))
16+
(core func (canon task.return (result (own $r)))))

‎test/binary/binary.wast‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,14 +943,34 @@
943943
"invalid leading byte (0x0) for outer alias target"
944944
)
945945

946+
;; Within a core:moduletype, type declarators use the Core WebAssembly
947+
;; encoding of core:rectype: a bare 0x50 is a non-final `sub` and a nested
948+
;; core:moduletype can't be encoded.
949+
(component binary
950+
"\00asm" "\0d\00\01\00" ;; preamble
951+
"\03\09" ;; core type section (9 bytes)
952+
"\01" ;; 1 core type
953+
"\50\01" ;; core:moduletype, 1 decl
954+
"\01\50\00\60\00\00" ;; 0x01 type: (sub (func))
955+
)
956+
(assert_malformed
957+
(component binary
958+
"\00asm" "\0d\00\01\00" ;; preamble
959+
"\03\0a" ;; core type section (10 bytes)
960+
"\01" ;; 1 core type
961+
"\50\01" ;; core:moduletype, 1 decl
962+
"\01\00\50\00\60\00\00" ;; 0x01 type: 0x00 is not a core:rectype
963+
)
964+
"invalid leading byte"
965+
)
946966
(assert_malformed
947967
(component binary
948968
"\00asm" "\0d\00\01\00" ;; preamble
949969
"\03\0a" ;; core type section (10 bytes)
950970
"\01" ;; 1 core type
951971
"\50\02" ;; core:moduletype, 2 decls
952-
"\01\50\00" ;; 0x01 type: nested core:moduletype
953-
"\01\60\00\00" ;; 0x01 type: core functype
972+
"\01\50\00" ;; 0x01 type: non-final sub, no supertypes
973+
"\01\60\00\00" ;; 0x01 is not a core:comptype
954974
)
955975
"invalid leading byte"
956976
)

0 commit comments

Comments
 (0)