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

Closed
ivica00 wants to merge 1 commit from upstream-pr/4691 into main
ivica00 commented 2026-10-12 19:07:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ivica00 on 2026-10-12T19:07:03Z.


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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4833846857 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4691). Originally posted by [ivica00](https://github.com/ivica00) on 2026-10-12T19:07:03Z. --- ### 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
adriadam10 commented 2026-10-13 14:59:04 +00:00 (Migrated from github.com)

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5505587991 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4691#pullrequestreview-5505587991). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T14:59:04Z. **Review:** CHANGES_REQUESTED --- 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.
ivica00 commented 2026-10-13 17:28:52 +00:00 (Migrated from github.com)

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:

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

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

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6151321752 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4691#issuecomment-6151321752). Originally posted by [ivica00](https://github.com/ivica00) on 2026-10-13T17:28:52Z. --- 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 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). 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 #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).
oneandonlydean commented 2026-10-14 02:38:39 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6159654280 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4691#issuecomment-6159654280). Originally posted by [oneandonlydean](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!4691
No description provided.