Skip to content

Refactor component host/libcalls #10959

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

Merged

Conversation

alexcrichton
Copy link
Member

In the spirit of making Wasmtime's internals safer this is a step forward for components to a new paradigm for how libcalls/host functions are implemented. Previously *mut ComponentInstance was liberally used but this meant that situations would often simultaneously have &mut ComponentInstance and &mut StoreOpaque accessible in the same function and there was no prevention of going from the store to the component instance, acquiring two aliasing mutable references (which would be unsound). The refactoring applied here is to redefine the entrypoints from the guest back into the host to operate on &mut dyn VMStore (or StoreContextMut<'_, T>) plus
wasmtime::component::Instance. This index-based approach means that there's no aliasing of component instances and stores and the Instance type can be used to look up anything within the store that's necessary.

This refactoring originated in the wasip3-prototyping repository and has been used to remove a good deal of unsafe code now that Instance is effectively safe to pass around and the store was already passed around anyway everywhere.

In the future I plan to apply a similar paradigm shift for core instances as well, but that'll require some more finesse for all the bits and bobs that core wasm does.

In the spirit of making Wasmtime's internals safer this is a step
forward for components to a new paradigm for how libcalls/host functions
are implemented. Previously `*mut ComponentInstance` was liberally used
but this meant that situations would often simultaneously have `&mut
ComponentInstance` and `&mut StoreOpaque` accessible in the same
function and there was no prevention of going from the store to the
component instance, acquiring two aliasing mutable references (which
would be unsound). The refactoring applied here is to redefine the
entrypoints from the guest back into the host to operate on `&mut dyn
VMStore` (or `StoreContextMut<'_, T>`) plus
`wasmtime::component::Instance`. This index-based approach means that
there's no aliasing of component instances and stores and the `Instance`
type can be used to look up anything within the store that's necessary.

This refactoring originated in the wasip3-prototyping repository and has
been used to remove a good deal of `unsafe` code now that `Instance` is
effectively safe to pass around and the store was already passed around
anyway everywhere.

In the future I plan to apply a similar paradigm shift for core
instances as well, but that'll require some more finesse for all the
bits and bobs that core wasm does.
@alexcrichton alexcrichton requested a review from a team as a code owner June 6, 2025 19:27
@alexcrichton alexcrichton requested review from dicej and removed request for a team June 6, 2025 19:27
@alexcrichton alexcrichton added this pull request to the merge queue Jun 6, 2025
Merged via the queue into bytecodealliance:main with commit 4233014 Jun 6, 2025
41 checks passed
@alexcrichton alexcrichton deleted the upstream-more-changes branch June 6, 2025 21:23
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