libc: a file held by a stream reopened with freopen cannot be renamed or removed on Windows; fix branch ready #5031

Closed
opened 2026-10-13 08:55:18 +00:00 by DavidSamir · 2 comments
DavidSamir commented 2026-10-13 08:55:18 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-13T08:55:18Z.


On Windows, a file held by a stream that went through freopen cannot be renamed or removed while the stream is open: rename fails with EIO and remove with EBUSY. With a stream from fopen, or a descriptor from sceKernelOpen, both work, as they do on the console and in the Linux build. A title that reopens its log or save file with freopen and then rotates or replaces it gets the error.

This is the gap docs/dev/debt/libc.md records: "A stream reopened with freopen still goes through _wfreopen, which does not share delete access".

Cause. fopen opens the file itself with FILE_SHARE_DELETE (#4090). freopen hands the name to the C runtime's _wfreopen, which opens it without that share mode, so every later rename or delete of the name is a sharing violation.

Observed on main c2565f014, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The stream is opened on one file and then reopened with freopen on a second one, in each of the modes r, rb, r+, w, w+, a, a+:

while the reopened stream holds the file main on Windows Linux build
rename(file, other) -1, errno 5 0
remove(file) -1, errno 16 0
sceKernelRename, with a descriptor from sceKernelOpen on the same file as well 0x80020005 0

I have not found a title that stops on this. It is the part of the delete-sharing work (#3316 for descriptors, #4090 for fopen) that the debt file still lists.

Fix, ready to cherry-pick. DavidSamir:fix/libc-freopen-delete-sharing-windows (9203b42ba, three commits on main c2565f014, three files, +49/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one).

  1. fix(libc): share delete access on a stream reopened with freopen on Windows: FileAndHeap.cpp.
  2. test(libc): rename and remove a file a reopened stream holds: file_streams.
  3. docs(debt): describe how a reopened stream shares delete access.

Windows only. _wfreopen still does the reopening, so the stream object, its buffer, its mode and its descriptor number are what they are today. After it succeeds, ShareReopenedFile opens the same file once more with read, write and delete sharing and the access the mode asks for, and moves the stream's descriptor onto that handle with _dup2, which closes the handle _wfreopen made. The append flag is carried over for the a modes. For descriptors 0 to 2 the Win32 standard handle is kept as it was, the way freopen on them already keeps it (#4640).

If the second open fails, the stream is closed and freopen returns null with that error, as it does when the reopening itself fails.

Not changed: between _wfreopen and the _dup2 the file is held without delete sharing, so a rename from another thread in that moment still fails. The debt entry now says how the reopened stream gets its handle.

file_streams gains a sequence: fopen in wb, write, freopen in ab, write, freopen in r+b; then rename and remove on the name must succeed while the stream is open, and the stream must still read back both writes.

Checked on Windows (9203b42ba on main c2565f014, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds):

  • With FileAndHeap.cpp as on main and the test kept, file_streams fails (exit 0xc0000409, File stream check failed). With the fix it passes 5/5 (--repeat until-fail:5), and guest_standard_streams, guest_filesystem, file_position, guest_file_lock_streams, guest_dup, guest_kernel_fcntl and guest_file_vector pass 3/3.
  • The probe behind the table, 127 lines, with main's libraries, the branch's and on the Linux build. For each of eight modes (the seven above and wx): same stream object, same descriptor number, ftell, fread, fseek, fwrite, fflush, rename and remove while held, and a read through the stream after the name is gone. Then file contents after w, a and r+ reopens, wx on a missing and on an existing file, a missing file, an invalid mode, a file a descriptor also holds (with flock between the two, sceKernelRename and sceKernelUnlink), and 3000 reopens in a row. 48 lines change against main, all of them the renames and removes, and 125 of the 127 equal the Linux build's. The two that do not are the same with main's libraries: fwrite on a stream reopened in r or rb does not set the stream's error indicator on Windows. The 3000 reopens leave the process handle count unchanged.
  • 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 and 313 over fdopen, run with both sets of libraries: no line differs, apart from two counters in the last that change from run to run with either set.
  • Timing of freopen itself, 4000 reopens per pass: 77 to 106 µs with main's libraries and 109 to 156 µs with the branch's, in three alternating runs of three passes each, so about 40 µs more per freopen. freopen now opens the file twice. No other function changes.
  • 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. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on poll: POLLPRI is not supported by WSAPoll in both (#4478); SwanStationPS5 was run four times with each set.
  • Full ctest --parallel 4: 1037 of 1039 pass. The two that fail, agc_driver_bda_span_reads and agc_driver_pixel_interlock, fail the same way with main c2565f014 built without this change on this machine (RTX 3070); they do not touch this code. A first run of the suite also failed agc_driver_flat_apertures_1, agc_driver_flat_apertures_2 and tls_function_coverage_register_address_loads (a 30 s timeout); the three then passed 5/5 on their own and in the second full run, which is the one counted here.
  • Commit 1 alone (85523bddd): full build, and file_streams, guest_standard_streams, guest_filesystem and file_position 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 file_streams and guest_standard_streams pass 3/3. The changed code is Windows only; the new test sequence runs on both and passes on Linux as it is.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5827452774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5031). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T08:55:18Z. --- On Windows, a file held by a stream that went through `freopen` cannot be renamed or removed while the stream is open: `rename` fails with `EIO` and `remove` with `EBUSY`. With a stream from `fopen`, or a descriptor from `sceKernelOpen`, both work, as they do on the console and in the Linux build. A title that reopens its log or save file with `freopen` and then rotates or replaces it gets the error. This is the gap [docs/dev/debt/libc.md](https://github.com/boykopovar/AnyPS5/blob/c2565f014/docs/dev/debt/libc.md?plain=1#L43) records: "A stream reopened with `freopen` still goes through `_wfreopen`, which does not share delete access". **Cause.** `fopen` opens the file itself with `FILE_SHARE_DELETE` (#4090). `freopen` hands the name to the C runtime's `_wfreopen`, which opens it without that share mode, so every later rename or delete of the name is a sharing violation. Observed on `main` c2565f014, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. The stream is opened on one file and then reopened with `freopen` on a second one, in each of the modes `r`, `rb`, `r+`, `w`, `w+`, `a`, `a+`: | while the reopened stream holds the file | `main` on Windows | Linux build | |---|---|---| | `rename(file, other)` | -1, errno 5 | 0 | | `remove(file)` | -1, errno 16 | 0 | | `sceKernelRename`, with a descriptor from `sceKernelOpen` on the same file as well | `0x80020005` | 0 | I have not found a title that stops on this. It is the part of the delete-sharing work (#3316 for descriptors, #4090 for `fopen`) that the debt file still lists. **Fix, ready to cherry-pick.** [`DavidSamir:fix/libc-freopen-delete-sharing-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libc-freopen-delete-sharing-windows) (9203b42ba, three commits on `main` c2565f014, three files, +49/-2). I can't open it as a PR while #4532 is open (one open PR without a merged one). 1. `fix(libc): share delete access on a stream reopened with freopen on Windows`: `FileAndHeap.cpp`. 2. `test(libc): rename and remove a file a reopened stream holds`: `file_streams`. 3. `docs(debt): describe how a reopened stream shares delete access`. Windows only. `_wfreopen` still does the reopening, so the stream object, its buffer, its mode and its descriptor number are what they are today. After it succeeds, `ShareReopenedFile` opens the same file once more with read, write and delete sharing and the access the mode asks for, and moves the stream's descriptor onto that handle with `_dup2`, which closes the handle `_wfreopen` made. The append flag is carried over for the `a` modes. For descriptors 0 to 2 the Win32 standard handle is kept as it was, the way `freopen` on them already keeps it (#4640). If the second open fails, the stream is closed and `freopen` returns null with that error, as it does when the reopening itself fails. Not changed: between `_wfreopen` and the `_dup2` the file is held without delete sharing, so a rename from another thread in that moment still fails. The debt entry now says how the reopened stream gets its handle. `file_streams` gains a sequence: `fopen` in `wb`, write, `freopen` in `ab`, write, `freopen` in `r+b`; then `rename` and `remove` on the name must succeed while the stream is open, and the stream must still read back both writes. **Checked on Windows** (9203b42ba on `main` c2565f014, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `FileAndHeap.cpp` as on `main` and the test kept, `file_streams` fails (exit 0xc0000409, `File stream check failed`). With the fix it passes 5/5 (`--repeat until-fail:5`), and `guest_standard_streams`, `guest_filesystem`, `file_position`, `guest_file_lock_streams`, `guest_dup`, `guest_kernel_fcntl` and `guest_file_vector` pass 3/3. - The probe behind the table, 127 lines, with `main`'s libraries, the branch's and on the Linux build. For each of eight modes (the seven above and `wx`): same stream object, same descriptor number, `ftell`, `fread`, `fseek`, `fwrite`, `fflush`, rename and remove while held, and a read through the stream after the name is gone. Then file contents after `w`, `a` and `r+` reopens, `wx` on a missing and on an existing file, a missing file, an invalid mode, a file a descriptor also holds (with `flock` between the two, `sceKernelRename` and `sceKernelUnlink`), and 3000 reopens in a row. 48 lines change against `main`, all of them the renames and removes, and 125 of the 127 equal the Linux build's. The two that do not are the same with `main`'s libraries: `fwrite` on a stream reopened in `r` or `rb` does not set the stream's error indicator on Windows. The 3000 reopens leave the process handle count unchanged. - 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 and 313 over `fdopen`, run with both sets of libraries: no line differs, apart from two counters in the last that change from run to run with either set. - Timing of `freopen` itself, 4000 reopens per pass: 77 to 106 µs with `main`'s libraries and 109 to 156 µs with the branch's, in three alternating runs of three passes each, so about 40 µs more per `freopen`. `freopen` now opens the file twice. No other function changes. - 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. SwanStationPS5 (PPSA98510) and PSXS5 (PPSA97510) stop on `poll: POLLPRI is not supported by WSAPoll` in both (#4478); SwanStationPS5 was run four times with each set. - Full `ctest --parallel 4`: 1037 of 1039 pass. The two that fail, `agc_driver_bda_span_reads` and `agc_driver_pixel_interlock`, fail the same way with `main` c2565f014 built without this change on this machine (RTX 3070); they do not touch this code. A first run of the suite also failed `agc_driver_flat_apertures_1`, `agc_driver_flat_apertures_2` and `tls_function_coverage_register_address_loads` (a 30 s timeout); the three then passed 5/5 on their own and in the second full run, which is the one counted here. - Commit 1 alone (85523bddd): full build, and `file_streams`, `guest_standard_streams`, `guest_filesystem` and `file_position` 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 `file_streams` and `guest_standard_streams` pass 3/3. The changed code is Windows only; the new test sequence runs on both and passes on Linux as it is. Not checked on a console or with a commercial title. AI-assisted: yes
milescoviello commented 2026-10-13 09:05:00 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T09:05:00Z.


@DavidSamir I've opened your branch as #5033 with your three commits and authorship kept. The fix and the test cherry-picked onto current main (2a2a3dc65) without changes. The debt commit had one conflict, because #4918 added its _Unwind_Context entry right after the line you edit, and I kept both entries as they are.

I added a Linux run there: Ubuntu 24.04, GCC 13.3, Release. The build is clean, file_streams passes 20/20 and all 334 guest_* tests pass in two runs. A MinGW-w64 -fsyntax-only -Wall -Wextra compile of FileAndHeap.cpp and FileStreams.cpp is clean. The Windows results in the PR are yours from this issue; I couldn't run Windows here. I'll keep it up to date with main and answer review there.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6142130561 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/5031#issuecomment-6142130561). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T09:05:00Z. --- @DavidSamir I've opened your branch as #5033 with your three commits and authorship kept. The fix and the test cherry-picked onto current `main` (2a2a3dc65) without changes. The debt commit had one conflict, because #4918 added its `_Unwind_Context` entry right after the line you edit, and I kept both entries as they are. I added a Linux run there: Ubuntu 24.04, GCC 13.3, Release. The build is clean, `file_streams` passes 20/20 and all 334 `guest_*` tests pass in two runs. A MinGW-w64 `-fsyntax-only -Wall -Wextra` compile of `FileAndHeap.cpp` and `FileStreams.cpp` is clean. The Windows results in the PR are yours from this issue; I couldn't run Windows here. I'll keep it up to date with `main` and answer review there. AI-assisted: yes
oneandonlydean commented 2026-10-14 06:53:58 +00:00 (Migrated from github.com)
Author
Contributor

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


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