Files
wasmtime/crates/wasi-common/src/wasi.rs
Tanya L. Crenshaw b06ed39c1e Fixes #2418: Enhance wiggle to generate its UserErrorConverstion trait with a function that returns Result<abi_err, String> (#2419)
* Enhance wiggle to generate its UserErrorConverstion trait with a function that returns
a Result<abi_err, String>.  This enhancement allows hostcall implementations using wiggle
to return an actionable error to the instance (the abi_err) or to terminate the instance
using the String as fatal error information.

* Enhance wiggle to generate its UserErrorConverstion trait with a function that returns
a Result<abi_err, String>.  This enhancement allows hostcall implementations using wiggle
to return an actionable error to the instance (the abi_err) or to terminate the instance
using the String as fatal error information.

* Enhance the wiggle/wasmtime integration to leverage new work in ab7e9c6.  Hostcall
implementations generated by wiggle now return an Result<abi_error, Trap>.  As a
result, hostcalls experiencing fatal errors may trap, thereby terminating the
wasmtime instance.  This enhancement has been performed for both wasi snapshot1
and wasi snapshot0.

* Update wasi-nn crate to reflect enhancement in issue #2418.

* Update wiggle test-helpers for wiggle enhancement made in issue #2418.

* Address PR feedback; omit verbose return statement.

* Address PR feedback; manually format within a proc macro.

* Address PR feedback; manually format proc macro.

* Restore return statements to wasi.rs.

* Restore return statements in funcs.rs.

* Address PR feedback; omit TODO and fix formatting.

* Ok-wrap error type in assert statement.
2020-11-24 14:06:57 -06:00

99 lines
3.0 KiB
Rust

use crate::{Error, WasiCtx};
use tracing::debug;
wiggle::from_witx!({
witx: ["$WASI_ROOT/phases/snapshot/witx/wasi_snapshot_preview1.witx"],
ctx: WasiCtx,
errors: { errno => Error },
});
use types::Errno;
impl wiggle::GuestErrorType for Errno {
fn success() -> Self {
Self::Success
}
}
impl types::GuestErrorConversion for WasiCtx {
fn into_errno(&self, e: wiggle::GuestError) -> Errno {
debug!("Guest error: {:?}", e);
e.into()
}
}
impl types::UserErrorConversion for WasiCtx {
fn errno_from_error(&self, e: Error) -> Result<Errno, String> {
debug!("Error: {:?}", e);
Ok(e.into())
}
}
impl From<Error> for Errno {
fn from(e: Error) -> Errno {
match e {
Error::Guest(e) => e.into(),
Error::TryFromInt(_) => Errno::Overflow,
Error::Utf8(_) => Errno::Ilseq,
Error::UnexpectedIo(_) => Errno::Io,
Error::GetRandom(_) => Errno::Io,
Error::TooBig => Errno::TooBig,
Error::Acces => Errno::Acces,
Error::Badf => Errno::Badf,
Error::Busy => Errno::Busy,
Error::Exist => Errno::Exist,
Error::Fault => Errno::Fault,
Error::Fbig => Errno::Fbig,
Error::Ilseq => Errno::Ilseq,
Error::Inval => Errno::Inval,
Error::Io => Errno::Io,
Error::Isdir => Errno::Isdir,
Error::Loop => Errno::Loop,
Error::Mfile => Errno::Mfile,
Error::Mlink => Errno::Mlink,
Error::Nametoolong => Errno::Nametoolong,
Error::Nfile => Errno::Nfile,
Error::Noent => Errno::Noent,
Error::Nomem => Errno::Nomem,
Error::Nospc => Errno::Nospc,
Error::Notdir => Errno::Notdir,
Error::Notempty => Errno::Notempty,
Error::Notsup => Errno::Notsup,
Error::Overflow => Errno::Overflow,
Error::Pipe => Errno::Pipe,
Error::Perm => Errno::Perm,
Error::Spipe => Errno::Spipe,
Error::Notcapable => Errno::Notcapable,
}
}
}
impl From<wiggle::GuestError> for Errno {
fn from(err: wiggle::GuestError) -> Self {
use wiggle::GuestError::*;
match err {
InvalidFlagValue { .. } => Self::Inval,
InvalidEnumValue { .. } => Self::Inval,
PtrOverflow { .. } => Self::Fault,
PtrOutOfBounds { .. } => Self::Fault,
PtrNotAligned { .. } => Self::Inval,
PtrBorrowed { .. } => Self::Fault,
InvalidUtf8 { .. } => Self::Ilseq,
TryFromIntError { .. } => Self::Overflow,
InFunc { .. } => Self::Inval,
InDataField { .. } => Self::Inval,
SliceLengthsDiffer { .. } => Self::Fault,
BorrowCheckerOutOfHandles { .. } => Self::Fault,
}
}
}
impl crate::fdpool::Fd for types::Fd {
fn as_raw(&self) -> u32 {
(*self).into()
}
fn from_raw(raw_fd: u32) -> Self {
Self::from(raw_fd)
}
}