Skip to content

fix(tests): compare x87 pointer words in guest_floating_point only when both images have ES set - #10937

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
TechLevelZero:fix/floating-point-thread-check-fpu-pointers
Nov 1, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
TechLevelZero:fix/floating-point-thread-check-fpu-pointers

Conversation

@TechLevelZero

Copy link
Copy Markdown
Contributor

What

guest_floating_point failed at random on AMD hosts, as #10899 describes. The test compared whole 28-byte fnstenv images. Those include the last x87 instruction pointer, its selector and opcode, and the last operand pointer and its selector (words 3 to 6). AMD CPUs save these only while an unmasked exception is pending (status ES bit; AMD64 Programmer's Manual, FXSAVE). So when the thread is switched out between two images, the restored words come back as zero.

  • A new SameEnvironment compares the control, status and tag words and MXCSR. It compares words 3 to 6 only when both images have ES set.
  • SameEnvironment is used in three checks: feholdexcept saves the full original environment, null feholdexcept preserves the environment, and the check after the worker threads are joined.
  • feholdexcept preserves x87 tags and instruction/data state is now feholdexcept preserves x87 tags. feholdexcept clears ES there (the test checks this), so those pointer words are never guaranteed on AMD.

None of the states this test builds has ES set, so in practice the pointer words are no longer compared. Keeping a pointer check that holds on AMD would need a state with ES set and an unmasked exception pending. That would be a separate change.

Fixes #10899.

Tested

guest_floating_point_tests libc.prx in a loop, idle. main and the branch were built from the same tree (Ubuntu 24.04, GCC 14.2.0, Release):

CPU main branch
AMD EPYC 7281 (Zen 1), Proxmox VM 117 / 300 0 / 300, then 0 / 1500
AMD Ryzen 7 9700X (Zen 5), bare metal, Fedora 44 88 / 300 (87 after the join, 1 in null feholdexcept) 0 / 300, then 0 / 1500
Intel Xeon Gold 6252, VM 0 / 300 0 / 300, then 0 / 1500

Reported on #10899 by others:

  • AMD Ryzen 7 PRO 5850U (Zen 3), bare metal (babyhuey): main 98 / 300, branch 0 / 600. An earlier version that only narrowed the check after the join still failed 2 / 300 there, in CheckHold; this version fixes that.
  • AMD Ryzen 9 7900 (Zen 4), Windows 11 (ycbuzi): 0 / 300 on both.

Negative controls, 30 runs each on the EPYC, all failing at the expected check:

  • x87 rounding flipped before the check after the join;
  • MXCSR FTZ flipped before the check after the join;
  • x87 rounding flipped before the null feholdexcept check;
  • MXCSR rounding corrupted in the image feholdexcept saved.

python3 tools/check_conventions.py --base origin/main passes. python3 tools/pr_overlap.py --branch fix/floating-point-thread-check-fpu-pointers reports no overlap.

Checklist

🤖 Generated with Claude Code

TechLevelZero and others added 2 commits October 25, 2026 10:57
…orker threads

The check compared all 28 bytes of fnstenv, including the last instruction
and operand pointers. AMD CPUs do not keep those across a context switch
when no exception is pending, so the check failed whenever the main thread
was switched out during join.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
AMD CPUs save the last instruction and operand pointers only while an
unmasked exception is pending (ES). A context switch between two fnstenv
images can zero them, which also failed the CheckHold and null
feholdexcept comparisons.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@EagleBearerYS

Copy link
Copy Markdown
Contributor

@TechLevelZero a data point for this one from GitHub's hosted runners. On our fork, guest_floating_point on main's test aborted at the same check twice in about 40 Linux runs of other changes, each a batch of 10 repeats:

floating-point environment: feholdexcept workers preserve the main environment

The Windows runner of both runs, also an EPYC 7763, passed, as ycbuzi found on Windows in #10899. So GitHub's Linux runners hit the case this PR handles. We haven't run this branch there.

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed exact head 8c058aa. The comparison ignores x87 instruction/data pointer fields unless both environments have ES set, while still comparing control/status/tag and MXCSR; the three whole-environment assertions use this helper, and the hold test no longer asserts pointer fields after feholdexcept clears ES. This narrows the test to state the host guarantees. Local conventions and diff checks passed. The reported AMD/Intel repeated runs are author-reported; hosted product jobs are skipped by path policy.

