Skip to content

A lightweight WebAssembly runtime that is fast, secure, and standards-compliant

Fix panic compiling an empty component with debug info (#14130)

m0g3ra1f7d4c3cb3f
diff --git a/crates/cranelift/src/debug/transform/simulate.rs b/crates/cranelift/src/debug/transform/simulate.rs
index dd39c2f543..2504ea3265 100644
--- a/crates/cranelift/src/debug/transform/simulate.rs
+++ b/crates/cranelift/src/debug/transform/simulate.rs
@@ -296,8 +296,15 @@ pub fn generate_simulated_dwarf(
out_strings: &mut write::StringTable,
isa: &dyn TargetIsa,
) -> Result<(), Error> {
+ // A component without any core modules has no functions to describe, and
+ // the compilation unit below names itself after the first translation's
+ // wasm file. There is nothing to simulate, so leave the DWARF empty.
+ let Some((_, first_translation)) = compilation.translations.iter().next() else {
+ return Ok(());
+ };
+
let (wasm_file, path) = {
- let di = &compilation.translations.iter().next().unwrap().1.debuginfo;
+ let di = &first_translation.debuginfo;
let path = di
.wasm_file
.path
diff --git a/tests/all/cli_tests.rs b/tests/all/cli_tests.rs
index 8f175b7077..9c2b8e9b20 100644
--- a/tests/all/cli_tests.rs
+++ b/tests/all/cli_tests.rs
@@ -3716,3 +3716,21 @@ fn non_utf8_raises_error() -> Result<()> {
}
Ok(())
}
+
+#[test]
+fn compile_empty_component_with_debug_info() -> Result<()> {
+ // A component with no core modules reached simulated-DWARF generation with
+ // nothing to describe, which used to panic instead of compiling.
+ let td = TempDir::new()?;
+ let cwasm = td.path().join("empty-component.cwasm");
+ let stdout = run_wasmtime(&[
+ "compile",
+ "-D",
+ "debug-info=y",
+ "tests/all/cli_tests/empty_component.wat",
+ "-o",
+ cwasm.to_str().unwrap(),
+ ])?;
+ assert_eq!(stdout, "");
+ Ok(())
+}
diff --git a/tests/all/cli_tests/empty_component.wat b/tests/all/cli_tests/empty_component.wat
new file mode 100644
index 0000000000..e5627d1d0f
--- /dev/null
+++ b/tests/all/cli_tests/empty_component.wat
@@ -0,0 +1 @@
+(component)