common: finish file reads that hit GPU-protected guest pages (ASTRO BOT) #2141
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!2141
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2141"
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?
@Nawid3333 on GitHub · 2026-11-06 19:34 UTC · original
Nawid3333:fix/protected-guest-file-reads→main· +346 −5 · 6 files · 3 commits · openASTRO BOT (PPSA21564) sometimes stops at its own assert
Room.cpp:787 ODX Definition is not found(orObjectDefinitionContext.cpp:89) right afterRead 0 bytesof an.odxbfile. Zero-byte reads like this happen when the read buffer lies in guest pages the GPU tracker keeps write-protected: the host read delivers nothing.Cause. The page manager write-protects guest pages it watches for GPU synchronization.
ReadFilewrites the buffer from kernel mode, which cannot raise the emulator's fault handler, so it fails withERROR_NOACCESS(998) orERROR_INVALID_USER_BUFFER(1784) andSysFileReadreturns a short read; on Linuxfreadstops withEFAULT. A user-mode copy into the same page faults intoHandleGpuFault->RenderContext::HandleFault(Write), the path a guest CPU store takes, which invalidates the caches over the page and unprotects it.Why after the read, not before.
KernelReadandKernelPreadalready invalidate the destination before the host read (fileSystem.cpp:697,:824). A lab build logged the page-manager state of every short read in one ASTRO BOT run: in 12 of 13, no page of the buffer was watched right before the host read and some or all of its pages were watched again right after; the 13th already had 15 of its 32 pages watched again before the call. Every buffer was fully GPU-mapped and none was a buffer-cache region. The pages are watched again between the preparation and the end of the read, so preparing earlier does not close the gap. The copy after the failure goes through the fault handler for whichever pages are protected when it touches them.Change.
SysFileReadrecords the file position before every read. When the read fails this way, Windows seeks back to that position and reads again from there, Linux continues after the bytes already delivered; both read through a thread-local 1 MiB buffer, up to the end of the file, and copy each chunk into the guest buffer in user mode. After a recovery the file position is the start plus the bytes delivered.InstallGpuResourcesinstalls a probe that accepts a range only when every guest mapping in it has CPU write permission. A read into memory the guest cannot write stays short, as on main, instead of making a read-only page writable.KernelRead/KernelPread/KernelPreadvthrough a host buffer every time; this change keeps reading directly into guest memory and copies only after a failed read.Tests.
KernelFileSystemTests(TestReadsIntoProtectedPages, from #1165):KernelPread,KernelReadandKernelPreadvfill buffers that cross into a write-protected page, a read past EOF recovers only the bytes the file holds, and a destination with a no-access page stays short without crashing.BufferCacheDirtyGarbageCollection(new case): a 12-byte file read ends in a page whose buffer is GPU-written; all 12 bytes must arrive, the range must be CPU-modified and no longer GPU-dirty, and the next buffer lookup must see the new words. The same file is also read throughKernelPread, into a fully GPU-mapped range (the pre-read invalidation unprotects it) and into a range whose first page is not GPU-mapped (the invalidation skips it, so the recovery has to deliver the bytes), each from a cleared destination and with the same checks.TestReadsIntoProtectedPagesandBufferCacheDirtyGarbageCollectionpass; with the recovery branch switched off (main's behaviour) both fail:pread fills a buffer that crosses into a write-protected pageandkernel-read bytes(theKernelPreadread into the partly mapped range).In-game. ASTRO BOT, boot-and-mash script, one build (this change + #2022) with a lab switch that turns the recovery off (= main's read path), three runs each in the order fix, base, base, fix, fix, base:
ReadFilecalls (998 and 1784, 128 KiB to 11 MiB), every one recovered in full, 0 zero-byte reads;Read 0 bytesof an.odxb.Fork CI on the PR head (
4b8336a1f): the Linux, Windows and macOS builds and tests pass.Written with Claude Code and OpenAI Codex (AI): the port, the tests, the diagnostics and the measurements, run on my PC; reviewed by me.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original · edited 2026-11-06 22:49 UTC
No actionable comments were generated in the recent review.
Walkthrough
The file I/O interface adds a callback for checking destination memory. Linux and Windows reads use the callback to recover from selected destination access errors and report bytes read. GPU resource installation registers a probe that checks virtual-range coverage and CPU-write permission, and clears the callback when resources are removed. Tests cover protected-page reads, reads past EOF, inaccessible destinations, and GPU cache state.
Suggested reviewers:
nmzikChange: Bug fix
Merge Risk:
Low · up to
4b833This PR recovers short file reads into GPU-protected guest pages. The remaining risks are test-only: gaps in coverage of the probe's rejection paths, and a temporary file that can be left behind after a failed assertion. It is mergeable with owner awareness.
Pre-merge checks:
4 ·
1
Full details: Docstring Coverage
Explanation
Docstring coverage is 16.67% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 6 functions across 5 files. (1 skipped: 1 too large.)
Resolution
Write docstrings for the functions missing them to satisfy the coverage threshold.
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
b3bedab5-0e2c-47ae-bb71-217eca57554dReviewing files that changed from the base of the PR and between
549d3630a0and4b8336a1ff.src/common/platform/sysLinuxFileIO.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/scripts/watch_inbox.pyline 380 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23922
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 23914
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1294
Collect reviews on watched PRs and reviews that mention USER.
Collector.reviewsruns only for USER’s open PRs. The inbox can therefore miss approvals, change requests, and review bodies on other watched PRs, including review-body mentions. The checkpoint then advances past those reviews.Add review collection for watched PRs and mentions on other PRs. Preserve the existing filters and avoid duplicate reports for USER’s PRs.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/scripts/watch_inbox.pyline 388 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 18927
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6904
Periodically recheck failed-check state.
For an older PR with an unchanged head, a resolved failure can remain in
previous. The retained key can suppress a later failure notification for the same head. Recheck commit statuses periodically, as fordirty.Suggested fix
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/scripts/watch_inbox.pyline 456 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 25370
Web query:
GitHub issue comment maximum body length 65536 characters API 422 limitResult:
Web query:
GitHub issue comment body 65536 characters 262144 bytes ASCII validation limit actual 2024Result:
Web query:
GitHub REST combined commit status endpoint maximum number contexts statuses response pagination context limitResult:
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2600
Web query:
GitHub maximum file name length git tree component 255 characters repository review comment path maximumResult:
Web query:
GitHub commit status context maximum length 100 characters combined status default 30 per_pageResult:
Bound the posted comment size.
rendercan exceed GitHub’s issue-comment limit when many failed-check contexts are listed. A failed POST prevents the checkpoint update. If later runs remain oversized, older events can fall outsideMAX_WINDOW.Limit the complete rendered comment by UTF-8 size, including state markers. Report any omitted events.
Ruff (0.17.1)
[warning] 449-449: String contains ambiguous
–(EN DASH). Did you mean-(HYPHEN-MINUS)?(RUF001)
[warning] 450-450: Consider iterable unpacking instead of concatenation
Replace with iterable unpacking
(RUF005)
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/workflows/delete-closed-branches.ymlline 42 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7999
Web query:
GitHub delete branch open pull request does pull request close automatically deleting head branch REST APIResult:
Web query:
GitHub REST API delete git reference branch open pull request automatically closes pull request git push delete head branchResult:
Recheck open pull requests before deletion.
If a pull request opens after the listing,
prs.tsvcan still show only closed pull requests. The scheduled run can then delete the branch and close the new pull request.Recheck both repositories immediately before deletion to narrow the race. A concurrency group alone cannot prevent a user from opening a pull request.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/workflows/delete-closed-branches.ymlline 50 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5903
Web query:
GitHub closed merged pull request head sha changes when pushing new commits to same branch after closing restore branch commitsResult:
Keep branches with work added after a PR closes.
If the owner pushes new commits after a PR closes, the scheduled run can delete the branch before a new PR opens. The new work is not protected by the closed PR. Check that the branch has no later work before deleting it.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/workflows/delete-closed-branches.ymlline 52 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 6020
Web query:
GitHub CLI gh api endpoint path percent encoding # fragment source code GitHub CLIResult:
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4844
Web query:
GitHub documentation restrictions branch names cannot contain # percent valid Git reference namesResult:
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9855
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 2825
Encode the branch name in the DELETE endpoint.
If an eligible branch contains
#,gh apiomits the fragment from the request path. This can delete a different branch, including one with an open pull request. Percent-encode the branch path segments before constructing the endpoint.@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/workflows/delete-closed-branches.ymlline 53 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 5912
Continue cleanup after a failed branch deletion.
If one
gh api -X DELETEcall fails,set -eskips all later branches. Log the failure and continue, then fail the run after the loop so the failure remains visible.Suggested fix
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original
On
.github/workflows/sync-upstream.ymlline 25 (outdated):Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 26550
Web query:
GitHub Actions GITHUB_TOKEN REST API merge-upstream push event trigger workflow exceptions github token sync forkResult:
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1323
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 1896
Run the fork build after a sync changes
main.The sync uses
GITHUB_TOKEN, so it does not trigger the fork’s push build. New upstream code can remain without a fork build or test run. Dispatchbuild.ymlexplicitly after a sync changesmain, or use a credential that can trigger push workflows.@ -899,0 +922,4 @@return true;};}SysFileSetRecoverableDestination(probe);@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original · edited 2026-11-06 20:12 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 28003
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 7404
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 29856
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 24696
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 4521
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 8833
Keep the destination mapped through the recovery copy.
KernelMunmapcan remove the destination after the probe approves it but beforeSysFileReadcopies into it. The GPU fault handler then cannot resolve the fault, and the process can crash. Protect the destination from unmapping for the full probe-and-copy operation.@ -5514,0 +5537,4 @@&prot_wrote);Require(name, "protected-read fixture size",prot_wrote == sizeof(prot_payload),"the protected-read fixture file was not written");@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original · edited 2026-11-06 20:12 UTC
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 41569
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11157
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 11904
Script executed:
Repository: KytyPS5/KytyPS5
Length of output: 9638
Use a unique temporary fixture per test process.
On Linux, a second run can truncate the shared fixture before the first run reads it. The first run can then fail its byte-count assertion. An assertion failure also leaves the fixture behind. Create a unique fixture for each process and remove it after the test.
@coderabbitai[bot] on GitHub · 2026-11-06 19:41 UTC · original · reviewed
Actionable comments posted: 10
fc4d52fd-5f2b-4be4-933f-65fe2ecd17f2Reviewing files that changed from the base of the PR and between
182161eb3eand 4ee58a2584a8205d36f42667240ba08b4a6d37c7..github/.gitignore.github/pull.yml.github/scripts/watch_inbox.py.github/watch-threads.txt.github/workflows/delete-closed-branches.yml.github/workflows/sync-upstream.yml.github/workflows/watch-inbox.ymlsrc/common/platform/sysFileIO.hsrc/common/platform/sysLinuxFileIO.cppsrc/common/platform/sysWindowsFileIO.cppsrc/kernel/memory.cpptests/KernelFileSystemTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nawid3333 on GitHub · 2026-11-06 20:08 UTC · original
Sorry for the noise: my first push of this branch was cut from my fork's
main, so the PR briefly showed about 110 fork-only merge commits and my fork's.githubfiles (sync and inbox workflows). That was a mistake. The branch now holds only the fix (76a84cc51on top of182161eb3), and CodeRabbit's comments on.github/...refer to those files and no longer apply.I set the PR to draft while I push one test follow-up from the review (a unique file name for the protected-read fixture) and recheck it; I will mark it ready again after that.
@ -899,0 +922,4 @@return true;};}SysFileSetRecoverableDestination(probe);@Nawid3333 on GitHub · 2026-11-06 20:12 UTC · original
The recovery copy is the guest's own store into its own buffer: a guest thread that unmaps a buffer while another of its threads is still reading a file into it would fault the same way on main with any CPU store into that buffer. Keeping the destination mapped would need a lock against
KernelMunmapacross file I/O for a use-after-free in the game; the probe stays a permission check, as the PR text says.@ -5514,0 +5537,4 @@&prot_wrote);Require(name, "protected-read fixture size",prot_wrote == sizeof(prot_payload),"the protected-read fixture file was not written");@Nawid3333 on GitHub · 2026-11-06 20:12 UTC · original
Fixed in
549d3630a: the fixture file name now carries asteady_clockcount, like the other temporary files in this test (kyty_pm4_nop_dump_{},kyty_shader_store_{}), so parallel runs do not share it.@ -899,0 +922,4 @@return true;};}SysFileSetRecoverableDestination(probe);@coderabbitai[bot] on GitHub · 2026-11-06 20:12 UTC · original
@Nawid3333 Thanks for clarifying the intended contract. The probe checks permissions; it does not guarantee the buffer’s lifetime. If the guest unmaps the buffer during the read, the recovery copy can fault.
I withdraw the request to keep the destination mapped through the copy.
Review thread resolved.
You are interacting with an AI system.
@ -5514,0 +5537,4 @@&prot_wrote);Require(name, "protected-read fixture size",prot_wrote == sizeof(prot_payload),"the protected-read fixture file was not written");@coderabbitai[bot] on GitHub · 2026-11-06 20:13 UTC · original
Analysis chain
Script executed:
Length of output: 13828
@Nawid3333Thanks for the update. The timestamp reduces the collision risk, but two processes can still get the same count. File creation would then truncate the existing fixture.A failed
Requirealso callsstd::abort()before the removal code runs. Please use exclusive fixture creation with a retry on collision, and arrange cleanup before an assertion aborts.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.
@ -5514,0 +5537,4 @@&prot_wrote);Require(name, "protected-read fixture size",prot_wrote == sizeof(prot_payload),"the protected-read fixture file was not written");@Nawid3333 on GitHub · 2026-11-06 20:13 UTC · original
The fixture now follows the other temporary files in this test:
kyty_pm4_nop_dump_{}uses the samesteady_clockname, the sameCommon::File::Createand the samestd::filesystem::removeafter its checks. Two processes collide only if both read the same clock tick at this line, and a file left behind by an aborted run has a name no later run uses. I'm keeping it as it is rather than adding a retry loop for this one test.@ -5514,0 +5537,4 @@&prot_wrote);Require(name, "protected-read fixture size",prot_wrote == sizeof(prot_payload),"the protected-read fixture file was not written");@coderabbitai[bot] on GitHub · 2026-11-06 20:14 UTC · original
@Nawid3333 Understood. The fixture follows the existing test pattern, and you’ve accepted the remaining collision risk. Thanks for explaining.
You are interacting with an AI system.
@coderabbitai[bot] on GitHub · 2026-11-06 20:58 UTC · original · reviewed
08684e30-4e6c-467b-a3fd-0b26529597b5Reviewing files that changed from the base of the PR and between
76a84cc511and549d3630a0.tests/KernelFileSystemTests.cpptests/ShaderRecompilerComputeTests.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
@Nawid3333 on GitHub · 2026-11-06 23:15 UTC · original
@coderabbitai Your nitpick from the last review (test the kernel read with GPU-watched memory) is in
4b8336a1f. The protected-read case now also reads the fixture throughKernelPread, twice:Each read starts from a cleared destination and checks the guest bytes, the CPU ownership and the next buffer lookup. On my PC (Windows 11, RX 9070 XT),
--buffer-cache-gc-onlypasses with this PR; with the PR'ssrcreverted to182161eb3it fails atkernel-read bytes: KernelPread lost protected guest bytes.The same commit drops the last
fseekoof the Linux recovery: after the loop the stream is already atstart + w, so that seek did nothing.@coderabbitai[bot] on GitHub · 2026-11-06 23:16 UTC · original
Analysis chain
Script executed:
Length of output: 14176
@Nawid3333Thanks for the follow-up. I checked commit4b8336a1f. The newKernelPreadcases cover both mapping layouts and check the guest bytes, CPU ownership, and the next buffer lookup.For successful recovery reads, the Linux loop advances the stream and
wby the same amount. Removing the final seek looks consistent with that behavior.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.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.