Skip to content

Commit

Permalink
fix: copy bytes from shared memory for fd_write
Browse files Browse the repository at this point in the history
Previous Wiggle changes (bytecodealliance#5229, bytecodealliance#5264) made it possible to access slices
of WebAssembly linear memory from Wiggle but limited the interface so
that slices of shared memory could only be accessed either `unsafe`-ly
or via copying (`GuestPtr::to_vec`). This change modifies `fd_write` to
unconditionally copy the bytes to be written before passing them to
Rust's standard library in an `IoSlice`. This is likely not the optimal
solution but enables further `wasi-threads` development. In the future
this commit should probably change to conditionally copy the bytes when
shared memory is detected and expand to fix up all the `expect` errors
of the same kind in `preview_1.rs`.
  • Loading branch information
abrown committed Dec 20, 2022
1 parent 1612bd6 commit 1277f87
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions crates/wasi-common/src/snapshots/preview_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,12 @@ impl wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
let f = self.table().get_file(u32::from(fd))?;
let f = f.get_cap(FileCaps::WRITE)?;

let guest_slices: Vec<wiggle::GuestSlice<u8>> = ciovs
let guest_slices: Vec<Vec<u8>> = ciovs
.iter()
.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()?
.expect("cannot use with shared memories; see https://github.com/bytecodealliance/wasmtime/issues/5235 (TODO)"))
Ok(iov.buf.as_array(iov.buf_len).to_vec()?)
})
.collect::<Result<_, Error>>()?;

Expand Down

0 comments on commit 1277f87

Please sign in to comment.