memory: retry guest accesses while a mapping is briefly replaced #1229

Closed
github-bot wants to merge 1 commit from gh-pr/1229/head into gh-pr/1229/base
Owner

@⁠TheCruZ on GitHub · 2026-10-08 19:10 UTC · original
pr/map-fixed-retry → main · +332 −0 · 5 files · 1 commits · closed without merging

Summary

Fixes an intermittent crash in Grand Theft Auto V (PPSA04263), about once every 100 sessions: the [RAGE] HDD Streamer thread died with an unhandled access violation inside zlib inflate_fast (read of the output window) or inflate (first store into next_out), on 64 KiB direct-memory pages at 0x10xxxxxxxx. The page was mapped again a moment later: the crash report could read it, with the decompressed data in place.

Problem

Two game threads commit the same page: AsyncCommitThread maps it with MAP_FIXED, and one or two milliseconds later the main thread maps the same physical page at the same address again with MAP_FIXED. On the console that is an atomic no-op, and the streamer is already decompressing into the page. The emulator replaces a mapping by unmapping it and mapping it again (ReplaceFixedRangeWithReserved), so for that moment the page is gone and the streamer's access faults.

Windows has the same kind of gap on its own: a file view can only be split by unmapping all of it, so unmapping part of a view briefly takes the parts that stay mapped away too. Those parts also came back with the view's protection, dropping the guest's mprotect and the page manager's write and read tracking on them.

Change

  • Both replacements (the MAP_FIXED replacement and the Windows view split) publish the affected range in a small lock-free table while the mapping is absent. The runtime linker's fault handler waits until the range is mapped again and retries the access (unless the GPU tracking resolves the fault first). A page that keeps faulting after its range was mapped again is still reported as a crash, so real access violations are not hidden.
  • A Windows view split restores the survivors' own page protections before faults there are retried.

Relation to #432 and #433

Both closed PRs targeted the same window: #432 skipped a fixed map that already matches the mapped range, #433 retried faults inside a MAP_FIXED replacement window with a bounded yield loop. This PR also covers the Windows partial-unmap gap of a split view, keeps the protections of the parts that stay mapped, and keeps reporting a page that still faults once its range is back.

Testing

  • Grand Theft Auto V (PPSA04263): played in my fork with this change. The crash itself is rare (about 1 in 100 sessions), so the race is reproduced deterministically by the tests below.
  • virtual_memory_allocation_tests: new cases check that the survivors of a view split keep their protections on Windows, and that a thread keeps using memory while other parts of its view are unmapped and while its mapping is replaced with MAP_FIXED (hundreds of retried faults per run, an unhandled access violation without this change). Passes.
  • page_manager_tests and memory_tracker_tests pass.
  • Tested on Windows 11 with AMD Ryzen 5900x and RTX 3090.

AI usage

AI (Claude Opus 5.5) was used to investigate the issue (crash reports, mapping traces) and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my GTA V fork, i will be bringing PRs here after test them enough.

**@⁠TheCruZ** on GitHub · 2026-10-08 19:10 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1229) `pr/map-fixed-retry` → `main` · +332 −0 · 5 files · 1 commits · closed without merging ## Summary Fixes an intermittent crash in Grand Theft Auto V (PPSA04263), about once every 100 sessions: the `[RAGE] HDD Streamer` thread died with an unhandled access violation inside zlib `inflate_fast` (read of the output window) or `inflate` (first store into `next_out`), on 64 KiB direct-memory pages at `0x10xxxxxxxx`. The page was mapped again a moment later: the crash report could read it, with the decompressed data in place. ## Problem Two game threads commit the same page: `AsyncCommitThread` maps it with `MAP_FIXED`, and one or two milliseconds later the main thread maps the same physical page at the same address again with `MAP_FIXED`. On the console that is an atomic no-op, and the streamer is already decompressing into the page. The emulator replaces a mapping by unmapping it and mapping it again (`ReplaceFixedRangeWithReserved`), so for that moment the page is gone and the streamer's access faults. Windows has the same kind of gap on its own: a file view can only be split by unmapping all of it, so unmapping part of a view briefly takes the parts that stay mapped away too. Those parts also came back with the view's protection, dropping the guest's `mprotect` and the page manager's write and read tracking on them. ## Change - Both replacements (the `MAP_FIXED` replacement and the Windows view split) publish the affected range in a small lock-free table while the mapping is absent. The runtime linker's fault handler waits until the range is mapped again and retries the access (unless the GPU tracking resolves the fault first). A page that keeps faulting after its range was mapped again is still reported as a crash, so real access violations are not hidden. - A Windows view split restores the survivors' own page protections before faults there are retried. ## Relation to #432 and #433 Both closed PRs targeted the same window: #432 skipped a fixed map that already matches the mapped range, #433 retried faults inside a `MAP_FIXED` replacement window with a bounded yield loop. This PR also covers the Windows partial-unmap gap of a split view, keeps the protections of the parts that stay mapped, and keeps reporting a page that still faults once its range is back. ## Testing - Grand Theft Auto V (PPSA04263): played in my fork with this change. The crash itself is rare (about 1 in 100 sessions), so the race is reproduced deterministically by the tests below. - `virtual_memory_allocation_tests`: new cases check that the survivors of a view split keep their protections on Windows, and that a thread keeps using memory while other parts of its view are unmapped and while its mapping is replaced with `MAP_FIXED` (hundreds of retried faults per run, an unhandled access violation without this change). Passes. - `page_manager_tests` and `memory_tracker_tests` pass. - Tested on Windows 11 with AMD Ryzen 5900x and RTX 3090. ## AI usage AI (Claude Opus 5.5) was used to investigate the issue (crash reports, mapping traces) and to implement the change. I reviewed the code, understand it, tested it as described above, and wrote this description myself, in general fully tested the game while doing few missions and playing in-game with my [GTA V fork](https://github.com/TheCruZ/KytyPS5-GTA), i will be bringing PRs here after test them enough.
GTA V (PPSA04263) crashed about once every 100 sessions in "[RAGE] HDD
Streamer" with an unhandled access violation inside zlib inflate_fast
(read of the output window) or inflate (first store into next_out), on
64 KiB direct-memory pages at 0x10xxxxxxxx. The page was mapped again a
moment later: the crash report could read it, with the decompressed data
in place.

