From 08b4b639f42c49ad1e5a03e2e3fdb6fd08d36478 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 10:24:06 -0700 Subject: [PATCH 1/6] add realtime and monotonic clock support for wasi on windows --- lib/wasi/Cargo.toml | 1 + lib/wasi/src/syscalls/windows.rs | 37 ++++++++++++++++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index efd64270e99..34c65ff4cb3 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -19,6 +19,7 @@ log = "0.4.6" byteorder = "1.3.1" # hack to get tests to work wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } +winapi = { version = "0.3.7", optional = true } [build-dependencies] glob = "0.2.11" diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index 6273695d322..e86caa2e8c0 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -5,7 +5,21 @@ pub fn platform_clock_res_get( clock_id: __wasi_clockid_t, resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - __WASI_EINVAL + let resolution = match clock_id { + // resolution of monotonic clock at 10ms, from: + // https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-gettickcount64 + __WASI_CLOCK_MONOTONIC => 10_000_000, + // TODO: verify or compute this + __WASI_CLOCK_REALTIME => 1, + __WASI_CLOCK_PROCESS_CPUTIME_ID => { + return __WASI_EINVAL; + } + __WASI_CLOCK_THREAD_CPUTIME_ID => { + return __WASI_EINVAL; + } + }; + resolution.set(resolution); + __WASI_ESUCCESS } pub fn platform_clock_time_get( @@ -13,5 +27,24 @@ pub fn platform_clock_time_get( precision: __wasi_timestamp_t, time: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - unimplemented!() + let nanos = match clock_id { + __WASI_CLOCK_MONOTONIC => { + let tick_ms = winapi::um::sysinfoapi::GetTickCount64(); + tick_ms * 1_000_000 + } + __WASI_CLOCK_REALTIME => { + let duration = wasi_try!(std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .ok_or(__WASI_EIO)); + duration.as_nanos() as u64 + } + __WASI_CLOCK_PROCESS_CPUTIME_ID => { + unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_PROCESS_CPUTIME_ID, ..)") + } + __WASI_CLOCK_THREAD_CPUTIME_ID => { + unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_THREAD_CPUTIME_ID, ..)") + } + }; + time.set(nanos); + __WASI_ESUCCESS } From 736bddfe17564aac055b1128896e914ba4a3c55c Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 10:35:51 -0700 Subject: [PATCH 2/6] debug log error in windows clock, conditionally pull in winapi --- lib/wasi/src/lib.rs | 2 ++ lib/wasi/src/syscalls/windows.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index 8621dc55e8b..efbb1945763 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -2,6 +2,8 @@ #[macro_use] extern crate log; +#[cfg(target = windows)] +extern crate winapi; #[macro_use] mod macros; diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index e86caa2e8c0..a6627be9485 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -5,7 +5,7 @@ pub fn platform_clock_res_get( clock_id: __wasi_clockid_t, resolution: &Cell<__wasi_timestamp_t>, ) -> __wasi_errno_t { - let resolution = match clock_id { + let resolution_val = match clock_id { // resolution of monotonic clock at 10ms, from: // https://docs.microsoft.com/en-us/windows/desktop/api/sysinfoapi/nf-sysinfoapi-gettickcount64 __WASI_CLOCK_MONOTONIC => 10_000_000, @@ -18,7 +18,7 @@ pub fn platform_clock_res_get( return __WASI_EINVAL; } }; - resolution.set(resolution); + resolution.set(resolution_val); __WASI_ESUCCESS } @@ -35,7 +35,10 @@ pub fn platform_clock_time_get( __WASI_CLOCK_REALTIME => { let duration = wasi_try!(std::time::SystemTime::now() .duration_since(std::time::UNIX_EPOCH) - .ok_or(__WASI_EIO)); + .map_err(|e| { + debug!("Error in wasi::platform_clock_time_get: {:?}", e); + __WASI_EIO + })); duration.as_nanos() as u64 } __WASI_CLOCK_PROCESS_CPUTIME_ID => { From 2a30fc3f1b1c9531450ed85bf47cfe909a533cd8 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 10:41:40 -0700 Subject: [PATCH 3/6] wrap call in unsafe, unconditionally depend on winapi --- Cargo.lock | 1 + lib/wasi/Cargo.toml | 2 +- lib/wasi/src/lib.rs | 2 +- lib/wasi/src/syscalls/windows.rs | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d23ccdd223c..f5504e8c14d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2492,6 +2492,7 @@ dependencies = [ "wasmer-dev-utils 0.4.2", "wasmer-runtime-core 0.4.2", "wasmer-singlepass-backend 0.4.2", + "winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index 34c65ff4cb3..e59b45f96b2 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -19,7 +19,7 @@ log = "0.4.6" byteorder = "1.3.1" # hack to get tests to work wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } -winapi = { version = "0.3.7", optional = true } +winapi = "0.3" [build-dependencies] glob = "0.2.11" diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index efbb1945763..2ebf294b31e 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -2,7 +2,7 @@ #[macro_use] extern crate log; -#[cfg(target = windows)] +#[cfg(target = "windows")] extern crate winapi; #[macro_use] diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index a6627be9485..b715d18c271 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -29,7 +29,7 @@ pub fn platform_clock_time_get( ) -> __wasi_errno_t { let nanos = match clock_id { __WASI_CLOCK_MONOTONIC => { - let tick_ms = winapi::um::sysinfoapi::GetTickCount64(); + let tick_ms = unsafe { winapi::um::sysinfoapi::GetTickCount64() }; tick_ms * 1_000_000 } __WASI_CLOCK_REALTIME => { From e989a86d338d6ff896ee0d00419c51458dee7b59 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 10:55:54 -0700 Subject: [PATCH 4/6] properly handle bad input on windows clocks --- lib/wasi/src/syscalls/windows.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/wasi/src/syscalls/windows.rs b/lib/wasi/src/syscalls/windows.rs index b715d18c271..f6559290bbc 100644 --- a/lib/wasi/src/syscalls/windows.rs +++ b/lib/wasi/src/syscalls/windows.rs @@ -17,6 +17,7 @@ pub fn platform_clock_res_get( __WASI_CLOCK_THREAD_CPUTIME_ID => { return __WASI_EINVAL; } + _ => return __WASI_EINVAL, }; resolution.set(resolution_val); __WASI_ESUCCESS @@ -47,6 +48,7 @@ pub fn platform_clock_time_get( __WASI_CLOCK_THREAD_CPUTIME_ID => { unimplemented!("wasi::platform_clock_time_get(__WASI_CLOCK_THREAD_CPUTIME_ID, ..)") } + _ => return __WASI_EINVAL, }; time.set(nanos); __WASI_ESUCCESS From 8857e6d320b7d9b0d030f713080d4c7cf9360ac2 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 11:15:13 -0700 Subject: [PATCH 5/6] move check with debug build higher in CI, fix bug in debug macro --- .circleci/config.yml | 8 ++++---- lib/wasi/src/state.rs | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index 73a6a77c761..fb7d77dc873 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -176,6 +176,10 @@ jobs: export LLVM_SYS_70_PREFIX="`pwd`/clang+llvm-7.0.0-x86_64-linux-gnu-ubuntu-16.04/" make test-emscripten-clif make test-emscripten-llvm + - run: + name: Debug flag checked + command: | + cargo check --features "debug" - run: name: Release Build command: | @@ -189,10 +193,6 @@ jobs: echo "${CIRCLE_TAG}" >> artifacts/git_version make build-install cp ./wasmer.tar.gz ./artifacts/$(./binary-name.sh) - - run: - name: Debug flag checked - command: | - cargo check --features "debug" - persist_to_workspace: root: . paths: diff --git a/lib/wasi/src/state.rs b/lib/wasi/src/state.rs index 979d63d4d98..bcb34d333ec 100644 --- a/lib/wasi/src/state.rs +++ b/lib/wasi/src/state.rs @@ -219,7 +219,7 @@ impl WasiFs { } debug!("wasi::fs::mapped_dirs"); for (alias, real_dir) in mapped_dirs { - debug!("Attempting to open {:?} at {}", dest_dir, alias); + debug!("Attempting to open {:?} at {}", real_dir, alias); // TODO: think about this let default_rights = 0x1FFFFFFF; // all rights let cur_dir_metadata = real_dir From deb00c886c2f453a2c1c1abe90e7d8b828099bf3 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Tue, 21 May 2019 11:23:24 -0700 Subject: [PATCH 6/6] change to depend on winapi only on windows --- lib/wasi/Cargo.toml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/wasi/Cargo.toml b/lib/wasi/Cargo.toml index e59b45f96b2..cb9d5033932 100644 --- a/lib/wasi/Cargo.toml +++ b/lib/wasi/Cargo.toml @@ -19,6 +19,8 @@ log = "0.4.6" byteorder = "1.3.1" # hack to get tests to work wasmer-singlepass-backend = { path = "../singlepass-backend", version = "0.4.2", optional = true } + +[target.'cfg(windows)'.dependencies] winapi = "0.3" [build-dependencies]