fix(agc): import shared guest memory through a read-write alias and never offer read-only pages #186

Merged
MichonGoddijn231849 merged 2 commits from upstream-pr/186 into main 2026-09-30 17:41:24 +00:00
MichonGoddijn231849 commented 2026-09-30 15:14:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-09-30T15:14:06Z.


What

On NVIDIA (RTX 5080, driver 610.74), vkAllocateMemory refuses a host-pointer import (VK_ERROR_OUT_OF_DEVICE_MEMORY) when any page in the range is read-only. After one refusal, later large imports in the same process fail too, even of memory that imported fine before.

main arms shared (direct memory) pages read-only for write tracking. So the first import of a direct-memory range is refused, and from then on every import fails ("out of memory ... retried later", thousands of times). Every BDA build then falls back to heap mirrors refreshed by copying: single dispatches took 11–16 s in "address bindings (bda)".

This change:

  • A range whose pages are all read-write (private memory, or the relinked executable's writable and copy-on-write image pages) is imported directly.
  • A range made only of views of one shared section (contiguous offsets, a 64 KiB-aligned start) is imported through a separate read-write view of that section (GuestArenaMapAlias). Write tracking never arms that view; the GPU reads the same pages, so CPU writes stay visible. The view is unmapped when the import is destroyed. WindowsMappings keeps a duplicated section handle per mapping, so the alias can be made after the caller closed its own handle.
  • A read-only range (PAGE_READONLY / PAGE_EXECUTE_READ, not shared) is copied on purpose. It is the one kind the driver refuses, and it can't change without a protection change, which updates the registry, so the copy is exact.
  • Anything else throws, naming the address, type and protection. So does a shared range that can't be aliased (the reason is in the message), and a failing MapViewOfFile3 (with GetLastError()).

Tested

Windows 11, RTX 5080 (driver 610.74), MinGW GCC 15.2.

Standalone Vulkan test, each case in a fresh process, 64 MiB unless noted:

Memory Import
VirtualAlloc RW, with or without MEM_WRITE_WATCH ok
placeholder replaced by private RW, with or without MEM_WRITE_WATCH ok
section view RW ok
private RW with one read-only page -2
after that -2: a range that imported before -2
after that -2: fresh 16 MiB or 1 MiB ok
a second RW view of a section whose guest view has read-only pages ok, and coherent with writes through the guest view
writable image pages (.data), and untouched copy-on-write image pages ok

GTA V (PPSA04264), with #167, measured before the rebase onto the latest main (the Windows import path is unchanged by it):

  • before: the first 128 MiB direct-memory import was refused and every later one was deferred (thousands);
  • after: 0 refused, 67 imported during boot. Intro, menus and loading are no longer stalled by mirror refreshes.
  • With the exceptions above: GTA V reaches the world (3–4 fps with #192). The first ranges they caught were the executable's read-only data (now copied), and its writable and copy-on-write data (now imported).
  • In game it is still slow; that is a separate cost (skipped draws), not covered here.

ctest on current main with this change: the libc and guest tests pass. Not run on this machine:

  • agc_driver, agc_shader_memory, agc_driver_flip and agc_driver_pm4 exit with 0xc0000139 at startup, the same as on plain main before the rebase;
  • agc_driver_graphics doesn't build here (tests/Graphics.cpp:1246, from the recent merges, untouched here);
  • empty_tls, tls_function_coverage and windows_gui (relinker) fail.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4692387562 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/186). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-09-30T15:14:06Z. --- ### What On NVIDIA (RTX 5080, driver 610.74), `vkAllocateMemory` refuses a host-pointer import (`VK_ERROR_OUT_OF_DEVICE_MEMORY`) when any page in the range is read-only. After one refusal, later large imports in the same process fail too, even of memory that imported fine before. `main` arms shared (direct memory) pages read-only for write tracking. So the first import of a direct-memory range is refused, and from then on every import fails ("out of memory ... retried later", thousands of times). Every BDA build then falls back to heap mirrors refreshed by copying: single dispatches took 11–16 s in "address bindings (bda)". This change: - A range whose pages are all read-write (private memory, or the relinked executable's writable and copy-on-write image pages) is imported directly. - A range made only of views of one shared section (contiguous offsets, a 64 KiB-aligned start) is imported through a separate read-write view of that section (`GuestArenaMapAlias`). Write tracking never arms that view; the GPU reads the same pages, so CPU writes stay visible. The view is unmapped when the import is destroyed. `WindowsMappings` keeps a duplicated section handle per mapping, so the alias can be made after the caller closed its own handle. - A read-only range (`PAGE_READONLY` / `PAGE_EXECUTE_READ`, not shared) is copied on purpose. It is the one kind the driver refuses, and it can't change without a protection change, which updates the registry, so the copy is exact. - Anything else throws, naming the address, type and protection. So does a shared range that can't be aliased (the reason is in the message), and a failing `MapViewOfFile3` (with `GetLastError()`). ### Tested Windows 11, RTX 5080 (driver 610.74), MinGW GCC 15.2. Standalone Vulkan test, each case in a fresh process, 64 MiB unless noted: | Memory | Import | |---|---| | `VirtualAlloc` RW, with or without `MEM_WRITE_WATCH` | ok | | placeholder replaced by private RW, with or without `MEM_WRITE_WATCH` | ok | | section view RW | ok | | private RW with **one** read-only page | `-2` | | after that `-2`: a range that imported before | `-2` | | after that `-2`: fresh 16 MiB or 1 MiB | ok | | a second RW view of a section whose guest view has read-only pages | ok, and coherent with writes through the guest view | | writable image pages (`.data`), and untouched copy-on-write image pages | ok | GTA V (PPSA04264), with #167, measured before the rebase onto the latest `main` (the Windows import path is unchanged by it): - before: the first 128 MiB direct-memory import was refused and every later one was deferred (thousands); - after: 0 refused, 67 imported during boot. Intro, menus and loading are no longer stalled by mirror refreshes. - With the exceptions above: GTA V reaches the world (3–4 fps with #192). The first ranges they caught were the executable's read-only data (now copied), and its writable and copy-on-write data (now imported). - In game it is still slow; that is a separate cost (skipped draws), not covered here. `ctest` on current `main` with this change: the libc and guest tests pass. Not run on this machine: - `agc_driver`, `agc_shader_memory`, `agc_driver_flip` and `agc_driver_pm4` exit with `0xc0000139` at startup, the same as on plain `main` before the rebase; - `agc_driver_graphics` doesn't build here (`tests/Graphics.cpp:1246`, from the recent merges, untouched here); - `empty_tls`, `tls_function_coverage` and `windows_gui` (relinker) fail. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes. Written with Claude (Anthropic) under my direction; I reviewed and tested it. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
boykopovar commented 2026-09-30 16:52:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by boykopovar on 2026-09-30T16:52:35Z.


Instead of quiet fallbacks, let's throw a clear exception in these places:

  • The memory is not fully private RW, and an alias cannot be obtained (currently skips the import and falls back to copying).
  • MapViewOfFile3 fails to create the RW alias (currently returns nullptr silently). Include the Windows error from GetLastError() in the exception.

If after this the game starts to crash earlier than before the changes, this will only show the real problem.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5915771761 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/186#issuecomment-5915771761). Originally posted by [boykopovar](https://github.com/boykopovar) on 2026-09-30T16:52:35Z. --- Instead of quiet fallbacks, let's throw a clear exception in these places: - The memory is not fully private RW, and an alias cannot be obtained (currently skips the import and falls back to copying). - `MapViewOfFile3` fails to create the RW alias (currently returns `nullptr` silently). Include the Windows error from `GetLastError()` in the exception. If after this the game starts to crash earlier than before the changes, this will only show the real problem.
MichonGoddijn231849 commented 2026-09-30 17:14:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MichonGoddijn231849 on 2026-09-30T17:14:51Z.


Done in f4be8f43, thanks. Both now throw with the reason:

  • A range that is neither read-write, read-only nor a shared mapping throws, naming the address, type and protection.
  • A shared range that can't be aliased throws with why: not whole shared pages, not one contiguous run of a section, or the offset not on the allocation granularity.
  • A failing MapViewOfFile3 throws with GetLastError().

It surfaced two things in GTA V right away. Both are in the relinked executable's own image, read by compute shader 0x242d47200:

  • Writable and copy-on-write data (MEM_IMAGE, PAGE_READWRITE / PAGE_WRITECOPY). I checked in a standalone test: NVIDIA imports those fine, so they are imported now instead of falling back.
  • Read-only data (MEM_IMAGE, PAGE_READONLY). That is the one kind the driver refuses to import. It can't change without a protection change, which already updates the registry, so it is copied on purpose; every other non-importable range throws.

With that, GTA V reaches the world again. If you'd rather not copy read-only memory, the alternative is for the loader to place the executable's read-only sections in private memory, so they import like the rest. Happy to do that instead.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5916116456 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/186#issuecomment-5916116456). Originally posted by [MichonGoddijn231849](https://github.com/MichonGoddijn231849) on 2026-09-30T17:14:51Z. --- Done in f4be8f43, thanks. Both now throw with the reason: - A range that is neither read-write, read-only nor a shared mapping throws, naming the address, type and protection. - A shared range that can't be aliased throws with why: not whole shared pages, not one contiguous run of a section, or the offset not on the allocation granularity. - A failing `MapViewOfFile3` throws with `GetLastError()`. It surfaced two things in GTA V right away. Both are in the relinked executable's own image, read by compute shader `0x242d47200`: - **Writable and copy-on-write data** (`MEM_IMAGE`, `PAGE_READWRITE` / `PAGE_WRITECOPY`). I checked in a standalone test: NVIDIA imports those fine, so they are imported now instead of falling back. - **Read-only data** (`MEM_IMAGE`, `PAGE_READONLY`). That is the one kind the driver refuses to import. It can't change without a protection change, which already updates the registry, so it is copied on purpose; every other non-importable range throws. With that, GTA V reaches the world again. If you'd rather not copy read-only memory, the alternative is for the loader to place the executable's read-only sections in private memory, so they import like the rest. Happy to do that instead.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!186
No description provided.