From f81945bee4d1abd6a94629767a96b79b293e5bb7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 4 Nov 2024 13:27:18 -0800 Subject: [PATCH] Fix a panic on the CLI with `-Scli=n` When a module trapped and `-Scli=n` was passed the CLI would panic instead of properly returning an error, and this commit fixes that. --- src/commands/run.rs | 22 ++++++++++------------ tests/all/cli_tests.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/src/commands/run.rs b/src/commands/run.rs index c9abf273947a..15b4b5f9bbe4 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -233,21 +233,19 @@ impl RunCommand { if let Some(exit) = e.downcast_ref::() { std::process::exit(exit.0); } - if e.is::() { - 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::() { + 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); } } diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs index b7ea967c7808..909227e1b866 100644 --- a/tests/all/cli_tests.rs +++ b/tests/all/cli_tests.rs @@ -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`. @@ -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"); @@ -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. @@ -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(()) +}