Merge remote-tracking branch 'origin/main' into pch/wasi_error_handling

This commit is contained in:
Pat Hickey
2020-08-25 15:22:51 -07:00
94 changed files with 3333 additions and 2168 deletions

View File

@@ -55,7 +55,7 @@ impl AsFile for dyn Handle + 'static {
} else if let Some(other) = self.as_any().downcast_ref::<OsOther>() {
other.as_file()
} else {
log::error!("tried to make std::fs::File from non-OS handle");
tracing::error!("tried to make std::fs::File from non-OS handle");
Err(io::Error::from_raw_os_error(libc::EBADF))
}
}
@@ -69,17 +69,26 @@ impl TryFrom<File> for Box<dyn Handle> {
match file_type {
types::Filetype::RegularFile => {
let handle = OsFile::try_from(file)?;
log::debug!("Created new instance of OsFile: {:?}", handle);
tracing::debug!(
handle = tracing::field::debug(&handle),
"Created new instance of OsFile"
);
Ok(Box::new(handle))
}
types::Filetype::Directory => {
let handle = OsDir::try_from(file)?;
log::debug!("Created new instance of OsDir: {:?}", handle);
tracing::debug!(
handle = tracing::field::debug(&handle),
"Created new instance of OsDir"
);
Ok(Box::new(handle))
}
_ => {
let handle = OsOther::try_from(file)?;
log::debug!("Created new instance of OsOther: {:?}", handle);
tracing::debug!(
handle = tracing::field::debug(&handle),
"Created new instance of OsOther"
);
Ok(Box::new(handle))
}
}

View File

@@ -3,10 +3,10 @@ use super::{fd, path, AsFile};
use crate::handle::{Handle, HandleRights};
use crate::wasi::types;
use crate::{Error, Result};
use log::{debug, error};
use std::any::Any;
use std::io;
use std::ops::Deref;
use tracing::{debug, error};
// TODO could this be cleaned up?
// The actual `OsDir` struct is OS-dependent, therefore we delegate

View File

@@ -24,7 +24,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
}
}
Err(err) => {
log::debug!("path_unlink_file fstatat error: {:?}", err);
tracing::debug!("path_unlink_file fstatat error: {:?}", err);
}
}
}
@@ -38,8 +38,8 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Result<()> {
use yanix::file::{fstatat, symlinkat, AtFlags};
log::debug!("path_symlink old_path = {:?}", old_path);
log::debug!(
tracing::debug!("path_symlink old_path = {:?}", old_path);
tracing::debug!(
"path_symlink (new_dirfd, new_path) = ({:?}, {:?})",
new_dirfd,
new_path
@@ -58,7 +58,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Resu
{
Ok(_) => return Err(Error::Exist),
Err(err) => {
log::debug!("path_symlink fstatat error: {:?}", err);
tracing::debug!("path_symlink fstatat error: {:?}", err);
}
}
}
@@ -106,7 +106,7 @@ pub(crate) fn rename(
}
}
Err(err) => {
log::debug!("path_rename fstatat error: {:?}", err);
tracing::debug!("path_rename fstatat error: {:?}", err);
}
}
}

View File

@@ -60,10 +60,10 @@ pub(crate) fn readdir<'a>(
// Seek if needed. Unless cookie is wasi::__WASI_DIRCOOKIE_START,
// new items may not be returned to the caller.
if cookie == wasi::DIRCOOKIE_START {
log::trace!(" | fd_readdir: doing rewinddir");
tracing::trace!("fd_readdir: doing rewinddir");
dir.rewind();
} else {
log::trace!(" | fd_readdir: doing seekdir to {}", cookie);
tracing::trace!("fd_readdir: doing seekdir to {}", cookie);
let loc = unsafe { SeekLoc::from_raw(cookie as i64)? };
dir.seek(loc);
}

View File

@@ -11,11 +11,11 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path: &str) -> Result<()> {
use yanix::file::symlinkat;
log::debug!("path_symlink old_path = {:?}", old_path);
log::debug!(
"path_symlink (new_dirfd, new_path) = ({:?}, {:?})",
new_dirfd,
new_path
tracing::debug!(
old_path = old_path,
new_dirfd = tracing::field::debug(new_dirfd),
new_path = new_path,
"path symlink"
);
unsafe { symlinkat(old_path, new_dirfd.as_raw_fd(), new_path)? };

View File

@@ -51,19 +51,22 @@ impl<T: AsRawFd> AsFile for T {
pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
let ft = file.metadata()?.file_type();
let file_type = if ft.is_block_device() {
log::debug!("Host fd {:?} is a block device", file.as_raw_fd());
tracing::debug!(
host_fd = tracing::field::display(file.as_raw_fd()),
"Host fd is a block device"
);
types::Filetype::BlockDevice
} else if ft.is_char_device() {
log::debug!("Host fd {:?} is a char device", file.as_raw_fd());
tracing::debug!("Host fd {:?} is a char device", file.as_raw_fd());
types::Filetype::CharacterDevice
} else if ft.is_dir() {
log::debug!("Host fd {:?} is a directory", file.as_raw_fd());
tracing::debug!("Host fd {:?} is a directory", file.as_raw_fd());
types::Filetype::Directory
} else if ft.is_file() {
log::debug!("Host fd {:?} is a file", file.as_raw_fd());
tracing::debug!("Host fd {:?} is a file", file.as_raw_fd());
types::Filetype::RegularFile
} else if ft.is_socket() {
log::debug!("Host fd {:?} is a socket", file.as_raw_fd());
tracing::debug!("Host fd {:?} is a socket", file.as_raw_fd());
use yanix::socket::{get_socket_type, SockType};
match unsafe { get_socket_type(file.as_raw_fd())? } {
SockType::Datagram => types::Filetype::SocketDgram,
@@ -71,10 +74,10 @@ pub(super) fn get_file_type(file: &File) -> io::Result<types::Filetype> {
_ => return Err(io::Error::from_raw_os_error(libc::EINVAL)),
}
} else if ft.is_fifo() {
log::debug!("Host fd {:?} is a fifo", file.as_raw_fd());
tracing::debug!("Host fd {:?} is a fifo", file.as_raw_fd());
types::Filetype::Unknown
} else {
log::debug!("Host fd {:?} is unknown", file.as_raw_fd());
tracing::debug!("Host fd {:?} is unknown", file.as_raw_fd());
return Err(io::Error::from_raw_os_error(libc::EINVAL));
};
Ok(file_type)

View File

@@ -55,7 +55,7 @@ pub(crate) fn readlinkat(dirfd: &OsDir, path: &str) -> Result<String> {
use std::os::unix::prelude::AsRawFd;
use yanix::file::readlinkat;
log::debug!("path_get readlinkat path = {:?}", path);
tracing::debug!(path = path, "path_get readlinkat");
let path = unsafe { readlinkat(dirfd.as_raw_fd(), path)? };
let path = from_host(path)?;
@@ -124,9 +124,12 @@ pub(crate) fn open(
// umask is, but don't set the executable flag, because it isn't yet
// meaningful for WASI programs to create executable files.
log::debug!("path_open dirfd = {:?}", dirfd);
log::debug!("path_open path = {:?}", path);
log::debug!("path_open oflags = {:?}", nix_all_oflags);
tracing::debug!(
dirfd = tracing::field::debug(dirfd),
path = tracing::field::debug(path),
oflags = tracing::field::debug(nix_all_oflags),
"path_open"
);
let fd_no = unsafe {
openat(
@@ -149,7 +152,10 @@ pub(crate) fn open(
}
}
Err(err) => {
log::debug!("path_open fstatat error: {:?}", err);
tracing::debug!(
error = tracing::field::debug(&err),
"path_open fstatat error",
);
}
}
}
@@ -165,7 +171,10 @@ pub(crate) fn open(
}
}
Err(err) => {
log::debug!("path_open fstatat error: {:?}", err);
tracing::debug!(
error = tracing::field::debug(&err),
"path_open fstatat error",
);
}
}
}
@@ -181,7 +190,7 @@ pub(crate) fn open(
}
};
log::debug!("path_open (host) new_fd = {:?}", new_fd);
tracing::debug!(new_fd = tracing::field::debug(new_fd));
// Determine the type of the new file descriptor and which rights contradict with this type
let file = unsafe { File::from_raw_fd(new_fd) };

View File

@@ -39,7 +39,10 @@ pub(crate) fn oneoff(
let delay = timeout.delay / 1_000_000; // poll syscall requires delay to expressed in milliseconds
delay.try_into().unwrap_or(libc::c_int::max_value())
});
log::debug!("poll_oneoff poll_timeout = {:?}", poll_timeout);
tracing::debug!(
poll_timeout = tracing::field::debug(poll_timeout),
"poll_oneoff"
);
let ready = loop {
match poll(&mut poll_fds, poll_timeout) {
@@ -92,16 +95,17 @@ fn handle_fd_event(
}
for (fd_event, poll_fd) in ready_events {
// log::debug!("poll_oneoff_handle_fd_event fd_event = {:?}", fd_event);
log::debug!("poll_oneoff_handle_fd_event poll_fd = {:?}", poll_fd);
tracing::debug!(
poll_fd = tracing::field::debug(poll_fd),
poll_event = tracing::field::debug(&fd_event),
"poll_oneoff handle_fd_event"
);
let revents = match poll_fd.revents() {
Some(revents) => revents,
None => continue,
};
log::debug!("poll_oneoff_handle_fd_event revents = {:?}", revents);
let nbytes = if fd_event.r#type == types::Eventtype::FdRead {
query_nbytes(fd_event.handle)?
} else {

View File

@@ -6,12 +6,12 @@ use crate::sys::osfile::OsFile;
use crate::sys::AsFile;
use crate::wasi::types;
use crate::Result;
use log::trace;
use std::convert::TryInto;
use std::fs::{File, OpenOptions};
use std::os::windows::fs::OpenOptionsExt;
use std::os::windows::prelude::{AsRawHandle, FromRawHandle};
use std::path::Path;
use tracing::trace;
use winx::file::{AccessMode, FileModeInformation, Flags};
pub(crate) fn fdstat_get(file: &File) -> Result<types::Fdflags> {

View File

@@ -47,7 +47,7 @@ fn concatenate<P: AsRef<Path>>(file: &OsDir, path: P) -> Result<PathBuf> {
// components with `out_path`
let out_path = PathBuf::from(strip_extended_prefix(out_path));
log::debug!("out_path={:?}", out_path);
tracing::debug!(out_path = tracing::field::debug(&out_path));
Ok(out_path)
}
@@ -139,7 +139,7 @@ pub(crate) fn readlinkat(dirfd: &OsDir, s_path: &str) -> Result<String> {
Err(e) => e,
};
if let Some(code) = err.raw_os_error() {
log::debug!("readlinkat error={:?}", code);
tracing::debug!("readlinkat error={:?}", code);
if code as u32 == winerror::ERROR_INVALID_NAME {
if s_path.ends_with('/') {
// strip "/" and check if exists
@@ -171,7 +171,10 @@ pub(crate) fn link(
let new_path = concatenate(new_dirfd, new_path)?;
if follow_symlinks {
// in particular, this will return an error if the target path doesn't exist
log::debug!("Following symlinks for path: {:?}", old_path);
tracing::debug!(
old_path = tracing::field::display(old_path.display()),
"Following symlinks"
);
old_path = fs::canonicalize(&old_path).map_err(|e| match e.raw_os_error() {
// fs::canonicalize under Windows will return:
// * ERROR_FILE_NOT_FOUND, if it encounters a dangling symlink
@@ -185,7 +188,7 @@ pub(crate) fn link(
Err(e) => e,
};
if let Some(code) = err.raw_os_error() {
log::debug!("path_link at fs::hard_link error code={:?}", code);
tracing::debug!("path_link at fs::hard_link error code={:?}", code);
if code as u32 == winerror::ERROR_ACCESS_DENIED {
// If an attempt is made to create a hard link to a directory, POSIX-compliant
// implementations of link return `EPERM`, but `ERROR_ACCESS_DENIED` is converted
@@ -249,7 +252,7 @@ pub(crate) fn open(
}
Err(err) => match err.raw_os_error() {
Some(code) => {
log::debug!("path_open at symlink_metadata error code={:?}", code);
tracing::debug!("path_open at symlink_metadata error code={:?}", code);
match code as u32 {
winerror::ERROR_FILE_NOT_FOUND => {
// file not found, let it proceed to actually
@@ -264,7 +267,7 @@ pub(crate) fn open(
};
}
None => {
log::debug!("Inconvertible OS error: {}", err);
tracing::debug!("Inconvertible OS error: {}", err);
return Err(Error::Io);
}
},
@@ -354,7 +357,7 @@ pub(crate) fn rename(
};
match err.raw_os_error() {
Some(code) => {
log::debug!("path_rename at rename error code={:?}", code);
tracing::debug!("path_rename at rename error code={:?}", code);
match code as u32 {
winerror::ERROR_ACCESS_DENIED => {
// So most likely dealing with new_path == dir.
@@ -386,7 +389,7 @@ pub(crate) fn rename(
Err(err.into())
}
None => {
log::debug!("Inconvertible OS error: {}", err);
tracing::debug!("Inconvertible OS error: {}", err);
Err(Error::Io)
}
}
@@ -418,7 +421,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path_: &str) -> Res
};
match err.raw_os_error() {
Some(code) => {
log::debug!("path_symlink at symlink_file error code={:?}", code);
tracing::debug!("path_symlink at symlink_file error code={:?}", code);
match code as u32 {
// If the target contains a trailing slash, the Windows API returns
// ERROR_INVALID_NAME (which corresponds to ENOENT) instead of
@@ -443,7 +446,7 @@ pub(crate) fn symlink(old_path: &str, new_dirfd: &OsDir, new_path_: &str) -> Res
Err(err.into())
}
None => {
log::debug!("Inconvertible OS error: {}", err);
tracing::debug!("Inconvertible OS error: {}", err);
Err(Error::Io)
}
}
@@ -469,7 +472,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
};
match err.raw_os_error() {
Some(code) => {
log::debug!("path_unlink_file at symlink_file error code={:?}", code);
tracing::debug!("path_unlink_file at symlink_file error code={:?}", code);
if code as u32 == winerror::ERROR_ACCESS_DENIED {
// try unlinking a dir symlink instead
return fs::remove_dir(path).map_err(Into::into);
@@ -478,7 +481,7 @@ pub(crate) fn unlink_file(dirfd: &OsDir, path: &str) -> Result<()> {
Err(err.into())
}
None => {
log::debug!("Inconvertible OS error: {}", err);
tracing::debug!("Inconvertible OS error: {}", err);
Err(Error::Io)
}
}

View File

@@ -8,12 +8,12 @@ use crate::sys::AsFile;
use crate::wasi::types;
use crate::{Error, Result};
use lazy_static::lazy_static;
use log::{debug, error, trace, warn};
use std::convert::TryInto;
use std::sync::mpsc::{self, Receiver, RecvTimeoutError, Sender, TryRecvError};
use std::sync::Mutex;
use std::thread;
use std::time::Duration;
use tracing::{debug, error, trace, warn};
struct StdinPoll {
request_tx: Sender<()>,
@@ -245,7 +245,7 @@ pub(crate) fn oneoff(
handle_error_event(event, types::Errno::Notsup, events);
}
} else {
log::error!("can poll FdEvent for OS resources only");
tracing::error!("can poll FdEvent for OS resources only");
return Err(Error::Badf);
}
}