Skip to content

fix(kernel): preserve errno when lstat rejects a Windows symbolic link - #4691

Closed
ivica00 wants to merge 1 commit into
boykopovar:mainfrom
ivica00:fix/lstat-windows-symlink-errno
Closed

ivica00 wants to merge 1 commit into
boykopovar:mainfrom
ivica00:fix/lstat-windows-symlink-errno

Conversation

@ivica00

@ivica00 ivica00 commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

What

lstat of 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:

  • Rebased cleanly onto upstream main (8cf6a0bc3).
  • Built libkernel and run guest_filesystem test:
    • Without this fix: fails at line 513 on checkUnsupportedLink(danglingLink) with Filesystem check failed at line 513 (guest errno 0, Win32 error 18) because directory iteration during ResolveHostPath resets errno to 0, and std::filesystem::remove returns 0 for file symbolic links.
    • With this fix: guest_filesystem passes (100% pass, 0.11 s), checking file, directory and dangling symbolic links, preserving errno 123, and cleaning up test links via DeleteFileW / RemoveDirectoryW.
  • Conventions checked with python tools/check_conventions.py --base upstream/main (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

@ivica00
ivica00 force-pushed the fix/lstat-windows-symlink-errno branch from 6938390 to 0646cbc Compare October 12, 2026 21:38

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR. Points to address:

  • Stdio.cpp:785-798: the throw is NotImplemented_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 in ResolvePathError_nid_no_patch or DoLstat. If nothing observes it, a try/catch around the whole function is extra code without a behaviour change.
  • No other NotImplemented path in libkernel restores errno; if this is wanted it should be a shared rule, not one function.
  • GuestFilesystem.cpp:587: replacing std::filesystem::remove with DeleteFileW/RemoveDirectoryW is not explained. If remove fails on these links, say what it returned and why; otherwise it's a separate topic.
  • The test is Windows-only and CI's windows job is skipped on PRs, so a Linux run can't show it fails without the change. Please link the failing/passing Windows output on the PR head (without #4780 applied).

Linux build and ctest (147 tests incl. guest_filesystem) pass, but they don't exercise this path.

Resolving the path clears errno before the unimplemented symbolic-link path throws, so that failure has to restore the previous errno.
@ivica00
ivica00 force-pushed the fix/lstat-windows-symlink-errno branch from 0646cbc to ff99c5b Compare October 13, 2026 17:28
@ivica00

ivica00 commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review @adriadam10! Here are the details on each point:

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

  2. Who reads it:
    The test guest_filesystem (checkUnsupportedLink) in core/libs/tests/GuestFilesystem.cpp (authored by MarcZX100 in 7f2a882 / 314c372):
    \\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).

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

  4. 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 fix(libkernel): stat a path with one host query #4938).
    Alternatively, if checkUnsupportedLink's invariant should stand (and as PR fix(libkernel): stat a path with one host query #4938 anticipated by planning to keep fix(kernel): preserve errno when lstat rejects a Windows symbolic link #4691's errno save/restore), this PR keeps it.

The branch has been rebased cleanly onto latest upstream/main (8cf6a0b).

@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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants