Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions crates/cranelift/src/func_environ.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,10 @@ impl TranslateTrap for FuncEnvironment<'_> {
) -> ir::FuncRef {
self.builtin_functions.load_builtin(builder.func, index)
}

fn debug_tags(&self, srcloc: ir::SourceLoc) -> Vec<ir::DebugTag> {
FuncEnvironment::debug_tags(self, srcloc)
}
}

#[derive(Default)]
Expand Down
7 changes: 6 additions & 1 deletion crates/cranelift/src/trap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub trait TranslateTrap {
builder: &mut FunctionBuilder<'_>,
index: BuiltinFunctionIndex,
) -> ir::FuncRef;
fn debug_tags(&self, _srcloc: ir::SourceLoc) -> Vec<ir::DebugTag> {
vec![]
}

fn trap(&mut self, builder: &mut FunctionBuilder, trap: ir::TrapCode) {
match (
Expand All @@ -36,12 +39,14 @@ pub trait TranslateTrap {
// pass in our trap code. Leave a debug `unreachable` in place
// afterwards as a defense-in-depth measure.
(false, Some(trap)) => {
let debug_tags = self.debug_tags(builder.srcloc());
let trap_libcall = self.builtin_funcref(builder, BuiltinFunctionIndex::trap());
let vmctx = self.vmctx_val(&mut builder.cursor());
let trap_code = builder.ins().iconst(I8, i64::from(trap as u8));
builder.ins().call(trap_libcall, &[vmctx, trap_code]);
let raise_libcall = self.builtin_funcref(builder, BuiltinFunctionIndex::raise());
builder.ins().call(raise_libcall, &[vmctx]);
let inst = builder.ins().call(raise_libcall, &[vmctx]);
builder.func.debug_tags.set(inst, debug_tags);
builder.ins().trap(TRAP_INTERNAL_ASSERT);
}
}
Expand Down
13 changes: 9 additions & 4 deletions tests/all/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -694,18 +694,23 @@ async fn hostcall_trap_events() -> wasmtime::Result<()> {
},
r#"
(module
(func (export "main")
(func (export "main") (result i32)
i32.const 0
i32.const 0
i32.div_u
drop))
drop
i32.const 42))
"#,
)?;

debug_event_checker!(
D, store,
{ 0 ;
wasmtime::DebugEvent::Trap(wasmtime_environ::Trap::IntegerDivisionByZero) => {}
wasmtime::DebugEvent::Trap(wasmtime_environ::Trap::IntegerDivisionByZero) => {
let frame = store.debug_exit_frames().next().unwrap();
let (_func, pc) = frame.wasm_function_index_and_pc(&mut store).unwrap().unwrap();
assert_eq!(pc, 0x26);
}
}
);

Expand All @@ -714,7 +719,7 @@ async fn hostcall_trap_events() -> wasmtime::Result<()> {

let instance = Instance::new_async(&mut store, &module, &[]).await?;
let func = instance.get_func(&mut store, "main").unwrap();
let mut results = [];
let mut results = [Val::I32(0)];
let result = func.call_async(&mut store, &[], &mut results).await;
assert!(result.is_err()); // Uncaught trap.
assert_eq!(counter.load(Ordering::Relaxed), 1);
Expand Down
2 changes: 2 additions & 0 deletions tests/disas/debug-exceptions.wat
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
;; bl #0x3dc
;; ec: ldur x2, [sp, #0x10]
;; bl #0x414
;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 66, slot at FP-0xc0, locals , stack I32 @ slot+0x8
;; f4: .byte 0x1f, 0xc1, 0x00, 0x00
;; mov x2, x0
;; mov w3, w2
Expand Down Expand Up @@ -141,6 +142,7 @@
;; 19c: bl #0x3dc
;; 1a0: ldur x2, [sp, #0x10]
;; 1a4: bl #0x414
;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 52, slot at FP-0xc0, locals , stack
;; 1a8: .byte 0x1f, 0xc1, 0x00, 0x00
;; 1ac: .byte 0x1f, 0xc1, 0x00, 0x00
;; 1b0: .byte 0x1f, 0xc1, 0x00, 0x00
1 change: 1 addition & 0 deletions tests/disas/debug.wat
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
;; 67: callq 0x18c
;; 6c: movq %r12, %rdi
;; 6f: callq 0x1bd
;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 35, slot at FP-0x30, locals I32 @ slot+0x8, I32 @ slot+0xc, stack
;; 74: ud2
;;
;; wasm[0]::array_to_wasm_trampoline[0]:
Expand Down
Loading