From bd09343fcad694128b559da0ce4c6e383eb29d93 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 17:09:39 -0700 Subject: [PATCH 1/5] add structure for cross-platform wasi syscall implementations --- lib/wasi/src/syscalls/mod.rs | 20 +++++++++++++------- lib/wasi/src/syscalls/unix/linux.rs | 19 +++++++++++++++++++ lib/wasi/src/syscalls/unix/macos.rs | 19 +++++++++++++++++++ lib/wasi/src/syscalls/unix/mod.rs | 11 +++++++++++ lib/wasi/src/syscalls/windows.rs | 19 +++++++++++++++++++ 5 files changed, 81 insertions(+), 7 deletions(-) create mode 100644 lib/wasi/src/syscalls/unix/linux.rs create mode 100644 lib/wasi/src/syscalls/unix/macos.rs create mode 100644 lib/wasi/src/syscalls/unix/mod.rs create mode 100644 lib/wasi/src/syscalls/windows.rs diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 0c5f2f9b018..077a61155f6 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -1,6 +1,19 @@ +pub mod types; +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub mod unix; +#[cfg(any(target_os = "windows"))] +pub mod windows; + use crate::state::WasiState; +use types::*; use wasmer_runtime_core::{memory::Memory, vm::Ctx}; +#[cfg(any(target_os = "linux", target_os = "macos"))] +pub use unix::*; + +#[cfg(any(target_os = "windows"))] +pub use windows::*; + #[allow(clippy::mut_from_ref)] fn get_wasi_state(ctx: &Ctx) -> &mut WasiState { unsafe { &mut *(ctx.data as *mut WasiState) } @@ -59,13 +72,6 @@ pub fn args_sizes_get(ctx: &mut Ctx, argc_out: u32, argv_buf_size_out: u32) { memory.view::()[(argv_buf_size_out / 4) as usize].set(total_arg_size as u32); } -pub fn clock_res_get(ctx: &mut Ctx) { - unimplemented!() -} -pub fn clock_time_get(ctx: &mut Ctx) { - unimplemented!() -} - /// ### `environ_get()` /// Read environment variable data. /// The sizes of the buffers should match that returned by [`environ_sizes_get()`](#environ_sizes_get). diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs new file mode 100644 index 00000000000..c3556faba0b --- /dev/null +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -0,0 +1,19 @@ +use crate::syscalls::types::*; +use wasmer_runtime_core::{memory::Memory, vm::Ctx}; + +pub fn clock_res_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + resolution: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + __WASI_EINVAL +} + +pub fn clock_time_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + precision: __wasi_timestamp_t, + time: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + unimplemented!() +} diff --git a/lib/wasi/src/syscalls/unix/macos.rs b/lib/wasi/src/syscalls/unix/macos.rs new file mode 100644 index 00000000000..c3556faba0b --- /dev/null +++ b/lib/wasi/src/syscalls/unix/macos.rs @@ -0,0 +1,19 @@ +use crate::syscalls::types::*; +use wasmer_runtime_core::{memory::Memory, vm::Ctx}; + +pub fn clock_res_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + resolution: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + __WASI_EINVAL +} + +pub fn clock_time_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + precision: __wasi_timestamp_t, + time: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + unimplemented!() +} diff --git a/lib/wasi/src/syscalls/unix/mod.rs b/lib/wasi/src/syscalls/unix/mod.rs new file mode 100644 index 00000000000..8fab04668ff --- /dev/null +++ b/lib/wasi/src/syscalls/unix/mod.rs @@ -0,0 +1,11 @@ +#[cfg(target_os = "linux")] +pub mod linux; + +#[cfg(target_os = "macos")] +pub mod macos; + +#[cfg(target_os = "linux")] +pub use linux::*; + +#[cfg(target_os = "macos")] +pub use macos::*; diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs new file mode 100644 index 00000000000..c3556faba0b --- /dev/null +++ b/lib/wasi/src/syscalls/windows.rs @@ -0,0 +1,19 @@ +use crate::syscalls::types::*; +use wasmer_runtime_core::{memory::Memory, vm::Ctx}; + +pub fn clock_res_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + resolution: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + __WASI_EINVAL +} + +pub fn clock_time_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + precision: __wasi_timestamp_t, + time: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + unimplemented!() +} From ea27effdb3d95907c3d8d34e4442b9cdf2ced20c Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 29 Mar 2019 10:20:08 -0700 Subject: [PATCH 2/5] keeep top level wasi calls that call out to platform-specific impls --- lib/wasi/src/syscalls/mod.rs | 17 +++++++++++++++++ lib/wasi/src/syscalls/unix/linux.rs | 4 ++-- lib/wasi/src/syscalls/unix/macos.rs | 4 ++-- lib/wasi/src/syscalls/windows.rs | 4 ++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 077a61155f6..380adc68bdb 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -72,6 +72,23 @@ pub fn args_sizes_get(ctx: &mut Ctx, argc_out: u32, argv_buf_size_out: u32) { memory.view::()[(argv_buf_size_out / 4) as usize].set(total_arg_size as u32); } +pub fn clock_res_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + resolution: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + platform_clock_res_get(ctx, clock_id, resolution) +} + +pub fn clock_time_get( + ctx: &mut Ctx, + clock_id: __wasi_clockid_t, + precision: __wasi_timestamp_t, + time: WasmPtr<__wasi_timestamp_t>, +) -> __wasi_errno_t { + platform_clock_time_get(ctx, clock_id, precision, time) +} + /// ### `environ_get()` /// Read environment variable data. /// The sizes of the buffers should match that returned by [`environ_sizes_get()`](#environ_sizes_get). diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs index c3556faba0b..43ca2cb6dab 100644 --- a/lib/wasi/src/syscalls/unix/linux.rs +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -1,7 +1,7 @@ use crate::syscalls::types::*; use wasmer_runtime_core::{memory::Memory, vm::Ctx}; -pub fn clock_res_get( +pub fn platform_clock_res_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t>, @@ -9,7 +9,7 @@ pub fn clock_res_get( __WASI_EINVAL } -pub fn clock_time_get( +pub fn platform_clock_time_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, diff --git a/lib/wasi/src/syscalls/unix/macos.rs b/lib/wasi/src/syscalls/unix/macos.rs index c3556faba0b..43ca2cb6dab 100644 --- a/lib/wasi/src/syscalls/unix/macos.rs +++ b/lib/wasi/src/syscalls/unix/macos.rs @@ -1,7 +1,7 @@ use crate::syscalls::types::*; use wasmer_runtime_core::{memory::Memory, vm::Ctx}; -pub fn clock_res_get( +pub fn platform_clock_res_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t>, @@ -9,7 +9,7 @@ pub fn clock_res_get( __WASI_EINVAL } -pub fn clock_time_get( +pub fn platform_clock_time_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index c3556faba0b..43ca2cb6dab 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -1,7 +1,7 @@ use crate::syscalls::types::*; use wasmer_runtime_core::{memory::Memory, vm::Ctx}; -pub fn clock_res_get( +pub fn platform_clock_res_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t>, @@ -9,7 +9,7 @@ pub fn clock_res_get( __WASI_EINVAL } -pub fn clock_time_get( +pub fn platform_clock_time_get( ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, From 23c09ac0429f78fdf4e82c255b85beda541241ed Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 29 Mar 2019 11:04:38 -0700 Subject: [PATCH 3/5] add imports --- lib/wasi/src/syscalls/unix/linux.rs | 1 + lib/wasi/src/syscalls/unix/macos.rs | 1 + lib/wasi/src/syscalls/windows.rs | 1 + 3 files changed, 3 insertions(+) diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs index 43ca2cb6dab..78ee51c9a0b 100644 --- a/lib/wasi/src/syscalls/unix/linux.rs +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -1,4 +1,5 @@ use crate::syscalls::types::*; +use crate::ptr::{Array, WasmPtr}, use wasmer_runtime_core::{memory::Memory, vm::Ctx}; pub fn platform_clock_res_get( diff --git a/lib/wasi/src/syscalls/unix/macos.rs b/lib/wasi/src/syscalls/unix/macos.rs index 43ca2cb6dab..78ee51c9a0b 100644 --- a/lib/wasi/src/syscalls/unix/macos.rs +++ b/lib/wasi/src/syscalls/unix/macos.rs @@ -1,4 +1,5 @@ use crate::syscalls::types::*; +use crate::ptr::{Array, WasmPtr}, use wasmer_runtime_core::{memory::Memory, vm::Ctx}; pub fn platform_clock_res_get( diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index 43ca2cb6dab..78ee51c9a0b 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -1,4 +1,5 @@ use crate::syscalls::types::*; +use crate::ptr::{Array, WasmPtr}, use wasmer_runtime_core::{memory::Memory, vm::Ctx}; pub fn platform_clock_res_get( From 48d34d9522e0fbeba3b2001dec649117c00675c6 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 29 Mar 2019 11:38:48 -0700 Subject: [PATCH 4/5] improve calling of platform-specific code and impl linux clock calls --- Cargo.lock | 1 + lib/wasi/Cargo.toml | 3 +- lib/wasi/src/syscalls/mod.rs | 16 +++++++-- lib/wasi/src/syscalls/unix/linux.rs | 50 ++++++++++++++++++++++++----- lib/wasi/src/syscalls/unix/macos.rs | 9 ++---- lib/wasi/src/syscalls/windows.rs | 9 ++---- 6 files changed, 65 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 18ac80db931..53f1b1d5f4b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1482,6 +1482,7 @@ dependencies = [ name = "wasmer-wasi" version = "0.1.0" dependencies = [ + "libc 0.2.50 (registry+https://github.com/rust-lang/crates.io-index)", "wasmer-runtime-core 0.2.1", ] diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index 17a2f14f429..3d2151d3e13 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -5,4 +5,5 @@ authors = ["The Wasmer Engineering Team "] edition = "2018" [dependencies] -wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" } \ No newline at end of file +wasmer-runtime-core = { path = "../runtime-core", version = "0.2.1" } +libc = "0.2.50" \ No newline at end of file diff --git a/lib/wasi/src/syscalls/mod.rs b/lib/wasi/src/syscalls/mod.rs index 0ccc7bd649a..95d5f5240fe 100644 --- a/lib/wasi/src/syscalls/mod.rs +++ b/lib/wasi/src/syscalls/mod.rs @@ -108,7 +108,13 @@ pub fn clock_res_get( clock_id: __wasi_clockid_t, resolution: WasmPtr<__wasi_timestamp_t>, ) -> __wasi_errno_t { - platform_clock_res_get(ctx, clock_id, resolution) + let memory = ctx.memory(0); + + if let Some(out_addr) = resolution.deref(memory) { + platform_clock_res_get(clock_id, out_addr) + } else { + __WASI_EFAULT + } } pub fn clock_time_get( @@ -117,7 +123,13 @@ pub fn clock_time_get( precision: __wasi_timestamp_t, time: WasmPtr<__wasi_timestamp_t>, ) -> __wasi_errno_t { - platform_clock_time_get(ctx, clock_id, precision, time) + let memory = ctx.memory(0); + + if let Some(out_addr) = time.deref(memory) { + platform_clock_time_get(clock_id, precision, out_addr) + } else { + __WASI_EFAULT + } } /// ### `environ_get()` diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs index 78ee51c9a0b..e68d470fad6 100644 --- a/lib/wasi/src/syscalls/unix/linux.rs +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -1,20 +1,54 @@ use crate::syscalls::types::*; -use crate::ptr::{Array, WasmPtr}, -use wasmer_runtime_core::{memory::Memory, vm::Ctx}; +use libc::{ + clock_getres, clock_gettime, timespec, CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, + CLOCK_REALTIME, CLOCK_THREAD_CPUTIME_ID, +}; +use std::cell::Cell; +use std::mem; pub fn platform_clock_res_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, - resolution: WasmPtr<__wasi_timestamp_t>, + resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - __WASI_EINVAL + let linux_clock_id = match clock_id { + __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, + __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, + __WASI_CLOCK_REALTIME => CLOCK_REALTIME, + __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, + _ => return __WASI_EINVAL, + }; + + let output = unsafe { + let mut timespec_out: timespec = mem::uninitialized(); + clock_getres(linux_clock_id, &mut timespec_out); + }; + + resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t); + + // TODO: map output of clock_getres to __wasi_errno_t + __WASI_ESUCCESS } pub fn platform_clock_time_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, - time: WasmPtr<__wasi_timestamp_t>, + time: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - unimplemented!() + let linux_clock_id = match clock_id { + __WASI_CLOCK_MONOTONIC => CLOCK_MONOTONIC, + __WASI_CLOCK_PROCESS_CPUTIME_ID => CLOCK_PROCESS_CPUTIME_ID, + __WASI_CLOCK_REALTIME => CLOCK_REALTIME, + __WASI_CLOCK_THREAD_CPUTIME_ID => CLOCK_THREAD_CPUTIME_ID, + _ => return __WASI_EINVAL, + }; + + let output = unsafe { + let mut timespec_out: timespec = mem::uninitialized(); + clock_gettime(linux_clock_id, precision, &mut timespec_out); + }; + + resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t); + + // TODO: map output of clock_gettime to __wasi_errno_t + __WASI_ESUCCESS } diff --git a/lib/wasi/src/syscalls/unix/macos.rs b/lib/wasi/src/syscalls/unix/macos.rs index 78ee51c9a0b..6273695d322 100644 --- a/lib/wasi/src/syscalls/unix/macos.rs +++ b/lib/wasi/src/syscalls/unix/macos.rs @@ -1,20 +1,17 @@ use crate::syscalls::types::*; -use crate::ptr::{Array, WasmPtr}, -use wasmer_runtime_core::{memory::Memory, vm::Ctx}; +use std::cell::Cell; pub fn platform_clock_res_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, - resolution: WasmPtr<__wasi_timestamp_t>, + resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { __WASI_EINVAL } pub fn platform_clock_time_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, - time: WasmPtr<__wasi_timestamp_t>, + time: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { unimplemented!() } diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index 78ee51c9a0b..6273695d322 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -1,20 +1,17 @@ use crate::syscalls::types::*; -use crate::ptr::{Array, WasmPtr}, -use wasmer_runtime_core::{memory::Memory, vm::Ctx}; +use std::cell::Cell; pub fn platform_clock_res_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, - resolution: WasmPtr<__wasi_timestamp_t>, + resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { __WASI_EINVAL } pub fn platform_clock_time_get( - ctx: &mut Ctx, clock_id: __wasi_clockid_t, precision: __wasi_timestamp_t, - time: WasmPtr<__wasi_timestamp_t>, + time: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { unimplemented!() } From de241a03f9bed9cef6b197f92e47b64a435a3871 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Fri, 29 Mar 2019 11:43:32 -0700 Subject: [PATCH 5/5] fix linux impl bugs --- lib/wasi/src/syscalls/unix/linux.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/wasi/src/syscalls/unix/linux.rs b/lib/wasi/src/syscalls/unix/linux.rs index e68d470fad6..80a079b9be7 100644 --- a/lib/wasi/src/syscalls/unix/linux.rs +++ b/lib/wasi/src/syscalls/unix/linux.rs @@ -42,12 +42,17 @@ pub fn platform_clock_time_get( _ => return __WASI_EINVAL, }; - let output = unsafe { + let (output, timespec_out) = unsafe { let mut timespec_out: timespec = mem::uninitialized(); - clock_gettime(linux_clock_id, precision, &mut timespec_out); + ( + clock_gettime(linux_clock_id, &mut timespec_out), + timespec_out, + ) }; - resolution.set(timespec_out.tv_nsec as __wasi_timestamp_t); + // TODO: adjust output by precision... + + time.set(timespec_out.tv_nsec as __wasi_timestamp_t); // TODO: map output of clock_gettime to __wasi_errno_t __WASI_ESUCCESS