Skip to content

Commit 9e5a085

Browse files
authored
Disallow non-type/instance aliases in component/instance types (#2462)
This fixes a bug in the validation of components where previously invalid `alias` items were allowed to be present in `instance` and `component` type definitions. The specification indicates that these should be disallowed and `wasmparser` erroneously allowed them. The impact of this change is expected to be nonexistent or negligible as this only applies to handwritten components. Components produced by `wit-component` do not use this feature so no previously produced component should be rejected. Additionally the impact of allowing these invalid `alias` annotations is also negligible as you couldn't actually do anything with the item anyway so it was just sort of dead bloat in a binary. cc WebAssembly/component-model#621
1 parent 70d6683 commit 9e5a085

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

crates/wasmparser/src/validator/component.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2446,29 +2446,42 @@ impl ComponentState {
24462446
types: &mut TypeAlloc,
24472447
offset: usize,
24482448
) -> Result<()> {
2449+
let component = components.last_mut().unwrap();
2450+
2451+
match component.kind {
2452+
// For a component itself all alias kind are allowed.
2453+
ComponentKind::Component => {}
2454+
2455+
// For instance/component types only aliases to `type` or `instance`
2456+
// items are allowed, and all other aliases are rejected.
2457+
ComponentKind::InstanceType | ComponentKind::ComponentType => match &alias {
2458+
crate::ComponentAlias::InstanceExport {
2459+
kind: ComponentExternalKind::Type | ComponentExternalKind::Instance,
2460+
..
2461+
}
2462+
| crate::ComponentAlias::Outer {
2463+
kind: ComponentOuterAliasKind::Type | ComponentOuterAliasKind::CoreType,
2464+
..
2465+
} => {}
2466+
2467+
_ => bail!(
2468+
offset,
2469+
"aliases in a component or instance type may only refer to types or instances"
2470+
),
2471+
},
2472+
}
2473+
24492474
match alias {
24502475
crate::ComponentAlias::InstanceExport {
24512476
instance_index,
24522477
kind,
24532478
name,
2454-
} => components.last_mut().unwrap().alias_instance_export(
2455-
instance_index,
2456-
kind,
2457-
name,
2458-
types,
2459-
offset,
2460-
),
2479+
} => component.alias_instance_export(instance_index, kind, name, types, offset),
24612480
crate::ComponentAlias::CoreInstanceExport {
24622481
instance_index,
24632482
kind,
24642483
name,
2465-
} => components.last_mut().unwrap().alias_core_instance_export(
2466-
instance_index,
2467-
kind,
2468-
name,
2469-
types,
2470-
offset,
2471-
),
2484+
} => component.alias_core_instance_export(instance_index, kind, name, types, offset),
24722485
crate::ComponentAlias::Outer { kind, count, index } => match kind {
24732486
ComponentOuterAliasKind::CoreModule => {
24742487
Self::alias_module(components, count, index, offset)

tests/cli/component-model/alias.wast

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,3 +299,12 @@
299299
(instance (instantiate (component $i "x")))
300300
)
301301
)
302+
303+
(assert_invalid
304+
(component
305+
(type (component
306+
(import "x" (instance $I (export "f" (func))))
307+
(alias export $I "f" (func))
308+
))
309+
)
310+
"aliases in a component or instance type may only refer to types or instances")

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,13 @@
183183
"line": 293,
184184
"filename": "alias.27.wasm",
185185
"module_type": "binary"
186+
},
187+
{
188+
"type": "assert_invalid",
189+
"line": 304,
190+
"filename": "alias.28.wasm",
191+
"module_type": "binary",
192+
"text": "aliases in a component or instance type may only refer to types or instances"
186193
}
187194
]
188195
}

0 commit comments

Comments
 (0)