Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
24 changes: 21 additions & 3 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ pub(crate) struct ComponentState {

/// Same as `toplevel_exported_resources`, but for imports.
toplevel_imported_resources: ComponentNameContext,

/// The type that's been assigned to slots of `context.{get,set}` by this
/// component. This is `None` until the first intrinsic is seen.
context_type: Option<ValType>,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -462,6 +466,7 @@ impl ComponentState {
imported_types: Default::default(),
toplevel_exported_resources: Default::default(),
toplevel_imported_resources: Default::default(),
context_type: None,
}
}

Expand Down Expand Up @@ -1559,20 +1564,33 @@ impl ComponentState {
Ok(())
}

fn validate_context_type(&self, ty: ValType, intrinsic: &str, offset: usize) -> Result<()> {
fn validate_context_type(&mut self, ty: ValType, intrinsic: &str, offset: usize) -> Result<()> {
match ty {
ValType::I32 => Ok(()),
ValType::I32 => {}
ValType::I64 => {
if !self.features.cm64() {
bail!(
offset,
"64-bit `{intrinsic}` requires the component model 64-bit feature"
)
}
Ok(())
{}
}
_ => bail!(offset, "`{intrinsic}` only supports `i32` or `i64`"),
}

match self.context_type {
None => self.context_type = Some(ty),
Some(other) => {
if other != ty {
bail!(
offset,
"`{intrinsic}` type must match previous context type"
)
}
}
}
Ok(())
}

fn thread_yield(&mut self, types: &mut TypeAlloc, offset: usize) -> Result<()> {
Expand Down
15 changes: 15 additions & 0 deletions tests/cli/component-model/async/threading-and-memory64.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
;; RUN: wast --assert default --snapshot tests/snapshots % -f cm-async,cm-threading,cm64

(assert_invalid
(component
(core func (canon context.set i32 0))
(core func (canon context.get i64 1))
)
"type must match previous context type")

(assert_invalid
(component
(core func (canon context.get i64 0))
(core func (canon context.set i32 1))
)
"type must match previous context type")
26 changes: 21 additions & 5 deletions tests/cli/component-model/memory64/context.wast
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,28 @@
))
)

;; Mixed i32 and i64 slots round-trip independently.
;; can't mix i32/i64
(assert_invalid
(component
(core func (canon context.get i32 0))
(core func (canon context.set i64 0))
)
"type must match previous context type")
(assert_invalid
(component
(core func (canon context.set i64 0))
(core func (canon context.get i32 0))
)
"type must match previous context type")

;; can mix across components
(component
(core func (canon context.get i32 0))
(core func (canon context.set i32 0))
(core func (canon context.get i64 0))
(core func (canon context.set i64 0))
(component
(core func (canon context.set i64 0))
)
(component
(core func (canon context.get i32 0))
)
)

;; Signature must match the declared slot width.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"source_filename": "tests/cli/component-model/async/threading-and-memory64.wast",
"commands": [
{
"type": "assert_invalid",
"line": 4,
"filename": "threading-and-memory64.0.wasm",
"module_type": "binary",
"text": "type must match previous context type"
},
{
"type": "assert_invalid",
"line": 11,
"filename": "threading-and-memory64.1.wasm",
"module_type": "binary",
"text": "type must match previous context type"
}
]
}
38 changes: 26 additions & 12 deletions tests/snapshots/cli/component-model/memory64/context.wast.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,57 @@
"module_type": "binary"
},
{
"type": "module",
"line": 22,
"type": "assert_invalid",
"line": 23,
"filename": "context.1.wasm",
"module_type": "binary"
"module_type": "binary",
"text": "type must match previous context type"
},
{
"type": "assert_invalid",
"line": 31,
"line": 29,
"filename": "context.2.wasm",
"module_type": "binary",
"text": "found: (func (result i64))"
"text": "type must match previous context type"
},
{
"type": "assert_invalid",
"line": 39,
"type": "module",
"line": 36,
"filename": "context.3.wasm",
"module_type": "binary",
"text": "found: (func (param i64))"
"module_type": "binary"
},
{
"type": "assert_invalid",
"line": 47,
"filename": "context.4.wasm",
"module_type": "binary",
"text": "found: (func (result i32))"
"text": "found: (func (result i64))"
},
{
"type": "assert_invalid",
"line": 55,
"filename": "context.5.wasm",
"module_type": "binary",
"text": "found: (func (param i32))"
"text": "found: (func (param i64))"
},
{
"type": "module",
"type": "assert_invalid",
"line": 63,
"filename": "context.6.wasm",
"module_type": "binary",
"text": "found: (func (result i32))"
},
{
"type": "assert_invalid",
"line": 71,
"filename": "context.7.wasm",
"module_type": "binary",
"text": "found: (func (param i32))"
},
{
"type": "module",
"line": 79,
"filename": "context.8.wasm",
"module_type": "binary"
}
]
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(component
(component (;0;)
(core func (;0;) (canon context.set i64 0))
)
(component (;1;)
(core func (;0;) (canon context.get i32 0))
)
)
Loading