libc: fdopen in an append mode overwrites instead of appending on Windows; fix branch ready #4735
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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 withopenand 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
fdopenhandles 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_postfixpasses the mode to the C runtime's_fdopen, which does not turn on append for the descriptor; on Windows only_openand_open_osfhandledo.fopenin an append mode is not affected, because it opens its descriptor with_O_APPENDitself.Observed on
maina60f52e31, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The file holds0123456789(empty in the last row) and the descriptor comes fromsceKernelOpen(path, O_RDWR); in the first row it is at offset 4:fdopen(fd, "a"),fputs("T"),fclose0123T567890123456789Tfdopen(fd, "a+")with the descriptor at offset 3,fread2,fseek(0, SEEK_CUR),fwrite("XY"),fclose01234XY7890123456789XYfputs("A"),fputs("B")through an"a"stream at offset 601B345A7890123456789ABfdopen(fd, "a"),fputs("x"),fcloseI 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 onmaina60f52e31, 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,AppendThroughDescriptorduplicates the descriptor's handle, opens the copy as a descriptor with_O_APPENDand 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
_dup2onto them moves the Win32 standard handle (#4631, #4640), and descriptors that are not disk files (pipes, theNULdescriptor behind directories and/dev/urandom), where append has no meaning.Not changed, and seen while testing:
sceKernelOpenwithO_APPENDuses on Windows (_open_osfhandlewith_O_APPEND); I did not measure that case. Eight threads appending 16 bytes 1000 times each leave 16 bytes onmainand 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 laterfflushthrows; FreeBSD and the Linux build fail thefdopenwithEINVAL. #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 infdopen_nid_postfixand inGuestStandardStreams.cpp, so whichever lands second needs a small rebase, which I can do.guest_standard_streamsgainsCheckDescriptorAppend: for"a","ab","a+"and"ae", a descriptor on a file holdingretainedis moved to offset 2 and wrapped; one byte written and flushed, and one more written afterfseek(0, SEEK_SET), must both land at the end of the file, andfilenomust still be the descriptor.Checked on Windows (ca4e59101 on
maina60f52e31, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release):FileAndHeap.cppas onmainand the new check kept,guest_standard_streamsfails (exit 0xc0000409, the test'sRequireaborting). With the fix it passes 5/5 (--repeat until-fail:5).main's libraries and the branch's:fdopenin"a","ab","a+","a+b"and"ae"on descriptors opened read-write and write-only, with and withoutO_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 setsO_APPENDon the descriptor. With the branch, on a wrapped descriptor: aflocktaken beforefdopenstill 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,preadanddup2onto it work as before; a pipe is unchanged; a closed descriptor, 999 and -1 return NULL with errno 9 as before.fdopen "a", write,fclosewith no failure and no change in the process handle count.sceKernelOpen,fdopen,fclose, three passes, two runs each:"a"takes 74 to 83 µs withmain'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 on9907993e5). No difference I can measure.fopenin 43 modes, seeking,ungetc,fgets, buffering,freopen,fdopen), run with both sets of libraries: two lines differ, both the contents afterfdopen(fd, "a"). A third probe of 122 lines over the descriptor functions is identical in both.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 onmain9907993e5; the change itself is the same. I do not know whether any of them callsfdopenin an append mode.ctest --parallel 4: 995 of 995 pass (7 skipped, no display).tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchlists #4728 for those two places and nothing else.Checked on Linux (same commit, WSL2 Ubuntu 24.04, GCC 13.3.0, Release):
guest_standard_streamspasses 3/3, so the new check matches the existing Linux behaviour. Only that target andlibswere built there.Not checked on a console or with a commercial title.
AI-assisted: yes
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 newCheckDescriptorAppendalso passes withmain'sFileAndHeap.cpp, so Linux is unchanged. The Windows results in the PR are yours from this issue. Thanks for the branch.AI-assisted: yes