fix(libc): watch direct memory mapped over its reservation on macOS #3478

Merged
fable-void merged 1 commit from upstream-pr/3478 into main 2026-10-11 11:52:59 +00:00
fable-void commented 2026-10-11 11:14:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by fable-void on 2026-10-11T11:14:37Z.


What

On macOS, the write watch stops reporting writes to direct memory that is mapped over an address range reserved and registered before. On current main (0f863c10), the new test stops at:

$ write_watch_tests mapped
a range mapped over its reservation keeps the reservation's protection

Register copied the protection it had recorded for pages it already watched over the protection the kernel had just reported. A reservation is registered as PROT_NONE; when direct memory is mapped over it read-write and registered again, the stale PROT_NONE stayed, and Collect only write-protects pages with PROT_WRITE, so the memory reported one write and then none. main does not build the libraries on macOS yet; in a macOS build of the libraries with this write watch, Hollow Knight: Silksong (PPSA12544) blinks its title menu text and loses glyphs.

Register now keeps a recorded protection only where its own write protection explains what the kernel reports, and takes the kernel's protection otherwise. A protected page that was mapped again also loses its protected mark, so erase no longer restores the old protection over the new mapping. The kernel is read under the lock, so a concurrent Collect cannot protect a page between the read and the merge.

mugurc's macos-port branch keeps the recorded protection only for protected pages, which covers the reservation case; with that version the second half of the test stops at a protected page reserved again reports its old protection.

Tested

macOS on Apple silicon, core/libs/tests/write_watch configured and built as in the write-watch job, natively (arm64) and as x86-64 (under Rosetta 2), ctest --repeat until-fail:3: 6 of 6 pass on both. write_watch_mapped fails on main with the line above on both. The same Register change in a macOS build of the libraries stopped the blinking menu text in PPSA12544.

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:4818643612 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3478). Originally posted by [fable-void](https://github.com/fable-void) on 2026-10-11T11:14:37Z. --- ### What On macOS, the write watch stops reporting writes to direct memory that is mapped over an address range reserved and registered before. On current `main` (`0f863c10`), the new test stops at: ``` $ write_watch_tests mapped a range mapped over its reservation keeps the reservation's protection ``` `Register` copied the protection it had recorded for pages it already watched over the protection the kernel had just reported. A reservation is registered as `PROT_NONE`; when direct memory is mapped over it read-write and registered again, the stale `PROT_NONE` stayed, and `Collect` only write-protects pages with `PROT_WRITE`, so the memory reported one write and then none. `main` does not build the libraries on macOS yet; in a macOS build of the libraries with this write watch, Hollow Knight: Silksong (PPSA12544) blinks its title menu text and loses glyphs. `Register` now keeps a recorded protection only where its own write protection explains what the kernel reports, and takes the kernel's protection otherwise. A protected page that was mapped again also loses its protected mark, so `erase` no longer restores the old protection over the new mapping. The kernel is read under the lock, so a concurrent `Collect` cannot protect a page between the read and the merge. mugurc's `macos-port` branch keeps the recorded protection only for protected pages, which covers the reservation case; with that version the second half of the test stops at `a protected page reserved again reports its old protection`. ### Tested macOS on Apple silicon, `core/libs/tests/write_watch` configured and built as in the `write-watch` job, natively (arm64) and as x86-64 (under Rosetta 2), `ctest --repeat until-fail:3`: 6 of 6 pass on both. `write_watch_mapped` fails on `main` with the line above on both. The same `Register` change in a macOS build of the libraries stopped the blinking menu text in PPSA12544. ### 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 11:52:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T11:52:55Z.


Checked the diff and the CI: the write-watch job passes on macos-15 and macos-15-intel with the new write_watch_mapped test registered in ctest. Read Register through both cases in the test (a reservation mapped read-write keeps the kernel protection; a protected page reserved again drops Protected, so erase no longer restores the old protection), and the lock now covers the kernel read. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108735038 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3478#issuecomment-6108735038). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T11:52:55Z. --- Checked the diff and the CI: the `write-watch` job passes on macos-15 and macos-15-intel with the new `write_watch_mapped` test registered in ctest. Read `Register` through both cases in the test (a reservation mapped read-write keeps the kernel protection; a protected page reserved again drops `Protected`, so `erase` no longer restores the old protection), and the lock now covers the kernel read. Merging.
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!3478
No description provided.