Skip to content

Fix rare C2 crash (remap state recovery in exit trampolines); support tagged pointers (TBI) - #4

Merged
losfair merged 3 commits into
mainfrom
fix/c2-remap-recovery-and-tbi
Oct 25, 2026
Merged

losfair merged 3 commits into
mainfrom
fix/c2-remap-recovery-and-tbi

Conversation

@losfair

@losfair losfair commented Oct 25, 2026

Copy link
Copy Markdown
Owner

1. Rare HotSpot C2 crash: wrong register values after an interrupt in an exit trampoline

About 1 in 60 javac runs crashed in C2 (PhaseIdealLoop::dom_depth), dereferencing 0xf941c000b0004680. Both halves of that value are AArch64 instructions. The register dump showed x24 == libjvm+0xb3d454, the return address of the preceding bl. So guest x24 had received guest x30's value.

Cause. The translator remaps hot unpinned registers into unused pinned host registers inside a block (here x24 into RSI, which normally holds x30).

  • State recovery decides whether a host IP is remapped from the last REMAP point at or before it.
  • Exit trampolines are emitted after the main code. When a block ends in a remapped self-loop back-edge, the scan reports "remapped" for every trampoline offset.
  • Every path into a trampoline has already run the remap epilogue. So a thread interrupted there got each candidate from its victim's host register: x24 ← RSI.
  • This only happens on an exit's first, unchained run, and needs an epoch change or a signal at that exact moment, which is why it was so rare.

Fix: emit a canonical REMAP 0 point at the start of the trampolines. A new unit test recovers state at a trampoline of a block that ends remapped. It failed before the fix and passes now.

2. Top-byte-ignore (tagged pointers)

arm64 Linux ignores bits 63:56 of user data addresses. Under kemu, a tagged load died with SIGSEGV/SI_KERNEL and address 0, because a tagged address is non-canonical on x86 and raises #GP.

  • Interpreter: every data access goes through an Env adapter that strips the tag. Fault addresses are reported untagged, as arm64 Linux does.
  • Translated code doesn't mask addresses, so untagged programs pay nothing.
    • A new hook in exc_general_protection (kemu_user_gp, in 0009-kemu-x86-fault.patch) recovers the guest state and runs the instruction in the interpreter.
    • It either continues, or delivers SEGV_MAPERR/SEGV_ACCERR with the address. That also fixes addresses beyond the 48-bit VA range, which previously reported address 0.
  • Adaptive: after 8 such faults, a block is retranslated with address masking (Cfg::tbi).
    • The base register is copied and untagged; writeback keeps the tag.
    • Atomics and exclusives in that mode use the interpreter helper.
    • These translations bypass the shared cache.
  • Not supported: the tagged-address syscall ABI (PR_SET_TAGGED_ADDR_CTRL). Syscalls given tagged pointers return EFAULT, which is also arm64's default behaviour.

Also adds kemu.stress=<n>, a debug knob that forces state recovery at random points.

Testing

  • kemu_core unit tests: 28/28. The new trampoline-recovery test fails without fix 1. The new interpreter TBI test is also included.
  • jit-test, JIT vs interpreter: 0 failures in plain, INTR, RELOC and new TBI=1 modes. TBI=1 uses tagged base registers and TBI-mode translation.
  • New tests/kernel/tbi, also added to vm-smoke.sh. It covers plain, register-offset, pair, SIMD and atomic accesses; writeback keeping the tag; a hot loop that gets retranslated (20M tagged load/store pairs in 0.05 s); and a fault through a tagged pointer. It passes with the JIT and with kemu.jit=0.
  • ABI suite passes, including the new sigsegv-high check.
  • vm-smoke.sh and vm-jit-stress.sh 2 2 pass locally on the final kernel. The kemu.stress=50 variant was still running when this PR was opened; CI runs both.
  • After fix 1 alone: 40/40 javac runs on fresh boots, and both stress variants pass. The crash was too rare to reproduce on demand, so the unit test is the direct proof.

🤖 Generated with Claude Code

https://claude.ai/code/session_01EkLScjRgtBQ8aqLMVan3KC

losfair and others added 3 commits October 25, 2026 00:08
A thread interrupted inside an exit trampoline of a remapped block was
recovered as if still remapped: the trampolines are emitted after the
main code, so the last REMAP point before them is the main code's, which
is "remapped" when the block ends in a self-loop back-edge. Every path
into a trampoline has run the remap epilogue, so this gave each remap
candidate its victim's host register and the victim its stale slot.

Seen as a rare (about 1 in 60 javac runs) crash in HotSpot's C2 compiler:
x24 (remapped into RSI) received x30's value, the return address
libjvm+0xb3d454, and PhaseIdealLoop::dom_depth then read its fields from
code. Interrupts land in trampolines only on an exit's first, unchained
run, and need an epoch change or signal at that moment, hence the rarity.

Emit a canonical REMAP 0 point at the start of the trampolines, with a
unit test that recovers at a trampoline of a block ending remapped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EkLScjRgtBQ8aqLMVan3KC
arm64 Linux ignores bits 63:56 of user data addresses (TBI); under kemu a
load through a tagged pointer died with SIGSEGV (si_code SI_KERNEL,
address 0), since such an address is non-canonical on x86 and raises #GP.

- Interpreter: every data access goes through an Env adapter that strips
  the tag (bits 63:56 := bit 55). Fault addresses are reported untagged,
  as arm64 Linux does without SA_EXPOSE_TAGBITS; IC IVAU is untagged too.
- Translated code doesn't mask, so untagged programs pay nothing. A new
  hook in exc_general_protection (kemu_user_gp) recovers the guest state
  and runs the instruction in the interpreter: it either completes, or
  the fault is delivered as SEGV_MAPERR/SEGV_ACCERR with the address
  (this also fixes addresses beyond the 48-bit VA range).
- A block that takes 8 such faults is retranslated with Cfg::tbi: the
  base is copied and untagged (shl 8; sar 8), writeback keeps the tag,
  atomics/exclusives use the helper. These blocks bypass the xcache.
- Tests: tests/kernel/tbi (access forms, writeback, SIMD, atomics, a hot
  loop that gets retranslated, tagged fault), an ABI check for addresses
  beyond the VA range, an interpreter unit test, and TBI=1 in jit-test
  (tagged base registers, TBI-mode translation vs. the interpreter).

The tagged-address syscall ABI (PR_SET_TAGGED_ADDR_CTRL) is not
supported; syscalls given tagged pointers fail with EFAULT as on arm64
by default.

Also adds the kemu.stress=<n> knob's history to DESIGN.md.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EkLScjRgtBQ8aqLMVan3KC
@losfair
losfair merged commit 9ee0d5d into main Oct 25, 2026
2 checks passed
@losfair
losfair deleted the fix/c2-remap-recovery-and-tbi branch October 25, 2026 07:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant