Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasi-common] Log string representation of WASI errno at the trace level #760

Merged
merged 3 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions crates/wasi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ pub enum Error {
#[cfg(unix)]
#[error("Yanix error: {0}")]
Yanix(#[from] yanix::YanixError),
#[cfg(windows)]
#[error("Winx error: {0}")]
Winx(#[from] winx::winerror::WinError),
}

impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Self::Wasi(WasiError::EOVERFLOW)
Self::EOVERFLOW
}
}

Expand All @@ -123,41 +120,49 @@ impl From<Infallible> for Error {

impl From<str::Utf8Error> for Error {
fn from(_: str::Utf8Error) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl Error {
pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t {
pub(crate) fn as_wasi_error(&self) -> WasiError {
match self {
Self::Wasi(no) => no.as_raw_errno(),
Self::Io(e) => errno_from_ioerror(e.to_owned()),
Self::Wasi(err) => *err,
Self::Io(err) => {
let err = match err.raw_os_error() {
Some(code) => Self::from_raw_os_error(code),
None => {
log::debug!("Inconvertible OS error: {}", err);
Self::EIO
}
};
err.as_wasi_error()
}
#[cfg(unix)]
Self::Yanix(err) => {
use yanix::YanixError::*;
let err = match err {
Errno(errno) => crate::sys::host_impl::errno_from_nix(*errno),
let err: Self = match err {
Errno(errno) => (*errno).into(),
NulError(err) => err.into(),
TryFromIntError(err) => (*err).into(),
};
err.as_wasi_errno()
err.as_wasi_error()
}
#[cfg(windows)]
Self::Winx(err) => crate::sys::host_impl::errno_from_win(*err),
}
}

pub const ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS);
pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG);
pub const EACCES: Self = Error::Wasi(WasiError::EACCES);
Expand Down Expand Up @@ -237,12 +242,6 @@ impl Error {
pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE);
}

fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t {
match e.raw_os_error() {
Some(code) => crate::sys::errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
wasi::__WASI_ERRNO_IO
}
}
pub(crate) trait FromRawOsError {
fn from_raw_os_error(code: i32) -> Self;
}
2 changes: 1 addition & 1 deletion crates/wasi-common/src/hostcalls/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::{hostcalls_impl, wasi, wasi32};
use crate::{wasi, wasi32};

hostcalls! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, memory: &mut [u8], fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/src/hostcalls/misc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::{hostcalls_impl, wasi, wasi32};
use crate::{wasi, wasi32};
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen;

Expand Down
16 changes: 8 additions & 8 deletions crates/wasi-common/src/hostcalls_impl/fs_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)]
use crate::sys::host_impl;
use crate::sys::hostcalls_impl::fs_helpers::*;
use crate::{fdentry::FdEntry, wasi, Error, Result};
use crate::{error::WasiError, fdentry::FdEntry, wasi, Error, Result};
use std::fs::File;
use std::path::{Component, Path};

