fix(libkernel): report ELOOP from unlink, mkdir, rmdir, truncate on Windows #8687

Merged
cadenasynyster merged 1 commit from upstream-pr/8687 into main 2026-10-23 08:08:53 +00:00
cadenasynyster commented 2026-10-20 21:04:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-20T21:04:07Z.


What

On Windows, sceKernelUnlink, sceKernelMkdir, sceKernelRmdir and sceKernelTruncate (and their POSIX wrappers) do not report ELOOP for a path that goes through a symbolic link that points to itself. FreeBSD returns ELOOP (62). Measured on main with loop -> loop:

Call Windows on main After this PR
unlink("loop/inside") EINVAL (0x80020016) ELOOP
mkdir("loop/inside") ENOTDIR; POSIX errno 20 ELOOP; errno 62
rmdir("loop/inside") ENOTDIR ELOOP
truncate("loop") EINVAL; POSIX errno 22 ELOOP; errno 62
rmdir("loop") ENOTDIR ENOTDIR (unchanged; FreeBSD does not follow the final link)

PathLookupFailure (#6942) only reports a loop when std::filesystem returns an error code for it, which Linux does and Windows does not. sceKernelStat already uses NativeSymbolicLinkLoop for this. That check moves into WindowsSymbolicLink.hpp as File::WindowsSymbolicLinkLoop (Open.cpp now calls it) and the four calls use it on their failure paths only, so successful calls do no extra work. For unlink, mkdir and rmdir the check is on the parent directory, since the final component is not followed. For truncate it is on the path itself, since truncate follows the link.

Refs #6983. Not covered here: open (#8640, #8655) and rename through a loop (#8696). #8217 is about the . and .. components of a rename, which this PR does not touch.

Tested

  • Windows 11 (NTFS, Developer Mode on), AMD Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON, base main c6145997e.
  • guest_filesystem has nine new checks on a stat-loop -> stat-loop link: unlink, mkdir, rmdir of stat-loop\inside (kernel and POSIX calls), truncate of the link (both calls), and rmdir of the link staying ENOTDIR. It passes.
  • A local probe, not part of this PR, ran a Windows port of the Linux-only guest_path_errors and printed every mismatch. The unlink, mkdir, rmdir and truncate rows above were mismatches before and are not after. The 300-character-name rows already passed on main. The 6 mismatches left are the open and rename rows.
  • ctest -R "file|stat|open|path|dir|link|trunc|mkdir|unlink|libc|errno": 120 tests, 108 passed. Of the 12 others, guest_libc_threads and guest_directory_entries were stale or unbuilt executables and pass after rebuilding. The other 10 are agc_* tests that showed exit 0xc0000139 (stale executables) because a full build stops at an unrelated AGC test target that does not compile with this MinGW (ImageArrayWindowPastLayerLimit.cpp:33: requested alignment 65536 exceeds the object file maximum of 8192).
  • Later full build: on a scratch branch of main plus #8687 and #8696, with the one-file build fix of #8310 applied locally (not part of this PR), the full Windows build completes and ctest -R "^agc_" runs 882 tests on an RTX 5070 Ti (driver 617.42): 876 pass, 3 are skipped and 3 fail (agc_driver_sample_order, agc_driver_sample_order_grid_reference, agc_driver_sample_order_recipe_depth_locations). The same 3 fail the same way with this PR's production files reverted to main and the libraries rebuilt, so they are not caused by this change.
  • python tools\check_conventions.py --base origin/main passes.
  • Not run: Linux and macOS (the change is under _WIN32), rename, open, and merging this together with #8640, #8655 or #8696 (the hunks are in different functions).

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
  • Fixes: none (refs #6983)
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4942473456 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8687). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-20T21:04:07Z. --- ### What On Windows, `sceKernelUnlink`, `sceKernelMkdir`, `sceKernelRmdir` and `sceKernelTruncate` (and their POSIX wrappers) do not report ELOOP for a path that goes through a symbolic link that points to itself. FreeBSD returns ELOOP (62). Measured on main with `loop -> loop`: | Call | Windows on main | After this PR | |---|---|---| | `unlink("loop/inside")` | EINVAL (`0x80020016`) | ELOOP | | `mkdir("loop/inside")` | ENOTDIR; POSIX errno 20 | ELOOP; errno 62 | | `rmdir("loop/inside")` | ENOTDIR | ELOOP | | `truncate("loop")` | EINVAL; POSIX errno 22 | ELOOP; errno 62 | | `rmdir("loop")` | ENOTDIR | ENOTDIR (unchanged; FreeBSD does not follow the final link) | `PathLookupFailure` (#6942) only reports a loop when `std::filesystem` returns an error code for it, which Linux does and Windows does not. `sceKernelStat` already uses `NativeSymbolicLinkLoop` for this. That check moves into `WindowsSymbolicLink.hpp` as `File::WindowsSymbolicLinkLoop` (`Open.cpp` now calls it) and the four calls use it on their failure paths only, so successful calls do no extra work. For `unlink`, `mkdir` and `rmdir` the check is on the parent directory, since the final component is not followed. For `truncate` it is on the path itself, since `truncate` follows the link. Refs #6983. Not covered here: `open` (#8640, #8655) and `rename` through a loop (#8696). #8217 is about the `.` and `..` components of a rename, which this PR does not touch. ### Tested - Windows 11 (NTFS, Developer Mode on), AMD Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7 UCRT, Release, BUILD_TESTING=ON, base main `c6145997e`. - `guest_filesystem` has nine new checks on a `stat-loop -> stat-loop` link: `unlink`, `mkdir`, `rmdir` of `stat-loop\inside` (kernel and POSIX calls), `truncate` of the link (both calls), and `rmdir` of the link staying ENOTDIR. It passes. - A local probe, not part of this PR, ran a Windows port of the Linux-only `guest_path_errors` and printed every mismatch. The `unlink`, `mkdir`, `rmdir` and `truncate` rows above were mismatches before and are not after. The 300-character-name rows already passed on main. The 6 mismatches left are the `open` and `rename` rows. - `ctest -R "file|stat|open|path|dir|link|trunc|mkdir|unlink|libc|errno"`: 120 tests, 108 passed. Of the 12 others, `guest_libc_threads` and `guest_directory_entries` were stale or unbuilt executables and pass after rebuilding. The other 10 are `agc_*` tests that showed exit `0xc0000139` (stale executables) because a full build stops at an unrelated AGC test target that does not compile with this MinGW (`ImageArrayWindowPastLayerLimit.cpp:33`: requested alignment 65536 exceeds the object file maximum of 8192). - Later full build: on a scratch branch of main plus #8687 and #8696, with the one-file build fix of #8310 applied locally (not part of this PR), the full Windows build completes and `ctest -R "^agc_"` runs 882 tests on an RTX 5070 Ti (driver 617.42): 876 pass, 3 are skipped and 3 fail (`agc_driver_sample_order`, `agc_driver_sample_order_grid_reference`, `agc_driver_sample_order_recipe_depth_locations`). The same 3 fail the same way with this PR's production files reverted to main and the libraries rebuilt, so they are not caused by this change. - `python tools\check_conventions.py --base origin/main` passes. - Not run: Linux and macOS (the change is under `_WIN32`), `rename`, `open`, and merging this together with #8640, #8655 or #8696 (the hunks are in different functions). ### 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] Fixes: none (refs #6983) - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
DavidSamir commented 2026-10-22 03:14:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T03:14:06Z.


A Windows measurement that backs this PR up. guest_path_errors (core/libs/tests/GuestPathErrors.cpp) is registered only under if(UNIX) on main (libkernel/CMakeLists.txt:332), so it never runs on Windows. I built it on Windows to see what it would report.

Setup: main 6ef5a1c4c, WinLibs GCC 15.2, Windows 11. The test was moved out of the guard on a scratch branch. Its three ::symlink calls were replaced with CreateSymbolicLinkW (unprivileged create). Require was made non-fatal, so every failing check is listed.

Result: every open/stat/lstat ELOOP and ENAMETOOLONG check passes. Exactly these checks fail (main line numbers):

line check
79 sceKernelUnlink("loop/inside") == SceEloop
101 sceKernelMkdir("loop/inside", 0755) == SceEloop
103 mkdir_nid_postfix("loop/inside") sets GuestEloop
104 sceKernelRmdir("loop/inside") == SceEloop
115 sceKernelRename("loop/inside", "target.txt") == SceEloop
117 sceKernelRename("plain.txt", "loop/inside") == SceEloop
124 sceKernelTruncate_nid_postfix("loop", 0) == SceEloop
126 truncate_nid_postfix("loop", 0) sets GuestEloop

Lines 79, 101-104 and 124-126 are the unlink/mkdir/rmdir/truncate paths this PR fixes. 115 and 117 are the rename path that #8696 fixes. With both merged, the existing test could run on Windows as the proof for both. The port it needs is CreateSymbolicLinkW plus the usual skip when the account lacks symlink rights. I haven't run this PR's branch; these results are from main only.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6331779052 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8687#issuecomment-6331779052). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T03:14:06Z. --- A Windows measurement that backs this PR up. `guest_path_errors` (core/libs/tests/GuestPathErrors.cpp) is registered only under `if(UNIX)` on main (libkernel/CMakeLists.txt:332), so it never runs on Windows. I built it on Windows to see what it would report. Setup: main 6ef5a1c4c, WinLibs GCC 15.2, Windows 11. The test was moved out of the guard on a scratch branch. Its three `::symlink` calls were replaced with `CreateSymbolicLinkW` (unprivileged create). `Require` was made non-fatal, so every failing check is listed. Result: every `open`/`stat`/`lstat` ELOOP and ENAMETOOLONG check passes. Exactly these checks fail (main line numbers): | line | check | |---|---| | 79 | `sceKernelUnlink("loop/inside") == SceEloop` | | 101 | `sceKernelMkdir("loop/inside", 0755) == SceEloop` | | 103 | `mkdir_nid_postfix("loop/inside")` sets `GuestEloop` | | 104 | `sceKernelRmdir("loop/inside") == SceEloop` | | 115 | `sceKernelRename("loop/inside", "target.txt") == SceEloop` | | 117 | `sceKernelRename("plain.txt", "loop/inside") == SceEloop` | | 124 | `sceKernelTruncate_nid_postfix("loop", 0) == SceEloop` | | 126 | `truncate_nid_postfix("loop", 0)` sets `GuestEloop` | Lines 79, 101-104 and 124-126 are the unlink/mkdir/rmdir/truncate paths this PR fixes. 115 and 117 are the rename path that #8696 fixes. With both merged, the existing test could run on Windows as the proof for both. The port it needs is `CreateSymbolicLinkW` plus the usual skip when the account lacks symlink rights. I haven't run this PR's branch; these results are from main only. AI-assisted: yes
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!8687
No description provided.