fix(libc): report writes consumed before an alias at both of its views #23976

Open
chitters2004 wants to merge 1 commit from upstream-pr/23976 into main
chitters2004 commented 2026-11-19 23:48:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by chitters2004 on 2026-11-19T23:48:37Z.


What

On Linux, DirectMemory.cpp WatchMapping registers a new mapping with GuestWriteWatchRegister and then aliases it to every mapping sharing its backing with separate GuestWriteWatchAlias calls. Watch::Alias in GuestWriteWatch.cpp only linked the two views. A collect that walked either view between those calls, from any thread (for example a tracked range of an earlier mapping at that address), consumed a write's dirty bit with a plan that had no link yet. The write was reported at that view only, and once the views were linked the other view never learned of it: a stale read through the new alias. The two views have separate userfaultfd write-protect bits, so a write through one never marks the other.

Alias now marks both linked ranges fresh, as Register already does for a new range. The next collect of either view reports the linked range whole and fans it out through the new link. Both sides are marked so the report survives either side being unregistered before it is collected. The cost is one whole report of the aliased overlap per Alias call. The window is narrow (a collect from another thread between those calls), and no title is known to show it. +4 lines in GuestWriteWatch.cpp, plus CheckAliasAfterConsumedWrite in libSceAgcDriver/tests/WriteTracking.cpp and the CTest agc_write_tracking_alias_after_consume (Linux only, skipped elsewhere): it maps one memfd at two views, registers and collects both, writes through one view, collects that view, aliases, and requires the other view to report the page and neither view to report it again afterwards, in both directions.

Tested

  • Fork CI on the fix (Linux gate and Windows, lavapipe), agc_write_tracking and its neighbours: Linux 4 passed (main: 3), no new VUIDs; Windows 11 passed, 1 skipped (the new test, Linux only), same results as main otherwise: https://github.com/chitters2004/AnyPS5/actions/runs/44637479711 (run on a commit with the same tree as this head; only the message differs)
  • Negative control (branch draft/ci-mf11-control: the new test on main c3a823624f without the Alias change): agc_write_tracking_alias_after_consume fails on Linux ("a write consumed at one view before an alias was never reported at the other view"), main's tests pass: https://github.com/chitters2004/AnyPS5/actions/runs/44637494214
  • Not run on a Linux machine with a physical GPU; the write watch needs userfaultfd write-protect async and PAGEMAP_SCAN (kernel 6.7+), as on the CI runners.

