libkernel: lseek on a pipe succeeds on Windows and reports a position and a size, where FreeBSD fails with ESPIPE; fix branch ready #4670

Closed
opened 2026-10-12 18:24:30 +00:00 by DavidSamir · 1 comment
DavidSamir commented 2026-10-12 18:24:30 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T18:24:30Z.


On Windows, lseek and sceKernelLseek succeed on a pipe and return a position, and with SEEK_END the number of bytes waiting in it. FreeBSD's lseek(2) fails with ESPIPE when the descriptor "is associated with a pipe, socket, or FIFO", and the Linux build does that. Code that asks lseek whether a descriptor is a file or a stream, or that sizes its input with SEEK_END, therefore treats a pipe as a file on Windows.

Cause. NativeLseek calls _lseeki64, and Windows accepts a file position on a pipe handle. #3151 already returns ESPIPE for positioned I/O on a socket and pread returns it for a pipe; lseek on a pipe was left.

Observed on main 2a5eca11c, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a pipe from pipe() holding the 11 bytes hello world:

call Windows FreeBSD, Linux build
sceKernelLseek(read end, 0, SEEK_SET) 0 0x8002001D (ESPIPE)
lseek(read end, 5, SEEK_SET) 5 -1, errno 29
lseek(read end, 5, SEEK_CUR) 10 -1, errno 29
sceKernelLseek(read end, 0, SEEK_END) 11 0x8002001D
sceKernelLseek(write end, 0, SEEK_CUR) 5 0x8002001D
sceKernelLseek(dup of the read end, 0, SEEK_CUR) 16 0x8002001D

The data in the pipe is not affected by these calls; the next read returns hello. 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-lseek-pipe-windows (32f49a97a, one commit on main 2a5eca11c, two files, +20/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one).

Windows NativeLseek makes the same _lseeki64 call as before, so a closed or invalid descriptor fails exactly as it did, and then returns ESPIPE when the handle's type is FILE_TYPE_PIPE. Shared-memory and directory descriptors return before that point as they do now. Files, /dev/urandom and character devices are unchanged.

Cost: the GetFileType call adds about 0.1 µs to each seek on this machine. One million sceKernelLseek calls on a file take 159 to 171 ns each with main's libraries and 264 to 282 ns with the branch's (nine passes each, in three runs). Keeping a table of pipe descriptors instead would avoid that, at the price of keeping it right across dup, dup2, fcntl and every way a descriptor is closed; I chose the single check. Say if you want the table.

Not changed: fseek and ftell on a stream over a pipe (they do not go through this function), ftruncate on a pipe, which returns 0 where the Linux build returns EINVAL, and lseek on a console.

guest_filesystem gains checks: on both ends of a pipe, lseek fails with errno 29 and sceKernelLseek returns 0x8002001D for SEEK_SET, SEEK_CUR and SEEK_END; the bytes written before are then read back unchanged; after close, lseek fails with errno 9.

