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

Add yanix crate and replace nix with yanix in wasi-common #649

Merged
merged 7 commits into from
Dec 9, 2019
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
2 changes: 1 addition & 1 deletion crates/wasi-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ num = { version = "0.2.0", default-features = false }
wig = { path = "wig" }

[target.'cfg(unix)'.dependencies]
nix = "0.15"
yanix = { path = "yanix" }

[target.'cfg(windows)'.dependencies]
winx = { path = "winx" }
Expand Down
33 changes: 19 additions & 14 deletions crates/wasi-common/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::wasi;
use std::convert::Infallible;
use std::num::TryFromIntError;
use std::{fmt, str};
use std::{ffi, fmt, str};
use thiserror::Error;

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
Expand Down Expand Up @@ -107,7 +107,7 @@ pub enum Error {
Wasi(WasiError),
Io(std::io::Error),
#[cfg(unix)]
Nix(nix::Error),
Nix(yanix::YanixError),
#[cfg(windows)]
Win(winx::winerror::WinError),
}
Expand All @@ -119,8 +119,8 @@ impl From<WasiError> for Error {
}

#[cfg(unix)]
impl From<nix::Error> for Error {
fn from(err: nix::Error) -> Self {
impl From<yanix::YanixError> for Error {
fn from(err: yanix::YanixError) -> Self {
Self::Nix(err)
}
}
Expand Down Expand Up @@ -149,6 +149,12 @@ impl From<str::Utf8Error> for Error {
}
}

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

#[cfg(windows)]
impl From<winx::winerror::WinError> for Error {
fn from(err: winx::winerror::WinError) -> Self {
Expand All @@ -162,16 +168,15 @@ impl Error {
Self::Wasi(no) => no.as_raw_errno(),
Self::Io(e) => errno_from_ioerror(e.to_owned()),
#[cfg(unix)]
Self::Nix(err) => err
.as_errno()
.map_or_else(
|| {
log::debug!("Unknown nix errno: {}", err);
Self::ENOSYS
},
crate::sys::host_impl::errno_from_nix,
)
.as_wasi_errno(),
Self::Nix(err) => {
use yanix::YanixError::*;
let err = match err {
Errno(errno) => crate::sys::host_impl::errno_from_nix(*errno),
NulError(err) => err.into(),
TryFromIntError(err) => (*err).into(),
};
err.as_wasi_errno()
}
#[cfg(windows)]
Self::Win(err) => crate::sys::host_impl::errno_from_win(*err),
}
Expand Down
61 changes: 60 additions & 1 deletion crates/wasi-common/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#![allow(non_snake_case)]

use crate::wasi::*;
use std::{io, slice};
use crate::{Error, Result};
use std::{convert::TryInto, io, mem, slice};
use wig::witx_host_types;

witx_host_types!("snapshot" "wasi_snapshot_preview1");
Expand All @@ -20,6 +21,64 @@ pub(crate) unsafe fn iovec_to_host_mut(iovec: &mut __wasi_iovec_t) -> io::IoSlic
io::IoSliceMut::new(slice)
}

#[allow(dead_code)] // trouble with sockets
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub(crate) enum FileType {
Unknown = __WASI_FILETYPE_UNKNOWN,
BlockDevice = __WASI_FILETYPE_BLOCK_DEVICE,
CharacterDevice = __WASI_FILETYPE_CHARACTER_DEVICE,
Directory = __WASI_FILETYPE_DIRECTORY,
RegularFile = __WASI_FILETYPE_REGULAR_FILE,
SocketDgram = __WASI_FILETYPE_SOCKET_DGRAM,
SocketStream = __WASI_FILETYPE_SOCKET_STREAM,
Symlink = __WASI_FILETYPE_SYMBOLIC_LINK,
}

impl FileType {
pub(crate) fn to_wasi(&self) -> __wasi_filetype_t {
*self as __wasi_filetype_t
}
}

#[derive(Debug, Clone)]
pub(crate) struct Dirent {
pub name: String,
pub ftype: FileType,
pub ino: u64,
pub cookie: __wasi_dircookie_t,
}

impl Dirent {
/// Serialize the directory entry to the format define by `__wasi_fd_readdir`,
/// so that the serialized entries can be concatenated by the implementation.
pub fn to_wasi_raw(&self) -> Result<Vec<u8>> {
let name = self.name.as_bytes();
let namlen = name.len();
let dirent_size = mem::size_of::<__wasi_dirent_t>();
let offset = dirent_size.checked_add(namlen).ok_or(Error::EOVERFLOW)?;

let mut raw = Vec::<u8>::with_capacity(offset);
raw.resize(offset, 0);

let sys_dirent = raw.as_mut_ptr() as *mut __wasi_dirent_t;
unsafe {
*sys_dirent = __wasi_dirent_t {
d_namlen: namlen.try_into()?,
d_ino: self.ino,
d_next: self.cookie,
d_type: self.ftype.to_wasi(),
};
}

let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 };
let sys_name = unsafe { slice::from_raw_parts_mut(sys_name, namlen) };
sys_name.copy_from_slice(&name);

Ok(raw)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
62 changes: 0 additions & 62 deletions crates/wasi-common/src/hostcalls_impl/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ use crate::sys::{host_impl, hostcalls_impl};
use crate::{helpers, host, wasi, wasi32, Error, Result};
use filetime::{set_file_handle_times, FileTime};
use log::trace;
use std::convert::TryInto;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::mem;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

pub(crate) unsafe fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasi::__wasi_fd_t) -> Result<()> {
Expand Down Expand Up @@ -1041,63 +1039,3 @@ pub(crate) unsafe fn fd_readdir(

enc_usize_byref(memory, buf_used, host_bufused)
}

#[allow(dead_code)] // trouble with sockets
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub(crate) enum FileType {
Unknown = wasi::__WASI_FILETYPE_UNKNOWN,
BlockDevice = wasi::__WASI_FILETYPE_BLOCK_DEVICE,
CharacterDevice = wasi::__WASI_FILETYPE_CHARACTER_DEVICE,
Directory = wasi::__WASI_FILETYPE_DIRECTORY,
RegularFile = wasi::__WASI_FILETYPE_REGULAR_FILE,
SocketDgram = wasi::__WASI_FILETYPE_SOCKET_DGRAM,
SocketStream = wasi::__WASI_FILETYPE_SOCKET_STREAM,
Symlink = wasi::__WASI_FILETYPE_SYMBOLIC_LINK,
}

impl FileType {
pub(crate) fn to_wasi(&self) -> wasi::__wasi_filetype_t {
*self as wasi::__wasi_filetype_t
}
}

#[derive(Debug, Clone)]
pub(crate) struct Dirent {
pub name: String,
pub ftype: FileType,
pub ino: u64,
pub cookie: wasi::__wasi_dircookie_t,
}

impl Dirent {
/// Serialize the directory entry to the format define by `__wasi_fd_readdir`,
/// so that the serialized entries can be concatenated by the implementation.
pub fn to_wasi_raw(&self) -> Result<Vec<u8>> {
use std::slice;

let name = self.name.as_bytes();
let namlen = name.len();
let dirent_size = mem::size_of::<wasi::__wasi_dirent_t>();
let offset = dirent_size.checked_add(namlen).ok_or(Error::EOVERFLOW)?;

let mut raw = Vec::<u8>::with_capacity(offset);
raw.resize(offset, 0);

let sys_dirent = raw.as_mut_ptr() as *mut wasi::__wasi_dirent_t;
unsafe {
sys_dirent.write_unaligned(wasi::__wasi_dirent_t {
d_namlen: namlen.try_into()?,
d_ino: self.ino,
d_next: self.cookie,
d_type: self.ftype.to_wasi(),
});
}

let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 };
let sys_name = unsafe { slice::from_raw_parts_mut(sys_name, namlen) };
sys_name.copy_from_slice(&name);

Ok(raw)
}
}
33 changes: 19 additions & 14 deletions crates/wasi-common/src/old/snapshot_0/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::old::snapshot_0::wasi;
use std::convert::Infallible;
use std::num::TryFromIntError;
use std::{fmt, str};
use std::{ffi, fmt, str};
use thiserror::Error;

#[derive(Clone, Copy, Debug, Error, Eq, PartialEq)]
Expand Down Expand Up @@ -107,7 +107,7 @@ pub enum Error {
Wasi(WasiError),
Io(std::io::Error),
#[cfg(unix)]
Nix(nix::Error),
Nix(yanix::YanixError),
#[cfg(windows)]
Win(winx::winerror::WinError),
}
Expand All @@ -119,8 +119,8 @@ impl From<WasiError> for Error {
}

#[cfg(unix)]
impl From<nix::Error> for Error {
fn from(err: nix::Error) -> Self {
impl From<yanix::YanixError> for Error {
fn from(err: yanix::YanixError) -> Self {
Self::Nix(err)
}
}
Expand Down Expand Up @@ -149,6 +149,12 @@ impl From<str::Utf8Error> for Error {
}
}

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

#[cfg(windows)]
impl From<winx::winerror::WinError> for Error {
fn from(err: winx::winerror::WinError) -> Self {
Expand All @@ -162,16 +168,15 @@ impl Error {
Self::Wasi(no) => no.as_raw_errno(),
Self::Io(e) => errno_from_ioerror(e.to_owned()),
#[cfg(unix)]
Self::Nix(err) => err
.as_errno()
.map_or_else(
|| {
log::debug!("Unknown nix errno: {}", err);
Self::ENOSYS
},
crate::old::snapshot_0::sys::host_impl::errno_from_nix,
)
.as_wasi_errno(),
Self::Nix(err) => {
use yanix::YanixError::*;
let err = match err {
Errno(errno) => crate::old::snapshot_0::sys::host_impl::errno_from_nix(*errno),
NulError(err) => err.into(),
TryFromIntError(err) => (*err).into(),
};
err.as_wasi_errno()
}
#[cfg(windows)]
Self::Win(err) => crate::old::snapshot_0::sys::host_impl::errno_from_win(*err),
}
Expand Down
61 changes: 60 additions & 1 deletion crates/wasi-common/src/old/snapshot_0/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#![allow(non_snake_case)]

use crate::old::snapshot_0::wasi::*;
use std::{io, slice};
use crate::old::snapshot_0::{Error, Result};
use std::{convert::TryInto, io, mem, slice};
use wig::witx_host_types;

witx_host_types!("old/snapshot_0" "wasi_unstable");
Expand All @@ -20,6 +21,64 @@ pub(crate) unsafe fn iovec_to_host_mut(iovec: &mut __wasi_iovec_t) -> io::IoSlic
io::IoSliceMut::new(slice)
}

#[allow(dead_code)] // trouble with sockets
#[derive(Clone, Copy, Debug)]
#[repr(u8)]
pub(crate) enum FileType {
Unknown = __WASI_FILETYPE_UNKNOWN,
BlockDevice = __WASI_FILETYPE_BLOCK_DEVICE,
CharacterDevice = __WASI_FILETYPE_CHARACTER_DEVICE,
Directory = __WASI_FILETYPE_DIRECTORY,
RegularFile = __WASI_FILETYPE_REGULAR_FILE,
SocketDgram = __WASI_FILETYPE_SOCKET_DGRAM,
SocketStream = __WASI_FILETYPE_SOCKET_STREAM,
Symlink = __WASI_FILETYPE_SYMBOLIC_LINK,
}

impl FileType {
pub(crate) fn to_wasi(&self) -> __wasi_filetype_t {
*self as __wasi_filetype_t
}
}

#[derive(Debug, Clone)]
pub(crate) struct Dirent {
pub name: String,
pub ftype: FileType,
pub ino: u64,
pub cookie: __wasi_dircookie_t,
}

impl Dirent {
/// Serialize the directory entry to the format define by `__wasi_fd_readdir`,
/// so that the serialized entries can be concatenated by the implementation.
pub fn to_wasi_raw(&self) -> Result<Vec<u8>> {
let name = self.name.as_bytes();
let namlen = name.len();
let dirent_size = mem::size_of::<__wasi_dirent_t>();
let offset = dirent_size.checked_add(namlen).ok_or(Error::EOVERFLOW)?;

let mut raw = Vec::<u8>::with_capacity(offset);
raw.resize(offset, 0);

let sys_dirent = raw.as_mut_ptr() as *mut __wasi_dirent_t;
unsafe {
*sys_dirent = __wasi_dirent_t {
d_namlen: namlen.try_into()?,
d_ino: self.ino,
d_next: self.cookie,
d_type: self.ftype.to_wasi(),
};
}

let sys_name = unsafe { sys_dirent.offset(1) as *mut u8 };
let sys_name = unsafe { slice::from_raw_parts_mut(sys_name, namlen) };
sys_name.copy_from_slice(&name);

Ok(raw)
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
Loading