Skip to content

Commit 3fd2bd7

Browse files
feat: (CM64) Allow component model resources to be represented by i64 as well as i32. (#2496)
* allow resource rep i64 * fix * allow CABI memory to be 64bit * fix * gate behind feature flag * fix tests * address comments * fix * remove file
1 parent 0274083 commit 3fd2bd7

File tree

18 files changed

+236
-26
lines changed

18 files changed

+236
-26
lines changed

crates/wasmparser/src/features.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,12 @@ define_wasm_features! {
312312
/// Corresponds to the 🗺️ character in
313313
/// <https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md>.
314314
pub cm_map: CM_MAP(1 << 38) = false;
315+
316+
/// Support for 64-bit contexts in the component model proposal.
317+
///
318+
/// Corresponds to the 🐘 character in
319+
/// <https://github.com/WebAssembly/component-model/blob/main/design/mvp/Explainer.md>.
320+
pub cm64: CM64(1 << 39) = false;
315321
}
316322
}
317323

crates/wasmparser/src/validator/component.rs

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,17 @@ impl ComponentState {
588588
}
589589

590590
// Current MVP restriction of the component model.
591-
if rep != ValType::I32 {
592-
bail!(offset, "resources can only be represented by `i32`");
591+
if rep == ValType::I64 && !component.features.cm64() {
592+
bail!(
593+
offset,
594+
"resources with `i64` require the `cm64` feature to be enabled"
595+
)
596+
}
597+
if rep != ValType::I32 && rep != ValType::I64 {
598+
bail!(
599+
offset,
600+
"resources can only be represented by `i32` or `i64`"
601+
);
593602
}
594603

595604
// If specified validate that the destructor is both a valid
@@ -4351,25 +4360,24 @@ impl ComponentState {
43514360
/// Validates that the linear memory at `idx` is valid to use as a canonical
43524361
/// ABI memory.
43534362
///
4354-
/// At this time this requires that the memory is a plain 32-bit linear
4355-
/// memory. Notably this disallows shared memory and 64-bit linear memories.
4363+
/// At this time this requires that the memory is a plain 32-bit or 64-bit linear
4364+
/// memory. Notably this disallows shared memory.
43564365
fn cabi_memory_at(&self, idx: u32, offset: usize) -> Result<()> {
43574366
let ty = self.memory_at(idx, offset)?;
4358-
SubtypeCx::memory_type(
4359-
ty,
4360-
&MemoryType {
4361-
initial: 0,
4362-
maximum: None,
4363-
memory64: false,
4364-
shared: false,
4365-
page_size_log2: None,
4366-
},
4367-
offset,
4368-
)
4369-
.map_err(|mut e| {
4370-
e.add_context("canonical ABI memory is not a 32-bit linear memory".into());
4371-
e
4372-
})
4367+
let valid_memory_type = MemoryType {
4368+
initial: 0,
4369+
maximum: None,
4370+
memory64: ty.memory64,
4371+
shared: false,
4372+
page_size_log2: None,
4373+
};
4374+
if ty.memory64 && !self.features.cm64() {
4375+
bail!(
4376+
offset,
4377+
"64-bit memories require the `cm64` feature to be enabled"
4378+
);
4379+
}
4380+
SubtypeCx::memory_type(ty, &valid_memory_type, offset)
43734381
}
43744382

43754383
/// Completes the translation of this component, performing final

tests/cli/component-model/memory64.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,4 @@
5151
(alias core export $A "m" (core memory $m))
5252
(core func (canon lower (func $x) (memory $m)))
5353
)
54-
"canonical ABI memory is not a 32-bit linear memory")
54+
"64-bit memories require the `cm64` feature to be enabled")
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
;; RUN: wast --assert default --snapshot tests/snapshots -f cm64 %
2+
3+
(component
4+
(import "x" (func $x (param "x" string)))
5+
(core module $A
6+
(memory (export "m") i64 1))
7+
(core instance $A (instantiate $A))
8+
(alias core export $A "m" (core memory $m))
9+
(core func (canon lower (func $x) (memory $m)))
10+
)
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
;; RUN: wast --assert default --snapshot tests/snapshots -f cm64 %
2+
3+
(component
4+
(type $x (resource (rep i64)))
5+
)
6+
7+
(component
8+
(type $x (resource (rep i64)))
9+
10+
(core func (canon resource.new $x))
11+
(core func (canon resource.rep $x))
12+
(core func (canon resource.drop $x))
13+
)
14+
15+
(component
16+
(core module $m
17+
(func (export "dtor") (param i64))
18+
)
19+
(core instance $m (instantiate $m))
20+
(type $x (resource (rep i64) (dtor (func $m "dtor"))))
21+
(core func (canon resource.new $x))
22+
)
23+
24+
(component
25+
(type $x (resource (rep i64)))
26+
(core func $f1 (canon resource.new $x))
27+
(core func $f2 (canon resource.rep $x))
28+
(core func $f3 (canon resource.drop $x))
29+
30+
(core module $m
31+
(import "" "f1" (func (param i64) (result i32)))
32+
(import "" "f2" (func (param i32) (result i64)))
33+
(import "" "f3" (func (param i32)))
34+
)
35+
36+
(core instance (instantiate $m
37+
(with "" (instance
38+
(export "f1" (func $f1))
39+
(export "f2" (func $f2))
40+
(export "f3" (func $f3))
41+
))
42+
))
43+
)
44+
45+
(assert_invalid
46+
(component
47+
(core module $m
48+
(func (export "dtor") (param i32))
49+
)
50+
(core instance $m (instantiate $m))
51+
(type $x (resource (rep i64) (dtor (func $m "dtor"))))
52+
(core func (canon resource.new $x))
53+
)
54+
"wrong signature for a destructor")
55+
56+
(assert_invalid
57+
(component
58+
(core module $m
59+
(func (export "dtor") (param i64))
60+
)
61+
(core instance $m (instantiate $m))
62+
(type $x (resource (rep i32) (dtor (func $m "dtor"))))
63+
(core func (canon resource.new $x))
64+
)
65+
"wrong signature for a destructor")
66+
67+
(assert_invalid
68+
(component
69+
(type $x (resource (rep v128)))
70+
)
71+
"resources can only be represented by `i32` or `i64`")

tests/cli/component-model/resources.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
(component
5353
(type $x (resource (rep i64)))
5454
)
55-
"resources can only be represented by `i32`")
55+
"resources with `i64` require the `cm64` feature to be enabled")
5656

5757
(assert_invalid
5858
(component

tests/cli/component-model/shared-everything-threads/not-accepted.wast

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@
3131
(alias core export $A "m" (core memory $m))
3232
(core func (canon lower (func $x) (memory $m)))
3333
)
34-
"canonical ABI memory is not a 32-bit linear memory")
34+
"mismatch in the shared flag for memories")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
error: invalid value 'unknown' for '--features <FEATURES>': unknown feature `unknown`
2-
Valid features: mutable-global, saturating-float-to-int, sign-extension, reference-types, multi-value, bulk-memory, simd, relaxed-simd, threads, shared-everything-threads, tail-call, floats, multi-memory, exceptions, memory64, extended-const, component-model, function-references, memory-control, gc, custom-page-sizes, legacy-exceptions, gc-types, stack-switching, wide-arithmetic, cm-values, cm-nested-names, cm-async, cm-async-stackful, cm-async-builtins, cm-threading, cm-error-context, cm-fixed-length-lists, cm-gc, call-indirect-overlong, bulk-memory-opt, custom-descriptors, compact-imports, cm-map, mvp, wasm1, wasm2, wasm3, lime1, all
2+
Valid features: mutable-global, saturating-float-to-int, sign-extension, reference-types, multi-value, bulk-memory, simd, relaxed-simd, threads, shared-everything-threads, tail-call, floats, multi-memory, exceptions, memory64, extended-const, component-model, function-references, memory-control, gc, custom-page-sizes, legacy-exceptions, gc-types, stack-switching, wide-arithmetic, cm-values, cm-nested-names, cm-async, cm-async-stackful, cm-async-builtins, cm-threading, cm-error-context, cm-fixed-length-lists, cm-gc, call-indirect-overlong, bulk-memory-opt, custom-descriptors, compact-imports, cm-map, cm64, mvp, wasm1, wasm2, wasm3, lime1, all
33

44
For more information, try '--help'.

tests/snapshots/cli/component-model/memory64.wast.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"line": 46,
3333
"filename": "memory64.4.wasm",
3434
"module_type": "binary",
35-
"text": "canonical ABI memory is not a 32-bit linear memory"
35+
"text": "64-bit memories require the `cm64` feature to be enabled"
3636
}
3737
]
3838
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"source_filename": "tests/cli/component-model/memory64/memory64.wast",
3+
"commands": [
4+
{
5+
"type": "module",
6+
"line": 3,
7+
"filename": "memory64.0.wasm",
8+
"module_type": "binary"
9+
}
10+
]
11+
}

0 commit comments

Comments
 (0)