fix(libc): refuse a macOS host write over a read-only watched page, as Windows does #28075

Open
magnub wants to merge 1 commit from upstream-pr/28075 into main
magnub commented 2026-11-28 11:38:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by magnub on 2026-11-28T11:38:42Z.


What

On macOS the write watch cannot see host writes, so GuestWriteWatchBeginHostWrite_nid_postfix marks every page of the range written as soon as a host write opens. Windows refuses the open instead when any page of the range is not writable (WindowsMappings::BeginHostWrite), and callers already treat a refused open as an unwritable destination.

GuestArenaBeginHostWrite_nid_postfix now does the same on macOS: when the write watch records a page of the range without PROT_WRITE, it returns false before marking anything. A host write could not land on such a page anyway, because opening it restores the recorded protection, which has no write bit.

This is what broke guest_videodec2_hevc_aliases on macOS. The upfront frame and output check from #21081 opens a host write over the read-only frame, and over an output that crosses into the read-only page, so pages were reported dirty although nothing was written. The code change is under __APPLE__; Linux and Windows are unchanged.

Tested

Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, on main 29b14e4c5e. #17425, #11049, #26739 and #25331 were merged locally only so that main builds on macOS.

  • guest_videodec2_hevc_aliases: fails 2/2 on main ("read-only frame dirtied watched output page"), passes 3/3 with this change.
  • The 116 tests that open guest memory for writing, including write_watch_* and guest_arena_*, before and after: the only difference is that guest_videodec2_hevc_aliases now passes. guest_kernel_sys fails both before and after.

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

https://claude.ai/code/session_012LL9hfx4BY6MbUCYMduDAQ

