From 2d2f01c8ca9742e076753b9876d93b9cf559ead5 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Thu, 8 Dec 2022 10:26:12 -0800 Subject: [PATCH] fix: copy bytes from shared memory for `fd_write` Previous Wiggle changes (#5229, #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`. --- crates/wasi-common/src/snapshots/preview_1.rs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/crates/wasi-common/src/snapshots/preview_1.rs b/crates/wasi-common/src/snapshots/preview_1.rs index 4776cb10c268..0f1377cbf9e6 100644 --- a/crates/wasi-common/src/snapshots/preview_1.rs +++ b/crates/wasi-common/src/snapshots/preview_1.rs @@ -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> = ciovs + let guest_slices: Vec> = 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::>()?;