@github-actions github-actions Bot added fix error Closes an issue (Fixes #N) needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
@TechLevelZero

Copy link
Copy Markdown
Contributor Author

Tested 8c058aa7c merged onto main 26ba9b6b7, with the tests tools/select_tests.py selects, on main and on the merge (Linux, Vulkan validation layer on, PR-only failures rerun alone 3 times on each side):

Hardware Driver Result
lavapipe on Intel Xeon Gold 6252 Mesa 26.0.8-1ubuntu0.3 (LLVM 21.1.8) pass, no PR-only failures (362 tests)

Test suite only; no title was run.

AI-assisted: yes (Claude Code)

@UnhealthyKraken UnhealthyKraken mentioned this pull request Oct 26, 2026
12 tasks done
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 26, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

@TechLevelZero Please add the missing SameEnvironment definition before CheckHold (the current head fails to compile at GuestFloatingPoint.cpp:253), and apply the intended comparison consistently to the saved/null/worker checks described in the PR. Preserve the control/status/tag/MXCSR comparisons and compare the pointer/opcode words only under the stated ES condition. Then rebuild guest_floating_point_tests on the new head, rerun the AMD/Intel coverage, and keep negative checks showing that rounding/MXCSR/tag corruption is caught.

@dantdmdarealguy

Copy link
Copy Markdown
Contributor

The merge of main into this branch (092cf70b60) dropped the SameEnvironment definition. It conflicted with #11191's SameControlState, and the resolution kept SameControlState but left the SameEnvironment call in CheckHold. So the head no longer compiles: GuestFloatingPoint.cpp:253:13: error: 'SameEnvironment' was not declared in this scope (WinLibs GCC 15.2.0, -std=c++20 -fsyntax-only). The PR's linux and windows jobs show as skipped, so CI did not catch it. With the definition from 8c058aa7cc restored above Require, the test passes 1000/1000 on main 4a6005955f (Arch Linux WSL2, GCC 16.1.1, Intel Core i9-14900K, 48 runs in parallel). main itself also passes 1000/1000 there, because Intel never fails this test. For reference, all 4 guest_floating_point aborts on main CI since #8863 merged ran on the Linux runner reporting AMD Ryzen 9 7950X3D: runs 40189660715, 40233681137, 40405370177 and 40520584563. The last two came after #11191 and failed in CheckHold (preserves x87 tags and instruction/data state, saves the full original environment), which is the case this PR covers.

@oneandonlydean

Copy link
Copy Markdown
Collaborator

Fix-up to your PR: restored your original SameEnvironment helper from 8c058aa before its use to fix the compilation failure; no other changes.

…ment

The null feholdexcept and worker checks compared only the x87 control and
status words and MXCSR, so a corrupted tag word went unnoticed there while
the saved check caught it. All three checks now use SameEnvironment, which
also compares the tag word and compares the pointer words only when both
images have ES set.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@TechLevelZero

Copy link
Copy Markdown
Contributor Author

@oneandonlydean thanks for restoring SameEnvironment in 33815b4. 61b0692 now uses it for the other two checks as well:

  • null feholdexcept preserves the environment and feholdexcept workers preserve the main environment used SameControlState from fix(main): compare only x87 control/status words and MXCSR across thread switches #11191. That compares only the control and status words and MXCSR, so a corrupted tag word went unnoticed there. Both now use SameEnvironment, so all three checks compare the control, status and tag words and MXCSR. They compare the pointer and opcode words only when both images have ES set. SameControlState had no other users and is removed.
  • CheckHold is unchanged from your fix-up.

Tested merged onto main 5454cd2 (GCC 15, Release, Intel Xeon Silver 4214):

Check Result
guest_floating_point 300 / 300
Negative control: tag word flipped before the check after the join fails at feholdexcept workers preserve the main environment

This host is Intel, which never failed this test, so the 300 runs only show nothing regressed. I'll rerun the AMD loops (EPYC 7281, Ryzen 7 9700X) and the rounding and MXCSR controls on this head and add them here.

python3 tools/check_conventions.py --base origin/main passes.

AI-assisted: yes (Claude Code)

@TechLevelZero

Copy link
Copy Markdown
Contributor Author

The AMD runs promised above, on 61b0692 merged onto main 26fa121 (Linux, GCC 13.3, Release):

Check Host Result
guest_floating_point AMD EPYC 7281 (Zen 1) 300 / 300
Negative control: x87 tag word flipped before the check after the join EPYC 7281 fails at feholdexcept workers preserve the main environment
Negative control: x87 rounding bits flipped there EPYC 7281 fails at the same check
Negative control: MXCSR rounding bits flipped there EPYC 7281 fails at the same check

The controls were a local patch (an env var that corrupts the main thread's environment just before afterThreads is read), not part of this PR. The Ryzen 7 9700X loop is still to do.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit 98c0c91 into boykopovar:main Nov 1, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix error Closes an issue (Fixes #N)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tests: guest_floating_point fails at random on AMD hosts: "feholdexcept workers preserve the main environment"

5 participants