libkernel: read on a directory descriptor returns 0 on Windows where the Linux build fails with EISDIR; fix branch ready #4912

Closed
opened 2026-10-13 03:47:47 +00:00 by DavidSamir · 0 comments
DavidSamir commented 2026-10-13 03:47:47 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T03:47:47Z.


On Windows, read, sceKernelRead, readv and sceKernelReadv on a directory descriptor return 0, the end of a file, and pread/preadv on it fail with ESPIPE. The Linux build fails all of them with EISDIR, the error FreeBSD's read(2) documents for a directory that cannot be read as a file. Code that opens a path and reads it, and that tells a directory from a file by that error, sees an empty file on Windows.

Cause. A directory descriptor on Windows is a descriptor on the NUL device with the directory's state kept beside it (OpenDirectoryDescriptor). getdents, lseek, fstat and fsync look that state up; the read calls do not, so they read NUL.

Observed on main 495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a descriptor from sceKernelOpen(directory, O_RDONLY) (the same with O_DIRECTORY):

call Windows Linux build
sceKernelRead(fd, buffer, 8) 0 0x80020015 (EISDIR)
read(fd, buffer, 8) 0 -1, errno 21
sceKernelReadv, readv 0 0x80020015; -1, errno 21
sceKernelPread, sceKernelPreadv, preadv 0x8002001D (ESPIPE); -1, errno 29 0x80020015; -1, errno 21
sceKernelPwrite(fd, "x", 1, 0) 0x8002001D 0x80020009 (EBADF)

I have not found a title that stops on this. It came out of running one probe of the file functions on the Windows build and on the Linux build and comparing the two outputs.

Fix, ready to cherry-pick. DavidSamir:fix/libkernel-read-directory-descriptor-windows (d7ef271ef, one commit on main 495bf91a0, three files, +23/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).

Windows only, three places, each using the existing File::DirectoryDescriptorPath:

  • NativeRead (read, sceKernelRead): when _read returns 0 and the descriptor is a directory, the result is EISDIR. The lookup is made only on a read that returned nothing, so a read that returns data costs what it costs today.
  • NativePositioned (pread, pwrite, their vector forms and AIO): where a descriptor that is not a disk file already fails with ESPIPE, a directory now fails with EISDIR on a read and EBADF on a write, as on Linux.
  • TransferIovecs (readv): a directory fails with EISDIR before anything is read.

Nothing is called a second time; getdents, lseek, fstat and fsync on the descriptor are not touched.

Not changed: ftruncate on a directory descriptor returns 0 on Windows (a separate fix follows), and read of zero bytes on the write end of a pipe returns 0 where Linux fails with EBADF.

guest_filesystem gains checks on a directory descriptor: read must fail with errno 21, sceKernelRead, sceKernelReadv, sceKernelPread and sceKernelPreadv must return 0x80020015, sceKernelPwrite 0x80020009, and sceKernelGetdents must still list the directory afterwards.

Checked on Windows (d7ef271ef on main 495bf91a0, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With Stdio.cpp and Open.cpp as on main and the checks kept, guest_filesystem fails at the first of them (Filesystem check failed at line 613). With the fix it passes 5/5 (--repeat until-fail:5), and guest_file_vector, guest_poll, guest_dup, guest_standard_streams, file_streams, guest_aio, guest_kernel_fcntl, guest_directory_rewind and guest_fdopendir pass 3/3.
  • A probe of 130 lines that makes about twenty calls on each of a directory, a directory opened with O_DIRECTORY, both ends of a pipe, a file opened read-write and read-only, and /dev/urandom, with main's libraries, the branch's and on the Linux build. Against main only the directory lines change, to the Linux results in the table; getdents before and after a rewind, fstat, the pipe, the files and /dev/urandom are identical. One directory line still differs from Linux apart from ftruncate: a read through a dup of the descriptor gives EISDIR on the branch and EINVAL on Linux.
  • Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 33 over readv and 33 over non-blocking pipes, run with both sets of libraries: two lines differ in the first, read and pread on a directory; none in the others.
  • Timing, 300,000 rounds, three passes in two alternating runs each: seek and readv on a file 4.2 to 4.4 µs with main's libraries and 4.3 to 4.4 µs with the branch's; write three bytes to a pipe and readv them 1.17 to 1.25 µs and 1.18 to 1.21 µs. No difference I can measure.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines. PSXS5 (PPSA97510) stops on poll: POLLPRI is not supported by WSAPoll in both; its number of stderr lines varies from run to run (32 to 46 in four runs with main's libraries, 35 to 36 in four with the branch's).
  • Full ctest --parallel 4: 1023 of 1023 pass (7 skipped, no display).
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch reports no overlap with open pull requests.

Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_filesystem passes 3/3, so the new checks match the existing Linux behaviour. Only that target and libs were built there.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5824167883 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4912). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T03:47:47Z. --- On Windows, `read`, `sceKernelRead`, `readv` and `sceKernelReadv` on a directory descriptor return 0, the end of a file, and `pread`/`preadv` on it fail with `ESPIPE`. The Linux build fails all of them with `EISDIR`, the error FreeBSD's [read(2)](https://man.freebsd.org/cgi/man.cgi?query=read&sektion=2) documents for a directory that cannot be read as a file. Code that opens a path and reads it, and that tells a directory from a file by that error, sees an empty file on Windows. **Cause.** A directory descriptor on Windows is a descriptor on the `NUL` device with the directory's state kept beside it ([`OpenDirectoryDescriptor`](https://github.com/boykopovar/AnyPS5/blob/495bf91a0/core/libs/prx/libkernel/File/src/DirectoryDescriptor.cpp)). `getdents`, `lseek`, `fstat` and `fsync` look that state up; the read calls do not, so they read `NUL`. Observed on `main` 495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a descriptor from `sceKernelOpen(directory, O_RDONLY)` (the same with `O_DIRECTORY`): | call | Windows | Linux build | |---|---|---| | `sceKernelRead(fd, buffer, 8)` | 0 | `0x80020015` (EISDIR) | | `read(fd, buffer, 8)` | 0 | -1, errno 21 | | `sceKernelReadv`, `readv` | 0 | `0x80020015`; -1, errno 21 | | `sceKernelPread`, `sceKernelPreadv`, `preadv` | `0x8002001D` (ESPIPE); -1, errno 29 | `0x80020015`; -1, errno 21 | | `sceKernelPwrite(fd, "x", 1, 0)` | `0x8002001D` | `0x80020009` (EBADF) | I have not found a title that stops on this. It came out of running one probe of the file functions on the Windows build and on the Linux build and comparing the two outputs. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libkernel-read-directory-descriptor-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-read-directory-descriptor-windows) (d7ef271ef, one commit on `main` 495bf91a0, three files, +23/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one). Windows only, three places, each using the existing `File::DirectoryDescriptorPath`: - `NativeRead` (`read`, `sceKernelRead`): when `_read` returns 0 and the descriptor is a directory, the result is `EISDIR`. The lookup is made only on a read that returned nothing, so a read that returns data costs what it costs today. - `NativePositioned` (`pread`, `pwrite`, their vector forms and AIO): where a descriptor that is not a disk file already fails with `ESPIPE`, a directory now fails with `EISDIR` on a read and `EBADF` on a write, as on Linux. - `TransferIovecs` (`readv`): a directory fails with `EISDIR` before anything is read. Nothing is called a second time; `getdents`, `lseek`, `fstat` and `fsync` on the descriptor are not touched. Not changed: `ftruncate` on a directory descriptor returns 0 on Windows (a separate fix follows), and `read` of zero bytes on the write end of a pipe returns 0 where Linux fails with `EBADF`. `guest_filesystem` gains checks on a directory descriptor: `read` must fail with errno 21, `sceKernelRead`, `sceKernelReadv`, `sceKernelPread` and `sceKernelPreadv` must return `0x80020015`, `sceKernelPwrite` `0x80020009`, and `sceKernelGetdents` must still list the directory afterwards. **Checked on Windows** (d7ef271ef on `main` 495bf91a0, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Stdio.cpp` and `Open.cpp` as on `main` and the checks kept, `guest_filesystem` fails at the first of them (`Filesystem check failed at line 613`). With the fix it passes 5/5 (`--repeat until-fail:5`), and `guest_file_vector`, `guest_poll`, `guest_dup`, `guest_standard_streams`, `file_streams`, `guest_aio`, `guest_kernel_fcntl`, `guest_directory_rewind` and `guest_fdopendir` pass 3/3. - A probe of 130 lines that makes about twenty calls on each of a directory, a directory opened with `O_DIRECTORY`, both ends of a pipe, a file opened read-write and read-only, and `/dev/urandom`, with `main`'s libraries, the branch's and on the Linux build. Against `main` only the directory lines change, to the Linux results in the table; `getdents` before and after a rewind, `fstat`, the pipe, the files and `/dev/urandom` are identical. One directory line still differs from Linux apart from `ftruncate`: a read through a `dup` of the descriptor gives `EISDIR` on the branch and `EINVAL` on Linux. - Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 33 over `readv` and 33 over non-blocking pipes, run with both sets of libraries: two lines differ in the first, `read` and `pread` on a directory; none in the others. - Timing, 300,000 rounds, three passes in two alternating runs each: seek and `readv` on a file 4.2 to 4.4 µs with `main`'s libraries and 4.3 to 4.4 µs with the branch's; write three bytes to a pipe and `readv` them 1.17 to 1.25 µs and 1.18 to 1.21 µs. No difference I can measure. - Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end at the same place in both, with the same number of stderr lines. PSXS5 (PPSA97510) stops on `poll: POLLPRI is not supported by WSAPoll` in both; its number of stderr lines varies from run to run (32 to 46 in four runs with `main`'s libraries, 35 to 36 in four with the branch's). - Full `ctest --parallel 4`: 1023 of 1023 pass (7 skipped, no display). - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` reports no overlap with open pull requests. **Checked on Linux** (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `guest_filesystem` passes 3/3, so the new checks match the existing Linux behaviour. Only that target and `libs` were built there. Not checked on a console or with a commercial title. AI-assisted: yes
Sign in to join this conversation.
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#4912
No description provided.