fix(libc): track never-collected pages in the macOS write watch #3703

Merged
utkuhalis merged 1 commit from upstream-pr/3703 into main 2026-10-11 21:53:09 +00:00
utkuhalis commented 2026-10-11 15:39:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by utkuhalis on 2026-10-11T15:39:51Z.


What

On macOS, GuestWriteWatchCollectFresh reported no pages and still returned true, and GuestWriteWatchCollectArmed was a full Collect. The Linux watch treats them differently: CollectFresh reports and arms only the ranges no collection has taken since registration, and CollectArmed leaves those ranges alone. On macOS, MarkWritten (Execution/src/GuestMemory.cpp) therefore counted pages that were never armed as clean, and they stayed writable with no tracking. A later CollectArmed also took them, which the caller does not expect.

The macOS backend now gives each page a fresh bit, set at registration (including a second registration) and cleared when a collection takes the page:

  • Collect takes fresh and armed pages, the same as before.
  • CollectFresh reports and arms only fresh pages and, like the Linux version, does not require the whole range to be covered.
  • CollectArmed skips fresh pages.
  • Protection changes, host writes and write faults keep the bit.

Without APS5_NO_WRITE_WATCH nothing else changes. With the watch disabled, CollectFresh now returns false, the same as Linux; it no longer returns true without collecting anything.

New write_watch_fresh and write_watch_armed tests in the standalone macOS write-watch suite (core/libs/tests/WriteWatch.cpp) check both collections. They mirror CheckWriteWatchFreshCollect and CheckWriteWatchArmedCollect in core/libs/tests/GuestMemory.cpp, which run only on Linux.

Tested

  • macOS 26, Apple M3 Pro, AppleClang. Standalone suite (cmake -S core/libs/tests/write_watch), run natively on arm64 and as x86-64 under Rosetta 2 (CMAKE_OSX_ARCHITECTURES=x86_64). All 8 write_watch_* tests, including write_watch_mapped from #3478, pass with --repeat until-fail:3.
  • Before the fix, write_watch_fresh and write_watch_armed fail and the other six pass.
  • The same change in a macOS port of the full build (Rosetta 2, MoltenVK) also passes agc_driver_graphics. Upstream's full build does not compile on macOS, so that test cannot run on this branch as it is.
  • Not tested on Linux or Windows. The change is inside the __APPLE__ backend and the macOS-only test.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820229662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3703). Originally posted by [utkuhalis](https://github.com/utkuhalis) on 2026-10-11T15:39:51Z. --- ### What On macOS, `GuestWriteWatchCollectFresh` reported no pages and still returned `true`, and `GuestWriteWatchCollectArmed` was a full `Collect`. The Linux watch treats them differently: `CollectFresh` reports and arms only the ranges no collection has taken since registration, and `CollectArmed` leaves those ranges alone. On macOS, `MarkWritten` (`Execution/src/GuestMemory.cpp`) therefore counted pages that were never armed as clean, and they stayed writable with no tracking. A later `CollectArmed` also took them, which the caller does not expect. The macOS backend now gives each page a fresh bit, set at registration (including a second registration) and cleared when a collection takes the page: - `Collect` takes fresh and armed pages, the same as before. - `CollectFresh` reports and arms only fresh pages and, like the Linux version, does not require the whole range to be covered. - `CollectArmed` skips fresh pages. - Protection changes, host writes and write faults keep the bit. Without `APS5_NO_WRITE_WATCH` nothing else changes. With the watch disabled, `CollectFresh` now returns `false`, the same as Linux; it no longer returns `true` without collecting anything. New `write_watch_fresh` and `write_watch_armed` tests in the standalone macOS write-watch suite (`core/libs/tests/WriteWatch.cpp`) check both collections. They mirror `CheckWriteWatchFreshCollect` and `CheckWriteWatchArmedCollect` in `core/libs/tests/GuestMemory.cpp`, which run only on Linux. ### Tested - macOS 26, Apple M3 Pro, AppleClang. Standalone suite (`cmake -S core/libs/tests/write_watch`), run natively on arm64 and as x86-64 under Rosetta 2 (`CMAKE_OSX_ARCHITECTURES=x86_64`). All 8 `write_watch_*` tests, including `write_watch_mapped` from #3478, pass with `--repeat until-fail:3`. - Before the fix, `write_watch_fresh` and `write_watch_armed` fail and the other six pass. - The same change in a macOS port of the full build (Rosetta 2, MoltenVK) also passes `agc_driver_graphics`. Upstream's full build does not compile on macOS, so that test cannot run on this branch as it is. - Not tested on Linux or Windows. The change is inside the `__APPLE__` backend and the macOS-only test. ### 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
adriadam10 commented 2026-10-11 21:53:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T21:53:05Z.


Checked the diff: the fresh bit is set on every Register path (including re-registering already watched pages), cleared only when a collection takes the page, and kept by SetProtection, host writes and write faults. CollectFresh no longer requires full coverage and returns false with the watch disabled, matching the Linux watch; CollectArmed skips fresh pages. The new write_watch_fresh/write_watch_armed tests are registered in ctest and mirror the Linux ones; the write-watch job passes on macos-15. Merging once the remaining macOS job is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6114210430 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3703#issuecomment-6114210430). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T21:53:05Z. --- Checked the diff: the fresh bit is set on every `Register` path (including re-registering already watched pages), cleared only when a collection takes the page, and kept by `SetProtection`, host writes and write faults. `CollectFresh` no longer requires full coverage and returns `false` with the watch disabled, matching the Linux watch; `CollectArmed` skips fresh pages. The new `write_watch_fresh`/`write_watch_armed` tests are registered in ctest and mirror the Linux ones; the `write-watch` job passes on macos-15. Merging once the remaining macOS job is green.
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!3703
No description provided.