Skip to content

fix(libc): compare, not mark, writable pages in a failing macOS host write - #216

Merged
utkuhalis merged 2 commits into
AnyTogether:mainfrom
hedibravofox:fix/macos-write-watch-partial-host-write
Dec 3, 2026
Merged

utkuhalis merged 2 commits into
AnyTogether:mainfrom
hedibravofox:fix/macos-write-watch-partial-host-write

Conversation

@hedibravofox

@hedibravofox hedibravofox commented Dec 1, 2026 •

Copy link
Copy Markdown

What

A macOS host write session (GuestArena::HostWrite) marks every writable watched page in its range written when it opens, because once a page is opened the watch no longer sees stores to it. If the range also covers a page the guest cannot write, the session's write fails partway, and the pages before it can be marked written though nothing stored to them. videodec2's ClearOutput (upstream code, shared with Linux and Windows) opens a session only to test whether an output is writable. For an output that starts on a writable watched page and runs into an unwritable one, guest_videodec2_hevc_aliases then fails straddling unwritable output dirtied watched prefix page. That is the check left after #213. Linux and Windows pass it.

When a session's range covers a watched page without guest write access, its writable, still-protected pages are now opened as hot pages: snapshot first, then unprotect. The next collection reports them written only if their bytes changed, so a partial copy that did store to the prefix is still seen. Sessions over fully writable ranges, which are the normal case, keep the current path, so they pay for no snapshot. With hot pages turned off (APS5_WRITE_WATCH_NO_HOT), pages fall back to being marked written as before.

Pages opened this way stay hot until the session ends. HostWrites and Collect's re-arm after 256 unchanged collections skip cool() for a page inside an open host write. Otherwise they could re-protect it mid-session, and the session's next host store would fault, which under Rosetta can fail to reach the watch. Once the session ends, both work on the page again. openCompared also checks its mprotect and falls back to the marking path if it fails.

A known cost: a large session with one unwritable watched page snapshots every clean, writable, protected page in its range under the watch lock. Those pages are then compared at each collection until they re-arm.

guest_videodec2_hevc_aliases passes and its ctest-known-failures.txt entry is removed.

Tested

macOS 27.2.1, Apple silicon, x86-64 build under Rosetta 2 (AppleClang 21.0.0, Release), rebased on current main (#213 merged):

  • New write_watch_host-partial-hot. It opens a host write over two writable pages and one read-only page, then checks four things:
    • the kernel keeps the first page writable through HostWrites and through 300 collections during the session;
    • a store in the session is reported;
    • the page is re-armed, no longer writable, after the session ends and enough collections pass;
    • a later guest store is reported.
  • Negative controls, each fails the new test on its own:
    • without the HostWrites skip: "HostWrites protected a page inside an open host write";
    • without the Collect skip: "a collection re-armed a page inside an open host write".
  • All 30 write_watch_* tests and guest_videodec2_hevc_aliases: pass 3 runs out of 3.
  • ctest -R 'write_watch|read_trap|ReadTrap|label_trap|guest_memory|import|agc_write|guest_arena|write_tracking|label|videodec2|host_write': 183 tests. All pass except guest_memory, which is listed and fails the same way on main (b23fbf4): "fixed guest mapping 0x1000000000+0x4000 overlaps a host region in the guest arena".
  • guest_videodec2_hevc_aliases: passes 3 runs out of 3. With APS5_WRITE_WATCH_NO_HOT=1, which skips the new path, it fails at the straddling check as before.
  • Before the review change, full ctest -j8 (7,107 tests): every failure is already in ctest-known-failures.txt (all or macos); no new failures.
  • python3 tools/check_conventions.py --base origin/main: all checks passed.

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 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: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

@utkuhalis

Copy link
Copy Markdown
Collaborator

Thanks, this is careful work. We traced it and found no path where a page that was really written stops being reported:

  • openCompared snapshots under the watch lock while the page is still write-protected, and only then unprotects it.
  • A store that was waiting in record() retries after the snapshot.
  • Collect's memcmp reports any change.
  • Pages that are already Written or Hot take the old marking path.

We also saw that the branch already contains #213, so merging this one brings both.

One thing we'd like changed before merging: pages opened this way are Hot while their host write session is still open, and nothing stops cool() from re-protecting them during that session. It can be reached two ways: through HostWrites() (line 1345, called from the driver's markWritten), and through Collect's re-arm after 256 unchanged collections (line 1304). Neither checks _hostWrites. Before this PR, pages in a session were never Hot, so that couldn't happen. If one is re-protected, the session's next host store faults. Usually record() handles that, but the comment on copyOwnBytes in GuestMemory.cpp says that under Rosetta such a fault can fail to reach the watch (Stray's queue stalled on a label for good), and avoiding that is why sessions unprotect pages at all. Skipping cool() for pages inside a range in _hostWrites closes it. A long partial session, such as a fiber's PinStack over a context with an unwritable page, is where it could bite.

Two smaller notes, not blockers:

  • A large HostWrite with a single unwritable watched page now snapshots every clean writable page in the range under the watch lock. Those pages then stay hot, compared on each collection, until they re-arm.
  • openCompared ignores mprotect's result (around line 1977). The old path does the same.

CI is approved. The macOS job has to show guest_videodec2_hevc_aliases passing on the runner, since its known-failures line is removed.

Hédi Ben Farhat and others added 2 commits December 3, 2026 11:12
…write

A macOS host write session marked every writable watched page in its range
written. When the range also covers a page the guest cannot write, the write
fails partway, and a session opened only to test writability (videodec2's
ClearOutput) dirtied the writable prefix without storing to it. Those pages
are now opened as hot pages with a snapshot, so the next collection reports
them only if their bytes changed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A partial host write opens its writable pages as hot pages. HostWrites
and Collect's re-arm after 256 unchanged collections could re-protect
them while the session was still open, so the session's next host store
would fault, which under Rosetta can fail to reach the watch. Both now
skip a page inside an open host write. openCompared also falls back to
the marking path when its mprotect fails.

write_watch_host-partial-hot checks that the page stays writable through
HostWrites and 300 collections during the session, and is re-armed after
it ends.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hedibravofox
hedibravofox force-pushed the fix/macos-write-watch-partial-host-write branch from 9dd7abe to 6572bf1 Compare December 3, 2026 10:25
@hedibravofox

Copy link
Copy Markdown
Author

Thanks for the review. I added 6572bf1f3b, which skips cool() for a page inside an open host write in both HostWrites() and Collect's re-arm, so a session's opened pages stay writable until it ends. New test: write_watch_host-partial-hot. Without either skip, it fails on that path's check. I also took the mprotect note: openCompared now falls back to the marking path if it fails. The snapshot cost is described in the body. The branch is rebased on main, now that #213 is merged.

@utkuhalis
utkuhalis merged commit 4b5633f into AnyTogether:main Dec 3, 2026
139 of 190 checks passed
@utkuhalis

Copy link
Copy Markdown
Collaborator

Merged, thanks! The cool() skip and write_watch_host-partial-hot cover what we were after. #226 now needs its one-line rebase in the write_watch test list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants