fix(libSceFiber): pin fiber contexts writable under write tracking on Windows #283
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/283"
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 SP4C3B4R-8 on 2026-10-01T21:57:06Z.
What
Fiber contexts live in guest memory. When write tracking had armed such a page read-only, a push on the fiber stack faulted and Windows could not dispatch the exception, because the exception frame is written to the same protected stack, so the process was terminated without any report. Fiber contexts are now pinned writable (
GuestArenaPinWritable_nid_postfix) for their lifetime; pinned pages are never armed and are always collected as written. Initialising a fiber object again without finalising it unpins the previous context first.Relation to #637
#637 keeps direct memory mapped at a single address in
MEM_WRITE_WATCHmemory, which is never write-protected. After #637 this PR only matters for fiber contexts in pages mapped at two or more addresses. In the title I tested, all 13 pins (9 distinct contexts, 1 MiB each) are in pages with a single mapping, so with #637 that title would not need the pinning. Whichever PR lands second gets rebased (both touchWindowsMappings.hpp). If #637 lands first, I'm fine with closing this one, or keeping it for aliased contexts.Tested
Windows 11, MinGW-w64 GCC 15.2.0 (winlibs, ucrt posix seh). Full build;
ctestpasses. New caseCheckPinnedSharedPagesincore/libs/tests/GuestMemory.cpp(Windows only). It maps direct memory and pins its middle page. The pinned page must stayPAGE_READWRITEand be reported on every collect. Its unpinned neighbour must be armed and reported once per write. AfterUnpinand a collect, the page must be armed again. The case fails if pinning is removed, or if collects arm pinned pages. Checked with a retail title: without this change it terminates silently (0xC0000005, no report) 39–57 s after start in 4 of 4 runs, with it it runs on (2 of 2, 120 s). Details in the comments.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-06T21:20:48Z.
Thanks. Which retail title did you check this with, and does it still terminate on current main without this PR? The branch is from 10-01, and CI hasn't run on it. A rebase onto main, plus the title ID and the point where it terminates, would show that both fixes (the TIB bounds order and pinning the fiber contexts) are still needed.
Imported from boykopovar/AnyPS5. Originally posted by SP4C3B4R-8 on 2026-10-06T21:35:29Z.
@oneandonlydean PPSA21564 v01.007.000, on Windows 11, Ryzen 7 7800X3D, RTX 5070 Ti. What I have so far is from your
astrobotat9121abeb(2026-10-03), before you pulled #282–#284 in: without these commits the process ended silently (no fault report, no log line) about 17 s after start, and with them it ran to the title. I didn't isolate the two fixes of this PR from each other or from #282 and #284 in that run, and I haven't tried currentmainyet.I'll rebase onto current
mainso CI runs, then check on currentmain(with #285 so it gets past the GDS stop) without this PR, and each fix alone, and post the title ID, the point where it terminates and the result of each. Tomorrow at the latest.Written with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by SP4C3B4R-8 on 2026-10-07T09:31:23Z.
@oneandonlydean results. PPSA21564 v01.007.000, Windows 11, Ryzen 7 7800X3D, RTX 5070 Ti, MinGW-w64 GCC 15.2.0,
APS5_HOST_IMPORT_MIB=20480, write watching on (default), boot without-lvl, 120 s per run,APS5_TRACE_FIBER=1.Current
main(219a1cf8) + #285, without this PR: it stops before the point where this matters. 2 of 2 runs end at 37–46 s withcompute shader 0x5005f6100: AGC driver: guest memory is not readable at 0x30 (range 0x30+0x4)(the #479 / #759 class; #759 doesn't merge cleanly ontomainnow, so I didn't stack it). By then the title has only initialised 6 fibers and returned from them 10 times.Your
astrobot39b59157, which gets past that stop, withAPS5_NO_SNAPSHOT_CHECK=1(see below):2aaedccb)2aaedccbreverted0xC0000005, no fault report, no log line; the last fiber event is alwaysreturn Resident, right before the title initialises its next fibers (Armadillo,Transition)0xC0000005at 40–41 sSo only the pinning is needed. That makes sense: the bounds order only matters if a push on the fiber stack faults, and with the contexts pinned writable those pages are never armed, so the push doesn't fault. I've dropped the bounds change: the branch is now one commit on current
main, pinning only (57588cf2, title updated). Full build passes;ctest382 of 383, the one failure beingagc_driver_scalar_sop1(movrelsd_2_b32), a recompiler test this change doesn't touch, which @MichonGoddijn231849 also reports failing onmain. CI should run on the push.Separately: without
APS5_NO_SNAPSHOT_CHECK,39b59157as is stops at boot in 2 of 2 runs, about 40 s in, withcompute shader 0x5005f6100: AGC graphics: guest snapshot differs from registered memory. Same shader as themainstop above. I'll put that in #357.Written with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T13:24:03Z.
Review: CHANGES_REQUESTED
Thanks for isolating the pinning from the bounds change, the 4-way table makes the cause clear. Code reading checks out (the only arm is behind
clear && !pinned, and Pin/Unpin invalidate the clean ranges). Two things before merge:core/libs/tests/GuestMemory.cppalready has_WIN32cases usingGuestArena, so a case there would run on the Windows job: map a direct memory range,GuestArenaPinWritable_nid_postfixpart of it,GuestArenaCollectWrites_nid_postfix(..., clear=true)twice, then check that the pinned page is stillPAGE_READWRITE(VirtualQuery) and is reported on both collects, while an unpinned page in the same range is armed and only reported once; afterUnpin+ collect it gets armed. It should fail without the change.MEM_WRITE_WATCHmemory and is never write-protected, so the fiber-stack fault only remains for pages mapped at two or more addresses. The two PRs conflict textually inWindowsMappings.hpp; whichever lands second needs a rebase. Please say in the description whether pinning is still needed on top of #637 (i.e. whether the title's fiber contexts are in aliased pages), so it's clear what this PR covers after both.Minor:
sceFiberInitializeon an object that is already initialised pins again without unpinning, so those pages stay pinned (reported as written on every collect) for good. Unpinning the previous context whenmagic == FIBER_MAGICwould avoid it, if a title re-initialises without finalising.Imported from boykopovar/AnyPS5. Originally posted by SP4C3B4R-8 on 2026-10-07T20:08:37Z.
@adriadam10 thanks, all three are in
78cb1598:CheckPinnedSharedPagesincore/libs/tests/GuestMemory.cppruns on the Windows job. It maps 3 pages of direct memory and pins the middle one. The pinned page staysPAGE_READWRITE(VirtualQuery) and is reported on every collect withclear=true. The unpinned neighbour is armed and reported once per write. AfterUnpinand one collect, the middle page is armed again and reported once on its next write. It fails with++page.pinsremoved, and it also fails if the collect arms pinned pages (clear && !pinnedturned back intoclear).sceFiberInitializeon an object whose magic is stillFIBER_MAGICnow unpins the previous context first.Written with AI assistance (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T20:25:22Z.
Review: APPROVED
Thanks, all three points are addressed. Checked: the PR merged onto current main builds and passes ctest on Linux (400/400). In code review, a pinned page is never armed (
clear && !pinned), Pin/Unpin invalidate the clean ranges, and the expected counts inCheckPinnedSharedPagesmatch the 16 KiB view / 4 KiB report granularity. The Windows CI job runs the new case. Merging once Windows and Linux CI are green. #637 will need a rebase onWindowsMappings.hppafter this.