common: retry Windows file reads through a bounce buffer when the guest buffer is protected #838
No reviewers
Labels
No labels
bug
documentation
duplicate
enhancement
GPU
GUI
help wanted
question
Shaders
Video
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/KytyPS5--KytyPS5!838
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/838/head"
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?
@ew-sudo on GitHub · 2026-09-25 20:53 UTC · original
ew-sudo:fix-read-protected-pages→main· +30 −2 · 1 files · 1 commits · closed without mergingProblem
On Windows,
SysFileReadcallsReadFiledirectly on the guest destination buffer and ignores its return value. When that buffer lies in write-protected guest memory (pages tracked for GPU synchronization),ReadFilefails because it writes from kernel mode, which never reaches the emulator's fault handler. The failure is silent:KernelRead/KernelPreadreturn 0 bytes and the game sees an empty file.In Astro Bot (PPSA21564, 01.007) this happens while loading levels and causes the failures reported in #745:
With a temporary log added around the call, the failing reads returned two error codes:
998(ERROR_NOACCESS)1784(ERROR_INVALID_USER_BUFFER), mostly for larger transfers (up to 160 MB in one call).Fix
When
ReadFilefails with one of these two errors, rewind to the original file position and read through a 1 MiB thread-local host buffer, copying each chunk into the guest buffer withmemcpy. That copy runs in user mode, so the fault handler can unprotect the pages as usual. This is the same approach the AMPR read path already uses (bounce buffer +memcpy). Successful reads are unchanged; the only extra cost on the normal path is oneSetFilePointerExquery per read.Testing
bounce buffer read N of N bytesduring testing, for both error codes), theXml.cpp:819assert no longer happens, and the level loads from the world map.main.Notes
freadinSysFileRead; a largefreadcan hit the same class of failure (EFAULT) on protected pages. I have not tested Linux, so this PR only changes the Windows path.I used Claude to help investigate and write this change; I built and tested it myself and reviewed the code.
@coderabbitai[bot] on GitHub · 2026-09-25 21:01 UTC · original
Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
Review profile: CHILL
Plan: Advanced
Run ID:
0d5edaf1-be99-4787-b028-f774a89b15cd📥 Commits
Reviewing files that changed from the base of the PR and between
497166009cand4214c331a1.📒 Files selected for processing (1)
src/common/platform/sysWindowsFileIO.cppIncluded review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.
📝 Walkthrough
Walkthrough
On Windows,
SysFileReadnow checks the result of the directReadFilecall. ForERROR_NOACCESSorERROR_INVALID_USER_BUFFER, it restores the saved file position and retries through a thread-local 1 MiB buffer, copying chunks to the destination. Other direct-read failures report zero bytes read.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
4214cNo actionable merge-blocking defect was established in the Windows read fallback. Merge after normal checks.
Security Architecture Review
Security architecture risk: 🟡 Moderate · up to
4214cThe change should recover reads into protected guest pages, but the fallback may also encounter invalid destinations or failed position restoration. Those cases could affect host-process availability or the bytes delivered to the guest.
Retained concerns
Security review details
Security Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Comment
@coderabbitai helpto get the list of available commands.@SergiioDev on GitHub · 2026-10-07 19:02 UTC · original
@ew-sudo I had the same problem, thanks for the PR. Let's see if we can get this fix merged
@Kronys256 on GitHub · 2026-10-13 07:18 UTC · original
Tested this PR on Windows with ASTRO BOT PPSA21564 01.007.000 (non-RT patch from #745, both cheats applied): it fixes the level-load crashes for me.
Setup: Windows 11 Pro 25H2, Ryzen 7 5800X3D, RX 9070 XT 16 GB (driver 32.0.31041.1004), 32 GB RAM.
Path: load an existing save, then steer the controller-ship from the hub towards a world.
Without this PR, every run crashed within seconds on a level-loading thread, each time at a different place in guest code:
df5b9b9LevelTask_ATQTmovzx ecx, word [rax+0x28]with rax=0ProductNextLoad_ATQTint 0x41aftermov esi, 0x333, i.e. theXml.cpp:819assert from the PR descriptionLevelTask_ATQT/OdxAsyncLoaderWith this PR (only commit
4214c33merged on top of BryKytyPS5 a9e1ae5, built with that repo's workflow): the world loaded and I finished a whole level, 9 minutes with no crash and a clean exit. Test build: https://github.com/MrGautier256/KytyPS5/releases/tag/BryKytyPS5-2026-10-12-2b5c97f-pr838I also built
df5b9b9+ this PR (https://github.com/MrGautier256/KytyPS5/releases/tag/KytyPS5-2026-10-12-7720a63-pr838) but did not play the level on it.Full, unedited logs of these four runs will follow in a reply.
@devklex on GitHub · 2026-10-18 04:21 UTC · original
I ran into the same failed reads in Astro Bot (PPSA21564, 01.007.000) on Windows 11 with an RTX 4090, right after New Game. The symptoms were the
OdxAsyncLoadercrash from the table above (pc 0x900388230 or 0x900387770, walking off at 0x11e8000000), an endless loop in the same thread, and an internal assert inProductNextLoad_ATQT. In case it helps review, this is why the destination is protected even thoughKernelRead,KernelPreadandKernelPreadvcallMemory::InvalidateMemoryfirst:InvalidateMemorylifts the write protection, butMemoryTracker::InvalidateRegionalso queues the range for a BDA sync.RenderContext::PrepareBda→BufferCache::SynchronizeBda) uploads those pages and write-protects them again. That can happen whileReadFileis still running.ReadFilethen fails at the first protected page, andSysFileReadignores the failure.With a fallback like this one, my runs logged 0–4 recovered reads per New Game (37 in 18 runs, each with 0 bytes read directly). They hit level data such as
.odxb,.skeland.animfiles, and.gnfptextures from 128 KiB to 11 MB. Before the fallback, 5 of 38 runs failed at New Game; with it (and a separate texture-streaming fix), 18 runs in a row got through.My version differs in two ways, in case either is useful here:
fileSystem.cppinstead ofSysFileRead, so it would also cover Linux and macOS. ThereSysFileReadusesfread, which passes large reads straight toread(2), and that stops withEFAULTat a protected page. I haven't tested those platforms.ReadFileinto them stops short, and checks that the fallback delivers every byte.If a maintainer wants either, I can open it as a PR or add the test on top of this one.
(Investigated with Claude Code, an AI assistant, which also drafted this comment. I read it before posting, and the runs are from my PC.)
@ZrDaniel07 on GitHub · 2026-10-24 01:48 UTC · original
Tested on Windows 11 with an RTX 5090 (driver 617.42) and a Ryzen 9 5900X: ASTRO BOT PPSA21564 01.007.000, no game patch. It fixes the level-load faults here too.
Base: @Nawid3333's
test/helper-1511(de0e387=main@6aaa533+ the SRT-walk fix from #1365 + aDS_APPENDfix that avoids the DEVICE_LOST from #1511), built locally, with4214c331cherry-picked on top. It applied cleanly.Read 0 byteslines in_kyty.txtde0e387aerial_aquarium_clouds_1v.jxm(19,462,176 bytes on disk)OdxAsyncLoaderboth times (pc0x9074d8153loading the world map,0x90706b5beloadingunderwater_aerial_garden)de0e387+ this PRTwo of Nawid's lab builds (
test/loopcap-1511, without this PR) also faulted atpc=0x900388230and0x900388244looking up key0x9ca52db2, the same site as in @MrGautier256's table, after 1–2 reads of 0 bytes each.With this PR the first two runs then stopped at the oversized mesh draw from #624 (
mesh draw exceeds host workgroup limits, see #793). With a local fix for that on top, I got into gameplay in the level and closed the game myself; it ran at under 1 FPS with the guest log going to a file.kernel_file_system_testsexits at startup with0xC0000028on this machine both with and without this PR, so I couldn't use it here.AI use: Claude Code built the branches, applied the PR and compared the logs with me; I ran the game and checked the outputs myself.
@Nawid3333 on GitHub · 2026-10-27 20:57 UTC · original
I ran the protected-read cases from #1165 (
TestReadsIntoProtectedPages) on Windows: main0685bffb, clang-cl Release, Windows 11. The fullkernel_file_systemtest stops earlier on Windows for unrelated reasons (the cancel case ofTestMutexHandoffandTestSaveTruncate), so I ran that test on its own.pread fills a buffer that crosses into a write-protected pagememcpyfaults on the no-access pageAfter
ERROR_NOACCESSthis PR copies into the whole destination, also into pages the fault handler doesn't resolve (unmapped or no-access guest memory). On main,SysFileReadjust returns the bytesReadFilereported, so such a read comes back short instead (I didn't run the no-access case on main itself); with the retry the process dies. #1165 already checks the destination on Linux (SysFileSetRecoverableDestination, set inInstallGpuResources). Doing the same on Windows puts both platforms on one rule. Diff on top of this PR + #1165:In ASTRO BOT (PPSA21564, RX 9070 XT) I ran main
ffc81377with this PR, #1165 and the diff on top: a 5-minute scripted run got as far as without them (same shader counts, level loading), no crash and noRead 0 bytesline. The run without them didn't showRead 0 byteseither this time; in my older runs (10-20 to 10-24 builds) it showed up in 6 of 20, on.gnfptexture files.The no-access case I added at the end of
TestReadsIntoProtectedPages(it only usesCommon::VirtualMemory, so it should behave the same on Linux; I haven't run it there):@ZrDaniel07 on GitHub · 2026-11-14 22:16 UTC · original
Still needed on
main3e226bff(11-14) for ASTRO BOT PPSA21564. Setup: Windows 11, RTX 5090 (driver 621.24), Ryzen 9 5900X.Without this PR. Build:
main3e226bffplus the MAX_MIP view fallback for the boot exit (same as #2172). Running from the same save, the game reaches the level select (about 78 VS / 77 PS / 60 CS compiled) and stops with:This happened in 3 of 3 runs (two fallback variants, one of them also run with
--redzone). The same fault (samepc) shows up oneecd9821and on every good step of a bisect from 11-06 to 11-07.--redzonedoes not change it (samepc).With this PR. I ported the retry to the same build, and the Garden level (
aerial_garden) loads at 1:44 from launch and plays with no exit.Rebase. The patch no longer applies.
1a24e34e("common: trim the File API", 11-04) removed theSYS_FILE_*types, andSysFileReadis now a singleReadFile(f.handle, data, size, &r, nullptr). The port keeps your logic unchanged:SetFilePointerEx(..., FILE_CURRENT);ERROR_NOACCESS/ERROR_INVALID_USER_BUFFER, seek back;thread_local1 MiB buffer andmemcpyinto guest memory.It adds
<algorithm>,<cstring>and<vector>. Diff (+31/-2, one file) is available if useful.AI use: Claude Code ported the patch and drafted this with me; I played the game.
Pull request closed