fix(libc): keep errno across the host read and write hooks of checked guest copies #21243
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21243"
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 milescoviello on 2026-11-16T01:24:57Z.
What
GuestCopyIn_nid_no_patch,GuestCopyInUnsynced_nid_no_patchandGuestCopyOut_nid_no_patchkeeperrno(dfb9b774fc, "keep errno when a checked guest copy faults on Linux"). They didn't, however, keep it across the hooks they run before the copy:CopyGuestBytescallsGuestArenaPrepareHostRead_nid_no_patch, which runs the host read hook, before it saveserrno.GuestCopyOut_nid_no_patchopens aGuestArena::HostWrite, which runs the host write hook, before its save. That bracket also closes, throughGuestArenaEndHostWrite_nid_postfix, after the restore.libSceAgcDriver installs both hooks (
GuestMemory.cppPrepareHostRead, andLifecycle.cppwithGuestMemory::PrepareHostWrite). They run driver code that waits on and flushes GPU work, which may changeerrno. When a hook does, the change reaches the guest through every export that reports a checked-copy failure as a return code. #20829 works around this per function with anerrnoguard in each export.This keeps
errnoonce, for the whole copy.CopyGuestBytesandGuestCopyOut_nid_no_patchnow hold aRestoreErrno(GuestWritable_nid_no_patch's local struct, moved to the file's anonymous namespace so all three use it) from before the hooks run until after the write bracket closes. The existing saves aroundprocess_vm_readv/process_vm_writevstay; they are now redundant but harmless, and leaving them keeps this from touching lines other open PRs change (#21156 edits next to them).Tested
Linux (Ubuntu 26.04.1, kernel 7.0), GCC 15.2, Ninja, Release,
-DBUILD_TESTING=ON:guest_copy_hook_errnoinstalls host read and write hooks that seterrnoto 99 and count their calls. It then checks thatGuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patchkeeperrnoat 71 for a good copy and for an unmapped0x10, both ways, and that each hook ran twice. Passes.main'sGeneral.cppand this test,guest_copy_hook_errnostops at line 37:GuestCopyIn_nid_no_patchleaves the read hook's 99 inerrno.guest_output_pointer_fault(unmapped and kernel-space addresses keeperrno, plus the seccomp case that must throw): passes.Windows 11 (VM, 4 vCPU, WinLibs GCC 15.2, Release):
guest_copy_hook_errnoandguest_output_pointer_faultpass, 3 of 3 runs.tools/check_conventions.pypasses on the branch and this body. A merge check against the open PRs that changeGeneral.cpp(#21156, #20832, #20678, #20812, #20813, #18112, #11446, #9371, #20315, #15973) finds no new conflicts: each has the same conflicts with this branch as withmain.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 PrivacyOnion on 2026-11-16T01:33:37Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
ea637fef4e:tools/check_conventions.pypassed;git diff --checkclean.guest_copy_hook_errno_testsbuilt cleanly.guest_copy_hook_errno):core/libs/prx/libc/src/General.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.RestoreErrnoincore/libs/prx/libc/src/General.cpp(lines 88-91) captureserrnoon construction and restores it on destruction across all return and exception paths.CopyGuestBytesincore/libs/prx/libc/src/General.cpp(line 94) constructsRestoreErrnobeforeGuestArenaPrepareHostRead_nid_no_patch(source, bytes), preservingerrnoacross the host read hook on Linux, Windows, and macOS.GuestWritable_nid_no_patchincore/libs/prx/libc/src/General.cpp(line 834) uses the anonymous-namespaceRestoreErrnowith identical RAII semantics as before.GuestCopyOut_nid_no_patchincore/libs/prx/libc/src/General.cpp(lines 871-873) declaresRestoreErrnobeforeGuestArena::HostWrite guard(destination, bytes), ensuring destruction order closesguard(andGuestArenaEndHostWrite_nid_postfix) beforerestorerestoreserrno.core/libs/tests/GuestCopyHookErrno.cppinstalls both read and write hooks that clobbererrnoto99, tests both valid and unmapped (0x10) pointers forGuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patch, verifieserrno == 71and hook call counts (g_reads == 2 && g_writes == 2), and resets both hooks tonullptrbefore exit.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T02:14:30Z.
Review: COMMENTED
Reviewed — no blocking findings
The errno guards cover the host hooks and the write bracket, and the added test exercises successful and faulting copies; I found no verified blocking defect.
What I ran
Inspected head
ea637fef4ecd331766938ebb6b7399932a55ea20usingrepo_read.py. No build or tests were run in this review; test results in the PR description are author-reported.Results
Findings
Blocking
None identified in the inspected code.
Non-blocking
None.
Sources
core/libs/prx/libc/src/General.cpp:RestoreErrnospans the read hook and copy, and the output-copy guard spansHostWritedestruction.core/libs/tests/GuestCopyHookErrno.cpp: exercises errno preservation for successful and unmapped input and output copies.core/libs/prx/libc/tests/cmake/guest_copy_hook_errno.cmake: registers the new test.Not verified
Did not run tests, build, or independently verify the author-reported environment and results. Read the changed
General.cppranges aroundCopyGuestBytes,GuestWritable_nid_no_patch, and the guest copy functions; read both added test files in full. Also readGuestArena.hppandGeneral.hppfor relevant declarations. No changed files were excluded from the review folder listing; the remainder ofGeneral.cppwas not read.To get this to a good state
Nothing is required. Optional: run the reported checks on this head in CI or locally.