libkernel: fcntl F_SETFL that changes O_APPEND on a file throws on Windows; implementation ready #4941

Closed
opened 2026-10-13 05:44:02 +00:00 by DavidSamir · 2 comments
DavidSamir commented 2026-10-13 05:44:02 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T05:44:02Z.


On Windows, fcntl(fd, F_SETFL, ...) that turns O_APPEND on or off for a file throws fcntl: changing O_APPEND on a file is not implemented and the process ends. FreeBSD and the Linux build change the flag. This is the gap docs/dev/debt/libkernel.md records ("Changing O_APPEND on a Windows file ... throw"), and guest_kernel_fcntl expects the throw on Windows.

The usual way a program reaches it: a logger or a library that takes a descriptor it did not open and makes sure it appends with fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_APPEND), or one that clears the flag on a descriptor opened with O_APPEND before it seeks and overwrites.

I have not found a title that stops on this. I am offering it because the pieces for it are already in the tree after #4735.

Feature, ready to cherry-pick. DavidSamir:fix/libkernel-fcntl-append-windows (c52947f1e, three commits on main e51428bf2, three files, +52/-6). I can't open it as a PR while #4532 is open (one open PR without a merged one).

The three commits, each of which builds and passes the tests on its own:

  1. feat(libkernel): change O_APPEND with fcntl on a file on Windows: the change in DescriptorControl.cpp, with the Windows-only expectation of the throw taken out of guest_kernel_fcntl, which it makes false.
  2. test(libkernel): check where writes land after fcntl changes O_APPEND: the new sequence.
  3. docs(debt): describe how fcntl changes O_APPEND on Windows.

Windows only, in ApplyHostStatus. On Windows appending is a flag of the C runtime's descriptor, set when the descriptor is made (_open_osfhandle with _O_APPEND, which is what sceKernelOpen uses for O_APPEND). To change it, SetAppend duplicates the descriptor's handle, makes a descriptor for the copy with the flag set or cleared and the same text or binary setting, and moves it onto the guest's descriptor with _dup2. The descriptor keeps its number and its file object, and with that its offset, its flock and its sharing with duplicates.

O_APPEND belongs to the open file, so the change is applied to every descriptor that shares it: the ones the guest table already ties to the same status flags (dup, dup2, F_DUPFD), checked with CompareObjectHandles to be the same open file, so a number that was closed and reused behind the table's back is left alone. Descriptors made by dup afterwards inherit the flag from the C runtime.

Still throws, as before: descriptors 0 to 2, whose handle is also the Win32 standard handle (#4631, #4640). The debt entry is rewritten to say that. Pipes, directories and devices only record the flag, as today.

Seen while testing and not changed: pwrite on a descriptor with O_APPEND writes at the given offset on Windows and appends on the Linux build. That is the same for a descriptor opened with O_APPEND on main.

guest_kernel_fcntl: the Windows-only expectation of the throw is removed, so F_SETFL with 0 on a descriptor opened with O_APPEND must succeed on both hosts, and a new sequence on a second file checks the behaviour: a write after a seek to 0 overwrites; after F_SETFL O_APPEND through a duplicate, F_GETFL on the first descriptor shows the flag and writes through either land at the end whatever the offset; after F_SETFL 0 a write at offset 1 overwrites again.

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

The runs below were made on b4f4e06d5, the same tree as c52947f1e before it was split into the three commits; nothing but the commit boundaries differs.

  • With DescriptorControl.cpp as on main and the test kept, guest_kernel_fcntl fails (exit 0xc0000409, the throw). With the change it passes 5/5 (--repeat until-fail:5), and guest_filesystem, guest_file_vector, guest_poll, guest_dup, guest_standard_streams, file_streams, guest_aio, guest_ioctl, guest_file_lock_lifetime, guest_file_lock_duplicate and guest_udp pass 3/3.
  • A probe of 87 lines with the branch's libraries and on the Linux build. Setting the flag (offset kept, writes land at the end after a seek to 0, pread unaffected, setting it twice, together with O_NONBLOCK), clearing it, a closed descriptor, a dup and an F_DUPFD copy made before and a dup made after, an independent open of the same file staying without the flag, clearing through a duplicate, flock held across the change and still excluding another open, FD_CLOEXEC kept, fstat, fsync, ftruncate, rename and unlink on the descriptor, a descriptor wrapped by fdopen, dup2 of another file onto the descriptor, and a read-only descriptor: 85 lines are identical to Linux, and the two that differ are the pwrite case above.
  • Same probe: 5000 rounds of set, write, clear on one descriptor with no failure, 5000 bytes in the file and no change in the process handle count; six threads toggling the flag 800 times each on their own files with no failure.
  • The part of the probe that does not change O_APPEND (F_SETFL O_NONBLOCK on a file, a descriptor opened with O_APPEND, F_SETFL O_APPEND on a pipe), 15 lines, is identical with main's libraries, the branch's and on Linux.
  • Timing, three passes in two alternating runs each: F_GETFL 336 to 432 ns with main's libraries and 330 to 375 ns with the branch's; F_SETFL toggling O_NONBLOCK 185 to 249 ns and 184 to 211 ns; seek and write 1.71 to 1.86 µs and 1.71 to 1.89 µs. No difference I can separate from the spread.
  • Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 33 over non-blocking pipes and 313 over fdopen, run with both sets of libraries: no line differs, apart from the size a file reaches when eight threads append to it at once, which changes from run to run with either set.
  • 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.
  • Full ctest --parallel 4: 1026 of 1028 pass. The two that fail, agc_driver_bda_span_reads and agc_driver_pixel_interlock, fail the same way with main e51428bf2 built without this change on this machine (vkWaitForFences recorder: Vulkan result -4, RTX 3070); they do not touch this code.
  • Commit 1 alone (89b7996c8): full build, and guest_kernel_fcntl, guest_filesystem, guest_dup and guest_file_lock_duplicate 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 (the same tree, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_kernel_fcntl passes 3/3, so the new sequence matches the existing Linux behaviour. Only that target and libs were built there.

Not checked on a console, on a Windows version older than this one, or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5825200144 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4941). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T05:44:02Z. --- On Windows, `fcntl(fd, F_SETFL, ...)` that turns `O_APPEND` on or off for a file throws `fcntl: changing O_APPEND on a file is not implemented` and the process ends. FreeBSD and the Linux build change the flag. This is the gap [docs/dev/debt/libkernel.md](https://github.com/boykopovar/AnyPS5/blob/e51428bf2/docs/dev/debt/libkernel.md?plain=1#L153) records ("Changing `O_APPEND` on a Windows file ... throw"), and `guest_kernel_fcntl` expects the throw on Windows. The usual way a program reaches it: a logger or a library that takes a descriptor it did not open and makes sure it appends with `fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_APPEND)`, or one that clears the flag on a descriptor opened with `O_APPEND` before it seeks and overwrites. I have not found a title that stops on this. I am offering it because the pieces for it are already in the tree after #4735. **Feature, ready to cherry-pick.** [`DavidSamir:fix/libkernel-fcntl-append-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libkernel-fcntl-append-windows) (c52947f1e, three commits on `main` e51428bf2, three files, +52/-6). I can't open it as a PR while #4532 is open (one open PR without a merged one). The three commits, each of which builds and passes the tests on its own: 1. `feat(libkernel): change O_APPEND with fcntl on a file on Windows`: the change in `DescriptorControl.cpp`, with the Windows-only expectation of the throw taken out of `guest_kernel_fcntl`, which it makes false. 2. `test(libkernel): check where writes land after fcntl changes O_APPEND`: the new sequence. 3. `docs(debt): describe how fcntl changes O_APPEND on Windows`. Windows only, in `ApplyHostStatus`. On Windows appending is a flag of the C runtime's descriptor, set when the descriptor is made (`_open_osfhandle` with `_O_APPEND`, which is what `sceKernelOpen` uses for `O_APPEND`). To change it, `SetAppend` duplicates the descriptor's handle, makes a descriptor for the copy with the flag set or cleared and the same text or binary setting, and moves it onto the guest's descriptor with `_dup2`. The descriptor keeps its number and its file object, and with that its offset, its `flock` and its sharing with duplicates. `O_APPEND` belongs to the open file, so the change is applied to every descriptor that shares it: the ones the guest table already ties to the same status flags (`dup`, `dup2`, `F_DUPFD`), checked with `CompareObjectHandles` to be the same open file, so a number that was closed and reused behind the table's back is left alone. Descriptors made by `dup` afterwards inherit the flag from the C runtime. Still throws, as before: descriptors 0 to 2, whose handle is also the Win32 standard handle (#4631, #4640). The debt entry is rewritten to say that. Pipes, directories and devices only record the flag, as today. Seen while testing and not changed: `pwrite` on a descriptor with `O_APPEND` writes at the given offset on Windows and appends on the Linux build. That is the same for a descriptor opened with `O_APPEND` on `main`. `guest_kernel_fcntl`: the Windows-only expectation of the throw is removed, so `F_SETFL` with 0 on a descriptor opened with `O_APPEND` must succeed on both hosts, and a new sequence on a second file checks the behaviour: a write after a seek to 0 overwrites; after `F_SETFL O_APPEND` through a duplicate, `F_GETFL` on the first descriptor shows the flag and writes through either land at the end whatever the offset; after `F_SETFL 0` a write at offset 1 overwrites again. **Checked on Windows** (c52947f1e on `main` e51428bf2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): The runs below were made on b4f4e06d5, the same tree as c52947f1e before it was split into the three commits; nothing but the commit boundaries differs. - With `DescriptorControl.cpp` as on `main` and the test kept, `guest_kernel_fcntl` fails (exit 0xc0000409, the throw). With the change it passes 5/5 (`--repeat until-fail:5`), and `guest_filesystem`, `guest_file_vector`, `guest_poll`, `guest_dup`, `guest_standard_streams`, `file_streams`, `guest_aio`, `guest_ioctl`, `guest_file_lock_lifetime`, `guest_file_lock_duplicate` and `guest_udp` pass 3/3. - A probe of 87 lines with the branch's libraries and on the Linux build. Setting the flag (offset kept, writes land at the end after a seek to 0, `pread` unaffected, setting it twice, together with `O_NONBLOCK`), clearing it, a closed descriptor, a `dup` and an `F_DUPFD` copy made before and a `dup` made after, an independent open of the same file staying without the flag, clearing through a duplicate, `flock` held across the change and still excluding another open, `FD_CLOEXEC` kept, `fstat`, `fsync`, `ftruncate`, `rename` and `unlink` on the descriptor, a descriptor wrapped by `fdopen`, `dup2` of another file onto the descriptor, and a read-only descriptor: 85 lines are identical to Linux, and the two that differ are the `pwrite` case above. - Same probe: 5000 rounds of set, write, clear on one descriptor with no failure, 5000 bytes in the file and no change in the process handle count; six threads toggling the flag 800 times each on their own files with no failure. - The part of the probe that does not change `O_APPEND` (`F_SETFL O_NONBLOCK` on a file, a descriptor opened with `O_APPEND`, `F_SETFL O_APPEND` on a pipe), 15 lines, is identical with `main`'s libraries, the branch's and on Linux. - Timing, three passes in two alternating runs each: `F_GETFL` 336 to 432 ns with `main`'s libraries and 330 to 375 ns with the branch's; `F_SETFL` toggling `O_NONBLOCK` 185 to 249 ns and 184 to 211 ns; seek and write 1.71 to 1.86 µs and 1.71 to 1.89 µs. No difference I can separate from the spread. - Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 33 over non-blocking pipes and 313 over `fdopen`, run with both sets of libraries: no line differs, apart from the size a file reaches when eight threads append to it at once, which changes from run to run with either set. - 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. - Full `ctest --parallel 4`: 1026 of 1028 pass. The two that fail, `agc_driver_bda_span_reads` and `agc_driver_pixel_interlock`, fail the same way with `main` e51428bf2 built without this change on this machine (`vkWaitForFences recorder: Vulkan result -4`, RTX 3070); they do not touch this code. - Commit 1 alone (89b7996c8): full build, and `guest_kernel_fcntl`, `guest_filesystem`, `guest_dup` and `guest_file_lock_duplicate` 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** (the same tree, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `guest_kernel_fcntl` passes 3/3, so the new sequence matches the existing Linux behaviour. Only that target and `libs` were built there. Not checked on a console, on a Windows version older than this one, or with a commercial title. AI-assisted: yes
karthikeya-tummala commented 2026-10-13 10:37:45 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by karthikeya-tummala on 2026-10-13T10:37:45Z.


Hey David,

Can I cherry pick them and open a PR?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143693078 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4941#issuecomment-6143693078). Originally posted by [karthikeya-tummala](https://github.com/karthikeya-tummala) on 2026-10-13T10:37:45Z. --- Hey David, Can I cherry pick them and open a PR?
oneandonlydean commented 2026-10-14 06:53:55 +00:00 (Migrated from github.com)
Author
Contributor

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


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:6162705030 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4941#issuecomment-6162705030). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T06:53:55Z. --- 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#4941
No description provided.