libkernel: ftruncate succeeds on a pipe, a directory descriptor and /dev/urandom on Windows, where FreeBSD fails with EINVAL; fix branch ready #4921

Closed
opened 2026-10-13 04:16:03 +00:00 by DavidSamir · 1 comment
DavidSamir commented 2026-10-13 04:16:03 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T04:16:03Z.


On Windows, ftruncate and sceKernelFtruncate return 0 on the write end of a pipe, on a directory descriptor and on /dev/urandom, and change nothing. FreeBSD's ftruncate(2) fails with EINVAL when the descriptor "references a socket, not a file", and the Linux build fails with EINVAL on all three. A caller that sizes a file through a descriptor it was handed is told the resize worked.

Cause. NativeFtruncate calls the C runtime's _chsize_s, which reports success on a pipe handle it can write and on the NUL device that stands behind a directory descriptor and /dev/urandom. The read end of a pipe already fails, because that handle cannot be written.

Observed on main 495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release:

sceKernelFtruncate(fd, 0) on Windows Linux build
the write end of a pipe (also with length 100) 0 0x80020016 (EINVAL)
the read end of a pipe 0x80020016 0x80020016
a directory descriptor, with and without O_DIRECTORY 0 0x80020016
/dev/urandom 0 0x80020016
a file opened read-write 0 0
a file opened read-only 0x80020016 0x80020016

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; #4670 listed the pipe case as not changed.

Fix, ready to cherry-pick. DavidSamir:fix/libkernel-ftruncate-not-a-file-windows (971fb51ef, one commit on main 495bf91a0, two files, +18/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one).

Windows only. NativeFtruncate asks for the handle's type before the call and returns EINVAL when it is not a disk file; a disk file goes to _chsize_s as today, and a closed descriptor still gets _chsize_s's EBADF. Shared-memory descriptors return before that point as they do now. truncate by path goes through the same function on a file it has just opened, so it is unchanged apart from the one GetFileType, which #4670 measured at about 0.1 µs on this machine.

Does not depend on #4912 (reads on a directory descriptor) and does not touch its lines.

