feat(libc): implement fesetenv #8937
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8937"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-21T06:42:18Z.
What
Adds missing
fesetenv(Q-bLp+b-RVY) to shared libc. It restores the saved 28-byte x87 environment and independent MXCSR from the existing 32-byte guest layout, preserving errno and leaving pending x87 exception delivery until a subsequent waiting instruction.The signature, layout and restore sequence follow the pinned public SDK header and FreeBSD reference. The name appears in the firmware export inventory and its NID is independently corroborated by
sce_symbols. Null inputs and bits unsupported by the current host'sFXSAVEMXCSR mask throw before changing state; a zero reported mask uses the architectural0xffbffallback; console fault behavior is unverified. Intel documents these reserved MXCSR bits. Console ABI/behavior and module/library versions remain unmeasured; the separate__fe_dfl_envobject remains missing, with its console initializer unknown.Tested
Windows x86-64, MinGW GCC15.2 Release; no game run. Baseline
ec7eacc62d5839aa3134e43d347485c923db07d1, candidate6e43cea19323048ab02875e7a43fa3729a048bed.fegetenvround trip; errno; two-thread isolation; read-only input ending exactly at guard page; null/host-unsupported bits preserve state.fwaitdelivers a pending enabled x87 exception. Windows checks saved active cause and exception code; denormal accepts INVALID_OPERATION only with solely the saved denormal cause (matching prior native hardware checks). POSIX handler checks SIGFPE/return ordering but not saved cause; Linux/macOS were not run here.libsand affected test targets build. Final CTest 10/10: new state test, six deferred-exception cases,guest_math,guest_math_exact,patched_libraries.357 patched libraries load,0 failures; this is loader coverage, not behavior coverage for those libraries.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:50:06Z.
Review: COMMENTED
One edge case to consider before merge: the guard only rejects MXCSR bits 16–31. The architectural MXCSR_MASK can also report unsupported bits within the low 16 (notably DAZ bit 6);
ldmxcsrwith one of those bits set raises #GP on a host that does not support it. I saw the test query the host mask before testing DAZ, but the implementation does not appear to use that mask to validate incoming environments. Does AnyPS5 require DAZ on every supported host, or shouldfesetenvreject an input containing host-unsupported bits beforefldenv/_mm_setcsr? The console behavior for malformed environments can remain documented as unverified.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:52:32Z.
I verified the concern against AMD’s current architecture manual: it says DAZ support is indicated by
MXCSR_MASKand recommends masking writes with the returned/default mask to avoid #GP. The project’s required AVX2 feature set does not itself check this per-register mask. Source: AMD64 Architecture Programmer’s Manual, Vol. 2.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T06:58:59Z.
Review: COMMENTED
Linux verification found a flaky failure in the new thread test. The build succeeds; all six
guest_set_env_pending_*cases pass.guest_set_envfailed atthread-local errnoon the first run and again on the fourth run ofctest --repeat until-fail:20; adding a diagnostic print made it pass once. InThreads(), each worker sets errno before the firststd::barrier::arrive_and_wait(), then checks it after a second barrier. Since the intended assertion is thatfesetenvpreserves errno, set the sentinel immediately after the first barrier and check it immediately aftersetEnvironment, before the second barrier. That removes unrelated synchronization calls from the errno assertion.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T07:04:57Z.
I made the errno test adjustment on my fork at
codex/fix-fesetenv-errno-test. It sets the sentinel after the first barrier and snapshots errno immediately afterfesetenv, before the second barrier. On Linux, the focused target builds;guest_set_envpasses 20 consecutive runs, and all seven environment tests pass.git diff --checkandtools/check_conventions.py --base upstream/mainpass. I could not push directly to this PR head (GitHub returned 403 for the fork branch), so the commit is available to cherry-pick if useful.Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-21T07:08:24Z.
Confirmed the mask gap. I found no explicit DAZ requirement in the project's host CPU checks, so the implementation now validates against the actual
FXSAVEMXCSR_MASK before changing either environment. A zero report uses the architectural0xffbffallback, following Intel's guidelines. Commit40add4dc2fb77a3096d0fa080adf94c779ba8e8b.I added the regression before changing production behavior. The existing DLL failed 4 of 15 modelled-mask cases (DAZ accepted under 0/0xffbf). The same probe executable passes 15/15 on the correction; it substitutes only the FXSAVE report in its own process. This is modelled capability coverage, not evidence from a physical CPU lacking DAZ. The actual function's rejection path and unchanged-state checks run natively.
The upstream test now tries every bit unsupported by the actual host mask. Full Windows libs build, all 10 native/loader cases, and 357 library loads pass. The final identical test binaries still give 7 missing-export failures/2 math passes against the original missing-export baseline, then 9/9 on the candidate. Independent review, conventions and whitespace checks pass; 16 base-conflicted overlap warnings were reviewed without a same-line/export conflict. Linux/macOS/console behavior remains unmeasured here. PR description and debt were updated; AI-assisted remains yes.
For reproduction, the source below takes a libc path and optionally the current function's FXSAVE instruction offset, memory displacement, instruction length and register index. Decode those from the exact candidate disassembly; for this GCC15.2 build they are
0x1b 0x30 5 4(RSP). Without a FXSAVE site, the initial implementation runs with no substitution and fails the four expected mask cases. The fixture patches only its own mapped image and restores it; no on-disk DLL or other process is modified.Modelled-host probe source
Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-21T07:14:42Z.
Fixed in
6e43cea19323048ab02875e7a43fa3729a048bed. The sentinel is now set after the first barrier and errno is captured immediately afterfesetenv, before the second barrier. The assertion checks that captured value. No library implementation changed in this commit.I reproduced the assertion flaw deterministically on Windows by compiling a temporary copy of the test with an errno write after each barrier. The old test fails at
thread-local errno; the repaired test passes with the identical injected side effects. The normal Windows main test then passed 20 consecutive repetitions, followed by 10/10 native/loader cases and 357 libraries loaded. Final identical binaries against the original missing-export baseline still give 7 failures/2 math passes, then 9/9 on the candidate. Conventions and whitespace checks pass.For a source-only reproducer, apply this transformation to a temporary copy of
GuestSetEnv.cppfrom each revision and compile it with the same includes, libc import library, Windows unwind/exit settings as the test target:Your Linux result is recorded as attributed review evidence; I have not run Linux here. A rerun on this head would verify that the reported flake is resolved on that host. The PR description now records this test correction and its validation; AI-assisted remains yes.
Imported from boykopovar/AnyPS5. Originally posted by UnhealthyKraken on 2026-10-21T07:34:32Z.
Thanks for the Linux verification and the test-fix commit. I had already applied the adjustment from your review in
6e43cea193before seeing this fork commit; the assertion window is equivalent, with only the captured variable name differing.I’ve recorded your 20 consecutive Linux runs and seven passing environment tests against your fork commit. Its parent is
05d3768, so that evidence covers the errno-test repair, while the later host-mask correction still has the separately documented Windows and modelled-mask validation. This preserves the source-head distinction and credits your contribution without claiming an own Linux run.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:24:58Z.
Review: CHANGES_REQUESTED
Re-check — changes requested
The new commit
test(libc): isolate fesetenv errno from synchronizationappears to implement my earlier test-isolation suggestion: the errno sentinel is set after the first barrier and checked immediately afterfesetenv, before the next barrier. The intervening synchronization no longer contaminates that assertion.I checked the exact PR head
d2386a7a78fca58ae72f0e2d4a1a8373c9e19750and hosted status. Windows and the register-context jobs pass, but the Linux product-test job is skipped. The earlier Linux run failedguest_set_envtwice in 20 repetitions, so the fix still needs an exact-head Linux run ofguest_set_envrepeated enough to show the race is gone. Please also refresh onto current mainaaccd0ba39b7; this branch's target base is10bec5c322eb.I did not run the C++ test locally. I am not approving the previously flaky test until the focused Linux result is reported.