Expand Down Expand Up @@ -117,10 +117,10 @@ pub(crate) fn path_get(
dir_stack.push(new_dir);
}
Err(e) => {
match e.as_wasi_errno() {
wasi::__WASI_ERRNO_LOOP
| wasi::__WASI_ERRNO_MLINK
| wasi::__WASI_ERRNO_NOTDIR =>
match e.as_wasi_error() {
WasiError::ELOOP
| WasiError::EMLINK
| WasiError::ENOTDIR =>
// Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag.
{
Expand Down Expand Up @@ -179,12 +179,12 @@ pub(crate) fn path_get(
continue;
}
Err(e) => {
if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT
if e.as_wasi_error() != WasiError::EINVAL
&& e.as_wasi_error() != 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_errno() != wasi::__WASI_ERRNO_NOTDIR
&& e.as_wasi_error() != WasiError::ENOTDIR
{
return Err(e);
}
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/src/hostcalls_impl/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ pub(crate) fn poll_oneoff(
let event = wasi::__wasi_event_t {
userdata: subscription.userdata,
r#type,
error: err.as_wasi_errno(),
error: err.as_wasi_error().as_raw_errno(),
u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0,
Expand Down
24 changes: 12 additions & 12 deletions crates/wasi-common/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ macro_rules! hostcalls {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen]
pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = match hostcalls_impl::$name($($arg,)*) {
Ok(()) => wasi::__WASI_ERRNO_SUCCESS,
Err(e) => e.as_wasi_errno(),
};

ret
let ret = crate::hostcalls_impl::$name($($arg,)*)
.err()
.unwrap_or(crate::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
)*)
}
Expand All @@ -18,12 +18,12 @@ macro_rules! hostcalls_old {
($(pub unsafe fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen_old]
pub unsafe fn $name($($arg: $ty,)*) -> $ret {
let ret = match hostcalls_impl::$name($($arg,)*) {
Ok(()) => wasi::__WASI_ERRNO_SUCCESS,
Err(e) => e.as_wasi_errno(),
};

ret
let ret = crate::old::snapshot_0::hostcalls_impl::$name($($arg,)*)
.err()
.unwrap_or(crate::old::snapshot_0::Error::ESUCCESS)
.as_wasi_error();
log::trace!(" | errno={}", ret);
ret.as_raw_errno()
}
)*)
}
45 changes: 22 additions & 23 deletions crates/wasi-common/src/old/snapshot_0/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,11 @@ pub enum Error {
#[cfg(unix)]
#[error("Yanix error: {0}")]
Yanix(#[from] yanix::YanixError),
#[cfg(windows)]
#[error("Winx error: {0}")]
Winx(#[from] winx::winerror::WinError),
}

impl From<TryFromIntError> for Error {
fn from(_: TryFromIntError) -> Self {
Self::Wasi(WasiError::EOVERFLOW)
Self::EOVERFLOW
}
}

Expand All @@ -123,41 +120,49 @@ impl From<Infallible> for Error {

impl From<str::Utf8Error> for Error {
fn from(_: str::Utf8Error) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl From<ffi::NulError> for Error {
fn from(_: ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl From<&ffi::NulError> for Error {
fn from(_: &ffi::NulError) -> Self {
Self::Wasi(WasiError::EILSEQ)
Self::EILSEQ
}
}

impl Error {
pub(crate) fn as_wasi_errno(&self) -> wasi::__wasi_errno_t {
pub(crate) fn as_wasi_error(&self) -> WasiError {
match self {
Self::Wasi(no) => no.as_raw_errno(),
Self::Io(e) => errno_from_ioerror(e.to_owned()),
Self::Wasi(err) => *err,
Self::Io(err) => {
let err = match err.raw_os_error() {
Some(code) => Self::from_raw_os_error(code),
None => {
log::debug!("Inconvertible OS error: {}", err);
Self::EIO
}
};
err.as_wasi_error()
}
#[cfg(unix)]
Self::Yanix(err) => {
use yanix::YanixError::*;
let err = match err {
Errno(errno) => crate::old::snapshot_0::sys::host_impl::errno_from_nix(*errno),
let err: Self = match err {
Errno(errno) => (*errno).into(),
NulError(err) => err.into(),
TryFromIntError(err) => (*err).into(),
};
err.as_wasi_errno()
err.as_wasi_error()
}
#[cfg(windows)]
Self::Winx(err) => crate::old::snapshot_0::sys::host_impl::errno_from_win(*err),
}
}

pub const ESUCCESS: Self = Error::Wasi(WasiError::ESUCCESS);
pub const E2BIG: Self = Error::Wasi(WasiError::E2BIG);
pub const EACCES: Self = Error::Wasi(WasiError::EACCES);
Expand Down Expand Up @@ -237,12 +242,6 @@ impl Error {
pub const ENOTCAPABLE: Self = Error::Wasi(WasiError::ENOTCAPABLE);
}

fn errno_from_ioerror(e: &std::io::Error) -> wasi::__wasi_errno_t {
match e.raw_os_error() {
Some(code) => crate::old::snapshot_0::sys::errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
wasi::__WASI_ERRNO_IO
}
}
pub(crate) trait FromRawOsError {
fn from_raw_os_error(code: i32) -> Self;
}
2 changes: 1 addition & 1 deletion crates/wasi-common/src/old/snapshot_0/hostcalls/fs.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{hostcalls_impl, wasi, wasi32};
use crate::old::snapshot_0::{wasi, wasi32};

hostcalls_old! {
pub unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t,) -> wasi::__wasi_errno_t;
Expand Down
2 changes: 1 addition & 1 deletion crates/wasi-common/src/old/snapshot_0/hostcalls/misc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::ctx::WasiCtx;
use crate::old::snapshot_0::{hostcalls_impl, wasi, wasi32};
use crate::old::snapshot_0::{wasi, wasi32};
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen_old;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(non_camel_case_types)]
use crate::old::snapshot_0::sys::host_impl;
use crate::old::snapshot_0::sys::hostcalls_impl::fs_helpers::*;
use crate::old::snapshot_0::{fdentry::FdEntry, wasi, Error, Result};
use crate::old::snapshot_0::{error::WasiError, fdentry::FdEntry, wasi, Error, Result};
use std::fs::File;
use std::path::{Component, Path};

Expand Down Expand Up @@ -117,10 +117,10 @@ pub(crate) fn path_get(
dir_stack.push(new_dir);
}
Err(e) => {
match e.as_wasi_errno() {
wasi::__WASI_ERRNO_LOOP
| wasi::__WASI_ERRNO_MLINK
| wasi::__WASI_ERRNO_NOTDIR =>
match e.as_wasi_error() {
WasiError::ELOOP
| WasiError::EMLINK
| WasiError::ENOTDIR =>
// Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag.
{
Expand Down Expand Up @@ -179,12 +179,12 @@ pub(crate) fn path_get(
continue;
}
Err(e) => {
if e.as_wasi_errno() != wasi::__WASI_ERRNO_INVAL
&& e.as_wasi_errno() != wasi::__WASI_ERRNO_NOENT
if e.as_wasi_error() != WasiError::EINVAL
&& e.as_wasi_error() != 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_errno() != wasi::__WASI_ERRNO_NOTDIR
&& e.as_wasi_error() != WasiError::ENOTDIR
{
return Err(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub(crate) fn poll_oneoff(
let event = wasi::__wasi_event_t {
userdata: subscription.userdata,
r#type,
error: err.as_wasi_errno(),
error: err.as_wasi_error().as_raw_errno(),
u: wasi::__wasi_event_u_t {
fd_readwrite: wasi::__wasi_event_fd_readwrite_t {
nbytes: 0,
Expand Down
9 changes: 0 additions & 9 deletions crates/wasi-common/src/old/snapshot_0/sys/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
use crate::old::snapshot_0::wasi;
use cfg_if::cfg_if;

cfg_if! {
if #[cfg(unix)] {
mod unix;
pub(crate) use self::unix::*;

pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t {
host_impl::errno_from_nix(yanix::Errno::from_i32(err)).as_wasi_errno()
}
} else if #[cfg(windows)] {
mod windows;
pub(crate) use self::windows::*;

pub(crate) fn errno_from_host(err: i32) -> wasi::__wasi_errno_t {
host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32))
}
} else {
compile_error!("wasi-common doesn't compile for this platform yet");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::old::snapshot_0::hostcalls_impl::PathGet;
use crate::old::snapshot_0::sys::host_impl;
use crate::old::snapshot_0::{Error, Result};
use std::os::unix::prelude::AsRawFd;

Expand Down Expand Up @@ -80,7 +79,7 @@ pub(crate) fn path_symlink(old_path: &str, resolved: PathGet) -> Result<()> {
Err(Error::ENOTDIR)
}
}
x => Err(host_impl::errno_from_nix(x)),
x => Err(x.into()),
}
} else {
Err(err.into())
Expand Down Expand Up @@ -132,7 +131,7 @@ pub(crate) fn path_rename(resolved_old: PathGet, resolved_new: PathGet) -> Resul
Err(Error::ENOENT)
}
}
x => Err(host_impl::errno_from_nix(x)),
x => Err(x.into()),
}
} else {
Err(err.into())
Expand Down
Loading