graphics: qualify a GPU page fault on the faulting byte #422

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

@⁠CheesyPoofs346 on GitHub · 2026-08-31 11:03 UTC · original
CheesyPoofs346:fix/gpu-fault-mapping-boundary → main · +5 −2 · 1 files · 1 commits · closed without merging

Summary

  • Qualify a GPU page fault on the faulting byte instead of an eight-byte window.
  • A fault whose address lies within eight bytes of the end of a mapped range is currently declined, which aborts the emulator.

Problem

GpuResourceManager::HandleFault decides whether a fault belongs to the GPU tracker like this:

constexpr uint64_t fault_size = 8;
if (!IsMapped(fault_vaddr, fault_size)) {
    return false;
}

A page fault reports a single address, but IsMapped is asked whether eight bytes starting at that address are mapped, and it requires the whole span to be contained in one range:

return m_mapped_ranges.Contains(vaddr, size);

When the faulting address lies within eight bytes of the end of a mapping, the span runs past that end, IsMapped returns false, and HandleFault declines. The caller in runtimeLinker.cpp then treats the fault as unrecognised and calls EXIT, so the emulator aborts on a page the tracker had itself write-protected.

Observed in Grand Theft Auto V (PPSA04264) as a recurring crash while the game streams world data. Instrumenting the exception path with VirtualQuery gave:

Unhandled host exception: code=0xC0000005 access=write address=0x000000151387fffa
  | at   base=0x151387f000 size=0x1000 state=MEM_COMMIT protect=PAGE_READONLY
  | prev base=0x151387e000 end=0x0000001513880000

The faulting page is committed and PAGE_READONLY — write-protected by the tracker — so this is a fault the handler was meant to service. 0x151387fffa + 8 = 0x1513880002 crosses the mapping's end at 0x1513880000, so the eight-byte test fails. Every crash address collected over several sessions sat within eight bytes of a mapping boundary.

After the change the write-protected-page crash no longer occurs; a separate, unrelated fault in that title now surfaces later with a different signature (MEM_RESERVE, protect=0), which I am still investigating and which is not addressed here.

Nothing else is altered: the widening to eight bytes is kept wherever the mapping actually extends that far, so the invalidation range is unchanged in the common case.

AI assistance

AI assistance was used for this change.

**@⁠CheesyPoofs346** on GitHub · 2026-08-31 11:03 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/422) `CheesyPoofs346:fix/gpu-fault-mapping-boundary` → `main` · +5 −2 · 1 files · 1 commits · closed without merging ## Summary - Qualify a GPU page fault on the faulting byte instead of an eight-byte window. - A fault whose address lies within eight bytes of the end of a mapped range is currently declined, which aborts the emulator. ## Problem `GpuResourceManager::HandleFault` decides whether a fault belongs to the GPU tracker like this: ```cpp constexpr uint64_t fault_size = 8; if (!IsMapped(fault_vaddr, fault_size)) { return false; } ``` A page fault reports a single address, but `IsMapped` is asked whether **eight bytes starting at that address** are mapped, and it requires the whole span to be contained in one range: ```cpp return m_mapped_ranges.Contains(vaddr, size); ``` When the faulting address lies within eight bytes of the end of a mapping, the span runs past that end, `IsMapped` returns false, and `HandleFault` declines. The caller in `runtimeLinker.cpp` then treats the fault as unrecognised and calls `EXIT`, so the emulator aborts on a page the tracker had itself write-protected. Observed in Grand Theft Auto V (PPSA04264) as a recurring crash while the game streams world data. Instrumenting the exception path with `VirtualQuery` gave: ``` Unhandled host exception: code=0xC0000005 access=write address=0x000000151387fffa | at base=0x151387f000 size=0x1000 state=MEM_COMMIT protect=PAGE_READONLY | prev base=0x151387e000 end=0x0000001513880000 ``` The faulting page is committed and `PAGE_READONLY` — write-protected by the tracker — so this is a fault the handler was meant to service. `0x151387fffa + 8 = 0x1513880002` crosses the mapping's end at `0x1513880000`, so the eight-byte test fails. Every crash address collected over several sessions sat within eight bytes of a mapping boundary. After the change the write-protected-page crash no longer occurs; a separate, unrelated fault in that title now surfaces later with a different signature (`MEM_RESERVE`, `protect=0`), which I am still investigating and which is not addressed here. Nothing else is altered: the widening to eight bytes is kept wherever the mapping actually extends that far, so the invalidation range is unchanged in the common case. ## AI assistance AI assistance was used for this change.
HandleFault asked whether eight bytes starting at the faulting address were mapped, and IsMapped requires the whole span to sit inside one range. A fault whose address lies within eight bytes of the end of a mapping therefore failed that test, the handler declined a fault on a page the tracker had itself write-protected, and the caller turned it into a fatal unhandled host exception. Qualify on the faulting byte and widen to eight only when the mapping extends that far.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:20:03 +00:00
github-bot closed this pull request 2026-11-26 09:20:03 +00:00
Author
Owner

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

Pull request overview

This PR adjusts GPU page-fault qualification in GpuResourceManager::HandleFault to avoid incorrectly rejecting faults that occur near the end of a mapped range, preventing fatal “unrecognized” faults that abort the emulator.

Changes:

  • Switches fault qualification from an 8-byte “must all be mapped” check to qualifying on the faulting byte.
  • Keeps the broader invalidation/read window where the mapping supports it, avoiding boundary-related false negatives.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-08-31 11:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/422#pullrequestreview-5065750650) · **reviewed** ## Pull request overview This PR adjusts GPU page-fault qualification in `GpuResourceManager::HandleFault` to avoid incorrectly rejecting faults that occur near the end of a mapped range, preventing fatal “unrecognized” faults that abort the emulator. **Changes:** - Switches fault qualification from an 8-byte “must all be mapped” check to qualifying on the faulting byte. - Keeps the broader invalidation/read window where the mapping supports it, avoiding boundary-related false negatives. --- 💡 <a href="/KytyPS5/KytyPS5/new/main?filename=.github/skills/code-review/SKILL.md" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Add a `code-review` agent skill</a> or configure MCP servers for context-aware, tailored reviews. <a href="https://docs.github.com/copilot/how-tos/use-copilot-agents/request-a-code-review/use-code-review?tool=webui#mcp-servers-and-agent-skills" class="Link--inTextBlock" target="_blank" rel="noopener noreferrer">Learn more in the docs.</a>
@ -17,2 +18,4 @@
const uint64_t fault_size = IsMapped(fault_vaddr, 8) ? 8 : 1;
if (!IsMapped(fault_vaddr, 1)) {
return false;
}
Author
Owner

@⁠Copilot on GitHub · 2026-08-31 11:06 UTC · original

The new comment says to qualify on the faulting byte and then widen only as far as the mapping allows, but the implementation computes the 8-byte window check first and only ever chooses 8 or 1. This also does two separate RangeSet lookups/locks in a fault path. Consider computing the maximum contiguous mapped span starting at fault_vaddr (up to 8) under a single shared_lock, and returning false if the span is 0.

**@⁠Copilot** on GitHub · 2026-08-31 11:06 UTC · [original](https://github.com/KytyPS5/KytyPS5/pull/422#discussion_r3893873519) The new comment says to qualify on the faulting byte and then widen only as far as the mapping allows, but the implementation computes the 8-byte window check first and only ever chooses 8 or 1. This also does two separate RangeSet lookups/locks in a fault path. Consider computing the maximum contiguous mapped span starting at fault_vaddr (up to 8) under a single shared_lock, and returning false if the span is 0.

Pull request closed

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