From 5f1c0eb86b79dc950c288bc558a75324394159ba Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Thu, 16 Jan 2020 23:39:53 +0100 Subject: [PATCH] Generate strerror from witx; tweak Display for WasiError (#832) This commit introduces two small changes: * it adds `gen_errno_strerror` to `wig` crate which generates a `strerror` function for `__wasi_errno_t` directly from `*.witx`, similarly to how it's done in the `wasi` crate * it tweaks `WasiError` type to include the error message generated with `strerror` when displaying the error --- crates/wasi-common/src/error.rs | 2 +- .../wasi-common/src/old/snapshot_0/error.rs | 2 +- crates/wasi-common/src/old/snapshot_0/wasi.rs | 83 ------------------- crates/wasi-common/src/wasi.rs | 83 ------------------- crates/wasi-common/wig/src/raw_types.rs | 32 +++++++ 5 files changed, 34 insertions(+), 168 deletions(-) diff --git a/crates/wasi-common/src/error.rs b/crates/wasi-common/src/error.rs index bd48fd86b0..303b3ed547 100644 --- a/crates/wasi-common/src/error.rs +++ b/crates/wasi-common/src/error.rs @@ -8,7 +8,7 @@ use thiserror::Error; #[derive(Clone, Copy, Debug, Error, Eq, PartialEq)] #[repr(u16)] -#[error("{:?}", self)] +#[error("{:?} ({})", self, wasi::strerror(*self as wasi::__wasi_errno_t))] pub enum WasiError { ESUCCESS = wasi::__WASI_ERRNO_SUCCESS, E2BIG = wasi::__WASI_ERRNO_2BIG, diff --git a/crates/wasi-common/src/old/snapshot_0/error.rs b/crates/wasi-common/src/old/snapshot_0/error.rs index 554d01f4e3..ae634520d3 100644 --- a/crates/wasi-common/src/old/snapshot_0/error.rs +++ b/crates/wasi-common/src/old/snapshot_0/error.rs @@ -8,7 +8,7 @@ use thiserror::Error; #[derive(Clone, Copy, Debug, Error, Eq, PartialEq)] #[repr(u16)] -#[error("{:?}", self)] +#[error("{:?} ({})", self, wasi::strerror(*self as wasi::__wasi_errno_t))] pub enum WasiError { ESUCCESS = wasi::__WASI_ERRNO_SUCCESS, E2BIG = wasi::__WASI_ERRNO_2BIG, diff --git a/crates/wasi-common/src/old/snapshot_0/wasi.rs b/crates/wasi-common/src/old/snapshot_0/wasi.rs index 5f690fc03a..864585cbdf 100644 --- a/crates/wasi-common/src/old/snapshot_0/wasi.rs +++ b/crates/wasi-common/src/old/snapshot_0/wasi.rs @@ -107,89 +107,6 @@ pub(crate) const RIGHTS_TTY_BASE: __wasi_rights_t = __WASI_RIGHTS_FD_READ #[allow(unused)] pub(crate) const RIGHTS_TTY_INHERITING: __wasi_rights_t = 0; -pub fn strerror(errno: __wasi_errno_t) -> &'static str { - match errno { - __WASI_ERRNO_SUCCESS => "__WASI_ERRNO_SUCCESS", - __WASI_ERRNO_2BIG => "__WASI_ERRNO_2BIG", - __WASI_ERRNO_ACCES => "__WASI_ERRNO_ACCES", - __WASI_ERRNO_ADDRINUSE => "__WASI_ERRNO_ADDRINUSE", - __WASI_ERRNO_ADDRNOTAVAIL => "__WASI_ERRNO_ADDRNOTAVAIL", - __WASI_ERRNO_AFNOSUPPORT => "__WASI_ERRNO_AFNOSUPPORT", - __WASI_ERRNO_AGAIN => "__WASI_ERRNO_AGAIN", - __WASI_ERRNO_ALREADY => "__WASI_ERRNO_ALREADY", - __WASI_ERRNO_BADF => "__WASI_ERRNO_BADF", - __WASI_ERRNO_BADMSG => "__WASI_ERRNO_BADMSG", - __WASI_ERRNO_BUSY => "__WASI_ERRNO_BUSY", - __WASI_ERRNO_CANCELED => "__WASI_ERRNO_CANCELED", - __WASI_ERRNO_CHILD => "__WASI_ERRNO_CHILD", - __WASI_ERRNO_CONNABORTED => "__WASI_ERRNO_CONNABORTED", - __WASI_ERRNO_CONNREFUSED => "__WASI_ERRNO_CONNREFUSED", - __WASI_ERRNO_CONNRESET => "__WASI_ERRNO_CONNRESET", - __WASI_ERRNO_DEADLK => "__WASI_ERRNO_DEADLK", - __WASI_ERRNO_DESTADDRREQ => "__WASI_ERRNO_DESTADDRREQ", - __WASI_ERRNO_DOM => "__WASI_ERRNO_DOM", - __WASI_ERRNO_DQUOT => "__WASI_ERRNO_DQUOT", - __WASI_ERRNO_EXIST => "__WASI_ERRNO_EXIST", - __WASI_ERRNO_FAULT => "__WASI_ERRNO_FAULT", - __WASI_ERRNO_FBIG => "__WASI_ERRNO_FBIG", - __WASI_ERRNO_HOSTUNREACH => "__WASI_ERRNO_HOSTUNREACH", - __WASI_ERRNO_IDRM => "__WASI_ERRNO_IDRM", - __WASI_ERRNO_ILSEQ => "__WASI_ERRNO_ILSEQ", - __WASI_ERRNO_INPROGRESS => "__WASI_ERRNO_INPROGRESS", - __WASI_ERRNO_INTR => "__WASI_ERRNO_INTR", - __WASI_ERRNO_INVAL => "__WASI_ERRNO_INVAL", - __WASI_ERRNO_IO => "__WASI_ERRNO_IO", - __WASI_ERRNO_ISCONN => "__WASI_ERRNO_ISCONN", - __WASI_ERRNO_ISDIR => "__WASI_ERRNO_ISDIR", - __WASI_ERRNO_LOOP => "__WASI_ERRNO_LOOP", - __WASI_ERRNO_MFILE => "__WASI_ERRNO_MFILE", - __WASI_ERRNO_MLINK => "__WASI_ERRNO_MLINK", - __WASI_ERRNO_MSGSIZE => "__WASI_ERRNO_MSGSIZE", - __WASI_ERRNO_MULTIHOP => "__WASI_ERRNO_MULTIHOP", - __WASI_ERRNO_NAMETOOLONG => "__WASI_ERRNO_NAMETOOLONG", - __WASI_ERRNO_NETDOWN => "__WASI_ERRNO_NETDOWN", - __WASI_ERRNO_NETRESET => "__WASI_ERRNO_NETRESET", - __WASI_ERRNO_NETUNREACH => "__WASI_ERRNO_NETUNREACH", - __WASI_ERRNO_NFILE => "__WASI_ERRNO_NFILE", - __WASI_ERRNO_NOBUFS => "__WASI_ERRNO_NOBUFS", - __WASI_ERRNO_NODEV => "__WASI_ERRNO_NODEV", - __WASI_ERRNO_NOENT => "__WASI_ERRNO_NOENT", - __WASI_ERRNO_NOEXEC => "__WASI_ERRNO_NOEXEC", - __WASI_ERRNO_NOLCK => "__WASI_ERRNO_NOLCK", - __WASI_ERRNO_NOLINK => "__WASI_ERRNO_NOLINK", - __WASI_ERRNO_NOMEM => "__WASI_ERRNO_NOMEM", - __WASI_ERRNO_NOMSG => "__WASI_ERRNO_NOMSG", - __WASI_ERRNO_NOPROTOOPT => "__WASI_ERRNO_NOPROTOOPT", - __WASI_ERRNO_NOSPC => "__WASI_ERRNO_NOSPC", - __WASI_ERRNO_NOSYS => "__WASI_ERRNO_NOSYS", - __WASI_ERRNO_NOTCONN => "__WASI_ERRNO_NOTCONN", - __WASI_ERRNO_NOTDIR => "__WASI_ERRNO_NOTDIR", - __WASI_ERRNO_NOTEMPTY => "__WASI_ERRNO_NOTEMPTY", - __WASI_ERRNO_NOTRECOVERABLE => "__WASI_ERRNO_NOTRECOVERABLE", - __WASI_ERRNO_NOTSOCK => "__WASI_ERRNO_NOTSOCK", - __WASI_ERRNO_NOTSUP => "__WASI_ERRNO_NOTSUP", - __WASI_ERRNO_NOTTY => "__WASI_ERRNO_NOTTY", - __WASI_ERRNO_NXIO => "__WASI_ERRNO_NXIO", - __WASI_ERRNO_OVERFLOW => "__WASI_ERRNO_OVERFLOW", - __WASI_ERRNO_OWNERDEAD => "__WASI_ERRNO_OWNERDEAD", - __WASI_ERRNO_PERM => "__WASI_ERRNO_PERM", - __WASI_ERRNO_PIPE => "__WASI_ERRNO_PIPE", - __WASI_ERRNO_PROTO => "__WASI_ERRNO_PROTO", - __WASI_ERRNO_PROTONOSUPPORT => "__WASI_ERRNO_PROTONOSUPPORT", - __WASI_ERRNO_PROTOTYPE => "__WASI_ERRNO_PROTOTYPE", - __WASI_ERRNO_RANGE => "__WASI_ERRNO_RANGE", - __WASI_ERRNO_ROFS => "__WASI_ERRNO_ROFS", - __WASI_ERRNO_SPIPE => "__WASI_ERRNO_SPIPE", - __WASI_ERRNO_SRCH => "__WASI_ERRNO_SRCH", - __WASI_ERRNO_STALE => "__WASI_ERRNO_STALE", - __WASI_ERRNO_TIMEDOUT => "__WASI_ERRNO_TIMEDOUT", - __WASI_ERRNO_TXTBSY => "__WASI_ERRNO_TXTBSY", - __WASI_ERRNO_XDEV => "__WASI_ERRNO_XDEV", - __WASI_ERRNO_NOTCAPABLE => "__WASI_ERRNO_NOTCAPABLE", - other => panic!("Undefined errno value {:?}", other), - } -} - pub fn whence_to_str(whence: __wasi_whence_t) -> &'static str { match whence { __WASI_WHENCE_CUR => "__WASI_WHENCE_CUR", diff --git a/crates/wasi-common/src/wasi.rs b/crates/wasi-common/src/wasi.rs index 5bcc4d3b27..2c05d03957 100644 --- a/crates/wasi-common/src/wasi.rs +++ b/crates/wasi-common/src/wasi.rs @@ -107,89 +107,6 @@ pub(crate) const RIGHTS_TTY_BASE: __wasi_rights_t = __WASI_RIGHTS_FD_READ #[allow(unused)] pub(crate) const RIGHTS_TTY_INHERITING: __wasi_rights_t = 0; -pub fn strerror(errno: __wasi_errno_t) -> &'static str { - match errno { - __WASI_ERRNO_SUCCESS => "__WASI_ERRNO_SUCCESS", - __WASI_ERRNO_2BIG => "__WASI_ERRNO_2BIG", - __WASI_ERRNO_ACCES => "__WASI_ERRNO_ACCES", - __WASI_ERRNO_ADDRINUSE => "__WASI_ERRNO_ADDRINUSE", - __WASI_ERRNO_ADDRNOTAVAIL => "__WASI_ERRNO_ADDRNOTAVAIL", - __WASI_ERRNO_AFNOSUPPORT => "__WASI_ERRNO_AFNOSUPPORT", - __WASI_ERRNO_AGAIN => "__WASI_ERRNO_AGAIN", - __WASI_ERRNO_ALREADY => "__WASI_ERRNO_ALREADY", - __WASI_ERRNO_BADF => "__WASI_ERRNO_BADF", - __WASI_ERRNO_BADMSG => "__WASI_ERRNO_BADMSG", - __WASI_ERRNO_BUSY => "__WASI_ERRNO_BUSY", - __WASI_ERRNO_CANCELED => "__WASI_ERRNO_CANCELED", - __WASI_ERRNO_CHILD => "__WASI_ERRNO_CHILD", - __WASI_ERRNO_CONNABORTED => "__WASI_ERRNO_CONNABORTED", - __WASI_ERRNO_CONNREFUSED => "__WASI_ERRNO_CONNREFUSED", - __WASI_ERRNO_CONNRESET => "__WASI_ERRNO_CONNRESET", - __WASI_ERRNO_DEADLK => "__WASI_ERRNO_DEADLK", - __WASI_ERRNO_DESTADDRREQ => "__WASI_ERRNO_DESTADDRREQ", - __WASI_ERRNO_DOM => "__WASI_ERRNO_DOM", - __WASI_ERRNO_DQUOT => "__WASI_ERRNO_DQUOT", - __WASI_ERRNO_EXIST => "__WASI_ERRNO_EXIST", - __WASI_ERRNO_FAULT => "__WASI_ERRNO_FAULT", - __WASI_ERRNO_FBIG => "__WASI_ERRNO_FBIG", - __WASI_ERRNO_HOSTUNREACH => "__WASI_ERRNO_HOSTUNREACH", - __WASI_ERRNO_IDRM => "__WASI_ERRNO_IDRM", - __WASI_ERRNO_ILSEQ => "__WASI_ERRNO_ILSEQ", - __WASI_ERRNO_INPROGRESS => "__WASI_ERRNO_INPROGRESS", - __WASI_ERRNO_INTR => "__WASI_ERRNO_INTR", - __WASI_ERRNO_INVAL => "__WASI_ERRNO_INVAL", - __WASI_ERRNO_IO => "__WASI_ERRNO_IO", - __WASI_ERRNO_ISCONN => "__WASI_ERRNO_ISCONN", - __WASI_ERRNO_ISDIR => "__WASI_ERRNO_ISDIR", - __WASI_ERRNO_LOOP => "__WASI_ERRNO_LOOP", - __WASI_ERRNO_MFILE => "__WASI_ERRNO_MFILE", - __WASI_ERRNO_MLINK => "__WASI_ERRNO_MLINK", - __WASI_ERRNO_MSGSIZE => "__WASI_ERRNO_MSGSIZE", - __WASI_ERRNO_MULTIHOP => "__WASI_ERRNO_MULTIHOP", - __WASI_ERRNO_NAMETOOLONG => "__WASI_ERRNO_NAMETOOLONG", - __WASI_ERRNO_NETDOWN => "__WASI_ERRNO_NETDOWN", - __WASI_ERRNO_NETRESET => "__WASI_ERRNO_NETRESET", - __WASI_ERRNO_NETUNREACH => "__WASI_ERRNO_NETUNREACH", - __WASI_ERRNO_NFILE => "__WASI_ERRNO_NFILE", - __WASI_ERRNO_NOBUFS => "__WASI_ERRNO_NOBUFS", - __WASI_ERRNO_NODEV => "__WASI_ERRNO_NODEV", - __WASI_ERRNO_NOENT => "__WASI_ERRNO_NOENT", - __WASI_ERRNO_NOEXEC => "__WASI_ERRNO_NOEXEC", - __WASI_ERRNO_NOLCK => "__WASI_ERRNO_NOLCK", - __WASI_ERRNO_NOLINK => "__WASI_ERRNO_NOLINK", - __WASI_ERRNO_NOMEM => "__WASI_ERRNO_NOMEM", - __WASI_ERRNO_NOMSG => "__WASI_ERRNO_NOMSG", - __WASI_ERRNO_NOPROTOOPT => "__WASI_ERRNO_NOPROTOOPT", - __WASI_ERRNO_NOSPC => "__WASI_ERRNO_NOSPC", - __WASI_ERRNO_NOSYS => "__WASI_ERRNO_NOSYS", - __WASI_ERRNO_NOTCONN => "__WASI_ERRNO_NOTCONN", - __WASI_ERRNO_NOTDIR => "__WASI_ERRNO_NOTDIR", - __WASI_ERRNO_NOTEMPTY => "__WASI_ERRNO_NOTEMPTY", - __WASI_ERRNO_NOTRECOVERABLE => "__WASI_ERRNO_NOTRECOVERABLE", - __WASI_ERRNO_NOTSOCK => "__WASI_ERRNO_NOTSOCK", - __WASI_ERRNO_NOTSUP => "__WASI_ERRNO_NOTSUP", - __WASI_ERRNO_NOTTY => "__WASI_ERRNO_NOTTY", - __WASI_ERRNO_NXIO => "__WASI_ERRNO_NXIO", - __WASI_ERRNO_OVERFLOW => "__WASI_ERRNO_OVERFLOW", - __WASI_ERRNO_OWNERDEAD => "__WASI_ERRNO_OWNERDEAD", - __WASI_ERRNO_PERM => "__WASI_ERRNO_PERM", - __WASI_ERRNO_PIPE => "__WASI_ERRNO_PIPE", - __WASI_ERRNO_PROTO => "__WASI_ERRNO_PROTO", - __WASI_ERRNO_PROTONOSUPPORT => "__WASI_ERRNO_PROTONOSUPPORT", - __WASI_ERRNO_PROTOTYPE => "__WASI_ERRNO_PROTOTYPE", - __WASI_ERRNO_RANGE => "__WASI_ERRNO_RANGE", - __WASI_ERRNO_ROFS => "__WASI_ERRNO_ROFS", - __WASI_ERRNO_SPIPE => "__WASI_ERRNO_SPIPE", - __WASI_ERRNO_SRCH => "__WASI_ERRNO_SRCH", - __WASI_ERRNO_STALE => "__WASI_ERRNO_STALE", - __WASI_ERRNO_TIMEDOUT => "__WASI_ERRNO_TIMEDOUT", - __WASI_ERRNO_TXTBSY => "__WASI_ERRNO_TXTBSY", - __WASI_ERRNO_XDEV => "__WASI_ERRNO_XDEV", - __WASI_ERRNO_NOTCAPABLE => "__WASI_ERRNO_NOTCAPABLE", - other => panic!("Undefined errno value {:?}", other), - } -} - pub fn whence_to_str(whence: __wasi_whence_t) -> &'static str { match whence { __WASI_WHENCE_CUR => "__WASI_WHENCE_CUR", diff --git a/crates/wasi-common/wig/src/raw_types.rs b/crates/wasi-common/wig/src/raw_types.rs index 15de007bdb..44b82db391 100644 --- a/crates/wasi-common/wig/src/raw_types.rs +++ b/crates/wasi-common/wig/src/raw_types.rs @@ -148,6 +148,38 @@ fn gen_datatype(output: &mut TokenStream, mode: Mode, namedtype: &witx::NamedTyp } }, } + + if namedtype.name.as_str() == "errno" { + // Generate strerror for errno type + gen_errno_strerror(output, namedtype); + } +} + +fn gen_errno_strerror(output: &mut TokenStream, namedtype: &witx::NamedType) { + let inner = match &namedtype.dt { + witx::TypeRef::Value(v) => match &**v { + witx::Type::Enum(e) => e, + x => panic!("expected Enum('errno'), instead received {:?}", x), + }, + x => panic!("expected Enum('errno'), instead received {:?}", x), + }; + let mut inner_group = TokenStream::new(); + for variant in &inner.variants { + let value_name = format_ident!( + "__WASI_ERRNO_{}", + variant.name.as_str().to_shouty_snake_case() + ); + let docs = variant.docs.trim(); + inner_group.extend(quote!(#value_name => #docs,)); + } + output.extend( + quote!(pub fn strerror(errno: __wasi_errno_t) -> &'static str { + match errno { + #inner_group + other => panic!("Undefined errno value {:?}", other), + } + }), + ); } fn int_repr_tokens(int_repr: witx::IntRepr) -> TokenStream {