Skip to content

Start reducing unsafety of ComponentInstance #10934

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
merged 1 commit into from
Jun 5, 2025

Conversation

alexcrichton
Copy link
Member

Work recently in the wasip3-prototyping repository has focused on reducing the amount of unsafe code and improving internal abstractions to facilitate this. One major thrust that's manifested in is the removal of the usage of *mut ComponentInstance where possible and instead using an index to safely borrow from the store to get the entire instance. This commit does not fully realize this vision just yet but lays some groundwork leading up to this.

This commit specifically removes the *mut ComponentInstance pointers in lift/lower contexts in favor of directly storing a wasmtime::component::Instance. When the raw ComponentInstance is needed it's acquired from the StoreOpaque in a safe fashion that borrows the entire store for the duration of the returned borrow. This in turn required pushing instances into the store earlier during instantiation because during instantiation an instance could call out to host APIs which do lifts/lowers.

There's still more work to be done to plumb this fully into libcalls and host function invocations but I wanted to upstream the wasip3 work piecemeal a chunk at a time.

Work recently in the wasip3-prototyping repository has focused on
reducing the amount of `unsafe` code and improving internal abstractions
to facilitate this. One major thrust that's manifested in is the removal
of the usage of `*mut ComponentInstance` where possible and instead
using an index to safely borrow from the store to get the entire
instance. This commit does not fully realize this vision just yet but
lays some groundwork leading up to this.

This commit specifically removes the `*mut ComponentInstance` pointers
in lift/lower contexts in favor of directly storing a
`wasmtime::component::Instance`. When the raw `ComponentInstance` is
needed it's acquired from the `StoreOpaque` in a safe fashion that
borrows the entire store for the duration of the returned borrow. This
in turn required pushing instances into the store earlier during
instantiation because during instantiation an instance could call out to
host APIs which do lifts/lowers.

There's still more work to be done to plumb this fully into libcalls and
host function invocations but I wanted to upstream the wasip3 work
piecemeal a chunk at a time.
@alexcrichton alexcrichton requested a review from a team as a code owner June 5, 2025 20:17
@alexcrichton alexcrichton requested review from fitzgen and removed request for a team June 5, 2025 20:17
Comment on lines -979 to -985
// Note that this is technically unsafe due to the fact that it enables
// `mem::swap`-ing two component instances which would get all the offsets
// mixed up and cause issues. This is scoped to just this module though as a
// convenience to forward to `&mut` methods on `ComponentInstance`.
unsafe fn instance_mut(&mut self) -> &mut ComponentInstance {
&mut *self.ptr.as_ptr()
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I'll note that this comment is still equally applicable today, but I don't think it's worth it trying to keep this distinction. It's just too damn useful to be able to get a safe &mut ComponentInstance and too subtle to avoid trying to do so. I opened #10933 to track a better solution here.

Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

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

Nice

@fitzgen fitzgen added this pull request to the merge queue Jun 5, 2025
@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Jun 5, 2025
Merged via the queue into bytecodealliance:main with commit 47e9088 Jun 5, 2025
41 checks passed
@alexcrichton alexcrichton deleted the upstream-index-changes branch June 5, 2025 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasmtime:api Related to the API of the `wasmtime` crate itself
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants