Skip to content

fix(libc): keep errno when a checked guest copy faults on Linux - #13664

Merged
adriadam10 merged 3 commits into
boykopovar:mainfrom
jotitadev:fix/guest-copy-preserve-errno
Nov 8, 2026
Merged

adriadam10 merged 3 commits into
boykopovar:mainfrom
jotitadev:fix/guest-copy-preserve-errno

Conversation

@jotitadev

@jotitadev jotitadev commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

What

GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch report a fault only through their false return. On Linux they use process_vm_readv/process_vm_writev, which also leave EFAULT in the host errno, and the guest errno is that same variable (__error returns &errno). On Windows (ReadProcessMemory/VirtualQuery) and macOS (mach_vm_read_overwrite/mach_vm_write) errno is never touched. So any caller that reports the fault through a return code, and any test that checks errno is left alone, sees errno == 14 on Linux only.

The Linux paths now save errno before the syscall and restore it on the EFAULT path, so the three hosts behave the same and each caller sets the guest errno itself. Every current caller in main already 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-EFAULT failures still throw as before.

libkernel has its own copies of this pattern that call process_vm_readv/process_vm_writev directly, so the second commit applies the same rule to them: ReadGuest, WriteGuest and StoreGuestId in System/src/Process.cpp, ReadGuest/WriteGuest in Socket/src/Udp.cpp (also exported as GuestSocketReadGuest_nid_no_patch), CopyIovecs in File/src/Stdio.cpp and ReadGuestTime in Time/Time.cpp. Their callers set the guest errno themselves (*__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-EFAULT path is unchanged. The two process_vm_readv probes 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 errno save/restore suggested in their reviews.

Tested

Ubuntu 26.04 x86_64, i7-8700, GCC 15.2, Release, BUILD_TESTING=ON, base main 9605300 (the branch was then rebased onto 7fcab08, which changes neither touched file).

Second commit (829f40c), base main c3c45d7, same machine:

  • guest_output_pointer_fault gains two checks on the same unmapped addresses: GuestSocketReadGuest_nid_no_patch (Udp.cpp helpers) and sceKernelReadv with an unreadable iovec table (Stdio.cpp CopyIovecs, returns 0x8002000e) keep errno. On main + the first commit it fails 3/3 at the GuestSocketReadGuest check; with only the Udp.cpp hunk reverted it fails 3/3 there, with only the Stdio.cpp hunk reverted it fails 3/3 at the sceKernelReadv check; with the commit 3/3 pass.
  • Full non-GPU suite: 1857/1857 on main and 1857/1857 on main + this branch. public_headers passes; tools/check_conventions.py clean.
  • fix(libkernel): return EFAULT from sceKernelGetAppInfo for invalid memory pointers #13798 @ bed3f89 merged on top with its local errno save/restore removed: guest_app_info aborts 3/3 on main + the first commit only, passes 3/3 on this branch (covers the Process.cpp WriteGuest). 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 a GuestCopyIn_nid_no_patch that main already has); #9371 and #11446 touch other functions in the same file. None changes the lines edited here.

Checklist

🤖 Generated with Claude Code

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 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/AnyPS5

Results

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:0

Registration:

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

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.

@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

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.

Pull request Shared files not compared
PR 5021 core/libs/prx/libc/src/General.cpp
core/libs/prx/libkernel/File/src/Stdio.cpp
core/libs/prx/libkernel/Socket/src/Udp.cpp
core/libs/prx/libkernel/System/src/Process.cpp
core/libs/prx/libkernel/Time/Time.cpp
PR 6936 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7088 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 7121 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 7447 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 8448 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 8463 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 9161 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9162 core/libs/prx/libkernel/System/src/Process.cpp
PR 9293 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9297 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9302 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 9371 core/libs/prx/libc/src/General.cpp
PR 11896 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 12130 core/libs/prx/libkernel/Time/Time.cpp
PR 12642 core/libs/prx/libkernel/Time/Time.cpp
PR 15037 core/libs/prx/libkernel/File/src/Stdio.cpp
PR 15785 core/libs/prx/libkernel/Socket/src/Udp.cpp
PR 16585 core/libs/prx/libkernel/Socket/src/Udp.cpp

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.
@jotitadev

Copy link
Copy Markdown
Contributor Author

@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 process_vm_readv/process_vm_writev directly and were not covered by the first commit, so they still left EFAULT in the guest errno on Linux: ReadGuest/WriteGuest/StoreGuestId in Process.cpp, ReadGuest/WriteGuest in Udp.cpp, CopyIovecs in Stdio.cpp and ReadGuestTime in Time.cpp. This is why #13798 needed its own local errno save/restore around WriteGuest. The commit applies the same rule (save errno, restore it on EFAULT, non-EFAULT unchanged); every caller already sets errno itself or returns an SCE code.

Tested on Ubuntu 26.04 x86_64 (i7-8700, GCC 15.2, Release), main c3c45d7:

  • New checks in guest_output_pointer_fault (GuestSocketReadGuest_nid_no_patch, sceKernelReadv with an unreadable iovec table): fail 3/3 without the commit, each reverted hunk fails 3/3 at its own check, pass 3/3 with it.
  • Non-GPU suite 1857/1857 on main and on main + branch; public_headers and check_conventions.py pass.
  • fix(libkernel): return EFAULT from sceKernelGetAppInfo for invalid memory pointers #13798 @ bed3f89 merged on top with its local restore removed: guest_app_info aborts 3/3 with the first commit only, passes 3/3 with this one.

Description updated with the details.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@adriadam10 adriadam10 self-assigned this Oct 31, 2026
@adriadam10
adriadam10 merged commit ae2b0c0 into boykopovar:main Nov 8, 2026
19 of 20 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Rechecked against current main: the merge is clean, and Transferred, ReadGuest and WriteGuest in Udp.cpp are unchanged by the recent socket commits. Every hunk (General.cpp CopyGuestBytes/GuestCopyOut, libkernel Process.cpp, Udp.cpp, Stdio.cpp, Time.cpp) saves and restores errno on the EFAULT path only, on Linux only, and non-EFAULT failures still throw. The new guest_output_pointer_fault checks pin errno after a faulting GuestCopyOut/GuestCopyIn/GuestSocketReadGuest/sceKernelReadv, and they only pass with the restore. Conventions and Windows are green; merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants