Skip to content

Commit

Permalink
Add flag to deny multiple WASI versions
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed Apr 22, 2021
1 parent b4ee78c commit bc9706e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
20 changes: 18 additions & 2 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WasiVersion>| -> String {
versions
.iter()
.map(|v| format!("`{}`", v.get_namespace_str()))
.collect::<Vec<String>>()
.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
Expand Down
3 changes: 3 additions & 0 deletions lib/cli/src/commands/run/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
9 changes: 3 additions & 6 deletions lib/wasi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn NamedResolver> = {
let mut resolver: Box<dyn NamedResolver> = {
let version = version_iter.next().ok_or(WasiError::UnknownWasiVersion)?;
Box::new(generate_import_object_from_env(
module.store(),
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions lib/wasi/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<WasiVersion> for WasiVersion {
fn eq(&self, other: &Self) -> bool {
match (*self, *other) {
Expand Down

0 comments on commit bc9706e

Please sign in to comment.