Skip to content

fix: preserve FP control state across SysV context switch, expand tests - #3

Merged
itsakeyfut merged 9 commits into
mainfrom
fix/sysv-fp-and-test-coverage
Jul 22, 2026
Merged

fix: preserve FP control state across SysV context switch, expand tests#3
itsakeyfut merged 9 commits into
mainfrom
fix/sysv-fp-and-test-coverage

Conversation

@itsakeyfut

Copy link
Copy Markdown
Owner

Summary

Fix a latent SysV bug where the context switch leaked FP control state between fibers, and expand the test suite to cover the core preservation and scheduling guarantees on both platforms.

Changes

  • Fix (src/arch/x86_64_sysv.zig): The SysV switch saved only the six callee-saved general-purpose registers, but the MXCSR control bits and the x87 control word are also callee-saved under the SysV ABI. A fiber that changed the rounding mode or FP exception masks could leak that state into whatever ran next. The switch now saves/restores both in a 16-byte slot and initStack seeds the defaults, mirroring the Windows path (MXCSR at slot+0, x87 CW at slot+8).
  • Tests (src/fiber.zig): eight new inline tests — cross-platform FP control-state preservation (the regression test for the fix), callee-saved GP registers, nested fibers, deep-stack yields, many yields, local-variable integrity, interleaved independent fibers (with a per-round assertion that positively confirms interleaving), and allocation-failure cleanup. The xmm6 test stays Windows-only by design (xmm6–15 are callee-saved only under Win64).
  • CHANGELOG: ### Fixed for the SysV fix, ### Added for the coverage.

No public API changes.

Notes

The SysV fix's runtime behavior is proven by the ubuntu-latest CI job, not locally: only the Windows target runs on the dev machine, and the Windows switch already preserved FP state, so the cross-platform FP test passes locally both before and after the asm change. On Linux, that test exercises the SysV switch and would fail without this fix.

Test Plan

  • zig build compiles cleanly
  • zig build test passes (11 tests, Windows)
  • zig build examples runs both examples
  • zig fmt --check . passes
  • CI green on ubuntu-latest (the real proof of the SysV fix) and windows-latest

The SysV switch saved only the six callee-saved general-purpose registers,
while MXCSR control bits and the x87 control word are also callee-saved under
the SysV ABI. Mirror the Windows path: save and restore both in the switch and
seed defaults in initStack, so rounding mode and FP exception masks no longer
leak between fibers.

Replace the Windows-only mxcsr test with a cross-platform FP control-state
test that now guards this on both platforms.
After one resume each, every fiber's counter must be exactly 1. Serial
run-to-completion would drive the first fiber to 3 before the second started,
so this positively confirms cooperative interleaving rather than only checking
the final per-fiber totals.
@itsakeyfut itsakeyfut self-assigned this Jul 22, 2026
The new asm-based tests only compiled on Windows. x86_64-linux and
x86_64-windows accept different inline-asm idioms, and these tests had no
comptime target guard (unlike the Windows-only xmm6 test), so they first
compiled on the Linux CI target and failed:

- The FP-control test's helpers used the register-address-dereference form
  (stmxcsr (%[o]) with an "r" operand), a Zig 0.16 x86_64-windows workaround
  that the Linux assembler rejects as an invalid memory operand. Split the
  helpers by target at comptime: Windows keeps the workaround, other targets
  use the standard "m"/"=m" memory constraint.
- The GP-register test used movabsq, an invalid mnemonic on the Linux
  assembler. Remove that test: it was already flagged as relying on compiler
  register scheduling, and callee-saved register preservation is covered by the
  behavioral tests (nested, deep-stack, local-integrity, interleaved).
@itsakeyfut
itsakeyfut merged commit 40e0009 into main Jul 22, 2026
2 checks passed
@itsakeyfut
itsakeyfut deleted the fix/sysv-fp-and-test-coverage branch July 22, 2026 13:22
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