feat(libSceFiber): add APS5_FIBER_GUARD guard pages on Windows #4892
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4892"
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 fdfawef324 on 2026-10-13T02:15:50Z.
What
Titles place fiber contexts back to back in guest memory with nothing between them. A fiber that overflows its stack overwrites the context below it, and the title fails much later, far from the cause.
APS5_FIBER_GUARD=1is an opt-in debugging switch, implemented on Windows. With it set:_sceFiberInitializeImplmakes the lowest 4 KiB page of the contextPAGE_NOACCESS, so an overflow faults at the instruction that causes it.sceFiberFinalize, or initializing the same object again, puts back the protection the page had.mallocare usually aligned to 16 bytes only, so in such titles most contexts may get no guard;USAGE.mdsays so.sceFiberGetInfostarts its scan above it.On Linux the variable throws a not implemented error at the first initialize. A guest unmap of arena memory there maps the range again as a
PROT_NONEreservation, which cannot be told apart from the guard, and the Linux mapping and protection paths do not go through one registry. This is recorded indocs/dev/debt/libSceFiber.md.Ownership of a guard
Each guard is owned through a ticket, which the fiber stores next to its guard address. Fibers that share one context share the guard, and the last finalize removes it. Releasing a ticket that is not registered throws
std::logic_error, except once for a ticket whose guard a guest unmap or mapping dropped. So a fiber whose context was unmapped and given to a new fiber cannot remove the new fiber's guard. Sharing a guard and the last finalize check that the page is still committedPAGE_NOACCESSand throw otherwise. A fiber whose guard is refused is left uninitialized and unpinned, and a finalize that throws leaves the fiber initialized.One registry for every guest protection change and unmap
The guard is registered in
WindowsMappings. Every path that changes a guarded page goes through it:HandleWrite. A write fault in the guard page of an armed shared view used to reopen that 4 KiB page and resume the overflowing write. It now returns false for a guarded address, so the fault reaches the crash report.VirtualProtectcalls inCommit,Pin,HandleWrite,BeginHostWriteand the per-page arming inCollectgo through oneprotecthelper. It protects the pages around each guard in the range and never the guard itself, and records the requested protection for finalize to restore.Mapkeeps its direct call becauseresethas already dropped every guard in the range.Collectneither reports, settles nor arms a guarded 4 KiB page of a shared view. The guard is never taken for a CPU write, and the page's pending state from before the guard is reported after the guard is removed. Fresh collection (#4535) skips the guarded page the same way.mprotect. The libkernel loop now sends every committed region through that helper (GuestArenaProtect_nid_no_patch, which no longer requires arena memory). Before this revision, memory outside the guest arena, such as the title image, went toVirtualProtectdirectly. A guestPROT_NONEof the guarded page left itPAGE_NOACCESS, the intact check passed, and the last finalize made the page writable again with the protection read at initialize. A range that crosses the arena boundary took the same direct path.resethandles arena memory. New in this revision, the libkernelmunmapof memory outside the arena goes throughGuestArenaDecommit_nid_no_patchandGuestArenaReleaseAllocation_nid_no_patch. Both drop the guard and record its tickets. Before, an unmap outside the arena left the guard registered and the next finalize threw.Host code that calls
VirtualProtectdirectly on guest memory (the restore of guest thread stack pages at thread exit,APS5_WATCH_WRITE) is still outside the registry. The intact check reports such a change unless it leaves the pagePAGE_NOACCESS. The debt entry lists this case.Per-page write tracking (#4661) and deferred activation (#4535)
Since #4661 the tracker keeps the write generation, seen generation and write protection per 4 KiB page of a 16 KiB shared view, and arms or opens each contiguous run of pages with one
VirtualProtect. The guard sits on top of that state: every run goes throughprotect, which skips the guard and records the run's protection as the one finalize restores, andCollectleaves the guarded page out, so the guard staysPAGE_NOACCESSthrough every arm and re-arm, finalize hands the page back with the protection and armed state the tracker expects, and every page that is not a guard is tracked exactly as onmain.Guardrefuses a range that is not made of whole 4 KiB pages.Since #4535 a freshly imported shared view is not armed at import: each view keeps a per-4 KiB collected mask, the never-collected pages are kept as fresh address ranges, armed collection skips pages that were never collected, and fresh collection activates them (reports and arms them) before
MarkWrittenstamps a GPU write. A guard page is never armed, activated, reported or reopened by either path. Fresh collection leaves a guarded page out the wayCollectdoes, so it is neither reported nor activated, and it does not count the page as fresh:Guardtakes the page out of the fresh ranges, which keeps the early return when nothing else is fresh, and an inaccessible view does not fail fresh collection because of its guard. When the guard is removed or dropped, a page that was never collected is fresh again and is reported once by the next fresh collection. Every page that is not a guard keeps #4535's deferred activation.The context is pinned writable before the guard is set, so no view of the guarded page is armed while it is guarded; pinned pages other than the guard are still collected as written. A protection change is not a write for
GetWriteWatch, so the guard adds no written page.Sources: Microsoft documentation for
VirtualProtect,VirtualFreeandGetWriteWatch.Tested
OS Windows Server 2025, RTX 3060, GCC 15.2 (winlibs MinGW-W64 UCRT),
build-dev. Rebased onupstream/mainea2bbefb2, after #4661 and #4535. The rebase conflicted inWindowsMappings.hpponly, inCollect(#4535's fresh selection next to the guard skip) and in the member list; the runs below are on the rebased commit.fiber_guardrunsfiber_tests guardwithAPS5_FIBER_GUARD=1on host, flexible (write-watched) and two-view direct memory. It covers:HandleWriterefusing a fault in the guard;sceKernelMprotectkeeping the guard, and write collection around it;VirtualProtectof the guard, after which sharing and the last finalize must throw;CheckGuardProtectedOutsideArenaandCheckGuardUnmappedOutsideArena, a guestmprotectandmunmapof a guarded context outside the guest arena.CheckGuardedSharedContextthe pinned four-view context is collected as 15 pages, none of them in the guard (16 before, the guard included), and a write to the former guard page after finalize is collected as one page in each view (4 before, the whole view).CheckSharedWritesAroundGuardinguest_memoryguards the lowest 4 KiB page of a freshly mapped 16 KiB shared view, writes the other three pages, collects and re-arms. The guard must stayPAGE_NOACCESS, a fault in it must not be resumed, and exactly the three written pages must be reported, in two rounds. After the guard is removed, the page is reported once, because it was never collected (Collectkept that state while it was guarded), then armed, and a write to it is reported alone. A second guard on the now armed page must survive a pin of the whole view.CheckFreshAroundGuardinwindows_write_watchwrites the second 4 KiB page of a freshly mapped shared view, guards it and collects fresh. The other fifteen pages must be reported and activated, and the guard must stayPAGE_NOACCESSthrough the activation and a re-arm of the page beside it. The guarded page must be neither reported, activated nor counted as fresh, also in aPROT_NONEview, where it must not make fresh collection fail. After the guard is removed, the page must be reported once by fresh collection, activated, and a write to it reported.VirtualProtectdirectly, as a plain take of themainside of the conflict gives,guest_memoryfails at the check after the pin (GuestMemory.cpp:1379).Collectnot leaving the guard out,guest_memoryfails at the first collect (GuestMemory.cpp:1364), andfiber_guardfails atthe pinned shared context but its guard page is collected as written.Guardleaves it in the fresh ranges),windows_write_watchfails atfresh collection reported a guarded page or lost a fresh one. With only the last two removed it fails atguarded page made an inaccessible view fresh, and without putting the page back in the fresh ranges atUnguardit fails atunguarded page was not reported as fresh.ctest -j 4):fiber,fiber_guard): 2/2.log_macros, which also fails onmain.guest_memoryincluded: 122/122.windows_write_watch: 1/1.agc_write_tracking: 1/1.agc_shared_write,agc_shared_write_partial_collect,agc_shared_write_partial_collect_two_pass: 3/3.check_conventions.pyfromupstream/mainpasses.Export.cppand the non-Windows branch ofFiber.cppdid not change in this revision.agc_write_trackingandagc_shared_write*were not run.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T05:57:04Z.
Rebased onto
ea2bbefb2, after #4661 (per-4 KiB write tracking of shared views) and #4535 (deferred write tracking for fresh Windows shared imports, with b24e1cc46's early return). The rebase conflicted inWindowsMappings.hpponly.protectSubpagesthrough the guard-awareprotect.protectprotects the pages around a guard and never the guard itself, and it records each run's protection for finalize.Collectneither reports, settles nor arms a guarded 4 KiB page.Guardrefuses ranges that are not whole 4 KiB pages. Pages that are not guards are tracked exactly as onmain.Collectdoes. It neither activates nor reports it, and does not count it as fresh:Guardtakes the page out of the fresh ranges, so the early return still applies when nothing else is fresh, and the check of an inaccessible view skips it. Removing or dropping the guard puts a page that was never collected back in the fresh ranges, so it is reported once by the next fresh collection. Armed collection is unchanged; it already skipped the guard throughCollect's guard skip. Every page that is not a guard keeps the deferred activation.CheckGuardedSharedContextexpects 15 pinned pages, none in the guard, and one page per view for the write after finalize.CheckSharedWritesAroundGuard(guest_memory): it guards the lowest 4 KiB of a 16 KiB shared view, writes the other three pages, collects and re-arms twice. The guard staysPAGE_NOACCESSand exactly the three pages are reported. After the guard is removed, the page is reported once because it was never collected, then armed. It also checks that a guard on an armed page survives a pin. It fails if the subpage runs bypassprotector ifCollectreports the guard.CheckFreshAroundGuard(windows_write_watch): a guarded page in a view collected fresh for the first time is neither activated nor reported, is not counted as fresh in aPROT_NONEview, and is reported exactly once afterUnguard. It fails with the guard handling removed from the fresh-collection path.fiber,fiber_guard,windows_write_watch,agc_write_tracking,agc_shared_write*and the libkernel set (guest_memoryincluded) pass. The libc set passes exceptlog_macros, which also fails onmain.Imported from boykopovar/AnyPS5. Originally posted by fdfawef324 on 2026-10-13T15:12:06Z.
Closing this: #4265 merged with its Windows half (
81364388a), so a fiber no longer runs host calls on the guest's context and each fiber's host stack has its own guard page. Guarding the guest context, which is whatAPS5_FIBER_GUARDdid, adds nothing now.Pull request closed