Skip to content

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

Merged
boykopovar merged 2 commits into
boykopovar:mainfrom
MichonGoddijn231849:fix/host-import-shared-alias
Sep 30, 2026
Merged

boykopovar merged 2 commits into
boykopovar:mainfrom
MichonGoddijn231849:fix/host-import-shared-alias

Conversation

@MichonGoddijn231849

@MichonGoddijn231849 MichonGoddijn231849 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

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 perf(agc): remember shader validation failures and reject repeats without throwing #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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Sep 30, 2026
…ever offer read-only pages

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@boykopovar

Copy link
Copy Markdown
Owner

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.

…r copied exactly

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@MichonGoddijn231849

Copy link
Copy Markdown
Contributor Author

Done in f4be8f4, 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.

@boykopovar
boykopovar merged commit f66789a into boykopovar:main Sep 30, 2026
3 checks passed
@MichonGoddijn231849
MichonGoddijn231849 deleted the fix/host-import-shared-alias branch October 9, 2026 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* libc

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants