Skip to content

fix(coro): clobber aliased vector registers in the context switch - #557

Merged
lalinsky merged 3 commits into
mainfrom
fix/coro-vector-clobbers
Jul 9, 2026
Merged

lalinsky merged 3 commits into
mainfrom
fix/coro-vector-clobbers

Conversation

@lalinsky

@lalinsky lalinsky commented Jul 9, 2026 •

Copy link
Copy Markdown
Owner

Problem

Intermittent crash on Windows/IOCP (ReleaseSafe): the scheduler dereferences a garbage task in getNextTask, because Executor.pending_cleanup gets stomped with the undefined (0xAA) fill pattern. It never reproduced on any other backend/OS, and had nothing to do with the pending_cleanup protocol.

Root cause

The x86_64 coroutine context switch's inline-asm clobber list declared its vector registers as zmm0-31 only. On a target without AVX-512 (e.g. -mcpu baseline, which is what we build) those zmm register names don't exist, so LLVM silently drops the clobbers and never marks the aliased xmm0-15 as clobbered (confirmed in X86ISelLowering: a named clobber for a register that requires an absent subtarget feature resolves to nothing).

The compiler is then free to keep a live value in an xmm register across the switch. On Windows x64, xmm6-15 are callee-saved, so it readily parks loop-invariants there — e.g. a .none union constant it will store into pending_cleanup. The switch destroys xmm6, and the garbage lands in pending_cleanup, later read as a bogus .park/.reschedule and crashing the scheduler. On Linux/SysV xmm6-15 are caller-saved, so the compiler never keeps a value live there across a call — which is why this was Windows/IOCP-only and looked like random field corruption.

Diagnosed with WinDbg Time-Travel Debugging: a hardware data breakpoint on pending_cleanup.tag reverse-executed to self.pending_cleanup = .none compiled as movups [r13], xmm6, with xmm6 = 0xAAAA… (undefined fill), and no reload of xmm6 after the switch (while r12/r13/r15 were reloaded — their clobbers were honored).

Fix

Add the aliased vector registers explicitly to each arch's context-switch clobber list, so the compiler can never keep a live value in a register the switch destroys:

  • x86_64: add xmm0-15 and ymm0-15 (alongside the existing zmm0-31). This is the actual bug fix.
  • aarch64: add NEON v0-31 (alongside the existing SVE z*). Defensive — LLVM already honors the z* clobbers as the aliased v*/d* even without SVE, so this is belt-and-suspenders.
  • riscv64/riscv32: add RVV v0-31. Defensive.
  • loongarch64: add LSX vr0-31 and LASX xr0-31. Defensive.

On a baseline build the wider/feature-gated names are absent and LLVM drops the extra clobbers (exactly as it did for zmm on baseline), so codegen is unchanged there. On a feature-enabled build (AVX/SVE/RVV/LSX/LASX) the clobber is honored and a vector value held across the switch is preserved.

Verification

  • Codegen-proven (x86_64): an empty asm clobbering .zmm0 on baseline leaves the value in xmm0 (clobber dropped); clobbering .xmm0 emits a preserving movaps first. The self-hosted (Debug) backend honors both — so the bug was LLVM/release-only, and the fix is safe on both backends.
  • Confirmed on a Windows VM: the repro (which crashed on the first run) is green after the fix.
  • Cross-compiled every touched arch on baseline (clobbers drop cleanly) and with the vector feature enabled (riscv64+v, loongarch64+lsx+lasx) — all build.
  • Native test suite shows only the pre-existing failures (same as main) — no regression.

Audit of other arches

Checked all switch clobber lists for the same aliasing gap:

  • arm/thumb (d0-31), powerpc64 (f+v+vs), sparc64 (q0-15) — all use register names present on their baselines, and no wider aliasing feature drops them. Not affected; left unchanged.
  • Only x86_64 was actually miscompiling; the aarch64/riscv/loongarch additions are defensive so a future feature-enabled build can't regress the same way.

@coderabbitai

coderabbitai Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Expands switchContext clobber lists to explicitly name SIMD/vector registers on x86_64, aarch64, riscv64, riscv32, and loongarch64, with comments that describe the relevant register coverage on each path.

Changes

SIMD clobber updates

Layer / File(s) Summary
Explicit x86_64 xmm/ymm clobbers
src/coro/coroutines.zig
The x86_64 switchContext inline-asm clobber literal now names xmm0–xmm15 and ymm0–ymm15, with comments about SSE2/AVX coverage before the wider-vector clobbers.
Explicit aarch64 v0-v31 clobbers
src/coro/coroutines.zig
The aarch64 switchContext inline-asm clobber literal now names v0–v31, with comments noting that SVE z*/p* clobbers do not cover NEON state.
Explicit RISC-V v0-v31 clobbers
src/coro/coroutines.zig
The riscv64 and riscv32 switchContext inline-asm clobber literals now name v0–v31, with comments describing baseline builds versus V-enabled builds.
Explicit loongarch64 vr/xr clobbers
src/coro/coroutines.zig
The loongarch64 switchContext inline-asm clobber literal now names vr0–vr31 and xr0–xr31, with comments describing baseline builds versus LSX/LASX builds.

Estimated code review effort: 4 (Complex) | ~45 minutes

Possibly related PRs

  • lalinsky/zio#3: Same switchContext inline-assembly clobber area in src/coro/coroutines.zig, with x86_64 register coverage changes.
  • lalinsky/zio#125: Same RISC-V switchContext assembly path and vector-register handling in src/coro/coroutines.zig.
  • lalinsky/zio#329: Same switchContext clobber-list logic, covering missing architecture-specific register state.

Poem

Registers line up, one by one,
xmm, v, vr—the work is done.
The switch spins through, no state left behind,
each arch gets the clobbers it should find.
Plain asm, less guesswork, job done right.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed It clearly describes the main change: expanding coroutine context-switch clobbers for aliased vector registers.
Description check ✅ Passed The description matches the bug, root cause, fix, and verification described in the changeset.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/coro-vector-clobbers

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

The context switch's inline-asm clobber list declared its vector registers as
zmm0-31 only. On a target without AVX-512 (e.g. -mcpu baseline) the zmm register
names don't exist, so LLVM silently drops those clobbers and never marks the
aliased xmm0-15 as clobbered (confirmed in X86ISelLowering: a named clobber for a
register requiring an absent subtarget feature resolves to nothing). The compiler
is then free to keep a live value in an xmm register across the switch; on
Windows x64 it parks values in the callee-saved xmm6-15, which the switch
destroys — e.g. a '.none' union constant that gets splatted into
Executor.pending_cleanup and later crashes the scheduler. Linux/SysV never keeps
values live in xmm across a call, so the bug only surfaced on Windows/IOCP.

Add xmm0-15 (covers the baseline SSE2 build) and ymm0-15 (AVX); zmm0-31 stay for
AVX-512. Proven at codegen: an empty asm clobbering .zmm0 leaves the value in
xmm0 (dropped); clobbering .xmm0 preserves it. Self-hosted backend honors both.

(A comptime 'clobber everything' helper was tried but over-clobbered GP
sub-registers/flags and miscompiled x86_64 release under LLVM; the explicit
xmm/ymm additions are the minimal, targeted fix.)
@lalinsky
lalinsky force-pushed the fix/coro-vector-clobbers branch from 4b0b2de to 6e6a80f Compare July 9, 2026 08:24
lalinsky added 2 commits July 9, 2026 10:37
The aarch64 switch listed only the SVE z0-31/p0-15 vector clobbers. Unlike x86's
zmm (which LLVM drops on non-AVX-512 targets), LLVM does honor the z* clobbers as
the aliased NEON v*/d* even without SVE, so this wasn't miscompiling — but relying
on that aliasing is fragile. Clobber the NEON v0-v31 explicitly so preservation
of the callee-saved v8-v15 across the switch is guaranteed regardless of how the
wide SVE names are treated (same class as the x86_64 xmm fix, #460). Vector regs
only — GP/flags are unchanged.
Mirror the x86_64 xmm/ymm fix (#460) on the remaining vector-capable
arches. The context-switch inline asm must clobber every register the
compiler could keep a live value in across the switch, including vector
registers.

- riscv64/riscv32: clobber RVV v0-v31
- loongarch64: clobber LSX vr0-vr31 and LASX xr0-xr31

These are defensive: on a baseline build the feature is absent and LLVM
drops the named clobber (as it does for x86_64 zmm on baseline), so the
codegen is unchanged. On a V/LSX/LASX-enabled build the clobber is
honored and a vector value held across the switch is preserved.
@lalinsky lalinsky changed the title fix(coro): clobber all vector registers in the x86_64 context switch fix(coro): clobber aliased vector registers in the context switch Jul 9, 2026
@lalinsky
lalinsky merged commit 1855689 into main Jul 9, 2026
26 checks passed
@lalinsky
lalinsky deleted the fix/coro-vector-clobbers branch July 9, 2026 08:55
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