Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,11 @@ impl ComponentState {
}

// Current MVP restriction of the component model.
if rep != ValType::I32 {
bail!(offset, "resources can only be represented by `i32`");
if rep != ValType::I32 && rep != ValType::I64 {
bail!(
offset,
"resources can only be represented by `i32` or `i64`"
);
}
Comment thread
alexcrichton marked this conversation as resolved.

// If specified validate that the destructor is both a valid
Expand Down Expand Up @@ -4351,10 +4354,11 @@ impl ComponentState {
/// Validates that the linear memory at `idx` is valid to use as a canonical
/// ABI memory.
///
/// At this time this requires that the memory is a plain 32-bit linear
/// memory. Notably this disallows shared memory and 64-bit linear memories.
/// At this time this requires that the memory is a plain 32-bit or 64-bit linear
/// memory. Notably this disallows shared memory.
fn cabi_memory_at(&self, idx: u32, offset: usize) -> Result<()> {
let ty = self.memory_at(idx, offset)?;

SubtypeCx::memory_type(
ty,
&MemoryType {
Expand All @@ -4366,8 +4370,27 @@ impl ComponentState {
},
offset,
)
.or_else(|e| {
if e.message()
.contains("mismatch in index type used for memories")
{
SubtypeCx::memory_type(
ty,
&MemoryType {
initial: 0,
maximum: None,
memory64: true,
shared: false,
page_size_log2: None,
},
offset,
)
} else {
Err(e)
}
})
.map_err(|mut e| {
e.add_context("canonical ABI memory is not a 32-bit linear memory".into());
e.add_context("canonical ABI memory is not a 32-bit or 64-bit linear memory".into());
e
Comment thread
michael-weigelt marked this conversation as resolved.
Outdated
})
}
Expand Down
2 changes: 0 additions & 2 deletions tests/cli/component-model/memory64.wast
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
(core instance (instantiate $B (with "" (instance (export "" (table $m))))))
)

(assert_invalid
(component
(import "x" (func $x (param "x" string)))
(core module $A
Expand All @@ -51,4 +50,3 @@
(alias core export $A "m" (core memory $m))
(core func (canon lower (func $x) (memory $m)))
)
"canonical ABI memory is not a 32-bit linear memory")
Comment thread
michael-weigelt marked this conversation as resolved.
65 changes: 65 additions & 0 deletions tests/cli/component-model/memory64/resources.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
;; RUN: wast --assert default --snapshot tests/snapshots %

(component
(type $x (resource (rep i64)))
)

(component
(type $x (resource (rep i64)))

(core func (canon resource.new $x))
(core func (canon resource.rep $x))
(core func (canon resource.drop $x))
)

(component
(core module $m
(func (export "dtor") (param i64))
)
(core instance $m (instantiate $m))
(type $x (resource (rep i64) (dtor (func $m "dtor"))))
(core func (canon resource.new $x))
)

(component
(type $x (resource (rep i64)))
(core func $f1 (canon resource.new $x))
(core func $f2 (canon resource.rep $x))
(core func $f3 (canon resource.drop $x))

(core module $m
(import "" "f1" (func (param i64) (result i32)))
(import "" "f2" (func (param i32) (result i64)))
(import "" "f3" (func (param i32)))
)

(core instance (instantiate $m
(with "" (instance
(export "f1" (func $f1))
(export "f2" (func $f2))
(export "f3" (func $f3))
))
))
)

(assert_invalid
(component
(core module $m
(func (export "dtor") (param i32))
)
(core instance $m (instantiate $m))
(type $x (resource (rep i64) (dtor (func $m "dtor"))))
(core func (canon resource.new $x))
)
"wrong signature for a destructor")

(assert_invalid
(component
(core module $m
(func (export "dtor") (param i64))
)
(core instance $m (instantiate $m))
(type $x (resource (rep i32) (dtor (func $m "dtor"))))
(core func (canon resource.new $x))
)
"wrong signature for a destructor")
2 changes: 1 addition & 1 deletion tests/cli/component-model/resources.wast
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

(assert_invalid
(component
(type $x (resource (rep i64)))
(type $x (resource (rep v128)))
Comment thread
michael-weigelt marked this conversation as resolved.
Outdated
)
"resources can only be represented by `i32`")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@
(alias core export $A "m" (core memory $m))
(core func (canon lower (func $x) (memory $m)))
)
"canonical ABI memory is not a 32-bit linear memory")
"canonical ABI memory is not a 32-bit or 64-bit linear memory")
7 changes: 3 additions & 4 deletions tests/snapshots/cli/component-model/memory64.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
"module_type": "binary"
},
{
"type": "assert_invalid",
"line": 46,
"type": "module",
"line": 45,
"filename": "memory64.4.wasm",
"module_type": "binary",
"text": "canonical ABI memory is not a 32-bit linear memory"
"module_type": "binary"
}
]
}
11 changes: 11 additions & 0 deletions tests/snapshots/cli/component-model/memory64.wast/4.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(component
(type (;0;) (func (param "x" string)))
(import "x" (func $x (;0;) (type 0)))
(core module $A (;0;)
(memory (;0;) i64 1)
(export "m" (memory 0))
)
(core instance $A (;0;) (instantiate $A))
(alias core export $A "m" (core memory $m (;0;)))
(core func (;0;) (canon lower (func $x) (memory $m)))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"source_filename": "tests/cli/component-model/memory64/resources.wast",
"commands": [
{
"type": "module",
"line": 3,
"filename": "resources.0.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 7,
"filename": "resources.1.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 15,
"filename": "resources.2.wasm",
"module_type": "binary"
},
{
"type": "module",
"line": 24,
"filename": "resources.3.wasm",
"module_type": "binary"
},
{
"type": "assert_invalid",
"line": 46,
"filename": "resources.4.wasm",
"module_type": "binary",
"text": "wrong signature for a destructor"
},
{
"type": "assert_invalid",
"line": 57,
"filename": "resources.5.wasm",
"module_type": "binary",
"text": "wrong signature for a destructor"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(component
(type $x (;0;) (resource (rep i64)))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(component
(type $x (;0;) (resource (rep i64)))
(core func (;0;) (canon resource.new $x))
(core func (;1;) (canon resource.rep $x))
(core func (;2;) (canon resource.drop $x))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
(component
(core module $m (;0;)
(type (;0;) (func (param i64)))
(export "dtor" (func 0))
(func (;0;) (type 0) (param i64))
)
(core instance $m (;0;) (instantiate $m))
(alias core export $m "dtor" (core func (;0;)))
(type $x (;0;) (resource (rep i64) (dtor (func 0))))
(core func (;1;) (canon resource.new $x))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
(component
(type $x (;0;) (resource (rep i64)))
(core func $f1 (;0;) (canon resource.new $x))
(core func $f2 (;1;) (canon resource.rep $x))
(core func $f3 (;2;) (canon resource.drop $x))
(core module $m (;0;)
(type (;0;) (func (param i64) (result i32)))
(type (;1;) (func (param i32) (result i64)))
(type (;2;) (func (param i32)))
(import "" "f1" (func (;0;) (type 0)))
(import "" "f2" (func (;1;) (type 1)))
(import "" "f3" (func (;2;) (type 2)))
)
(core instance (;0;)
(export "f1" (func $f1))
(export "f2" (func $f2))
(export "f3" (func $f3))
)
(core instance (;1;) (instantiate $m
(with "" (instance 0))
)
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"line": 25,
"filename": "not-accepted.2.wasm",
"module_type": "binary",
"text": "canonical ABI memory is not a 32-bit linear memory"
"text": "canonical ABI memory is not a 32-bit or 64-bit linear memory"
}
]
}
Loading