wasi-abi: one-more try with getting fd_read write
Forgot to correctly take into account the iovec buffer size: the contents of the file-being-read should be cut-off to the size of the input buffer with the length of what was actually read returned correctly.
[ci skip]
@@ -1778,13 +1778,15 @@ impl WASMIRuntimeState {
let offset: FileSize = args.nth(3);
let address: u32 = args.nth(4);
- match self.fd_pread_base(&fd, len as usize, &offset) {
- Ok(content) => {
- self.write_buffer(buf, content.as_slice())?;
- self.write_buffer(address, &(ErrNo::Success as u16).to_le_bytes())?;
+ let result = self.fd_pread_base(&fd, len as usize, &offset)?;
+
+ if result.len() < len as usize {
+ self.write_buffer(buf, &result)?;
+ self.write_buffer(address, &u32::to_le_bytes(result.len() as u32))?;
+ } else {
+ self.write_buffer(buf, &result[0..len as usize])?;
+ self.write_buffer(address, &u32::to_le_bytes(len))?;
}
- Err(e) => self.write_buffer(address, &u16::from(e).to_le_bytes())?,
- };
Ok(ErrNo::Success)
}
@@ -1811,8 +1813,6 @@ impl WASMIRuntimeState {
}
/// The implementation of the WASI `fd_prestat_dir_name` function.
- ///
- /// XXX: may panic if `u32` and `usize` bitwidths are different!
fn wasi_fd_prestat_dir_name(&mut self, args: RuntimeArgs) -> WASIError {
if args.len() != 3 {
return Err(RuntimePanic::bad_arguments_to_host_function(
@@ -1848,6 +1848,7 @@ impl WASMIRuntimeState {
}
let address: u32 = args.nth(3);
+
self.write_buffer(address, &u32::to_le_bytes(0u32))?;
Ok(ErrNo::Success)
@@ -1869,8 +1870,13 @@ impl WASMIRuntimeState {
let result = self.fd_read_base(&fd, len as usize)?;
+ if result.len() < len as usize {
self.write_buffer(buf, &result)?;
self.write_buffer(address, &u32::to_le_bytes(result.len() as u32))?;
+ } else {
+ self.write_buffer(buf, &result[0..(len as usize)])?;
+ self.write_buffer(address, &u32::to_le_bytes(len))?;
+ }
Ok(ErrNo::Success)
}
