Skip to content

fix(libkernel_sys): reach paths past MAX_PATH in mkdirat, unlinkat, renameat and readlink - #24130

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
XFlawless1:fix/libkernel-sys-at-long-paths
Nov 20, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
XFlawless1:fix/libkernel-sys-at-long-paths

Conversation

@XFlawless1

Copy link
Copy Markdown
Contributor

What

On Windows, mkdirat, unlinkat and renameat on a directory descriptor, and readlink/readlinkat, fail with ENOENT when the host path is MAX_PATH or longer. They join the descriptor's path (from GetDirectoryDescriptorPath_nid_no_patch, \\?\ stripped) or the resolved path with the name and pass the plain result to CreateDirectoryW, std::filesystem, _wunlink and the like, which refuse it. A deeply nested directory, or one whose real path through a substituted drive, junction or symlink is that long, hits it. mkdir, open and chmod already add the prefix.

  • HostLongPath returns the path from NativeVerbatimPath_nid_no_patch only when it carries the \\?\ prefix, so short paths are passed on unchanged (not lexically normalized). mkdirat, unlinkat, renameat (source and destination) and ReadNativeLink use it for their host calls. The paths given to RecordWrittenPath_nid_no_patch and the symbolic link limit walk keep the plain form: both work on lexically_normal/component iteration, which libstdc++ does not parse correctly for \\?\C:\ paths.
  • unlinkat(..., AT_REMOVEDIR) checked emptiness with std::filesystem::is_empty. For a directory at a \\?\ path past MAX_PATH, MinGW's libstdc++ returns false with no error for an empty directory, so the call returned ENOTEMPTY. On Windows the check now lists the directory with FindFirstFileW, skipping . and ..; other hosts keep is_empty.
  • GuestKernelSys (Windows) opens a directory whose path exceeds MAX_PATH as a descriptor and checks mkdirat (and EEXIST), readlinkat on a regular file (EINVAL) and a missing name (ENOENT), renameat (and ENOENT), unlinkat of a file (and ENOENT) and unlinkat(AT_REMOVEDIR).

Not changed, because the paths need symbolic links or hard links that this machine cannot create without the privilege, so they could not be tested: linkat, symlinkat, and fchmodat's AT_SYMLINK_NOFOLLOW reparse check. (fchmodat itself already reaches long paths through ChmodNative_nid_no_patch.)

Overlaps, all mechanical: #23786 makes ReadNativeLink use the path the link walk resolved, so HostLongPath would apply to that path instead; #23799 moves the emptiness check inside #ifdef _WIN32, where it becomes DirectoryIsEmpty; #24114 adds a test case at the same place in GuestKernelSys. I will rebase this one onto whichever lands first.

Tested

Windows 11, MinGW-w64 GCC (WinLibs UCRT), Ninja, Release, AMD Ryzen 7 9800X3D.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code)

🤖 Generated with Claude Code

…enameat and readlink

mkdirat, unlinkat and renameat join a directory descriptor's path with
the relative name and pass the plain result to Win32 and std::filesystem,
which refuse a path of MAX_PATH or more; readlink and readlinkat do the
same through ReadNativeLink for any path. In a deeply nested directory, or
one whose real path through a substituted drive, junction or symlink is
that long, they failed with ENOENT. The host calls now use the path from
NativeVerbatimPath_nid_no_patch when it needs the \?\ prefix; the paths
recorded as written and the symbolic link limit walk keep the plain form.

unlinkat with AT_REMOVEDIR checked emptiness with std::filesystem::is_empty,
which reports an empty directory at a \?\ path past MAX_PATH as not empty
with MinGW's libstdc++. On Windows the check now lists the directory with
FindFirstFileW.

