Skip to content

Commit

Permalink
Merge #3045
Browse files Browse the repository at this point in the history
3045: Fixed WASI fd_read syscall when reading multiple iovs and read is partial (for #2904) r=ptitSeb a=ptitSeb

# Description
Fixed an issue with WASI fd_read: when reading on multiple iovs, if a read is partial (so not all the wanted bytes are read), the function would still continue to try reading on subsequent iov (eventualy reading more bytes but not in the right place).

Co-authored-by: ptitSeb <[email protected]>
  • Loading branch information
bors[bot] and ptitSeb authored Jul 27, 2022
2 parents 72a7348 + 9c868b3 commit a1e82a5
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,18 @@ pub(crate) fn read_bytes<T: Read, M: MemorySize>(
for iov in iovs_arr.iter() {
let iov_inner = iov.read().map_err(mem_error_to_wasi)?;
raw_bytes.clear();
raw_bytes.resize(from_offset::<M>(iov_inner.buf_len)?, 0);
bytes_read += reader.read(&mut raw_bytes).map_err(map_io_err)?;
let to_read = from_offset::<M>(iov_inner.buf_len)?;
raw_bytes.resize(to_read, 0);
let has_read = reader.read(&mut raw_bytes).map_err(map_io_err)?;

let buf = WasmPtr::<u8, M>::new(iov_inner.buf)
.slice(ctx, memory, iov_inner.buf_len)
.map_err(mem_error_to_wasi)?;
buf.write_slice(&raw_bytes).map_err(mem_error_to_wasi)?;
bytes_read += has_read;
if has_read != to_read {
return Ok(bytes_read);
}
}
Ok(bytes_read)
}
Expand Down Expand Up @@ -1113,7 +1118,7 @@ pub fn fd_read<M: MemorySize>(
trace!("wasi::fd_read: fd={}", fd);
let env = ctx.data();
let (memory, mut state, inodes) = env.get_memory_and_wasi_state_and_inodes(0);

//let iovs_len = if iovs_len > M::Offset::from(1u32) { M::Offset::from(1u32) } else { iovs_len };
let iovs_arr = wasi_try_mem_ok!(iovs.slice(&ctx, memory, iovs_len));
let nread_ref = nread.deref(&ctx, memory);

Expand Down Expand Up @@ -1240,7 +1245,6 @@ pub fn fd_read<M: MemorySize>(
bytes_read
}
};

let bytes_read: M::Offset = wasi_try_ok!(bytes_read.try_into().map_err(|_| __WASI_EOVERFLOW));
wasi_try_mem_ok!(nread_ref.write(bytes_read));

Expand Down

0 comments on commit a1e82a5

Please sign in to comment.