wiggle: adapt Wiggle guest slices for unsafe shared use (#5229)
* 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`
This commit is contained in:
@@ -468,14 +468,16 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ)?;
|
||||
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?)
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> =
|
||||
iovs.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?.expect(
|
||||
"cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)",
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
let mut ioslices: Vec<IoSliceMut> = guest_slices
|
||||
.iter_mut()
|
||||
@@ -497,14 +499,16 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ | FileCaps::SEEK)?;
|
||||
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?)
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> =
|
||||
iovs.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?.expect(
|
||||
"cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)",
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
let mut ioslices: Vec<IoSliceMut> = guest_slices
|
||||
.iter_mut()
|
||||
@@ -530,7 +534,11 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Ciovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice()?)
|
||||
Ok(iov
|
||||
.buf
|
||||
.as_array(iov.buf_len)
|
||||
.as_slice()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
@@ -559,7 +567,11 @@ impl wasi_unstable::WasiUnstable for WasiCtx {
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Ciovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice()?)
|
||||
Ok(iov
|
||||
.buf
|
||||
.as_array(iov.buf_len)
|
||||
.as_slice()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
|
||||
@@ -521,14 +521,16 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ)?;
|
||||
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?)
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> =
|
||||
iovs.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?.expect(
|
||||
"cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)",
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
let mut ioslices: Vec<IoSliceMut> = guest_slices
|
||||
.iter_mut()
|
||||
@@ -550,14 +552,16 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ | FileCaps::SEEK)?;
|
||||
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = iovs
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?)
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> =
|
||||
iovs.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?.expect(
|
||||
"cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)",
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
let mut ioslices: Vec<IoSliceMut> = guest_slices
|
||||
.iter_mut()
|
||||
@@ -583,7 +587,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Ciovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice()?)
|
||||
Ok(iov
|
||||
.buf
|
||||
.as_array(iov.buf_len)
|
||||
.as_slice()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
@@ -612,7 +620,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Ciovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice()?)
|
||||
Ok(iov
|
||||
.buf
|
||||
.as_array(iov.buf_len)
|
||||
.as_slice()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
@@ -654,7 +666,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
if path_len < path_max_len as usize {
|
||||
return Err(Error::name_too_long());
|
||||
}
|
||||
let mut p_memory = path.as_array(path_len as u32).as_slice_mut()?;
|
||||
let mut p_memory = path
|
||||
.as_array(path_len as u32)
|
||||
.as_slice_mut()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)");
|
||||
p_memory.copy_from_slice(path_bytes);
|
||||
Ok(())
|
||||
} else {
|
||||
@@ -948,7 +963,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
if link_len > buf_len as usize {
|
||||
return Err(Error::range());
|
||||
}
|
||||
let mut buf = buf.as_array(link_len as u32).as_slice_mut()?;
|
||||
let mut buf = buf
|
||||
.as_array(link_len as u32)
|
||||
.as_slice_mut()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)");
|
||||
buf.copy_from_slice(link_bytes);
|
||||
Ok(link_len as types::Size)
|
||||
}
|
||||
@@ -1236,7 +1254,10 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
buf: &GuestPtr<'a, u8>,
|
||||
buf_len: types::Size,
|
||||
) -> Result<(), Error> {
|
||||
let mut buf = buf.as_array(buf_len).as_slice_mut()?;
|
||||
let mut buf = buf
|
||||
.as_array(buf_len)
|
||||
.as_slice_mut()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)");
|
||||
self.random.try_fill_bytes(buf.deref_mut())?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -1273,14 +1294,17 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.get_file_mut(u32::from(fd))?
|
||||
.get_cap_mut(FileCaps::READ)?;
|
||||
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> = ri_data
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?)
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
let mut guest_slices: Vec<wiggle::GuestSliceMut<u8>> =
|
||||
ri_data
|
||||
.iter()
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Iovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice_mut()?.expect(
|
||||
"cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)",
|
||||
))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
let mut ioslices: Vec<IoSliceMut> = guest_slices
|
||||
.iter_mut()
|
||||
@@ -1307,7 +1331,11 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
|
||||
.map(|iov_ptr| {
|
||||
let iov_ptr = iov_ptr?;
|
||||
let iov: types::Ciovec = iov_ptr.read()?;
|
||||
Ok(iov.buf.as_array(iov.buf_len).as_slice()?)
|
||||
Ok(iov
|
||||
.buf
|
||||
.as_array(iov.buf_len)
|
||||
.as_slice()?
|
||||
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
|
||||
})
|
||||
.collect::<Result<_, Error>>()?;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user