fix(libc): forward writes to truncated macOS mappings #12435
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12435"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by plexerPK on 2026-10-28T13:31:24Z.
What
A CPU store into a watched MAP_SHARED mapping can loop in the macOS signal handler after ftruncate removes its backing. The handler keeps accepting the fault because the recorded page permissions include write access, even though retrying cannot succeed.
Check mprotect before marking a protected page writable. For a fault on an already-writable watched page, use mach_vm_read_overwrite to probe the fault address without recursively dereferencing unavailable backing in the handler. Failed probes forward the original fault. This preserves legitimate delayed faults from concurrent writers; the normal first-write path does not perform a Mach copy.
This follow-up includes the prerequisite changes from #12117 and removes its resolved truncation debt entry. No foreign PR implementation is included. Current-main baseline:
750cf6cf48.Ownership: inspected all 1,336 open PRs before implementation and refreshed the inventory to 1,343 before publication. #12117 is our prerequisite; #11801 changes forwarding of user-generated signals; #7192 exposes HandleFault. Neither implements backing validation or checks failed protection restoration. No competing truncation fix found.
Tested
macOS 27.0.1, Apple clang 21, native arm64 Release; tests compile the actual GuestWriteWatch.cpp:
Reproduce:
cmake -S core/libs/tests/write_watch -B build-write-watch -DCMAKE_BUILD_TYPE=Release,cmake --build build-write-watch,ctest --test-dir build-write-watch --output-on-failure.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T15:04:00Z.
Review: COMMENTED
Review of head
1f1da4b0ffon macOS 26, Apple clang, AI-assisted. I only had the host, no console. I ran the standalonecore/libs/tests/write_watchproject the way the body describes, plus the x86_64 run the body says it could not do.Results
-DCMAKE_OSX_ARCHITECTURES=x86_64). No build warnings.lateandthreadsalso passed 5 back-to-back runs on x86_64.ASAN_OPTIONS=handle_segv=0:handle_sigbus=0:allow_user_segv_handler=1. Without those options,backing_shared,backing_restore,execute-faultandread-faultfail. That's because ASan's own SIGBUS report makes the child exit instead of dying by signal, so it comes from the test harness, not the code.eb20686c42(the #12117 prerequisite; that file, WriteWatch.cpp and the write_watch CMakeLists are identical to #12117's) under the new tests. Onlywrite_watch_backing_sharedandwrite_watch_backing_restorefail, on both arm64 and x86_64. That matches the body.git merge-treeagainst main17f8dfad51is clean.Mutants (both arches give the same result)
::mprotect(...)backing_restorereturn truebacking_shared(hang until SIGALRM)return falsebacking_late(bus error: the delayed second writer gets forwarded)isWriteFaultdropped fromfault()execute-fault&& copied == sizeof(value)copiedis always the full size when the call returns KERN_SUCCESSstate = ...Writtenbefore the mprotect checkThe last survivor is minor. If restoring protection fails, the page has already been marked Written with Protected cleared, so the next Collect reports a page that was never written. The PR's ordering is correct; nothing tests it. Checking in
backing_restorethat the page is still unreported after a failed restore would cover it. Not blocking.Scope note (not blocking, not a regression)
The new probe checks that the page is readable, not that it is writable. I mapped and registered a page as RW, wrote to it, then called plain
::mprotect(page, PROT_READ)without going throughGuestWriteWatchProtect, then wrote again. The process hangs untilalarmfires (rc 142) on head and on theeb20686c42base. The recorded state still contains PROT_WRITE, andmach_vm_read_overwritesucceeds on a readable page. Guest mprotect goes through DirectMemory →GuestWriteWatchProtect, so only host-side code that changes protection directly can reach this, and that's outside this PR's truncation topic. If you want to close it later, askingmach_vm_regionfor the current protection, or doing a one-bytemach_vm_writeback of the probed byte, would test writability itself.So, COMMENT rather than APPROVE: the change is correct and both of its new checks are covered by tests. I'm not approving because it stacks on #12117, which has no reviews yet, and I didn't run the full libs lane.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T15:53:24Z.
Review: APPROVED
Reviewed exact head
1f1da4b0ff1592c9a545f39e9c112bf935630aaa. The PR is explicitly stacked on #12117; that prerequisite is now approved, and its implementation is unchanged from the dependency content in this branch. The new macOS backing probe only runs when a page is already recorded writable; whenmach_vm_read_overwritecannot read the address (the truncated backing case), the handler forwards the original fault, while normal protected-page writes still use the prior fast path. The failed-mprotectordering avoids marking a page written before protection restoration succeeds. Independent exact-head macOS testing reports 17/17 on arm64 and x86_64/Rosetta, 17/17 under ASan/UBSan with the signal options needed by this harness, repeated late-fault/thread tests, and red controls for both new behaviors. I independently confirmed the four-file patch applies cleanly to currentmainandgit diff --checkpasses. I could not run Mach VM tests on this Linux host; hosted macOS write-watch jobs remain queued, so those results are not claimed as completed CI. The reviewer’s non-blocking note about host code changing mapping protection outside GuestWriteWatch is outside the supported protection API and is not a regression from this patch. No blocking issue found.