Skip to content

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

Merged
adriadam10 merged 1 commit into
boykopovar:mainfrom
AnyTogether:fix/macos-write-watch-fresh
Oct 11, 2026
Merged

adriadam10 merged 1 commit into
boykopovar:mainfrom
AnyTogether:fix/macos-write-watch-fresh

Conversation

@utkuhalis

Copy link
Copy Markdown
Contributor

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 fix(libc): watch direct memory mapped over its reservation on macOS #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

On macOS, GuestWriteWatchCollectFresh reported nothing and returned true, so
MarkWritten took pages no collection had armed yet for clean, and they stayed
writable without being tracked. GuestWriteWatchCollectArmed was a full collect,
which also took those pages.

Pages now carry a fresh bit from registration until a collection takes them, as
the Linux watch keeps its fresh ranges: CollectFresh reports and arms only fresh
pages, CollectArmed skips them, and Collect takes both. Protection changes, host
writes and faults keep the bit.

The write_watch fresh and armed tests check both collections.
yabanana added a commit to yabanana/AnyPS5 that referenced this pull request Oct 11, 2026
Reuse the regression cases from upstream PR boykopovar#3703 at 99ddfa7. The macOS foundation already supplies the fresh-page behavior; run its regression cases against the native graphics closure as well.
@adriadam10

Copy link
Copy Markdown
Collaborator

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.

@adriadam10
adriadam10 merged commit c8536ab into boykopovar:main Oct 11, 2026
15 checks passed
@mrzacarias mrzacarias mentioned this pull request Oct 11, 2026
mugurc added a commit to mugurc/AnyPS5 that referenced this pull request Oct 12, 2026
Conflicts: the write watch takes main's fresh-page tracking (boykopovar#3703) in place of
the port's own; main's comparison fix replaces the port's ordered not-equal;
the kqueue, getc_unlocked and socketpair notes in TechnicalDebt.md and the
Rosetta sentence in BUILD.md are kept next to main's new entries; the Apple
branches of ModuleInfo.cpp, Stdio.cpp and SharedMemory.cpp stay beside main's
additions. The macOS build of what main added follows in the next commit.
@utkuhalis
utkuhalis deleted the fix/macos-write-watch-fresh branch October 12, 2026 05:30
ycarogabriel-lgtm pushed a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
Conflicts: the write watch takes main's fresh-page tracking (boykopovar#3703) in place of
the port's own; main's comparison fix replaces the port's ordered not-equal;
the kqueue, getc_unlocked and socketpair notes in TechnicalDebt.md and the
Rosetta sentence in BUILD.md are kept next to main's new entries; the Apple
branches of ModuleInfo.cpp, Stdio.cpp and SharedMemory.cpp stay beside main's
additions. The macOS build of what main added follows in the next commit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants