fix(libc): back the main image's writable mappings with anonymous memory on Linux #1176

Merged
EdouardSence merged 1 commit from upstream-pr/1176 into main 2026-10-08 08:41:36 +00:00
EdouardSence commented 2026-10-07 16:44:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-07T16:44:07Z.


What

On Linux with RADV, the main executable's writable segments are never host-imported. amdgpu userptr refuses file-backed VMAs (libdrm sets AMDGPU_GEM_USERPTR_ANONONLY, the same cause #943 describes for memfd direct memory), and the executable's rw-p PT_LOAD mappings are file-backed MAP_PRIVATE. They become writable image mirrors, and AcquireRegistered compares every 64 KiB block of them on each address-based build.

On PPSA21567 that is ~133 MiB per build. Sampling the queue worker with eu-stack (80 samples during the intro), 70 were in AcquireRegistered → compareBlocks → compareBlock (memcmp), and the three RefreshPool helpers sat at 100% doing the same.

This change: at libc load, before guest code runs, each rw-p mapping of /proc/self/exe is replaced by an anonymous copy at the same address (mmap anonymous, memcpy, mremap(MREMAP_FIXED)). Contents, address and protection are unchanged; RELRO and read-only mappings are untouched. The ranges then import like any other anonymous memory and no writable mirror is created. Windows is unchanged.

New test guest_image_anonymous (Linux): an executable linked with libc checks that no rw-p mapping of its own file remains and that its initialized data is intact and writable. It fails without the change and passes with it.

Tested

  • Arch Linux (CachyOS), kernel 7.2.8, GCC 16.2.1, Ryzen 7 7800X3D, RX 9070 XT (RADV GFX1201, Mesa 26.2.3).
  • ctest: 399/399 pass on main 4b16995d with the change.
  • PPSA21567, on oneandonlydean's astrobot (93faece1) with #943 cherry-picked, udmabuf.size_limit_mb=8192, APS5_NO_SNAPSHOT_CHECK=1, warm shader and pipeline caches, APS5_PROFILE_DRAW=1, no input (intro):
without with
writable image mirrors 3 (blocks compared per 10 s: ~98M cumulative) 0
[drawkinds] BDA resources per draw 3701 µs 12.9 µs
presents per 10 s after the logos 35–62 123–246

upstream main alone can't reach this point on RADV (it stops on unsupported descriptor role Gds, then on the address-copy limit without #943), so the numbers are from that stack.

Not caused by this change, recorded for completeness: on that stack both arms end in an amdgpu GPU page fault and ring timeout (without the change once in a 15 min run, at ~3 min, SQC (data) permission fault; with it in 2 of 3 runs at ~50 s and ~150 s). After the device is lost the process hangs instead of exiting.

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 (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes (Claude Code wrote the investigation, the code and this description; the runs above are on my machine)

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4775672056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1176). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-07T16:44:07Z. --- ### What On Linux with RADV, the main executable's writable segments are never host-imported. amdgpu userptr refuses file-backed VMAs (libdrm sets `AMDGPU_GEM_USERPTR_ANONONLY`, the same cause #943 describes for memfd direct memory), and the executable's `rw-p` PT_LOAD mappings are file-backed `MAP_PRIVATE`. They become writable image mirrors, and `AcquireRegistered` compares every 64 KiB block of them on each address-based build. On PPSA21567 that is ~133 MiB per build. Sampling the queue worker with `eu-stack` (80 samples during the intro), 70 were in `AcquireRegistered` → `compareBlocks` → `compareBlock` (`memcmp`), and the three `RefreshPool` helpers sat at 100% doing the same. This change: at libc load, before guest code runs, each `rw-p` mapping of `/proc/self/exe` is replaced by an anonymous copy at the same address (`mmap` anonymous, `memcpy`, `mremap(MREMAP_FIXED)`). Contents, address and protection are unchanged; RELRO and read-only mappings are untouched. The ranges then import like any other anonymous memory and no writable mirror is created. Windows is unchanged. New test `guest_image_anonymous` (Linux): an executable linked with libc checks that no `rw-p` mapping of its own file remains and that its initialized data is intact and writable. It fails without the change and passes with it. ### Tested - Arch Linux (CachyOS), kernel 7.2.8, GCC 16.2.1, Ryzen 7 7800X3D, RX 9070 XT (RADV GFX1201, Mesa 26.2.3). - `ctest`: 399/399 pass on `main` 4b16995d with the change. - PPSA21567, on oneandonlydean's `astrobot` (93faece1) with #943 cherry-picked, `udmabuf.size_limit_mb=8192`, `APS5_NO_SNAPSHOT_CHECK=1`, warm shader and pipeline caches, `APS5_PROFILE_DRAW=1`, no input (intro): | | without | with | |---|---|---| | writable image mirrors | 3 (blocks compared per 10 s: ~98M cumulative) | 0 | | `[drawkinds]` BDA `resources` per draw | 3701 µs | 12.9 µs | | presents per 10 s after the logos | 35–62 | 123–246 | `upstream main` alone can't reach this point on RADV (it stops on `unsupported descriptor role Gds`, then on the address-copy limit without #943), so the numbers are from that stack. Not caused by this change, recorded for completeness: on that stack both arms end in an amdgpu GPU page fault and ring timeout (without the change once in a 15 min run, at ~3 min, `SQC (data)` permission fault; with it in 2 of 3 runs at ~50 s and ~150 s). After the device is lost the process hangs instead of exiting. ### 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](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [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 (Claude Code wrote the investigation, the code and this description; the runs above are on my machine) 🤖 Generated with [Claude Code](https://claude.com/claude-code)
adriadam10 commented 2026-10-08 08:41:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T08:41:33Z.


Checked on current main: build + ctest 425/425 green; guest_image_anonymous passes and fails without the GuestAllocations.cpp change. Reviewed the remap: only rw-p mappings of the exe are replaced (RELRO and anonymous bss untouched), same address/protection, failures throw. Linux and Windows CI green. The title-level timings can't be reproduced here; merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6056113324 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1176#issuecomment-6056113324). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T08:41:33Z. --- Checked on current main: build + ctest 425/425 green; `guest_image_anonymous` passes and fails without the `GuestAllocations.cpp` change. Reviewed the remap: only `rw-p` mappings of the exe are replaced (RELRO and anonymous bss untouched), same address/protection, failures throw. Linux and Windows CI green. The title-level timings can't be reproduced here; merging.
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!1176
No description provided.