libkernel: open with O_RDONLY|O_TRUNC fails with EINVAL on Windows, where FreeBSD truncates the file; fix branch ready #5095

Closed
opened 2026-10-13 11:27:56 +00:00 by DavidSamir · 2 comments
DavidSamir commented 2026-10-13 11:27:56 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T11:27:56Z.


On Windows, sceKernelOpen(path, O_RDONLY | O_TRUNC) and open with the same flags fail with EINVAL (0x80020016) and leave the file as it was. On FreeBSD the open succeeds, the file is emptied and the descriptor is read-only: vn_open_vnode asks for write permission on the file when O_TRUNC is set, whatever the access mode, and kern_openat then truncates it. The Linux build does the same.

Cause. NativeOpen maps O_TRUNC without O_CREAT to TRUNCATE_EXISTING and O_RDONLY to GENERIC_READ. CreateFileW rejects that pair with ERROR_INVALID_PARAMETER: TRUNCATE_EXISTING needs GENERIC_WRITE.

Observed on main bb9a4d8a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds ten bytes:

main on Windows Linux build
sceKernelOpen(file, O_RDONLY|O_TRUNC) 0x80020016, ten bytes left descriptor, file empty
the same with O_APPEND, O_NONBLOCK or O_EXCL added 0x80020016 descriptor, file empty
open(file, O_RDONLY|O_TRUNC) -1, errno 22 descriptor, file empty
the same on a file with mode 0444 0x80020016 0x8002000D (EACCES), file kept
O_RDONLY|O_CREAT|O_TRUNC descriptor, file empty descriptor, file empty

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-open-rdonly-trunc-windows (c076185f3, three commits on main bb9a4d8a2, three files, +46/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).

  1. fix(libkernel): truncate on open with O_RDONLY and O_TRUNC on Windows: Open.cpp.
  2. test(libkernel): open a file with O_RDONLY and O_TRUNC: guest_filesystem.
  3. docs(debt): describe open with O_RDONLY and O_TRUNC on Windows.

Windows only, in NativeOpen. For O_RDONLY with O_TRUNC and without O_CREAT, the file is opened with GENERIC_READ | GENERIC_WRITE, which lets TRUNCATE_EXISTING through and, like FreeBSD, refuses a read-only file with EACCES. The handle is then narrowed to FILE_GENERIC_READ with DuplicateHandle and DUPLICATE_CLOSE_SOURCE, so the descriptor is read-only: a write fails with EBADF, ftruncate with EINVAL, and F_GETFL reports O_RDONLY. The path is chosen from the flags before the call; nothing is tried twice. Every other combination of flags takes the same calls as before.

One difference is left and is recorded in the debt file: the narrowed handle is the same open file, which Windows still counts as a writer in its sharing checks, so another host process that opens the file without FILE_SHARE_WRITE is refused while the descriptor is open. Opens made by the guest share write access and are not affected.

guest_filesystem gains TestReadOnlyTruncatingOpen: with the file open through a second descriptor, O_RDONLY|O_TRUNC must succeed and empty it (seen through the host and through fstat on the other descriptor); a write through the new descriptor must fail with EBADF and ftruncate with EINVAL; bytes written through the other descriptor must be readable through it; a missing file must give ENOENT and not be created, a directory EISDIR, and a 0444 file EACCES with its contents kept.

Checked on Windows (c076185f3 on main bb9a4d8a2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With Open.cpp as on main and the test kept, guest_filesystem fails (exit 0xc0000409, Filesystem check failed at line 213 (guest errno 22, Win32 error 87)). With the fix it passes 5/5 (--repeat until-fail:5), and guest_kernel_fcntl, guest_truncate, guest_dup, guest_file_vector, guest_file_lock_conversion, guest_file_lock_duplicate, file_streams, guest_standard_streams, guest_posix_filesystem and guest_fdopendir pass 3/3.
  • The probe behind the table, 214 lines with the branch's libraries and on the Linux build. For each of O_RDONLY|O_TRUNC on a file, on an empty file, with O_APPEND, with O_NONBLOCK, with O_EXCL, with O_CREAT on an existing and on a missing file and with O_CREAT|O_EXCL: the result, the file afterwards, F_GETFL, fstat, lseek, read, write, pwrite, ftruncate, fsync, flock, pread, unlinking the file while open, and close. Then a 0444 file, a directory with and without O_DIRECTORY, O_DIRECTORY on a file, a missing directory, /dev/null, /dev/urandom, the O_WRONLY, O_RDWR and plain O_RDONLY opens for comparison, open through libc, a file another descriptor holds (its size, its offset, a write through it read back through the new descriptor, a write through a dup of the new descriptor failing), a file another descriptor holds a flock on, a name removed while open, and 3000 truncating opens in a row. 212 of the 214 lines equal the Linux build's. The two that do not are the same with main's libraries and are not about truncation: O_DIRECTORY on a file returns the right sceKernel error without setting errno, and a path under a regular file gives ENOENT where Linux gives ENOTDIR. The 3000 opens leave the process handle count unchanged.
  • In that probe, every line for the O_WRONLY, O_RDWR, plain O_RDONLY and O_CREAT opens is identical with main's libraries and the branch's.
  • Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over flock, 44 over pipe streams, 15 over fcntl status flags, 127 over freopen, 130 over directory descriptors, 117 over reads and writes on streams opened the other way, and 313 over fdopen, run with both sets of libraries: the only lines that differ are the two in the first probe that open with O_RDONLY|O_TRUNC, and a file size in the last that changes from run to run with either set.
  • Timing of open and close, 20,000 rounds per pass, three alternating runs: O_RDONLY 76 to 92 µs with main's libraries and 81 to 98 µs with the branch's; O_RDWR 77 to 93 and 79 to 100; O_WRONLY|O_TRUNC 103 to 105 and 99 to 125; O_RDWR|O_CREAT|O_TRUNC 143 to 174 and 125 to 157. No difference I can separate from the spread.
  • Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) end at the same place in both; ccleste's log has one more periodic radv/ps5 submissions line in one of the two runs. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on poll: POLLPRI is not supported by WSAPoll in both (#4478).
  • Full ctest --parallel 4: 1051 of 1051 pass, as with main bb9a4d8a2 built without this change.
  • Commit 1 alone (0f455fca5): full build, and guest_filesystem, guest_kernel_fcntl, guest_truncate, guest_dup and file_streams pass 3/3.
  • 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): libs builds, and guest_filesystem and guest_kernel_fcntl pass 3/3, so the new test matches the existing Linux behaviour. The changed code is Windows only.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5829462923 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5095). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T11:27:56Z. --- On Windows, `sceKernelOpen(path, O_RDONLY | O_TRUNC)` and `open` with the same flags fail with `EINVAL` (`0x80020016`) and leave the file as it was. On FreeBSD the open succeeds, the file is emptied and the descriptor is read-only: [`vn_open_vnode`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_vnops.c#L318-L322) asks for write permission on the file when `O_TRUNC` is set, whatever the access mode, and [`kern_openat`](https://github.com/freebsd/freebsd-src/blob/releng/11.4/sys/kern/vfs_syscalls.c#L1002-L1006) then truncates it. The Linux build does the same. **Cause.** [`NativeOpen`](https://github.com/boykopovar/AnyPS5/blob/bb9a4d8a2/core/libs/prx/libkernel/File/src/Open.cpp#L29-L45) maps `O_TRUNC` without `O_CREAT` to `TRUNCATE_EXISTING` and `O_RDONLY` to `GENERIC_READ`. `CreateFileW` rejects that pair with `ERROR_INVALID_PARAMETER`: `TRUNCATE_EXISTING` needs `GENERIC_WRITE`. Observed on `main` bb9a4d8a2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds ten bytes: | | `main` on Windows | Linux build | |---|---|---| | `sceKernelOpen(file, O_RDONLY\|O_TRUNC)` | `0x80020016`, ten bytes left | descriptor, file empty | | the same with `O_APPEND`, `O_NONBLOCK` or `O_EXCL` added | `0x80020016` | descriptor, file empty | | `open(file, O_RDONLY\|O_TRUNC)` | -1, errno 22 | descriptor, file empty | | the same on a file with mode 0444 | `0x80020016` | `0x8002000D` (EACCES), file kept | | `O_RDONLY\|O_CREAT\|O_TRUNC` | descriptor, file empty | descriptor, file empty | 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-open-rdonly-trunc-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-open-rdonly-trunc-windows) (c076185f3, three commits on `main` bb9a4d8a2, three files, +46/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one). 1. `fix(libkernel): truncate on open with O_RDONLY and O_TRUNC on Windows`: `Open.cpp`. 2. `test(libkernel): open a file with O_RDONLY and O_TRUNC`: `guest_filesystem`. 3. `docs(debt): describe open with O_RDONLY and O_TRUNC on Windows`. Windows only, in `NativeOpen`. For `O_RDONLY` with `O_TRUNC` and without `O_CREAT`, the file is opened with `GENERIC_READ | GENERIC_WRITE`, which lets `TRUNCATE_EXISTING` through and, like FreeBSD, refuses a read-only file with `EACCES`. The handle is then narrowed to `FILE_GENERIC_READ` with `DuplicateHandle` and `DUPLICATE_CLOSE_SOURCE`, so the descriptor is read-only: a write fails with `EBADF`, `ftruncate` with `EINVAL`, and `F_GETFL` reports `O_RDONLY`. The path is chosen from the flags before the call; nothing is tried twice. Every other combination of flags takes the same calls as before. One difference is left and is recorded in the debt file: the narrowed handle is the same open file, which Windows still counts as a writer in its sharing checks, so another host process that opens the file without `FILE_SHARE_WRITE` is refused while the descriptor is open. Opens made by the guest share write access and are not affected. `guest_filesystem` gains `TestReadOnlyTruncatingOpen`: with the file open through a second descriptor, `O_RDONLY|O_TRUNC` must succeed and empty it (seen through the host and through `fstat` on the other descriptor); a write through the new descriptor must fail with `EBADF` and `ftruncate` with `EINVAL`; bytes written through the other descriptor must be readable through it; a missing file must give `ENOENT` and not be created, a directory `EISDIR`, and a 0444 file `EACCES` with its contents kept. **Checked on Windows** (c076185f3 on `main` bb9a4d8a2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `Open.cpp` as on `main` and the test kept, `guest_filesystem` fails (exit 0xc0000409, `Filesystem check failed at line 213 (guest errno 22, Win32 error 87)`). With the fix it passes 5/5 (`--repeat until-fail:5`), and `guest_kernel_fcntl`, `guest_truncate`, `guest_dup`, `guest_file_vector`, `guest_file_lock_conversion`, `guest_file_lock_duplicate`, `file_streams`, `guest_standard_streams`, `guest_posix_filesystem` and `guest_fdopendir` pass 3/3. - The probe behind the table, 214 lines with the branch's libraries and on the Linux build. For each of `O_RDONLY|O_TRUNC` on a file, on an empty file, with `O_APPEND`, with `O_NONBLOCK`, with `O_EXCL`, with `O_CREAT` on an existing and on a missing file and with `O_CREAT|O_EXCL`: the result, the file afterwards, `F_GETFL`, `fstat`, `lseek`, `read`, `write`, `pwrite`, `ftruncate`, `fsync`, `flock`, `pread`, unlinking the file while open, and `close`. Then a 0444 file, a directory with and without `O_DIRECTORY`, `O_DIRECTORY` on a file, a missing directory, `/dev/null`, `/dev/urandom`, the `O_WRONLY`, `O_RDWR` and plain `O_RDONLY` opens for comparison, `open` through libc, a file another descriptor holds (its size, its offset, a write through it read back through the new descriptor, a write through a `dup` of the new descriptor failing), a file another descriptor holds a `flock` on, a name removed while open, and 3000 truncating opens in a row. 212 of the 214 lines equal the Linux build's. The two that do not are the same with `main`'s libraries and are not about truncation: `O_DIRECTORY` on a file returns the right `sceKernel` error without setting errno, and a path under a regular file gives `ENOENT` where Linux gives `ENOTDIR`. The 3000 opens leave the process handle count unchanged. - In that probe, every line for the `O_WRONLY`, `O_RDWR`, plain `O_RDONLY` and `O_CREAT` opens is identical with `main`'s libraries and the branch's. - Probes of 122 lines over the descriptor functions, 866 over streams, 188 over the file, directory, vector and mapping functions, 431 over time, number, character and string functions, 145 over threads, 95 and 54 over `flock`, 44 over pipe streams, 15 over `fcntl` status flags, 127 over `freopen`, 130 over directory descriptors, 117 over reads and writes on streams opened the other way, and 313 over `fdopen`, run with both sets of libraries: the only lines that differ are the two in the first probe that open with `O_RDONLY|O_TRUNC`, and a file size in the last that changes from run to run with either set. - Timing of open and close, 20,000 rounds per pass, three alternating runs: `O_RDONLY` 76 to 92 µs with `main`'s libraries and 81 to 98 µs with the branch's; `O_RDWR` 77 to 93 and 79 to 100; `O_WRONLY|O_TRUNC` 103 to 105 and 99 to 125; `O_RDWR|O_CREAT|O_TRUNC` 143 to 174 and 125 to 157. No difference I can separate from the spread. - Titles from the homebrew catalog, 35 s each with both sets of libraries, windows kept off-screen: ccleste (PPSA59322), RommPS (PPSA76677), ProsperoTV (PPSA99003), DOOM (PPSA99666), ScreenTester5 (PPSA69420), Prospero Vibrate (PPSA99920), SymphonyStation5 (PPSA17641), ProsperoStore (PPSA99000), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) end at the same place in both; ccleste's log has one more periodic `radv/ps5 submissions` line in one of the two runs. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on `poll: POLLPRI is not supported by WSAPoll` in both (#4478). - Full `ctest --parallel 4`: 1051 of 1051 pass, as with `main` bb9a4d8a2 built without this change. - Commit 1 alone (0f455fca5): full build, and `guest_filesystem`, `guest_kernel_fcntl`, `guest_truncate`, `guest_dup` and `file_streams` pass 3/3. - `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): `libs` builds, and `guest_filesystem` and `guest_kernel_fcntl` pass 3/3, so the new test matches the existing Linux behaviour. The changed code is Windows only. Not checked on a console or with a commercial title. AI-assisted: yes
MarcZX10 commented 2026-10-14 06:27:40 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-14T06:27:40Z.


Opened #5684 carrying David's Windows O_RDONLY|O_TRUNC fix, tests, and debt note with his commit authorship preserved. On Linux GCC 12.2 Release, the build succeeded and guest_filesystem plus guest_kernel_fcntl passed (2/2); conventions passed. The detailed Windows matrix is in this issue and will receive additional hosted CI coverage.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162342706 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5095#issuecomment-6162342706). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-14T06:27:40Z. --- Opened #5684 carrying David's Windows `O_RDONLY|O_TRUNC` fix, tests, and debt note with his commit authorship preserved. On Linux GCC 12.2 Release, the build succeeded and `guest_filesystem` plus `guest_kernel_fcntl` passed (2/2); conventions passed. The detailed Windows matrix is in this issue and will receive additional hosted CI coverage.
oneandonlydean commented 2026-10-14 06:54:03 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T06:54:03Z.


Closing: no title is known to reach this, and functions are only changed when a title needs them. The catalog titles in the report run the same with and without the change. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6162707056 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5095#issuecomment-6162707056). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:54:03Z. --- Closing: no title is known to reach this, and functions are only changed when a title needs them. The catalog titles in the report run the same with and without the change. If a title stops on or misbehaves because of it on current main, comment here with the title ID and the log line.
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#5095
No description provided.