libc: fdopen in an append mode overwrites instead of appending on Windows; fix branch ready #4735

Closed
opened 2026-10-12 20:06:22 +00:00 by DavidSamir · 1 comment
DavidSamir commented 2026-10-12 20:06:22 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-12T20:06:22Z.


On Windows, a stream made with fdopen(fd, "a") (also "ab", "a+", "ae") does not append: it writes at the descriptor's current offset and overwrites what is there. A program that opens its log or save file with open and wraps the descriptor in an append stream replaces the start of the file with each new run instead of adding to it. The Linux build appends.

FreeBSD's fdopen handles exactly this case: "If opened for appending, but underlying descriptor does not have O_APPEND bit set, assert __SAPP so that __swrite() caller will _sseek() to the end before write."

Cause. fdopen_nid_postfix passes the mode to the C runtime's _fdopen, which does not turn on append for the descriptor; on Windows only _open and _open_osfhandle do. fopen in an append mode is not affected, because it opens its descriptor with _O_APPEND itself.

Observed on main a60f52e31, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds 0123456789 (empty in the last row) and the descriptor comes from sceKernelOpen(path, O_RDWR); in the first row it is at offset 4:

calls Windows Linux build
fdopen(fd, "a"), fputs("T"), fclose 0123T56789 0123456789T
fdopen(fd, "a+") with the descriptor at offset 3, fread 2, fseek(0, SEEK_CUR), fwrite("XY"), fclose 01234XY789 0123456789XY
a duplicate of the descriptor is moved to offset 2 between two flushed fputs("A"), fputs("B") through an "a" stream at offset 6 01B345A789 0123456789AB
5000 times: open, fdopen(fd, "a"), fputs("x"), fclose file holds 1 byte 5000 bytes

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

Fix, ready to cherry-pick. DavidSamir:fix/libc-fdopen-append-windows (ca4e59101, one commit on main a60f52e31, two files, +52/-0). I can't open it as a PR while #4532 is open (one open PR without a merged one).

Windows only, and only for the append modes of fdopen. Before _fdopen, AppendThroughDescriptor duplicates the descriptor's handle, opens the copy as a descriptor with _O_APPEND and the same text or binary setting, and moves it onto the guest's descriptor with _dup2. The descriptor keeps its number, its file object and so its offset, its locks and its sharing with duplicates; what changes is that the C runtime now seeks to the end before each write through it, which is what FreeBSD's stream does. Every other mode takes the path it takes today.

