common: finish Linux file reads that hit protected guest pages #1165
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!1165
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/1165/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?
@NeySlim on GitHub · 2026-10-07 22:00 UTC · original
NeySlim:fix/protected-guest-reads→main· +228 −1 · 10 files · 5 commits · closed without mergingProblem
This is the Linux side of #838. On Linux,
SysFileReadusesfreadstraight into the guest buffer. When part of that buffer is write-protected guest memory (pages tracked for GPU synchronization),read()fails withEFAULTat the first protected page instead of raising the fault the emulator handles.freadthen returns a short count, andKernelRead,KernelPread,KernelPreadvand AIO reads report fewer bytes than the file has.God of War Ragnarök (PPSA08332, 05.005) hits this a few minutes after boot while streaming texture LOD packs. One AIO read returned short, stopping exactly on a page boundary:
The game checks the completed byte count against its table and stops with
int 0x41(Unhandled host exception ... pc=0x9003405ba). Because the protection depends on what the GPU thread is doing at that moment, the failing read, and the assertion site, change from run to run.Change
When
freadstops short withferror()anderrno == EFAULT,SysFileReadclears the error, seeks back tostart + bytes_readand finishes through a 1 MiB thread-local host buffer copied withmemcpy. That copy runs in user mode, so the emulator's fault handler unprotects the pages as for any guest write. This is the approach of #838 for Windows and of the AMPR read path. Successful reads are unchanged, apart from oneftelloper read.Test
New
TestReadsIntoProtectedPagesinKernelFileSystemTests. It allocates two 64 KiB regions withCommon::VirtualMemory, makes the second read-only, and installs aHostExceptionhandler that stands in for the emulator's: it unprotects the region on a write fault.KernelPread,KernelReadandKernelPreadvmust then fill a buffer crossing into the protected region with the full file contents, through the handler.The test only uses portable APIs. On Windows it passes only with #838, which fixes the same read path there. If you prefer, I can guard it for Linux until #838 lands.
Validation
Linux, AMD Radeon 8060S, Mesa RADV 26.2.3.
kernel_file_system_tests: passes. Without the change it fails withpread fills a buffer that crosses into a write-protected page.int 0x41in its loading code. The game runs for 10 minutes and reaches the in-game loading screen.Not tested on Windows: this change only touches
sysLinuxFileIO.cpp, and #838 covers Windows.AI use
I used Claude Code (AI agent) to investigate the crash, write the change and the test, and run the tests above on my machine. I reviewed the diff and the results before opening this PR.
@coderabbitai[bot] on GitHub · 2026-10-07 22:03 UTC · original · edited 2026-10-16 01:01 UTC
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
45cfabed-47ef-4e54-9ae0-bab44ccb7392📥 Commits
Reviewing files that changed from the base of the PR and between 531b70a4d569ffcb5c7ab106a80a168bf0196038 and 828171643ccb4618518a5863b224a616e86a34e0.
📒 Files selected for processing (2)
src/common/platform/sysLinuxFileIO.cpptests/KernelFileSystemTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
📝 Walkthrough
SysFileReadnow retries unread bytes through a host buffer after an eligible short read caused byEFAULT. It copies data only when the registered destination probe accepts the range. GPU resource installation registers a probe based on mapped ranges and GPU write-fault handling. Tests cover protected-buffer reads throughKernelPread,KernelRead, andKernelPreadv, plus reads past EOF and the Linux unmapped-page case.Suggested reviewers:
nmzikPriority: ➖ Normal
Change: Bug fix
Merge Risk: ⚪ Minimal · up to
82817The past-EOF read test covers the reported regression. No actionable merge-blocking risk remains after normal checks.
🚥 Pre-merge checks | ✅ 4 | ❌ 1
❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
❤️ Share
Comment
@coderabbitai helpto get the list of available commands.@ -87,0 +124,4 @@if (got == 0) {break;}std::memcpy(static_cast<uint8_t*>(data) + w, chunk.data(), got);@coderabbitai[bot] on GitHub · 2026-10-07 22:03 UTC · original · edited 2026-10-07 22:35 UTC
🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
Do not copy into an unmapped destination after
EFAULT.EFAULTcan mean thatdatais unmapped, not that a guest page is write-protected. If a direct read fails for an unmapped destination, the retry can read bytes intochunk; thismemcpythen faults instead of returning the short read. Restrict recovery to a destination known to be protected guest memory, or use a fault-aware copy that can report failure. (man7.org)✅ Confirmed as addressed by @NeySlim
@coderabbitai[bot] on GitHub · 2026-10-07 22:03 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
a02cb448-2b59-49cb-84e0-e36872214fa7📥 Commits
Reviewing files that changed from the base of the PR and between
c86c8b4d93and 738f994586cd70e4d5078617315be8dc71b625dc.📒 Files selected for processing (2)
src/common/platform/sysLinuxFileIO.cpptests/KernelFileSystemTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
@ -87,0 +124,4 @@if (got == 0) {break;}std::memcpy(static_cast<uint8_t*>(data) + w, chunk.data(), got);@NeySlim on GitHub · 2026-10-07 22:34 UTC · original
Good point, fixed in b70b01a4. Before each bounce copy,
SysFileReadnow checks withmincore()that the destination pages are mapped. A write-protected page is still mapped, so the recovery still applies there. An unmapped destination stops the loop and keeps the short read, with the file position left right after the bytes delivered.TestReadsIntoProtectedPagesgained a Linux case: apreadinto a buffer whose second page is unmapped returns the first page only. Without the check, that case crashes in the copy.@ -87,0 +124,4 @@if (got == 0) {break;}std::memcpy(static_cast<uint8_t*>(data) + w, chunk.data(), got);@coderabbitai[bot] on GitHub · 2026-10-07 22:35 UTC · original
@NeySlim, thanks for the fix. The supplied code checks the destination mapping before each fallback read and copy. If a destination page is unmapped, recovery stops and the file position remains after the bytes delivered. Write-protected mapped pages can still use recovery.The reported Linux regression test covers the original crash case.
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-07 22:39 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
53626841-8c70-46f4-a9e1-433bb8145ba6📥 Commits
Reviewing files that changed from the base of the PR and between 738f994586cd70e4d5078617315be8dc71b625dc and b70b01a4563fba8ebe47da01231eda1a45ce96c6.
📒 Files selected for processing (2)
src/common/platform/sysLinuxFileIO.cpptests/KernelFileSystemTests.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@coderabbitai[bot] on GitHub · 2026-10-07 22:39 UTC · original · edited 2026-10-08 05:18 UTC
On
src/common/platform/sysLinuxFileIO.cppline 94 (outdated):🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8413
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 34136
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23512
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 42139
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11366
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4412
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18034
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 3523
Do not use
mincoreas a write-access check.A guest read can target a process-lifetime reserved range. Linux maps that range with
PROT_NONE, butmincorestill accepts it. The retry then callsstd::memcpy, which raises a write access violation.The reservation is not registered with
RenderContext::MapMemory.HandleFaulttherefore returnsfalse, and the global handler restores the default signal action. The process terminates instead of returning the short read withEFAULTthat the originalfreadproduced.Restrict this recovery to destinations with a handler that can make the range writable, or use a copy method that reports an inaccessible destination without raising an unhandled fault.
✅ Confirmed as addressed by @NeySlim
@NeySlim on GitHub · 2026-10-08 05:17 UTC · original
On
src/common/platform/sysLinuxFileIO.cppline 94 (outdated):Right,
mincoreaccepts a reservedPROT_NONErange, and it also broke the macOS build (different signature). Replaced in 070f58e:SysFileReadnow asks a probe that the kernel installs with the GPU resources (SysFileSetRecoverableDestination), and that probe only accepts ranges registered withRenderContext::MapMemory. Those are exactly the ranges whose write faultsHandleGpuFaultresolves. Any other destination, whether reserved, unmapped or simply not tracked, keeps the short read from the originalfread. The test now installs its own probe for the guarded target, and apreadinto a destination the probe rejects returns the short count. With a probe that accepts everything, that case crashes, as you described. On Windows the probe is a no-op: #838 handles that path.@coderabbitai[bot] on GitHub · 2026-10-08 05:18 UTC · original
On
src/common/platform/sysLinuxFileIO.cppline 94 (outdated):@NeySlimThanks for the fix and the clarification.SysFileReadnow checks the recovery probe before copying from the host buffer. If the probe is absent or rejects the destination, it preserves the short count. This addresses the unhandled write-fault concern.The probe-rejection test also covers the failure case raised in this thread.
✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-08 05:22 UTC · original · edited 2026-10-08 05:43 UTC
On
src/kernel/memory.cppline 938 (outdated):🩺 Stability & Availability | 🟠 Major | 🏗️ Heavy lift
🔎 Supported by static analysis
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11067
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11067
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 24825
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4007
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 12238
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1213
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 24763
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11087
🏁 Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5770
Restrict the probe to destinations with recoverable GPU-tracking state.
IsMappedonly checks mapped-range membership.HandleFaultreturnstruefor every mapped write fault, butMemoryTracker::InvalidateRegionremoves host protection only for existing tracking regions. An untracked mapped range can therefore pass the probe, resume after the fault handler, and fault again whenSysFileReadcallsmemcpy.Use a write-fault eligibility check from the GPU resource or cache-tracking layer. Do not use
IsMappedalone.✅ Confirmed as addressed by @NeySlim
@coderabbitai[bot] on GitHub · 2026-10-08 05:22 UTC · original · reviewed
Actionable comments posted: 1
ℹ️ Review info
⚙️ Run configuration
5bf92774-c0a4-4567-99de-6b3eae8f2f7f📥 Commits
Reviewing files that changed from the base of the PR and between b70b01a4563fba8ebe47da01231eda1a45ce96c6 and 070f58e3f0544cb689e8e91eabfce7bfa10d244b.
📒 Files selected for processing (5)
src/common/platform/sysFileIO.hsrc/common/platform/sysLinuxFileIO.cppsrc/common/platform/sysWindowsFileIO.cppsrc/kernel/memory.cpptests/KernelFileSystemTests.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.
@NeySlim on GitHub · 2026-10-08 05:43 UTC · original
On
src/kernel/memory.cppline 938 (outdated):Agreed,
IsMappedis too broad:HandleFaultreturnstruefor any mapped write fault, but only pages with a write watcher get their protection removed. A destination the guest mapped read-only would fault again inmemcpy. Fixed in 531b70a: the probe now walks the guest ranges of the destination. Guest-writable pages are accepted, since they do not fault. Any other page must be watched by thePageManager(newPageManager::IsWatched, throughRenderContext::ResolvesWriteFaults), which is the case where the fault handler restores write access. Everything else keeps the short read.PageManagerTestscoversIsWatchedfor untracked, watched, partially watched and released pages. The check is platform-independent; only the Linux read path uses the probe.@coderabbitai[bot] on GitHub · 2026-10-08 05:43 UTC · original
On
src/kernel/memory.cppline 938 (outdated):@NeySlimThanks for the fix and clarification. The supplied probe now accepts guest-writable ranges and requiresResolvesWriteFaultsfor other ranges. This addresses the original concern: mapped-range membership alone does not establish write-fault recovery.✏️ Learnings added
If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
✅ Review thread resolved.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-10-08 05:50 UTC · original · reviewed
ℹ️ Review info
⚙️ Run configuration
5b297ce6-35cb-455c-a363-f0688c3e58ec📥 Commits
Reviewing files that changed from the base of the PR and between 070f58e3f0544cb689e8e91eabfce7bfa10d244b and 531b70a4d569ffcb5c7ab106a80a168bf0196038.
📒 Files selected for processing (6)
src/graphics/host_gpu/pageManager.cppsrc/graphics/host_gpu/pageManager.hsrc/graphics/host_gpu/renderer/renderContext.cppsrc/graphics/host_gpu/renderer/renderContext.hsrc/kernel/memory.cpptests/PageManagerTests.cpp🚧 Files skipped from review as they are similar to previous changes (1)
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.
@NeySlim on GitHub · 2026-10-08 10:09 UTC · original
@coderabbitai Good catch on the EOF case, fixed in 8281716. The retry loop now bounds each chunk by the bytes the file can still supply (
fstaton regular files), so the probe never covers the tail of a request past EOF, and the loop stops on a short chunk. New case inTestReadsIntoProtectedPages: apreadthat starts at a protected page, reaches EOF before leaving the recoverable range, and asks for more than that range. It returns the remaining file bytes. Without the bound, it returns 0.@coderabbitai[bot] on GitHub · 2026-10-08 10:10 UTC · original
🧩 Analysis chain
🏁 Script executed:
Length of output: 4049
🏁 Script executed:
Length of output: 6223
@NeySlimThanks for the fix. I verified that8281716bounds the recovery probe by the remaining regular-file bytes and stops after a short chunk.The new
TestReadsIntoProtectedPagescase checks the returned byte count, the guard fault, and the copied data when the request extends past EOF and the recoverable range. This addresses the EOF concern. I did not run the tests.If you found this review helpful, would you consider giving us a shout-out on X?
Thank you for using CodeRabbit!
You are interacting with an AI system.
@ali-commits on GitHub · 2026-10-15 01:41 UTC · original
Additional Linux test evidence for current head 828171643ccb4618518a5863b224a616e86a34e0 (including the current recoverable-destination probe and page-manager changes):
kernel_file_system_testsandpage_manager_testsfalse &&, rebuildpread fills a buffer that crosses into a write-protected pageFull test logs and reproduction commands. Host: Intel i7-13700, Linux 7.2.9-2-cachyos, Clang 23.1.1 Release, CMake 4.4.4. The negative control is a recovery-disabled version of this head, not a full pre-PR base checkout.
This supports the Linux protected-page recovery behavior and shows that the regression test detects removal of the recovery. It is useful evidence for moving the fix forward through maintainer review. It is not a blanket merge certification: real GPU synchronization, race behavior, Astro/God of War gameplay and Windows were not validated in this test. Our separate successful Astro playtest used the Jetsku fork, so it is not attributed to this PR.
Published by GPT-6 Astra on behalf of @ali-commits, at their explicit request. AI prepared and ran these checks and wrote this comment. No independent human code review is asserted.
Pull request closed