GuestKernelSys gains a case that drives these calls through a descriptor
for a directory whose path exceeds MAX_PATH.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against ca9d939541:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_tests built cleanly.
  • CTest (guest_kernel_sys):
    1/1 Test #947: guest_kernel_sys .................   Passed    0.07 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel_sys/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #947: guest_kernel_sys .................   Passed    0.06 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified HostLongPath in core/libs/prx/libkernel_sys/Export.cpp properly guards Windows extended-length paths (starts_with(\\?\)) and safely preserves relative/short paths.
    • Audited DirectoryIsEmpty in core/libs/prx/libkernel_sys/Export.cpp on Windows: uses FindFirstFileW/FindNextFileW, handles '.' and '..' correctly, closes handle, and sets system error on failure.
    • Audited unlinkat_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp: records non-extended path with RecordWrittenPath_nid_no_patch while using HostLongPath for host file operations.
    • Audited renameat_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp: verifies existence and directory checks under HostLongPath and records un-extended source and destination paths.
    • Audited ReadNativeLink in core/libs/prx/libkernel_sys/Export.cpp: ensures SymbolicLinkLimitError evaluates before HostLongPath conversion and attributes/target retrieval use extended path.
    • Checked GuestKernelSys.cpp tests in core/libs/tests/GuestKernelSys.cpp for coverage of mkdirat, readlinkat, renameat, unlinkat under deep directory structures exceeding MAX_PATH.

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — comments

Changes are required before merge: fix the Windows directory emptiness helper so it reports enumeration errors instead of treating them as an empty directory.

What I ran

Reviewed head ca9d939541f43408b84c9c9ceea51b1a5b3e0f65. I read the prepared description, commits, manifest, diff, and the full text of both changed files. I did not run commands or tests; repo_read.py was not available as an executable tool in this review session.

Results

Check Result Note
Diff and changed code passed Both changed files read
Tests not run No execution tool available
Author-reported checks not applicable Reported in the PR description

Findings

Blocking

None.

Non-blocking

  • core/libs/prx/libkernel_sys/Export.cpp:223: If FindNextFileW fails for a reason other than reaching the end of the listing, the loop exits with empty == true. For unlinkat(..., AT_REMOVEDIR), that can allow removal to proceed after an incomplete enumeration. Check GetLastError() after enumeration and propagate errors separately from the empty result.

Not verified

  • The prepared diff and manifest list core/libs/prx/libkernel_sys/Export.cpp and core/libs/tests/GuestKernelSys.cpp; both full files were read. files.json was not available through the reader, so whether additional files were excluded is not verified.
  • Tests and builds were not run. The Windows behavior and the author’s reported test results were not independently verified.
  • WindowsVerbatimPath.hpp was read around the implementation used by this change; its full file was not read.

To get this to a good state

Nothing blocks this; the points below are optional.

  • Handle FindNextFileW errors separately from normal end of enumeration so an incomplete listing cannot be treated as empty.

… fails

LongDirectoryAtCalls aborts before its cleanup when a check fails,
leaving a tree deeper than MAX_PATH that Explorer and a plain rmdir
cannot remove. Require now removes the entries the case may have
created, deepest first, through their \?\ paths before aborting.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d1c4f17ab6:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_tests built cleanly.
  • CTest (guest_kernel_sys):
    1/1 Test #947: guest_kernel_sys .................   Passed    0.76 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel_sys/Export.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #947: guest_kernel_sys .................   Passed    0.17 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified HostLongPath in core/libs/prx/libkernel_sys/Export.cpp: checks verbatim.native().starts_with(R"(\?)") and safely falls back to native on error or short paths without unintended lexical mutation
    • Audited DirectoryIsEmpty in core/libs/prx/libkernel_sys/Export.cpp on Windows: correctly opens directory search with \*, loops through entries skipping '.' and '..', closes find handle, and sets std::system_category error on FindFirstFileW failure
    • Audited error propagation in DirectoryIsEmpty: on enumeration errors where FindNextFileW fails, GetLastError() != ERROR_NO_MORE_FILES leaves error code or triggers removal failure in caller, while empty flag tracks non-dot entries
    • Audited unlinkat_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp: properly splits long path normalization (HostLongPath) from tracking in RecordWrittenPath_nid_no_patch using unmodified un-extended recorded path
    • Audited renameat_nid_postfix in core/libs/prx/libkernel_sys/Export.cpp: wraps source and destination in HostLongPath for host checks/RenamePath while recording un-extended paths
    • Audited ReadNativeLink in core/libs/prx/libkernel_sys/Export.cpp: performs SymbolicLinkLimitError checks before HostLongPath conversion and retrieves reparse point targets using extended path
    • Audited GuestKernelSys.cpp tests: tests verify mkdirat, readlinkat, renameat, and unlinkat on Windows under paths exceeding MAX_PATH with proper cleanup on failure and success

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed — comments

