loader: retry a fault on memory the guest map reports as committed #435

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

@⁠CheesyPoofs346 on GitHub · 2026-08-31 22:45 UTC · original
CheesyPoofs346:fix/retry-transient-map-faults → main · +22 −0 · 1 files · 1 commits · closed without merging

Summary

  • Retry a faulting instruction when the guest memory map reports the address as committed, instead of aborting the emulator.
  • Covers the window where guest memory is briefly unmapped during a remap.

Problem

MapBacking / UnmapBacking remap guest memory through placeholders, so there is a short window in which the pages are not mapped. A guest thread that touches the range during that window takes an access violation on memory that is committed both before and after the remap, and KytyExceptionHandler turns any fault it does not recognise into a fatal EXIT.

Instrumenting the exception path with VirtualQuery showed the state changing underneath two adjacent calls. at is queried before prev, yet they disagree about the same range:

Unhandled host exception: code=0xC0000005 access=write address=0x0000001038 2e6000
  | at   base=0x10382e6000 size=0xa000       state=MEM_RESERVE protect=0
  | prev base=0x10382e5000 end=0x10382f0000  state=MEM_COMMIT  protect=PAGE_READWRITE
  | guest 0x10382e0000-0x10382f0000 committed=1 direct=1

Both cover 0x10382e6000-0x10382f0000. The range was unmapped when the first query ran and mapped when the second did, so the fault is transient rather than a real invalid access. Logging every MapBacking failure produced no hits, confirming nothing failed — the fault falls between the unmap and the remap.

Observed in Grand Theft Auto V (PPSA04264) as a reproducible crash while the game streams world data (driving, entering vehicles). With this change the same session completes the first mission with no faults, where previously it aborted every time.

The retry is bounded to 4096 attempts per faulting address, so an address that never becomes valid still reaches the existing fatal path with its diagnostics intact.

Scope and limits

This is a mitigation, not a removal of the race. The complete fix is to make the remap atomic with respect to guest access — either holding guest threads off during the placeholder window, or mapping new backing over the old without an intermediate unmapped state. That is a larger change to the memory manager and is not attempted here.

Validation

  • Built on Windows with clang-cl/Ninja and verified in game from the same change in my working tree; the PR branch itself was not built in isolation because that checkout lacks the vendored submodules.
  • GTA5 previously crashed reproducibly during world streaming; with this change the first mission completes with zero faults.
  • No change in behaviour for faults the GPU handler already services, or for addresses the guest map does not report as committed.

AI assistance

AI assistance was used for this change.

**@⁠CheesyPoofs346** on GitHub · 2026-08-31 22:45 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/435) `CheesyPoofs346:fix/retry-transient-map-faults` → `main` · +22 −0 · 1 files · 1 commits · closed without merging ## Summary - Retry a faulting instruction when the guest memory map reports the address as committed, instead of aborting the emulator. - Covers the window where guest memory is briefly unmapped during a remap. ## Problem `MapBacking` / `UnmapBacking` remap guest memory through placeholders, so there is a short window in which the pages are not mapped. A guest thread that touches the range during that window takes an access violation on memory that is committed both before and after the remap, and `KytyExceptionHandler` turns any fault it does not recognise into a fatal `EXIT`. Instrumenting the exception path with `VirtualQuery` showed the state changing underneath two adjacent calls. `at` is queried before `prev`, yet they disagree about the same range: ``` Unhandled host exception: code=0xC0000005 access=write address=0x0000001038 2e6000 | at base=0x10382e6000 size=0xa000 state=MEM_RESERVE protect=0 | prev base=0x10382e5000 end=0x10382f0000 state=MEM_COMMIT protect=PAGE_READWRITE | guest 0x10382e0000-0x10382f0000 committed=1 direct=1 ``` Both cover `0x10382e6000-0x10382f0000`. The range was unmapped when the first query ran and mapped when the second did, so the fault is transient rather than a real invalid access. Logging every `MapBacking` failure produced no hits, confirming nothing failed — the fault falls between the unmap and the remap. Observed in Grand Theft Auto V (PPSA04264) as a reproducible crash while the game streams world data (driving, entering vehicles). With this change the same session completes the first mission with no faults, where previously it aborted every time. The retry is bounded to 4096 attempts per faulting address, so an address that never becomes valid still reaches the existing fatal path with its diagnostics intact. ## Scope and limits This is a mitigation, not a removal of the race. The complete fix is to make the remap atomic with respect to guest access — either holding guest threads off during the placeholder window, or mapping new backing over the old without an intermediate unmapped state. That is a larger change to the memory manager and is not attempted here. ## Validation - Built on Windows with clang-cl/Ninja and verified in game from the same change in my working tree; the PR branch itself was not built in isolation because that checkout lacks the vendored submodules. - GTA5 previously crashed reproducibly during world streaming; with this change the first mission completes with zero faults. - No change in behaviour for faults the GPU handler already services, or for addresses the guest map does not report as committed. ## AI assistance AI assistance was used for this change.
Remapping guest memory is not atomic: MapBacking/UnmapBacking go through placeholders, so the pages are briefly absent and a guest thread touching them in that window faults on memory that is committed both before and after. Retry the faulting instruction rather than aborting, bounded to 4096 attempts per address so a genuinely bad access still fails.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:20:45 +00:00
github-bot closed this pull request 2026-11-26 09:20:45 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-08-31 22:45 UTC · original · reviewed

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-08-31 22:45 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/435#pullrequestreview-5072100901) · **reviewed** Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:47 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d3d046775

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/435#pullrequestreview-5072120373) · **reviewed** ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `5d3d046775` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
@ -803,0 +817,4 @@
last_vaddr = info->access_violation_vaddr;
retries = 0;
}
if (++retries <= 4096) {
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-08-31 22:47 UTC · original

P2 Badge Reset the retry budget between separate fault episodes

If the same virtual address is transiently faulted during multiple successful remaps, these thread-local retries accumulate because a successful instruction execution never returns here to clear the counter, and the counter is reset only when a different fault address is observed. Consequently, the 4097th otherwise recoverable fault at that address falls through to the fatal path, so long-running workloads that repeatedly remap a hot address can still crash despite every individual remap completing successfully.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-08-31 22:47 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/435#discussion_r3899096659) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Reset the retry budget between separate fault episodes** If the same virtual address is transiently faulted during multiple successful remaps, these thread-local retries accumulate because a successful instruction execution never returns here to clear the counter, and the counter is reset only when a different fault address is observed. Consequently, the 4097th otherwise recoverable fault at that address falls through to the fatal path, so long-running workloads that repeatedly remap a hot address can still crash despite every individual remap completing successfully. Useful? React with 👍 / 👎.
Author
Owner

@⁠Nmzik on GitHub · 2026-09-01 20:53 UTC · original

Hack

**@⁠Nmzik** on GitHub · 2026-09-01 20:53 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/435#issuecomment-5500291931) Hack

Pull request closed

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