diff --git a/wasmtime-wasi/Cargo.toml b/wasmtime-wasi/Cargo.toml index f08ac14f74d0..7b4161cd1f70 100644 --- a/wasmtime-wasi/Cargo.toml +++ b/wasmtime-wasi/Cargo.toml @@ -20,6 +20,7 @@ target-lexicon = "0.3.0" cast = { version = "0.2.2", default-features = false } log = { version = "0.4.6", default-features = false } libc = "0.2.50" +errno = "0.2.4" [build-dependencies] cmake = "0.1.35" diff --git a/wasmtime-wasi/build.rs b/wasmtime-wasi/build.rs index ed070ea88b4a..46d7058caf6b 100644 --- a/wasmtime-wasi/build.rs +++ b/wasmtime-wasi/build.rs @@ -24,7 +24,84 @@ fn main() { .whitelist_type("fd_table") .whitelist_type("fd_prestats") .whitelist_type("argv_environ_values") - .whitelist_var("__WASI_.*"); + .whitelist_var("__WASI_.*") + .blacklist_item("__WASI_ESUCCESS") + .blacklist_item("__WASI_E2BIG") + .blacklist_item("__WASI_EACCES") + .blacklist_item("__WASI_EADDRINUSE") + .blacklist_item("__WASI_EADDRNOTAVAIL") + .blacklist_item("__WASI_EAFNOSUPPORT") + .blacklist_item("__WASI_EAGAIN") + .blacklist_item("__WASI_EALREADY") + .blacklist_item("__WASI_EBADF") + .blacklist_item("__WASI_EBADMSG") + .blacklist_item("__WASI_EBUSY") + .blacklist_item("__WASI_ECANCELED") + .blacklist_item("__WASI_ECHILD") + .blacklist_item("__WASI_ECONNABORTED") + .blacklist_item("__WASI_ECONNREFUSED") + .blacklist_item("__WASI_ECONNRESET") + .blacklist_item("__WASI_EDEADLK") + .blacklist_item("__WASI_EDESTADDRREQ") + .blacklist_item("__WASI_EDOM") + .blacklist_item("__WASI_EDQUOT") + .blacklist_item("__WASI_EEXIST") + .blacklist_item("__WASI_EFAULT") + .blacklist_item("__WASI_EFBIG") + .blacklist_item("__WASI_EHOSTUNREACH") + .blacklist_item("__WASI_EIDRM") + .blacklist_item("__WASI_EILSEQ") + .blacklist_item("__WASI_EINPROGRESS") + .blacklist_item("__WASI_EINTR") + .blacklist_item("__WASI_EINVAL") + .blacklist_item("__WASI_EIO") + .blacklist_item("__WASI_EISCONN") + .blacklist_item("__WASI_EISDIR") + .blacklist_item("__WASI_ELOOP") + .blacklist_item("__WASI_EMFILE") + .blacklist_item("__WASI_EMLINK") + .blacklist_item("__WASI_EMSGSIZE") + .blacklist_item("__WASI_EMULTIHOP") + .blacklist_item("__WASI_ENAMETOOLONG") + .blacklist_item("__WASI_ENETDOWN") + .blacklist_item("__WASI_ENETRESET") + .blacklist_item("__WASI_ENETUNREACH") + .blacklist_item("__WASI_ENFILE") + .blacklist_item("__WASI_ENOBUFS") + .blacklist_item("__WASI_ENODEV") + .blacklist_item("__WASI_ENOENT") + .blacklist_item("__WASI_ENOEXEC") + .blacklist_item("__WASI_ENOLCK") + .blacklist_item("__WASI_ENOLINK") + .blacklist_item("__WASI_ENOMEM") + .blacklist_item("__WASI_ENOMSG") + .blacklist_item("__WASI_ENOPROTOOPT") + .blacklist_item("__WASI_ENOSPC") + .blacklist_item("__WASI_ENOSYS") + .blacklist_item("__WASI_ENOTCONN") + .blacklist_item("__WASI_ENOTDIR") + .blacklist_item("__WASI_ENOTEMPTY") + .blacklist_item("__WASI_ENOTRECOVERABLE") + .blacklist_item("__WASI_ENOTSOCK") + .blacklist_item("__WASI_ENOTSUP") + .blacklist_item("__WASI_ENOTTY") + .blacklist_item("__WASI_ENXIO") + .blacklist_item("__WASI_EOVERFLOW") + .blacklist_item("__WASI_EOWNERDEAD") + .blacklist_item("__WASI_EPERM") + .blacklist_item("__WASI_EPIPE") + .blacklist_item("__WASI_EPROTO") + .blacklist_item("__WASI_EPROTONOSUPPORT") + .blacklist_item("__WASI_EPROTOTYPE") + .blacklist_item("__WASI_ERANGE") + .blacklist_item("__WASI_EROFS") + .blacklist_item("__WASI_ESPIPE") + .blacklist_item("__WASI_ESRCH") + .blacklist_item("__WASI_ESTALE") + .blacklist_item("__WASI_ETIMEDOUT") + .blacklist_item("__WASI_ETXTBSY") + .blacklist_item("__WASI_EXDEV") + .blacklist_item("__WASI_ENOTCAPABLE"); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h index 49af8252508b..7410161ee1bc 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h +++ b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h @@ -850,9 +850,6 @@ __wasi_errno_t wasmtime_ssp_sock_shutdown( __wasi_sdflags_t how ) WASMTIME_SSP_SYSCALL_NAME(sock_shutdown) __attribute__((__warn_unused_result__)); -__wasi_errno_t wasmtime_ssp_sched_yield(void) - WASMTIME_SSP_SYSCALL_NAME(sched_yield) __attribute__((__warn_unused_result__)); - #ifdef __cplusplus } #endif diff --git a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c index 11e88fbec514..873132afcfcd 100644 --- a/wasmtime-wasi/sandboxed-system-primitives/src/posix.c +++ b/wasmtime-wasi/sandboxed-system-primitives/src/posix.c @@ -2684,12 +2684,6 @@ __wasi_errno_t wasmtime_ssp_sock_shutdown( return 0; } -__wasi_errno_t wasmtime_ssp_sched_yield(void) { - if (sched_yield() < 0) - return convert_errno(errno); - return 0; -} - __wasi_errno_t wasmtime_ssp_args_get( #if !defined(WASMTIME_SSP_STATIC_CURFDS) struct argv_environ_values *argv_environ, diff --git a/wasmtime-wasi/src/host.rs b/wasmtime-wasi/src/host.rs index 918ea92f5eef..1b83c5491eea 100644 --- a/wasmtime-wasi/src/host.rs +++ b/wasmtime-wasi/src/host.rs @@ -4,3 +4,82 @@ include!(concat!(env!("OUT_DIR"), "/wasmtime_ssp.rs")); pub type char = ::std::os::raw::c_char; pub type void = ::std::os::raw::c_void; +// pub type __wasi_errno_t = u16; + +pub const __WASI_ESUCCESS: __wasi_errno_t = 0; +pub const __WASI_E2BIG: __wasi_errno_t = 1; +pub const __WASI_EACCES: __wasi_errno_t = 2; +pub const __WASI_EADDRINUSE: __wasi_errno_t = 3; +pub const __WASI_EADDRNOTAVAIL: __wasi_errno_t = 4; +pub const __WASI_EAFNOSUPPORT: __wasi_errno_t = 5; +pub const __WASI_EAGAIN: __wasi_errno_t = 6; +pub const __WASI_EALREADY: __wasi_errno_t = 7; +pub const __WASI_EBADF: __wasi_errno_t = 8; +pub const __WASI_EBADMSG: __wasi_errno_t = 9; +pub const __WASI_EBUSY: __wasi_errno_t = 10; +pub const __WASI_ECANCELED: __wasi_errno_t = 11; +pub const __WASI_ECHILD: __wasi_errno_t = 12; +pub const __WASI_ECONNABORTED: __wasi_errno_t = 13; +pub const __WASI_ECONNREFUSED: __wasi_errno_t = 14; +pub const __WASI_ECONNRESET: __wasi_errno_t = 15; +pub const __WASI_EDEADLK: __wasi_errno_t = 16; +pub const __WASI_EDESTADDRREQ: __wasi_errno_t = 17; +pub const __WASI_EDOM: __wasi_errno_t = 18; +pub const __WASI_EDQUOT: __wasi_errno_t = 19; +pub const __WASI_EEXIST: __wasi_errno_t = 20; +pub const __WASI_EFAULT: __wasi_errno_t = 21; +pub const __WASI_EFBIG: __wasi_errno_t = 22; +pub const __WASI_EHOSTUNREACH: __wasi_errno_t = 23; +pub const __WASI_EIDRM: __wasi_errno_t = 24; +pub const __WASI_EILSEQ: __wasi_errno_t = 25; +pub const __WASI_EINPROGRESS: __wasi_errno_t = 26; +pub const __WASI_EINTR: __wasi_errno_t = 27; +pub const __WASI_EINVAL: __wasi_errno_t = 28; +pub const __WASI_EIO: __wasi_errno_t = 29; +pub const __WASI_EISCONN: __wasi_errno_t = 30; +pub const __WASI_EISDIR: __wasi_errno_t = 31; +pub const __WASI_ELOOP: __wasi_errno_t = 32; +pub const __WASI_EMFILE: __wasi_errno_t = 33; +pub const __WASI_EMLINK: __wasi_errno_t = 34; +pub const __WASI_EMSGSIZE: __wasi_errno_t = 35; +pub const __WASI_EMULTIHOP: __wasi_errno_t = 36; +pub const __WASI_ENAMETOOLONG: __wasi_errno_t = 37; +pub const __WASI_ENETDOWN: __wasi_errno_t = 38; +pub const __WASI_ENETRESET: __wasi_errno_t = 39; +pub const __WASI_ENETUNREACH: __wasi_errno_t = 40; +pub const __WASI_ENFILE: __wasi_errno_t = 41; +pub const __WASI_ENOBUFS: __wasi_errno_t = 42; +pub const __WASI_ENODEV: __wasi_errno_t = 43; +pub const __WASI_ENOENT: __wasi_errno_t = 44; +pub const __WASI_ENOEXEC: __wasi_errno_t = 45; +pub const __WASI_ENOLCK: __wasi_errno_t = 46; +pub const __WASI_ENOLINK: __wasi_errno_t = 47; +pub const __WASI_ENOMEM: __wasi_errno_t = 48; +pub const __WASI_ENOMSG: __wasi_errno_t = 49; +pub const __WASI_ENOPROTOOPT: __wasi_errno_t = 50; +pub const __WASI_ENOSPC: __wasi_errno_t = 51; +pub const __WASI_ENOSYS: __wasi_errno_t = 52; +pub const __WASI_ENOTCONN: __wasi_errno_t = 53; +pub const __WASI_ENOTDIR: __wasi_errno_t = 54; +pub const __WASI_ENOTEMPTY: __wasi_errno_t = 55; +pub const __WASI_ENOTRECOVERABLE: __wasi_errno_t = 56; +pub const __WASI_ENOTSOCK: __wasi_errno_t = 57; +pub const __WASI_ENOTSUP: __wasi_errno_t = 58; +pub const __WASI_ENOTTY: __wasi_errno_t = 59; +pub const __WASI_ENXIO: __wasi_errno_t = 60; +pub const __WASI_EOVERFLOW: __wasi_errno_t = 61; +pub const __WASI_EOWNERDEAD: __wasi_errno_t = 62; +pub const __WASI_EPERM: __wasi_errno_t = 63; +pub const __WASI_EPIPE: __wasi_errno_t = 64; +pub const __WASI_EPROTO: __wasi_errno_t = 65; +pub const __WASI_EPROTONOSUPPORT: __wasi_errno_t = 66; +pub const __WASI_EPROTOTYPE: __wasi_errno_t = 67; +pub const __WASI_ERANGE: __wasi_errno_t = 68; +pub const __WASI_EROFS: __wasi_errno_t = 69; +pub const __WASI_ESPIPE: __wasi_errno_t = 70; +pub const __WASI_ESRCH: __wasi_errno_t = 71; +pub const __WASI_ESTALE: __wasi_errno_t = 72; +pub const __WASI_ETIMEDOUT: __wasi_errno_t = 73; +pub const __WASI_ETXTBSY: __wasi_errno_t = 74; +pub const __WASI_EXDEV: __wasi_errno_t = 75; +pub const __WASI_ENOTCAPABLE: __wasi_errno_t = 76; diff --git a/wasmtime-wasi/src/host_impls.rs b/wasmtime-wasi/src/host_impls.rs index 8383a41372b7..a509ba25828d 100644 --- a/wasmtime-wasi/src/host_impls.rs +++ b/wasmtime-wasi/src/host_impls.rs @@ -1,5 +1,103 @@ +use super::host; use super::wasm32; +use errno::{errno, Errno}; + +/// Convert POSIX error code to host's WASI error code +fn convert_errno(error: Errno) -> host::__wasi_errno_t { + #[allow(unreachable_patterns)] + match error.into() { + libc::E2BIG => host::__WASI_E2BIG, + libc::EACCES => host::__WASI_EACCES, + libc::EADDRINUSE => host::__WASI_EADDRINUSE, + libc::EADDRNOTAVAIL => host::__WASI_EADDRNOTAVAIL, + libc::EAFNOSUPPORT => host::__WASI_EAFNOSUPPORT, + libc::EAGAIN | libc::EWOULDBLOCK => host::__WASI_EAGAIN, + libc::EALREADY => host::__WASI_EALREADY, + libc::EBADF => host::__WASI_EBADF, + libc::EBADMSG => host::__WASI_EBADMSG, + libc::EBUSY => host::__WASI_EBUSY, + libc::ECANCELED => host::__WASI_ECANCELED, + libc::ECHILD => host::__WASI_ECHILD, + libc::ECONNABORTED => host::__WASI_ECONNABORTED, + libc::ECONNREFUSED => host::__WASI_ECONNREFUSED, + libc::ECONNRESET => host::__WASI_ECONNRESET, + libc::EDEADLK => host::__WASI_EDEADLK, + libc::EDESTADDRREQ => host::__WASI_EDESTADDRREQ, + libc::EDOM => host::__WASI_EDOM, + libc::EDQUOT => host::__WASI_EDQUOT, + libc::EEXIST => host::__WASI_EEXIST, + libc::EFAULT => host::__WASI_EFAULT, + libc::EFBIG => host::__WASI_EFBIG, + libc::EHOSTUNREACH => host::__WASI_EHOSTUNREACH, + libc::EIDRM => host::__WASI_EIDRM, + libc::EILSEQ => host::__WASI_EILSEQ, + libc::EINPROGRESS => host::__WASI_EINPROGRESS, + libc::EINTR => host::__WASI_EINTR, + libc::EINVAL => host::__WASI_EINVAL, + libc::EIO => host::__WASI_EIO, + libc::EISCONN => host::__WASI_EISCONN, + libc::EISDIR => host::__WASI_EISDIR, + libc::ELOOP => host::__WASI_ELOOP, + libc::EMFILE => host::__WASI_EMFILE, + libc::EMLINK => host::__WASI_EMLINK, + libc::EMSGSIZE => host::__WASI_EMSGSIZE, + libc::EMULTIHOP => host::__WASI_EMULTIHOP, + libc::ENAMETOOLONG => host::__WASI_ENAMETOOLONG, + libc::ENETDOWN => host::__WASI_ENETDOWN, + libc::ENETRESET => host::__WASI_ENETRESET, + libc::ENETUNREACH => host::__WASI_ENETUNREACH, + libc::ENFILE => host::__WASI_ENFILE, + libc::ENOBUFS => host::__WASI_ENOBUFS, + libc::ENODEV => host::__WASI_ENODEV, + libc::ENOENT => host::__WASI_ENOENT, + libc::ENOEXEC => host::__WASI_ENOEXEC, + libc::ENOLCK => host::__WASI_ENOLCK, + libc::ENOLINK => host::__WASI_ENOLINK, + libc::ENOMEM => host::__WASI_ENOMEM, + libc::ENOMSG => host::__WASI_ENOMSG, + libc::ENOPROTOOPT => host::__WASI_ENOPROTOOPT, + libc::ENOSPC => host::__WASI_ENOSPC, + libc::ENOSYS => host::__WASI_ENOSYS, + // TODO: verify if this is correct + #[cfg(target_os = "freebsd")] + libc::ENOTCAPABLE => host::__WASI_ENOTCAPABLE, + libc::ENOTCONN => host::__WASI_ENOTCONN, + libc::ENOTDIR => host::__WASI_ENOTDIR, + libc::ENOTEMPTY => host::__WASI_ENOTEMPTY, + libc::ENOTRECOVERABLE => host::__WASI_ENOTRECOVERABLE, + libc::ENOTSOCK => host::__WASI_ENOTSOCK, + libc::ENOTSUP | libc::EOPNOTSUPP => host::__WASI_ENOTSUP, + libc::ENOTTY => host::__WASI_ENOTTY, + libc::ENXIO => host::__WASI_ENXIO, + libc::EOVERFLOW => host::__WASI_EOVERFLOW, + libc::EOWNERDEAD => host::__WASI_EOWNERDEAD, + libc::EPERM => host::__WASI_EPERM, + libc::EPIPE => host::__WASI_EPIPE, + libc::EPROTO => host::__WASI_EPROTO, + libc::EPROTONOSUPPORT => host::__WASI_EPROTONOSUPPORT, + libc::EPROTOTYPE => host::__WASI_EPROTOTYPE, + libc::ERANGE => host::__WASI_ERANGE, + libc::EROFS => host::__WASI_EROFS, + libc::ESPIPE => host::__WASI_ESPIPE, + libc::ESRCH => host::__WASI_ESRCH, + libc::ESTALE => host::__WASI_ESTALE, + libc::ETIMEDOUT => host::__WASI_ETIMEDOUT, + libc::ETXTBSY => host::__WASI_ETXTBSY, + libc::EXDEV => host::__WASI_EXDEV, + _ => host::__WASI_ENOSYS, + } +} pub fn wasmtime_ssp_proc_exit(rval: wasm32::__wasi_exitcode_t) { ::std::process::exit(rval as i32) } + +pub fn wasmtime_ssp_sched_yield() -> host::__wasi_errno_t { + unsafe { + if libc::sched_yield() < 0 { + return convert_errno(errno()); + } + } + + host::__WASI_ESUCCESS +} diff --git a/wasmtime-wasi/src/lib.rs b/wasmtime-wasi/src/lib.rs index b6d9eb135663..d38f1d155b2f 100644 --- a/wasmtime-wasi/src/lib.rs +++ b/wasmtime-wasi/src/lib.rs @@ -8,6 +8,7 @@ extern crate wasmtime_jit; extern crate wasmtime_runtime; #[macro_use] extern crate log; +extern crate errno; mod host; mod host_impls; diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index 7901f5531a76..4f61b4fa46d2 100644 --- a/wasmtime-wasi/src/syscalls.rs +++ b/wasmtime-wasi/src/syscalls.rs @@ -1413,7 +1413,7 @@ syscalls! { } pub unsafe extern "C" fn sched_yield(_vmctx: *mut VMContext,) -> wasm32::__wasi_errno_t { - let e = host::wasmtime_ssp_sched_yield(); + let e = host_impls::wasmtime_ssp_sched_yield(); return_encoded_errno(e) }