fix(libkernel): set guest errno on failure in sceKernel file calls #24960
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/24960"
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 birdmarty on 2026-11-21T13:12:03Z.
What
Set guest
errno(*__error()) on failure and preserve it on success across the POSIX-pairedsceKernel*file and descriptor calls inlibkernel.On the PS5 console,
libkernel'ssceKernel*wrappers return0x80020000 | errnoon failure and also store thaterrnoin*__error()(already recorded indocs/dev/debt/libkernel.mdforsceKernelFcntlon firmware 13.60,sceKernelGetAppInfoon firmware 12.70/13.09,sceKernelGetProcessAbiTypeon firmware 13.60, and inlibkernel_sys.mdforsceKernelGetAppCategoryTypeon firmware 13.590.001). In AnyPS5,sceKernelFcntlandsceKernelFtruncatealready behaved this way because they delegated to their POSIX implementations, andsceKernelClose/Read/Write/Readv/Writevdid so only for guest sockets, while the remainingsceKernel*file calls returned0x8002xxxxwithout setting*__error(). Because__error()aliases hosterrno, internal host calls (such asstd::filesystem::existsinsceKernelMkdir) could also overwrite*__error()withENOENTeven when asceKernel*call succeeded.Guest code that calls
sceKernelMkdir(or anothersceKernel*file call) and inspects*__error()on a nonzero return (for example checking*__error() == EEXISTaftersceKernelMkdirreturns0x80020011) therefore saw a staleerrnoinstead of17.This reverses the rule an earlier change applied to
sceKernelReadv:829f40c523(a follow-up to #13798) made it keeperrnowhen its iovec copy faults, on the reasoning that a call reporting the fault through its return code should not touch the guest'serrno, andguest_output_pointer_faultchecks that. On the console thesceKernel*wrappers return0x80020000 | errnowitherrnoset (the measurements above), and a readv whose iovec array cannot be read fails in the kernel withEFAULT, sosceKernelReadvnow setserrnoto 14 like the other wrapped calls, and the test'ssceKernelReadvcheck expects 14. The checked copy helpers themselves (GuestCopyIn/GuestCopyOut,GuestSocketReadGuest,CopyIovecs) still keeperrnoon a fault, and the test's checks of them are unchanged. That this readvEFAULTpath setserrnoon the console is inferred from those wrappers, not measured.Changes
core/libs/prx/libkernel/File/src/Stdio.cpp,core/libs/prx/libkernel/File/src/Open.cpp: wrap the POSIX-pairedsceKernel*file/descriptor calls (sceKernelOpen,Close,Read,Write,Lseek,Stat,Unlink,Fstat,Fsync,Fdatasync,Getdirentries,Getdents,Mkdir,Rmdir,Rename,Chmod,Fchmod,Truncate,Ftruncate,Utimes,Futimes,Pread,Pwrite,Readv,Writev,Preadv,Pwritev,Rdup) withFile::RunKernelFileCall(core/libs/prx/libkernel/File/include/File.hpp), which saves*__error()before the call, storesstatic_cast<int>(result) & 0xffffin*__error()whenresult < 0, and restores the savederrnowhenresult >= 0.sceKernelOpen,sceKernelCloseandsceKernelLseekare wrapped whole, soerrnois set after their failure logging.core/libs/tests/GuestKernelFileErrno.cppandcore/libs/prx/libkernel/tests/cmake/guest_kernel_file_errno_tests.cmake: addguest_kernel_file_errno, verifying that each of these calls sets the matching guesterrnoon failure (includingsceKernelMkdiron an existing directory settingerrno == 17over a priorENOENT) and leaves a preseterrnountouched on success.core/libs/tests/GuestOutputPointerFault.cpp:sceKernelReadvover an unreadable iovec array now expectserrno14 (see above); its checks of the copy helpers keepingerrnoare unchanged.docs/dev/debt/changes/fix-libkernel-file-calls-set-errno.md: note that individual per-error-path console measurement for each wrapper (including user-space argument checks and host-safetyEFAULTcopies) has not been done.Tested
Windows 11 Pro (build 26300), Intel Core i9-9900K, MinGW-w64 GCC 15.3.0, Release build with tests, based on
origin/main8ab98849d5.origin/main8ab98849d5Stdio.cppandOpen.cppbuilt with the new test):guest_kernel_file_errnofails immediately at line 70 (GuestKernelFileErrno failed at line 70 (guest errno 2)), wheresceKernelMkdiron a new directory clobbered the sentinelerrnoto2(and on an existing directory leaveserrnoat2instead of17).37052d92e0):guest_kernel_file_errnoand 13 related file/kernel tests: 14/14 passed (guest_kernel_file_errno,guest_filesystem,guest_kernel_sync,guest_posix_scalar_io,guest_kernel_sys_dup,guest_truncate,guest_truncate_race,guest_truncate_extend,guest_file_timestamps,kernel_error,guest_kernel_errors,guest_path_name_length,guest_sce_v2_filesystem_calls,guest_kernel_sys).guest_file_lock_*: 7/7 passed.guest_fios2: 1/1 passed.public_headers: passed.53fe6e13e4: shared helper,errnoafter logging,guest_output_pointer_faultupdate): the Windows CI run on37052d92e0failed onlyguest_output_pointer_fault(itssceKernelReadvcheck expected the old kepterrno). With the follow-up, on the same base, 104 file, descriptor and kernel tests passed and 1 skipped itself (guest_fopen_errors):guest_kernel_file_errno,guest_output_pointer_fault,guest_filesystem,guest_filesystem_error,guest_kernel_fstat,guest_kernel_pread_pwrite,guest_kernel_rename,guest_fstat_fault,guest_fstat_fault_syscall,guest_file_vector,guest_file_vector_memory,guest_truncate*,guest_fios2*,guest_file_lock_*,guest_kernel_sys*,guest_posix_scalar_io,guest_sce_v2_filesystem_calls,guest_path_name_length,guest_rename_dot,guest_app_info,kernel_errorand the others in the same groups.ffc0cb860c): the second commit had wrappedsceKernelOpenin a lambda, so its three log lines were taggedoperator();sceKernelOpennow setserrnoat each return throughFile::KernelFileResult, after its logging. 104/104 of the same tests pass (onmaine60b2f74adwith my other open PRs), and a Forza Horizon 5 log shows 9,305[src/Open.cpp:406 sceKernelOpen]lines and nooperator()tag.py -3 tools/check_conventions.py --base origin/main:All conventions checks passed.py -3 tools/debt_fold.py --check: fails only on an unrelated pre-existingmainfragment (docs/dev/debt/changes/feat-libwebrtc-rnn-vad.md:3); this branch's fragment passedcheck_conventions.py.py -3 tools/pr_overlap.py --names sceKernelMkdirand--branch fix/libkernel-file-calls-set-errno(2,179 open PRs): no open PR implements this change. Textual overlaps onOpen.cppare unrelated (#12682 read cancellation, #24487/#24488 ksem id checks); the 12 conflicted PRs touchingStdio.cpp/Open.cpp(#6936, #7121, #7447, #9293, #9297, #9302, #12688, #12700, #12965, #15037, #18190, #23799) and the library-split/mirroring PRs (#18112, #18190, #24322, #24910, #24917) were checked and none overlap functionally.libkerneldebt notes.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-21T13:15:44Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
37052d92e0:tools/check_conventions.pypassed.guest_kernel_file_errno_testsbuilt cleanly.guest_kernel_file_errno):core/libs/prx/libkernel/File/src/Open.cpp,core/libs/prx/libkernel/File/src/Stdio.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T13:19:29Z.
Review: COMMENTED
Reviewed — no blocking findings
The changed code and test files were inspected, but the review is incomplete because the full diffs and all changed-file contents were not examined; no formal approval is justified.
What I ran
37052d92e02e2a8c63d3b31add74a70e274e9772using the pinnedrepo_read.pyreader.Results
Findings
Blocking
None identified in the inspected portions.
Non-blocking
None.
Not verified
The full contents of the changed files and the remaining diff were not read. Files listed as reviewed in the manifest:
Open.cpp,Stdio.cpp,guest_kernel_file_errno_tests.cmake,GuestKernelFileErrno.cpp, andfix-libkernel-file-calls-set-errno.md. No files were excluded according to the file listing.To get this to a good state
Nothing is required. Complete inspection of the remaining diff and changed-file contents before making a formal review decision.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-21T14:50:04Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
53fe6e13e4:tools/check_conventions.pypassed.guest_kernel_file_errno_tests,guest_output_pointer_fault_testsbuilt cleanly.guest_output_pointer_fault,guest_output_pointer_fault_syscall,guest_kernel_file_errno):core/libs/prx/libkernel/File/include/File.hpp,core/libs/prx/libkernel/File/src/Open.cpp,core/libs/prx/libkernel/File/src/Stdio.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-22T11:54:47Z.
Review: APPROVED
Windows check of head
ffc0cb860cwith GCC 16, merged ontomain7eb79b0c48(clean merge).Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (
x86_64-ucrt-posix-seh, r3), CMake 4.4.4, Ninja 1.13.2, Release full build, run from PowerShell. This was built together with #22140, which touches other files.guest_kernel_file_errno,guest_output_pointer_fault,guest_filesystem*,guest_kernel_fstat,guest_kernel_pread_pwrite,guest_kernel_rename,guest_fstat_fault*,guest_file_vector*,guest_truncate*,guest_fios2*,guest_file_lock_*,guest_kernel_sys,guest_posix_scalar_io,guest_sce_v2_filesystem_calls,guest_path_name_length,guest_rename_dot,guest_app_info,guest_kernel_sync,guest_kernel_sys_dup,guest_file_timestamps,guest_kernel_errors,kernel_error):main37 of 37, and with this PR 38 of 38 in 3 of 3 runs. The extra one isguest_kernel_file_errno.File::KernelFileResultkeeping the savederrnoon failure too (both overloads:= result < 0 ? (… & 0xffff) : saved;→= saved;),guest_kernel_file_errnoaborts (0xc0000409).The approach reads right to me. One helper sets
errnoto the low 16 bits of the0x8002xxxxresult after the call's own logging, and restores the caller'serrnoon success, so host calls like thestd::filesystemchecks insidesceKernelMkdirno longer leakENOENTinto the guest. ThesceKernelReadvchange from keepingerrnoto setting 14 is explained in the description, and the fragment notes that the per-path console behaviour isn't measured.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T12:17:50Z.
Review: COMMENTED
Reviewed — comments
Nothing blocks this; the points below are optional.
What I ran
Reviewed head
ffc0cb860ca36c68411523599ead1f73987fe168usingrepo_read.pytools. No commands or tests were run by me; test results in the PR description are author-reported.Results
Findings
Blocking
None.
Non-blocking
sceKernelGetdirentries/sceKernelGetdentsonly as failure cases for invalid descriptors, and does not coversceKernelOpen's random-device path. Adding success and error cases for those branches would strengthen the wrapper coverage. Relevant changed wrappers:core/libs/prx/libkernel/File/src/Stdio.cpp:1736andcore/libs/prx/libkernel/File/src/Open.cpp:407.EFAULTerrno behavior on console is inferred rather than measured. Keep that distinction explicit until firmware measurement confirms it.Not verified
manifest.jsonwere read:File.hpp,Open.cpp,Stdio.cpp,guest_kernel_file_errno_tests.cmake,GuestKernelFileErrno.cpp,GuestOutputPointerFault.cpp, andfix-libkernel-file-calls-set-errno.md.files.jsonwas not available through the reader; no exclusions are listed in the manifest.To get this to a good state
Nothing is required.
Optional: extend the test to cover successful directory-entry reads and the random-device open branch, and measure the readv copy-fault errno behavior on console.