Left as they are: descriptors 0 to 2, because _dup2 onto them moves the Win32 standard handle (#4631, #4640), and descriptors that are not disk files (pipes, the NUL descriptor behind directories and /dev/urandom), where append has no meaning.

Not changed, and seen while testing:

  • Several streams appending to one file through different descriptors at the same time can still lose writes, because the C runtime seeks and then writes. It is the mechanism sceKernelOpen with O_APPEND uses on Windows (_open_osfhandle with _O_APPEND); I did not measure that case. Eight threads appending 16 bytes 1000 times each leave 16 bytes on main and about 72600 of the 128000 with the branch; Linux leaves all of them.
  • fdopen(fd, "w") on a read-only descriptor succeeds on Windows and the later fflush throws; FreeBSD and the Linux build fail the fdopen with EINVAL. #4728 fixes that. It does not change where an append stream writes, and this branch does not depend on it; the two add lines at the same place in fdopen_nid_postfix and in GuestStandardStreams.cpp, so whichever lands second needs a small rebase, which I can do.

guest_standard_streams gains CheckDescriptorAppend: for "a", "ab", "a+" and "ae", a descriptor on a file holding retained is moved to offset 2 and wrapped; one byte written and flushed, and one more written after fseek(0, SEEK_SET), must both land at the end of the file, and fileno must still be the descriptor.

Checked on Windows (ca4e59101 on main a60f52e31, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release):

  • With FileAndHeap.cpp as on main and the new check kept, guest_standard_streams fails (exit 0xc0000409, the test's Require aborting). With the fix it passes 5/5 (--repeat until-fail:5).
  • A probe of 313 lines run with main's libraries and the branch's: fdopen in "a", "ab", "a+", "a+b" and "ae" on descriptors opened read-write and write-only, with and without O_APPEND, at offset 0 and 4, now appends as the Linux build does; "w" and "r+" are identical in both. One line still differs from the Linux build: pwrite(fd, "F", 1, 0) on the wrapped descriptor writes at offset 0 with the branch, as on FreeBSD, where the stream and not the descriptor appends; Linux appends it, because glibc sets O_APPEND on the descriptor. With the branch, on a wrapped descriptor: a flock taken before fdopen still excludes another open and can be released; a duplicate made before sees the same offset and size; fcntl(F_GETFL), fstat, fsync, ftruncate, rename, unlink, pread and dup2 onto it work as before; a pipe is unchanged; a closed descriptor, 999 and -1 return NULL with errno 9 as before.
  • Same probe: 5000 times open, fdopen "a", write, fclose with no failure and no change in the process handle count.
  • Timing, 20000 times sceKernelOpen, fdopen, fclose, three passes, two runs each: "a" takes 74 to 83 µs with main's libraries and 77 to 79 µs with the branch's; "r" 75 to 81 µs and 75 to 77 µs (measured before the rebase, 03a62d662 on 9907993e5). No difference I can measure.
  • A second probe of 884 lines over the stream functions (fopen in 43 modes, seeking, ungetc, fgets, buffering, freopen, fdopen), run with both sets of libraries: two lines differ, both the contents after fdopen(fd, "a"). A third probe of 122 lines over the descriptor functions is identical in both.
  • Titles from the homebrew catalog, all of which import fdopen, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), 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. These runs were made before the rebase, with 03a62d662 on main 9907993e5; the change itself is the same. I do not know whether any of them calls fdopen in an append mode.
  • Full ctest --parallel 4: 995 of 995 pass (7 skipped, no display).
  • tools/check_conventions.py --base origin/main passes. tools/pr_overlap.py --branch lists #4728 for those two places and nothing else.

Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): guest_standard_streams passes 3/3, so the new check matches 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:5819747683 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4735). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-12T20:06:22Z. --- On Windows, a stream made with `fdopen(fd, "a")` (also `"ab"`, `"a+"`, `"ae"`) does not append: it writes at the descriptor's current offset and overwrites what is there. A program that opens its log or save file with `open` and wraps the descriptor in an append stream replaces the start of the file with each new run instead of adding to it. The Linux build appends. FreeBSD's [`fdopen`](https://github.com/freebsd/freebsd-src/blob/main/lib/libc/stdio/fdopen.c#L95-L103) handles exactly this case: "If opened for appending, but underlying descriptor does not have O_APPEND bit set, assert __SAPP so that __swrite() caller will _sseek() to the end before write." **Cause.** [`fdopen_nid_postfix`](https://github.com/boykopovar/AnyPS5/blob/a60f52e31/core/libs/prx/libc/src/FileAndHeap.cpp#L188-L208) passes the mode to the C runtime's `_fdopen`, which does not turn on append for the descriptor; on Windows only `_open` and `_open_osfhandle` do. `fopen` in an append mode is not affected, because it opens its descriptor with `_O_APPEND` itself. Observed on `main` a60f52e31, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds `0123456789` (empty in the last row) and the descriptor comes from `sceKernelOpen(path, O_RDWR)`; in the first row it is at offset 4: | calls | Windows | Linux build | |---|---|---| | `fdopen(fd, "a")`, `fputs("T")`, `fclose` | `0123T56789` | `0123456789T` | | `fdopen(fd, "a+")` with the descriptor at offset 3, `fread` 2, `fseek(0, SEEK_CUR)`, `fwrite("XY")`, `fclose` | `01234XY789` | `0123456789XY` | | a duplicate of the descriptor is moved to offset 2 between two flushed `fputs("A")`, `fputs("B")` through an `"a"` stream at offset 6 | `01B345A789` | `0123456789AB` | | 5000 times: open, `fdopen(fd, "a")`, `fputs("x")`, `fclose` | file holds 1 byte | 5000 bytes | I have not found a title that stops on this. It came out of running one probe of the stream functions on the Windows build and on the Linux build and comparing the two outputs. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libc-fdopen-append-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libc-fdopen-append-windows) (ca4e59101, one commit on `main` a60f52e31, two files, +52/-0). I can't open it as a PR while #4532 is open (one open PR without a merged one). Windows only, and only for the append modes of `fdopen`. Before `_fdopen`, `AppendThroughDescriptor` duplicates the descriptor's handle, opens the copy as a descriptor with `_O_APPEND` and the same text or binary setting, and moves it onto the guest's descriptor with `_dup2`. The descriptor keeps its number, its file object and so its offset, its locks and its sharing with duplicates; what changes is that the C runtime now seeks to the end before each write through it, which is what FreeBSD's stream does. Every other mode takes the path it takes today. Left as they are: descriptors 0 to 2, because `_dup2` onto them moves the Win32 standard handle (#4631, #4640), and descriptors that are not disk files (pipes, the `NUL` descriptor behind directories and `/dev/urandom`), where append has no meaning. Not changed, and seen while testing: - Several streams appending to one file through different descriptors at the same time can still lose writes, because the C runtime seeks and then writes. It is the mechanism `sceKernelOpen` with `O_APPEND` uses on Windows (`_open_osfhandle` with `_O_APPEND`); I did not measure that case. Eight threads appending 16 bytes 1000 times each leave 16 bytes on `main` and about 72600 of the 128000 with the branch; Linux leaves all of them. - `fdopen(fd, "w")` on a read-only descriptor succeeds on Windows and the later `fflush` throws; FreeBSD and the Linux build fail the `fdopen` with `EINVAL`. #4728 fixes that. It does not change where an append stream writes, and this branch does not depend on it; the two add lines at the same place in `fdopen_nid_postfix` and in `GuestStandardStreams.cpp`, so whichever lands second needs a small rebase, which I can do. `guest_standard_streams` gains `CheckDescriptorAppend`: for `"a"`, `"ab"`, `"a+"` and `"ae"`, a descriptor on a file holding `retained` is moved to offset 2 and wrapped; one byte written and flushed, and one more written after `fseek(0, SEEK_SET)`, must both land at the end of the file, and `fileno` must still be the descriptor. **Checked on Windows** (ca4e59101 on `main` a60f52e31, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release): - With `FileAndHeap.cpp` as on `main` and the new check kept, `guest_standard_streams` fails (exit 0xc0000409, the test's `Require` aborting). With the fix it passes 5/5 (`--repeat until-fail:5`). - A probe of 313 lines run with `main`'s libraries and the branch's: `fdopen` in `"a"`, `"ab"`, `"a+"`, `"a+b"` and `"ae"` on descriptors opened read-write and write-only, with and without `O_APPEND`, at offset 0 and 4, now appends as the Linux build does; `"w"` and `"r+"` are identical in both. One line still differs from the Linux build: `pwrite(fd, "F", 1, 0)` on the wrapped descriptor writes at offset 0 with the branch, as on FreeBSD, where the stream and not the descriptor appends; Linux appends it, because glibc sets `O_APPEND` on the descriptor. With the branch, on a wrapped descriptor: a `flock` taken before `fdopen` still excludes another open and can be released; a duplicate made before sees the same offset and size; `fcntl(F_GETFL)`, `fstat`, `fsync`, `ftruncate`, `rename`, `unlink`, `pread` and `dup2` onto it work as before; a pipe is unchanged; a closed descriptor, 999 and -1 return NULL with errno 9 as before. - Same probe: 5000 times open, `fdopen "a"`, write, `fclose` with no failure and no change in the process handle count. - Timing, 20000 times `sceKernelOpen`, `fdopen`, `fclose`, three passes, two runs each: `"a"` takes 74 to 83 µs with `main`'s libraries and 77 to 79 µs with the branch's; `"r"` 75 to 81 µs and 75 to 77 µs (measured before the rebase, 03a62d662 on 9907993e5). No difference I can measure. - A second probe of 884 lines over the stream functions (`fopen` in 43 modes, seeking, `ungetc`, `fgets`, buffering, `freopen`, `fdopen`), run with both sets of libraries: two lines differ, both the contents after `fdopen(fd, "a")`. A third probe of 122 lines over the descriptor functions is identical in both. - Titles from the homebrew catalog, all of which import `fdopen`, 35 s each with both sets of libraries: ccleste (PPSA59322), RommPS (PPSA76677), SwanStationPS5 (PPSA98510), ProsperoTV (PPSA99003), PSXS5 (PPSA97510), 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. These runs were made before the rebase, with 03a62d662 on `main` 9907993e5; the change itself is the same. I do not know whether any of them calls `fdopen` in an append mode. - Full `ctest --parallel 4`: 995 of 995 pass (7 skipped, no display). - `tools/check_conventions.py --base origin/main` passes. `tools/pr_overlap.py --branch` lists #4728 for those two places and nothing else. **Checked on Linux** (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release): `guest_standard_streams` passes 3/3, so the new check matches 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 20:12:27 +00:00 (Migrated from github.com)
Author
Contributor

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


Carried in #4738, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 312 guest_* tests pass in two runs, and the new CheckDescriptorAppend also passes with main's FileAndHeap.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:6131826181 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4735#issuecomment-6131826181). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T20:12:27Z. --- Carried in #4738, keeping your commit and authorship. I added the full Linux run (Ubuntu 24.04, GCC 13.3): all 312 `guest_*` tests pass in two runs, and the new `CheckDescriptorAppend` also passes with `main`'s `FileAndHeap.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#4735
No description provided.