Skip to content

Commit

Permalink
Do not run Wasm with 0 imports as WASI in the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark McCaskey committed May 4, 2021
1 parent f2dd752 commit 5d41f99
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 32 deletions.
62 changes: 33 additions & 29 deletions lib/cli/src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,37 +156,41 @@ impl Run {
use wasmer_wasi::WasiVersion;

let wasi_versions = Wasi::get_versions(&module);
if let Some(wasi_versions) = wasi_versions {
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);
match wasi_versions {
Some(wasi_versions) if !wasi_versions.is_empty() => {
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
.command_name
.clone()
.or_else(|| {
self.path
.file_name()
.map(|f| f.to_string_lossy().to_string())
})
.unwrap_or_default();
return self
.wasi
.execute(module, program_name, self.args.clone())
.with_context(|| "WASI execution failed");
let program_name = self
.command_name
.clone()
.or_else(|| {
self.path
.file_name()
.map(|f| f.to_string_lossy().to_string())
})
.unwrap_or_default();
return self
.wasi
.execute(module, program_name, self.args.clone())
.with_context(|| "WASI execution failed");
}
// not WASI
_ => (),
}
}

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/cli/tests/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn wasi_test_wasm_path() -> String {
format!("{}/{}", C_ASSET_PATH, "qjs.wasm")
}

fn wasi_test_empty_wat_path() -> String {
fn test_no_imports_wat_path() -> String {
format!("{}/{}", ASSET_PATH, "fib.wat")
}

Expand Down Expand Up @@ -39,10 +39,10 @@ fn run_wasi_works() -> anyhow::Result<()> {
}

#[test]
fn run_empty_wasi_works() -> anyhow::Result<()> {
fn run_no_imports_wasm_works() -> anyhow::Result<()> {
let output = Command::new(WASMER_PATH)
.arg("run")
.arg(wasi_test_empty_wat_path())
.arg(test_no_imports_wat_path())
.output()?;

if !output.status.success() {
Expand Down

0 comments on commit 5d41f99

Please sign in to comment.