Checklist

  • Based on current main (c3a823624f); no other open PR implements the same functions (#20580, #20126, #19557, #19054, #12792 touch GuestWriteWatch.cpp and #23613, #23770, #20349 touch DirectMemory.cpp, none changes Alias or WatchMapping)
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: lavapipe (Mesa, fork CI Linux and Windows); no physical GPU run
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5404975254 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23976). Originally posted by [chitters2004](https://github.com/chitters2004) on 2026-11-19T23:48:37Z. --- ### What On Linux, `DirectMemory.cpp` `WatchMapping` registers a new mapping with `GuestWriteWatchRegister` and then aliases it to every mapping sharing its backing with separate `GuestWriteWatchAlias` calls. `Watch::Alias` in `GuestWriteWatch.cpp` only linked the two views. A collect that walked either view between those calls, from any thread (for example a tracked range of an earlier mapping at that address), consumed a write's dirty bit with a plan that had no link yet. The write was reported at that view only, and once the views were linked the other view never learned of it: a stale read through the new alias. The two views have separate userfaultfd write-protect bits, so a write through one never marks the other. `Alias` now marks both linked ranges fresh, as `Register` already does for a new range. The next collect of either view reports the linked range whole and fans it out through the new link. Both sides are marked so the report survives either side being unregistered before it is collected. The cost is one whole report of the aliased overlap per `Alias` call. The window is narrow (a collect from another thread between those calls), and no title is known to show it. +4 lines in `GuestWriteWatch.cpp`, plus `CheckAliasAfterConsumedWrite` in `libSceAgcDriver/tests/WriteTracking.cpp` and the CTest `agc_write_tracking_alias_after_consume` (Linux only, skipped elsewhere): it maps one memfd at two views, registers and collects both, writes through one view, collects that view, aliases, and requires the other view to report the page and neither view to report it again afterwards, in both directions. ### Tested - Fork CI on the fix (Linux gate and Windows, lavapipe), `agc_write_tracking` and its neighbours: Linux 4 passed (main: 3), no new VUIDs; Windows 11 passed, 1 skipped (the new test, Linux only), same results as main otherwise: https://github.com/chitters2004/AnyPS5/actions/runs/44637479711 (run on a commit with the same tree as this head; only the message differs) - Negative control (branch `draft/ci-mf11-control`: the new test on main c3a823624f without the `Alias` change): `agc_write_tracking_alias_after_consume` fails on Linux ("a write consumed at one view before an alias was never reported at the other view"), main's tests pass: https://github.com/chitters2004/AnyPS5/actions/runs/44637494214 - Not run on a Linux machine with a physical GPU; the write watch needs `userfaultfd` write-protect async and `PAGEMAP_SCAN` (kernel 6.7+), as on the CI runners. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main` (c3a823624f); no other open PR implements the same functions (#20580, #20126, #19557, #19054, #12792 touch `GuestWriteWatch.cpp` and #23613, #23770, #20349 touch `DirectMemory.cpp`, none changes `Alias` or `WatchMapping`) - [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) and the licence notice of code adapted from upstream (see AGENTS.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) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: lavapipe (Mesa, fork CI Linux and Windows); no physical GPU run - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
MarcZX10 commented 2026-11-20 11:57:55 +00:00 (Migrated from github.com)

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

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the points below are optional.

What I ran

Reviewed head bb328c92a21f5d33c0d93ce43e19242f959ca66a against main. I did not run commands or tests. The author reports Linux and Windows CI results and a failing negative control in the PR description; I did not independently verify those runs.

Results

Check Result Note
Diff and changed lines passed All three changed files read
Tests in this review not run CI claims not independently verified
Negative control not run Author reports it failed as expected

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run the test suite or verify the reported CI runs, physical GPU behavior, or console behavior. All files listed as reviewed in the manifest were read: core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp, core/libs/prx/libSceAgcDriver/tests/cmake/agc_write_tracking_tests.cmake, and core/libs/prx/libc/src/GuestWriteWatch.cpp. No files were excluded by the manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5992293165 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23976#pullrequestreview-5992293165). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T11:57:55Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the points below are optional. ## What I ran Reviewed head `bb328c92a21f5d33c0d93ce43e19242f959ca66a` against `main`. I did not run commands or tests. The author reports Linux and Windows CI results and a failing negative control in the PR description; I did not independently verify those runs. ## Results | Check | Result | Note | |---|---|---| | Diff and changed lines | passed | All three changed files read | | Tests in this review | not run | CI claims not independently verified | | Negative control | not run | Author reports it failed as expected | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run the test suite or verify the reported CI runs, physical GPU behavior, or console behavior. All files listed as reviewed in the manifest were read: `core/libs/prx/libSceAgcDriver/tests/WriteTracking.cpp`, `core/libs/prx/libSceAgcDriver/tests/cmake/agc_write_tracking_tests.cmake`, and `core/libs/prx/libc/src/GuestWriteWatch.cpp`. No files were excluded by the manifest. ## To get this to a good state Nothing is required.
Plyxen commented 2026-12-03 14:10:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Plyxen on 2026-12-03T14:10:34Z.

Review: APPROVED


I read the diff at bb328c92a2. Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).

  • agc_write_tracking_alias_after_consume and the other agc_write_tracking tests: pass.
  • Control (Linux, the same tree with only this PR's source change taken out): it fails with "a write consumed at one view before an alias was never reported at the other view". The new test skips itself on Windows.

Not checked: a title.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171416255 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23976#pullrequestreview-6171416255). Originally posted by [Plyxen](https://github.com/Plyxen) on 2026-12-03T14:10:34Z. **Review:** APPROVED --- I read the diff at `bb328c92a2`. Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into `main` at `6c6b29320fe` together with other open PRs (so not an isolated build). - `agc_write_tracking_alias_after_consume` and the other `agc_write_tracking` tests: pass. - Control (Linux, the same tree with only this PR's source change taken out): it fails with "a write consumed at one view before an alias was never reported at the other view". The new test skips itself on Windows. Not checked: a title. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 18:55:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T18:55:30Z.

Review: COMMENTED


On Linux, agc_write_tracking_alias_after_consume passed at bb328c92a2. It exercises a write consumed before the alias is registered in both view orders, then checks that the next collection after reporting is quiet. The generation-wrap, mark-order, and Vulkan scalar-buffer controls passed on the same baseline and change. Both scalar runs loaded the Khronos validation layer and had no missing-layer warnings or VUID messages.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6176383378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23976#pullrequestreview-6176383378). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T18:55:30Z. **Review:** COMMENTED --- On Linux, `agc_write_tracking_alias_after_consume` passed at `bb328c92a2`. It exercises a write consumed before the alias is registered in both view orders, then checks that the next collection after reporting is quiet. The generation-wrap, mark-order, and Vulkan scalar-buffer controls passed on the same baseline and change. Both scalar runs loaded the Khronos validation layer and had no missing-layer warnings or VUID messages. 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/23976:upstream-pr/23976'
git switch 'upstream-pr/23976'

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