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

wiggle: copy guest slices back to shared memory #5471

Merged
merged 4 commits into from
Jan 3, 2023

Conversation

abrown
Copy link
Collaborator

@abrown abrown commented Dec 19, 2022

This change upgrades UnsafeGuestSlice in Wiggle to expose more
functionality to be able to use std::ptr::copy for writing bytes into
Wasm shared memory. Additionally, it adds a new GuestCow type for
delineating between Wasm memory regions that can be borrowed (non-shared
memory) or must be copied (shared memory) in order to maintain Rust
guarantees.

With these in place, it is now possible to implement the preview1
"read" functions for shared memory. Previously, these would panic if
attempting to copy to a shared memory. This change removes the panic and
introduces some (rather complex) logic for handling both the shared and
non-shared cases:

  • if reading into a Wasm non-shared memory, Wiggle guarantees that no
    other guest pointers will touch the memory region and, in the absence
    of concurrency, a WASI function can write directly to this memory
  • if reading into a Wasm shared memory, the memory region can be
    concurrently modified. At @alexcrichton's request re: Rust safety,
    this change copies all of the bytes into an intermediate buffer before
    using std::ptr::copy to move them into Wasm memory.

This change only applies to the preview0 and preview1
implementations of wasi-common. Fixing up other WASI implementations
(esp. wasi-crypto) is left for later.

@github-actions github-actions bot added the wasi Issues pertaining to WASI label Dec 19, 2022
@github-actions
Copy link

Subscribe to Label Action

cc @kubkon

This issue or pull request has been labeled: "wasi"

Thus the following users have been cc'd because of the following labels:

  • kubkon: wasi

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

@abrown abrown marked this pull request as ready for review December 19, 2022 22:03
abrown added a commit to abrown/wasmtime that referenced this pull request Dec 19, 2022
Along the same lines as bytecodealliance#5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.
abrown added a commit to abrown/wasmtime that referenced this pull request Dec 19, 2022
Along the same lines as bytecodealliance#5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.
This change upgrades `UnsafeGuestSlice` in Wiggle to expose more
functionality to be able to use `std::ptr::copy` for writing bytes into
Wasm shared memory. Additionally, it adds a new `GuestCow` type for
delineating between Wasm memory regions that can be borrowed (non-shared
memory) or must be copied (shared memory) in order to maintain Rust
guarantees.

With these in place, it is now possible to implement the `preview1`
"read" functions for shared memory. Previously, these would panic if
attempting to copy to a shared memory. This change removes the panic and
introduces some (rather complex) logic for handling both the shared and
non-shared cases:
- if reading into a Wasm non-shared memory, Wiggle guarantees that no
  other guest pointers will touch the memory region and, in the absence
  of concurrency, a WASI function can write directly to this memory
- if reading into a Wasm shared memory, the memory region can be
  concurrently modified. At @alexcrichton's request re: Rust safety,
  this change copies all of the bytes into an intermediate buffer before
  using `std::ptr::copy` to move them into Wasm memory.

This change only applies to the `preview0` and `preview1`
implementations of `wasi-common`. Fixing up other WASI implementations
(esp. wasi-crypto) is left for later.
abrown added a commit to abrown/wasmtime that referenced this pull request Dec 20, 2022
Along the same lines as bytecodealliance#5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.

This change updates the uses of Wiggle strings in both wasi-common and
wasi-crypto.
abrown added a commit to abrown/wasmtime that referenced this pull request Dec 20, 2022
Along the same lines as bytecodealliance#5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.

This change updates the uses of Wiggle strings in both wasi-common and
wasi-crypto.
crates/wasi-common/src/snapshots/preview_0.rs Outdated Show resolved Hide resolved
Comment on lines 1116 to 1122
// If the Wasm memory is shared, copy to an intermediate buffer to
// avoid Rust unsafety (i.e., the called function could rely on
// `&mut [u8]`'s exclusive ownership which is not guaranteed due to
// potential access from other threads).
let mut tmp = vec![0; buf.len().min(MAX_SHARED_BUFFER_SIZE)];
self.random.try_fill_bytes(&mut tmp)?;
buf.copy_from_slice(&tmp)?;
Copy link
Member

Choose a reason for hiding this comment

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

Since random_get fills the entire buffer I think this will need to loop or otherwise this will return an error since tmp may be shorter than buf

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

You might want to take a look at 652988f since I changed the GuestPtr API slightly.

crates/wiggle/src/lib.rs Show resolved Hide resolved
Copy link
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

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

Looks good!

If you're up for it I also just notice that the read_vectored pattern is here 4 times, so possibly refactoring to just one definition might be a good idea but not a blocker.

@abrown abrown enabled auto-merge (squash) January 3, 2023 18:51
@abrown abrown merged commit f911855 into bytecodealliance:main Jan 3, 2023
@abrown abrown deleted the wiggle-copied-reads branch January 3, 2023 20:01
abrown added a commit to abrown/wasmtime that referenced this pull request Jan 3, 2023
Along the same lines as bytecodealliance#5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.

This change updates the uses of Wiggle strings in both wasi-common and
wasi-crypto.
alexcrichton pushed a commit that referenced this pull request Jan 4, 2023
* wiggle: copy guest strings from shared memory

Along the same lines as #5471, this change adds a new smart pointer,
`GuestStrCow`, to copy the string bytes over from Wasm memory to the
host when the string is found in shared memory. This is necessary to
maintain Rust guarantees: with shared memory, the bytes backing a
`GuestStr` could be altered by another thread and this would invalidate
the assumption that we can dereference at any point to `&str`.
`GuestStrCow` is essentially a wrapper around `GuestStr` when the memory
is not shared but copies the memory region into a `String` when the
memory is shared.

This change updates the uses of Wiggle strings in both wasi-common and
wasi-crypto.

* review: perform UTF-8 check on `GuestStr` construction
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.

None yet

2 participants