diff --git a/Cargo.toml b/Cargo.toml
index 3c756d3..284f654 100644
@@ -18,6 +18,7 @@ rand = "0.6"
cfg-if = "0.1.9"
log = "0.4"
filetime = "0.2.7"
+wasi = "0.7.0"
[target.'cfg(unix)'.dependencies]
nix = "0.15"
@@ -38,8 +39,8 @@ cranelift-native = "0.40.0"
target-lexicon = "0.4.0"
pretty_env_logger = "0.3.0"
-[patch."https://github.com/CraneStation/wasi-common"]
-wasi-common = { path = "." }
+#[patch."https://github.com/CraneStation/wasi-common"]
+#wasi-common = { path = "." }
[build-dependencies]
cfg-if = "0.1.9"
diff --git a/src/ctx.rs b/src/ctx.rs
index 993f6dd..f8f1651 100644
@@ -6,6 +6,7 @@ use std::collections::HashMap;
use std::ffi::CString;
use std::fs::File;
use std::path::{Path, PathBuf};
+use wasi::wasi_unstable;
pub struct WasiCtxBuilder {
fds: HashMap<host::__wasi_fd_t, FdEntry>,
@@ -33,7 +34,7 @@ impl WasiCtxBuilder {
pub fn args<S: AsRef<str>>(mut self, args: impl Iterator<Item = S>) -> Result<Self> {
let args: Result<Vec<CString>> = args
- .map(|arg| CString::new(arg.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE))
+ .map(|arg| CString::new(arg.as_ref()).map_err(|_| wasi_unstable::ENOTCAPABLE))
.collect();
self.args = args?;
Ok(self)
@@ -41,7 +42,7 @@ impl WasiCtxBuilder {
pub fn arg(mut self, arg: &str) -> Result<Self> {
self.args
- .push(CString::new(arg).map_err(|_| host::__WASI_ENOTCAPABLE)?);
+ .push(CString::new(arg).map_err(|_| wasi_unstable::ENOTCAPABLE)?);
Ok(self)
}
@@ -58,8 +59,8 @@ impl WasiCtxBuilder {
pub fn env<S: AsRef<str>>(mut self, k: S, v: S) -> Result<Self> {
self.env.insert(
- CString::new(k.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE)?,
- CString::new(v.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE)?,
+ CString::new(k.as_ref()).map_err(|_| wasi_unstable::ENOTCAPABLE)?,
+ CString::new(v.as_ref()).map_err(|_| wasi_unstable::ENOTCAPABLE)?,
);
Ok(self)
}
@@ -71,11 +72,11 @@ impl WasiCtxBuilder {
let env: Result<HashMap<CString, CString>> = envs
.map(|t| {
let (k, v) = t.borrow();
- let k = CString::new(k.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE);
- let v = CString::new(v.as_ref()).map_err(|_| host::__WASI_ENOTCAPABLE);
+ let k = CString::new(k.as_ref()).map_err(|_| wasi_unstable::ENOTCAPABLE);
+ let v = CString::new(v.as_ref()).map_err(|_| wasi_unstable::ENOTCAPABLE);
match (k, v) {
(Ok(k), Ok(v)) => Ok((k, v)),
- _ => Err(host::__WASI_ENOTCAPABLE),
+ _ => Err(wasi_unstable::ENOTCAPABLE),
}
})
.collect();
@@ -93,11 +94,11 @@ impl WasiCtxBuilder {
let mut preopen_fd = 3;
for (guest_path, dir) in self.preopens {
if !dir.metadata().map_err(errno_from_ioerror)?.is_dir() {
- return Err(host::__WASI_EBADF);
+ return Err(wasi_unstable::EBADF);
}
while self.fds.contains_key(&preopen_fd) {
- preopen_fd = preopen_fd.checked_add(1).ok_or(host::__WASI_ENFILE)?;
+ preopen_fd = preopen_fd.checked_add(1).ok_or(wasi_unstable::ENFILE)?;
}
let mut fe = FdEntry::from(dir)?;
fe.preopen_path = Some(guest_path);
@@ -137,7 +138,7 @@ impl WasiCtx {
///
/// - File descriptors 0, 1, and 2 inherit stdin, stdout, and stderr from the host process.
///
- /// - Environment variables are inherited from the host process.
+ /// - Environment variables are inherited from the host process. fixme
///
/// To override these behaviors, use `WasiCtxBuilder`.
pub fn new<S: AsRef<str>>(args: impl Iterator<Item = S>) -> Result<Self> {
@@ -161,7 +162,7 @@ impl WasiCtx {
if let Some(fe) = self.fds.get(&fd) {
Self::validate_rights(fe, rights_base, rights_inheriting).and(Ok(fe))
} else {
- Err(host::__WASI_EBADF)
+ Err(wasi_unstable::EBADF)
}
}
@@ -174,7 +175,7 @@ impl WasiCtx {
if let Some(fe) = self.fds.get_mut(&fd) {
Self::validate_rights(fe, rights_base, rights_inheriting).and(Ok(fe))
} else {
- Err(host::__WASI_EBADF)
+ Err(wasi_unstable::EBADF)
}
}
@@ -184,7 +185,7 @@ impl WasiCtx {
rights_inheriting: host::__wasi_rights_t,
) -> Result<()> {
if !fe.rights_base & rights_base != 0 || !fe.rights_inheriting & rights_inheriting != 0 {
- Err(host::__WASI_ENOTCAPABLE)
+ Err(wasi_unstable::ENOTCAPABLE)
} else {
Ok(())
}
@@ -197,7 +198,7 @@ impl WasiCtx {
if let Some(next_fd) = fd.checked_add(1) {
fd = next_fd;
} else {
- return Err(host::__WASI_EMFILE);
+ return Err(wasi_unstable::EMFILE);
}
}
self.fds.insert(fd, fe);
diff --git a/src/fdentry.rs b/src/fdentry.rs
index 6ca142c..fa7fca9 100644
@@ -1,5 +1,6 @@
use crate::sys::{errno_from_ioerror, fdentry_impl};
use crate::{host, Result};
+use wasi::wasi_unstable;
use std::mem::ManuallyDrop;
use std::path::PathBuf;
@@ -17,7 +18,7 @@ impl Descriptor {
pub fn as_file(&self) -> Result<&fs::File> {
match self {
Descriptor::File(f) => Ok(f),
- _ => Err(host::__WASI_EBADF),
+ _ => Err(wasi_unstable::EBADF),
}
}
diff --git a/src/helpers.rs b/src/helpers.rs
index 08755b5..f73fec9 100644
@@ -1,10 +1,11 @@
-use crate::{host, Result};
+use crate::Result;
use std::convert::TryInto;
use std::time::{SystemTime, UNIX_EPOCH};
+use wasi::wasi_unstable;
pub(crate) fn systemtime_to_timestamp(st: SystemTime) -> Result<u64> {
st.duration_since(UNIX_EPOCH)
- .map_err(|_| host::__WASI_EINVAL)? // date earlier than UNIX_EPOCH
+ .map_err(|_| wasi_unstable::EINVAL)? // date earlier than UNIX_EPOCH
.as_nanos()
.try_into()
- .map_err(|_| host::__WASI_EOVERFLOW) // u128 doesn't fit into u64
+ .map_err(|_| wasi_unstable::EOVERFLOW) // u128 doesn't fit into u64
}
diff --git a/src/hostcalls/fs.rs b/src/hostcalls/fs.rs
index 9163c8e..004fbe9 100644
@@ -1,11 +1,14 @@
#![allow(non_camel_case_types)]
use crate::ctx::WasiCtx;
use crate::wasm32;
+use crate::Result;
+use core::num::NonZeroU16;
+use log::trace;
hostcalls! {
- pub fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t,) -> wasm32::__wasi_errno_t;
+ pub fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t,) -> Result<()>;
- pub fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t,) -> wasm32::__wasi_errno_t;
+ pub fn fd_datasync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t,) -> Result<()>;
pub fn fd_pread(
wasi_ctx: &WasiCtx,
@@ -15,7 +18,7 @@ hostcalls! {
iovs_len: wasm32::size_t,
offset: wasm32::__wasi_filesize_t,
nread: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_pwrite(
wasi_ctx: &WasiCtx,
@@ -25,7 +28,7 @@ hostcalls! {
iovs_len: wasm32::size_t,
offset: wasm32::__wasi_filesize_t,
nwritten: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_read(
wasi_ctx: &mut WasiCtx,
@@ -34,13 +37,13 @@ hostcalls! {
iovs_ptr: wasm32::uintptr_t,
iovs_len: wasm32::size_t,
nread: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_renumber(
wasi_ctx: &mut WasiCtx,
from: wasm32::__wasi_fd_t,
to: wasm32::__wasi_fd_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_seek(
wasi_ctx: &WasiCtx,
@@ -49,36 +52,36 @@ hostcalls! {
offset: wasm32::__wasi_filedelta_t,
whence: wasm32::__wasi_whence_t,
newoffset: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_tell(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasm32::__wasi_fd_t,
newoffset: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_fdstat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasm32::__wasi_fd_t,
fdstat_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_fdstat_set_flags(
wasi_ctx: &WasiCtx,
fd: wasm32::__wasi_fd_t,
fdflags: wasm32::__wasi_fdflags_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_fdstat_set_rights(
wasi_ctx: &mut WasiCtx,
fd: wasm32::__wasi_fd_t,
fs_rights_base: wasm32::__wasi_rights_t,
fs_rights_inheriting: wasm32::__wasi_rights_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
- pub fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t,) -> wasm32::__wasi_errno_t;
+ pub fn fd_sync(wasi_ctx: &WasiCtx, fd: wasm32::__wasi_fd_t,) -> Result<()>;
pub fn fd_write(
wasi_ctx: &mut WasiCtx,
@@ -87,7 +90,7 @@ hostcalls! {
iovs_ptr: wasm32::uintptr_t,
iovs_len: wasm32::size_t,
nwritten: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_advise(
wasi_ctx: &WasiCtx,
@@ -95,14 +98,14 @@ hostcalls! {
offset: wasm32::__wasi_filesize_t,
len: wasm32::__wasi_filesize_t,
advice: wasm32::__wasi_advice_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_allocate(
wasi_ctx: &WasiCtx,
fd: wasm32::__wasi_fd_t,
offset: wasm32::__wasi_filesize_t,
len: wasm32::__wasi_filesize_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_create_directory(
wasi_ctx: &WasiCtx,
@@ -110,7 +113,7 @@ hostcalls! {
dirfd: wasm32::__wasi_fd_t,
path_ptr: wasm32::uintptr_t,
path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_link(
wasi_ctx: &WasiCtx,
@@ -122,7 +125,7 @@ hostcalls! {
new_dirfd: wasm32::__wasi_fd_t,
new_path_ptr: wasm32::uintptr_t,
new_path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_open(
wasi_ctx: &mut WasiCtx,
@@ -136,7 +139,7 @@ hostcalls! {
fs_rights_inheriting: wasm32::__wasi_rights_t,
fs_flags: wasm32::__wasi_fdflags_t,
fd_out_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_readdir(
wasi_ctx: &WasiCtx,
@@ -146,7 +149,7 @@ hostcalls! {
buf_len: wasm32::size_t,
cookie: wasm32::__wasi_dircookie_t,
buf_used: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_readlink(
wasi_ctx: &WasiCtx,
@@ -157,7 +160,7 @@ hostcalls! {
buf_ptr: wasm32::uintptr_t,
buf_len: wasm32::size_t,
buf_used: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_rename(
wasi_ctx: &WasiCtx,
@@ -168,14 +171,14 @@ hostcalls! {
new_dirfd: wasm32::__wasi_fd_t,
new_path_ptr: wasm32::uintptr_t,
new_path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_filestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasm32::__wasi_fd_t,
filestat_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_filestat_set_times(
wasi_ctx: &WasiCtx,
@@ -183,13 +186,13 @@ hostcalls! {
st_atim: wasm32::__wasi_timestamp_t,
st_mtim: wasm32::__wasi_timestamp_t,
fst_flags: wasm32::__wasi_fstflags_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_filestat_set_size(
wasi_ctx: &WasiCtx,
fd: wasm32::__wasi_fd_t,
st_size: wasm32::__wasi_filesize_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_filestat_get(
wasi_ctx: &WasiCtx,
@@ -199,7 +202,7 @@ hostcalls! {
path_ptr: wasm32::uintptr_t,
path_len: wasm32::size_t,
filestat_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_filestat_set_times(
wasi_ctx: &WasiCtx,
@@ -211,7 +214,7 @@ hostcalls! {
st_atim: wasm32::__wasi_timestamp_t,
st_mtim: wasm32::__wasi_timestamp_t,
fst_flags: wasm32::__wasi_fstflags_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_symlink(
wasi_ctx: &WasiCtx,
@@ -221,7 +224,7 @@ hostcalls! {
dirfd: wasm32::__wasi_fd_t,
new_path_ptr: wasm32::uintptr_t,
new_path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_unlink_file(
wasi_ctx: &WasiCtx,
@@ -229,7 +232,7 @@ hostcalls! {
dirfd: wasm32::__wasi_fd_t,
path_ptr: wasm32::uintptr_t,
path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn path_remove_directory(
wasi_ctx: &WasiCtx,
@@ -237,14 +240,14 @@ hostcalls! {
dirfd: wasm32::__wasi_fd_t,
path_ptr: wasm32::uintptr_t,
path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_prestat_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
fd: wasm32::__wasi_fd_t,
prestat_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn fd_prestat_dir_name(
wasi_ctx: &WasiCtx,
@@ -252,5 +255,5 @@ hostcalls! {
fd: wasm32::__wasi_fd_t,
path_ptr: wasm32::uintptr_t,
path_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
}
diff --git a/src/hostcalls/misc.rs b/src/hostcalls/misc.rs
index fc41fff..e777dbd 100644
@@ -2,6 +2,7 @@
use crate::ctx::WasiCtx;
use crate::memory::*;
use crate::wasm32;
+use crate::Result;
use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen;
@@ -19,7 +20,7 @@ pub fn proc_raise(
_wasi_ctx: &WasiCtx,
_memory: &mut [u8],
_sig: wasm32::__wasi_signal_t,
-) -> wasm32::__wasi_errno_t {
+) -> Result<()> {
unimplemented!("proc_raise")
}
@@ -29,47 +30,47 @@ hostcalls! {
memory: &mut [u8],
argv_ptr: wasm32::uintptr_t,
argv_buf: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn args_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
argc_ptr: wasm32::uintptr_t,
argv_buf_size_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn environ_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_ptr: wasm32::uintptr_t,
environ_buf: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn environ_sizes_get(
wasi_ctx: &WasiCtx,
memory: &mut [u8],
environ_count_ptr: wasm32::uintptr_t,
environ_size_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn random_get(
memory: &mut [u8],
buf_ptr: wasm32::uintptr_t,
buf_len: wasm32::size_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn clock_res_get(
memory: &mut [u8],
clock_id: wasm32::__wasi_clockid_t,
resolution_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn clock_time_get(
memory: &mut [u8],
clock_id: wasm32::__wasi_clockid_t,
precision: wasm32::__wasi_timestamp_t,
time_ptr: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
pub fn poll_oneoff(
memory: &mut [u8],
@@ -77,7 +78,7 @@ hostcalls! {
output: wasm32::uintptr_t,
nsubscriptions: wasm32::size_t,
nevents: wasm32::uintptr_t,
- ) -> wasm32::__wasi_errno_t;
+ ) -> Result<()>;
- pub fn sched_yield() -> wasm32::__wasi_errno_t;
+ pub fn sched_yield() -> Result<()>;
}
diff --git a/src/hostcalls/sock.rs b/src/hostcalls/sock.rs
index ed8fb7c..028a057 100644
@@ -3,6 +3,8 @@
#![allow(unused)]
use crate::ctx::WasiCtx;
use crate::wasm32;
+use core::num::NonZeroU16;
+use log::trace;
use wasi_common_cbindgen::wasi_common_cbindgen;
#[wasi_common_cbindgen]
@@ -15,7 +17,7 @@ pub fn sock_recv(
ri_flags: wasm32::__wasi_riflags_t,
ro_datalen: wasm32::uintptr_t,
ro_flags: wasm32::uintptr_t,
-) -> wasm32::__wasi_errno_t {
+) -> Result<(), NonZeroU16> {
unimplemented!("sock_recv")
}
@@ -28,7 +30,7 @@ pub fn sock_send(
si_data_len: wasm32::size_t,
si_flags: wasm32::__wasi_siflags_t,
so_datalen: wasm32::uintptr_t,
-) -> wasm32::__wasi_errno_t {
+) -> Result<(), NonZeroU16> {
unimplemented!("sock_send")
}
@@ -38,6 +40,6 @@ pub fn sock_shutdown(
memory: &mut [u8],
sock: wasm32::__wasi_fd_t,
how: wasm32::__wasi_sdflags_t,
-) -> wasm32::__wasi_errno_t {
+) -> Result<(), NonZeroU16> {
unimplemented!("sock_shutdown")
}
diff --git a/src/hostcalls_impl/fs.rs b/src/hostcalls_impl/fs.rs
index fb69ffb..274291a 100644
@@ -12,6 +12,7 @@ use log::trace;
use std::fs::File;
use std::io::{self, Read, Seek, SeekFrom, Write};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
+use wasi::wasi_unstable;
pub(crate) fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> Result<()> {
trace!("fd_close(fd={:?})", fd);
@@ -20,11 +21,11 @@ pub(crate) fn fd_close(wasi_ctx: &mut WasiCtx, fd: wasm32::__wasi_fd_t) -> Resul
if let Some(fdent) = wasi_ctx.fds.get(&fd) {
// can't close preopened files
if fdent.preopen_path.is_some() {
- return Err(host::__WASI_ENOTSUP);
+ return Err(wasi_unstable::ENOTSUP);
}
}
- let mut fe = wasi_ctx.fds.remove(&fd).ok_or(host::__WASI_EBADF)?;
+ let mut fe = wasi_ctx.fds.remove(&fd).ok_or(wasi_unstable::EBADF)?;
fe.fd_object.needs_close = true;
Ok(())
@@ -68,7 +69,7 @@ pub(crate) fn fd_pread(
let offset = dec_filesize(offset);
if offset > i64::max_value() as u64 {
- return Err(host::__WASI_EIO);
+ return Err(wasi_unstable::EIO);
}
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
let mut buf = vec![0; buf_size];
@@ -117,7 +118,7 @@ pub(crate) fn fd_pwrite(
let offset = dec_filesize(offset);
if offset > i64::max_value() as u64 {
- return Err(host::__WASI_EIO);
+ return Err(wasi_unstable::EIO);
}
let buf_size = iovs.iter().map(|v| v.buf_len).sum();
let mut buf = Vec::with_capacity(buf_size);
@@ -160,7 +161,7 @@ pub(crate) fn fd_read(
let maybe_host_nread = match &mut *fe.fd_object.descriptor {
Descriptor::File(f) => f.read_vectored(&mut iovs),
Descriptor::Stdin => io::stdin().lock().read_vectored(&mut iovs),
- _ => return Err(host::__WASI_EBADF),
+ _ => return Err(wasi_unstable::EBADF),
};
let host_nread = maybe_host_nread.map_err(errno_from_ioerror)?;
@@ -181,14 +182,14 @@ pub(crate) fn fd_renumber(
let to = dec_fd(to);
if !wasi_ctx.contains_fd_entry(from) || !wasi_ctx.contains_fd_entry(to) {
- return Err(host::__WASI_EBADF);
+ return Err(wasi_unstable::EBADF);
}
// Don't allow renumbering over a pre-opened resource.
// TODO: Eventually, we do want to permit this, once libpreopen in
// userspace is capable of removing entries from its tables as well.
if wasi_ctx.fds[&from].preopen_path.is_some() || wasi_ctx.fds[&to].preopen_path.is_some() {
- return Err(host::__WASI_ENOTSUP);
+ return Err(wasi_unstable::ENOTSUP);
}
// check if stdio fds
@@ -196,7 +197,7 @@ pub(crate) fn fd_renumber(
if !wasi_ctx.fds[&from].fd_object.descriptor.is_file()
|| !wasi_ctx.fds[&to].fd_object.descriptor.is_file()
{
- return Err(host::__WASI_EBADF);
+ return Err(wasi_unstable::EBADF);
}
let fe_from_dup = wasi_ctx.fds[&from]
@@ -244,7 +245,7 @@ pub(crate) fn fd_seek(
host::__WASI_WHENCE_CUR => SeekFrom::Current(offset),
host::__WASI_WHENCE_END => SeekFrom::End(offset),
host::__WASI_WHENCE_SET => SeekFrom::Start(offset as u64),
- _ => return Err(host::__WASI_EINVAL),
+ _ => return Err(wasi_unstable::EINVAL),
};
let host_newoffset = fd.seek(pos).map_err(errno_from_ioerror)?;
@@ -328,12 +329,12 @@ pub(crate) fn fd_fdstat_set_rights(
);
let fd = dec_fd(fd);
- let fe = wasi_ctx.fds.get_mut(&fd).ok_or(host::__WASI_EBADF)?;
+ let fe = wasi_ctx.fds.get_mut(&fd).ok_or(wasi_unstable::EBADF)?;
if fe.rights_base & fs_rights_base != fs_rights_base
|| fe.rights_inheriting & fs_rights_inheriting != fs_rights_inheriting
{
- return Err(host::__WASI_ENOTCAPABLE);
+ return Err(wasi_unstable::ENOTCAPABLE);
}
fe.rights_base = fs_rights_base;
fe.rights_inheriting = fs_rights_inheriting;
@@ -378,7 +379,7 @@ pub(crate) fn fd_write(
// perform unbuffered writes
let host_nwritten = match &mut *fe.fd_object.descriptor {
Descriptor::File(f) => f.write_vectored(&iovs).map_err(errno_from_ioerror)?,
- Descriptor::Stdin => return Err(host::__WASI_EBADF),
+ Descriptor::Stdin => return Err(wasi_unstable::EBADF),
Descriptor::Stdout => {
// lock for the duration of the scope
let stdout = io::stdout();
@@ -442,10 +443,10 @@ pub(crate) fn fd_allocate(
let metadata = fd.metadata().map_err(errno_from_ioerror)?;
let current_size = metadata.len();
- let wanted_size = offset.checked_add(len).ok_or(host::__WASI_E2BIG)?;
+ let wanted_size = offset.checked_add(len).ok_or(wasi_unstable::E2BIG)?;
// This check will be unnecessary when rust-lang/rust#63326 is fixed
if wanted_size > i64::max_value() as u64 {
- return Err(host::__WASI_E2BIG);
+ return Err(wasi_unstable::E2BIG);
}
if wanted_size > current_size {
@@ -782,7 +783,7 @@ pub(crate) fn fd_filestat_set_times_impl(
let set_mtim_now = fst_flags & host::__WASI_FILESTAT_SET_MTIM_NOW != 0;
if (set_atim && set_atim_now) || (set_mtim && set_mtim_now) {
- return Err(host::__WASI_EINVAL);
+ return Err(wasi_unstable::EINVAL);
}
let atim = if set_atim {
let time = UNIX_EPOCH + Duration::from_nanos(st_atim);
@@ -821,7 +822,7 @@ pub(crate) fn fd_filestat_set_size(
let st_size = dec_filesize(st_size);
// This check will be unnecessary when rust-lang/rust#63326 is fixed
if st_size > i64::max_value() as u64 {
- return Err(host::__WASI_E2BIG);
+ return Err(wasi_unstable::E2BIG);
}
fd.set_len(st_size).map_err(errno_from_ioerror)
}
@@ -1006,9 +1007,9 @@ pub(crate) fn fd_prestat_get(
wasi_ctx
.get_fd_entry(fd, host::__WASI_RIGHT_PATH_OPEN, 0)
.and_then(|fe| {
- let po_path = fe.preopen_path.as_ref().ok_or(host::__WASI_ENOTSUP)?;
+ let po_path = fe.preopen_path.as_ref().ok_or(wasi_unstable::ENOTSUP)?;
if fe.fd_object.file_type != host::__WASI_FILETYPE_DIRECTORY {
- return Err(host::__WASI_ENOTDIR);
+ return Err(wasi_unstable::ENOTDIR);
}
let path = host_impl::path_from_host(po_path.as_os_str())?;
@@ -1047,15 +1048,15 @@ pub(crate) fn fd_prestat_dir_name(
wasi_ctx
.get_fd_entry(fd, host::__WASI_RIGHT_PATH_OPEN, 0)
.and_then(|fe| {
- let po_path = fe.preopen_path.as_ref().ok_or(host::__WASI_ENOTSUP)?;
+ let po_path = fe.preopen_path.as_ref().ok_or(wasi_unstable::ENOTSUP)?;
if fe.fd_object.file_type != host::__WASI_FILETYPE_DIRECTORY {
- return Err(host::__WASI_ENOTDIR);
+ return Err(wasi_unstable::ENOTDIR);
}
let path = host_impl::path_from_host(po_path.as_os_str())?;
if path.len() > dec_usize(path_len) {
- return Err(host::__WASI_ENAMETOOLONG);
+ return Err(wasi_unstable::ENAMETOOLONG);
}
trace!(" | (path_ptr,path_len)='{}'", path);
diff --git a/src/hostcalls_impl/fs_helpers.rs b/src/hostcalls_impl/fs_helpers.rs
index ec433ae..ec5378b 100644
@@ -4,6 +4,7 @@ use crate::sys::{errno_from_host, host_impl};
use crate::{host, Result};
use std::fs::File;
use std::path::{Component, Path};
+use wasi::wasi_unstable;
pub(crate) struct PathGet {
dirfd: File,
@@ -33,12 +34,12 @@ pub(crate) fn path_get(
if path.contains('\0') {
// if contains NUL, return EILSEQ
- return Err(host::__WASI_EILSEQ);
+ return Err(wasi_unstable::EILSEQ);
}
let dirfd = dirfd.try_clone().map_err(|err| {
err.raw_os_error()
- .map_or(host::__WASI_EBADF, errno_from_host)
+ .map_or(wasi_unstable::EBADF, errno_from_host)
})?;
// Stack of directory file descriptors. Index 0 always corresponds with the directory provided
@@ -64,7 +65,7 @@ pub(crate) fn path_get(
let ends_with_slash = cur_path.ends_with('/');
let mut components = Path::new(&cur_path).components();
let head = match components.next() {
- None => return Err(host::__WASI_ENOENT),
+ None => return Err(wasi_unstable::ENOENT),
Some(p) => p,
};
let tail = components.as_path();
@@ -80,18 +81,18 @@ pub(crate) fn path_get(
match head {
Component::Prefix(_) | Component::RootDir => {
// path is absolute!
- return Err(host::__WASI_ENOTCAPABLE);
+ return Err(wasi_unstable::ENOTCAPABLE);
}
Component::CurDir => {
// "." so skip
}
Component::ParentDir => {
// ".." so pop a dir
- let _ = dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?;
+ let _ = dir_stack.pop().ok_or(wasi_unstable::ENOTCAPABLE)?;
// we're not allowed to pop past the original directory
if dir_stack.is_empty() {
- return Err(host::__WASI_ENOTCAPABLE);
+ return Err(wasi_unstable::ENOTCAPABLE);
}
}
Component::Normal(head) => {
@@ -102,26 +103,27 @@ pub(crate) fn path_get(
}
if !path_stack.is_empty() || (ends_with_slash && !needs_final_component) {
- match openat(dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?, &head) {
+ match openat(dir_stack.last().ok_or(wasi_unstable::ENOTCAPABLE)?, &head)
+ {
Ok(new_dir) => {
dir_stack.push(new_dir);
}
Err(e)
- if e == host::__WASI_ELOOP
- || e == host::__WASI_EMLINK
- || e == host::__WASI_ENOTDIR =>
+ if e == wasi_unstable::ELOOP
+ || e == wasi_unstable::EMLINK
+ || e == wasi_unstable::ENOTDIR =>
// Check to see if it was a symlink. Linux indicates
// this with ENOTDIR because of the O_DIRECTORY flag.
{
// attempt symlink expansion
let mut link_path = readlinkat(
- dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
+ dir_stack.last().ok_or(wasi_unstable::ENOTCAPABLE)?,
&head,
)?;
symlink_expansions += 1;
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
- return Err(host::__WASI_ELOOP);
+ return Err(wasi_unstable::ELOOP);
}
if head.ends_with('/') {
@@ -147,13 +149,13 @@ pub(crate) fn path_get(
// if there's a trailing slash, or if `LOOKUP_SYMLINK_FOLLOW` is set, attempt
// symlink expansion
match readlinkat(
- dir_stack.last().ok_or(host::__WASI_ENOTCAPABLE)?,
+ dir_stack.last().ok_or(wasi_unstable::ENOTCAPABLE)?,
&head,
) {
Ok(mut link_path) => {
symlink_expansions += 1;
if symlink_expansions > MAX_SYMLINK_EXPANSIONS {
- return Err(host::__WASI_ELOOP);
+ return Err(wasi_unstable::ELOOP);
}
if head.ends_with('/') {
@@ -169,7 +171,7 @@ pub(crate) fn path_get(
continue;
}
Err(e) => {
- if e != host::__WASI_EINVAL && e != host::__WASI_ENOENT {
+ if e != wasi_unstable::EINVAL && e != wasi_unstable::ENOENT {
return Err(e);
}
}
@@ -178,7 +180,7 @@ pub(crate) fn path_get(
// not a symlink, so we're done;
return Ok(PathGet {
- dirfd: dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
+ dirfd: dir_stack.pop().ok_or(wasi_unstable::ENOTCAPABLE)?,
path: head,
});
}
@@ -188,7 +190,7 @@ pub(crate) fn path_get(
// no further components to process. means we've hit a case like "." or "a/..", or if the
// input path has trailing slashes and `needs_final_component` is not set
return Ok(PathGet {
- dirfd: dir_stack.pop().ok_or(host::__WASI_ENOTCAPABLE)?,
+ dirfd: dir_stack.pop().ok_or(wasi_unstable::ENOTCAPABLE)?,
path: String::from("."),
});
}
diff --git a/src/hostcalls_impl/misc.rs b/src/hostcalls_impl/misc.rs
index 3e4cbfe..a6f3d17 100644
@@ -2,9 +2,10 @@
use crate::ctx::WasiCtx;
use crate::memory::*;
use crate::sys::hostcalls_impl;
-use crate::{host, wasm32, Result};
+use crate::{wasm32, Result};
use log::trace;
use std::convert::TryFrom;
+use wasi::wasi_unstable;
pub(crate) fn args_get(
wasi_ctx: &WasiCtx,
@@ -30,10 +31,10 @@ pub(crate) fn args_get(
argv.push(arg_ptr);
let len =
- wasm32::uintptr_t::try_from(arg_bytes.len()).map_err(|_| host::__WASI_EOVERFLOW)?;
+ wasm32::uintptr_t::try_from(arg_bytes.len()).map_err(|_| wasi_unstable::EOVERFLOW)?;
argv_buf_offset = argv_buf_offset
.checked_add(len)
- .ok_or(host::__WASI_EOVERFLOW)?;
+ .ok_or(wasi_unstable::EOVERFLOW)?;
}
enc_slice_of(memory, argv.as_slice(), argv_ptr)
@@ -91,10 +92,10 @@ pub(crate) fn environ_get(
environ.push(env_ptr);
let len =
- wasm32::uintptr_t::try_from(env_bytes.len()).map_err(|_| host::__WASI_EOVERFLOW)?;
+ wasm32::uintptr_t::try_from(env_bytes.len()).map_err(|_| wasi_unstable::EOVERFLOW)?;
environ_buf_offset = environ_buf_offset
.checked_add(len)
- .ok_or(host::__WASI_EOVERFLOW)?;
+ .ok_or(wasi_unstable::EOVERFLOW)?;
}
enc_slice_of(memory, environ.as_slice(), environ_ptr)
@@ -119,7 +120,7 @@ pub(crate) fn environ_sizes_get(
.try_fold(0, |acc: u32, pair| {
acc.checked_add(pair.as_bytes_with_nul().len() as u32)
})
- .ok_or(host::__WASI_EOVERFLOW)?;
+ .ok_or(wasi_unstable::EOVERFLOW)?;
trace!(" | *environ_count_ptr={:?}", environ_count);
@@ -202,7 +203,7 @@ pub(crate) fn poll_oneoff(
);
if nsubscriptions as u64 > wasm32::__wasi_filesize_t::max_value() {
- return Err(host::__WASI_EINVAL);
+ return Err(wasi_unstable::EINVAL);
}
enc_pointee(memory, nevents, 0)?;
diff --git a/src/lib.rs b/src/lib.rs
index 23f3760..c1ec7d3 100644
@@ -36,4 +36,6 @@ pub mod wasm32;
pub use ctx::{WasiCtx, WasiCtxBuilder};
pub use sys::preopen_dir;
-pub(crate) type Result<T> = std::result::Result<T, self::host::__wasi_errno_t>;
+use wasi::wasi_unstable;
+
+pub(crate) type Result<T> = std::result::Result<T, wasi_unstable::Error>;
diff --git a/src/macros.rs b/src/macros.rs
index 43a361f..2b119b5 100644
@@ -2,12 +2,7 @@ macro_rules! hostcalls {
($(pub fn $name:ident($($arg:ident: $ty:ty,)*) -> $ret:ty;)*) => ($(
#[wasi_common_cbindgen::wasi_common_cbindgen]
pub fn $name($($arg: $ty,)*) -> $ret {
- let ret = match crate::hostcalls_impl::$name($($arg,)*) {
- Ok(()) => crate::host::__WASI_ESUCCESS,
- Err(e) => e,
- };
-
- crate::hostcalls::return_enc_errno(ret)
+ crate::hostcalls_impl::$name($($arg,)*)
}
)*)
}
diff --git a/src/sys/mod.rs b/src/sys/mod.rs
index aad58cd..357d943 100644
@@ -1,5 +1,5 @@
-use crate::host;
use cfg_if::cfg_if;
+use wasi::wasi_unstable;
cfg_if! {
if #[cfg(unix)] {
@@ -7,7 +7,7 @@ cfg_if! {
pub(crate) use self::unix::*;
pub use self::unix::preopen_dir;
- pub(crate) fn errno_from_host(err: i32) -> host::__wasi_errno_t {
+ pub(crate) fn errno_from_host(err: i32) -> wasi_unstable::Error {
host_impl::errno_from_nix(nix::errno::from_i32(err))
}
} else if #[cfg(windows)] {
@@ -15,7 +15,7 @@ cfg_if! {
pub(crate) use self::windows::*;
pub use self::windows::preopen_dir;
- pub(crate) fn errno_from_host(err: i32) -> host::__wasi_errno_t {
+ pub(crate) fn errno_from_host(err: i32) -> wasi_unstable::Error {
host_impl::errno_from_win(winx::winerror::WinError::from_u32(err as u32))
}
} else {
@@ -23,12 +23,12 @@ cfg_if! {
}
}
-pub(crate) fn errno_from_ioerror(e: std::io::Error) -> host::__wasi_errno_t {
+pub(crate) fn errno_from_ioerror(e: std::io::Error) -> wasi_unstable::Error {
match e.raw_os_error() {
Some(code) => errno_from_host(code),
None => {
log::debug!("Inconvertible OS error: {}", e);
- host::__WASI_EIO
+ wasi_unstable::EIO
}
}
}
diff --git a/src/sys/unix/host_impl.rs b/src/sys/unix/host_impl.rs
index 9402b8b..4bfecab 100644
@@ -5,9 +5,10 @@
use crate::{host, memory, Result};
use std::ffi::OsStr;
use std::os::unix::prelude::OsStrExt;
+use wasi::wasi_unstable;
-pub(crate) fn errno_from_nix(errno: nix::errno::Errno) -> host::__wasi_errno_t {
- match errno {
+pub(crate) fn errno_from_nix(errno: nix::errno::Errno) -> wasi_unstable::Error {
+ wasi_unstable::Error::new(match errno {
nix::errno::Errno::EPERM => host::__WASI_EPERM,
nix::errno::Errno::ENOENT => host::__WASI_ENOENT,
nix::errno::Errno::ESRCH => host::__WASI_ESRCH,
@@ -83,7 +84,8 @@ pub(crate) fn errno_from_nix(errno: nix::errno::Errno) -> host::__wasi_errno_t {
nix::errno::Errno::EOWNERDEAD => host::__WASI_EOWNERDEAD,
nix::errno::Errno::ENOTRECOVERABLE => host::__WASI_ENOTRECOVERABLE,
_ => host::__WASI_ENOSYS,
- }
+ })
+ .unwrap()
}
#[cfg(target_os = "linux")]
diff --git a/src/sys/windows/host_impl.rs b/src/sys/windows/host_impl.rs
index c53f29b..2d2cd8e 100644
@@ -7,20 +7,21 @@ use std::ffi::OsStr;
use std::fs::OpenOptions;
use std::os::windows::ffi::OsStrExt;
use std::os::windows::fs::OpenOptionsExt;
+use wasi::wasi_unstable;
use winx::file::{AccessMode, Attributes, CreationDisposition, Flags};
-pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_errno_t {
+pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> wasi_unstable::Error {
// TODO: implement error mapping between Windows and WASI
use winx::winerror::WinError::*;
- match error {
- ERROR_SUCCESS => host::__WASI_ESUCCESS,
+ wasi_unstable::Error::new(match error {
+ ERROR_SUCCESS => panic!("FIXME: if we need to support this here, return a Result"),
ERROR_BAD_ENVIRONMENT => host::__WASI_E2BIG,
ERROR_FILE_NOT_FOUND => host::__WASI_ENOENT,
ERROR_PATH_NOT_FOUND => host::__WASI_ENOENT,
ERROR_TOO_MANY_OPEN_FILES => host::__WASI_ENFILE,
ERROR_ACCESS_DENIED => host::__WASI_EACCES,
ERROR_SHARING_VIOLATION => host::__WASI_EACCES,
- ERROR_PRIVILEGE_NOT_HELD => host::__WASI_ENOTCAPABLE, // TODO is this the correct mapping?
+ ERROR_PRIVILEGE_NOT_HELD => host::__WASI_ENOTCAPABLE, // TODO is this the correct mapping? fixme: might be EPERM instead
ERROR_INVALID_HANDLE => host::__WASI_EBADF,
ERROR_INVALID_NAME => host::__WASI_EINVAL,
ERROR_NOT_ENOUGH_MEMORY => host::__WASI_ENOMEM,
@@ -35,7 +36,7 @@ pub(crate) fn errno_from_win(error: winx::winerror::WinError) -> host::__wasi_er
ERROR_NOT_A_REPARSE_POINT => host::__WASI_EINVAL,
ERROR_NEGATIVE_SEEK => host::__WASI_EINVAL,
_ => host::__WASI_ENOTSUP,
- }
+ })
}
pub(crate) fn fdflags_from_win(mode: AccessMode) -> host::__wasi_fdflags_t {
diff --git a/src/sys/windows/hostcalls_impl/misc.rs b/src/sys/windows/hostcalls_impl/misc.rs
index f920957..2514911 100644
@@ -5,8 +5,6 @@ use crate::memory::*;
use crate::sys::host_impl;
use crate::{host, wasm32, Result};
-use wasi_common_cbindgen::wasi_common_cbindgen;
-
pub(crate) fn clock_res_get(clock_id: host::__wasi_clockid_t) -> Result<host::__wasi_timestamp_t> {
unimplemented!("clock_res_get")
}
diff --git a/wasi-common-cbindgen/src/lib.rs b/wasi-common-cbindgen/src/lib.rs
index 31bd115..a981f13 100644
@@ -70,6 +70,7 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt
_ => {}
}
}
+ let trace_arg_ident = call_arg_ident.clone();
// capture output arg
let output = &function.decl.output;
@@ -83,9 +84,23 @@ pub fn wasi_common_cbindgen(attr: TokenStream, function: TokenStream) -> TokenSt
#arg_ident: #arg_type,
)*
) #output {
- #fn_ident(#(
+ trace!("{}(", stringify!(#fn_ident));
+ #(
+ trace!(" {} = fixme,", stringify!(#trace_arg_ident));
+ )*
+
+ let ret = #fn_ident(#(
#call_arg_ident,
- )*)
+ )*);
+
+ let ret = crate::hostcalls::return_enc_errno(ret);
+
+ trace!(") -> {}", ret);
+
+ match NonZeroU16::new(ret) {
+ None => Ok(()),
+ Some(r) => Err(r),
+ }
}
};