fix(libkernel): return errno from fstat, flock and write instead of throwing on a bad argument #4929
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4929"
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 xXPBSXx on 2026-10-13T04:55:36Z.
What
Three calls in libkernel
Stdio.cppthrew a C++ exception on an argument FreeBSD answers with an errno. A throw out of a guest call ends the process, where the title would see-1and handle it:fstatstruct statEFAULT(14) (the copyout fails)flockflock failed ... error=6EBADF(9),sys_flocktakes the descriptor withfgetfirstwriteLONG_MAX; the test covers -1 only)EINVAL(22),sys_writefailsnbyte > IOSIZE_MAX(sys_writeinsys/kern/sys_generic.cbegins withif (uap->nbyte > IOSIZE_MAX) return (EINVAL);sys/sys/systm.hlines 165-169 defineIOSIZE_MAXasiosize_max()on LP64 andSSIZE_MAXotherwise, andiosize_max()insys_generic.creturnsSSIZE_MAX, orINT_MAXfor a 32-bit process or with thedebug.iosize_max_clampsysctl set (default 0); all read inrelease/11.4.0; a negative length is above it)(
fstaton a closed descriptor or -1 threw as well; #4391 fixed that onmain, and the test keeps checking it.)flockkeeps its throw for host errors it does not know; onlyERROR_INVALID_HANDLE(Windows) andEBADF(Linux) becomeEBADF. The negative-length check ofwritecomes before the guest-socket branch, so it applies to a guest socket as well; the test covers a file descriptor only.Found by calling these functions with invalid arguments, one process per call, on
main; the other calls tried (fsync,fdatasync,fchmod,futimes,ftruncate,lseek,readv,writev,preadv,pwritev,pwrite,ioctl,dup,dup2,fcntl,readwith a null buffer) already returnEBADForEFAULT.preadthrows too; the open #4719 changes that (git merge-treeof the two heads is clean).No title evidence: I did not observe a title that calls one of these on a bad descriptor. The reason to fix them is that they end the process instead of failing the call, not a conformance detail.
Overlap: #4903 (
fstaton a guest socket) is open and the overlap bot lists it forcore/libs/prx/libkernel/File/src/Stdio.cppL645-648, thefstathead next to the null check rewritten here; whichever lands second needs a small rebase. #5403 contains #4903's commit and conflicts here for that reason: it replaces the line after the null check, this PR rewrites the null check itself. Both changes apply together (sb == nullptrreturnsEFAULT, then a guest socket reports its status). Otherwisegit merge-treeof the headd3e58a1ceagainstmainbf8dc610eis clean (checked, not rebuilt); the test registration sits beforeguest_socket_descriptorsincore/libs/prx/libkernel/CMakeLists.txt, away from the other new tests. #4818 (failwritewithEINVALfor a size aboveSSIZE_MAX, open, older) contains the same one-line change inwrite(if (size < 0) return PosixFailure(GUEST_EINVAL););git merge-treeof the two is clean, whichever lands second drops the line.Tested
Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, on
mainc2565f014; moved since to a newermain(git merge-treeof the head againstmainbf8dc610eis clean; not rebuilt there) with its test registration at its own place:guest_descriptor_bad_arguments(new) passes: all three cases and thefstatcheck on a closed descriptor, plusfstatandflockon an open file still succeed.Stdio.cpp(fstat_nid_disambig1_nid_postfix: invalid argument, abort). The test stops at its first failing check, so that run names only the first case (fstatwith a null buffer); thewriteandflockcases were not run one by one without the change (the old code throws in each of them, see the diff).guest_file*,guest_dup,guest_socket*,guest_poll,guest_selectandguest_filesystemtests pass.python tools/check_conventions.py --base upstream/mainpasses.-j4), the headd2e7f675fmerged ontomain7a0c838ca:guest_descriptor_bad_argumentspasses (0.62 s, ctest); withStdio.cpptaken back tomainand the new test kept it fails (uncaughtinvalid_argument, exit code0xc0000409). Second run, head merged ontomaind1670009f, the targets of the group built as well: onmainaloneguest_filesystem,guest_file_vector,guest_memory,guest_kernel_sys,guest_signalsandguest_directory_bufferpass (6 of 6); with the head and its test 7 of 7 pass, 0 failed. This replaces the earlier combined-build figures. Not run: the socket tests, Linux, a console, a title.d2e7f675f(test only):guest_descriptor_bad_argumentsremoves a leftover test file at its start and closes its descriptor and removes the file before a failing check ends the process. Rebuilt and run on the new head (base1e7f4967e, Windows 10, MinGW GCC 15.2.0, Release,-j4):guest_descriptor_bad_argumentspasses; withStdio.cpptaken back to the base it fails (the old code throwsinvalid_argument, exit code0xc0000409);python tools/check_conventions.py --base upstream/mainpasses. A run that ends by such a throw still leaves the file behind (the process does not reach the test's own cleanup); the next run removes it first.flockbranch forEBADFand thefstatchange build from one source, only Windows was built), a console, a title run.Checklist
main; no other open PR implements the same functions (overlap with #4903 and #4719 described above)NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt (docs/dev/debt/libkernel.md).mdfiles or images added to the repositoryAI-assisted (Claude Code).
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:35:55Z.
Closing: no title is known to reach this. With the queue at 700+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.
Pull request closed