Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasmparser: Use type-specific identifiers pervasively #1257

Merged

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented Oct 19, 2023

This commit changes wasmparser from the uni-typed TypeId identifier to a myriad of type-specific identifiers, such as ComponentInstanceTypeId and ComponentDefinedTypeId. There are also identifiers for any kind of type within some group, such as ComponentAnyTypeId for any kind of component type and AnyTypeId for any kind of type, component or core.

The wasmparser::Types arena can be indexed by any of these leaf type identifiers, which all implement the TypeIdentifier trait. The resulting indexed data is the particular type, e.g. a ComponentFuncType, rather than the uni-typed wasmparser::Type.

There was a lot of ocean boiling involved here. Apologies in advance for the pain of this upgrade! That said, it should provide type safety benefits moving forward and sets the stage for further identifier refactors for core types that are necessary for Wasm GC.

If you are upgrading and were using TypeId before, and don't want to spend time gaining type safety on your type lookups, then just use AnyTypeId instead of the old TypeId. They are morally identical. Alternatively, if you know a particular TypeId was always a component function, for example, and you always did types[id].unwrap_component_func(), then you can switch to using ComponentFuncTypeId.

@fitzgen
Copy link
Member Author

fitzgen commented Oct 19, 2023

FWIW: 3.5 million fuzz iterations and no problems discovered thus far.

@fitzgen fitzgen force-pushed the different-core-vs-component-type-ids branch from ed6fd19 to 370d151 Compare October 19, 2023 20:46
This commit changes `wasmparser` from the uni-typed `TypeId` identifier to a
myriad of type-specific identifiers, such as `ComponentInstanceTypeId` and
`ComponentDefinedTypeId`. There are also identifiers for any kind of type within
some group, such as `ComponentAnyTypeId` for any kind of component type and
`AnyTypeId` for any kind of type, component or core.

The `wasmparser::Types` arena can be indexed by any of these leaf type
identifiers, which all implement the `TypeIdentifier` trait. The resulting
indexed data is the particular type, e.g. a `ComponentFuncType`, rather than the
uni-typed `wasmparser::Type`.

There was a lot of ocean boiling involved here. Apologies in advance for the
pain of this upgrade! That said, it should provide type safety benefits moving
forward and sets the stage for further identifier refactors for core types that
are necessary for Wasm GC.

If you are upgrading and were using `TypeId` before, and don't want to spend
time gaining type safety on your type lookups, then just use `AnyTypeId` instead
of the old `TypeId`. They are morally identical. Alternatively, if you know a
particular `TypeId` was always a component function, for example, and you always
did `types[id].unwrap_component_func()`, then you can switch to using
`ComponentFuncTypeId`.

Co-Authored-By: Alex Crichton <alex@alexcrichton.com>
Comment on lines +785 to +788
/// Unwrap a `FuncType` or panic.
pub fn unwrap_func(&self) -> &FuncType {
self.structural_type.unwrap_func()
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this perhaps also check that is_final is true or false? (I forget which it should check for)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default is final == true but there is nothing stopping one from defining a non-final function type, and I'd imagine this would still want to return the underlying function type.

crates/wasmparser/src/validator/types.rs Outdated Show resolved Hide resolved
@fitzgen fitzgen enabled auto-merge (squash) October 19, 2023 22:56
@fitzgen fitzgen merged commit 537111b into bytecodealliance:main Oct 19, 2023
15 checks passed
@fitzgen fitzgen deleted the different-core-vs-component-type-ids branch October 19, 2023 23:19
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request Oct 30, 2023
This commit updates the wasm-tools family of crate for a number of
notable updates:

* bytecodealliance/wasm-tools#1257 - wasmparser's ID-based
  infrastructure has been refactored to have more precise types for each
  ID rather than one all-purpose `TypeId`.
* bytecodealliance/wasm-tools#1262 - the implementation of
  "implementation imports" for the component model which both updates
  the binary format in addition to adding more syntactic forms of
  imports.
* bytecodealliance/wasm-tools#1260 - a new encoding scheme for component
  information for `wit-component` in objects (not used by Wasmtime but
  used by bindings generators).

Translation for components needed to be updated to account for the first
change, but otherwise this was a straightforward update.
github-merge-queue bot pushed a commit to bytecodealliance/wasmtime that referenced this pull request Oct 30, 2023
* Update wasm-tools crates

This commit updates the wasm-tools family of crate for a number of
notable updates:

* bytecodealliance/wasm-tools#1257 - wasmparser's ID-based
  infrastructure has been refactored to have more precise types for each
  ID rather than one all-purpose `TypeId`.
* bytecodealliance/wasm-tools#1262 - the implementation of
  "implementation imports" for the component model which both updates
  the binary format in addition to adding more syntactic forms of
  imports.
* bytecodealliance/wasm-tools#1260 - a new encoding scheme for component
  information for `wit-component` in objects (not used by Wasmtime but
  used by bindings generators).

Translation for components needed to be updated to account for the first
change, but otherwise this was a straightforward update.

* Remove a TODO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants