fix(kernel): preserve errno when lstat rejects a Windows symbolic link #4691
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/4691"
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 ivica00 on 2026-10-12T19:07:03Z.
What
lstatof a Windows symbolic link still throws for the unimplemented path. Path resolution clears guest errno before that throw when resolving dangling links or non-existent paths (due to directory iteration in case-insensitivity resolution). Restore the previous errno when the operation throws so guest errno remains unchanged, and clean up Windows test links properly.Tested
Windows Server 2025 / Windows 10 x64, MinGW-w64 GCC 15.2.0 (UCRT), Release:
main(8cf6a0bc3).guest_filesystemtest:checkUnsupportedLink(danglingLink)withFilesystem check failed at line 513 (guest errno 0, Win32 error 18)because directory iteration duringResolveHostPathresets errno to 0, andstd::filesystem::removereturns 0 for file symbolic links.guest_filesystempasses (100% pass, 0.11 s), checking file, directory and dangling symbolic links, preserving errno 123, and cleaning up test links viaDeleteFileW/RemoveDirectoryW.python tools/check_conventions.py --base upstream/main(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)Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T14:59:04Z.
Review: CHANGES_REQUESTED
Thanks for the PR. Points to address:
Stdio.cpp:785-798: the throw isNotImplemented_nid_no_patch(runtime_error), which ends the guest, so nothing observes errno afterwards. Please say who reads it (a catch in the host, a title) and where errno is cleared before the throw; I couldn't find a clear inResolvePathError_nid_no_patchorDoLstat. If nothing observes it, a try/catch around the whole function is extra code without a behaviour change.NotImplementedpath in libkernel restores errno; if this is wanted it should be a shared rule, not one function.GuestFilesystem.cpp:587: replacingstd::filesystem::removewithDeleteFileW/RemoveDirectoryWis not explained. Ifremovefails on these links, say what it returned and why; otherwise it's a separate topic.Linux build and ctest (147 tests incl.
guest_filesystem) pass, but they don't exercise this path.Imported from boykopovar/AnyPS5. Originally posted by ivica00 on 2026-10-13T17:28:52Z.
Thanks for the review @adriadam10! Here are the details on each point:
Where errno is cleared:
In ResolveHostPath(root, relative) in General.cpp:
When resolving dangling-link, std::filesystem::exists(direct, error) returns alse (because exists() follows symlinks and the target does not exist).
It falls through to the case-insensitivity fallback scanning via std::filesystem::directory_iterator.
On Windows with MinGW-w64 UCRT, scanning to the end of the directory encounters ERROR_NO_MORE_FILES (Win32 error 18), and the underlying CRT resets errno to 0.
Since ResolvePath runs in libc.prx, its errno is the guest errno (*__error_nid_postfix()). So when ResolvePathError_nid_no_patch returns to lstat_nid_postfix, guest errno has been cleared from sentinelError (123) to 0.
Who reads it:
The test guest_filesystem (checkUnsupportedLink) in core/libs/tests/GuestFilesystem.cpp (authored by MarcZX100 in
7f2a8826/314c3722):\\cpp
const auto checkUnsupportedLink = [](const std::filesystem::path& link) {
FileStat untouched;
std::memset(&untouched, 0x5a, sizeof(untouched));
constexpr int sentinelError = 123;
*__error_nid_postfix() = sentinelError;
...
const bool errorUnchanged = *__error_nid_postfix() == sentinelError;
return rejected && errorUnchanged && statusUnchanged;
};
\\
On Linux CI, this test path is skipped (#ifdef _WIN32), but running guest_filesystem on Windows on main aborts at line 513:
\Filesystem check failed at line 513 (guest errno 0, Win32 error 18)
because sentinelError (123) became 0.
With this fix, guest_filesystem passes (100%, 0.11 s).
Why \DeleteFileW\ / \RemoveDirectoryW:
On Windows with GCC 15 (MinGW-w64 UCRT), std::filesystem::remove(fileLink) and std::filesystem::remove(danglingLink) return alse (0) for file reparse points created via CreateSymbolicLinkW (without deleting them), causing the subsequent Require(...) to fail. DeleteFileW for file/dangling links and RemoveDirectoryW for directory links properly delete the reparse points.
Next steps / design decision:
If preserving errno across NotImplemented is considered unnecessary for libkernel since guest execution aborts on unimplemented paths, we can instead drop errorUnchanged from checkUnsupportedLink in GuestFilesystem.cpp and leave Stdio.cpp untouched (which also avoids any overlap with #4938).
Alternatively, if checkUnsupportedLink's invariant should stand (and as PR #4938 anticipated by planning to keep #4691's errno save/restore), this PR keeps it.
The branch has been rebased cleanly onto latest upstream/main (
8cf6a0bc3).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T02:38:39Z.
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