Move common functionality into hostcalls mod
This commit is contained in:
893
src/hostcalls/fs.rs
Normal file
893
src/hostcalls/fs.rs
Normal file
@@ -0,0 +1,893 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::memory::*;
|
||||
use crate::sys::host_impl;
|
||||
use crate::sys::hostcalls_impl;
|
||||
use crate::{host, wasm32};
|
||||
|
||||
use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
if let Some(fdent) = wasi_ctx.fds.get(&fd) {
|
||||
// can't close preopened files
|
||||
if fdent.preopen_path.is_some() {
|
||||
return wasm32::__WASI_ENOTSUP;
|
||||
}
|
||||
}
|
||||
if let Some(mut fdent) = wasi_ctx.fds.remove(&fd) {
|
||||
fdent.fd_object.needs_close = false;
|
||||
match hostcalls_impl::fd_close(fdent) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
} else {
|
||||
wasm32::__WASI_EBADF
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_DATASYNC;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
match hostcalls_impl::fd_datasync(fe) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_pread(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
iovs_ptr: wasm32::uintptr_t,
|
||||
iovs_len: wasm32::size_t,
|
||||
offset: wasm32::__wasi_filesize_t,
|
||||
nread: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let iovs = match dec_iovec_slice(memory, iovs_ptr, iovs_len) {
|
||||
Ok(iovs) => iovs,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_FD_READ;
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let offset = dec_filesize(offset);
|
||||
if offset > i64::max_value() as u64 {
|
||||
return wasm32::__WASI_EIO;
|
||||
}
|
||||
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
|
||||
let mut buf = vec![0; buf_size];
|
||||
let host_nread = match hostcalls_impl::fd_pread(fe, &mut buf, offset) {
|
||||
Ok(host_nread) => host_nread,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let mut buf_offset = 0;
|
||||
let mut left = host_nread;
|
||||
for iov in &iovs {
|
||||
if left == 0 {
|
||||
break;
|
||||
}
|
||||
let vec_len = std::cmp::min(iov.buf_len, left);
|
||||
unsafe { std::slice::from_raw_parts_mut(iov.buf as *mut u8, vec_len) }
|
||||
.copy_from_slice(&buf[buf_offset..buf_offset + vec_len]);
|
||||
buf_offset += vec_len;
|
||||
left -= vec_len;
|
||||
}
|
||||
enc_usize_byref(memory, nread, host_nread)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_pwrite(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
iovs_ptr: wasm32::uintptr_t,
|
||||
iovs_len: wasm32::size_t,
|
||||
offset: wasm32::__wasi_filesize_t,
|
||||
nwritten: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let iovs = match dec_iovec_slice(memory, iovs_ptr, iovs_len) {
|
||||
Ok(iovs) => iovs,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_FD_READ;
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let offset = dec_filesize(offset);
|
||||
if offset > i64::max_value() as u64 {
|
||||
return wasm32::__WASI_EIO;
|
||||
}
|
||||
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
|
||||
let mut buf = Vec::with_capacity(buf_size);
|
||||
for iov in &iovs {
|
||||
buf.extend_from_slice(unsafe {
|
||||
std::slice::from_raw_parts(iov.buf as *const u8, iov.buf_len)
|
||||
});
|
||||
}
|
||||
let host_nwritten = match hostcalls_impl::fd_pwrite(fe, &buf, offset) {
|
||||
Ok(host_nwritten) => host_nwritten,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
enc_usize_byref(memory, nwritten, host_nwritten)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_read(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
iovs_ptr: wasm32::uintptr_t,
|
||||
iovs_len: wasm32::size_t,
|
||||
nread: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let mut iovs = match dec_iovec_slice(memory, iovs_ptr, iovs_len) {
|
||||
Ok(iovs) => iovs,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, host::__WASI_RIGHT_FD_READ, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
let host_nread = match hostcalls_impl::fd_read(fe, &mut iovs) {
|
||||
Ok(host_nread) => host_nread,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
if host_nread == 0 {
|
||||
// we hit eof, so remove the fdentry from the context
|
||||
let mut fe = wasi_ctx.fds.remove(&fd).expect("file entry is still there");
|
||||
fe.fd_object.needs_close = false;
|
||||
}
|
||||
|
||||
enc_usize_byref(memory, nread, host_nread)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_renumber(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
from: wasm32::__wasi_fd_t,
|
||||
to: wasm32::__wasi_fd_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let from = dec_fd(from);
|
||||
let to = dec_fd(to);
|
||||
|
||||
match hostcalls_impl::fd_renumber(wasi_ctx, from, to) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_seek(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
offset: wasm32::__wasi_filedelta_t,
|
||||
whence: wasm32::__wasi_whence_t,
|
||||
newoffset: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let offset = dec_filedelta(offset);
|
||||
let whence = dec_whence(whence);
|
||||
|
||||
let rights = if offset == 0 && whence == host::__WASI_WHENCE_CUR {
|
||||
host::__WASI_RIGHT_FD_TELL
|
||||
} else {
|
||||
host::__WASI_RIGHT_FD_SEEK | host::__WASI_RIGHT_FD_TELL
|
||||
};
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_newoffset = match hostcalls_impl::fd_seek(fe, offset, whence) {
|
||||
Ok(host_newoffset) => host_newoffset,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_filesize_byref(memory, newoffset, host_newoffset)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_tell(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
newoffset: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_TELL;
|
||||
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_offset = match hostcalls_impl::fd_tell(fe) {
|
||||
Ok(host_offset) => host_offset,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_filesize_byref(memory, newoffset, host_offset)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_fdstat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
fdstat_ptr: wasm32::uintptr_t, // *mut wasm32::__wasi_fdstat_t
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let mut host_fdstat = match dec_fdstat_byref(memory, fdstat_ptr) {
|
||||
Ok(host_fdstat) => host_fdstat,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
let errno = if let Some(fe) = wasi_ctx.fds.get(&host_fd) {
|
||||
host_fdstat.fs_filetype = fe.fd_object.ty;
|
||||
host_fdstat.fs_rights_base = fe.rights_base;
|
||||
host_fdstat.fs_rights_inheriting = fe.rights_inheriting;
|
||||
host_fdstat.fs_flags = match hostcalls_impl::fd_fdstat_get(fe) {
|
||||
Ok(flags) => flags,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
wasm32::__WASI_ESUCCESS
|
||||
} else {
|
||||
wasm32::__WASI_EBADF
|
||||
};
|
||||
|
||||
if let Err(e) = enc_fdstat_byref(memory, fdstat_ptr, host_fdstat) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
|
||||
errno
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_fdstat_set_flags(
|
||||
wasi_ctx: &WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
fdflags: wasm32::__wasi_fdflags_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let host_fdflags = dec_fdflags(fdflags);
|
||||
match wasi_ctx.fds.get(&host_fd) {
|
||||
Some(fe) => match hostcalls_impl::fd_fdstat_set_flags(fe, host_fdflags) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
},
|
||||
None => wasm32::__WASI_EBADF,
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_fdstat_set_rights(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
fs_rights_base: wasm32::__wasi_rights_t,
|
||||
fs_rights_inheriting: wasm32::__wasi_rights_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let fe = match wasi_ctx.fds.get_mut(&host_fd) {
|
||||
Some(fe) => fe,
|
||||
None => return wasm32::__WASI_EBADF,
|
||||
};
|
||||
if fe.rights_base & fs_rights_base != fs_rights_base
|
||||
|| fe.rights_inheriting & fs_rights_inheriting != fs_rights_inheriting
|
||||
{
|
||||
return wasm32::__WASI_ENOTCAPABLE;
|
||||
}
|
||||
|
||||
fe.rights_base = fs_rights_base;
|
||||
fe.rights_inheriting = fs_rights_inheriting;
|
||||
wasm32::__WASI_ESUCCESS
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_SYNC;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
match hostcalls_impl::fd_sync(fe) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_write(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
iovs_ptr: wasm32::uintptr_t,
|
||||
iovs_len: wasm32::size_t,
|
||||
nwritten: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
let iovs = match dec_iovec_slice(memory, iovs_ptr, iovs_len) {
|
||||
Ok(iovs) => iovs,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, host::__WASI_RIGHT_FD_WRITE, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_nwritten = match hostcalls_impl::fd_write(fe, &iovs) {
|
||||
Ok(host_nwritten) => host_nwritten,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_usize_byref(memory, nwritten, host_nwritten)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_advise(
|
||||
wasi_ctx: &WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
offset: wasm32::__wasi_filesize_t,
|
||||
len: wasm32::__wasi_filesize_t,
|
||||
advice: wasm32::__wasi_advice_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_ADVISE;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let advice = dec_advice(advice);
|
||||
let offset = dec_filesize(offset);
|
||||
let len = dec_filesize(len);
|
||||
|
||||
match hostcalls_impl::fd_advise(fe, advice, offset, len) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_allocate(
|
||||
wasi_ctx: &WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
offset: wasm32::__wasi_filesize_t,
|
||||
len: wasm32::__wasi_filesize_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_ALLOCATE;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let offset = dec_filesize(offset);
|
||||
let len = dec_filesize(len);
|
||||
|
||||
match hostcalls_impl::fd_allocate(fe, offset, len) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_create_directory(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match hostcalls_impl::path_create_directory(wasi_ctx, dirfd, &path) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_link(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
old_dirfd: wasm32::__wasi_fd_t,
|
||||
_old_flags: wasm32::__wasi_lookupflags_t,
|
||||
old_path_ptr: wasm32::uintptr_t,
|
||||
old_path_len: wasm32::size_t,
|
||||
new_dirfd: wasm32::__wasi_fd_t,
|
||||
new_path_ptr: wasm32::uintptr_t,
|
||||
new_path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let old_dirfd = dec_fd(old_dirfd);
|
||||
let new_dirfd = dec_fd(new_dirfd);
|
||||
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match hostcalls_impl::path_link(
|
||||
wasi_ctx,
|
||||
old_dirfd,
|
||||
new_dirfd,
|
||||
&old_path,
|
||||
&new_path,
|
||||
host::__WASI_RIGHT_PATH_LINK_SOURCE,
|
||||
host::__WASI_RIGHT_PATH_LINK_TARGET,
|
||||
) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_open(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
dirflags: wasm32::__wasi_lookupflags_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
oflags: wasm32::__wasi_oflags_t,
|
||||
fs_rights_base: wasm32::__wasi_rights_t,
|
||||
fs_rights_inheriting: wasm32::__wasi_rights_t,
|
||||
fs_flags: wasm32::__wasi_fdflags_t,
|
||||
fd_out_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let dirflags = dec_lookupflags(dirflags);
|
||||
let oflags = dec_oflags(oflags);
|
||||
let fs_rights_base = dec_rights(fs_rights_base);
|
||||
let fs_rights_inheriting = dec_rights(fs_rights_inheriting);
|
||||
let fs_flags = dec_fdflags(fs_flags);
|
||||
|
||||
// which open mode do we need?
|
||||
let read = fs_rights_base & (host::__WASI_RIGHT_FD_READ | host::__WASI_RIGHT_FD_READDIR) != 0;
|
||||
let write = fs_rights_base
|
||||
& (host::__WASI_RIGHT_FD_DATASYNC
|
||||
| host::__WASI_RIGHT_FD_WRITE
|
||||
| host::__WASI_RIGHT_FD_ALLOCATE
|
||||
| host::__WASI_RIGHT_FD_FILESTAT_SET_SIZE)
|
||||
!= 0;
|
||||
|
||||
// which rights are needed on the dirfd?
|
||||
let needed_base = host::__WASI_RIGHT_PATH_OPEN;
|
||||
let needed_inheriting = fs_rights_base | fs_rights_inheriting;
|
||||
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
let fe = match hostcalls_impl::path_open(
|
||||
wasi_ctx,
|
||||
dirfd,
|
||||
dirflags,
|
||||
&path,
|
||||
oflags,
|
||||
read,
|
||||
write,
|
||||
needed_base,
|
||||
needed_inheriting,
|
||||
fs_flags,
|
||||
) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
let guest_fd = match wasi_ctx.insert_fd_entry(fe) {
|
||||
Ok(fd) => fd,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_fd_byref(memory, fd_out_ptr, guest_fd)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_readdir(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
buf: wasm32::uintptr_t,
|
||||
buf_len: wasm32::size_t,
|
||||
cookie: wasm32::__wasi_dircookie_t,
|
||||
buf_used: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
match enc_usize_byref(memory, buf_used, 0) {
|
||||
Ok(_) => {}
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_READDIR;
|
||||
let fe = match wasi_ctx.get_fd_entry(fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_buf = match dec_slice_of_mut::<u8>(memory, buf, buf_len) {
|
||||
Ok(host_buf) => host_buf,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let cookie = dec_dircookie(cookie);
|
||||
|
||||
let host_bufused = match hostcalls_impl::fd_readdir(fe, host_buf, cookie) {
|
||||
Ok(host_bufused) => host_bufused,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_usize_byref(memory, buf_used, host_bufused)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_readlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
buf_ptr: wasm32::uintptr_t,
|
||||
buf_len: wasm32::size_t,
|
||||
buf_used: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
match enc_usize_byref(memory, buf_used, 0) {
|
||||
Ok(_) => {}
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice).to_owned(),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_PATH_READLINK;
|
||||
let mut buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) {
|
||||
Ok(slice) => slice,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_bufused =
|
||||
match hostcalls_impl::path_readlink(wasi_ctx, dirfd, path.as_os_str(), rights, &mut buf) {
|
||||
Ok(host_bufused) => host_bufused,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
match enc_usize_byref(memory, buf_used, host_bufused) {
|
||||
Ok(_) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_rename(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
old_dirfd: wasm32::__wasi_fd_t,
|
||||
old_path_ptr: wasm32::uintptr_t,
|
||||
old_path_len: wasm32::size_t,
|
||||
new_dirfd: wasm32::__wasi_fd_t,
|
||||
new_path_ptr: wasm32::uintptr_t,
|
||||
new_path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let old_dirfd = dec_fd(old_dirfd);
|
||||
let new_dirfd = dec_fd(new_dirfd);
|
||||
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let old_rights = host::__WASI_RIGHT_PATH_RENAME_SOURCE;
|
||||
let new_rights = host::__WASI_RIGHT_PATH_RENAME_TARGET;
|
||||
|
||||
match hostcalls_impl::path_rename(
|
||||
wasi_ctx, old_dirfd, &old_path, old_rights, new_dirfd, &new_path, new_rights,
|
||||
) {
|
||||
Ok(()) => host::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_filestat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let fe = match wasi_ctx.fds.get(&host_fd) {
|
||||
Some(fe) => fe,
|
||||
None => return wasm32::__WASI_EBADF,
|
||||
};
|
||||
|
||||
let host_filestat = match hostcalls_impl::fd_filestat_get(fe) {
|
||||
Ok(fstat) => fstat,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match enc_filestat_byref(memory, filestat_ptr, host_filestat) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_filestat_set_times(
|
||||
wasi_ctx: &WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
st_atim: wasm32::__wasi_timestamp_t,
|
||||
st_mtim: wasm32::__wasi_timestamp_t,
|
||||
fst_flags: wasm32::__wasi_fstflags_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_FILESTAT_SET_TIMES;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let st_atim = dec_timestamp(st_atim);
|
||||
let st_mtim = dec_timestamp(st_mtim);
|
||||
let fst_flags = dec_fstflags(fst_flags);
|
||||
|
||||
match hostcalls_impl::fd_filestat_set_times(fe, st_atim, st_mtim, fst_flags) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_filestat_set_size(
|
||||
wasi_ctx: &WasiCtx,
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
st_size: wasm32::__wasi_filesize_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let host_fd = dec_fd(fd);
|
||||
let rights = host::__WASI_RIGHT_FD_FILESTAT_SET_SIZE;
|
||||
let fe = match wasi_ctx.get_fd_entry(host_fd, rights, 0) {
|
||||
Ok(fe) => fe,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let st_size = dec_filesize(st_size);
|
||||
if st_size > i64::max_value() as u64 {
|
||||
return wasm32::__WASI_E2BIG;
|
||||
}
|
||||
|
||||
match hostcalls_impl::fd_filestat_set_size(fe, st_size) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_filestat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
dirflags: wasm32::__wasi_lookupflags_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
filestat_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let dirflags = dec_lookupflags(dirflags);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let host_filestat = match hostcalls_impl::path_filestat_get(wasi_ctx, dirfd, dirflags, &path) {
|
||||
Ok(host_filestat) => host_filestat,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match enc_filestat_byref(memory, filestat_ptr, host_filestat) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_filestat_set_times(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
dirflags: wasm32::__wasi_lookupflags_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
st_atim: wasm32::__wasi_timestamp_t,
|
||||
st_mtim: wasm32::__wasi_timestamp_t,
|
||||
fst_flags: wasm32::__wasi_fstflags_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let dirflags = dec_lookupflags(dirflags);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_PATH_FILESTAT_SET_TIMES;
|
||||
let st_atim = dec_timestamp(st_atim);
|
||||
let st_mtim = dec_timestamp(st_mtim);
|
||||
let fst_flags = dec_fstflags(fst_flags);
|
||||
|
||||
match hostcalls_impl::path_filestat_set_times(
|
||||
wasi_ctx, dirfd, dirflags, &path, rights, st_atim, st_mtim, fst_flags,
|
||||
) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_symlink(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
old_path_ptr: wasm32::uintptr_t,
|
||||
old_path_len: wasm32::size_t,
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
new_path_ptr: wasm32::uintptr_t,
|
||||
new_path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let old_path = match dec_slice_of::<u8>(memory, old_path_ptr, old_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let new_path = match dec_slice_of::<u8>(memory, new_path_ptr, new_path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_PATH_SYMLINK;
|
||||
|
||||
match hostcalls_impl::path_symlink(wasi_ctx, dirfd, rights, &old_path, &new_path) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_unlink_file(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match hostcalls_impl::path_unlink_file(
|
||||
wasi_ctx,
|
||||
dirfd,
|
||||
&path,
|
||||
host::__WASI_RIGHT_PATH_UNLINK_FILE,
|
||||
) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn path_remove_directory(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
dirfd: wasm32::__wasi_fd_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let dirfd = dec_fd(dirfd);
|
||||
let path = match dec_slice_of::<u8>(memory, path_ptr, path_len) {
|
||||
Ok(slice) => host_impl::path_from_raw(slice),
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let rights = host::__WASI_RIGHT_PATH_REMOVE_DIRECTORY;
|
||||
|
||||
match hostcalls_impl::path_remove_directory(wasi_ctx, dirfd, &path, rights) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_prestat_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
prestat_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
// TODO: is this the correct right for this?
|
||||
match wasi_ctx.get_fd_entry(fd, host::__WASI_RIGHT_PATH_OPEN.into(), 0) {
|
||||
Ok(fe) => {
|
||||
if let Some(po_path) = &fe.preopen_path {
|
||||
if fe.fd_object.ty != host::__WASI_FILETYPE_DIRECTORY {
|
||||
return wasm32::__WASI_ENOTDIR;
|
||||
}
|
||||
enc_prestat_byref(
|
||||
memory,
|
||||
prestat_ptr,
|
||||
host::__wasi_prestat_t {
|
||||
pr_type: host::__WASI_PREOPENTYPE_DIR,
|
||||
u: host::__wasi_prestat_t___wasi_prestat_u {
|
||||
dir: host::__wasi_prestat_t___wasi_prestat_u___wasi_prestat_u_dir_t {
|
||||
pr_name_len: host_impl::path_to_raw(po_path).len(),
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(|e| e)
|
||||
} else {
|
||||
wasm32::__WASI_ENOTSUP
|
||||
}
|
||||
}
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn fd_prestat_dir_name(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
fd: wasm32::__wasi_fd_t,
|
||||
path_ptr: wasm32::uintptr_t,
|
||||
path_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let fd = dec_fd(fd);
|
||||
|
||||
match wasi_ctx.get_fd_entry(fd, host::__WASI_RIGHT_PATH_OPEN.into(), 0) {
|
||||
Ok(fe) => {
|
||||
if let Some(po_path) = &fe.preopen_path {
|
||||
if fe.fd_object.ty != host::__WASI_FILETYPE_DIRECTORY {
|
||||
return wasm32::__WASI_ENOTDIR;
|
||||
}
|
||||
let path_bytes = host_impl::path_to_raw(po_path);
|
||||
if path_bytes.len() > dec_usize(path_len) {
|
||||
return wasm32::__WASI_ENAMETOOLONG;
|
||||
}
|
||||
enc_slice_of(memory, &path_bytes, path_ptr)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(|e| e)
|
||||
} else {
|
||||
wasm32::__WASI_ENOTSUP
|
||||
}
|
||||
}
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
239
src/hostcalls/misc.rs
Normal file
239
src/hostcalls/misc.rs
Normal file
@@ -0,0 +1,239 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::memory::*;
|
||||
use crate::sys::hostcalls_impl;
|
||||
use crate::wasm32;
|
||||
use std::convert::TryFrom;
|
||||
|
||||
use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn args_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
argv_ptr: wasm32::uintptr_t,
|
||||
argv_buf: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let mut argv_buf_offset = 0;
|
||||
let mut argv = vec![];
|
||||
|
||||
for arg in wasi_ctx.args.iter() {
|
||||
let arg_bytes = arg.as_bytes_with_nul();
|
||||
let arg_ptr = argv_buf + argv_buf_offset;
|
||||
|
||||
if let Err(e) = enc_slice_of(memory, arg_bytes, arg_ptr) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
|
||||
argv.push(arg_ptr);
|
||||
|
||||
argv_buf_offset = if let Some(new_offset) = argv_buf_offset.checked_add(
|
||||
wasm32::uintptr_t::try_from(arg_bytes.len())
|
||||
.expect("cast overflow would have been caught by `enc_slice_of` above"),
|
||||
) {
|
||||
new_offset
|
||||
} else {
|
||||
return wasm32::__WASI_EOVERFLOW;
|
||||
}
|
||||
}
|
||||
|
||||
enc_slice_of(memory, argv.as_slice(), argv_ptr)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn args_sizes_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
argc_ptr: wasm32::uintptr_t,
|
||||
argv_buf_size_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let argc = wasi_ctx.args.len();
|
||||
let argv_size = wasi_ctx
|
||||
.args
|
||||
.iter()
|
||||
.map(|arg| arg.as_bytes_with_nul().len())
|
||||
.sum();
|
||||
|
||||
if let Err(e) = enc_usize_byref(memory, argc_ptr, argc) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
if let Err(e) = enc_usize_byref(memory, argv_buf_size_ptr, argv_size) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
wasm32::__WASI_ESUCCESS
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn environ_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
environ_ptr: wasm32::uintptr_t,
|
||||
environ_buf: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let mut environ_buf_offset = 0;
|
||||
let mut environ = vec![];
|
||||
|
||||
for pair in wasi_ctx.env.iter() {
|
||||
let env_bytes = pair.as_bytes_with_nul();
|
||||
let env_ptr = environ_buf + environ_buf_offset;
|
||||
|
||||
if let Err(e) = enc_slice_of(memory, env_bytes, env_ptr) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
|
||||
environ.push(env_ptr);
|
||||
|
||||
environ_buf_offset = if let Some(new_offset) = environ_buf_offset.checked_add(
|
||||
wasm32::uintptr_t::try_from(env_bytes.len())
|
||||
.expect("cast overflow would have been caught by `enc_slice_of` above"),
|
||||
) {
|
||||
new_offset
|
||||
} else {
|
||||
return wasm32::__WASI_EOVERFLOW;
|
||||
}
|
||||
}
|
||||
|
||||
enc_slice_of(memory, environ.as_slice(), environ_ptr)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn environ_sizes_get(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
environ_count_ptr: wasm32::uintptr_t,
|
||||
environ_size_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let environ_count = wasi_ctx.env.len();
|
||||
if let Some(environ_size) = wasi_ctx.env.iter().try_fold(0, |acc: u32, pair| {
|
||||
acc.checked_add(pair.as_bytes_with_nul().len() as u32)
|
||||
}) {
|
||||
if let Err(e) = enc_usize_byref(memory, environ_count_ptr, environ_count) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
if let Err(e) = enc_usize_byref(memory, environ_size_ptr, environ_size as usize) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
wasm32::__WASI_ESUCCESS
|
||||
} else {
|
||||
wasm32::__WASI_EOVERFLOW
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn proc_exit(rval: wasm32::__wasi_exitcode_t) -> () {
|
||||
// TODO: Rather than call std::process::exit here, we should trigger a
|
||||
// stack unwind similar to a trap.
|
||||
std::process::exit(dec_exitcode(rval) as i32);
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn proc_raise(
|
||||
_wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
_sig: wasm32::__wasi_signal_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
unimplemented!("proc_raise")
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn random_get(
|
||||
memory: &mut [u8],
|
||||
buf_ptr: wasm32::uintptr_t,
|
||||
buf_len: wasm32::size_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
use rand::{thread_rng, RngCore};
|
||||
|
||||
let buf = match dec_slice_of_mut::<u8>(memory, buf_ptr, buf_len) {
|
||||
Ok(buf) => buf,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
thread_rng().fill_bytes(buf);
|
||||
|
||||
return wasm32::__WASI_ESUCCESS;
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn clock_res_get(
|
||||
memory: &mut [u8],
|
||||
clock_id: wasm32::__wasi_clockid_t,
|
||||
resolution_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let clock_id = dec_clockid(clock_id);
|
||||
let resolution = match hostcalls_impl::clock_res_get(clock_id) {
|
||||
Ok(resolution) => resolution,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_timestamp_byref(memory, resolution_ptr, resolution)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn clock_time_get(
|
||||
memory: &mut [u8],
|
||||
clock_id: wasm32::__wasi_clockid_t,
|
||||
// ignored for now, but will be useful once we put optional limits on precision to reduce side
|
||||
// channels
|
||||
_precision: wasm32::__wasi_timestamp_t,
|
||||
time_ptr: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
let clock_id = dec_clockid(clock_id);
|
||||
let time = match hostcalls_impl::clock_time_get(clock_id) {
|
||||
Ok(time) => time,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
enc_timestamp_byref(memory, time_ptr, time)
|
||||
.map(|_| wasm32::__WASI_ESUCCESS)
|
||||
.unwrap_or_else(enc_errno)
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn poll_oneoff(
|
||||
memory: &mut [u8],
|
||||
input: wasm32::uintptr_t,
|
||||
output: wasm32::uintptr_t,
|
||||
nsubscriptions: wasm32::size_t,
|
||||
nevents: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
if nsubscriptions as u64 > wasm32::__wasi_filesize_t::max_value() {
|
||||
return wasm32::__WASI_EINVAL;
|
||||
}
|
||||
if let Err(e) = enc_pointee(memory, nevents, 0) {
|
||||
return enc_errno(e);
|
||||
}
|
||||
let input_slice =
|
||||
match dec_slice_of::<wasm32::__wasi_subscription_t>(memory, input, nsubscriptions) {
|
||||
Ok(input_slice) => input_slice,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let input: Vec<_> = input_slice.iter().map(dec_subscription).collect();
|
||||
let output_slice =
|
||||
match dec_slice_of_mut::<wasm32::__wasi_event_t>(memory, output, nsubscriptions) {
|
||||
Ok(output_slice) => output_slice,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
let events_count = match hostcalls_impl::poll_oneoff(input, output_slice) {
|
||||
Ok(events_count) => events_count,
|
||||
Err(e) => return enc_errno(e),
|
||||
};
|
||||
|
||||
match enc_pointee(memory, nevents, events_count) {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn sched_yield() -> wasm32::__wasi_errno_t {
|
||||
match hostcalls_impl::sched_yield() {
|
||||
Ok(()) => wasm32::__WASI_ESUCCESS,
|
||||
Err(e) => enc_errno(e),
|
||||
}
|
||||
}
|
||||
7
src/hostcalls/mod.rs
Normal file
7
src/hostcalls/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
mod fs;
|
||||
mod misc;
|
||||
mod sock;
|
||||
|
||||
pub use self::fs::*;
|
||||
pub use self::misc::*;
|
||||
pub use self::sock::*;
|
||||
44
src/hostcalls/sock.rs
Normal file
44
src/hostcalls/sock.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
#![allow(unused_unsafe)]
|
||||
#![allow(unused)]
|
||||
|
||||
use crate::ctx::WasiCtx;
|
||||
use crate::wasm32;
|
||||
use wasi_common_cbindgen::wasi_common_cbindgen;
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn sock_recv(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
sock: wasm32::__wasi_fd_t,
|
||||
ri_data: wasm32::uintptr_t,
|
||||
ri_data_len: wasm32::size_t,
|
||||
ri_flags: wasm32::__wasi_riflags_t,
|
||||
ro_datalen: wasm32::uintptr_t,
|
||||
ro_flags: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
unimplemented!("sock_recv")
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn sock_send(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
sock: wasm32::__wasi_fd_t,
|
||||
si_data: wasm32::uintptr_t,
|
||||
si_data_len: wasm32::size_t,
|
||||
si_flags: wasm32::__wasi_siflags_t,
|
||||
so_datalen: wasm32::uintptr_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
unimplemented!("sock_send")
|
||||
}
|
||||
|
||||
#[wasi_common_cbindgen]
|
||||
pub fn sock_shutdown(
|
||||
wasi_ctx: &WasiCtx,
|
||||
memory: &mut [u8],
|
||||
sock: wasm32::__wasi_fd_t,
|
||||
how: wasm32::__wasi_sdflags_t,
|
||||
) -> wasm32::__wasi_errno_t {
|
||||
unimplemented!("sock_shutdown")
|
||||
}
|
||||
Reference in New Issue
Block a user