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 Rust impl of wasmtime_ssp_sched_yield #127

Merged
merged 2 commits into from
Apr 30, 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
1 change: 1 addition & 0 deletions wasmtime-wasi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
79 changes: 78 additions & 1 deletion wasmtime-wasi/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 0 additions & 6 deletions wasmtime-wasi/sandboxed-system-primitives/src/posix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
79 changes: 79 additions & 0 deletions wasmtime-wasi/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I convinced myself that it's ok to put these here, because eventually, wasmtime_ssp.h and the bindgen-generated wasmtime_ssp.rs will go away and we'll need these :-).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed! :-)

98 changes: 98 additions & 0 deletions wasmtime-wasi/src/host_impls.rs
Original file line number Diff line number Diff line change
@@ -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,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is correct, at least at present.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
}
1 change: 1 addition & 0 deletions wasmtime-wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion wasmtime-wasi/src/syscalls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down