trap proc_exit and unsupported funcs

This commit is contained in:
Pat Hickey
2021-01-07 14:56:22 -08:00
parent c2a715ca54
commit 82128cbc5b
2 changed files with 62 additions and 49 deletions

View File

@@ -34,6 +34,10 @@ pub enum Error {
#[error("Unexpected IoError: {0}")] #[error("Unexpected IoError: {0}")]
UnexpectedIo(#[source] std::io::Error), UnexpectedIo(#[source] std::io::Error),
/// An unsupported feature of Wasi was used. This error will trap.
#[error("Unsupported feature: {0}")]
Unsupported(&'static str),
// Below this, all variants are from the `$errno` type: // Below this, all variants are from the `$errno` type:
/// Errno::TooBig: Argument list too long /// Errno::TooBig: Argument list too long
#[error("TooBig: Argument list too long")] #[error("TooBig: Argument list too long")]

View File

@@ -32,23 +32,27 @@ impl types::GuestErrorConversion for WasiCtx {
impl types::UserErrorConversion for WasiCtx { impl types::UserErrorConversion for WasiCtx {
fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> { fn errno_from_error(&self, e: Error) -> Result<types::Errno, wiggle::Trap> {
debug!("Error: {:?}", e); debug!("Error: {:?}", e);
Ok(e.into()) e.try_into()
} }
} }
impl From<Error> for types::Errno { impl TryFrom<Error> for types::Errno {
fn from(e: Error) -> types::Errno { type Error = wiggle::Trap;
fn try_from(e: Error) -> Result<types::Errno, wiggle::Trap> {
use std::io::ErrorKind; use std::io::ErrorKind;
use types::Errno; use types::Errno;
match e { match e {
Error::Guest(e) => e.into(), Error::Unsupported(feat) => {
Error::TryFromInt(_) => Errno::Overflow, Err(wiggle::Trap::String(format!("Unsupported: {0}", feat)))
Error::Utf8(_) => Errno::Ilseq, }
Error::Guest(e) => Ok(e.into()),
Error::TryFromInt(_) => Ok(Errno::Overflow),
Error::Utf8(_) => Ok(Errno::Ilseq),
Error::UnexpectedIo(e) => match e.kind() { Error::UnexpectedIo(e) => match e.kind() {
ErrorKind::NotFound => Errno::Noent, ErrorKind::NotFound => Ok(Errno::Noent),
ErrorKind::PermissionDenied => Errno::Perm, ErrorKind::PermissionDenied => Ok(Errno::Perm),
ErrorKind::AlreadyExists => Errno::Exist, ErrorKind::AlreadyExists => Ok(Errno::Exist),
ErrorKind::InvalidInput => Errno::Ilseq, ErrorKind::InvalidInput => Ok(Errno::Ilseq),
ErrorKind::ConnectionRefused ErrorKind::ConnectionRefused
| ErrorKind::ConnectionReset | ErrorKind::ConnectionReset
| ErrorKind::ConnectionAborted | ErrorKind::ConnectionAborted
@@ -63,40 +67,40 @@ impl From<Error> for types::Errno {
| ErrorKind::Interrupted | ErrorKind::Interrupted
| ErrorKind::Other | ErrorKind::Other
| ErrorKind::UnexpectedEof | ErrorKind::UnexpectedEof
| _ => Errno::Io, | _ => Ok(Errno::Io),
}, },
Error::CapRand(_) => Errno::Io, Error::CapRand(_) => Ok(Errno::Io),
Error::TooBig => Errno::TooBig, Error::TooBig => Ok(Errno::TooBig),
Error::Acces => Errno::Acces, Error::Acces => Ok(Errno::Acces),
Error::Badf => Errno::Badf, Error::Badf => Ok(Errno::Badf),
Error::Busy => Errno::Busy, Error::Busy => Ok(Errno::Busy),
Error::Exist => Errno::Exist, Error::Exist => Ok(Errno::Exist),
Error::Fault => Errno::Fault, Error::Fault => Ok(Errno::Fault),
Error::Fbig => Errno::Fbig, Error::Fbig => Ok(Errno::Fbig),
Error::Ilseq => Errno::Ilseq, Error::Ilseq => Ok(Errno::Ilseq),
Error::Inval => Errno::Inval, Error::Inval => Ok(Errno::Inval),
Error::Io => Errno::Io, Error::Io => Ok(Errno::Io),
Error::Isdir => Errno::Isdir, Error::Isdir => Ok(Errno::Isdir),
Error::Loop => Errno::Loop, Error::Loop => Ok(Errno::Loop),
Error::Mfile => Errno::Mfile, Error::Mfile => Ok(Errno::Mfile),
Error::Mlink => Errno::Mlink, Error::Mlink => Ok(Errno::Mlink),
Error::Nametoolong => Errno::Nametoolong, Error::Nametoolong => Ok(Errno::Nametoolong),
Error::Nfile => Errno::Nfile, Error::Nfile => Ok(Errno::Nfile),
Error::Noent => Errno::Noent, Error::Noent => Ok(Errno::Noent),
Error::Nomem => Errno::Nomem, Error::Nomem => Ok(Errno::Nomem),
Error::Nospc => Errno::Nospc, Error::Nospc => Ok(Errno::Nospc),
Error::Notdir => Errno::Notdir, Error::Notdir => Ok(Errno::Notdir),
Error::Notempty => Errno::Notempty, Error::Notempty => Ok(Errno::Notempty),
Error::Notsup => Errno::Notsup, Error::Notsup => Ok(Errno::Notsup),
Error::Overflow => Errno::Overflow, Error::Overflow => Ok(Errno::Overflow),
Error::Pipe => Errno::Pipe, Error::Pipe => Ok(Errno::Pipe),
Error::Perm => Errno::Perm, Error::Perm => Ok(Errno::Perm),
Error::Range => Errno::Range, Error::Range => Ok(Errno::Range),
Error::Spipe => Errno::Spipe, Error::Spipe => Ok(Errno::Spipe),
Error::FileNotCapable { .. } => Errno::Notcapable, Error::FileNotCapable { .. } => Ok(Errno::Notcapable),
Error::DirNotCapable { .. } => Errno::Notcapable, Error::DirNotCapable { .. } => Ok(Errno::Notcapable),
Error::NotCapable => Errno::Notcapable, Error::NotCapable => Ok(Errno::Notcapable),
Error::TableOverflow => Errno::Overflow, Error::TableOverflow => Ok(Errno::Overflow),
} }
} }
} }
@@ -885,12 +889,17 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
unimplemented!() unimplemented!()
} }
fn proc_exit(&self, _rval: types::Exitcode) -> wiggle::Trap { fn proc_exit(&self, status: types::Exitcode) -> wiggle::Trap {
unimplemented!() // Check that the status is within WASI's range.
if status < 126 {
wiggle::Trap::I32Exit(status as i32)
} else {
wiggle::Trap::String("exit with invalid exit status outside of [0..126)".to_owned())
}
} }
fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> { fn proc_raise(&self, _sig: types::Signal) -> Result<(), Error> {
unimplemented!() Err(Error::Unsupported("proc_raise"))
} }
fn sched_yield(&self) -> Result<(), Error> { fn sched_yield(&self) -> Result<(), Error> {
@@ -909,7 +918,7 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
_ri_data: &types::IovecArray<'_>, _ri_data: &types::IovecArray<'_>,
_ri_flags: types::Riflags, _ri_flags: types::Riflags,
) -> Result<(types::Size, types::Roflags), Error> { ) -> Result<(types::Size, types::Roflags), Error> {
unimplemented!() Err(Error::Unsupported("sock_recv"))
} }
fn sock_send( fn sock_send(
@@ -918,11 +927,11 @@ impl<'a> wasi_snapshot_preview1::WasiSnapshotPreview1 for WasiCtx {
_si_data: &types::CiovecArray<'_>, _si_data: &types::CiovecArray<'_>,
_si_flags: types::Siflags, _si_flags: types::Siflags,
) -> Result<types::Size, Error> { ) -> Result<types::Size, Error> {
unimplemented!() Err(Error::Unsupported("sock_send"))
} }
fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> { fn sock_shutdown(&self, _fd: types::Fd, _how: types::Sdflags) -> Result<(), Error> {
unimplemented!() Err(Error::Unsupported("sock_shutdown"))
} }
} }