From bc9706e3c6d94776140a8c5581c99e5ef5d24430 Mon Sep 17 00:00:00 2001 From: Mark McCaskey Date: Thu, 22 Apr 2021 08:32:08 -0700 Subject: [PATCH] Add flag to deny multiple WASI versions --- CHANGELOG.md | 1 + lib/cli/src/commands/run.rs | 20 ++++++++++++++++++-- lib/cli/src/commands/run/wasi.rs | 3 +++ lib/wasi/src/lib.rs | 9 +++------ lib/wasi/src/utils.rs | 11 +++++++++++ 5 files changed, 36 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ba0f76593..ba22bdaa13e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Looking for changes that affect our C API? See the [C API Changelog](lib/c-api/C - [#2135](https://github.com/wasmerio/wasmer/pull/2135) [Documentation](./PACKAGING.md) for linux distribution maintainers ### Changed +- [#2251](https://github.com/wasmerio/wasmer/pull/2251) Wasmer CLI will now execute WASI modules with multiple WASI namespaces in them by default. Use `--allow-multiple-wasi-versions` to suppress the warning and use `--deny-multiple-wasi-versions` to make it an error. - [#2201](https://github.com/wasmerio/wasmer/pull/2201) Implement `loupe::MemoryUsage` for `wasmer::Instance`. - [#2200](https://github.com/wasmerio/wasmer/pull/2200) Implement `loupe::MemoryUsage` for `wasmer::Module`. - [#2199](https://github.com/wasmerio/wasmer/pull/2199) Implement `loupe::MemoryUsage` for `wasmer::Store`. diff --git a/lib/cli/src/commands/run.rs b/lib/cli/src/commands/run.rs index 2dfac57e5c7..3e0904d44e2 100644 --- a/lib/cli/src/commands/run.rs +++ b/lib/cli/src/commands/run.rs @@ -152,10 +152,26 @@ impl Run { // If WASI is enabled, try to execute it with it #[cfg(feature = "wasi")] { + use std::collections::BTreeSet; + use wasmer_wasi::WasiVersion; + let wasi_versions = Wasi::get_versions(&module); if let Some(wasi_versions) = wasi_versions { - if wasi_versions.len() >= 2 && !self.wasi.allow_multiple_wasi_versions { - warning!("Found more than 1 WASI version in this module. If this is intentional, pass `--allow-multiple-wasi-versions` to suppress this warning."); + if wasi_versions.len() >= 2 { + let get_version_list = |versions: &BTreeSet| -> String { + versions + .iter() + .map(|v| format!("`{}`", v.get_namespace_str())) + .collect::>() + .join(", ") + }; + if self.wasi.deny_multiple_wasi_versions { + let version_list = get_version_list(&wasi_versions); + bail!("Found more than 1 WASI version in this module ({}) and `--deny-multiple-wasi-versions` is enabled.", version_list); + } else if !self.wasi.allow_multiple_wasi_versions { + let version_list = get_version_list(&wasi_versions); + warning!("Found more than 1 WASI version in this module ({}). If this is intentional, pass `--allow-multiple-wasi-versions` to suppress this warning.", version_list); + } } let program_name = self diff --git a/lib/cli/src/commands/run/wasi.rs b/lib/cli/src/commands/run/wasi.rs index c64fdac9f1c..9d9662a978c 100644 --- a/lib/cli/src/commands/run/wasi.rs +++ b/lib/cli/src/commands/run/wasi.rs @@ -29,6 +29,9 @@ pub struct Wasi { #[clap(long = "allow-multiple-wasi-versions")] pub allow_multiple_wasi_versions: bool, + + #[clap(long = "deny-multiple-wasi-versions")] + pub deny_multiple_wasi_versions: bool, } #[allow(dead_code)] diff --git a/lib/wasi/src/lib.rs b/lib/wasi/src/lib.rs index c93fc6526c1..ea7ca43cd84 100644 --- a/lib/wasi/src/lib.rs +++ b/lib/wasi/src/lib.rs @@ -89,7 +89,7 @@ impl WasiEnv { let wasi_versions = get_wasi_versions(module, false).ok_or(WasiError::UnknownWasiVersion)?; let mut version_iter = wasi_versions.iter(); - let mut resolver: Box = { + let mut resolver: Box = { let version = version_iter.next().ok_or(WasiError::UnknownWasiVersion)?; Box::new(generate_import_object_from_env( module.store(), @@ -98,11 +98,8 @@ impl WasiEnv { )) }; for version in version_iter { - let new_import_object = generate_import_object_from_env( - module.store(), - self.clone(), - *version, - ); + let new_import_object = + generate_import_object_from_env(module.store(), self.clone(), *version); resolver = Box::new(new_import_object.chain_front(resolver)); } Ok(resolver) diff --git a/lib/wasi/src/utils.rs b/lib/wasi/src/utils.rs index 5eee8b5ccf1..153bf1d55e5 100644 --- a/lib/wasi/src/utils.rs +++ b/lib/wasi/src/utils.rs @@ -31,6 +31,17 @@ pub enum WasiVersion { Latest, } +impl WasiVersion { + /// Get the version as its namespace str as it appears in Wasm modules. + pub const fn get_namespace_str(&self) -> &'static str { + match *self { + WasiVersion::Snapshot0 => SNAPSHOT0_NAMESPACE, + WasiVersion::Snapshot1 => SNAPSHOT1_NAMESPACE, + WasiVersion::Latest => SNAPSHOT1_NAMESPACE, + } + } +} + impl PartialEq for WasiVersion { fn eq(&self, other: &Self) -> bool { match (*self, *other) {