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

Remove the borrow checking from wiggle entirely #8702

Merged

Conversation

alexcrichton
Copy link
Member

This commit is a refactoring of the wiggle crate which powers the *.witx-based bindings generation of Wasmtime for wasip1 support. Originally wiggle had a full-blown runtime borrow checker which verified that borrows were disjoint when appropriate. In #8277 this was removed in favor of a more coarse "either all shared or all mutable" guarantee. It turns out that this exactly matches what the Rust type system guarantees at compile time as well.

This commit removes all runtime borrow checking in favor of compile-time borrow checking instead. This means that there is no longer the possibility of a runtime error arising from borrowing errors. Current bindings in Wasmtime needed no restructuring to work with this new API.

The source of the refactors here are all in the wiggle crate. Changes include:

  • The GuestPtr type lost its type parameter. Additionally it only contains a u32 pointer now instead.
  • The GuestMemory trait is replaced with a simple enum of possibilities.
  • Helper methods on GuestPtr are all moved to GuestMemory.
  • A number of abstractions were simplified now that borrow checking is no longer necessary.
  • Generated trait methods now all take &mut GuestMemory<'_> as an argument.

These changes were then propagated to the wasmtime-wasi and wasi-common crates in their preview0 and preview1 implementations of WASI. All changes are just general refactors, no functional change is intended here.

@alexcrichton alexcrichton requested a review from elliottt May 29, 2024 01:59
@alexcrichton alexcrichton requested a review from a team as a code owner May 29, 2024 01:59
@github-actions github-actions bot added the wasi Issues pertaining to WASI label May 29, 2024
Copy link
Contributor

@pchickey pchickey left a comment

Choose a reason for hiding this comment

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

We sure did spend turn a lot of CPU cycles into heat by dynamically checking a property that was statically provable all these years, didnt we

crates/wasi-common/src/snapshots/preview_0.rs Outdated Show resolved Hide resolved
TokenStream::from(quote! { #code #metadata })
let mut ret = quote! { #code #metadata };

if std::env::var("WIGGLE_DEBUG_BINDGEN").is_ok() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wish I had done this years ago

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 should wrap this up in a helper as well since I'm starting to litter this all over the macros I work on...

@iximeow
Copy link
Contributor

iximeow commented May 29, 2024

to be fair to ourselves, when we thought embedders might provide functions that involve multiple concurrent outstanding borrows it all made a bit more sense :)

@alexcrichton alexcrichton added this pull request to the merge queue May 30, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 30, 2024
This commit is a refactoring of the `wiggle` crate which powers the
`*.witx`-based bindings generation of Wasmtime for wasip1 support.
Originally `wiggle` had a full-blown runtime borrow checker which
verified that borrows were disjoint when appropriate. In bytecodealliance#8277 this was
removed in favor of a more coarse "either all shared or all mutable"
guarantee. It turns out that this exactly matches what the Rust type
system guarantees at compile time as well.

This commit removes all runtime borrow checking in favor of compile-time
borrow checking instead. This means that there is no longer the
possibility of a runtime error arising from borrowing errors. Current
bindings in Wasmtime needed no restructuring to work with this new API.

The source of the refactors here are all in the `wiggle` crate. Changes
include:

* The `GuestPtr` type lost its type parameter. Additionally it only
  contains a `u32` pointer now instead.
* The `GuestMemory` trait is replaced with a simple `enum` of
  possibilities.
* Helper methods on `GuestPtr` are all moved to `GuestMemory`.
* A number of abstractions were simplified now that borrow checking is
  no longer necessary.
* Generated trait methods now all take `&mut GuestMemory<'_>` as an
  argument.

These changes were then propagated to the `wasmtime-wasi` and
`wasi-common` crates in their preview0 and preview1 implementations of
WASI. All changes are just general refactors, no functional change is
intended here.
@alexcrichton alexcrichton force-pushed the wiggle-no-borrow-checking branch from 5be87d5 to 0130672 Compare May 31, 2024 14:48
@alexcrichton alexcrichton enabled auto-merge May 31, 2024 14:48
@alexcrichton alexcrichton added this pull request to the merge queue May 31, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 31, 2024
@alexcrichton alexcrichton enabled auto-merge May 31, 2024 16:06
@alexcrichton alexcrichton added this pull request to the merge queue May 31, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 31, 2024
@acfoltzer
Copy link
Contributor

I'm so glad we're able to do this now, thank you!!

@alexcrichton alexcrichton enabled auto-merge May 31, 2024 17:14
@alexcrichton alexcrichton added this pull request to the merge queue May 31, 2024
Merged via the queue into bytecodealliance:main with commit f141165 May 31, 2024
36 checks passed
@alexcrichton alexcrichton deleted the wiggle-no-borrow-checking branch May 31, 2024 17:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasi Issues pertaining to WASI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants