Skip to content

Commit

Permalink
Fix a panic on the CLI with -Scli=n
Browse files Browse the repository at this point in the history
When a module trapped and `-Scli=n` was passed the CLI would panic
instead of properly returning an error, and this commit fixes that.
  • Loading branch information
alexcrichton committed Nov 4, 2024
1 parent 859d38d commit f81945b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 17 deletions.
22 changes: 10 additions & 12 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,21 +233,19 @@ impl RunCommand {
if let Some(exit) = e.downcast_ref::<wasmtime_wasi::I32Exit>() {
std::process::exit(exit.0);
}
if e.is::<wasmtime::Trap>() {
eprintln!("Error: {e:?}");
cfg_if::cfg_if! {
if #[cfg(unix)] {
std::process::exit(rustix::process::EXIT_SIGNALED_SIGABRT);
} else if #[cfg(windows)] {
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=vs-2019
std::process::exit(3);
}
}
if e.is::<wasmtime::Trap>() {
eprintln!("Error: {e:?}");
cfg_if::cfg_if! {
if #[cfg(unix)] {
std::process::exit(rustix::process::EXIT_SIGNALED_SIGABRT);
} else if #[cfg(windows)] {
// https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/abort?view=vs-2019
std::process::exit(3);
}
}
return Err(e);
} else {
unreachable!("either preview1_ctx or preview2_ctx present")
}
return Err(e);
}
}

Expand Down
29 changes: 24 additions & 5 deletions tests/all/cli_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use anyhow::{bail, Result};
use std::fs::File;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::process::{Command, Output, Stdio};
use std::process::{Command, ExitStatus, Output, Stdio};
use tempfile::{NamedTempFile, TempDir};

// Run the wasmtime CLI with the provided args and return the `Output`.
Expand Down Expand Up @@ -174,10 +174,13 @@ fn run_wasmtime_unreachable_wat() -> Result<()> {

assert_ne!(output.stderr, b"");
assert_eq!(output.stdout, b"");
assert!(!output.status.success());

let code = output
.status
assert_trap_code(&output.status);
Ok(())
}

fn assert_trap_code(status: &ExitStatus) {
let code = status
.code()
.expect("wasmtime process should exit normally");

Expand All @@ -186,7 +189,6 @@ fn run_wasmtime_unreachable_wat() -> Result<()> {
assert_eq!(code, 128 + libc::SIGABRT);
#[cfg(windows)]
assert_eq!(code, 3);
Ok(())
}

// Run a simple WASI hello world, snapshot0 edition.
Expand Down Expand Up @@ -2035,3 +2037,20 @@ fn profile_with_vtune() -> Result<()> {
fn is_vtune_available() -> bool {
Command::new("vtune").arg("-version").output().is_ok()
}

#[test]
fn unreachable_without_wasi() -> Result<()> {
let output = run_wasmtime_for_output(
&[
"-Scli=n",
"-Ccache=n",
"tests/all/cli_tests/unreachable.wat",
],
None,
)?;

assert_ne!(output.stderr, b"");
assert_eq!(output.stdout, b"");
assert_trap_code(&output.status);
Ok(())
}

0 comments on commit f81945b

Please sign in to comment.