feat(libc): trap guest reads of write-watched pages on macOS #13442

Merged
Gonic0 merged 5 commits from upstream-pr/13442 into main 2026-11-01 09:13:35 +00:00
Gonic0 commented 2026-10-30 14:42:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gonic0 on 2026-10-30T14:42:32Z.


What

The macOS write watch gains read traps. GuestWriteWatchTrapReads(address, bytes) takes all access to the pages of a watched range (PROT_NONE) until the resolver set with GuestWriteWatchSetReadTrapResolver has run for it. GuestWriteWatchReleaseReads removes a trap without running the resolver. On Linux and Windows the three exports return false or do nothing.

Nothing sets read traps yet, as with GuestWriteWatchProtectionChanged. The intended user is a follow-up in libSceAgcDriver: StorageTexture::StoreReadbackBeforeLabel writes back every pending linear or small storage image at each label, whether or not the CPU reads it. With traps, the write-back can wait until a guest thread touches the memory. On PPSA21564 (ASTRO BOT 01.007.000), during the logos before the intro, skipping those write-backs (APS5_NO_LABEL_READBACK_STORE=1) gives 43.1 and 43.2 FPS against 37.6 and 37.5 with them, alternated, and the image stays intact. I then trapped those ranges with a temporary handler, without writing them back. In 75 s, every fault came from the emulator's own reads and writes, none from guest code. During the logos there were no faults. That is one title and one phase, so the follow-up keeps the write-back as the fallback. That follow-up is #13499. It traps those ranges at the label instead of writing them back, releases a trap when FlushPending stores its image, and resolves a trap through the host-read hook. In the same scene it gives 44.0 and 44.3 FPS against 38.1 and 37.1, alternated, with the same image.

How it works:

  • A page under a trap has a new state bit (0x80). The traps themselves are kept as exact ranges, so two images sharing a page each keep their own trap, and releasing one leaves the page trapped for the other.
  • A fault on a trapped page, read or write, is checked before record(). The faulting thread hands the page to a resolver thread and waits on a Mach semaphore. The resolver cannot run on the faulting thread: the handler is installed without SA_NODEFER, and a resolver storing into write-watched pages would raise the blocked signal again.
  • The resolver thread takes every trap on the page and every trap sharing a page with those, calls the resolver once per trap with its exact range, then gives the pages back the protection the watch records for them.
  • Faults on pages of a trap being resolved:
    • A fault from the resolving thread itself gets the page back at once, so the resolver can store into its range.
    • A fault from another thread waits until the resolution ends.
    • A fault that arrives after its page was given back is retried, at most 16 times per thread between two untraps, and at most 1,024 times in all between two untraps, so a fault that keeps coming back stays bounded when more threads fault than the 64-entry per-thread table holds. A fault the page's protection does not explain, such as a file mapping whose backing was truncated, is still passed on.
  • Collections keep trapped pages at PROT_NONE.
  • Trapping a range that is already trapped exactly does nothing and returns true, so a caller can trap the same range at every label without keeping its own record.
  • GuestWriteWatchBeginHostWrite, GuestWriteWatchProtect and GuestWriteWatchProtectionChanged run the resolver for the traps they cover first, on the calling thread, since a system call cannot write a PROT_NONE page and a new protection would erase the trap. A resolver may itself start a host write over its own range; the driver brackets GPU imports that way.
  • Registering or unregistering memory drops its traps without running the resolver, since the memory is being replaced.

#7192 adds GuestWriteWatchHandleFault, which calls record() directly; if both land, that entry point needs the same check for trapped pages first. The macOS write-watch entry in docs/dev/debt/libc.md gets the gaps:

  • a system call that reads a trapped page fails with EFAULT;
  • a resolver that stores with the CPU, or starts a host write, makes the pages accessible to other threads before it returns;
  • a thread that holds something the resolver needs must not touch a trapped range, or start a host write or protect over one being resolved: it would wait for the resolver forever;
  • a resolver that touches the pages of another trap takes them without resolving that trap.

Tested

Apple M5 Pro, macOS 27.2. Configuring core/libs/tests/write_watch on main fails on macOS because two test names are registered twice; #13220 fixes that. So I built write_watch_tests from this branch with clang++ -std=c++20 -O2 -Wall -Wextra, for x86-64 (run under Rosetta 2) and for arm64. I also built it with the project's CMake in a tree with the open macOS port PRs, where these files are identical. There, ctest -R '^write_watch_' passes 23 of 23. libc also builds there with the project's flags.

New test modes:

  • read-trap:
    • a read from another thread sees the resolver's bytes, and the resolver runs once with the exact range on the watch's thread;
    • the resolver's stores are collected as writes;
    • a released trap does not run the resolver, also after the same range was trapped twice;
    • a write to a trapped page lands after the resolver's bytes;
    • a collection keeps a trapped page at PROT_NONE;
    • two traps sharing a page;
    • a host write runs the resolver on the calling thread, after which read() from a pipe fills the page;
    • GuestWriteWatchProtect resolves the trap and applies its protection;
    • a resolver that brackets its stores as a host write;
    • unregistering restores the protection without running the resolver;
    • GuestWriteWatchTrapReads fails without a resolver, on unwatched memory and under a host write.
  • read-trap-threads: four threads read 32 trapped pages, two at a time on the same page. The resolver stores through a second mapping of the same file, as a GPU stores through a host import. Every read sees the resolver's bytes and each trap is resolved exactly once.
  • read-trap-fault: with traps set, a read of a watched file mapping whose backing was truncated still kills the process with SIGBUS instead of being retried forever.
  • read-trap-fault-bound: 66 threads read a watched file mapping whose backing was truncated, with a Mach exception port on each. The test passes their faults on as signals strictly in turn, so each thread's retry entry is evicted before its next fault, for 40 rounds. The process still dies with SIGBUS.
  • read-trap-late: 32 threads read a trapped page with a Mach exception port set on each, which holds their faults until all 32 have faulted. The page is then resolved, and only after that are the faults passed on as signals. All 32 handlers find the page given back and retry, and every read sees the resolver's bytes.

All write-watch modes pass on both architectures. read-trap, read-trap-threads, read-trap-fault, read-trap-late and read-trap-fault-bound passed 10 times each on both, and read-trap-threads 30 more times on each. The other tests in that tree that use the write watch, guest_arena_*, guest_memory_lifetime*, agc_driver_guest_memory_lifetime* and guest_memory_pool_stats, pass. guest_memory did not build there, for a sceKernelMunmap call unrelated to this change.

Negative controls, each of which fails a test:

  • record() before the trap check: "a write to a trapped page does not land after the resolver's bytes";
  • collections re-protecting trapped pages read-only: "a collection gives back the access to a trapped page";
  • no pass for the resolving thread's own faults: read-trap hangs until its alarm;
  • another thread's fault during a resolution taking the page: "a concurrent read of a trapped page does not see the resolver's bytes";
  • the resolver's nested host write waiting on its own resolution: read-trap hangs until its alarm;
  • no retry of a fault after its page was given back: read-trap-threads dies with SIGBUS;
  • unbounded retries: "a read fault on a mapping with missing backing is not delivered while read traps are set";
  • a second trap of the same range adding another: "a released read trap set twice does not give back the watch's protection";
  • no bound on all retries between two untraps: "66 threads that keep faulting on a truncated mapping in turn are retried without bound";
  • one retry budget per page for all threads, as before 73afc098f1: "32 read faults that arrive after their page was given back are not all retried";
  • host writes keeping the trap: "a host write over a trapped page does not resolve it first on its own thread";
  • releasing a trap ignoring the others on its page: "releasing one read trap gives back a page another one still traps".

