fix(libc): track writes to singly mapped direct memory without faults on Windows #5596
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5596"
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 tibzejoker on 2026-10-14T00:42:12Z.
What
Port of @DotDebian's #637 onto current
main, as asked on #637. DotDebian stays the author of the main commit, and our changes are separate commits.On Windows, AnyPS5 tracked CPU writes to direct memory by arming its section views read-only and taking a write fault. The exception frame Windows builds for such a fault, with live AVX state, overwrites the System V red zone below RSP, so guest leaf functions that keep values there read back garbage (#361). GTA V's zlib
inflate_fastwas one of them.With this change, a direct memory page mapped at a single guest address is private
MEM_WRITE_WATCHmemory at that address, and its writes are collected withGetWriteWatch, like flexible memory: no fault. A page mapped at a second address moves its whole resident run into the section behind a read-only fence while it is copied, and every address then becomes a view, tracked by write protection as before.Commits:
fix(libc): track writes to singly mapped direct memory without faults on Windows(author DotDebian): #637's two commits squashed and merged by hand ontomain's 4 KiB subpage tracking of shared views. Resident runs keep a collected bit per page and enter the fresh ranges, soFreshandArmedcollections select them like shared views. A demoted run hands its collected bits to the new views and reports every page once. The fixes from the 2026-10-07 review are kept: fenced copies in demote and evict, demoting the whole run, unmapped pages kept out of the section, and thewatchResidentguard inCollect.fix(libc): keep resident mappings to direct memory backings on Windows: only direct memory goes resident, throughGuestArenaMapDirect. Other single-mapped sections (file-backed JIT and shm memory, whose aliases use duplicated handles) keepmain's shared-view path; making them resident brokeguest_jit_shared_memory.test(libc): require resident direct memory in the resident run tests:CheckBreakingAResidentRunKeepsConcurrentAccessesandCheckRemappedDirectMemoryIsChargedOncenow check that the first mapping isMEM_PRIVATE, so they fail onmaininstead of passing there.docs(libc): move the Windows direct memory debt entry to libc.md.The 2026-10-11 review points:
MapAliasthrows throughrefuse(...)when a resident run extends outside the aliased range.importAllocationkeepsmain's caller and fallback, with noentry.alias == nullptrbranch.tests/Recorder.cppkeepsmain's switches and adds--host-import-second-mapping-only.mainnow fail there, as above.Tested
Windows 11, i9-13900K, RTX 4090, MinGW-w64 GCC 15.2.0 (WinLibs r7), Release,
BUILD_TESTING=ON, onmaince5c83953.guest_memory: passes (ctest -R guest_memory, 2/2). Onmain, the resident run tests fail at the newMEM_PRIVATEcheck, and #637's owng_accessViolations == 0check fails as well.ctest: 1112/1112 pass.tools/check_conventions.py --base upstream/mainpasses.mainwithout this change it crashes in the RAGE main thread right after the save loads (access violation at eboot 0x28aacee). With it, the prologue ran for 150 s with correct rendering, onmain8d98e8851plus the other open PRs GTA needs to reach Story (#5003, #448's port, #1186, #1189).Not tested on Linux: the change is in the Windows mapping code.
Checklist
main; no other open PR implements the same functions (supersedes #637; #2054 overlaps and is stale)NotImplemented_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
https://claude.ai/code/session_01V1W4jdNSKTV7xwuS6xQPJV
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T01:01:03Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)core/libs/prx/libc/include/WindowsMappings.hppL263-264, L366-367, L410docs/dev/debt/libc.mdL30core/libs/prx/libc/include/WindowsMappings.hppL77-78, L263-264, L366-367, L596-597docs/dev/debt/libc.mdL30Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T01:24:18Z.
@tibzejoker thanks, this covers the 10-11 points. Since it changes the Windows memory model, could you run GTA V (PPSA04264) on this head alone on top of
mainand post the result? The 150 s run in #5024 was on a combined stack.Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T02:17:42Z.
@oneandonlydean GTA V (PPSA04264) on this head alone. Windows 11, i9-13900K, RTX 4090 (NVIDIA 616.92), MinGW-w64 GCC 15.2.0, Release. Write watch on, no other PR and no local change, one run each:
maince5c83953(this PR's base): the title reaches the menu and Story is requested. While Story loads, it crashes with an access violation in[RAGE] HDD Streamer: a write through a null pointer at eboot 0x28aacee. This is the same address as the crash in the RAGE main thread I reported on #637 and #5052.b7d7e71bf: the same steps load Story without a crash. It then stops about 30 s into Story atAGC graphics: sample masks are unsupported, which is #448's territory and the next stop GTA hits onmain.So, on its own, this head removes the write-watch crash and nothing new shows up before the next known stop. With #448 (fdfawef324's port) on top, the Story prologue ran for 150 s with correct rendering, as posted in #5024.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by tibzejoker on 2026-10-14T06:16:07Z.
A correction to my run above: "removes the write-watch crash" was based on one run per tree. Since then I've run GTA more often with this change in (main
ee9037d85+ #757 and the other Story PRs, write watch on). In 1 run of 6, the same crash came back 15 s into Story:[RAGE] HDD Streamer, a null write at eboot 0x28aacee. The other 5 ran 60-180 s without it. Without this change, both of my runs crashed there.So this change makes the crash rare in GTA, but it doesn't remove it. One possible reason is that only direct memory goes resident, so writes to other shared views can still fault. I haven't traced that one crash, so I can't tell whether it was red-zone damage or something else at the same site.
AI-assisted (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by EagleBearerYS on 2026-10-14T06:49:48Z.
Resident Evil Village (PPSA01556, 01.210.000) aborts at boot since this PR, 19 s in and before any dialog:
RTX 3070 Laptop GPU (8 GB), driver 617.42, Windows 11 Home 10.0.26300, Ryzen 9 5900HS, 15.4 GB RAM.
main0b6d033b8, relinked withmain's relinker. Plainmaincan't boot Village here for an unrelated reason: it prepares every shader at registration, and after 10 minutes private memory was at 21.9 GB with 6.8 GB of commit free. So this run adds two local commits that prepare shaders at their first draw (APS5_RE8_LAZY_PREPARE=1,APS5_NO_GPL=1). They don't touch the import or mirror paths.That range is a 419 MiB direct-memory heap mapped once. Before this PR it was shared views, served by a heap mirror, and Village booted to gameplay on our older branch. With this PR it's resident private memory. The address-based build's 64 MiB window import leaves the write watch for private memory, and the heap mirror refuses ranges with unwatched blocks, so the whole range would be copied on each build, past the 64 MiB guard. Raising the guard (
APS5_ADDRESS_COPY_MAX_MIB=512) gets past it.#5688 fixes it: heap mirrors keep such ranges and refresh the blocks outside the write watch. With it the abort is gone and the range is host-imported: