Commit 9d730ce
committed
Debug: add some infrastructure for catching traps, and handle traps properly in Pulley.
This is a followup to #11895 where I had disabled a test that failed to
emit a debug event for a hostcall-generated trap on a divide-by-zero in
Pulley. This PR allows that test to pass, and brings Pulley back to
parity with native Cranelift in debug support currently.
This was a bit of a "start to pull the thread and the entire finished
mechanism materializes" PR; happy to consider ways to split it up if
needed. In short, disabling signal-based traps on a Pulley configuration
still relies on Pulley opcodes (e.g., divide) actually trapping, in a
way that looks more like a "native ISA trap"; so I had to start to build
out the actual trap-handling mechanisms. In any case, this will all be
needed for followup work soon that will handle traps on native platforms
(redirecting from signals by injecting calls), so this is not a
distraction.
This PR includes, ranked in decreasing order of "may scare other
Wasmtime maintainers" score:
- A raw `NonNull<dyn VMStore>` in the `CallThreadState`, with a long
comment about provenance and mut-borrow exclusivity. This is needed
right now to allow the interpreter to invoke the debug event handler,
but will soon be needed when injecting hostcalls on signals, because a
signal context also has no state available from the Wasm code other
than what is in TLS. Hence, we need a way to get the store back from
the Wasm when we do something that is "morally a hostcall" at a
trapping instruction.
I do believe this is sound, or at least close to it if not (please
scrutinize carefully!); the basic idea is that the Wasm acts as an
opaque blob in the middle, and the pointer comes out of it one way or
another (the normal way, as the first arg to a hostcall, or the weird
way, via TLS and the CallThreadState during a trap).
Exclusive ownership is still clear at any given point and only one
`&mut` ever exists in the current frame at a time. That said, I
haven't tested with miri yet.
This does require careful thought about the Wasm compilation, too; we
need the moral equivalent of a `&mut self` reborrow as-if we were
making a hostcall on each trapping instruction. It turns out that we
already treat them as memory-fence instructions, so nothing loaded
from the store can be moved or cached across them, and I've added a
comment now about how this is load-bearing.
- Updates to `CallThreadState`'s "exit state", normally set by the exit
trampoline, that we now also set when we invoke a debug event handler
during a trap context[^1] so that `Store::debug_frames` properly sees
the current activation. This is a little more awkward than it could be
because we store the *trampoline* FP, not last Wasm FP, and there is
no trampoline frame in this case, so I've added a flag and some
conditionals. I'm happy to refactor instead to go (back) to storing
the last Wasm FP instead, with the extra load in the exit trampoline
to compute that.
- A whole bunch of plumbing, creating a large but mechanical diff, in
the code translator to actually add debug tags on all traps and calls
to `raise`. It turns out that once I got all of the above working in
Pulley, the test disagreed about current Wasm PC between native and
Pulley, and Pulley was right; native was getting it wrong because the
`raise` libcall was sunk to the bottom in a cold block and, without
tags, we scanned backward to pick up the last Wasm PC in the function.
This new plumbing and addition of tags in all the appropriate places
fixes that.
[^1]: I keep saying "during a trap context" here, but to avoid any
signal-safety scares, note that when this is done for native
signals in a followup PR, we will inject a hostcall by modifying
stack/register state and returning from the actual signal context,
so it really is as-if we did a hostcall from a trapping
instruction.1 parent dbc21cd commit 9d730ce
File tree
18 files changed
+674
-228
lines changed- cranelift/codegen/src
- crates
- cranelift/src
- func_environ
- gc
- enabled
- translate
- wasmtime/src/runtime
- vm
- traphandlers
- tests/all
18 files changed
+674
-228
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
150 | 150 | | |
151 | 151 | | |
152 | 152 | | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
153 | 159 | | |
154 | 160 | | |
155 | 161 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
| 87 | + | |
87 | 88 | | |
88 | 89 | | |
89 | 90 | | |
90 | 91 | | |
91 | 92 | | |
92 | | - | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
93 | 96 | | |
94 | 97 | | |
95 | 98 | | |
| |||
113 | 116 | | |
114 | 117 | | |
115 | 118 | | |
| 119 | + | |
116 | 120 | | |
117 | 121 | | |
118 | 122 | | |
| |||
123 | 127 | | |
124 | 128 | | |
125 | 129 | | |
126 | | - | |
| 130 | + | |
127 | 131 | | |
128 | 132 | | |
129 | 133 | | |
| |||
148 | 152 | | |
149 | 153 | | |
150 | 154 | | |
| 155 | + | |
151 | 156 | | |
152 | 157 | | |
153 | 158 | | |
| |||
177 | 182 | | |
178 | 183 | | |
179 | 184 | | |
| 185 | + | |
180 | 186 | | |
181 | 187 | | |
182 | 188 | | |
| |||
298 | 304 | | |
299 | 305 | | |
300 | 306 | | |
301 | | - | |
| 307 | + | |
302 | 308 | | |
303 | 309 | | |
304 | 310 | | |
| |||
308 | 314 | | |
309 | 315 | | |
310 | 316 | | |
311 | | - | |
| 317 | + | |
312 | 318 | | |
313 | 319 | | |
314 | 320 | | |
| |||
430 | 436 | | |
431 | 437 | | |
432 | 438 | | |
| 439 | + | |
433 | 440 | | |
434 | 441 | | |
435 | 442 | | |
| |||
464 | 471 | | |
465 | 472 | | |
466 | 473 | | |
| 474 | + | |
467 | 475 | | |
468 | 476 | | |
469 | 477 | | |
| |||
513 | 521 | | |
514 | 522 | | |
515 | 523 | | |
| 524 | + | |
516 | 525 | | |
517 | 526 | | |
518 | 527 | | |
| |||
558 | 567 | | |
559 | 568 | | |
560 | 569 | | |
| 570 | + | |
561 | 571 | | |
562 | 572 | | |
563 | 573 | | |
| |||
575 | 585 | | |
576 | 586 | | |
577 | 587 | | |
578 | | - | |
| 588 | + | |
579 | 589 | | |
580 | 590 | | |
581 | 591 | | |
| |||
603 | 613 | | |
604 | 614 | | |
605 | 615 | | |
| 616 | + | |
606 | 617 | | |
607 | 618 | | |
608 | 619 | | |
| |||
756 | 767 | | |
757 | 768 | | |
758 | 769 | | |
| 770 | + | |
759 | 771 | | |
760 | 772 | | |
761 | | - | |
| 773 | + | |
762 | 774 | | |
763 | 775 | | |
764 | 776 | | |
| |||
0 commit comments