feat(libkernel_sys): implement fstat #9008
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9008"
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 verusferro on 2026-10-21T09:35:23Z.
What
fstatin libkernel_sys was aNotImplemented_nid_no_patchstub from the discovery exports. It now calls libkernel's_fstat, asfstatfsin the same file already calls_fstatfs. It has FreeBSD'sint fstat(int, struct stat*)signature, and it gives the same status,EFAULTfor a null buffer andEBADFfor a descriptor that is not open as libkernel'sfstat.guest_kernel_syscompares it with_fstat, as it doesfstatfswith_fstatfs.Tested
Ubuntu 24.04 (WSL2) x86-64, GCC 13.3, Release, full build with
BUILD_TESTING=ON:ctest -R '^(patched_libraries|guest_kernel_sys|conventions_unknown_nids)$': 3/3 pass;patched_librariesreports 357 patched libraries loaded, 0 failed.libkernel_sys.prxexportsfstat(mqQMh1zPPT8) and imports_fstat(A0O5kF5x4LQ), which patchedlibkernel.prxexports.guest_kernel_sysgains anfstatcheck beside thefstatfsone:fstatand_fstaton the same descriptor give a byte-identicalFileStatwithst_size1; a null buffer gives -1/EFAULT; a closed descriptor and -1 give -1/EBADF. Making the forwarder return 0 fails the null-buffer check.fstatreturns 0 withst_size5 andS_IFREG, byte-identical to_fstat.tools/progress.py: libkernel_sys 13 → 14 done, 532 → 531 todo.python3 tools/check_conventions.py --base origin/main: passed.python3 tools/pr_overlap.py: #9103 and #9161 remove the stub next to thefstatstub inExport.Discovery.cpp, and #9294 adds functions wherefstatgoes inExport.cpp; none implementsfstat, and whichever lands second keeps both sides.Windows: built and tested by @ycbuzi in the comments. Not tested: macOS, titles.
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 ycbuzi on 2026-10-21T17:51:27Z.
Windows run of
29f1a34ccbmerged ontomain80565c5942(594200db72, clean), the platform the description lists as not tested. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release,BUILD_TESTING=ON.Built
libs,libkernel_sys,guest_kernel_sys_testsandpatched_libraries_testswith no errors; the two changed files compile without warnings, and the three the full build prints come from files this PR does not change.ctest --repeat until-fail:2onpatched_libraries,guest_kernel_sysandconventions_unknown_nids, the set in the description: all three pass on both repeats.I ported the description's probe to Windows: a program linked against the unpatched
libkernel_sys,libkernelandlibcimport libraries that opens a 5-byte file with the guestopen. All nine checks pass:fstatst_size5,S_IFREGFileStatagainst_fstaton the same descriptorEFAULTEBADFNot run: the patched-NID form of the probe, a title.
Imported from boykopovar/AnyPS5. Originally posted by mpratsaki on 2026-10-21T19:52:56Z.
A public homebrew that reaches this stub on Windows today: LakeSnes from the ps5-payload-dev websrv v0.34 bundle (
lakesnes.elf). Static analysis only; nothing was run.lakesnes.elfhaslibkernel_sys.sprxinDT_NEEDEDand importsfstatunscoped by name (the SDK form, 226 unscoped imports, no scoped ones).main2457319ab(--windows --trap-sdk-syscall-stubs --skip-sce-module), the startup library list is… libkernel_sys.prx, libSceLibcInternal.prx, libSceNet.prx, libc.prx, libkernel.prx: canonicallibkernel.prxis added last since #8867.fstatby name, so the stub falls back to its NIDmqQMh1zPPT8and takes the first library in that list exporting it. Since #8837,libkernel_sys.prxdoes (fstat_nid_postfixinExport.Discovery.cpp, aNotImplemented_nid_no_patchstub), ahead of libkernel'sfstat_nid_disambig1_nid_postfix. So LakeSnes bindsfstatto the throwing stub, where this PR's forwarding to_fstatwould give it libkernel's implementation.Same search over the v0.1.3 Windows
libs/export tables for its other imports: with #8867's addedlibkernel.prxall 226 resolve, andfstatis the only one that a #8837 stub reaches first. Of the 527 #8837 stubs,fstatandpwrite(#9019) are the two whose NID libkernel.prx v0.1.3 also exports.AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ycbuzi on 2026-10-21T20:19:58Z.
@mpratsaki I ran it on Windows: LakeSnes doesn't get as far as
fstat.lakesnes.elffrom websrv v0.34 (LakeSnes.zipsha25660ce0b19…, matching the release digest), converted with--windows --trap-sdk-syscall-stubs --skip-sce-module --trace-importsonmain0c6a5bd593and on this PR merged onto it (0cadf6d264), stops at start-up withFATAL: unhandled exception 0xc0000005and 0 presents, with no ROM and with each of the two bundled ROMs: 3 of 3 runs on each build, the same frames every time (lakesnes.exe+0x13e66b,+0x13e5d4).The fault is the SDK startup's first call through its entry argument:
_startpassesrdion unchanged, the function it calls keeps it inrbx, and the first function that one calls runscall *(%rbx)withedi= 1 andrsi="sceKernelDlsym"(lakesnes.exe+0x13e9a1). That matchespayload_initand__crt_syscall_initin the SDK's crt callingpayload_args_t::sys_dynlib_dlsym, the entry #9381 describes on Linux. So on Windows LakeSnes can't show this PR's effect until that entry is supplied. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0, Release, RTX 4070 SUPER.Imported from boykopovar/AnyPS5. Originally posted by mpratsaki on 2026-10-21T21:11:30Z.
@ycbuzi Thanks for running it and for tracing the fault. You're right, and "reaches this stub" in my comment overstated a static result: the import binding I described happens at start-up, but LakeSnes is an SDK payload and stops in
payload_init(#9381) before it can callfstat. So it isn't evidence that this PR changes a running program; please don't count it as title evidence for #9008.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T04:12:38Z.
Review: COMMENTED
Checked against main
6ef5a1c4cand FreeBSD 11.4 sys/sys/stat.h / ps5-payload-dev/sdk sce_stubs/libkernel_sys.c:int fstat(int, struct stat *)and the existingfstatfs→_fstatfspattern above it. Errors come from libkernel'sfstat(Stdio.cpp:896-912): EFAULT for a null buffer on an open descriptor, EBADF otherwise.fstatfshas the same check in core/libs/tests/GuestKernelSys.cpp:278-285: same-descriptor comparison with_fstatfs,nullptr→ -1/14, and closed or -1 descriptor → -1/9. Adding thefstat/_fstatequivalent there would keep the forwarder covered if either side changes.fstat.BUILD_TESTING=ONbuild) merged onto main6ef5a1c4c; the only failing step is the existingImageArrayWindowPastLayerLimit.cppalignment error that #8310 fixes. Not run here: this machine's Smart App Control blocks freshly built test executables.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by verusferro on 2026-10-22T12:35:56Z.
@harisibrar1 Thanks. Added in
fa5fc8d5abeside thefstatfscheck:fstatand_fstaton the same descriptor give a byte-identicalFileStat, a null buffer gives -1/EFAULT, and a closed descriptor and -1 give -1/EBADF. Making the forwarder return 0 fails the new null-buffer check.guest_kernel_sys,patched_librariesandconventions_unknown_nidspass on Linux (Ubuntu 24.04, GCC 13.3, Release).