Skip to content

fix(libkernel): stat a path with one host query - #4938

Closed
fdfawef324 wants to merge 1 commit into
boykopovar:mainfrom
fdfawef324:perf/stat-one-query
Closed

fdfawef324 wants to merge 1 commit into
boykopovar:mainfrom
fdfawef324:perf/stat-one-query

Conversation

@fdfawef324

@fdfawef324 fdfawef324 commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

What

On Windows, sceKernelStat (and stat, which calls it) queried each existing path four times:

  1. std::filesystem::exists in ResolveHostPath (General.cpp). This ran under the working-directory mutex, and also under the alias mutex for an aliased path.
  2. std::filesystem::status in sceKernelStat (Open.cpp).
  3. _wstat64 in DoStat (NativeStat.cpp).
  4. A second CreateFileW in DoStat.

lstat queried it three times. The queries could disagree. sceKernelStat threw FillFileStat: stat failed in these cases:

  • a file removed between status and DoStat;
  • a dangling link;
  • a directory symbolic link to a file, or a file symbolic link to a directory;
  • a file named with a trailing backslash, or with a last component . (file.txt/., file.txt\., fileLink/.).

The change:

  • Path resolution 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, so other callers are unchanged.
    • The new three-argument ResolvePathError_nid_no_patch passes the probe it is given.
  • File::StatGuestPath passes a probe that does the whole stat:
    • one CreateFileW;
    • then GetFileType, GetFileInformationByHandle and GetFileInformationByHandleEx(FileBasicInfo) on that handle.
  • The probe's result is reused when resolution returns the probed path. Only a case-insensitive match or an explicit native path is opened a second time.
  • sceKernelStat and lstat use File::StatGuestPath. sceKernelStat tries the relinked .guest.prx name (File::PathOrRelinkedModule, from fix(libkernel): resolve original .prx names to relinked .guest.prx in stat, open and reachability #4688) only when that query reports ENOENT, so a stat of an existing file stays one query.
  • FillFileStat(path) and fstat of a directory descriptor use the same single-open query. They therefore also throw when a handle query fails, and report ENOENT when the open fails.
  • A GetFileType result other than a disk file, character device or pipe now throws. The previous revision returned EBADF there, which sceKernelStat folded into ENOENT.
  • The working directory is copied under its mutex, and the matching alias under the alias mutex. Neither mutex is held while the file system is queried. chdir still resolves under the lock.
  • On Linux the stat is one stat(2) or lstat(2).

Paths that name a directory

A guest path that ends in / or \, or whose last component is . or .., names a directory, as in FreeBSD lookup():

  • stat and lstat follow a final link for it;
  • both report ENOTDIR when 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/.. into ENOTDIR and missing/.. into ENOENT, 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. stat agreed with sceKernelStat in every row.

path main: sceKernelStat / lstat previous revision this PR, stat and lstat
file.txt/., file.txt\., FILE.TXT/. throws FillFileStat / ENOENT 0100666, size 10 / ENOENT ENOTDIR
fileLink/., FILELINK/. throws FillFileStat / ENOENT 0100666, size 10 / ENOENT ENOTDIR
file.txt/./, file.txt/.//, file.txt/ ENOTDIR / ENOENT ENOTDIR ENOTDIR
file.txt/.., file.txt/../ ENOTDIR ENOTDIR ENOTDIR
missing/., missing/.., missing/./ ENOENT ENOENT ENOENT
Folder/., Folder/./, FOLDER/., Folder/.. the directory the directory the directory
dirLink/. the directory / throws "lstat of a Windows symbolic link not implemented" the directory / throws the directory
DIRLINK/./ the directory / throws the directory the directory

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. lstat of file.txt/ reported ENOENT on main. It now reports ENOTDIR, as FreeBSD does.

Mode and device fields

st_mode, st_rdev and the device and pipe fields keep the rules of the UCRT _wstat64 (Windows SDK 10.0.26100 ucrt/filesystem/stat.cpp):

  • read-only clears the write bits;
  • a name ending in .exe, .cmd, .bat or .com adds the execute bits;
  • the owner bits are copied to group and other;
  • st_rdev is the drive number;
  • character devices and pipes report S_IFCHR or S_IFIFO, with st_dev and st_rdev 0xffffffff.

Times, size, nlink, dev and ino come from the handle, as before. On Windows, st_blksize is the constant 512 and st_blocks is computed from the size, as before.

Links, and where this differs from _wstat64

Links 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) against sceKernelStat, stat and lstat, on main and on this branch. Each case ran with no suffix, with / and with \.

path _wstat64 main this PR
file symlink to a 10-byte file 0100666, size 0 0100666, size 10 0100666, size 10
file symlink to a read-only file 0100666, size 0 0100666 0100444
read-only file symlink to a writable file 0100444, size 0 0100444 0100666
read-only directory symlink or junction to a writable directory 040555 040555 040777
link.exe to file.txt, plainLink to tool.exe 0100777, 0100666 same same
dangling file symlink, directory symlink or junction the link: 0100666, 040777 throws FillFileStat ENOENT
directory symlink to a file (Windows error 267) 040777 throws FillFileStat ENOENT
file symlink to a directory (Windows error 5) 0100666 throws FillFileStat ENOENT
fileLink/ ENOENT ENOTDIR ENOTDIR
fileLink\, file.txt\ ENOENT throws FillFileStat ENOTDIR
danglingFileLink/, dirAsFileLink/ ENOENT ENOTDIR ENOENT
roDirLink/, roJunction\ 040555 (the link) 040555 040777
Junction/Inner.txt/, dirLink/Inner.txt/ the file, 0100666 ENOTDIR; lstat reports the file ENOTDIR from stat and lstat
lstat("dirLink/"), lstat("Junction\") throws "lstat of a Windows symbolic link not implemented" the directory
lstat("file.txt/"), lstat("fileLink/") ENOENT ENOTDIR

How links are handled:

  • stat follows a final link. It opens a final symbolic link or junction without FILE_FLAG_OPEN_REPARSE_POINT, as FreeBSD stat(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.
  • _wstat64 reports the link itself. It takes the path through GetFileInformationByName (the GetFileInfoByName fast path in stat.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. Its CreateFileW fallback, taken only when that call fails, follows links as this code does.
  • A link that CreateFileW cannot follow reports ENOENT. This covers a dangling link, a directory symbolic link to a file, and a file symbolic link to a directory. _wstat64 reports the link in these cases.
  • lstat of a path that names a directory follows the final link, as described above.
  • lstat of a link without such a suffix still throws NotImplemented. fix(kernel): preserve errno when lstat rejects a Windows symbolic link #4691 changes the errno around that throw.

The other deviations from _wstat64:

  • A drive or UNC root that CreateFileW cannot open reports ENOENT. _wstat64 fabricates a directory dated 1980-01-01.
  • A file named with a trailing separator reports ENOTDIR. _wstat64 reports ENOENT, or the file itself when an earlier component is a link.
  • If GetFileType reports an unexpected type or either handle query fails, the call throws. _wstat64 returns the mapped error.
  • A path that cannot be opened for any reason reports ENOENT. FreeBSD reports the cause: EACCES, ENAMETOOLONG, ELOOP, or ENOTDIR for 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

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_filesystem has three new tests:

  • TestStatFields compares sceKernelStat and stat against GetFileInformationByHandle and FILE_BASIC_INFO (stat(2) on Linux). It checks mode, size, nlink, ino, dev, rdev, blocks, blksize and all four times for:
    • a file, a case-mismatched name, a read-only file and an .EXE;
    • a directory, with and without a trailing /;
    • a junction (a directory symlink on Linux);
    • a read-only junction to a writable directory (040777);
    • a file reached through a junction;
    • symbolic links to a file and to a read-only file (0100444).
  • TestStatFields also checks lstat("Junction/") against the directory, and the junction lstat throw.
  • TestStatFields checks the error code and that the buffer is untouched for:
    • a missing file and a missing parent;
    • a dangling junction, with and without /;
    • the empty path;
    • a file, or a file link, with / or \ (ENOTDIR from all three calls);
    • a directory symbolic link to a file, and a file symbolic link to a directory.
  • TestStatDotComponents starts from the reviewer's reproduction and pins the first table above for sceKernelStat, stat and lstat:
    • file.txt with /., /./, \., /.//, /.. and /../, plus FILE.TXT/., FileLink/. and FILELINK/.: ENOTDIR, with the buffer untouched;
    • Missing with /., /.. and /./: ENOENT;
    • Folder/., Folder/./, FOLDER/., Junction/. and junction/./: the directory's fields, with lstat equal to sceKernelStat of Folder;
    • Folder/..: the parent's fields.
  • Some of these run through a path alias as well: file.txt with /, /., \. and /./, Missing/., Folder/. and Junction/.
  • Symbolic links are created with SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE. If that fails, the test prints that the file-link cases were not tested, as GuestKernelSys does. Here they ran.
  • TestStatDuringRemoval stats a file while another thread creates and removes it 300 times.

Red:

  • With the fix reverted (the previous revision's trailing-separator check restored) and the new test, guest_filesystem fails at the first file.txt/. check in 2 of 2 runs. sceKernelStat returns 0 there.
  • With main's libraries and the new test, TestStatDotComponents aborts on file.txt/. with FillFileStat: stat failed. In the committed order, TestStatFields aborts first, at lstat("Junction/") (lstat of a Windows symbolic link not implemented).
  • Earlier rounds showed the other main-only failures:
    • the read-only junction's st_mode (main reports 040555);
    • lstat("Data.bin/") (ENOENT);
    • Data.bin\ and the dangling junction;
    • the removal race (3 of 3 runs).

Green, after the rebase onto current main (88107b7):

  • guest_filesystem passed 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_macros fails 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

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

@xXPBSXx

xXPBSXx commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Windows test of this PR.

Environment: Windows 10 (19045), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, BUILD_TESTING=ON, -j2, libusb-cmake from a local checkout. Built from main c2565f014 with this PR and ten other open PRs (#4972, #4967, #4966, #4945, #4938, #4936, #4933, #4898, #4896, #4888) merged together; the merges had no conflict.

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).

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>
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 14, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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.

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

Labels

docs libc libkernel needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants