Repository navigation
fix(libkernel): stat a path with one host query - #4938
fdfawef324 wants to merge 1 commit into
Conversation
|
Windows test of this PR. Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, Result: guest_filesystem (1 of 1, 5.4 s) pass. Not checked: whether the test fails without the change in the library code; the PR was not tested alone, only in the combined merge; no title run; Linux not tested. AI-assisted (Claude Code). |
6dd5c85 to
be8e5bb
Compare
be8e5bb to
d23a90c
Compare
73dcbfc to
d98ce20
Compare
d98ce20 to
29da462
Compare
sceKernelStat (and stat, which calls it) queried an existing path four times on Windows: std::filesystem::exists in ResolveHostPath to choose between the exact name and the case-insensitive lookup, then std::filesystem::status in sceKernelStat, then _wstat64 and a second CreateFileW in DoStat; lstat queried it three times. The queries could disagree, and sceKernelStat threw "FillFileStat: stat failed" for a file removed between status and DoStat, for a dangling link, for a directory symbolic link to a file or a file symbolic link to a directory, and for a file named with a trailing backslash or a trailing "/." component. Path resolution now takes a probe (ResolvePathProbing_nid_no_patch) in place of the exists check on the exact host name; ResolvePath_nid_no_patch and the two-argument ResolvePathError_nid_no_patch pass GuestPathExists_nid_no_patch, the former check, and the new three-argument overload passes the probe it is given. StatGuestPath passes a probe that does the whole stat: one CreateFileW, then GetFileType, GetFileInformationByHandle and GetFileInformationByHandleEx(FileBasicInfo) on that handle, and reuses the result when resolution returns the probed path. Only a case-insensitive match or an explicit native path is opened again. sceKernelStat and lstat use it; sceKernelStat tries the relinked .guest.prx name (File::PathOrRelinkedModule) only when that query reports ENOENT, so a stat of an existing file stays one query. FillFileStat and fstat of a directory descriptor use the same single-open query. The working directory is copied under its mutex and the matching alias under the alias mutex, so the file system is no longer touched while either is held. chdir still resolves under the lock. A guest path that ends in a separator or whose last component is "." or ".." names a directory, as FreeBSD lookup() treats it: stat and lstat follow a final link for it and report ENOTDIR when the result is not a directory. The check is made on the guest path, so it also holds for an aliased path, whose trailing separator resolution trims. "file/.", "file\.", "FILE/.", "fileLink/." and their "/./" forms report ENOTDIR, "file/.." reports ENOTDIR from resolution as before, "missing/." ENOENT, and "dir/.", "dirLink/." and "DIR/./" the directory, from lstat too. lstat of a link without such a suffix still throws. st_mode, st_rdev and the device and pipe fields follow the UCRT _wstat64 rules (Windows SDK 10.0.26100 ucrt/filesystem/stat.cpp); times, size, nlink, dev and ino come from the handle as before, st_blksize is 512 and st_blocks is computed from the size. Links are handled as FreeBSD does, which is not what the system _wstat64 does: stat opens a final symbolic link or junction without FILE_FLAG_OPEN_REPARSE_POINT, so the file type, the read-only bit and every other field come from the target, and only the execute bits come from the name given. _wstat64 takes the path through GetFileInformationByName, which reports the link itself (its own directory attribute and read-only bit, size 0). A dangling link, a directory symbolic link to a file and a file symbolic link to a directory, which CreateFileW cannot follow, report ENOENT, where _wstat64 reports the link. The other deviations from _wstat64: a drive or UNC root that CreateFileW cannot open reports ENOENT instead of a fabricated directory, a file named as a directory reports ENOTDIR, and a GetFileType result other than a disk file, character device or pipe, or a failure of either handle query, throws. All of them, with the probed modes, are recorded in docs/dev/debt/libkernel.md. On Linux the stat is one stat(2) or lstat(2) call. guest_filesystem gains TestStatFields, which pins mode, size, nlink, ino, dev, rdev, blocks, blksize and the four times against GetFileInformationByHandle and FILE_BASIC_INFO (stat(2) on Linux) for a file, a case-mismatched name, a read-only file, an .EXE, a directory with and without a trailing slash, a junction (a directory symlink on Linux), a read-only junction to a writable directory, a file reached through a junction, and symbolic links to a file and to a read-only file; lstat of "Junction/" against the directory; and the error and untouched buffer for a missing file, a missing parent, a dangling junction with and without a trailing slash, the empty path, a file or a file link with a trailing slash or backslash, and a directory symbolic link to a file and a file symbolic link to a directory. TestStatDotComponents pins the "." and ".." cases above, directly and through a path alias. TestStatDuringRemoval stats a file while another thread creates and removes it 300 times. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
29da462 to
f96b329
Compare
|
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. |
What
On Windows,
sceKernelStat(andstat, which calls it) queried each existing path four times:std::filesystem::existsinResolveHostPath(General.cpp). This ran under the working-directory mutex, and also under the alias mutex for an aliased path.std::filesystem::statusinsceKernelStat(Open.cpp)._wstat64inDoStat(NativeStat.cpp).CreateFileWinDoStat.lstatqueried it three times. The queries could disagree.sceKernelStatthrewFillFileStat: stat failedin these cases:statusandDoStat;.(file.txt/.,file.txt\.,fileLink/.).The change:
ResolvePathProbing_nid_no_patch) in place of theexistscheck on the exact host name.ResolvePath_nid_no_patchand the two-argumentResolvePathError_nid_no_patchpassGuestPathExists_nid_no_patch, the former check, so other callers are unchanged.ResolvePathError_nid_no_patchpasses the probe it is given.File::StatGuestPathpasses a probe that does the whole stat:CreateFileW;GetFileType,GetFileInformationByHandleandGetFileInformationByHandleEx(FileBasicInfo)on that handle.sceKernelStatandlstatuseFile::StatGuestPath.sceKernelStattries the relinked.guest.prxname (File::PathOrRelinkedModule, from fix(libkernel): resolve original .prx names to relinked .guest.prx in stat, open and reachability #4688) only when that query reportsENOENT, so a stat of an existing file stays one query.FillFileStat(path)andfstatof a directory descriptor use the same single-open query. They therefore also throw when a handle query fails, and reportENOENTwhen the open fails.GetFileTyperesult other than a disk file, character device or pipe now throws. The previous revision returnedEBADFthere, whichsceKernelStatfolded intoENOENT.chdirstill resolves under the lock.stat(2)orlstat(2).Paths that name a directory
A guest path that ends in
/or\, or whose last component is.or.., names a directory, as in FreeBSDlookup():statandlstatfollow a final link for it;ENOTDIRwhen the result is not a directory.The check uses the guest path, not the resolved host path. Resolution can drop the trailing separator, because it trims an aliased path. Path resolution itself is unchanged: it still turns
file/..intoENOTDIRandmissing/..intoENOENT, as main does.I probed this with a scratch program outside the repository, on this machine. It ran against main's libraries, the previous revision of this PR, and this revision.
statagreed withsceKernelStatin every row.sceKernelStat/lstatstatandlstatfile.txt/.,file.txt\.,FILE.TXT/.FillFileStat/ENOENT0100666, size 10 /ENOENTENOTDIRfileLink/.,FILELINK/.FillFileStat/ENOENT0100666, size 10 /ENOENTENOTDIRfile.txt/./,file.txt/.//,file.txt/ENOTDIR/ENOENTENOTDIRENOTDIRfile.txt/..,file.txt/../ENOTDIRENOTDIRENOTDIRmissing/.,missing/..,missing/./ENOENTENOENTENOENTFolder/.,Folder/./,FOLDER/.,Folder/..dirLink/.DIRLINK/./Where main threw, the new values follow FreeBSD. A component after a non-directory reports
ENOTDIR, and a.component after a link follows the link.lstatoffile.txt/reportedENOENTon main. It now reportsENOTDIR, as FreeBSD does.Mode and device fields
st_mode,st_rdevand the device and pipe fields keep the rules of the UCRT_wstat64(Windows SDK 10.0.26100ucrt/filesystem/stat.cpp):.exe,.cmd,.bator.comadds the execute bits;st_rdevis the drive number;S_IFCHRorS_IFIFO, withst_devandst_rdev0xffffffff.Times, size, nlink, dev and ino come from the handle, as before. On Windows,
st_blksizeis the constant 512 andst_blocksis computed from the size, as before.Links, and where this differs from
_wstat64Links follow FreeBSD on purpose, not the system
_wstat64. An earlier round of the same scratch probe compared the system_wstat64(with both absolute and relative paths) againstsceKernelStat,statandlstat, on main and on this branch. Each case ran with no suffix, with/and with\._wstat640100666, size 00100666, size 100100666, size 100100666, size 0010066601004440100444, size 001004440100666040555040555040777link.exetofile.txt,plainLinktotool.exe0100777,01006660100666,040777FillFileStatENOENT040777FillFileStatENOENT0100666FillFileStatENOENTfileLink/ENOENTENOTDIRENOTDIRfileLink\,file.txt\ENOENTFillFileStatENOTDIRdanglingFileLink/,dirAsFileLink/ENOENTENOTDIRENOENTroDirLink/,roJunction\040555(the link)040555040777Junction/Inner.txt/,dirLink/Inner.txt/0100666ENOTDIR;lstatreports the fileENOTDIRfromstatandlstatlstat("dirLink/"),lstat("Junction\")lstat("file.txt/"),lstat("fileLink/")ENOENTENOTDIRHow links are handled:
statfollows a final link. It opens a final symbolic link or junction withoutFILE_FLAG_OPEN_REPARSE_POINT, as FreeBSDstat(2)follows it. The file type, the read-only bit and every other field come from the target. Only the execute bits come from the name given, as in_wstat64._wstat64reports the link itself. It takes the path throughGetFileInformationByName(theGetFileInfoByNamefast path instat.cpp). That call reports a final link as itself, with or without a trailing separator: the link's own directory attribute and read-only bit, and size 0. ItsCreateFileWfallback, taken only when that call fails, follows links as this code does.CreateFileWcannot follow reportsENOENT. This covers a dangling link, a directory symbolic link to a file, and a file symbolic link to a directory._wstat64reports the link in these cases.lstatof a path that names a directory follows the final link, as described above.lstatof a link without such a suffix still throwsNotImplemented. fix(kernel): preserve errno when lstat rejects a Windows symbolic link #4691 changes the errno around that throw.The other deviations from
_wstat64:CreateFileWcannot open reportsENOENT._wstat64fabricates a directory dated 1980-01-01.ENOTDIR._wstat64reportsENOENT, or the file itself when an earlier component is a link.GetFileTypereports an unexpected type or either handle query fails, the call throws._wstat64returns the mapped error.ENOENT. FreeBSD reports the cause:EACCES,ENAMETOOLONG,ELOOP, orENOTDIRfor a path that continues past a file.Each of these deviations is recorded in the "Windows file identity" entry of
docs/dev/debt/libkernel.md. None of these values has been compared with a console.No speed change is claimed: no title was run and no trace was captured.
Related pull requests
.guest.prxnames) is now in main. This PR is rebased onto it and keeps itsTestRelinkedModuleNames, which passes.lstatrejects a Windows symbolic link)Stdio.cpp(lstat_nid_postfix).File::StatGuestPath.%VAR%mount tokens)General.hppandGeneral.cpp.ExpandPathSegmentruns before the probe, so whichever PR lands second rebases.docs/dev/debt/libkernel.mdhunk.I last checked #4691, #4565 and #4379 for the previous revision. I have not contacted their authors yet, so the order is a proposal until they agree. This PR works without any of them.
Tested
OS: Windows Server 2025, GPU RTX 3060, GCC 15.2 (winlibs UCRT). Not tested with a title.
Linux was not built locally. The Linux branches of the change and of the test are reasoned only. A full Linux CI run needs a collaborator to start it.
guest_filesystemhas three new tests:TestStatFieldscomparessceKernelStatandstatagainstGetFileInformationByHandleandFILE_BASIC_INFO(stat(2)on Linux). It checks mode, size, nlink, ino, dev, rdev, blocks, blksize and all four times for:.EXE;/;040777);0100444).TestStatFieldsalso checkslstat("Junction/")against the directory, and the junctionlstatthrow.TestStatFieldschecks the error code and that the buffer is untouched for:/;/or\(ENOTDIRfrom all three calls);TestStatDotComponentsstarts from the reviewer's reproduction and pins the first table above forsceKernelStat,statandlstat:file.txtwith/.,/./,\.,/.//,/..and/../, plusFILE.TXT/.,FileLink/.andFILELINK/.:ENOTDIR, with the buffer untouched;Missingwith/.,/..and/./:ENOENT;Folder/.,Folder/./,FOLDER/.,Junction/.andjunction/./: the directory's fields, withlstatequal tosceKernelStatofFolder;Folder/..: the parent's fields.file.txtwith/,/.,\.and/./,Missing/.,Folder/.andJunction/.SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE. If that fails, the test prints that the file-link cases were not tested, asGuestKernelSysdoes. Here they ran.TestStatDuringRemovalstats a file while another thread creates and removes it 300 times.Red:
guest_filesystemfails at the firstfile.txt/.check in 2 of 2 runs.sceKernelStatreturns 0 there.TestStatDotComponentsaborts onfile.txt/.withFillFileStat: stat failed. In the committed order,TestStatFieldsaborts first, atlstat("Junction/")(lstat of a Windows symbolic link not implemented).st_mode(main reports040555);lstat("Data.bin/")(ENOENT);Data.bin\and the dangling junction;Green, after the rebase onto current main (88107b7):
guest_filesystempassed 5 of 5 runs (0.5 s each).ctest --test-dir build-dev/core/libs/prx/libkernel -j 4: 121/121 passed.ctest --test-dir build-dev/core/libs/prx/libc -j 4: 82/83 passed.log_macrosfails the same way on main.tools/check_test_registration.py: 749 test executables, none new.Not rerun this round: the other users of the resolver (AppContent, AvPlayer, Font, LibcInternal, Ngs2, PlayGo, SaveData). Relative to the previous revision, this one does not change libc. The previous review ran them, and they passed.
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