From a51ffb6b6ddc5417113e121aed41fec0d721fe50 Mon Sep 17 00:00:00 2001 From: Jakub Konka Date: Sun, 28 Apr 2019 16:48:43 +0200 Subject: [PATCH] Add Rust impl of wasmtime_ssp_sched_yield Also, add Rust implementation of errno and convert_errno. --- .../include/wasmtime_ssp.h | 3 - .../sandboxed-system-primitives/src/posix.c | 6 - wasmtime-wasi/src/host.rs | 131 ++++++++++++++++++ wasmtime-wasi/src/host_impls.rs | 11 ++ wasmtime-wasi/src/syscalls.rs | 2 +- 5 files changed, 143 insertions(+), 10 deletions(-) diff --git a/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h b/wasmtime-wasi/sandboxed-system-primitives/include/wasmtime_ssp.h index 49af825250..7410161ee1 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 11e88fbec5..873132afcf 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 918ea92f5e..baa0049243 100644 --- a/wasmtime-wasi/src/host.rs +++ b/wasmtime-wasi/src/host.rs @@ -4,3 +4,134 @@ include!(concat!(env!("OUT_DIR"), "/wasmtime_ssp.rs")); pub type char = ::std::os::raw::c_char; pub type void = ::std::os::raw::c_void; + +use super::wasm32; + +// Taken from rust's implementation of `std::os` functionality for unix systems +// rust/src/libstd/sys/unix/os.rs +// https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libstd/sys/unix/os.rs +extern "C" { + #[cfg(not(target_os = "dragonfly"))] + #[cfg_attr( + any(target_os = "linux", target_os = "fuchsia", target_os = "l4re"), + link_name = "__errno_location" + )] + #[cfg_attr( + any( + target_os = "bitrig", + target_os = "netbsd", + target_os = "openbsd", + target_os = "android", + target_os = "hermit", + target_env = "newlib" + ), + link_name = "__errno" + )] + #[cfg_attr(target_os = "solaris", link_name = "___errno")] + #[cfg_attr( + any(target_os = "macos", target_os = "ios", target_os = "freebsd"), + link_name = "__error" + )] + #[cfg_attr(target_os = "haiku", link_name = "_errnop")] + fn errno_location() -> *mut libc::c_int; +} + +#[cfg(not(target_os = "dragonfly"))] +pub fn errno() -> i32 { + unsafe { (*errno_location()) as i32 } +} + +#[cfg(target_os = "dragonfly")] +pub fn errno() -> i32 { + extern "C" { + #[thread_local] + static errno: c_int; + } + + unsafe { errno as i32 } +} + +pub fn convert_errno(errno: i32) -> wasm32::__wasi_errno_t { + #[allow(unreachable_patterns)] + match errno { + libc::E2BIG => wasm32::__WASI_E2BIG, + libc::EACCES => wasm32::__WASI_EACCES, + libc::EADDRINUSE => wasm32::__WASI_EADDRINUSE, + libc::EADDRNOTAVAIL => wasm32::__WASI_EADDRNOTAVAIL, + libc::EAFNOSUPPORT => wasm32::__WASI_EAFNOSUPPORT, + libc::EAGAIN | libc::EWOULDBLOCK => wasm32::__WASI_EAGAIN, + libc::EALREADY => wasm32::__WASI_EALREADY, + libc::EBADF => wasm32::__WASI_EBADF, + libc::EBADMSG => wasm32::__WASI_EBADMSG, + libc::EBUSY => wasm32::__WASI_EBUSY, + libc::ECANCELED => wasm32::__WASI_ECANCELED, + libc::ECHILD => wasm32::__WASI_ECHILD, + libc::ECONNABORTED => wasm32::__WASI_ECONNABORTED, + libc::ECONNREFUSED => wasm32::__WASI_ECONNREFUSED, + libc::ECONNRESET => wasm32::__WASI_ECONNRESET, + libc::EDEADLK => wasm32::__WASI_EDEADLK, + libc::EDESTADDRREQ => wasm32::__WASI_EDESTADDRREQ, + libc::EDOM => wasm32::__WASI_EDOM, + libc::EDQUOT => wasm32::__WASI_EDQUOT, + libc::EEXIST => wasm32::__WASI_EEXIST, + libc::EFAULT => wasm32::__WASI_EFAULT, + libc::EFBIG => wasm32::__WASI_EFBIG, + libc::EHOSTUNREACH => wasm32::__WASI_EHOSTUNREACH, + libc::EIDRM => wasm32::__WASI_EIDRM, + libc::EILSEQ => wasm32::__WASI_EILSEQ, + libc::EINPROGRESS => wasm32::__WASI_EINPROGRESS, + libc::EINTR => wasm32::__WASI_EINTR, + libc::EINVAL => wasm32::__WASI_EINVAL, + libc::EIO => wasm32::__WASI_EIO, + libc::EISCONN => wasm32::__WASI_EISCONN, + libc::EISDIR => wasm32::__WASI_EISDIR, + libc::ELOOP => wasm32::__WASI_ELOOP, + libc::EMFILE => wasm32::__WASI_EMFILE, + libc::EMLINK => wasm32::__WASI_EMLINK, + libc::EMSGSIZE => wasm32::__WASI_EMSGSIZE, + libc::EMULTIHOP => wasm32::__WASI_EMULTIHOP, + libc::ENAMETOOLONG => wasm32::__WASI_ENAMETOOLONG, + libc::ENETDOWN => wasm32::__WASI_ENETDOWN, + libc::ENETRESET => wasm32::__WASI_ENETRESET, + libc::ENETUNREACH => wasm32::__WASI_ENETUNREACH, + libc::ENFILE => wasm32::__WASI_ENFILE, + libc::ENOBUFS => wasm32::__WASI_ENOBUFS, + libc::ENODEV => wasm32::__WASI_ENODEV, + libc::ENOENT => wasm32::__WASI_ENOENT, + libc::ENOEXEC => wasm32::__WASI_ENOEXEC, + libc::ENOLCK => wasm32::__WASI_ENOLCK, + libc::ENOLINK => wasm32::__WASI_ENOLINK, + libc::ENOMEM => wasm32::__WASI_ENOMEM, + libc::ENOMSG => wasm32::__WASI_ENOMSG, + libc::ENOPROTOOPT => wasm32::__WASI_ENOPROTOOPT, + libc::ENOSPC => wasm32::__WASI_ENOSPC, + libc::ENOSYS => wasm32::__WASI_ENOSYS, + // TODO: verify if this is correct + #[cfg(target_os = "freebsd")] + libc::ENOTCAPABLE => wasm32::__WASI_ENOTCAPABLE, + libc::ENOTCONN => wasm32::__WASI_ENOTCONN, + libc::ENOTDIR => wasm32::__WASI_ENOTDIR, + libc::ENOTEMPTY => wasm32::__WASI_ENOTEMPTY, + libc::ENOTRECOVERABLE => wasm32::__WASI_ENOTRECOVERABLE, + libc::ENOTSOCK => wasm32::__WASI_ENOTSOCK, + libc::ENOTSUP | libc::EOPNOTSUPP => wasm32::__WASI_ENOTSUP, + libc::ENOTTY => wasm32::__WASI_ENOTTY, + libc::ENXIO => wasm32::__WASI_ENXIO, + libc::EOVERFLOW => wasm32::__WASI_EOVERFLOW, + libc::EOWNERDEAD => wasm32::__WASI_EOWNERDEAD, + libc::EPERM => wasm32::__WASI_EPERM, + libc::EPIPE => wasm32::__WASI_EPIPE, + libc::EPROTO => wasm32::__WASI_EPROTO, + libc::EPROTONOSUPPORT => wasm32::__WASI_EPROTONOSUPPORT, + libc::EPROTOTYPE => wasm32::__WASI_EPROTOTYPE, + libc::ERANGE => wasm32::__WASI_ERANGE, + libc::EROFS => wasm32::__WASI_EROFS, + libc::ESPIPE => wasm32::__WASI_ESPIPE, + libc::ESRCH => wasm32::__WASI_ESRCH, + libc::ESTALE => wasm32::__WASI_ESTALE, + libc::ETIMEDOUT => wasm32::__WASI_ETIMEDOUT, + libc::ETXTBSY => wasm32::__WASI_ETXTBSY, + libc::EXDEV => wasm32::__WASI_EXDEV, + _ => wasm32::__WASI_ENOSYS, + } +} diff --git a/wasmtime-wasi/src/host_impls.rs b/wasmtime-wasi/src/host_impls.rs index 8383a41372..7edb425621 100644 --- a/wasmtime-wasi/src/host_impls.rs +++ b/wasmtime-wasi/src/host_impls.rs @@ -1,5 +1,16 @@ +use super::host; use super::wasm32; pub fn wasmtime_ssp_proc_exit(rval: wasm32::__wasi_exitcode_t) { ::std::process::exit(rval as i32) } + +pub fn wasmtime_ssp_sched_yield() -> wasm32::__wasi_errno_t { + unsafe { + if libc::sched_yield() < 0 { + return host::convert_errno(host::errno()); + } + } + + wasm32::__WASI_ESUCCESS +} diff --git a/wasmtime-wasi/src/syscalls.rs b/wasmtime-wasi/src/syscalls.rs index 7901f5531a..4f61b4fa46 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) }