Summary by CodeRabbit

  • Bug Fixes
    • On Apple, host writes to memory regions reported as non-writable are now rejected. Other platforms are unchanged.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5539728636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28075). Originally posted by [magnub](https://github.com/magnub) on 2026-11-28T11:38:42Z. --- ### What On macOS the write watch cannot see host writes, so `GuestWriteWatchBeginHostWrite_nid_postfix` marks every page of the range written as soon as a host write opens. Windows refuses the open instead when any page of the range is not writable (`WindowsMappings::BeginHostWrite`), and callers already treat a refused open as an unwritable destination. `GuestArenaBeginHostWrite_nid_postfix` now does the same on macOS: when the write watch records a page of the range without `PROT_WRITE`, it returns false before marking anything. A host write could not land on such a page anyway, because opening it restores the recorded protection, which has no write bit. This is what broke `guest_videodec2_hevc_aliases` on macOS. The upfront `frame` and `output` check from #21081 opens a host write over the read-only frame, and over an output that crosses into the read-only page, so pages were reported dirty although nothing was written. The code change is under `__APPLE__`; Linux and Windows are unchanged. ### Tested Apple M1 Pro, macOS 27.0.1, x86_64 build under Rosetta 2, on main 29b14e4c5e. #17425, #11049, #26739 and #25331 were merged locally only so that main builds on macOS. - `guest_videodec2_hevc_aliases`: fails 2/2 on main ("read-only frame dirtied watched output page"), passes 3/3 with this change. - The 116 tests that open guest memory for writing, including `write_watch_*` and `guest_arena_*`, before and after: the only difference is that `guest_videodec2_hevc_aliases` now passes. `guest_kernel_sys` fails both before and after. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) 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: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_012LL9hfx4BY6MbUCYMduDAQ <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * On Apple, host writes to memory regions reported as non-writable are now rejected. Other platforms are unchanged. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
MarcZX10 commented 2026-11-28 11:47:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-28T11:47:41Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the read-only protection check runs before the write watch marks any pages dirty.

What I ran

Reviewed head 54e22cc7d934e5bbc74ffee7df92ad8183ec081f from the prepared diff and reader-pinned source. No commands or tests were run.

Results

Check Result Note
Diff and source review passed One changed source file read
Tests not run No tests run in this review

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported macOS test results were not independently verified. No test files were changed. The review bundle’s files.json was not available, so I could not compare the read file count against its reviewed list. GuestArena.cpp was the only changed file listed in the manifest and was read; the remaining repository files were not read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6097609425 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28075#pullrequestreview-6097609425). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-28T11:47:41Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the read-only protection check runs before the write watch marks any pages dirty. ## What I ran Reviewed head `54e22cc7d934e5bbc74ffee7df92ad8183ec081f` from the prepared diff and reader-pinned source. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Diff and source review | passed | One changed source file read | | Tests | not run | No tests run in this review | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported macOS test results were not independently verified. No test files were changed. The review bundle’s `files.json` was not available, so I could not compare the read file count against its `reviewed` list. `GuestArena.cpp` was the only changed file listed in the manifest and was read; the remaining repository files were not read. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-29 00:23:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T00:23:28Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code supports rejecting macOS host writes that span a watched page without write permission.

What I ran

Reviewed head 93edeb564a305a6effd4244931d7f59b3a4a35c using the prepared diff and pinned reader repo_read.py. No tests or builds were run.

Results

Check Result Note
Diff and description passed One source file changed
Targeted source review passed Protection lookup and write-watch path inspected
Tests and build not run No local execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

Tests reported in the PR description were not run independently. Only the changed GuestArena.cpp and targeted portions of GuestWriteWatch.cpp and GuestWriteWatch.hpp were read. No other changed files were listed in the manifest; files.json was not available from the reader.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6100703064 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28075#pullrequestreview-6100703064). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-29T00:23:28Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code supports rejecting macOS host writes that span a watched page without write permission. ## What I ran Reviewed head `93edeb564a305a6effd4244931d7f59b3a4a35c` using the prepared diff and pinned reader `repo_read.py`. No tests or builds were run. ## Results | Check | Result | Note | |---|---|---| | Diff and description | passed | One source file changed | | Targeted source review | passed | Protection lookup and write-watch path inspected | | Tests and build | not run | No local execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Tests reported in the PR description were not run independently. Only the changed `GuestArena.cpp` and targeted portions of `GuestWriteWatch.cpp` and `GuestWriteWatch.hpp` were read. No other changed files were listed in the manifest; `files.json` was not available from the reader. ## To get this to a good state Nothing is required.
NobleBrains commented 2026-11-30 23:53:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NobleBrains on 2026-11-30T23:53:38Z.

Review: APPROVED


Confirmed on macOS 15.

Setup: Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, BUILD_TESTING=ON. These are the OS and compiler generation of the macos-15 runner; the description tested macOS 27.
Tree: main 93d64f2d1e + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (main does not build on macOS without them, see #28168) + #22091 + #28369, then #28075, #28076 and #28284 merged one after another. Each merged cleanly.

guest_videodec2_hevc_aliases failures
before this PR (same tree) 3/3
with this PR at 93edeb564a 0/5

Full ctest -j 6 with all of them merged: 7038/7066 pass. Compared with the same tree before these PRs, no test fails that passed before. jsc_wtf_cpu_time aborted once under the parallel load, then passed 5/5 on its own; it is unrelated JavaScriptCore CPU-time code.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6122643054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28075#pullrequestreview-6122643054). Originally posted by [NobleBrains](https://github.com/NobleBrains) on 2026-11-30T23:53:38Z. **Review:** APPROVED --- Confirmed on macOS 15. **Setup:** Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, `BUILD_TESTING=ON`. These are the OS and compiler generation of the `macos-15` runner; the description tested macOS 27. **Tree:** `main` `93d64f2d1e` + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (`main` does not build on macOS without them, see #28168) + #22091 + #28369, then #28075, #28076 and #28284 merged one after another. Each merged cleanly. | `guest_videodec2_hevc_aliases` | failures | |---|---| | before this PR (same tree) | 3/3 | | with this PR at `93edeb564a` | 0/5 | **Full `ctest -j 6`** with all of them merged: 7038/7066 pass. Compared with the same tree before these PRs, no test fails that passed before. `jsc_wtf_cpu_time` aborted once under the parallel load, then passed 5/5 on its own; it is unrelated JavaScriptCore CPU-time code. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 14:02:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T14:02:43Z.

Review: COMMENTED


I followed the Darwin range walk into GuestWriteWatchProtection_nid_postfix. It advances through watched runs and unwatched gaps up to the requested end, so a range that starts writable and crosses into a watched read-only page is rejected before the write watch is opened. The Apple-specific implementation was reviewed from source because this checkout runs on Linux.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6171285519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28075#pullrequestreview-6171285519). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T14:02:43Z. **Review:** COMMENTED --- I followed the Darwin range walk into `GuestWriteWatchProtection_nid_postfix`. It advances through watched runs and unwatched gaps up to the requested end, so a range that starts writable and crosses into a watched read-only page is rejected before the write watch is opened. The Apple-specific implementation was reviewed from source because this checkout runs on Linux. 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/28075:upstream-pr/28075'
git switch 'upstream-pr/28075'

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