add pread on linux

This commit is contained in:
Mark McCaskey 2019-03-29 15:10:46 -07:00
parent 88212d356f
commit 35fbf573c9
5 changed files with 78 additions and 1 deletions

View File

@ -248,6 +248,7 @@ pub fn fd_filestat_set_times(
) -> __wasi_errno_t {
unimplemented!()
}
pub fn fd_pread(
ctx: &mut Ctx,
fd: __wasi_fd_t,
@ -256,8 +257,17 @@ pub fn fd_pread(
offset: __wasi_filesize_t,
nread: WasmPtr<u32>,
) -> __wasi_errno_t {
unimplemented!()
let memory = ctx.memory(0);
if let ((Some(iov_cells), Some(nread_cell))) =
(iovs.deref(memory, 0, iovs_len), nread.deref(memory))
{
platform_fd_pread(fd, iov_cells, iovs_len, offset, nread_cell)
} else {
__WASI_EFAULT
}
}
pub fn fd_prestat_get(
ctx: &mut Ctx,
fd: __wasi_fd_t,

View File

@ -1,6 +1,7 @@
#![allow(non_camel_case_types)]
use crate::ptr::{Array, WasmPtr};
use std::mem;
use wasmer_runtime_core::types::{ValueError, ValueType};
pub type __wasi_advice_t = u8;
@ -212,6 +213,25 @@ pub struct __wasi_iovec_t {
pub buf_len: u32,
}
impl ValueType for __wasi_iovec_t {
fn into_le(self, buffer: &mut [u8]) {
self.buf
.into_le(&mut buffer[..mem::size_of::<WasmPtr<u8, Array>>()]);
self.buf_len
.into_le(&mut buffer[mem::size_of::<WasmPtr<u8, Array>>()..]);
}
fn from_le(buffer: &[u8]) -> Result<Self, ValueError> {
if buffer.len() >= mem::size_of::<__wasi_iovec_t>() {
let buf = ValueType::from_le(&buffer[..mem::size_of::<WasmPtr<u8, Array>>()])?;
let buf_len = ValueType::from_le(&buffer[mem::size_of::<WasmPtr<u8, Array>>()..])?;
Ok(Self { buf, buf_len })
} else {
Err(ValueError::BufferTooSmall)
}
}
}
pub type __wasi_linkcount_t = u32;
pub type __wasi_lookupflags_t = u32;

View File

@ -1 +1,27 @@
use crate::syscalls::types::*;
use std::cell::Cell;
use libc::preadv;
pub fn platform_fd_pread(
fd: __wasi_fd_t,
iovs: &[Cell<__wasi_iovec_t>],
iovs_len: u32,
offset: __wasi_filesize_t,
nread: &Cell<u32>,
) -> __wasi_errno_t {
let (result, iovec) = unsafe {
let mut iovec = vec![mem::uninitialized(); iovs_len as usize];
(preadv(fd, &mut iovec, iovs_len, offset), iovec)
};
nread.set(result);
for (arr_cell, i) in iov_arr.iter().enumerate() {
let wasi_iovec = __wasi_iovec_t {
buf: iovec[i] as _,
buf_len: iovec[i].iov_len as u32,
};
arr_cell.set(wasi_iovec);
}
__WASI_ESUCCESS
}

View File

@ -1 +1,12 @@
use crate::syscalls::types::*;
use std::cell::Cell;
pub fn platform_fd_pread(
fd: __wasi_fd_t,
iovs: &[Cell<__wasi_iovec_t>],
iovs_len: u32,
offset: __wasi_filesize_t,
nread: &Cell<u32>,
) -> __wasi_errno_t {
unimplemented!()
}

View File

@ -15,3 +15,13 @@ pub fn platform_clock_time_get(
) -> __wasi_errno_t {
unimplemented!()
}
pub fn platform_fd_pread(
fd: __wasi_fd_t,
iovs: &[Cell<__wasi_iovec_t>],
iovs_len: u32,
offset: __wasi_filesize_t,
nread: &Cell<u32>,
) -> __wasi_errno_t {
unimplemented!()
}