Path: blob/main/crates/wasi-common/src/snapshots/preview_1/error.rs
2465 views
pub use super::types::{Errno, Error};12pub trait ErrorExt {3fn not_found() -> Self;4fn too_big() -> Self;5fn badf() -> Self;6fn exist() -> Self;7fn illegal_byte_sequence() -> Self;8fn invalid_argument() -> Self;9fn io() -> Self;10fn name_too_long() -> Self;11fn not_dir() -> Self;12fn not_supported() -> Self;13fn overflow() -> Self;14fn range() -> Self;15fn seek_pipe() -> Self;16fn perm() -> Self;17}1819impl ErrorExt for Error {20fn not_found() -> Self {21Errno::Noent.into()22}23fn too_big() -> Self {24Errno::TooBig.into()25}26fn badf() -> Self {27Errno::Badf.into()28}29fn exist() -> Self {30Errno::Exist.into()31}32fn illegal_byte_sequence() -> Self {33Errno::Ilseq.into()34}35fn invalid_argument() -> Self {36Errno::Inval.into()37}38fn io() -> Self {39Errno::Io.into()40}41fn name_too_long() -> Self {42Errno::Nametoolong.into()43}44fn not_dir() -> Self {45Errno::Notdir.into()46}47fn not_supported() -> Self {48Errno::Notsup.into()49}50fn overflow() -> Self {51Errno::Overflow.into()52}53fn range() -> Self {54Errno::Range.into()55}56fn seek_pipe() -> Self {57Errno::Spipe.into()58}59fn perm() -> Self {60Errno::Perm.into()61}62}6364#[cfg(unix)]65fn from_raw_os_error(err: Option<i32>) -> Option<Error> {66use rustix::io::Errno as RustixErrno;67if err.is_none() {68return None;69}70Some(match RustixErrno::from_raw_os_error(err.unwrap()) {71RustixErrno::AGAIN => Errno::Again.into(),72RustixErrno::PIPE => Errno::Pipe.into(),73RustixErrno::PERM => Errno::Perm.into(),74RustixErrno::NOENT => Errno::Noent.into(),75RustixErrno::NOMEM => Errno::Nomem.into(),76RustixErrno::TOOBIG => Errno::TooBig.into(),77RustixErrno::IO => Errno::Io.into(),78RustixErrno::BADF => Errno::Badf.into(),79RustixErrno::BUSY => Errno::Busy.into(),80RustixErrno::ACCESS => Errno::Acces.into(),81RustixErrno::FAULT => Errno::Fault.into(),82RustixErrno::NOTDIR => Errno::Notdir.into(),83RustixErrno::ISDIR => Errno::Isdir.into(),84RustixErrno::INVAL => Errno::Inval.into(),85RustixErrno::EXIST => Errno::Exist.into(),86RustixErrno::FBIG => Errno::Fbig.into(),87RustixErrno::NOSPC => Errno::Nospc.into(),88RustixErrno::SPIPE => Errno::Spipe.into(),89RustixErrno::MFILE => Errno::Mfile.into(),90RustixErrno::MLINK => Errno::Mlink.into(),91RustixErrno::NAMETOOLONG => Errno::Nametoolong.into(),92RustixErrno::NFILE => Errno::Nfile.into(),93RustixErrno::NOTEMPTY => Errno::Notempty.into(),94RustixErrno::LOOP => Errno::Loop.into(),95RustixErrno::OVERFLOW => Errno::Overflow.into(),96RustixErrno::ILSEQ => Errno::Ilseq.into(),97RustixErrno::NOTSUP => Errno::Notsup.into(),98RustixErrno::ADDRINUSE => Errno::Addrinuse.into(),99RustixErrno::CANCELED => Errno::Canceled.into(),100RustixErrno::ADDRNOTAVAIL => Errno::Addrnotavail.into(),101RustixErrno::AFNOSUPPORT => Errno::Afnosupport.into(),102RustixErrno::ALREADY => Errno::Already.into(),103RustixErrno::CONNABORTED => Errno::Connaborted.into(),104RustixErrno::CONNREFUSED => Errno::Connrefused.into(),105RustixErrno::CONNRESET => Errno::Connreset.into(),106RustixErrno::DESTADDRREQ => Errno::Destaddrreq.into(),107RustixErrno::DQUOT => Errno::Dquot.into(),108RustixErrno::HOSTUNREACH => Errno::Hostunreach.into(),109RustixErrno::INPROGRESS => Errno::Inprogress.into(),110RustixErrno::INTR => Errno::Intr.into(),111RustixErrno::ISCONN => Errno::Isconn.into(),112RustixErrno::MSGSIZE => Errno::Msgsize.into(),113RustixErrno::NETDOWN => Errno::Netdown.into(),114RustixErrno::NETRESET => Errno::Netreset.into(),115RustixErrno::NETUNREACH => Errno::Netunreach.into(),116RustixErrno::NOBUFS => Errno::Nobufs.into(),117RustixErrno::NOPROTOOPT => Errno::Noprotoopt.into(),118RustixErrno::NOTCONN => Errno::Notconn.into(),119RustixErrno::NOTSOCK => Errno::Notsock.into(),120RustixErrno::PROTONOSUPPORT => Errno::Protonosupport.into(),121RustixErrno::PROTOTYPE => Errno::Prototype.into(),122RustixErrno::STALE => Errno::Stale.into(),123RustixErrno::TIMEDOUT => Errno::Timedout.into(),124125// On some platforms.into(), these have the same value as other errno values.126#[allow(unreachable_patterns, reason = "see comment")]127RustixErrno::WOULDBLOCK => Errno::Again.into(),128#[allow(unreachable_patterns, reason = "see comment")]129RustixErrno::OPNOTSUPP => Errno::Notsup.into(),130131_ => return None,132})133}134#[cfg(windows)]135fn from_raw_os_error(raw_os_error: Option<i32>) -> Option<Error> {136use windows_sys::Win32::Foundation;137use windows_sys::Win32::Networking::WinSock;138139match raw_os_error.map(|code| code as u32) {140Some(Foundation::ERROR_BAD_ENVIRONMENT) => return Some(Errno::TooBig.into()),141Some(Foundation::ERROR_FILE_NOT_FOUND) => return Some(Errno::Noent.into()),142Some(Foundation::ERROR_PATH_NOT_FOUND) => return Some(Errno::Noent.into()),143Some(Foundation::ERROR_TOO_MANY_OPEN_FILES) => return Some(Errno::Nfile.into()),144Some(Foundation::ERROR_ACCESS_DENIED) => return Some(Errno::Acces.into()),145Some(Foundation::ERROR_SHARING_VIOLATION) => return Some(Errno::Acces.into()),146Some(Foundation::ERROR_PRIVILEGE_NOT_HELD) => return Some(Errno::Perm.into()),147Some(Foundation::ERROR_INVALID_HANDLE) => return Some(Errno::Badf.into()),148Some(Foundation::ERROR_INVALID_NAME) => return Some(Errno::Noent.into()),149Some(Foundation::ERROR_NOT_ENOUGH_MEMORY) => return Some(Errno::Nomem.into()),150Some(Foundation::ERROR_OUTOFMEMORY) => return Some(Errno::Nomem.into()),151Some(Foundation::ERROR_DIR_NOT_EMPTY) => return Some(Errno::Notempty.into()),152Some(Foundation::ERROR_NOT_READY) => return Some(Errno::Busy.into()),153Some(Foundation::ERROR_BUSY) => return Some(Errno::Busy.into()),154Some(Foundation::ERROR_NOT_SUPPORTED) => return Some(Errno::Notsup.into()),155Some(Foundation::ERROR_FILE_EXISTS) => return Some(Errno::Exist.into()),156Some(Foundation::ERROR_BROKEN_PIPE) => return Some(Errno::Pipe.into()),157Some(Foundation::ERROR_BUFFER_OVERFLOW) => return Some(Errno::Nametoolong.into()),158Some(Foundation::ERROR_NOT_A_REPARSE_POINT) => return Some(Errno::Inval.into()),159Some(Foundation::ERROR_NEGATIVE_SEEK) => return Some(Errno::Inval.into()),160Some(Foundation::ERROR_DIRECTORY) => return Some(Errno::Notdir.into()),161Some(Foundation::ERROR_ALREADY_EXISTS) => return Some(Errno::Exist.into()),162Some(Foundation::ERROR_STOPPED_ON_SYMLINK) => return Some(Errno::Loop.into()),163Some(Foundation::ERROR_DIRECTORY_NOT_SUPPORTED) => return Some(Errno::Isdir.into()),164_ => {}165}166167match raw_os_error {168Some(WinSock::WSAEWOULDBLOCK) => Some(Errno::Again.into()),169Some(WinSock::WSAECANCELLED) => Some(Errno::Canceled.into()),170Some(WinSock::WSA_E_CANCELLED) => Some(Errno::Canceled.into()),171Some(WinSock::WSAEBADF) => Some(Errno::Badf.into()),172Some(WinSock::WSAEFAULT) => Some(Errno::Fault.into()),173Some(WinSock::WSAEINVAL) => Some(Errno::Inval.into()),174Some(WinSock::WSAEMFILE) => Some(Errno::Mfile.into()),175Some(WinSock::WSAENAMETOOLONG) => Some(Errno::Nametoolong.into()),176Some(WinSock::WSAENOTEMPTY) => Some(Errno::Notempty.into()),177Some(WinSock::WSAELOOP) => Some(Errno::Loop.into()),178Some(WinSock::WSAEOPNOTSUPP) => Some(Errno::Notsup.into()),179Some(WinSock::WSAEADDRINUSE) => Some(Errno::Addrinuse.into()),180Some(WinSock::WSAEACCES) => Some(Errno::Acces.into()),181Some(WinSock::WSAEADDRNOTAVAIL) => Some(Errno::Addrnotavail.into()),182Some(WinSock::WSAEAFNOSUPPORT) => Some(Errno::Afnosupport.into()),183Some(WinSock::WSAEALREADY) => Some(Errno::Already.into()),184Some(WinSock::WSAECONNABORTED) => Some(Errno::Connaborted.into()),185Some(WinSock::WSAECONNREFUSED) => Some(Errno::Connrefused.into()),186Some(WinSock::WSAECONNRESET) => Some(Errno::Connreset.into()),187Some(WinSock::WSAEDESTADDRREQ) => Some(Errno::Destaddrreq.into()),188Some(WinSock::WSAEDQUOT) => Some(Errno::Dquot.into()),189Some(WinSock::WSAEHOSTUNREACH) => Some(Errno::Hostunreach.into()),190Some(WinSock::WSAEINPROGRESS) => Some(Errno::Inprogress.into()),191Some(WinSock::WSAEINTR) => Some(Errno::Intr.into()),192Some(WinSock::WSAEISCONN) => Some(Errno::Isconn.into()),193Some(WinSock::WSAEMSGSIZE) => Some(Errno::Msgsize.into()),194Some(WinSock::WSAENETDOWN) => Some(Errno::Netdown.into()),195Some(WinSock::WSAENETRESET) => Some(Errno::Netreset.into()),196Some(WinSock::WSAENETUNREACH) => Some(Errno::Netunreach.into()),197Some(WinSock::WSAENOBUFS) => Some(Errno::Nobufs.into()),198Some(WinSock::WSAENOPROTOOPT) => Some(Errno::Noprotoopt.into()),199Some(WinSock::WSAENOTCONN) => Some(Errno::Notconn.into()),200Some(WinSock::WSAENOTSOCK) => Some(Errno::Notsock.into()),201Some(WinSock::WSAEPROTONOSUPPORT) => Some(Errno::Protonosupport.into()),202Some(WinSock::WSAEPROTOTYPE) => Some(Errno::Prototype.into()),203Some(WinSock::WSAESTALE) => Some(Errno::Stale.into()),204Some(WinSock::WSAETIMEDOUT) => Some(Errno::Timedout.into()),205_ => None,206}207}208209impl From<std::io::Error> for Error {210fn from(err: std::io::Error) -> Error {211match from_raw_os_error(err.raw_os_error()) {212Some(errno) => errno,213None => match err.kind() {214std::io::ErrorKind::NotFound => Errno::Noent.into(),215std::io::ErrorKind::PermissionDenied => Errno::Perm.into(),216std::io::ErrorKind::AlreadyExists => Errno::Exist.into(),217std::io::ErrorKind::InvalidInput => Errno::Inval.into(),218std::io::ErrorKind::WouldBlock => Errno::Again.into(),219_ => Error::trap(anyhow::anyhow!(err).context("Unknown OS error")),220},221}222}223}224225impl From<cap_rand::Error> for Error {226fn from(err: cap_rand::Error) -> Error {227// I picked Error::Io as a 'reasonable default', FIXME dan is this ok?228from_raw_os_error(err.raw_os_error()).unwrap_or_else(|| Error::from(Errno::Io))229}230}231232impl From<wiggle::GuestError> for Error {233fn from(err: wiggle::GuestError) -> Error {234use wiggle::GuestError::*;235match err {236InvalidFlagValue { .. } => Errno::Inval.into(),237InvalidEnumValue { .. } => Errno::Inval.into(),238// As per239// https://github.com/WebAssembly/wasi/blob/main/legacy/tools/witx-docs.md#pointers240//241// > If a misaligned pointer is passed to a function, the function242// > shall trap.243// >244// > If an out-of-bounds pointer is passed to a function and the245// > function needs to dereference it, the function shall trap.246//247// so this turns OOB and misalignment errors into traps.248PtrOverflow { .. } | PtrOutOfBounds { .. } | PtrNotAligned { .. } => {249Error::trap(err.into())250}251InvalidUtf8 { .. } => Errno::Ilseq.into(),252TryFromIntError { .. } => Errno::Overflow.into(),253SliceLengthsDiffer { .. } => Errno::Fault.into(),254InFunc { err, .. } => Error::from(*err),255}256}257}258259impl From<std::num::TryFromIntError> for Error {260fn from(_err: std::num::TryFromIntError) -> Error {261Errno::Overflow.into()262}263}264265266