feat(libc): arm compared and resident guest memory under APS5_ARM_COMPARED and APS5_ARM_RESIDENT #16372

Open
Nesyth wants to merge 20 commits from upstream-pr/16372 into main
Nesyth commented 2026-11-06 12:55:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Nesyth on 2026-11-06T12:55:42Z.


What

On Windows, guest stores into write-protected guest pages fault, and the fault handler marks the pages written. That is how guest memory is tracked without walking GetWriteWatch or comparing bytes. #361 limited it: a fault resumed in guest code overwrites the System V red zone [rsp-0x80, rsp) of an unframed leaf function. So host-imported shared pages are compared byte by byte instead, and resident runs are not armed.

The relinker relocation PR (#16371) frames or relocates every red-zone leaf of a relinked image. With that in place, this PR brings arming back behind two opt-in switches:

  • APS5_ARM_COMPARED=1: host-imported shared pages are no longer marked compare-when-shared. A clearing collect write-protects them like any other shared page, so a guest store faults instead of every proof comparing their bytes.
  • APS5_ARM_RESIDENT=1: a clearing collect write-protects a settled, write-watched resident span of a run of at least 256 KiB that no guest store has reached since it was armed, and remembers it clean. Later collects there skip GetWriteWatch. The first guest store faults and returns the whole run to write watching for good, so a run the guest keeps writing costs one fault. It is a runtime switch, so APS5_AB can toggle it.

Both stay off by default. They are safe only for images relinked with --frame-red-zone and APS5_RELINK_RELOCATE_RED_ZONE=1.

How

  • GuestArenaCompareWhenShared_nid_no_patch and GuestArenaCompareResidentWhenShared_nid_no_patch return early under APS5_ARM_COMPARED.
  • WindowsMappings::collectResident arms a span when the run is watched, settled, unwritten since its last arming and at least 256 KiB long. It requires every view in the span to be resident with the same protection and no host writes, host imports or pins.
  • A store into an armed resident view, a pin of an armed view and turning the switch off all resume the run: its protection is restored and its clean ranges are forgotten. A demoted run forgets its clean ranges as well.
  • The driver passes the runtime switch to libc on the next walk (GuestArenaSetArmResident_nid_no_patch).

Tested

Windows 11 Pro 26300, Radeon RX 7900 XTX (AMD Software 26.10.1), Ryzen 7 7800X3D, MinGW-w64 GCC 15.2, Ninja, Release, BUILD_TESTING=ON, this branch on #16371 on main 65ad958fb9.

  • Full build: clean.
  • Focused tests, 23 of 23 pass: guest_memory, the new guest_memory_armed registration (APS5_ARM_RESIDENT=1, APS5_ARM_COMPARED=1; CheckLargeResidentWritesDoNotFault then expects the settled span read-only and exactly one fault), agc_rewatched_direct_shared, agc_import_alias_shared, agc_compared_shared_generations, the write tracking tests, agc_runtime_switches, the relinker and red-zone framer tests and cli_args.
  • ^agc: 1384 of 1386 pass. The two failures, agc_driver_direct_write_completion and agc_driver_sample_order_grid_reference, also fail on this machine without this branch.
  • In game, PPSA04264, Performance graphics mode, open-world gameplay, relinked with --frame-red-zone and APS5_RELINK_RELOCATE_RED_ZONE=1 (617 red-zone leaves: 441 framed in place, 128 relocated whole), APS5_DRAW_PIPELINE=1 in every run (the configuration #15456 makes the default), one binary throughout. No crash and no device loss in any run, with or without the switches.
  • The effect is measured in-run, because the open-world camera drifts without input so each boot loads a different amount of traffic and cross-boot frame rates are not comparable (five 180 s boots ran 97-135 ms median regardless of the switches). In-run A/B of APS5_ARM_RESIDENT with APS5_ARM_COMPARED=1 in both arms (APS5_AB=30:APS5_ARM_RESIDENT, 30 s phases from 190 s, 18 phase pairs, each B phase against its neighbouring A phases, slope-corrected at equal commits per present): -1.99 +- 0.72 ms, -1.7 +- 0.6% of a 117.5 ms frame. This is resident arming alone; compared-memory arming removes the byte compares that a separate proof otherwise runs (they were about 5% of the committer before arming), but it has no in-run switch, so it is covered by the boots above rather than an A/B.

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 under Silent stubs in their library's technical debt
  • 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: #16371 (this branch starts with its 12 commits; the 13th is this change)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: AMD Radeon RX 7900 XTX, Windows 11
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5188145609 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16372). Originally posted by [Nesyth](https://github.com/Nesyth) on 2026-11-06T12:55:42Z. --- ### What On Windows, guest stores into write-protected guest pages fault, and the fault handler marks the pages written. That is how guest memory is tracked without walking `GetWriteWatch` or comparing bytes. #361 limited it: a fault resumed in guest code overwrites the System V red zone `[rsp-0x80, rsp)` of an unframed leaf function. So host-imported shared pages are compared byte by byte instead, and resident runs are not armed. The relinker relocation PR (#16371) frames or relocates every red-zone leaf of a relinked image. With that in place, this PR brings arming back behind two opt-in switches: - `APS5_ARM_COMPARED=1`: host-imported shared pages are no longer marked compare-when-shared. A clearing collect write-protects them like any other shared page, so a guest store faults instead of every proof comparing their bytes. - `APS5_ARM_RESIDENT=1`: a clearing collect write-protects a settled, write-watched resident span of a run of at least 256 KiB that no guest store has reached since it was armed, and remembers it clean. Later collects there skip `GetWriteWatch`. The first guest store faults and returns the whole run to write watching for good, so a run the guest keeps writing costs one fault. It is a runtime switch, so `APS5_AB` can toggle it. Both stay off by default. They are safe only for images relinked with `--frame-red-zone` and `APS5_RELINK_RELOCATE_RED_ZONE=1`. ### How - `GuestArenaCompareWhenShared_nid_no_patch` and `GuestArenaCompareResidentWhenShared_nid_no_patch` return early under `APS5_ARM_COMPARED`. - `WindowsMappings::collectResident` arms a span when the run is watched, settled, unwritten since its last arming and at least 256 KiB long. It requires every view in the span to be resident with the same protection and no host writes, host imports or pins. - A store into an armed resident view, a pin of an armed view and turning the switch off all resume the run: its protection is restored and its clean ranges are forgotten. A demoted run forgets its clean ranges as well. - The driver passes the runtime switch to libc on the next walk (`GuestArenaSetArmResident_nid_no_patch`). ### Tested Windows 11 Pro 26300, Radeon RX 7900 XTX (AMD Software 26.10.1), Ryzen 7 7800X3D, MinGW-w64 GCC 15.2, Ninja, Release, `BUILD_TESTING=ON`, this branch on #16371 on `main` `65ad958fb9`. - Full build: clean. - Focused tests, 23 of 23 pass: `guest_memory`, the new `guest_memory_armed` registration (`APS5_ARM_RESIDENT=1`, `APS5_ARM_COMPARED=1`; `CheckLargeResidentWritesDoNotFault` then expects the settled span read-only and exactly one fault), `agc_rewatched_direct_shared`, `agc_import_alias_shared`, `agc_compared_shared_generations`, the write tracking tests, `agc_runtime_switches`, the relinker and red-zone framer tests and `cli_args`. - `^agc`: 1384 of 1386 pass. The two failures, `agc_driver_direct_write_completion` and `agc_driver_sample_order_grid_reference`, also fail on this machine without this branch. - **In game, PPSA04264**, Performance graphics mode, open-world gameplay, relinked with `--frame-red-zone` and `APS5_RELINK_RELOCATE_RED_ZONE=1` (617 red-zone leaves: 441 framed in place, 128 relocated whole), `APS5_DRAW_PIPELINE=1` in every run (the configuration #15456 makes the default), one binary throughout. No crash and no device loss in any run, with or without the switches. - The effect is measured in-run, because the open-world camera drifts without input so each boot loads a different amount of traffic and cross-boot frame rates are not comparable (five 180 s boots ran 97-135 ms median regardless of the switches). In-run A/B of `APS5_ARM_RESIDENT` with `APS5_ARM_COMPARED=1` in both arms (`APS5_AB=30:APS5_ARM_RESIDENT`, 30 s phases from 190 s, 18 phase pairs, each B phase against its neighbouring A phases, slope-corrected at equal commits per present): **-1.99 +- 0.72 ms, -1.7 +- 0.6% of a 117.5 ms frame**. This is resident arming alone; compared-memory arming removes the byte compares that a separate proof otherwise runs (they were about 5% of the committer before arming), but it has no in-run switch, so it is covered by the boots above rather than an A/B. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) - [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: #16371 (this branch starts with its 12 commits; the 13th is this change) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: AMD Radeon RX 7900 XTX, Windows 11 - [x] AI-assisted: yes
MarcZX10 commented 2026-11-16 14:17:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T14:17:49Z.

Review: COMMENTED


Reviewed — no blocking findings

The targeted review found no verified blocking defect, but the review is incomplete because I did not read every changed file or run the build and tests.

What I ran

Reviewed head 605499656c5ecd1d6ea1cd8783052c845276d871 using the prepared review artifacts and targeted repository reads through repo_read.py. No build or tests were run.

Results

Check Result Note
Targeted code review passed No blocking defect verified in inspected code
Build and tests not run No execution environment available
Full changed-file review not run Several changed files were not read

Negative control

Not run; no tests were executed.

Findings

Blocking

None verified in the inspected code.

Non-blocking

None.

Sources

Read description.md, commits.md, manifest.json, and diff.patch, plus targeted ranges of WindowsMappings.hpp, RedZoneFramer.cpp, GuestMemory.cpp, and GuestArena.cpp at the pinned head.

Not verified

The full text of the remaining changed files was not read, and files.json was not available through the review reader. Build and test results in the PR description were author-reported and not independently verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5921475495 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16372#pullrequestreview-5921475495). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-16T14:17:49Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The targeted review found no verified blocking defect, but the review is incomplete because I did not read every changed file or run the build and tests. ## What I ran Reviewed head `605499656c5ecd1d6ea1cd8783052c845276d871` using the prepared review artifacts and targeted repository reads through `repo_read.py`. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Targeted code review | passed | No blocking defect verified in inspected code | | Build and tests | not run | No execution environment available | | Full changed-file review | not run | Several changed files were not read | ## Negative control Not run; no tests were executed. ## Findings ### Blocking None verified in the inspected code. ### Non-blocking None. ## Sources Read `description.md`, `commits.md`, `manifest.json`, and `diff.patch`, plus targeted ranges of `WindowsMappings.hpp`, `RedZoneFramer.cpp`, `GuestMemory.cpp`, and `GuestArena.cpp` at the pinned head. ## Not verified The full text of the remaining changed files was not read, and `files.json` was not available through the review reader. Build and test results in the PR description were author-reported and not independently verified. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-20 11:03:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:03:19Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change based on the inspected code; the author’s reported build and test results were not independently verified.

What I ran

Reviewed head 5b960b195b1154beb98b891de5c36edbc94fc32f using repo_read.py to inspect the prepared diff, changed-file manifest, and targeted code. No build or tests were run.

Results

Check Result Note
Prepared diff and targeted code passed Reviewed the memory arming and red-zone relocation paths
Build and tests not run Results in the PR description are author-reported

Findings

Blocking

None identified in the code inspected.

Non-blocking

None.

Not verified

The manifest lists 18 changed files, all marked included. The review folder contains no files/ copies, so repository files were read through repo_read.py. Read: core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp (changed hunk in diff only); core/libs/prx/libSceAgcDriver/Graphics/include/RuntimeSwitches.hpp; core/libs/prx/libSceAgcDriver/Graphics/src/RuntimeSwitches.cpp; core/libs/prx/libc/include/GuestArena.hpp (changed hunk in diff only); core/libs/prx/libc/include/WindowsMappings.hpp; core/libs/prx/libc/src/GuestArena.cpp (changed hunk in diff only); core/libs/prx/libkernel/CMakeLists.txt (changed hunk in diff only); core/libs/tests/GuestMemory.cpp (changed hunk in diff only); core/relinker/codegen/include/codegen/x86/RedZoneFramer.hpp (changed hunk in diff only); core/relinker/codegen/src/x86/RedZoneFramer.cpp; core/relinker/codegen/tests/RedZoneFramerExecutionTests.cpp (changed hunk in diff only); core/relinker/codegen/tests/RedZoneFramerTests.cpp; core/relinker/elfpatcher/src/windows/WindowsTrampolineBuilder.cpp (changed hunk in diff only); core/relinker/relinker/src/pipeline/Conversion.cpp (changed hunk in diff only); and docs/dev/ENVIRONMENT.md (changed hunk in diff only). The four changed CMake test registration files were inspected in the diff only. Full files and surrounding callers for those diff-only entries, as well as physical-console behavior and the reported performance measurements, were not independently verified. No tests were run, so test coverage and execution results are not confirmed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5991635983 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16372#pullrequestreview-5991635983). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:03:19Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change based on the inspected code; the author’s reported build and test results were not independently verified. ## What I ran Reviewed head `5b960b195b1154beb98b891de5c36edbc94fc32f` using `repo_read.py` to inspect the prepared diff, changed-file manifest, and targeted code. No build or tests were run. ## Results | Check | Result | Note | |---|---|---| | Prepared diff and targeted code | passed | Reviewed the memory arming and red-zone relocation paths | | Build and tests | not run | Results in the PR description are author-reported | ## Findings ### Blocking None identified in the code inspected. ### Non-blocking None. ## Not verified The manifest lists 18 changed files, all marked included. The review folder contains no `files/` copies, so repository files were read through `repo_read.py`. Read: `core/libs/prx/libSceAgcDriver/Execution/src/GuestMemory.cpp` (changed hunk in diff only); `core/libs/prx/libSceAgcDriver/Graphics/include/RuntimeSwitches.hpp`; `core/libs/prx/libSceAgcDriver/Graphics/src/RuntimeSwitches.cpp`; `core/libs/prx/libc/include/GuestArena.hpp` (changed hunk in diff only); `core/libs/prx/libc/include/WindowsMappings.hpp`; `core/libs/prx/libc/src/GuestArena.cpp` (changed hunk in diff only); `core/libs/prx/libkernel/CMakeLists.txt` (changed hunk in diff only); `core/libs/tests/GuestMemory.cpp` (changed hunk in diff only); `core/relinker/codegen/include/codegen/x86/RedZoneFramer.hpp` (changed hunk in diff only); `core/relinker/codegen/src/x86/RedZoneFramer.cpp`; `core/relinker/codegen/tests/RedZoneFramerExecutionTests.cpp` (changed hunk in diff only); `core/relinker/codegen/tests/RedZoneFramerTests.cpp`; `core/relinker/elfpatcher/src/windows/WindowsTrampolineBuilder.cpp` (changed hunk in diff only); `core/relinker/relinker/src/pipeline/Conversion.cpp` (changed hunk in diff only); and `docs/dev/ENVIRONMENT.md` (changed hunk in diff only). The four changed CMake test registration files were inspected in the diff only. Full files and surrounding callers for those diff-only entries, as well as physical-console behavior and the reported performance measurements, were not independently verified. No tests were run, so test coverage and execution results are not confirmed. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 22:37:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T22:37:29Z.

Review: COMMENTED


The trial merge onto c1601e56d5 conflicts in WindowsMappings.hpp and Conversion.cpp. Please revalidate the red-zone framing prerequisite on the resolved branch before enabling either arming switch. Keep distinct native Windows cases for compared and resident mappings, since their protection and proof paths differ. #18293 builds on the same switches, so the exact dependency order belongs in the comparison. I have no native Windows result.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6179840721 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16372#pullrequestreview-6179840721). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T22:37:29Z. **Review:** COMMENTED --- The trial merge onto `c1601e56d5` conflicts in `WindowsMappings.hpp` and `Conversion.cpp`. Please revalidate the red-zone framing prerequisite on the resolved branch before enabling either arming switch. Keep distinct native Windows cases for compared and resident mappings, since their protection and proof paths differ. #18293 builds on the same switches, so the exact dependency order belongs in the comparison. I have no native Windows result. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/16372:upstream-pr/16372'
git switch 'upstream-pr/16372'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/16372'
git switch 'upstream-pr/16372'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/16372'
git switch 'upstream-pr/16372'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/16372'
git switch 'main'
git merge --squash 'upstream-pr/16372'
git switch 'main'
git merge --ff-only 'upstream-pr/16372'
git switch 'main'
git merge 'upstream-pr/16372'
git push origin 'main'
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!16372
No description provided.