Checked on Windows (32f49a97a on main 2a5eca11c, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release):

  • With Open.cpp as on main and the new checks kept, guest_filesystem fails at the first of them (Filesystem check failed at line 222). With the fix it passes 5/5 (--repeat until-fail:5).
  • The probe behind the table, with main's libraries and the branch's: every pipe line changes to ESPIPE; a closed descriptor (EBADF), /dev/urandom, a file with SEEK_SET and SEEK_END, and descriptors 0 and 1 give the same results in both.
  • A second probe of 97 file, directory and pipe operations, run with both sets of libraries: one line differs, lseek on a pipe.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510) and ProsperoTV (PPSA99003), which create pipes, and PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end the same way in both, with the same number of stderr lines. SwanStationPS5 and ProsperoTV exit 0xc0000409 in both without a message (#4631) and PSXS5 stops on poll in both, each after a time that varies from run to run.
  • Full ctest --parallel 4: 973 of 973 pass (7 skipped, no display). guest_standard_streams, file_streams, guest_dup, guest_fiopen, guest_poll, guest_directory_rewind, guest_file_vector and guest_aio pass 3/3.
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch lists #4536 and #4640 for the same two places in GuestFilesystem.cpp (a declaration line and the end of the pipe checks); neither changes lseek.

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:5818515399 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4670). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T18:24:30Z. --- On Windows, `lseek` and `sceKernelLseek` succeed on a pipe and return a position, and with `SEEK_END` the number of bytes waiting in it. FreeBSD's [lseek(2)](https://man.freebsd.org/cgi/man.cgi?query=lseek&sektion=2) fails with `ESPIPE` when the descriptor "is associated with a pipe, socket, or FIFO", and the Linux build does that. Code that asks `lseek` whether a descriptor is a file or a stream, or that sizes its input with `SEEK_END`, therefore treats a pipe as a file on Windows. **Cause.** [`NativeLseek`](https://github.com/boykopovar/AnyPS5/blob/2a5eca11c/core/libs/prx/libkernel/File/src/Open.cpp#L44-L47) calls `_lseeki64`, and Windows accepts a file position on a pipe handle. #3151 already returns `ESPIPE` for positioned I/O on a socket and `pread` returns it for a pipe; `lseek` on a pipe was left. Observed on `main` 2a5eca11c, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release, on a pipe from `pipe()` holding the 11 bytes `hello world`: | call | Windows | FreeBSD, Linux build | |---|---|---| | `sceKernelLseek(read end, 0, SEEK_SET)` | 0 | `0x8002001D` (ESPIPE) | | `lseek(read end, 5, SEEK_SET)` | 5 | -1, errno 29 | | `lseek(read end, 5, SEEK_CUR)` | 10 | -1, errno 29 | | `sceKernelLseek(read end, 0, SEEK_END)` | 11 | `0x8002001D` | | `sceKernelLseek(write end, 0, SEEK_CUR)` | 5 | `0x8002001D` | | `sceKernelLseek(dup of the read end, 0, SEEK_CUR)` | 16 | `0x8002001D` | The data in the pipe is not affected by these calls; the next `read` returns `hello`. 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-lseek-pipe-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-lseek-pipe-windows) (32f49a97a, one commit on `main` 2a5eca11c, two files, +20/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one). Windows `NativeLseek` makes the same `_lseeki64` call as before, so a closed or invalid descriptor fails exactly as it did, and then returns `ESPIPE` when the handle's type is `FILE_TYPE_PIPE`. Shared-memory and directory descriptors return before that point as they do now. Files, `/dev/urandom` and character devices are unchanged. Cost: the `GetFileType` call adds about 0.1 µs to each seek on this machine. One million `sceKernelLseek` calls on a file take 159 to 171 ns each with `main`'s libraries and 264 to 282 ns with the branch's (nine passes each, in three runs). Keeping a table of pipe descriptors instead would avoid that, at the price of keeping it right across `dup`, `dup2`, `fcntl` and every way a descriptor is closed; I chose the single check. Say if you want the table. Not changed: `fseek` and `ftell` on a stream over a pipe (they do not go through this function), `ftruncate` on a pipe, which returns 0 where the Linux build returns `EINVAL`, and `lseek` on a console. `guest_filesystem` gains checks: on both ends of a pipe, `lseek` fails with errno 29 and `sceKernelLseek` returns `0x8002001D` for `SEEK_SET`, `SEEK_CUR` and `SEEK_END`; the bytes written before are then read back unchanged; after `close`, `lseek` fails with errno 9. **Checked on Windows** (32f49a97a on `main` 2a5eca11c, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release): - With `Open.cpp` as on `main` and the new checks kept, `guest_filesystem` fails at the first of them (`Filesystem check failed at line 222`). With the fix it passes 5/5 (`--repeat until-fail:5`). - The probe behind the table, with `main`'s libraries and the branch's: every pipe line changes to `ESPIPE`; a closed descriptor (`EBADF`), `/dev/urandom`, a file with `SEEK_SET` and `SEEK_END`, and descriptors 0 and 1 give the same results in both. - A second probe of 97 file, directory and pipe operations, run with both sets of libraries: one line differs, `lseek` on a pipe. - Titles from the homebrew catalog, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510) and ProsperoTV (PPSA99003), which create pipes, and PSXS5 (PPSA97510), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000) and OpenNOW (PPSA99082) end the same way in both, with the same number of stderr lines. SwanStationPS5 and ProsperoTV exit 0xc0000409 in both without a message (#4631) and PSXS5 stops on `poll` in both, each after a time that varies from run to run. - Full `ctest --parallel 4`: 973 of 973 pass (7 skipped, no display). `guest_standard_streams`, `file_streams`, `guest_dup`, `guest_fiopen`, `guest_poll`, `guest_directory_rewind`, `guest_file_vector` and `guest_aio` pass 3/3. - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` lists #4536 and #4640 for the same two places in `GuestFilesystem.cpp` (a declaration line and the end of the pipe checks); neither changes `lseek`. **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
milescoviello commented 2026-10-12 18:27:08 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T18:27:08Z.


Carried in #4672, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 306 guest_* tests pass, and the new pipe checks also pass with main's Open.cpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6130172902 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4670#issuecomment-6130172902). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T18:27:08Z. --- Carried in #4672, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 306 `guest_*` tests pass, and the new pipe checks also pass with `main`'s `Open.cpp`, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch. 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#4670
No description provided.