From bc863fcf0cf3cebf5b10255e0cfa283d80c7e7aa Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 12:19:23 -0700 Subject: [PATCH 1/5] hook up wasi to wasmer --- Cargo.lock | 1 + Cargo.toml | 4 +++- lib/wasi/src/lib.rs | 4 ++++ lib/wasi/src/utils.rs | 8 ++++++++ src/bin/wasmer.rs | 16 ++++++++++++++++ src/webassembly.rs | 1 + 6 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 lib/wasi/src/utils.rs diff --git a/Cargo.lock b/Cargo.lock index af062dc8670..18ac80db931 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1339,6 +1339,7 @@ dependencies = [ "wasmer-llvm-backend 0.1.0", "wasmer-runtime 0.2.1", "wasmer-runtime-core 0.2.1", + "wasmer-wasi 0.1.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 06da98dc95f..db2a2b5f381 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ wasmer-runtime-core = { path = "lib/runtime-core" } wasmer-emscripten = { path = "lib/emscripten" } wasmer-llvm-backend = { path = "lib/llvm-backend", optional = true } wasmer-dynasm-backend = { path = "lib/dynasm-backend", optional = true } +wasmer-wasi = { path = "lib/wasi", optional = true } [workspace] members = ["lib/clif-backend", "lib/dynasm-backend", "lib/runtime", "lib/runtime-core", "lib/emscripten", "lib/spectests", "lib/win-exception-handler", "lib/runtime-c-api", "lib/llvm-backend", "lib/wasi"] @@ -42,4 +43,5 @@ default = ["fast-tests"] # This feature will allow cargo test to run much faster fast-tests = [] llvm = ["wasmer-llvm-backend"] -dynasm = ["wasmer-dynasm-backend"] \ No newline at end of file +dynasm = ["wasmer-dynasm-backend"] +wasi = ["wasmer-wasi"] \ No newline at end of file diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index cade0133297..d6f72cbfcf6 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -1,6 +1,10 @@ mod syscalls; +mod utils; + use syscalls::*; +pub use self::utils::is_wasi_module; + use wasmer_runtime_core::{func, import::ImportObject, imports}; pub fn generate_import_object() -> ImportObject { diff --git a/lib/wasi/src/utils.rs b/lib/wasi/src/utils.rs new file mode 100644 index 00000000000..c6fb3267dfc --- /dev/null +++ b/lib/wasi/src/utils.rs @@ -0,0 +1,8 @@ +use wasmer_runtime_core::{module::Module, vm::Ctx}; + +// Cargo culting this from our emscripten implementation for now, but it seems like a +// good thing to check; TODO: verify this is useful +pub fn is_wasi_module(module: &Module) -> bool { + true + // TODO: +} diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 61bd50dacc9..b12465c9921 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -15,6 +15,7 @@ use wasmer::*; use wasmer_emscripten; use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH}; use wasmer_runtime_core::backend::CompilerConfig; +use wasmer_wasi; #[derive(Debug, StructOpt)] #[structopt(name = "wasmer", about = "Wasm execution runtime.")] @@ -200,6 +201,8 @@ fn execute_wasm(options: &Run) -> Result<(), String> { .map_err(|e| format!("Can't compile module: {:?}", e))? }; + // TODO: refactor this + #[cfg(not(features = "wasi"))] let (_abi, import_object, _em_globals) = if wasmer_emscripten::is_emscripten_module(&module) { let mut emscripten_globals = wasmer_emscripten::EmscriptenGlobals::new(&module); ( @@ -215,6 +218,19 @@ fn execute_wasm(options: &Run) -> Result<(), String> { ) }; + #[cfg(features = "wasi")] + let (_abi, import_object) = if wasmer_wasi::is_wasi_module(&module) { + ( + InstanceABI::WASI, + wasmer_emscripten::generate_import_object(), + ) + } else { + ( + InstanceABI::None, + wasmer_runtime_core::import::ImportObject::new(), + ) + }; + let mut instance = module .instantiate(&import_object) .map_err(|e| format!("Can't instantiate module: {:?}", e))?; diff --git a/src/webassembly.rs b/src/webassembly.rs index d3496606096..51f1e7a927d 100644 --- a/src/webassembly.rs +++ b/src/webassembly.rs @@ -21,6 +21,7 @@ pub struct ResultObject { #[derive(PartialEq)] pub enum InstanceABI { Emscripten, + WASI, None, } From 72dd995522a684ac6ed2a99b782400f2391420d8 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 12:21:44 -0700 Subject: [PATCH 2/5] fix typo --- src/bin/wasmer.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index b12465c9921..59f3c8fab84 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -220,10 +220,7 @@ fn execute_wasm(options: &Run) -> Result<(), String> { #[cfg(features = "wasi")] let (_abi, import_object) = if wasmer_wasi::is_wasi_module(&module) { - ( - InstanceABI::WASI, - wasmer_emscripten::generate_import_object(), - ) + (InstanceABI::WASI, wasmer_wasi::generate_import_object()) } else { ( InstanceABI::None, From 3c01c11f0168501743486afea94244cdc7b83323 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 13:24:15 -0700 Subject: [PATCH 3/5] pass args and env vars to wasi --- src/bin/wasmer.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 59f3c8fab84..0c493c92eed 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -220,7 +220,16 @@ fn execute_wasm(options: &Run) -> Result<(), String> { #[cfg(features = "wasi")] let (_abi, import_object) = if wasmer_wasi::is_wasi_module(&module) { - (InstanceABI::WASI, wasmer_wasi::generate_import_object()) + ( + InstanceABI::WASI, + wasmer_wasi::generate_import_object( + options.args.iter().map(|arg| arg.into_bytes()).collect(), + env::vars() + .iter() + .map(|(k, v)| format!("{}={}", k, v).into_bytes()) + .collect(), + ), + ) } else { ( InstanceABI::None, From a69fdfef3850925d60d3a1224badffa3dac1c5e3 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 13:46:30 -0700 Subject: [PATCH 4/5] implement wasi check --- lib/wasi/src/utils.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/wasi/src/utils.rs b/lib/wasi/src/utils.rs index c6fb3267dfc..4c3680be2e8 100644 --- a/lib/wasi/src/utils.rs +++ b/lib/wasi/src/utils.rs @@ -1,8 +1,15 @@ -use wasmer_runtime_core::{module::Module, vm::Ctx}; +use wasmer_runtime_core::module::Module; -// Cargo culting this from our emscripten implementation for now, but it seems like a -// good thing to check; TODO: verify this is useful +/// Check if a provided module is compiled with WASI support pub fn is_wasi_module(module: &Module) -> bool { - true - // TODO: + for (_, import_name) in &module.info().imported_functions { + let namespace = module + .info() + .namespace_table + .get(import_name.namespace_index); + if namespace == "wasi_unstable" { + return true; + } + } + false } From ce22818c35f0ed405114872bf88f9d43f9a33f16 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 28 Mar 2019 14:22:28 -0700 Subject: [PATCH 5/5] add feature gate on import --- src/bin/wasmer.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/bin/wasmer.rs b/src/bin/wasmer.rs index 0c493c92eed..29de018e232 100644 --- a/src/bin/wasmer.rs +++ b/src/bin/wasmer.rs @@ -15,6 +15,7 @@ use wasmer::*; use wasmer_emscripten; use wasmer_runtime::cache::{Cache as BaseCache, FileSystemCache, WasmHash, WASMER_VERSION_HASH}; use wasmer_runtime_core::backend::CompilerConfig; +#[cfg(feature = "wasi")] use wasmer_wasi; #[derive(Debug, StructOpt)]