fix(libc): copy checked guest bytes in process on macOS #27164
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/27164"
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 Quimera996 on 2026-11-25T09:15:05Z.
What
On macOS,
GuestCopyIn_nid_no_patch,GuestCopyInUnsynced_nid_no_patchandGuestCopyOut_nid_no_patchnow copy in process, with a copy whose faults a SIGSEGV/SIGBUS handler turns into afalsereturn, instead of onemach_vm_read_overwriteormach_vm_writecall per copy. This is #21156's scheme for Linux brought to macOS. #26519 measured the Mach calls as the reasonguest_icu_comparisonstimes out underctest -jon an M1.core/libs/prx/libc/src/specifics/macos/GuestFaultCopy.cpp(new) holds the copy and the handler. The install, the chain to the earlier disposition and the reinstall after a guest SIGSEGV registration are the Linux file's. What differs:rep movsb. Rosetta 2 runs that at about 0.5 ns per byte, which made a 64 KiB copy several times slower than the Mach call. It copies 64-byte and 16-byte SSE blocks and a last overlapping 16-byte block, one alignedmovlormovqfor 4 and 8 bytes, and single bytes otherwise.raise(SIGSEGV)after a fault arrives with that fault'ssi_codeandsi_addr. So the handler looks only at the instruction address, and the chain does what macOS does with each earlier disposition without asking which kind of signal it is:SIG_IGNreturns (macOS itself runs an ignored faulting instruction again forever, checked natively and under Rosetta 2), andSIG_DFLis restored and the signal raised.Handler order: the write watch installs its SIGBUS/SIGSEGV handler while libc loads (both are set before
main, and are not withAPS5_NO_WRITE_WATCH=1), so the copy handler, installed at the first copy, runs first. A copy that faults on a read-trapped page therefore fails without running the resolver, as the Mach call fails today;guest_fault_copynow checks this. Writes are unchanged:GuestArena::HostWriteopens watched pages before the store.APS5_GUEST_COPY_SYSCALL=1keeps the Mach calls, as it keeps the system calls on Linux.Tests:
guest_fault_copyis registered on macOS. Its_syscalltwin stays Linux-only, because a failedmach_vm_read_overwritecopies no prefix and the test's prefix checks expect one.vm_kernel_page_sizeon macOS: Rosetta 2 reports 4 KiB pages, but files are backed by 16 KiB pages, so the old offset was still readable.guest_http2_cookiescomparedru_maxrssgrowth with64 * 1024, which is 64 MiB on Linux (kilobytes) and 64 KiB on macOS (bytes). The Mach path grew 61,440 bytes there and passed; a failing copy on the new path takes 8 KiB more (the signal frame), 69,632 bytes, and failed. The limit is now 64 MiB on macOS too.GuestHarfBuzzShaping.cppandGuestAudioPropagation.cpphave the same line; I left them, because this change does not reach them.guest_audio_propagationfails on that line intermittently without this change too (35 of 100 runs onmainhere).SA_RESETHANDcase (RequireResetHandChainsToDefault) runs on macOS only wheresigactionreports the flag. macOS keeps the flag but does not report it: natively on macOS 27.2.1 on Apple silicon0x4reads back as0x0, and LODIBIDO measured the same on macOS 15.7.8 on Intel, where the case failed. Rosetta 2 reports it, which is why the case passed here. The copy handler learns the flag only by reading it back, so on such a host an earlier one-shot handler is called more than once.Known gaps are in the debt fragment: I have not run an Intel Mac (LODIBIDO's comment has a run of
9af021255aon one), a failed copy costs one or two signal deliveries (about 19 µs each under Rosetta 2, against 0.5 µs for a failed Mach call), a SIGSEGV or SIGBUS sent from another thread to a thread inside the byte loop fails that copy, and a one-shot (SA_RESETHAND) handler registered by host code before the first copy is not always called exactly once. Guest registrations are not affected by the last one: libkernel resets those itself.Unchanged: Linux and Windows,
GuestWritable_nid_no_patch, and the standalone write-watch and guest-arena builds.Tested
macOS 27.2.1, Apple M5, Xcode 27.2 (Apple clang 21.0.0), x86-64 Release build under Rosetta 2,
mainf7948d080aplus #26739 (without it libc does not compile on macOS). No title was run: I have none, so the timings below are the test's own and a copy benchmark, not trace summaries.guest_icu_comparisons(#26519): 9.00, 6.49 and 7.02 s alone onmain, 6.40 s under ctest. With this change 0.19 to 0.22 s, after a first run of 1.09 s. WithAPS5_GUEST_COPY_SYSCALL=1on the changed build, 6.40 to 6.48 s.guest_fault_copy: passes, three runs (2.3 to 3.9 s). It covers copies of 1 to 65536 bytes at eight alignments both ways,falsewitherrnokept for0x10, a non-canonical address, aPROT_NONEpage, a span running into one and a write to a read-only page, exact prefixes for 1 to 33 and 48 to 257 bytes, 4 threads of 20,000 good and faulting copies, and the handler-chain cases of #21156 (an earlier handler, a later one,SIG_IGN,SA_RESETHAND, guest registrations throughsigaction_nid_postfix, a libkernel exception handler with a raised SIGBUS).SA_RESETHANDcase, second commit:guest_fault_copypasses 5 of 5 runs under Rosetta 2, where the case runs. I cannot run an Intel Mac, so I put aDYLD_INSERT_LIBRARIESinterposer in front ofsigactionthat clearsSA_RESETHANDin the returned action, as macOS does natively. With it the test at9af021255afails 5 of 5 withcheck failed at line 134, the failure LODIBIDO reported, and the changed test passes 5 of 5.sigactionstill reports a one-shot handler after it has run. One that ran before the first copy is then called once more (3 of 3 runs), where withAPS5_GUEST_COPY_SYSCALL=1the process ends on SIGSEGV. This is in the same debt entry.GuestCopyIn_nid_no_patch/GuestCopyOut_nid_no_patch, best of 7 rounds, two runs of each path on the same build:APS5_GUEST_COPY_SYSCALL=1)A failing 64 B copy, with the routine on its own: 38.4 µs against 0.47 µs.
ctest --parallel 8 --timeout 120withSDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy, 6600 tests registered: 6547 passed, 36 not run, 17 failed.libcurl.prx, fixed by #17425;GuestLibcxxRegexError.cpp;PipelinedFlushReadOrder.cpp).APS5_GUEST_COPY_SYSCALL=1on the same build:agc_unit_flags_allocation_failure_populated,agc_write_tracking,file_streams,guest_kernel_sys,guest_kernel_sys_execve,guest_libc_internal_exception_virtuals,guest_libcxx_exception_pointer,guest_math,guest_math_exact,guest_poll,guest_sce_net,guest_socket_options,guest_socket_port_zero,guest_throw_bad_array_new_length,guest_udp,run_report.agc_graphics_profile_draw, is intermittent on both paths:agc_driver_graphics_testsfailed 2 runs of 10 on each, withtwo ranges under a budget for two were not imported.guest_refused_connectfailed in an earlier run and in the Mach-path rerun and passed in this one.tools/check_conventions.pypasses.tools/pr_overlap.py --branchreports no overlap with the 150 pull requests it could compare; it could not compare 87 that conflict withmainindocs/dev/ENVIRONMENT.md,core/libs/prx/libc/CMakeLists.txtorGeneral.cpp. I read the three that touchGeneral.cpp(#9371, #18190, #20315): none touches the copy functions.9af021255aon one; the second commit has not run there), Linux and Windows builds (the Linux test file gains one case), a title.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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-25T09:21:54Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the review covers all changed files included in the manifest, and the author-reported macOS test results are not independently verified.
What I ran
Reviewed head
9af021255a7e231388d4454d80c59db7f24eda6cusing the pinnedrepo_read.pyreader. No tests or builds were run. I readdescription.md,commits.md,manifest.json,diff.patch, and the full text of all nine changed files listed in the manifest.files.jsonwas not available as a review document; the changed-file listing showed all nine changed files included.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
The reported Apple M5/Rosetta 2 build and test results, other macOS hardware, Linux and Windows builds, and behavior in a title were not independently verified. No changed files were excluded from the review.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by Jazeupoi on 2026-11-25T09:37:33Z.
Review: COMMENTED
Tested on a second Mac.
Setup: Apple M4, macOS 27.0.1, Apple clang 21.0.0, x86_64 Release under Rosetta 2. Head
9af021255amerged cleanly ontomain44eccbf1b0plus #17425, #11049, #25331 and #26739. The control runs are the same tree without this PR.mainguest_fault_copyguest_icu_comparisonsguest_icu_comparisonswithAPS5_GUEST_COPY_SYSCALL=1guest_http2_cookiesBecause the copy handler now sits in front of the write watch handler, I also ran the
write_watch_*,guest_memory,guest_socket_*andguest_kernel_socket_*tests with this PR: 49 of 51 pass. The two that fail,guest_socket_optionsandguest_socket_port_zero, fail the same way on the control tree (#17879 and #17878 target them).No title was run.
AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-26T21:19:08Z.
Review: COMMENTED
Actionable comments posted: 6
86c94e41-01f5-4e1e-8b5b-7cb147484e5bReviewing files that changed from the base of the PR and between
9c0d60cbfbandc98153c2b2.core/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/include/specifics/macos/GuestFaultCopy.hppcore/libs/prx/libc/src/General.cppcore/libs/prx/libc/src/specifics/macos/GuestFaultCopy.cppcore/libs/prx/libc/tests/cmake/guest_fault_copy.cmakecore/libs/tests/GuestFaultCopy.cppcore/libs/tests/GuestHttp2Cookies.cppdocs/dev/ENVIRONMENT.mddocs/dev/debt/changes/macos-guest-fault-copy.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T21:29:57Z.
Review: COMMENTED
Reviewed — no blocking findings
The diff for head
c98153c2b2f8759d683de7d1fc7705b74d2d30f4adds checked in-process guest copies on macOS x86-64; I found no verified blocking defect, but could not run the checks or validate Intel Mac behavior.What I ran
No commands or tests were run; repository reading was limited to the prepared review artifacts and targeted code ranges at the pinned head. The review artifacts report author-run macOS Rosetta 2 checks, but those results were not independently verified.
Results
Findings
Blocking
None verified.
Non-blocking
SA_RESETHANDwhensigactiondoes not report that flag. The debt entry documents this limitation, and the test skips the case when the flag is unreported. Consider resolving this before relying on exact one-shot semantics for host-registered handlers. Evidence:core/libs/prx/libc/src/specifics/macos/GuestFaultCopy.cpp:215andcore/libs/tests/GuestFaultCopy.cpp:133.Not verified
core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/include/specifics/macos/GuestFaultCopy.hpp,core/libs/prx/libc/src/General.cpp,core/libs/prx/libc/src/specifics/macos/GuestFaultCopy.cpp,core/libs/prx/libc/tests/cmake/guest_fault_copy.cmake,core/libs/tests/GuestFaultCopy.cpp,core/libs/tests/GuestHttp2Cookies.cpp,docs/dev/ENVIRONMENT.md, anddocs/dev/debt/changes/macos-guest-fault-copy.md. All nine changed files listed in the manifest were read. The prepared description, commits, manifest, and diff were also read.To get this to a good state
Nothing is required. Optional: address the documented
SA_RESETHANDlimitation for host handlers before promising one-shot chaining semantics.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-12-03T12:21:52Z.
Review: COMMENTED
Actionable comments
416f29c6-e0e7-4ce3-8336-e2e175dd36a4Reviewing files that changed from the base of the PR and between
c98153c2b2and81df5ebf3e.core/libs/prx/libc/CMakeLists.txtcore/libs/prx/libc/src/General.cppcore/libs/tests/GuestHttp2Cookies.cppdocs/dev/ENVIRONMENT.mdIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-12-03T12:28:31Z.
Review: COMMENTED
Reviewed — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
81df5ebf3e63de842ad221b31b90da9d68f52e31using the prepared diff and the nine included head snapshots. I did not run tests or compile code.Results
Negative control
Not run; no test execution was performed.
Findings
Blocking
None.
Non-blocking
SA_RESETHANDthroughsigaction; the PR documents this behavior and limits the test to hosts that report the flag. GuestFaultCopy.cpp, debt noteSources
Reviewed the prepared snapshots for all nine changed files listed in
files.json:core/libs/prx/libc/CMakeLists.txt,core/libs/prx/libc/include/specifics/macos/GuestFaultCopy.hpp,core/libs/prx/libc/src/General.cpp,core/libs/prx/libc/src/specifics/macos/GuestFaultCopy.cpp,core/libs/prx/libc/tests/cmake/guest_fault_copy.cmake,core/libs/tests/GuestFaultCopy.cpp,core/libs/tests/GuestHttp2Cookies.cpp,docs/dev/ENVIRONMENT.md, anddocs/dev/debt/changes/macos-guest-fault-copy.md.Not verified
The prepared description reports testing under Rosetta 2 but no Intel Mac run; I did not independently verify those reported results. The pinned reader lookup could not complete because it attempted to write its read log in the read-only run directory. No build, tests, or other platform checks were run.
To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T15:55:03Z.
Review: COMMENTED
Chaincopies a non-atomicsigactionfrom the active slot and can also rewrite that slot forSA_RESETHAND. Multiple signal handlers can touch the same slot concurrently. The installer can also reuse an old slot while a handler is still reading it. The atomic active index publishes the selected slot but gives no lifetime guarantee for its contents. Please keep published handler state immutable for readers and track one-shot delivery separately.Source review from Linux.
AI-assisted: yes.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.