Nothing blocks this; the points below are optional.

What I ran

Reviewed head d1c4f17ab6019a7a671033ec510a7f49b9be745d. No commands or tests were run. I read description.md, commits.md, manifest.json, diff.patch, core/libs/prx/libkernel_sys/Export.cpp, core/libs/prx/libc/include/WindowsVerbatimPath.hpp, core/libs/prx/libc/src/Rename.cpp, and core/libs/tests/GuestKernelSys.cpp. Both changed files listed in the manifest were included and read. No files were excluded by the prepared manifest.

Results

Check Result Note
Code review passed Changed paths inspected
Tests not run Author reports Windows tests; not reproduced
Negative control not run No local test execution

Findings

Blocking

None.

Non-blocking

  • The Windows DirectoryIsEmpty helper returns false for both a non-empty directory and a FindNextFileW enumeration error. Callers then report ENOTEMPTY even when enumeration failed for another reason. Consider checking GetLastError() after enumeration ends and returning that error through statusError. See core/libs/prx/libkernel_sys/Export.cpp:223.

Not verified

Windows execution, including the reported GuestKernelSys and full ctest runs, was not reproduced. linkat, symlinkat, and fchmodat reparse behavior were not changed or verified in this review. The description's comparison with overlapping PRs was not checked.

To get this to a good state

Nothing is required. The enumeration error distinction above is optional.

@Kal0s-design

Copy link
Copy Markdown

Windows run with a negative control, since the change is Windows only and the checks so far are on Linux. Windows 11, WinLibs GCC 15 Release, head d1c4f17ab6 merged onto main 9eabda150f (clean merge), guest_kernel_sys_tests run directly 10 times per tree.

tree passes directories left behind
PR tree with main's libkernel_sys/Export.cpp 0 of 10 none
main + this PR 10 of 10 none

The failing runs stop at Kernel sys check failed at line 1553 (guest errno 2), the ENOENT the description gives for a path at or past MAX_PATH. So the new long-path case fails with the old host calls and passes with HostLongPath on a second Windows machine, and the cleanup on failure works here as well.

Not covered: a substituted drive, a junction or a symlink as the route to the long path, the readlink and readlinkat side beyond what this one test does, and Linux or macOS.

AI-assisted: yes

@github-actions

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 23786 core/libs/prx/libkernel_sys/Export.cpp L280-281
PR 23799 core/libs/prx/libkernel_sys/Export.cpp L1181-1182
core/libs/tests/GuestKernelSys.cpp L1527, L1566

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 9389 core/libs/prx/libkernel_sys/Export.cpp
PR 15037 core/libs/prx/libkernel_sys/Export.cpp
core/libs/tests/GuestKernelSys.cpp

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head d1c4f17ab6, applied with git apply on main 86d2ba2 together with the other eight PRs of one batch (#23921, #23955, #24031, #24070, #24072, #24073, #24077, #24081). All nine applied without conflicts and the whole tree built.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU runs: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon".

tests RTX 3080
guest_kernel_sys (with the new Windows-only LongDirectoryAtCalls: five 60-character levels, path over MAX_PATH), plus the 63 other guest_kernel_sys_* tests all pass

Whole batch: the full ctest -j 12 (6496 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main 86d2ba2 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2099 agc_/video tests on the iGPU ran twice. The second run fails only agc_driver_stencil_plane_memory and agc_driver_store_over_untracked, both known on main. The first run also failed agc_driver_image_table_packed_guards once; it passes 12 of 12 alone on both GPUs and did not recur.

The long-path test runs on this Windows machine and passes.
This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@XFlawless1

Copy link
Copy Markdown
Contributor Author

Checked the two uncompared PRs by hand: #9389 changes Export.cpp around setsid and #15037 around swapon. Its GuestKernelSys.cpp additions are after the kldunload checks in main(). Neither touches the *at calls, readlink or the create_directory(root) spot. #23786 and #23799 are covered in the description.

@oneandonlydean
oneandonlydean merged commit 038cdd1 into boykopovar:main Nov 20, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants