From daa25d1021b7a558cfaab585340eb6b68771b7ef Mon Sep 17 00:00:00 2001 From: vms Date: Sat, 8 Aug 2020 23:07:21 +0300 Subject: [PATCH] add getting WASI state for repl --- .circleci/config.yml | 6 ++-- Cargo.lock | 2 ++ engine/src/engine.rs | 14 ++++++++ engine/src/module/fce_module.rs | 4 +++ fluence-app-service/Cargo.toml | 5 +-- fluence-app-service/src/service.rs | 7 ++++ fluence-faas/src/faas.rs | 7 ++++ tools/repl/Cargo.toml | 1 + tools/repl/src/main.rs | 54 ++++++++++++++++++++++++++++++ 9 files changed, 95 insertions(+), 5 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index bcf17f7e..fc6ae349 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -44,19 +44,19 @@ jobs: - checkout - restore_cache: keys: - - examples01-{{ checksum "examples/ipfs_node/wasm/ipfs_node/Cargo.toml" }}-{{ checksum "examples/ipfs_node/wasm/ipfs_rpc/Cargo.toml" }}-{{ checksum "examples/ipfs_node/Cargo.toml" }} + - examples01-{{ checksum "examples/ipfs_node/effector/Cargo.toml" }}-{{ checksum "examples/ipfs_node/pure/Cargo.toml" }} - run: | rustup toolchain install nightly rustup component add rustfmt rustup component add clippy cargo install cargo-wasi - cd examples/ipfs_node/wasm/ipfs_node + cd examples/ipfs_node/effector cargo fmt --all -- --check --color always cargo wasi build cargo clippy -v --target wasm32-wasi - cd ../ipfs_rpc + cd ../pure cargo fmt --all -- --check --color always cargo wasi build cargo clippy -v --target wasm32-wasi diff --git a/Cargo.lock b/Cargo.lock index ff982dac..27045c8d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -457,6 +457,7 @@ dependencies = [ "rustyline", "serde_json", "uuid", + "wasmer-wasi-fl", ] [[package]] @@ -508,6 +509,7 @@ dependencies = [ "log", "serde_json", "toml", + "wasmer-wasi-fl", ] [[package]] diff --git a/engine/src/engine.rs b/engine/src/engine.rs index cdbe7c27..f9695b40 100644 --- a/engine/src/engine.rs +++ b/engine/src/engine.rs @@ -106,6 +106,20 @@ impl FCE { } } + pub fn module_wasi_state>( + &mut self, + module_name: S, + ) -> Result<&wasmer_wasi::state::WasiState> { + self.module_wasi_state_(module_name.as_ref()) + } + + fn module_wasi_state_(&mut self, module_name: &str) -> Result<&wasmer_wasi::state::WasiState> { + match self.modules.get_mut(module_name) { + Some(module) => Ok(module.get_wasi_state()), + None => Err(FCEError::NoSuchModule(module_name.to_string())), + } + } + /// Return function signatures of all loaded info FCE modules with their names. pub fn interface(&self) -> impl Iterator>)> { self.modules.iter().map(|(module_name, module)| { diff --git a/engine/src/module/fce_module.rs b/engine/src/module/fce_module.rs index 879df7dd..6f3ab74f 100644 --- a/engine/src/module/fce_module.rs +++ b/engine/src/module/fce_module.rs @@ -154,6 +154,10 @@ impl FCEModule { }) } + pub(crate) fn get_wasi_state(&mut self) -> &wasmer_wasi::state::WasiState { + unsafe { wasmer_wasi::state::get_wasi_state(self.wasmer_instance.context_mut()) } + } + // TODO: change the cloning Callable behaviour after changes of Wasmer API pub(super) fn get_callable(&self, function_name: &str) -> Result> { match self.exports_funcs.get(function_name) { diff --git a/fluence-app-service/Cargo.toml b/fluence-app-service/Cargo.toml index 79577a29..4c68734c 100644 --- a/fluence-app-service/Cargo.toml +++ b/fluence-app-service/Cargo.toml @@ -7,9 +7,10 @@ edition = "2018" [dependencies] fluence-faas = { path = "../fluence-faas" } -toml = "0.5.6" -serde_json = "1.0.53" log = "0.4.8" +serde_json = "1.0.53" +toml = "0.5.6" +wasmer-wasi = { package = "wasmer-wasi-fl", version = "0.17.0" } [features] raw-module-api = ["fluence-faas/raw-module-api"] diff --git a/fluence-app-service/src/service.rs b/fluence-app-service/src/service.rs index f913aba8..8627bc0d 100644 --- a/fluence-app-service/src/service.rs +++ b/fluence-app-service/src/service.rs @@ -197,4 +197,11 @@ impl AppService { pub fn unload_module>(&mut self, module_name: S) -> Result<()> { self.faas.unload_module(module_name).map_err(Into::into) } + + pub fn get_wasi_state>( + &mut self, + module_name: S, + ) -> Result<&wasmer_wasi::state::WasiState> { + self.faas.module_wasi_state(module_name).map_err(Into::into) + } } diff --git a/fluence-faas/src/faas.rs b/fluence-faas/src/faas.rs index 17b43f1d..d46803d7 100644 --- a/fluence-faas/src/faas.rs +++ b/fluence-faas/src/faas.rs @@ -253,4 +253,11 @@ impl FluenceFaaS { pub fn unload_module>(&mut self, module_name: S) -> Result<()> { self.fce.unload_module(module_name).map_err(Into::into) } + + pub fn module_wasi_state>( + &mut self, + module_name: S, + ) -> Result<&wasmer_wasi::state::WasiState> { + self.fce.module_wasi_state(module_name).map_err(Into::into) + } } diff --git a/tools/repl/Cargo.toml b/tools/repl/Cargo.toml index f0c67506..ba22a5e2 100644 --- a/tools/repl/Cargo.toml +++ b/tools/repl/Cargo.toml @@ -17,6 +17,7 @@ fluence-app-service = { path = "../../fluence-app-service", features = ["raw-mod anyhow = "1.0.31" clap = "2.33.1" serde_json = "1.0.57" +wasmer-wasi = { package = "wasmer-wasi-fl", version = "0.17.0"} rustyline = "6.1.2" rustop = "1.1.0" diff --git a/tools/repl/src/main.rs b/tools/repl/src/main.rs index 29c67674..45663f97 100644 --- a/tools/repl/src/main.rs +++ b/tools/repl/src/main.rs @@ -119,6 +119,20 @@ fn main() -> Result<(), anyhow::Error> { }; println!("{}", result); } + Some("envs") => { + next_argument!(module_name, args, "Module name should be specified"); + match app_service.get_wasi_state(module_name) { + Ok(wasi_state) => print_envs(wasi_state), + Err(e) => println!("{}", e), + }; + } + Some("fs") => { + next_argument!(module_name, args, "Module name should be specified"); + match app_service.get_wasi_state(module_name) { + Ok(wasi_state) => print_fs_state(wasi_state), + Err(e) => println!("{}", e), + }; + } Some("interface") => { let interface = app_service.get_interface(); println!("application service interface:\n{}", interface); @@ -131,6 +145,8 @@ fn main() -> Result<(), anyhow::Error> { unload - to unload Wasm module from AppService\n\ call [args] - to call function with func_name of module with module_name\n\ interface - to print public interface of current AppService\n\ + envs - to print environment variables of module with module_name\n\ + fs - to print filesystem state of module with module_name\n\ h/help - to print this message\n\ e/exit/q/quit - to exit" ); @@ -172,3 +188,41 @@ fn create_service_from_config>( Ok(app_service) } + +fn print_envs(wasi_state: &wasmer_wasi::state::WasiState) { + let envs = &wasi_state.envs; + + println!("Environment variables:"); + for env in envs.iter() { + match String::from_utf8(env.clone()) { + Ok(string) => println!("{}", string), + Err(_) => println!("{:?}", env), + } + } +} + +fn print_fs_state(wasi_state: &wasmer_wasi::state::WasiState) { + let wasi_fs = &wasi_state.fs; + + println!("preopened file descriptors:\n{:?}\n", wasi_fs.preopen_fds); + + println!("name map:"); + for (name, inode) in &wasi_fs.name_map { + println!("{} - {:?}", name, inode); + } + + println!("\nfile descriptors map:"); + for (id, fd) in &wasi_fs.fd_map { + println!("{} - {:?}", id, fd); + } + + println!("\norphan file descriptors:"); + for (fd, inode) in &wasi_fs.orphan_fds { + println!("{:?} - {:?}", fd, inode); + } + + println!("\ninodes:"); + for (id, inode) in wasi_fs.inodes.iter().enumerate() { + println!("{}: {:?}", id, inode); + } +}