Skip to content

fix(libkernel): report EINVAL for getdirentries on a non-directory - #7152

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
Bonjourcestmatt:fix/getdents-not-directory
Oct 18, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
Bonjourcestmatt:fix/getdents-not-directory

Conversation

@Bonjourcestmatt

@Bonjourcestmatt Bonjourcestmatt commented Oct 17, 2026 •

Copy link
Copy Markdown
Contributor

What

getdirentries and getdents on a descriptor that is not a directory returned ENOTDIR: from the host getdents64 on Linux, and for every descriptor missing from the directory table on Windows, invalid ones included. FreeBSD 11.4's kern_getdirentries (vfs_syscalls.c) returns EINVAL when the vnode is not a directory, and EBADF for a descriptor that is not open.

  • Linux: the descriptor is checked with fstat before the lseek that reads the base offset. A host error such as EBADF is passed through, and anything that is not a directory (regular file, pipe, FIFO, device) returns EINVAL. The getdents64 error line is unchanged.
  • Windows: a descriptor outside the directory table returns EBADF when _get_osfhandle rejects it and EINVAL otherwise. Every guest descriptor kind (files, sockets, kqueues, /dev/random, shared memory) holds a CRT descriptor, so a valid non-directory descriptor gets EINVAL.

With the fstat check a pipe no longer fails with ESPIPE on Linux, so no difference is left to list in the technical debt. The macOS getdirentries64 path proposed in #6222 is not covered. Not measured on a console.

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0 UCRT, full build with submodules: guest_filesystem, guest_directory_close and guest_directory_buffer pass (the new checks: sceKernelGetdents, getdents and getdirentries on a regular file return 0x80020016 / -1 with errno 22; the two Windows directory tests now expect EINVAL and EBADF), and the non-GPU suite passes 733/733.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • Not run on Linux locally: the Linux path (the fstat check) was not compiled here. The same new checks run there; no test opens a pipe.

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, written with Claude (Anthropic); I am responsible for every line.

🤖 Generated with Claude Code

sceKernelGetdirentries on a descriptor that is not a directory returned
ENOTDIR from the host getdents64 on Linux, and ENOTDIR for every descriptor
missing from the directory table on Windows, including invalid ones.
FreeBSD 11.4's kern_getdirentries returns EINVAL when the vnode is not a
directory.

Return EINVAL for a regular file on both hosts and EBADF for an invalid
descriptor on Windows. Test getdents and getdirentries on a regular file and
list the pipe case, which still fails with ESPIPE on Linux, in the library's
technical debt file.

Written with AI assistance (Claude).

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

Copy link
Copy Markdown
Collaborator

Full CI on 9dc6542 (run 39039630866): Linux passes, but Windows fails 2 existing tests that still expect the old errors. Both abort with 0xc0000409 at a Require:

  • guest_directory_close: core/libs/tests/GuestDirectoryClose.cpp checks that sceKernelGetdents on a plain file returns ENOTDIR. With this change it returns EINVAL.
  • guest_directory_buffer: core/libs/tests/GuestDirectoryBuffer.cpp checks that sceKernelGetdents(-1, ...) returns ENOTDIR. With this change it returns EBADF, which matches FreeBSD for a bad descriptor.

Both pass on main 47a266c. The new errors look right to us (kern_getdirentries gives EINVAL for a non-directory vnode and getvnode gives EBADF for a bad fd), so please update those two expectations to the new values and confirm both tests pass on Windows. The code review is otherwise a pass.

…ests

guest_directory_close expected ENOTDIR from getdents on a descriptor number
reused by a regular file, and guest_directory_buffer expected ENOTDIR for
descriptor -1. kern_getdirentries returns EINVAL for a vnode that is not a
directory and getvnode returns EBADF for a descriptor that is not open, which
is what sceKernelGetdirentries now reports on Windows.

Written with AI assistance (Claude).

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

Copy link
Copy Markdown
Contributor Author

Updated both expectations in b808658: guest_directory_close now expects EINVAL (0x80020016) for the reused descriptor and guest_directory_buffer expects EBADF for descriptor -1.

Windows 11, MinGW-w64 GCC 15.2.0 UCRT, full build with no failures: guest_directory_close, guest_directory_buffer and guest_filesystem pass, and the non-GPU suite (ctest -E "agc|Agc|vulkan|gpu|shader" -j6) passes 733/733. Sorry for missing these two; I had only run guest_filesystem.

🤖 Addressed by Claude Code

…inux

The Linux path mapped ENOTDIR from getdents64 to EINVAL, but a pipe or FIFO
still failed with ESPIPE from the lseek that reads the base offset first.
FreeBSD 11.4's kern_getdirentries returns EINVAL for any descriptor that is
not a directory vnode, pipes included, and EBADF for one that is not open.

Check the descriptor with fstat before the lseek: EBADF and other host
errors are passed through, and anything that is not a directory returns
EINVAL. The getdents64 error line is unchanged again, and the technical debt
entry is removed since no difference is left.

Written with AI assistance (Claude).

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

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

Linux run of the fstat path, since the description notes it was not compiled or run locally (and CI skipped the Linux job on this head).

Machine: Linux x86_64, i7-8700, Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build.
Head d915e5835 merged onto main 5de7a46e0 (clean merge).

  • guest_filesystem passes, so on Linux sceKernelGetdents on a regular file returns 0x80020016 and getdents/getdirentries return -1 with errno 22. The other directory and filesystem tests (ctest -R "filesystem|directory|getdents|dirent", 6 tests) also pass.
  • GuestFilesystem.cpp only, on main: guest_filesystem aborts, so the new checks cover the Linux change too.
  • Non-GPU suite (ctest -E "^agc|gpu|vulkan|video_out"): 697/697, same as main.

guest_directory_close and guest_directory_buffer are only registered on Windows, so the two updated expectations are not exercised here.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

@oneandonlydean oneandonlydean self-assigned this Oct 18, 2026
@oneandonlydean
oneandonlydean merged commit 7cdfbd2 into boykopovar:main Oct 18, 2026
9 checks passed
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
Upstream (boykopovar#7152) made getdents and getdirentries refuse a plain file with EINVAL, and guest_filesystem now checks it. The macOS implementation is separate from Linux's and reached openat(fd, "."), which fails with ENOTDIR and aborted the test. It now checks that the descriptor is a directory first, as the Linux branch does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 18, 2026
Upstream (boykopovar#7152) made getdents and getdirentries refuse a plain file with EINVAL, and guest_filesystem now checks it. The macOS implementation is separate from Linux's and reached openat(fd, "."), which fails with ENOTDIR and aborted the test. It now checks that the descriptor is a directory first, as the Linux branch does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit 47ba2ef)
ycarogabriel-lgtm added a commit to ycarogabriel-lgtm/AnyPS5 that referenced this pull request Oct 25, 2026
Upstream (boykopovar#7152) made getdents and getdirentries refuse a plain file with EINVAL, and guest_filesystem now checks it. The macOS implementation is separate from Linux's and reached openat(fd, "."), which fails with ENOTDIR and aborted the test. It now checks that the descriptor is a directory first, as the Linux branch does.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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