Skip to content

Commit

Permalink
Fix validation of resource methods in instance types (#1072)
Browse files Browse the repository at this point in the history
This commit fixes an issue in `wasmparser`'s validation of component
instance types where `[method]foo.bar`-style name mangling was
erroneously considered invalid. This happened because registration of
which names were valid to use only happened for components and component
types, not for instance types. This is because the location that
registered names of resources also did other validation which isn't
required for instance types as that validation is deferred to later.

This commit addresses the issue by moving the logic to register the
names of resources above the short-circuit that skips the rest of the
validation logic in the function.
  • Loading branch information
alexcrichton authored Jun 20, 2023
1 parent 2c55ad6 commit 5b8ff53
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
28 changes: 15 additions & 13 deletions crates/wasmparser/src/validator/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,21 @@ impl ComponentState {
ty: &ComponentEntityType,
types: &TypeAlloc,
) -> bool {
if let ComponentEntityType::Type { created, .. } = ty {
// If this is a top-level resource then register it in the
// appropriate context so later validation of method-like-names
// works out.
if let Some(name) = toplevel_name {
if let Type::Resource(_) = types[*created] {
let cx = match kind {
ExternKind::Import => &mut self.toplevel_imported_resources,
ExternKind::Export => &mut self.toplevel_exported_resources,
};
cx.register(name, *created);
}
}
}

match self.kind {
ComponentKind::Component | ComponentKind::ComponentType => {}
ComponentKind::InstanceType => return true,
Expand Down Expand Up @@ -589,19 +604,6 @@ impl ComponentState {
}
}

// If this is a top-level resource then register it in the
// appropriate context so later validation of method-like-names
// works out.
if let Some(name) = toplevel_name {
if let Type::Resource(_) = types[*created] {
let cx = match kind {
ExternKind::Import => &mut self.toplevel_imported_resources,
ExternKind::Export => &mut self.toplevel_exported_resources,
};
cx.register(name, *created);
}
}

true
}

Expand Down
10 changes: 10 additions & 0 deletions tests/local/component-model/resources.wast
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,13 @@
(export "t2" (instance (type $t2')))
))
)

(component
(type (component
(type (instance
(export "bar" (type (sub resource)))
(export "[static]bar.a" (func))
))
(export "x" (instance (type 0)))
))
)
14 changes: 14 additions & 0 deletions tests/snapshots/local/component-model/resources.wast/110.print
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
(component
(type (;0;)
(component
(type (;0;)
(instance
(export (;0;) "bar" (type (sub resource)))
(type (;1;) (func))
(export (;0;) "[static]bar.a" (func (type 1)))
)
)
(export (;0;) "x" (instance (type 0)))
)
)
)

0 comments on commit 5b8ff53

Please sign in to comment.