Not tested on Linux or Windows, where the change adds three exports that return false or do nothing.

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 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: #13220 (the write-watch tests configure only with it)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5086154366 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13442). Originally posted by [Gonic0](https://github.com/Gonic0) on 2026-10-30T14:42:32Z. --- ### What The macOS write watch gains read traps. `GuestWriteWatchTrapReads(address, bytes)` takes all access to the pages of a watched range (`PROT_NONE`) until the resolver set with `GuestWriteWatchSetReadTrapResolver` has run for it. `GuestWriteWatchReleaseReads` removes a trap without running the resolver. On Linux and Windows the three exports return false or do nothing. Nothing sets read traps yet, as with `GuestWriteWatchProtectionChanged`. The intended user is a follow-up in libSceAgcDriver: `StorageTexture::StoreReadbackBeforeLabel` writes back every pending linear or small storage image at each label, whether or not the CPU reads it. With traps, the write-back can wait until a guest thread touches the memory. On PPSA21564 (ASTRO BOT 01.007.000), during the logos before the intro, skipping those write-backs (`APS5_NO_LABEL_READBACK_STORE=1`) gives 43.1 and 43.2 FPS against 37.6 and 37.5 with them, alternated, and the image stays intact. I then trapped those ranges with a temporary handler, without writing them back. In 75 s, every fault came from the emulator's own reads and writes, none from guest code. During the logos there were no faults. That is one title and one phase, so the follow-up keeps the write-back as the fallback. That follow-up is #13499. It traps those ranges at the label instead of writing them back, releases a trap when `FlushPending` stores its image, and resolves a trap through the host-read hook. In the same scene it gives 44.0 and 44.3 FPS against 38.1 and 37.1, alternated, with the same image. How it works: - A page under a trap has a new state bit (`0x80`). The traps themselves are kept as exact ranges, so two images sharing a page each keep their own trap, and releasing one leaves the page trapped for the other. - A fault on a trapped page, read or write, is checked before `record()`. The faulting thread hands the page to a resolver thread and waits on a Mach semaphore. The resolver cannot run on the faulting thread: the handler is installed without `SA_NODEFER`, and a resolver storing into write-watched pages would raise the blocked signal again. - The resolver thread takes every trap on the page and every trap sharing a page with those, calls the resolver once per trap with its exact range, then gives the pages back the protection the watch records for them. - Faults on pages of a trap being resolved: - A fault from the resolving thread itself gets the page back at once, so the resolver can store into its range. - A fault from another thread waits until the resolution ends. - A fault that arrives after its page was given back is retried, at most 16 times per thread between two untraps, and at most 1,024 times in all between two untraps, so a fault that keeps coming back stays bounded when more threads fault than the 64-entry per-thread table holds. A fault the page's protection does not explain, such as a file mapping whose backing was truncated, is still passed on. - Collections keep trapped pages at `PROT_NONE`. - Trapping a range that is already trapped exactly does nothing and returns true, so a caller can trap the same range at every label without keeping its own record. - `GuestWriteWatchBeginHostWrite`, `GuestWriteWatchProtect` and `GuestWriteWatchProtectionChanged` run the resolver for the traps they cover first, on the calling thread, since a system call cannot write a `PROT_NONE` page and a new protection would erase the trap. A resolver may itself start a host write over its own range; the driver brackets GPU imports that way. - Registering or unregistering memory drops its traps without running the resolver, since the memory is being replaced. #7192 adds `GuestWriteWatchHandleFault`, which calls `record()` directly; if both land, that entry point needs the same check for trapped pages first. The macOS write-watch entry in `docs/dev/debt/libc.md` gets the gaps: - a system call that reads a trapped page fails with `EFAULT`; - a resolver that stores with the CPU, or starts a host write, makes the pages accessible to other threads before it returns; - a thread that holds something the resolver needs must not touch a trapped range, or start a host write or protect over one being resolved: it would wait for the resolver forever; - a resolver that touches the pages of another trap takes them without resolving that trap. ### Tested Apple M5 Pro, macOS 27.2. Configuring `core/libs/tests/write_watch` on `main` fails on macOS because two test names are registered twice; #13220 fixes that. So I built `write_watch_tests` from this branch with `clang++ -std=c++20 -O2 -Wall -Wextra`, for x86-64 (run under Rosetta 2) and for arm64. I also built it with the project's CMake in a tree with the open macOS port PRs, where these files are identical. There, `ctest -R '^write_watch_'` passes 23 of 23. libc also builds there with the project's flags. New test modes: - `read-trap`: - a read from another thread sees the resolver's bytes, and the resolver runs once with the exact range on the watch's thread; - the resolver's stores are collected as writes; - a released trap does not run the resolver, also after the same range was trapped twice; - a write to a trapped page lands after the resolver's bytes; - a collection keeps a trapped page at `PROT_NONE`; - two traps sharing a page; - a host write runs the resolver on the calling thread, after which `read()` from a pipe fills the page; - `GuestWriteWatchProtect` resolves the trap and applies its protection; - a resolver that brackets its stores as a host write; - unregistering restores the protection without running the resolver; - `GuestWriteWatchTrapReads` fails without a resolver, on unwatched memory and under a host write. - `read-trap-threads`: four threads read 32 trapped pages, two at a time on the same page. The resolver stores through a second mapping of the same file, as a GPU stores through a host import. Every read sees the resolver's bytes and each trap is resolved exactly once. - `read-trap-fault`: with traps set, a read of a watched file mapping whose backing was truncated still kills the process with `SIGBUS` instead of being retried forever. - `read-trap-fault-bound`: 66 threads read a watched file mapping whose backing was truncated, with a Mach exception port on each. The test passes their faults on as signals strictly in turn, so each thread's retry entry is evicted before its next fault, for 40 rounds. The process still dies with `SIGBUS`. - `read-trap-late`: 32 threads read a trapped page with a Mach exception port set on each, which holds their faults until all 32 have faulted. The page is then resolved, and only after that are the faults passed on as signals. All 32 handlers find the page given back and retry, and every read sees the resolver's bytes. All write-watch modes pass on both architectures. `read-trap`, `read-trap-threads`, `read-trap-fault`, `read-trap-late` and `read-trap-fault-bound` passed 10 times each on both, and `read-trap-threads` 30 more times on each. The other tests in that tree that use the write watch, `guest_arena_*`, `guest_memory_lifetime*`, `agc_driver_guest_memory_lifetime*` and `guest_memory_pool_stats`, pass. `guest_memory` did not build there, for a `sceKernelMunmap` call unrelated to this change. Negative controls, each of which fails a test: - `record()` before the trap check: "a write to a trapped page does not land after the resolver's bytes"; - collections re-protecting trapped pages read-only: "a collection gives back the access to a trapped page"; - no pass for the resolving thread's own faults: `read-trap` hangs until its alarm; - another thread's fault during a resolution taking the page: "a concurrent read of a trapped page does not see the resolver's bytes"; - the resolver's nested host write waiting on its own resolution: `read-trap` hangs until its alarm; - no retry of a fault after its page was given back: `read-trap-threads` dies with `SIGBUS`; - unbounded retries: "a read fault on a mapping with missing backing is not delivered while read traps are set"; - a second trap of the same range adding another: "a released read trap set twice does not give back the watch's protection"; - no bound on all retries between two untraps: "66 threads that keep faulting on a truncated mapping in turn are retried without bound"; - one retry budget per page for all threads, as before `73afc098f1`: "32 read faults that arrive after their page was given back are not all retried"; - host writes keeping the trap: "a host write over a trapped page does not resolve it first on its own thread"; - releasing a trap ignoring the others on its page: "releasing one read trap gives back a page another one still traps". Not tested on Linux or Windows, where the change adds three exports that return false or do nothing. ### 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 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: #13220 (the write-watch tests configure only with it) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!13442
No description provided.