Repository navigation
fix(libc): keep errno when a checked guest copy faults on Linux - #13664
Conversation
GuestCopyIn and GuestCopyOut report a fault through their return value. On Linux, process_vm_readv/process_vm_writev also left EFAULT in errno, which the guest shares, so a caller that reports the fault through a return code (or a test that checks errno is untouched) saw errno 14 on Linux only. Windows (ReadProcessMemory/VirtualQuery) and macOS (mach_vm_*) never touch errno. Restore the caller's errno on the EFAULT path so the three hosts behave the same and each caller sets the guest errno itself.
MarcZX10
left a comment
There was a problem hiding this comment.
Reviewed — looks good
The Linux EFAULT paths now restore the caller's errno; the new regression assertions exercise both checked-copy directions and are registered as guest_output_pointer_fault.
What I ran
Reviewed head dfb9b774fc5c58b509a4ee7f17c7868b8014230c against PR base 7fcab08f71e463b1768730a626a0f5472d336708. The current-main merge preview was clean against e8efeed58af48d0682ab2e0eab72cfaf996e4886. Environment: Linux x86-64, Python 3.11.2; GCC 12.2 was available but not invoked. No local C++ build or test was run.
python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 13664
python3 tools/check_conventions.py --base 7fcab08f71e463b1768730a626a0f5472d336708 --head refs/review/pr-13664-current --title 'fix(libc): keep errno when a checked guest copy faults on Linux' --body /tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13664-dfb9b774fc5c/pr-body.md
timeout 30s python3 tools/pr_test_quality.py --root /marc/AnyPS5 --json core/libs/prx/libc/src/General.cpp core/libs/tests/GuestOutputPointerFault.cpp; echo exit:$?
git grep -n 'GuestOutputPointerFault\|guest_output_pointer_fault' refs/review/pr-13664-current -- CMakeLists.txt core tools
git diff --check 7fcab08f71e463b1768730a626a0f5472d336708...refs/review/pr-13664-current
python3 tools/pr_stack_check.py 13664 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-stack-13664
cat /tmp/anyps5-stack-13664/stack-report.md
gh pr checks 13664 --repo boykopovar/AnyPS5Results
| Check | Result |
|---|---|
| Source review | On Linux, GuestArenaBeginHostWrite returns true and the write-watch hook is a no-op; the test's invalid addresses therefore reach process_vm_writev/process_vm_readv. The changed EFAULT branches restore the saved value. |
| Test registration and quality | CMake registers GuestOutputPointerFault.cpp as guest_output_pointer_fault; the test-quality heuristic's unregistered-test warning is a false positive. |
| Diff and conventions | git diff --check exited 0; conventions passed. |
| Current-main merge preview | Clean against e8efeed58af48d0682ab2e0eab72cfaf996e4886; no declared dependency. |
| Author-reported tests | The author reports 1853/1853 non-GPU tests on both base and PR, three failing baseline repetitions and three passing PR repetitions for the new errno assertion, and passing dependent checks from #13552/#13577 without their local errno changes. I did not reproduce these runs. |
| Hosted CI | Windows, Ubuntu Debug/Release register-context, relinker Ubuntu, relinker ASan, conventions, eligibility, intake, labels, library portability, and report passed. Linux was skipped; Windows relinker and macOS jobs remained pending at the last snapshot. |
| Precheck wrapper | Diff/conventions/mergeability passed. The aggregate says checks failed because its isolated checkout lacks workspace-only ABI/test-quality scripts; the ABI audit is not applicable to this PR, and I ran the test-quality heuristic directly. |
Unedited command output
Convention check:
All conventions checks passed.
Test-quality heuristic:
{
"findings": [
{
"path": "core/libs/tests/GuestOutputPointerFault.cpp",
"line": 1,
"kind": "possible-unregistered-cpp-test",
"message": "Changed C++ test source path was not found literally in CMake files; inspect registration, generated source lists, and workflow coverage."
}
],
"negative_control_candidates": [],
"limits": [
"Heuristics can miss weak assertions and can flag valid tests; inspect every finding.",
"A test that passes against base is not by itself proof that the PR test is inadequate.",
"Negative controls are only candidates until the test actually runs against base and fails by assertion rather than import/setup error."
]
}
exit:0Registration:
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2122: add_test_executable(guest_output_pointer_fault_tests ${LIBS_INCLUDE_DIR}/tests/GuestOutputPointerFault.cpp)
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2123: target_include_directories(guest_output_pointer_fault_tests PRIVATE ${LIBS_INCLUDE_DIR})
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2124: target_link_libraries(guest_output_pointer_fault_tests PRIVATE libkernel libc)
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2125: configure_windows_unwind(guest_output_pointer_fault_tests)
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2126: add_test(NAME guest_output_pointer_fault COMMAND guest_output_pointer_fault_tests)
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2127: set_tests_properties(guest_output_pointer_fault PROPERTIES TIMEOUT 15)
refs/review/pr-13664-current:core/libs/prx/libkernel/CMakeLists.txt:2129: set_tests_properties(guest_output_pointer_fault PROPERTIES ENVIRONMENT_MODIFICATION
Merge preview:
# PR stack check — fresh
Target: https://github.com/boykopovar/AnyPS5/pull/13664
Target branch tip: `e8efeed58af48d0682ab2e0eab72cfaf996e4886`
All heads unchanged: **True**
Dependency order: #13664
## Individual merge previews
- PR #13664: **clean** against current base; head `dfb9b774fc5c58b509a4ee7f17c7868b8014230c`; changed paths: none reported
## Dependency relationships
No declared PR dependencies were found in the target PR chain.
## Limits
Merge previews apply each exact PR head independently to the current base. A dependency head that is absent from its dependent is reported separately from merge conflicts; file overlap is only a signal and does not prove a conflict. This tool does not build, resolve conflicts, publish comments, or update branches.
git diff --check emitted no output and exited 0.
Precheck helper stderr:
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13664-dfb9b774fc5c/head-dfb9b774fc5c-9d110e3c/tools/pr_abi_audit.py': [Errno 2] No such file or directory
/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-13664-dfb9b774fc5c/head-dfb9b774fc5c-9d110e3c/tools/pr_test_quality.py': [Errno 2] No such file or directory
Negative control
| Assertion | Baseline change | Result |
|---|---|---|
| The errno-preservation assertion catches the Linux EFAULT leak | Author reports the new assertion against the base without either fix hunk | Author reports 3/3 failures at GuestCopyOut; with only the GuestCopyOut hunk, 3/3 failures at GuestCopyIn; with both hunks, 3/3 passes. Not independently reproduced. |
Findings
Blocking
None found in the reviewed change.
Non-blocking
The Linux full-suite and focused negative-control results are author-reported; Linux CI was skipped. Windows relinker and macOS checks were still pending at the last snapshot.
Sources
- Linux checked-copy changes at the reviewed head
- Regression assertions at the reviewed head
- Linux host-write path used by the regression test at the reviewed head
- Guest
errnoaccessor at the reviewed head
Not verified
No local build or C++ test was run. Linux, Windows relinker, and macOS hosted results were not complete/available as passed at review time. The author's Linux results were not independently reproduced.
|
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
libkernel has its own checked guest copy helpers that call process_vm_readv/process_vm_writev directly: ReadGuest, WriteGuest and StoreGuestId in Process.cpp, ReadGuest/WriteGuest in Udp.cpp (also exported as GuestSocketReadGuest), CopyIovecs in Stdio.cpp and ReadGuestTime in Time.cpp. On EFAULT they left errno at 14, which is the guest errno, so a caller that reports the fault through a return code (sceKernelReadv, or sceKernelGetAppInfo in boykopovar#13798) still changed the guest's errno. Windows and macOS do not touch errno there. Apply the same rule as GuestCopyIn/GuestCopyOut: save errno before the call and restore it when the call fails with EFAULT. Failures other than EFAULT behave as before.
|
@MarcZX10 heads-up: I pushed a second commit (829f40c) on top of the head you approved (dfb9b77), no force-push. libkernel has its own guest copy helpers that call Tested on Ubuntu 26.04 x86_64 (i7-8700, GCC 15.2, Release),
Description updated with the details. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine. |
|
Rechecked against current main: the merge is clean, and |
What
GuestCopyIn_nid_no_patchandGuestCopyOut_nid_no_patchreport a fault only through theirfalsereturn. On Linux they useprocess_vm_readv/process_vm_writev, which also leaveEFAULTin the hosterrno, and the guesterrnois that same variable (__errorreturns&errno). On Windows (ReadProcessMemory/VirtualQuery) and macOS (mach_vm_read_overwrite/mach_vm_write)errnois never touched. So any caller that reports the fault through a return code, and any test that checkserrnois left alone, seeserrno == 14on Linux only.The Linux paths now save
errnobefore the syscall and restore it on theEFAULTpath, so the three hosts behave the same and each caller sets the guesterrnoitself. Every current caller inmainalready does that (*__error() = 14,Fail(GuestEfault),PosixFailure(GUEST_EFAULT),Fault()), returns an SCE error code, or throws, so none depends on the leaked value. Non-EFAULTfailures still throw as before.libkernel has its own copies of this pattern that call
process_vm_readv/process_vm_writevdirectly, so the second commit applies the same rule to them:ReadGuest,WriteGuestandStoreGuestIdinSystem/src/Process.cpp,ReadGuest/WriteGuestinSocket/src/Udp.cpp(also exported asGuestSocketReadGuest_nid_no_patch),CopyIovecsinFile/src/Stdio.cppandReadGuestTimeinTime/Time.cpp. Their callers set the guesterrnothemselves (*__error() = 14,Fail(14),fail(NET_EFAULT)) or return an SCE code (sceKernelReadv/Writev/Preadv/Pwritev), so none depends on the leaked value either;StoreGuestId's non-EFAULTpath is unchanged. The twoprocess_vm_readvprobes in libSceAgcDriver (GuestMemory.cpp,FrameRecorder.cpp) are host-side page probes, not guest copies, and are left alone.This is the shared cause of the Linux-only failures in the "return EFAULT for invalid memory" family, for example #13577 (getlogin_r) and #13552 (system version queries): with this change their tests pass on Linux without the local
errnosave/restore suggested in their reviews.Tested
Ubuntu 26.04 x86_64, i7-8700, GCC 15.2, Release,
BUILD_TESTING=ON, basemain9605300 (the branch was then rebased onto 7fcab08, which changes neither touched file).guest_output_pointer_fault:GuestCopyOut/GuestCopyInon an unmapped address keeperrnoas the caller left it. Without the fix it fails 3/3 at theGuestCopyOutcheck; with only theGuestCopyOuthunk it fails 3/3 at theGuestCopyIncheck; with the fix 3/3 pass.ctest -E "^agc|gpu|vulkan|video_out"): 1853/1853 on the base and 1853/1853 with the fix.public_headerspasses;tools/check_conventions.pyclean.guest_system_configurationaborts 3/3 onmain, passes 3/3 on this branch.errno == 12345check aftergetlogin_r(0x10, size)fails 3/3 onmainand passes on this branch. Itsgetlogin_r(0x10, 1) == 14check still fails on every host (the size check runs first and returns 34); that is a test issue in fix(libkernel): return EFAULT from getlogin_r and SetLogin for invalid memory pointers #13577, not covered here.Second commit (829f40c), base
mainc3c45d7, same machine:guest_output_pointer_faultgains two checks on the same unmapped addresses:GuestSocketReadGuest_nid_no_patch(Udp.cpp helpers) andsceKernelReadvwith an unreadable iovec table (Stdio.cppCopyIovecs, returns0x8002000e) keeperrno. Onmain+ the first commit it fails 3/3 at theGuestSocketReadGuestcheck; with only the Udp.cpp hunk reverted it fails 3/3 there, with only the Stdio.cpp hunk reverted it fails 3/3 at thesceKernelReadvcheck; with the commit 3/3 pass.mainand 1857/1857 onmain+ this branch.public_headerspasses;tools/check_conventions.pyclean.errnosave/restore removed:guest_app_infoaborts 3/3 onmain+ the first commit only, passes 3/3 on this branch (covers the Process.cppWriteGuest). With its restore kept it passes too, and a 187-test libkernel subset (process, socket, readv/writev, time, app_info) passes.Overlap: #11281 also touches
General.cpp(it adds aGuestCopyIn_nid_no_patchthatmainalready has); #9371 and #11446 touch other functions in the same file. None changes the lines edited here.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)🤖 Generated with Claude Code