Two game threads commit the same page: AsyncCommitThread maps it with
MAP_FIXED, and one or two milliseconds later the main thread maps the same
physical page at the same address again with MAP_FIXED. On the console
that is an atomic no-op, and the streamer already decompresses into the
page. The emulator replaces a mapping by unmapping it and mapping it again
(ReplaceFixedRangeWithReserved), so for that moment the page is gone and
the streamer's access faults.

Windows has the same kind of gap on its own: a file view can only be
split by unmapping all of it, so unmapping part of a view briefly takes
the parts that stay mapped away too. Those parts also came back with the
view's protection, dropping guest mprotect and the page manager's write
and read tracking on them.

- Both replacements publish the affected range in a small lock-free table
  while the mapping is absent. The runtime linker's fault handler waits
  until the range is mapped again and retries the access (unless the GPU
  tracking resolves the fault); a page that keeps faulting after its
  range was mapped again is still reported.
- A Windows view split restores the survivors' own page protections
  before faults there are retried.
- Tests: survivors keep their protections on Windows, and a thread keeps
  using memory while other parts of its view are unmapped and while its
  mapping is replaced with MAP_FIXED (hundreds of retried faults per run,
  an unhandled access violation without this change).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:49:02 +00:00
github-bot closed this pull request 2026-11-26 09:49:02 +00:00
Author
Owner

@⁠coderabbitai[bot] on GitHub · 2026-10-08 19:10 UTC · original

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 11 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used all 8 included reviews currently available.

Learn how review limits work.

Review configuration:

⚙️ Run configuration
  • Configuration used: Repository: KytyPS5/KytyPS5/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: c82178b8-03a0-4a08-a6b1-530b6e676633
📥 Commits

Reviewing files that changed from the base of the PR and between 2f4b8a335d and 1beb8b8f14.

📒 Files selected for processing (5)
  • src/kernel/memory.cpp
  • src/kernel/memory.h
  • src/kernel/memoryAddressSpace.inc
  • src/loader/runtimeLinker.cpp
  • tests/VirtualMemoryAllocationTests.cpp
  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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 help to get the list of available commands.

**@⁠coderabbitai[bot]** on GitHub · 2026-10-08 19:10 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/1229#issuecomment-6067153995) <!-- This is an auto-generated comment: summarize by coderabbit.ai --> <!-- This is an auto-generated comment: rate limited by coderabbit.ai --> > [!WARNING] > ## Review limit reached > > You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository. > > **Next included review available in 11 minutes.** > > [Check out review usage here](https://app.coderabbit.ai/dashboard/review-capacity?orgId=d7cf2749-f181-4119-ba0c-ad79606afdfe). > > <details> > <summary>View limit details</summary> > > **Limit details:** You’ve used all 8 included reviews currently available. > > [Learn how review limits work](https://docs.coderabbit.ai/management/plans#rate-limits). > > **Review configuration:** > > <details> > <summary>⚙️ Run configuration</summary> > > - **Configuration used**: Repository: KytyPS5/KytyPS5/.coderabbit.yaml > - **Review profile**: CHILL > - **Plan**: Advanced > - **Run ID**: `c82178b8-03a0-4a08-a6b1-530b6e676633` > > </details> > > <details> > <summary>📥 Commits</summary> > > Reviewing files that changed from the base of the PR and between 2f4b8a335d5e27a5153bdea670bd877a061e37b1 and 1beb8b8f148bcbcd886b388ba9076482ab22ea6d. > > </details> > > <details> > <summary>📒 Files selected for processing (5)</summary> > > * `src/kernel/memory.cpp` > * `src/kernel/memory.h` > * `src/kernel/memoryAddressSpace.inc` > * `src/loader/runtimeLinker.cpp` > * `tests/VirtualMemoryAllocationTests.cpp` > > </details> > > </details> <!-- end of auto-generated comment: rate limited by coderabbit.ai --> <!-- autopilot:start --> - [ ] <!-- {"checkboxId":"2708ad07-9f24-4260-9c11-7dc76a49f2e3"} --> <strong title="Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts">Autopilot</strong> · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts <!-- autopilot:end --> <!-- tips_start --> --- Thanks for using [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=KytyPS5/KytyPS5&utm_content=1229)! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. <details> <summary>❤️ Share</summary> - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code) </details> <sub>Comment `@coderabbitai help` to get the list of available commands.</sub> <!-- tips_end -->

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.