[wasi-common]: clean up error handling (#1253)
* Introduce WasiCtxBuilderError error type `WasiCtxBuilderError` is the `wasi-common` client-facing error type which is exclusively thrown when building a new `WasiCtx` instance. As such, building such an instance should not require the client to understand different WASI errno values as was assumed until now. This commit is a first step at streamlining error handling in `wasi-common` and makes way for the `wiggle` crate. When adding the `WasiCtxBuilderError`, I've had to do two things of notable importance: 1. I've removed a couple of `ok_or` calls in `WasiCtxBuilder::build` and replaced them with `unwrap`s, following the same pattern in different builder methods above. This is fine since we _always_ operate on non-empty `Option`s in `WasiCtxBuilder` thus `unwrap`ing will never fail. On the other hand, this might be a good opportunity to rethink the structure of our builder, and how we good remove the said `Option`s especially since we always populate them with empty containers to begin with. I understand this is to make chaining of builder methods easier which take and return `&mut self` and the same applies to `WasiCtxBuilder::build(&mut self)` method, but perhaps it would more cleanly signal the intentions if we simply moved `WasiCtxBuilder` instance around. Food for thought! 2. Methods specific to determining rights of passed around `std::fs::File` objects when populating `WasiCtx` `FdEntry` entities now return `io::Error` directly so that we can reuse them in `WasiCtxBuilder` methods (returning `WasiCtxBuilderError` error type), and in syscalls (returning WASI errno). * Return WasiError directly in syscalls Also, removes `error::Error` type altogether. Now, `io::Error` and related are automatically converted to their corresponding WASI errno value encapsulated as `WasiError`. While here, it made sense to me to move `WasiError` to `wasi` module which will align itself well with the upcoming changes introduced by `wiggle`. To different standard `Result` from WASI specific, I've created a helper alias `WasiResult` also residing in `wasi` module. * Update wig * Add from ffi::NulError and pass context to NotADirectory * Add dummy commit to test CI
This commit is contained in:
@@ -7,7 +7,8 @@ use crate::old::snapshot_0::memory::*;
|
||||
use crate::old::snapshot_0::sys::fdentry_impl::determine_type_rights;
|
||||
use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::path_open_rights;
|
||||
use crate::old::snapshot_0::sys::{host_impl, hostcalls_impl};
|
||||
use crate::old::snapshot_0::{helpers, host, wasi, wasi32, Error, Result};
|
||||
use crate::old::snapshot_0::wasi::{self, WasiError, WasiResult};
|
||||
use crate::old::snapshot_0::{helpers, host, wasi32};
|
||||
use crate::sandboxed_tty_writer::SandboxedTTYWriter;
|
||||
use filetime::{set_file_handle_times, FileTime};
|
||||
use log::trace;
|
||||
@@ -20,13 +21,13 @@ pub(crate) unsafe fn fd_close(
|
||||
wasi_ctx: &mut WasiCtx,
|
||||
_mem: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_close(fd={:?})", fd);
|
||||
|
||||
if let Ok(fe) = wasi_ctx.get_fd_entry(fd) {
|
||||
// can't close preopened files
|
||||
if fe.preopen_path.is_some() {
|
||||
return Err(Error::ENOTSUP);
|
||||
return Err(WasiError::ENOTSUP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,7 +39,7 @@ pub(crate) unsafe fn fd_datasync(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_mem: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_datasync(fd={:?})", fd);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -57,7 +58,7 @@ pub(crate) unsafe fn fd_pread(
|
||||
iovs_len: wasi32::size_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
nread: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_pread(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, offset={}, nread={:#x?})",
|
||||
fd,
|
||||
@@ -75,7 +76,7 @@ pub(crate) unsafe fn fd_pread(
|
||||
let iovs = dec_iovec_slice(memory, iovs_ptr, iovs_len)?;
|
||||
|
||||
if offset > i64::max_value() as u64 {
|
||||
return Err(Error::EIO);
|
||||
return Err(WasiError::EIO);
|
||||
}
|
||||
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
|
||||
let mut buf = vec![0; buf_size];
|
||||
@@ -106,7 +107,7 @@ pub(crate) unsafe fn fd_pwrite(
|
||||
iovs_len: wasi32::size_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
nwritten: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_pwrite(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, offset={}, nwritten={:#x?})",
|
||||
fd,
|
||||
@@ -126,7 +127,7 @@ pub(crate) unsafe fn fd_pwrite(
|
||||
let iovs = dec_ciovec_slice(memory, iovs_ptr, iovs_len)?;
|
||||
|
||||
if offset > i64::max_value() as u64 {
|
||||
return Err(Error::EIO);
|
||||
return Err(WasiError::EIO);
|
||||
}
|
||||
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
|
||||
let mut buf = Vec::with_capacity(buf_size);
|
||||
@@ -150,7 +151,7 @@ pub(crate) unsafe fn fd_read(
|
||||
iovs_ptr: wasi32::uintptr_t,
|
||||
iovs_len: wasi32::size_t,
|
||||
nread: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_read(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, nread={:#x?})",
|
||||
fd,
|
||||
@@ -171,7 +172,7 @@ pub(crate) unsafe fn fd_read(
|
||||
{
|
||||
Descriptor::OsHandle(file) => file.read_vectored(&mut iovs),
|
||||
Descriptor::Stdin => io::stdin().read_vectored(&mut iovs),
|
||||
_ => return Err(Error::EBADF),
|
||||
_ => return Err(WasiError::EBADF),
|
||||
};
|
||||
|
||||
let host_nread = maybe_host_nread?;
|
||||
@@ -186,11 +187,11 @@ pub(crate) unsafe fn fd_renumber(
|
||||
_mem: &mut [u8],
|
||||
from: wasi::__wasi_fd_t,
|
||||
to: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_renumber(from={:?}, to={:?})", from, to);
|
||||
|
||||
if !wasi_ctx.contains_fd_entry(from) {
|
||||
return Err(Error::EBADF);
|
||||
return Err(WasiError::EBADF);
|
||||
}
|
||||
|
||||
// Don't allow renumbering over a pre-opened resource.
|
||||
@@ -198,11 +199,11 @@ pub(crate) unsafe fn fd_renumber(
|
||||
// userspace is capable of removing entries from its tables as well.
|
||||
let from_fe = wasi_ctx.get_fd_entry(from)?;
|
||||
if from_fe.preopen_path.is_some() {
|
||||
return Err(Error::ENOTSUP);
|
||||
return Err(WasiError::ENOTSUP);
|
||||
}
|
||||
if let Ok(to_fe) = wasi_ctx.get_fd_entry(to) {
|
||||
if to_fe.preopen_path.is_some() {
|
||||
return Err(Error::ENOTSUP);
|
||||
return Err(WasiError::ENOTSUP);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -219,7 +220,7 @@ pub(crate) unsafe fn fd_seek(
|
||||
offset: wasi::__wasi_filedelta_t,
|
||||
whence: wasi::__wasi_whence_t,
|
||||
newoffset: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_seek(fd={:?}, offset={:?}, whence={}, newoffset={:#x?})",
|
||||
fd,
|
||||
@@ -242,7 +243,7 @@ pub(crate) unsafe fn fd_seek(
|
||||
wasi::__WASI_WHENCE_CUR => SeekFrom::Current(offset),
|
||||
wasi::__WASI_WHENCE_END => SeekFrom::End(offset),
|
||||
wasi::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
|
||||
_ => return Err(Error::EINVAL),
|
||||
_ => return Err(WasiError::EINVAL),
|
||||
};
|
||||
let host_newoffset = fd.seek(pos)?;
|
||||
|
||||
@@ -256,7 +257,7 @@ pub(crate) unsafe fn fd_tell(
|
||||
memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
newoffset: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_tell(fd={:?}, newoffset={:#x?})", fd, newoffset);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -276,7 +277,7 @@ pub(crate) unsafe fn fd_fdstat_get(
|
||||
memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fdstat_ptr: wasi32::uintptr_t, // *mut wasi::__wasi_fdstat_t
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_fdstat_get(fd={:?}, fdstat_ptr={:#x?})", fd, fdstat_ptr);
|
||||
|
||||
let mut fdstat = dec_fdstat_byref(memory, fdstat_ptr)?;
|
||||
@@ -303,7 +304,7 @@ pub(crate) unsafe fn fd_fdstat_set_flags(
|
||||
_mem: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fdflags: wasi::__wasi_fdflags_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_fdstat_set_flags(fd={:?}, fdflags={:#x?})", fd, fdflags);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -320,7 +321,7 @@ pub(crate) unsafe fn fd_fdstat_set_rights(
|
||||
fd: wasi::__wasi_fd_t,
|
||||
fs_rights_base: wasi::__wasi_rights_t,
|
||||
fs_rights_inheriting: wasi::__wasi_rights_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_fdstat_set_rights(fd={:?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?})",
|
||||
fd,
|
||||
@@ -332,7 +333,7 @@ pub(crate) unsafe fn fd_fdstat_set_rights(
|
||||
if fe.rights_base & fs_rights_base != fs_rights_base
|
||||
|| fe.rights_inheriting & fs_rights_inheriting != fs_rights_inheriting
|
||||
{
|
||||
return Err(Error::ENOTCAPABLE);
|
||||
return Err(WasiError::ENOTCAPABLE);
|
||||
}
|
||||
fe.rights_base = fs_rights_base;
|
||||
fe.rights_inheriting = fs_rights_inheriting;
|
||||
@@ -344,7 +345,7 @@ pub(crate) unsafe fn fd_sync(
|
||||
wasi_ctx: &WasiCtx,
|
||||
_mem: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_sync(fd={:?})", fd);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -361,7 +362,7 @@ pub(crate) unsafe fn fd_write(
|
||||
iovs_ptr: wasi32::uintptr_t,
|
||||
iovs_len: wasi32::size_t,
|
||||
nwritten: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_write(fd={:?}, iovs_ptr={:#x?}, iovs_len={:?}, nwritten={:#x?})",
|
||||
fd,
|
||||
@@ -385,7 +386,7 @@ pub(crate) unsafe fn fd_write(
|
||||
file.write_vectored(&iovs)?
|
||||
}
|
||||
}
|
||||
Descriptor::Stdin => return Err(Error::EBADF),
|
||||
Descriptor::Stdin => return Err(WasiError::EBADF),
|
||||
Descriptor::Stdout => {
|
||||
// lock for the duration of the scope
|
||||
let stdout = io::stdout();
|
||||
@@ -417,7 +418,7 @@ pub(crate) unsafe fn fd_advise(
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
len: wasi::__wasi_filesize_t,
|
||||
advice: wasi::__wasi_advice_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_advise(fd={:?}, offset={}, len={}, advice={:?})",
|
||||
fd,
|
||||
@@ -440,7 +441,7 @@ pub(crate) unsafe fn fd_allocate(
|
||||
fd: wasi::__wasi_fd_t,
|
||||
offset: wasi::__wasi_filesize_t,
|
||||
len: wasi::__wasi_filesize_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_allocate(fd={:?}, offset={}, len={})", fd, offset, len);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -451,10 +452,10 @@ pub(crate) unsafe fn fd_allocate(
|
||||
let metadata = fd.metadata()?;
|
||||
|
||||
let current_size = metadata.len();
|
||||
let wanted_size = offset.checked_add(len).ok_or(Error::E2BIG)?;
|
||||
let wanted_size = offset.checked_add(len).ok_or(WasiError::E2BIG)?;
|
||||
// This check will be unnecessary when rust-lang/rust#63326 is fixed
|
||||
if wanted_size > i64::max_value() as u64 {
|
||||
return Err(Error::E2BIG);
|
||||
return Err(WasiError::E2BIG);
|
||||
}
|
||||
|
||||
if wanted_size > current_size {
|
||||
@@ -470,7 +471,7 @@ pub(crate) unsafe fn path_create_directory(
|
||||
dirfd: wasi::__wasi_fd_t,
|
||||
path_ptr: wasi32::uintptr_t,
|
||||
path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_create_directory(dirfd={:?}, path_ptr={:#x?}, path_len={})",
|
||||
dirfd,
|
||||
@@ -499,7 +500,7 @@ pub(crate) unsafe fn path_link(
|
||||
new_dirfd: wasi::__wasi_fd_t,
|
||||
new_path_ptr: wasi32::uintptr_t,
|
||||
new_path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_link(old_dirfd={:?}, old_flags={:?}, old_path_ptr={:#x?}, old_path_len={}, new_dirfd={:?}, new_path_ptr={:#x?}, new_path_len={})",
|
||||
old_dirfd,
|
||||
@@ -551,7 +552,7 @@ pub(crate) unsafe fn path_open(
|
||||
fs_rights_inheriting: wasi::__wasi_rights_t,
|
||||
fs_flags: wasi::__wasi_fdflags_t,
|
||||
fd_out_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_open(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={:?}, oflags={:#x?}, fs_rights_base={:#x?}, fs_rights_inheriting={:#x?}, fs_flags={:#x?}, fd_out_ptr={:#x?})",
|
||||
dirfd,
|
||||
@@ -616,7 +617,7 @@ pub(crate) unsafe fn path_readlink(
|
||||
buf_ptr: wasi32::uintptr_t,
|
||||
buf_len: wasi32::size_t,
|
||||
buf_used: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_readlink(dirfd={:?}, path_ptr={:#x?}, path_len={:?}, buf_ptr={:#x?}, buf_len={}, buf_used={:#x?})",
|
||||
dirfd,
|
||||
@@ -655,7 +656,7 @@ pub(crate) unsafe fn path_rename(
|
||||
new_dirfd: wasi::__wasi_fd_t,
|
||||
new_path_ptr: wasi32::uintptr_t,
|
||||
new_path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_rename(old_dirfd={:?}, old_path_ptr={:#x?}, old_path_len={:?}, new_dirfd={:?}, new_path_ptr={:#x?}, new_path_len={:?})",
|
||||
old_dirfd,
|
||||
@@ -702,7 +703,7 @@ pub(crate) unsafe fn fd_filestat_get(
|
||||
memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
filestat_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_filestat_get(fd={:?}, filestat_ptr={:#x?})",
|
||||
fd,
|
||||
@@ -724,7 +725,7 @@ pub(crate) unsafe fn fd_filestat_set_times(
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
st_mtim: wasi::__wasi_timestamp_t,
|
||||
fst_flags: wasi::__wasi_fstflags_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_filestat_set_times(fd={:?}, st_atim={}, st_mtim={}, fst_flags={:#x?})",
|
||||
fd,
|
||||
@@ -746,14 +747,14 @@ pub(crate) fn fd_filestat_set_times_impl(
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
st_mtim: wasi::__wasi_timestamp_t,
|
||||
fst_flags: wasi::__wasi_fstflags_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
let set_atim = fst_flags & wasi::__WASI_FSTFLAGS_ATIM != 0;
|
||||
let set_atim_now = fst_flags & wasi::__WASI_FSTFLAGS_ATIM_NOW != 0;
|
||||
let set_mtim = fst_flags & wasi::__WASI_FSTFLAGS_MTIM != 0;
|
||||
let set_mtim_now = fst_flags & wasi::__WASI_FSTFLAGS_MTIM_NOW != 0;
|
||||
|
||||
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
|
||||
return Err(Error::EINVAL);
|
||||
return Err(WasiError::EINVAL);
|
||||
}
|
||||
let atim = if set_atim {
|
||||
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
|
||||
@@ -782,7 +783,7 @@ pub(crate) unsafe fn fd_filestat_set_size(
|
||||
_mem: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
st_size: wasi::__wasi_filesize_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("fd_filestat_set_size(fd={:?}, st_size={})", fd, st_size);
|
||||
|
||||
let fd = wasi_ctx
|
||||
@@ -792,7 +793,7 @@ pub(crate) unsafe fn fd_filestat_set_size(
|
||||
|
||||
// This check will be unnecessary when rust-lang/rust#63326 is fixed
|
||||
if st_size > i64::max_value() as u64 {
|
||||
return Err(Error::E2BIG);
|
||||
return Err(WasiError::E2BIG);
|
||||
}
|
||||
fd.set_len(st_size).map_err(Into::into)
|
||||
}
|
||||
@@ -805,7 +806,7 @@ pub(crate) unsafe fn path_filestat_get(
|
||||
path_ptr: wasi32::uintptr_t,
|
||||
path_len: wasi32::size_t,
|
||||
filestat_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_filestat_get(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={}, filestat_ptr={:#x?})",
|
||||
dirfd,
|
||||
@@ -845,7 +846,7 @@ pub(crate) unsafe fn path_filestat_set_times(
|
||||
st_atim: wasi::__wasi_timestamp_t,
|
||||
st_mtim: wasi::__wasi_timestamp_t,
|
||||
fst_flags: wasi::__wasi_fstflags_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_filestat_set_times(dirfd={:?}, dirflags={:?}, path_ptr={:#x?}, path_len={}, st_atim={}, st_mtim={}, fst_flags={:#x?})",
|
||||
dirfd,
|
||||
@@ -881,7 +882,7 @@ pub(crate) unsafe fn path_symlink(
|
||||
dirfd: wasi::__wasi_fd_t,
|
||||
new_path_ptr: wasi32::uintptr_t,
|
||||
new_path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_symlink(old_path_ptr={:#x?}, old_path_len={}, dirfd={:?}, new_path_ptr={:#x?}, new_path_len={})",
|
||||
old_path_ptr,
|
||||
@@ -909,7 +910,7 @@ pub(crate) unsafe fn path_unlink_file(
|
||||
dirfd: wasi::__wasi_fd_t,
|
||||
path_ptr: wasi32::uintptr_t,
|
||||
path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_unlink_file(dirfd={:?}, path_ptr={:#x?}, path_len={})",
|
||||
dirfd,
|
||||
@@ -933,7 +934,7 @@ pub(crate) unsafe fn path_remove_directory(
|
||||
dirfd: wasi::__wasi_fd_t,
|
||||
path_ptr: wasi32::uintptr_t,
|
||||
path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"path_remove_directory(dirfd={:?}, path_ptr={:#x?}, path_len={})",
|
||||
dirfd,
|
||||
@@ -965,7 +966,7 @@ pub(crate) unsafe fn fd_prestat_get(
|
||||
memory: &mut [u8],
|
||||
fd: wasi::__wasi_fd_t,
|
||||
prestat_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_prestat_get(fd={:?}, prestat_ptr={:#x?})",
|
||||
fd,
|
||||
@@ -974,9 +975,9 @@ pub(crate) unsafe fn fd_prestat_get(
|
||||
|
||||
// TODO: should we validate any rights here?
|
||||
let fe = wasi_ctx.get_fd_entry(fd)?;
|
||||
let po_path = fe.preopen_path.as_ref().ok_or(Error::ENOTSUP)?;
|
||||
let po_path = fe.preopen_path.as_ref().ok_or(WasiError::ENOTSUP)?;
|
||||
if fe.file_type != wasi::__WASI_FILETYPE_DIRECTORY {
|
||||
return Err(Error::ENOTDIR);
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
|
||||
let path = host_impl::path_from_host(po_path.as_os_str())?;
|
||||
@@ -1001,7 +1002,7 @@ pub(crate) unsafe fn fd_prestat_dir_name(
|
||||
fd: wasi::__wasi_fd_t,
|
||||
path_ptr: wasi32::uintptr_t,
|
||||
path_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_prestat_dir_name(fd={:?}, path_ptr={:#x?}, path_len={})",
|
||||
fd,
|
||||
@@ -1011,15 +1012,15 @@ pub(crate) unsafe fn fd_prestat_dir_name(
|
||||
|
||||
// TODO: should we validate any rights here?
|
||||
let fe = wasi_ctx.get_fd_entry(fd)?;
|
||||
let po_path = fe.preopen_path.as_ref().ok_or(Error::ENOTSUP)?;
|
||||
let po_path = fe.preopen_path.as_ref().ok_or(WasiError::ENOTSUP)?;
|
||||
if fe.file_type != wasi::__WASI_FILETYPE_DIRECTORY {
|
||||
return Err(Error::ENOTDIR);
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
|
||||
let path = host_impl::path_from_host(po_path.as_os_str())?;
|
||||
|
||||
if path.len() > dec_usize(path_len) {
|
||||
return Err(Error::ENAMETOOLONG);
|
||||
return Err(WasiError::ENAMETOOLONG);
|
||||
}
|
||||
|
||||
trace!(" | (path_ptr,path_len)='{}'", path);
|
||||
@@ -1035,7 +1036,7 @@ pub(crate) unsafe fn fd_readdir(
|
||||
buf_len: wasi32::size_t,
|
||||
cookie: wasi::__wasi_dircookie_t,
|
||||
buf_used: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"fd_readdir(fd={:?}, buf={:#x?}, buf_len={}, cookie={:#x?}, buf_used={:#x?})",
|
||||
fd,
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
#![allow(non_camel_case_types)]
|
||||
use crate::old::snapshot_0::fdentry::FdEntry;
|
||||
use crate::old::snapshot_0::sys::host_impl;
|
||||
use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::*;
|
||||
use crate::old::snapshot_0::{error::WasiError, fdentry::FdEntry, wasi, Error, Result};
|
||||
use crate::old::snapshot_0::wasi::{self, WasiError, WasiResult};
|
||||
use std::fs::File;
|
||||
use std::path::{Component, Path};
|
||||
|
||||
@@ -31,17 +32,17 @@ pub(crate) fn path_get(
|
||||
dirflags: wasi::__wasi_lookupflags_t,
|
||||
path: &str,
|
||||
needs_final_component: bool,
|
||||
) -> Result<PathGet> {
|
||||
) -> WasiResult<PathGet> {
|
||||
const MAX_SYMLINK_EXPANSIONS: usize = 128;
|
||||
|
||||
if path.contains('\0') {
|
||||
// if contains NUL, return EILSEQ
|
||||
return Err(Error::EILSEQ);
|
||||
return Err(WasiError::EILSEQ);
|
||||
}
|
||||
|
||||
if fe.file_type != wasi::__WASI_FILETYPE_DIRECTORY {
|
||||
// if `dirfd` doesn't refer to a directory, return `ENOTDIR`.
|
||||
return Err(Error::ENOTDIR);
|
||||
return Err(WasiError::ENOTDIR);
|
||||
}
|
||||
|
||||
let dirfd = fe
|
||||
@@ -72,7 +73,7 @@ pub(crate) fn path_get(
|
||||
let ends_with_slash = cur_path.ends_with('/');
|
||||
let mut components = Path::new(&cur_path).components();
|
||||
let head = match components.next() {
|
||||
None => return Err(Error::ENOENT),
|
||||
None => return Err(WasiError::ENOENT),
|
||||
Some(p) => p,
|
||||
};
|
||||
let tail = components.as_path();
|
||||
@@ -90,18 +91,18 @@ pub(crate) fn path_get(
|
||||
match head {
|
||||
Component::Prefix(_) | Component::RootDir => {
|
||||
// path is absolute!
|
||||
return Err(Error::ENOTCAPABLE);
|
||||
return Err(WasiError::ENOTCAPABLE);
|
||||
}
|
||||
Component::CurDir => {
|
||||
// "." so skip
|
||||
}
|
||||
Component::ParentDir => {
|
||||
// ".." so pop a dir
|
||||
let _ = dir_stack.pop().ok_or(Error::ENOTCAPABLE)?;
|
||||
let _ = dir_stack.pop().ok_or(WasiError::ENOTCAPABLE)?;
|
||||
|
||||
// we're not allowed to pop past the original directory
|
||||
if dir_stack.is_empty() {
|
||||
return Err(Error::ENOTCAPABLE);
|
||||
return Err(WasiError::ENOTCAPABLE);
|
||||
}
|
||||
}
|
||||
Component::Normal(head) => {
|
||||
@@ -112,12 +113,12 @@ pub(crate) fn path_get(
|
||||
}
|
||||
|
||||
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
|
||||
match openat(dir_stack.last().ok_or(Error::ENOTCAPABLE)?, &head) {
|
||||
match openat(dir_stack.last().ok_or(WasiError::ENOTCAPABLE)?, &head) {
|
||||
Ok(new_dir) => {
|
||||
dir_stack.push(new_dir);
|
||||
}
|
||||
Err(e) => {
|
||||
match e.as_wasi_error() {
|
||||
match e {
|
||||
WasiError::ELOOP
|
||||
| WasiError::EMLINK
|
||||
| WasiError::ENOTDIR =>
|
||||
@@ -126,13 +127,13 @@ pub(crate) fn path_get(
|
||||
{
|
||||
// attempt symlink expansion
|
||||
let mut link_path = readlinkat(
|
||||
dir_stack.last().ok_or(Error::ENOTCAPABLE)?,
|
||||
dir_stack.last().ok_or(WasiError::ENOTCAPABLE)?,
|
||||
&head,
|
||||
)?;
|
||||
|
||||
symlink_expansions += 1;
|
||||
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
|
||||
return Err(Error::ELOOP);
|
||||
return Err(WasiError::ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with('/') {
|
||||
@@ -159,11 +160,12 @@ pub(crate) fn path_get(
|
||||
{
|
||||
// if there's a trailing slash, or if `LOOKUP_SYMLINK_FOLLOW` is set, attempt
|
||||
// symlink expansion
|
||||
match readlinkat(dir_stack.last().ok_or(Error::ENOTCAPABLE)?, &head) {
|
||||
match readlinkat(dir_stack.last().ok_or(WasiError::ENOTCAPABLE)?, &head)
|
||||
{
|
||||
Ok(mut link_path) => {
|
||||
symlink_expansions += 1;
|
||||
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
|
||||
return Err(Error::ELOOP);
|
||||
return Err(WasiError::ELOOP);
|
||||
}
|
||||
|
||||
if head.ends_with('/') {
|
||||
@@ -179,12 +181,12 @@ pub(crate) fn path_get(
|
||||
continue;
|
||||
}
|
||||
Err(e) => {
|
||||
if e.as_wasi_error() != WasiError::EINVAL
|
||||
&& e.as_wasi_error() != WasiError::ENOENT
|
||||
if e != WasiError::EINVAL
|
||||
&& e != WasiError::ENOENT
|
||||
// this handles the cases when trying to link to
|
||||
// a destination that already exists, and the target
|
||||
// path contains a slash
|
||||
&& e.as_wasi_error() != WasiError::ENOTDIR
|
||||
&& e != WasiError::ENOTDIR
|
||||
{
|
||||
return Err(e);
|
||||
}
|
||||
@@ -194,7 +196,7 @@ pub(crate) fn path_get(
|
||||
|
||||
// not a symlink, so we're done;
|
||||
return Ok(PathGet {
|
||||
dirfd: dir_stack.pop().ok_or(Error::ENOTCAPABLE)?,
|
||||
dirfd: dir_stack.pop().ok_or(WasiError::ENOTCAPABLE)?,
|
||||
path: head,
|
||||
});
|
||||
}
|
||||
@@ -204,7 +206,7 @@ pub(crate) fn path_get(
|
||||
// no further components to process. means we've hit a case like "." or "a/..", or if the
|
||||
// input path has trailing slashes and `needs_final_component` is not set
|
||||
return Ok(PathGet {
|
||||
dirfd: dir_stack.pop().ok_or(Error::ENOTCAPABLE)?,
|
||||
dirfd: dir_stack.pop().ok_or(WasiError::ENOTCAPABLE)?,
|
||||
path: String::from("."),
|
||||
});
|
||||
}
|
||||
|
||||
@@ -3,7 +3,8 @@ use crate::old::snapshot_0::ctx::WasiCtx;
|
||||
use crate::old::snapshot_0::fdentry::Descriptor;
|
||||
use crate::old::snapshot_0::memory::*;
|
||||
use crate::old::snapshot_0::sys::hostcalls_impl;
|
||||
use crate::old::snapshot_0::{wasi, wasi32, Error, Result};
|
||||
use crate::old::snapshot_0::wasi::{self, WasiError, WasiResult};
|
||||
use crate::old::snapshot_0::wasi32;
|
||||
use log::{error, trace};
|
||||
use std::convert::TryFrom;
|
||||
|
||||
@@ -12,7 +13,7 @@ pub(crate) fn args_get(
|
||||
memory: &mut [u8],
|
||||
argv_ptr: wasi32::uintptr_t,
|
||||
argv_buf: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"args_get(argv_ptr={:#x?}, argv_buf={:#x?})",
|
||||
argv_ptr,
|
||||
@@ -31,7 +32,9 @@ pub(crate) fn args_get(
|
||||
argv.push(arg_ptr);
|
||||
|
||||
let len = wasi32::uintptr_t::try_from(arg_bytes.len())?;
|
||||
argv_buf_offset = argv_buf_offset.checked_add(len).ok_or(Error::EOVERFLOW)?;
|
||||
argv_buf_offset = argv_buf_offset
|
||||
.checked_add(len)
|
||||
.ok_or(WasiError::EOVERFLOW)?;
|
||||
}
|
||||
|
||||
enc_slice_of_wasi32_uintptr(memory, argv.as_slice(), argv_ptr)
|
||||
@@ -42,7 +45,7 @@ pub(crate) fn args_sizes_get(
|
||||
memory: &mut [u8],
|
||||
argc_ptr: wasi32::uintptr_t,
|
||||
argv_buf_size_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"args_sizes_get(argc_ptr={:#x?}, argv_buf_size_ptr={:#x?})",
|
||||
argc_ptr,
|
||||
@@ -70,7 +73,7 @@ pub(crate) fn environ_get(
|
||||
memory: &mut [u8],
|
||||
environ_ptr: wasi32::uintptr_t,
|
||||
environ_buf: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"environ_get(environ_ptr={:#x?}, environ_buf={:#x?})",
|
||||
environ_ptr,
|
||||
@@ -91,7 +94,7 @@ pub(crate) fn environ_get(
|
||||
let len = wasi32::uintptr_t::try_from(env_bytes.len())?;
|
||||
environ_buf_offset = environ_buf_offset
|
||||
.checked_add(len)
|
||||
.ok_or(Error::EOVERFLOW)?;
|
||||
.ok_or(WasiError::EOVERFLOW)?;
|
||||
}
|
||||
|
||||
enc_slice_of_wasi32_uintptr(memory, environ.as_slice(), environ_ptr)
|
||||
@@ -102,7 +105,7 @@ pub(crate) fn environ_sizes_get(
|
||||
memory: &mut [u8],
|
||||
environ_count_ptr: wasi32::uintptr_t,
|
||||
environ_size_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"environ_sizes_get(environ_count_ptr={:#x?}, environ_size_ptr={:#x?})",
|
||||
environ_count_ptr,
|
||||
@@ -116,7 +119,7 @@ pub(crate) fn environ_sizes_get(
|
||||
.try_fold(0, |acc: u32, pair| {
|
||||
acc.checked_add(pair.as_bytes_with_nul().len() as u32)
|
||||
})
|
||||
.ok_or(Error::EOVERFLOW)?;
|
||||
.ok_or(WasiError::EOVERFLOW)?;
|
||||
|
||||
trace!(" | *environ_count_ptr={:?}", environ_count);
|
||||
|
||||
@@ -132,14 +135,14 @@ pub(crate) fn random_get(
|
||||
memory: &mut [u8],
|
||||
buf_ptr: wasi32::uintptr_t,
|
||||
buf_len: wasi32::size_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!("random_get(buf_ptr={:#x?}, buf_len={:?})", buf_ptr, buf_len);
|
||||
|
||||
let buf = dec_slice_of_mut_u8(memory, buf_ptr, buf_len)?;
|
||||
|
||||
getrandom::getrandom(buf).map_err(|err| {
|
||||
error!("getrandom failure: {:?}", err);
|
||||
Error::EIO
|
||||
WasiError::EIO
|
||||
})
|
||||
}
|
||||
|
||||
@@ -148,7 +151,7 @@ pub(crate) fn clock_res_get(
|
||||
memory: &mut [u8],
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
resolution_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"clock_res_get(clock_id={:?}, resolution_ptr={:#x?})",
|
||||
clock_id,
|
||||
@@ -168,7 +171,7 @@ pub(crate) fn clock_time_get(
|
||||
clock_id: wasi::__wasi_clockid_t,
|
||||
precision: wasi::__wasi_timestamp_t,
|
||||
time_ptr: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"clock_time_get(clock_id={:?}, precision={:?}, time_ptr={:#x?})",
|
||||
clock_id,
|
||||
@@ -183,7 +186,7 @@ pub(crate) fn clock_time_get(
|
||||
enc_timestamp_byref(memory, time_ptr, time)
|
||||
}
|
||||
|
||||
pub(crate) fn sched_yield(_wasi_ctx: &WasiCtx, _memory: &mut [u8]) -> Result<()> {
|
||||
pub(crate) fn sched_yield(_wasi_ctx: &WasiCtx, _memory: &mut [u8]) -> WasiResult<()> {
|
||||
trace!("sched_yield()");
|
||||
|
||||
std::thread::yield_now();
|
||||
@@ -198,7 +201,7 @@ pub(crate) fn poll_oneoff(
|
||||
output: wasi32::uintptr_t,
|
||||
nsubscriptions: wasi32::size_t,
|
||||
nevents: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
trace!(
|
||||
"poll_oneoff(input={:#x?}, output={:#x?}, nsubscriptions={}, nevents={:#x?})",
|
||||
input,
|
||||
@@ -208,7 +211,7 @@ pub(crate) fn poll_oneoff(
|
||||
);
|
||||
|
||||
if u64::from(nsubscriptions) > wasi::__wasi_filesize_t::max_value() {
|
||||
return Err(Error::EINVAL);
|
||||
return Err(WasiError::EINVAL);
|
||||
}
|
||||
|
||||
enc_int_byref(memory, nevents, 0)?;
|
||||
@@ -253,7 +256,7 @@ pub(crate) fn poll_oneoff(
|
||||
Err(err) => {
|
||||
let event = wasi::__wasi_event_t {
|
||||
userdata: subscription.userdata,
|
||||
error: err.as_wasi_error().as_raw_errno(),
|
||||
error: err.as_raw_errno(),
|
||||
r#type: wasi::__WASI_EVENTTYPE_FD_READ,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: 0,
|
||||
@@ -281,7 +284,7 @@ pub(crate) fn poll_oneoff(
|
||||
Err(err) => {
|
||||
let event = wasi::__wasi_event_t {
|
||||
userdata: subscription.userdata,
|
||||
error: err.as_wasi_error().as_raw_errno(),
|
||||
error: err.as_raw_errno(),
|
||||
r#type: wasi::__WASI_EVENTTYPE_FD_WRITE,
|
||||
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
|
||||
nbytes: 0,
|
||||
@@ -301,7 +304,7 @@ pub(crate) fn poll_oneoff(
|
||||
|
||||
hostcalls_impl::poll_oneoff(timeout, fd_events, &mut events)?;
|
||||
|
||||
let events_count = u32::try_from(events.len()).map_err(|_| Error::EOVERFLOW)?;
|
||||
let events_count = u32::try_from(events.len()).map_err(|_| WasiError::EOVERFLOW)?;
|
||||
|
||||
enc_events(memory, output, nsubscriptions, events)?;
|
||||
|
||||
@@ -310,7 +313,9 @@ pub(crate) fn poll_oneoff(
|
||||
enc_int_byref(memory, nevents, events_count)
|
||||
}
|
||||
|
||||
fn wasi_clock_to_relative_ns_delay(wasi_clock: wasi::__wasi_subscription_clock_t) -> Result<u128> {
|
||||
fn wasi_clock_to_relative_ns_delay(
|
||||
wasi_clock: wasi::__wasi_subscription_clock_t,
|
||||
) -> WasiResult<u128> {
|
||||
use std::time::SystemTime;
|
||||
|
||||
if wasi_clock.flags != wasi::__WASI_SUBCLOCKFLAGS_SUBSCRIPTION_CLOCK_ABSTIME {
|
||||
@@ -318,7 +323,7 @@ fn wasi_clock_to_relative_ns_delay(wasi_clock: wasi::__wasi_subscription_clock_t
|
||||
}
|
||||
let now: u128 = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.map_err(|_| Error::ENOTCAPABLE)?
|
||||
.map_err(|_| WasiError::ENOTCAPABLE)?
|
||||
.as_nanos();
|
||||
let deadline = u128::from(wasi_clock.timeout);
|
||||
Ok(deadline.saturating_sub(now))
|
||||
@@ -348,6 +353,6 @@ pub(crate) fn proc_raise(
|
||||
_wasi_ctx: &WasiCtx,
|
||||
_memory: &mut [u8],
|
||||
_sig: wasi::__wasi_signal_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
unimplemented!("proc_raise")
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use crate::old::snapshot_0::{wasi, wasi32, Result, WasiCtx};
|
||||
use crate::old::snapshot_0::wasi::{self, WasiResult};
|
||||
use crate::old::snapshot_0::{wasi32, WasiCtx};
|
||||
|
||||
pub fn sock_recv(
|
||||
_wasi_ctx: &WasiCtx,
|
||||
@@ -9,7 +10,7 @@ pub fn sock_recv(
|
||||
_ri_flags: wasi::__wasi_riflags_t,
|
||||
_ro_datalen: wasi32::uintptr_t,
|
||||
_ro_flags: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
unimplemented!("sock_recv")
|
||||
}
|
||||
|
||||
@@ -21,7 +22,7 @@ pub fn sock_send(
|
||||
_si_data_len: wasi32::size_t,
|
||||
_si_flags: wasi::__wasi_siflags_t,
|
||||
_so_datalen: wasi32::uintptr_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
unimplemented!("sock_send")
|
||||
}
|
||||
|
||||
@@ -30,6 +31,6 @@ pub fn sock_shutdown(
|
||||
_memory: &mut [u8],
|
||||
_sock: wasi::__wasi_fd_t,
|
||||
_how: wasi::__wasi_sdflags_t,
|
||||
) -> Result<()> {
|
||||
) -> WasiResult<()> {
|
||||
unimplemented!("sock_shutdown")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user