guest_filesystem gains checks: on both ends of a pipe holding abc, sceKernelFtruncate with length 0 and 8 must return 0x80020016 and the three bytes must then be read back; on a directory descriptor and on /dev/urandom it must return 0x80020016.

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

  • With Stdio.cpp as on main and the checks kept, guest_filesystem fails at the pipe check (Filesystem check failed at line 233). 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, guest_shm and guest_kernel_sync pass 3/3.
  • The probe behind the table, 130 lines, with main's libraries, the branch's and on the Linux build: nine lines change against main, every one an ftruncate on a directory, a duplicate of one, a pipe's write end or /dev/urandom, and after it no ftruncate line differs from the Linux build. The files (lengths 0, 100 and -1, read-write and read-only), the pipe's read end and a closed descriptor give the same results with both sets of libraries.
  • Probes of 122 lines over the descriptor functions and 188 over the file, directory, vector and mapping functions, run with both sets of libraries: two lines differ in the first, ftruncate on a directory and on a pipe, while its ftruncate to 4 and to 20 on a file and its truncate by path are identical; none differ in the second.
  • 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 (#4478); its number of stderr lines varies from run to run with either set of libraries.
  • 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:5824417644 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4921). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T04:16:03Z. --- On Windows, `ftruncate` and `sceKernelFtruncate` return 0 on the write end of a pipe, on a directory descriptor and on `/dev/urandom`, and change nothing. FreeBSD's [ftruncate(2)](https://man.freebsd.org/cgi/man.cgi?query=ftruncate&sektion=2) fails with `EINVAL` when the descriptor "references a socket, not a file", and the Linux build fails with `EINVAL` on all three. A caller that sizes a file through a descriptor it was handed is told the resize worked. **Cause.** [`NativeFtruncate`](https://github.com/boykopovar/AnyPS5/blob/495bf91a0/core/libs/prx/libkernel/File/src/Stdio.cpp#L106-L109) calls the C runtime's `_chsize_s`, which reports success on a pipe handle it can write and on the `NUL` device that stands behind a directory descriptor and `/dev/urandom`. The read end of a pipe already fails, because that handle cannot be written. Observed on `main` 495bf91a0, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release: | `sceKernelFtruncate(fd, 0)` on | Windows | Linux build | |---|---|---| | the write end of a pipe (also with length 100) | 0 | `0x80020016` (EINVAL) | | the read end of a pipe | `0x80020016` | `0x80020016` | | a directory descriptor, with and without `O_DIRECTORY` | 0 | `0x80020016` | | `/dev/urandom` | 0 | `0x80020016` | | a file opened read-write | 0 | 0 | | a file opened read-only | `0x80020016` | `0x80020016` | 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; #4670 listed the pipe case as not changed. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libkernel-ftruncate-not-a-file-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-ftruncate-not-a-file-windows) (971fb51ef, one commit on `main` 495bf91a0, two files, +18/-1). I can't open it as a PR while #4532 is open (one open PR without a merged one). Windows only. `NativeFtruncate` asks for the handle's type before the call and returns `EINVAL` when it is not a disk file; a disk file goes to `_chsize_s` as today, and a closed descriptor still gets `_chsize_s`'s `EBADF`. Shared-memory descriptors return before that point as they do now. `truncate` by path goes through the same function on a file it has just opened, so it is unchanged apart from the one `GetFileType`, which #4670 measured at about 0.1 µs on this machine. Does not depend on #4912 (reads on a directory descriptor) and does not touch its lines. `guest_filesystem` gains checks: on both ends of a pipe holding `abc`, `sceKernelFtruncate` with length 0 and 8 must return `0x80020016` and the three bytes must then be read back; on a directory descriptor and on `/dev/urandom` it must return `0x80020016`. **Checked on Windows** (971fb51ef on `main` 495bf91a0, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Stdio.cpp` as on `main` and the checks kept, `guest_filesystem` fails at the pipe check (`Filesystem check failed at line 233`). 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`, `guest_shm` and `guest_kernel_sync` pass 3/3. - The probe behind the table, 130 lines, with `main`'s libraries, the branch's and on the Linux build: nine lines change against `main`, every one an `ftruncate` on a directory, a duplicate of one, a pipe's write end or `/dev/urandom`, and after it no `ftruncate` line differs from the Linux build. The files (lengths 0, 100 and -1, read-write and read-only), the pipe's read end and a closed descriptor give the same results with both sets of libraries. - Probes of 122 lines over the descriptor functions and 188 over the file, directory, vector and mapping functions, run with both sets of libraries: two lines differ in the first, `ftruncate` on a directory and on a pipe, while its `ftruncate` to 4 and to 20 on a file and its `truncate` by path are identical; none differ in the second. - 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 (#4478); its number of stderr lines varies from run to run with either set of libraries. - 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
MarcZX10 commented 2026-10-13 04:23:04 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-13T04:23:04Z.


I carried your 971fb51ef commit into PR #4923, preserving your original commit authorship. On current main (e51428bf2), Linux GCC 12.2 Release builds guest_filesystem_tests; CTest guest_filesystem passes (1/1), and conventions, test registration and diff checks pass. Windows runtime evidence remains the report in this issue; I did not rerun it locally. The change also merges cleanly with #4917's separate Stdio.cpp changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6138181741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4921#issuecomment-6138181741). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-13T04:23:04Z. --- I carried your `971fb51ef` commit into [PR #4923](https://github.com/boykopovar/AnyPS5/pull/4923), preserving your original commit authorship. On current `main` (`e51428bf2`), Linux GCC 12.2 Release builds `guest_filesystem_tests`; CTest `guest_filesystem` passes (1/1), and conventions, test registration and diff checks pass. Windows runtime evidence remains the report in this issue; I did not rerun it locally. The change also merges cleanly with #4917's separate `Stdio.cpp` changes.
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#4921
No description provided.