Skip to content

Commit

Permalink
Always flush when writing, even if we get invalid pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Aug 28, 2019
1 parent 7d05d3c commit 6767087
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions lib/wasi/src/syscalls/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub(crate) fn get_wasi_state(ctx: &Ctx) -> &mut WasiState {
unsafe { state::get_wasi_state(&mut *(ctx as *const Ctx as *mut Ctx)) }
}

fn write_bytes<T: Write>(
fn write_bytes_inner<T: Write>(
mut write_loc: T,
memory: &Memory,
iovs_arr_cell: &[Cell<__wasi_ciovec_t>],
Expand All @@ -44,19 +44,25 @@ fn write_bytes<T: Write>(
let iov_inner = iov.get();
let bytes = iov_inner.buf.deref(memory, 0, iov_inner.buf_len)?;
write_loc
.write(&bytes.iter().map(|b_cell| b_cell.get()).collect::<Vec<u8>>())
.map_err(|_| {
write_loc.flush();
__WASI_EIO
})?;
.write_all(&bytes.iter().map(|b_cell| b_cell.get()).collect::<Vec<u8>>())
.map_err(|_| __WASI_EIO)?;

// TODO: handle failure more accurately
bytes_written += iov_inner.buf_len;
}
write_loc.flush();
Ok(bytes_written)
}

fn write_bytes<T: Write>(
mut write_loc: T,
memory: &Memory,
iovs_arr_cell: &[Cell<__wasi_ciovec_t>],
) -> Result<u32, __wasi_errno_t> {
let result = write_bytes_inner(&mut write_loc, memory, iovs_arr_cell);
write_loc.flush();
result
}

fn read_bytes<T: Read>(
mut reader: T,
memory: &Memory,
Expand Down

0 comments on commit 6767087

Please sign in to comment.