Skip to content

Commit 62c93f5

Browse files
authored
Fix core dumps for composed components (#14067)
* Fix core dumps for composed components Signed-off-by: subotac <73706465+subotac@users.noreply.github.com> * Handle missing coredump globals during serialization Signed-off-by: subotac <73706465+subotac@users.noreply.github.com> * Document missing coredump resource indices Signed-off-by: subotac <73706465+subotac@users.noreply.github.com> --------- Signed-off-by: subotac <73706465+subotac@users.noreply.github.com>
1 parent 65b7954 commit 62c93f5

2 files changed

Lines changed: 66 additions & 1 deletion

File tree

crates/wasmtime/src/runtime/coredump.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,11 @@ impl WasmCoreDump {
269269

270270
let module_index = module_to_index[&module.id()];
271271

272+
// Core dumps are best-effort and may not capture every memory
273+
// referenced by an instance. In particular, shared memories
274+
// are intentionally omitted because their data cannot be
275+
// safely read through `Memory`. Use an invalid index for any
276+
// absent memory instead of panicking while serializing.
272277
let memories = instance
273278
.all_memories(store.0)
274279
.filter_map(|(_, m)| m.unshared())
@@ -280,11 +285,22 @@ impl WasmCoreDump {
280285
})
281286
.collect::<Vec<_>>();
282287

288+
// Component adapter modules can import runtime-managed globals,
289+
// such as component instance flags, whose definitions are not
290+
// enumerated by `StoreOpaque::for_each_global`. These globals
291+
// are visible through `Instance::all_globals` but absent from
292+
// the dump's globals section, so use an invalid index rather
293+
// than panicking while serializing.
283294
let globals = instance
284295
.all_globals(store.0)
285296
.collect::<Vec<_>>()
286297
.into_iter()
287-
.map(|(_i, global)| global_to_idx[&global.hash_key(&store.0)])
298+
.map(|(_i, global)| {
299+
global_to_idx
300+
.get(&global.hash_key(&store.0))
301+
.copied()
302+
.unwrap_or(u32::MAX)
303+
})
288304
.collect::<Vec<_>>();
289305

290306
instances.instance(module_index, memories, globals);

tests/all/coredump.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,52 @@ fn core_dump_with_shared_memory() -> Result<()> {
293293

294294
Ok(())
295295
}
296+
297+
#[test]
298+
#[cfg_attr(miri, ignore)]
299+
fn coredump_with_composed_component_adapters() -> Result<()> {
300+
use wasmtime::component::{Component, Linker};
301+
302+
let mut config = Config::new();
303+
config.coredump_on_trap(true);
304+
let engine = Engine::new(&config)?;
305+
let component = Component::new(
306+
&engine,
307+
r#"
308+
(component
309+
(component $A
310+
(core module $m
311+
(func (export "f") (param i32) (result i32) unreachable)
312+
)
313+
(core instance $i (instantiate $m))
314+
(func (export "f") (param "x" u32) (result u32)
315+
(canon lift (core func $i "f")))
316+
)
317+
(component $B
318+
(import "f" (func $f (param "x" u32) (result u32)))
319+
(core func $fl (canon lower (func $f)))
320+
(core module $m
321+
(import "" "f" (func $f (param i32) (result i32)))
322+
(func (export "run") (call $f (i32.const 1)) drop)
323+
)
324+
(core instance $i (instantiate $m
325+
(with "" (instance (export "f" (func $fl))))))
326+
(func (export "run") (canon lift (core func $i "run")))
327+
)
328+
(instance $a (instantiate $A))
329+
(instance $b (instantiate $B (with "f" (func $a "f"))))
330+
(func (export "run") (alias export $b "run"))
331+
)
332+
"#,
333+
)?;
334+
let mut store = Store::new(&engine, ());
335+
let instance = Linker::new(&engine).instantiate(&mut store, &component)?;
336+
let run = instance.get_typed_func::<(), ()>(&mut store, "run")?;
337+
338+
let err = run.call(&mut store, ()).unwrap_err();
339+
let coredump = err.downcast_ref::<WasmCoreDump>().unwrap();
340+
let bytes = coredump.serialize(&mut store, "composed-component-adapters");
341+
wasmparser::Validator::new().validate_all(&bytes)?;
342+
343+
Ok(())
344+
}

0 commit comments

Comments
 (0)