Skip to content

Commit 1ab8603

Browse files
authored
Debugging: force hostcall-based-traps when guest-debugging is enabled. (#12052)
* Debugging: force hostcall-based-traps when guest-debugging is enabled. We manually configure this for tests now but this change formalizes the dependence on hostcall-based traps that we outlined in #11964. * Re-bless tests. * Review feedback.
1 parent 5ad241c commit 1ab8603

File tree

3 files changed

+92
-46
lines changed

3 files changed

+92
-46
lines changed

crates/wasmtime/src/config.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,15 @@ impl Config {
23762376
}
23772377
}
23782378

2379+
// If guest-debugging is enabled, we must disable
2380+
// signals-based traps. Do this before we process the user's
2381+
// provided tunables settings so we can detect a conflict with
2382+
// an explicit request to use signals-based traps.
2383+
#[cfg(feature = "debug")]
2384+
if self.tunables.debug_guest == Some(true) {
2385+
tunables.signals_based_traps = false;
2386+
}
2387+
23792388
self.tunables.configure(&mut tunables);
23802389

23812390
// If we're going to compile with winch, we must use the winch calling convention.
@@ -2400,8 +2409,13 @@ impl Config {
24002409
None
24012410
};
24022411

2403-
if !cfg!(feature = "debug") && tunables.debug_guest {
2404-
bail!("debug instrumentation support was disabled at compile time");
2412+
if tunables.debug_guest {
2413+
if !cfg!(feature = "debug") {
2414+
bail!("debug instrumentation support was disabled at compile time");
2415+
}
2416+
if tunables.signals_based_traps {
2417+
bail!("cannot use signals-based traps with guest debugging enabled");
2418+
}
24052419
}
24062420

24072421
Ok((tunables, features))

tests/all/debug.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ use wasmtime::{
77
Store, StoreContextMut,
88
};
99

10+
#[test]
11+
fn debugging_does_not_work_with_signal_based_traps() {
12+
let mut config = Config::default();
13+
config.guest_debug(true).signals_based_traps(true);
14+
let err = Engine::new(&config).expect_err("invalid config should produce an error");
15+
assert!(format!("{err:?}").contains("cannot use signals-based traps"));
16+
}
17+
1018
fn get_module_and_store<C: Fn(&mut Config)>(
1119
c: C,
1220
wat: &str,
@@ -370,8 +378,6 @@ async fn hostcall_trap_events() -> anyhow::Result<()> {
370378
|config| {
371379
config.async_support(true);
372380
config.wasm_exceptions(true);
373-
// Force hostcall-based traps.
374-
config.signals_based_traps(false);
375381
},
376382
r#"
377383
(module

tests/disas/debug-exceptions.wat

Lines changed: 68 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@
1616
;; wasm[0]::function[1]:
1717
;; stp x29, x30, [sp, #-0x10]!
1818
;; mov x29, sp
19-
;; ldur x16, [x2, #8]
20-
;; ldur x16, [x16, #0x10]
21-
;; add x16, x16, #0xc0
22-
;; cmp sp, x16
23-
;; b.lo #0x110
24-
;; 1c: stp x27, x28, [sp, #-0x10]!
19+
;; stp x27, x28, [sp, #-0x10]!
2520
;; stp x25, x26, [sp, #-0x10]!
2621
;; stp x23, x24, [sp, #-0x10]!
2722
;; stp x21, x22, [sp, #-0x10]!
@@ -30,55 +25,79 @@
3025
;; stp d12, d13, [sp, #-0x10]!
3126
;; stp d10, d11, [sp, #-0x10]!
3227
;; stp d8, d9, [sp, #-0x10]!
33-
;; sub sp, sp, #0x20
34-
;; stur x2, [sp]
28+
;; sub sp, sp, #0x30
29+
;; ldr x3, [x2, #8]
30+
;; ldr x3, [x3, #0x10]
31+
;; mov x4, sp
32+
;; cmp x4, x3
33+
;; b.lo #0x15c
34+
;; 44: stur x2, [sp]
3535
;; stur x2, [sp, #0x10]
36-
;; mov w27, #0x2a
37-
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 61, slot at FP-0xb0, locals , stack
38-
;; stur w27, [sp, #8]
39-
;; stur w27, [sp, #8]
40-
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 64, slot at FP-0xb0, locals , stack
36+
;; mov w22, #0x2a
37+
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 61, slot at FP-0xc0, locals , stack
38+
;; stur w22, [sp, #8]
39+
;; stur w22, [sp, #8]
40+
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 64, slot at FP-0xc0, locals , stack
4141
;; ldur x2, [sp, #0x10]
42-
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 66, slot at FP-0xb0, locals , stack I32 @ slot+0x8
43-
;; bl #0x31c
44-
;; 60: mov x21, x2
42+
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 66, slot at FP-0xc0, locals , stack I32 @ slot+0x8
43+
;; bl #0x3f0
44+
;; 60: mov x24, x2
4545
;; mov w3, #0x4000000
4646
;; mov w4, #2
4747
;; mov w5, #0x28
4848
;; mov w6, #8
4949
;; ldur x2, [sp, #0x10]
50-
;; bl #0x2a8
51-
;; 7c: ldur x11, [sp, #0x10]
52-
;; ldr x0, [x11, #8]
53-
;; ldr x5, [x0, #0x18]
54-
;; add x0, x5, #0x20
55-
;; str w27, [x0, w2, uxtw]
56-
;; add x3, x5, #0x18
57-
;; mov x4, x21
58-
;; str w4, [x3, w2, uxtw]
50+
;; bl #0x310
51+
;; 7c: ldur x13, [sp, #0x10]
52+
;; ldr x5, [x13, #8]
53+
;; ldr x6, [x5, #0x18]
54+
;; stur x5, [sp, #0x20]
55+
;; add x15, x6, #0x20
56+
;; str w22, [x15, w2, uxtw]
57+
;; add x3, x6, #0x18
58+
;; mov x7, x24
59+
;; str w7, [x3, w2, uxtw]
5960
;; mov w3, #0
60-
;; add x4, x5, #0x1c
61-
;; stur x5, [sp, #0x18]
61+
;; add x4, x6, #0x1c
62+
;; stur x6, [sp, #0x18]
6263
;; str w3, [x4, w2, uxtw]
6364
;; mov x3, x2
6465
;; ldur x2, [sp, #0x10]
65-
;; bl #0x354
66-
;; ├─╼ exception frame offset: SP = FP - 0xb0
67-
;; ╰─╼ exception handler: tag=0, context at [SP+0x10], handler=0xbc
68-
;; b8: .byte 0x1f, 0xc1, 0x00, 0x00
69-
;; ldur x5, [sp, #0x18]
70-
;; add x4, x5, #0x20
71-
;; ldr w6, [x4, w0, uxtw]
72-
;; stur w6, [sp, #8]
66+
;; bl #0x428
67+
;; ├─╼ exception frame offset: SP = FP - 0xc0
68+
;; ╰─╼ exception handler: tag=0, context at [SP+0x10], handler=0xd4
69+
;; bc: mov w3, #9
7370
;; ldur x2, [sp, #0x10]
74-
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 72, slot at FP-0xb0, locals , stack I32 @ slot+0x8
75-
;; ldr x8, [x2, #0x30]
71+
;; bl #0x384
72+
;; c8: ldur x2, [sp, #0x10]
73+
;; bl #0x3bc
74+
;; d0: .byte 0x1f, 0xc1, 0x00, 0x00
75+
;; mov x15, x0
76+
;; mov w6, w15
77+
;; mov x7, #0x28
78+
;; adds x5, x6, x7
79+
;; cset x7, hs
80+
;; uxtb w6, w7
81+
;; cbnz x6, #0x174
82+
;; f0: ldur x12, [sp, #0x20]
83+
;; ldr x8, [x12, #0x20]
84+
;; cmp x5, x8
85+
;; cset x9, hi
86+
;; uxtb w9, w9
87+
;; cbnz x9, #0x178
88+
;; 108: ldur x6, [sp, #0x18]
89+
;; add x10, x6, #0x20
90+
;; ldr w12, [x10, w15, uxtw]
91+
;; stur w12, [sp, #8]
92+
;; ldur x2, [sp, #0x10]
93+
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 72, slot at FP-0xc0, locals , stack I32 @ slot+0x8
94+
;; ldr x14, [x2, #0x30]
7695
;; ldr x2, [x2, #0x40]
7796
;; ldur x3, [sp, #0x10]
78-
;; blr x8
79-
;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 72, slot at FP-0xb0, locals , stack I32 @ slot+0x8
80-
;; e0: add sp, sp, #0x20
81-
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 75, slot at FP-0xb0, locals , stack
97+
;; blr x14
98+
;; ╰─╼ debug frame state (after previous inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 72, slot at FP-0xc0, locals , stack I32 @ slot+0x8
99+
;; 12c: add sp, sp, #0x30
100+
;; ╰─╼ debug frame state (before next inst): func key DefinedWasmFunction(StaticModuleIndex(0), DefinedFuncIndex(0)), wasm PC 75, slot at FP-0xc0, locals , stack
82101
;; ldp d8, d9, [sp], #0x10
83102
;; ldp d10, d11, [sp], #0x10
84103
;; ldp d12, d13, [sp], #0x10
@@ -90,4 +109,11 @@
90109
;; ldp x27, x28, [sp], #0x10
91110
;; ldp x29, x30, [sp], #0x10
92111
;; ret
93-
;; 110: .byte 0x1f, 0xc1, 0x00, 0x00
112+
;; 15c: stur x2, [sp, #0x10]
113+
;; 160: mov w3, #0
114+
;; 164: bl #0x384
115+
;; 168: ldur x2, [sp, #0x10]
116+
;; 16c: bl #0x3bc
117+
;; 170: .byte 0x1f, 0xc1, 0x00, 0x00
118+
;; 174: .byte 0x1f, 0xc1, 0x00, 0x00
119+
;; 178: .byte 0x1f, 0xc1, 0x00, 0x00

0 commit comments

Comments
 (0)