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

shared memory: audit safety of pointer copies in the presence of shared memories #4203

Open
Tracked by #4245
abrown opened this issue Jun 1, 2022 · 1 comment
Open
Tracked by #4245
Labels
wasm-proposal:threads Issues related to the WebAssembly threads proposal

Comments

@abrown
Copy link
Collaborator

abrown commented Jun 1, 2022

In discussions about #4187, we questioned whether several uses of (the equivalent of) memset and memcpy are still safe when shared memory is present. This issue is a placeholder to discuss that and will be used for tagging the relevant locations in the code.

@abrown abrown changed the title Audit safety of pointer copies in the presence of shared memories shared memory: audit safety of pointer copies in the presence of shared memories Jun 7, 2022
@alexcrichton alexcrichton added the wasm-proposal:threads Issues related to the WebAssembly threads proposal label Jun 8, 2022
@abrown
Copy link
Collaborator Author

abrown commented Jun 15, 2022

For reference, see several SpiderMonkey snippets.

abrown added a commit to abrown/wasmtime that referenced this issue Nov 9, 2022
alexcrichton pushed a commit that referenced this issue Nov 10, 2022
* wiggle: adapt Wiggle guest slices for `unsafe` shared use

When multiple threads can concurrently modify a WebAssembly shared
memory, the underlying data for a Wiggle `GuestSlice` and
`GuestSliceMut` could change due to access from other threads. This
breaks Rust guarantees when `&[T]` and `&mut [T]` slices are handed out.
This change modifies `GuestPtr` to make `as_slice` and `as_slice_mut`
return an `Option` which is `None` when the underlying WebAssembly
memory is shared.

But WASI implementations still need access to the underlying WebAssembly
memory, both to read to it and write from it. This change adds new APIs:
- `GuestPtr::to_vec` copies the  bytes from WebAssembly memory (from
  which we can safely take a `&[T]`)
- `GuestPtr::as_unsafe_slice_mut` returns a wrapper `struct` from which
  we can  `unsafe`-ly return a mutable slice (users must accept the
  unsafety of concurrently modifying a `&mut [T]`)

This approach allows us to maintain Wiggle's borrow-checking
infrastructure, which enforces the guarantee that Wiggle will not modify
overlapping regions, e.g. This is important because the underlying
system calls may expect this. Though other threads may modify the same
underlying region, this is impossible to prevent; at least Wiggle will
not be able to do so.

Finally, the changes to Wiggle's API are propagated to all WASI
implementations in Wasmtime. For now, code locations that attempt to get
a guest slice will panic if the underlying memory is shared. Note that
Wiggle is not enabled for shared memory (that will come later in
something like #5054), but when it is, these panics will be clear
indicators of locations that must be re-implemented in a thread-safe
way.

* review: remove double cast

* review: refactor to include more logic in 'UnsafeGuestSlice'

* review: add reference to #4203

* review: link all thread-safe WASI fixups to #5235

* fix: consume 'UnsafeGuestSlice' during conversion to safe versions

* review: remove 'as_slice' and 'as_slice_mut'

* review: use 'as_unsafe_slice_mut' in 'to_vec'

* review: add `UnsafeBorrowResult`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wasm-proposal:threads Issues related to the WebAssembly threads proposal
Projects
None yet
Development

No branches or pull requests

2 participants