libc: a flock makes reads and writes through other descriptors fail on Windows, where FreeBSD locks are advisory; fix branch ready #4957

Closed
opened 2026-10-13 06:27:47 +00:00 by DavidSamir · 1 comment
DavidSamir commented 2026-10-13 06:27:47 +00:00 (Migrated from github.com)
Contributor

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


On Windows, while one descriptor holds a flock on a file, reads and writes through any other open of that file fail: with an exclusive lock everything fails, with a shared lock every write. FreeBSD's flock(2) locks are advisory ("Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee consistency (i.e., processes may still access files without using advisory locks possibly resulting in inconsistencies)"), and the Linux build reads and writes normally. A title that locks its save or database file through one descriptor and reads it through another, or through a FILE*, gets EIO or EACCES.

This is the gap docs/dev/debt/libc.md records: "The host's LockFileEx locks also restrict ordinary reads and writes through other handles, unlike FreeBSD's advisory locks".

Cause. GuestFileLock_nid_no_patch locks the whole file (LockFileEx from offset 0 for MAXDWORD:MAXDWORD bytes), and a Windows byte-range lock is enforced on every read and write that touches the range.

Observed on main e51428bf2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. first and other are two sceKernelOpen(path, O_RDWR) of one file; first holds the lock:

through other first holds LOCK_EX first holds LOCK_SH Linux build, both
sceKernelPread 0x80020005 (EIO) 4 4
sceKernelRead 0x8002000D (EACCES) 4 4
sceKernelPwrite 0x80020005 0x80020005 1
sceKernelWrite, also past the end 0x8002000D 0x8002000D 1
fgetc on a stream from fopen(path, "r+") -1, errno 13 not run the byte
flock(other, LOCK_EX|LOCK_NB) -1, errno 35 -1, errno 35 -1, errno 35

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/libc-flock-advisory-windows (e4a1b54ad, three commits on main e51428bf2, three files, +18/-3). I can't open it as a PR while #4532 is open (one open PR without a merged one).

  1. fix(libc): make flock advisory on Windows so other descriptors can read and write: FileLock.cpp.
  2. test(libc): read and write through another descriptor while a flock is held: guest_file_lock_conversion.
  3. docs(debt): describe the byte the Windows flock locks.

Windows only. The lock and the unlock take one byte at offset 2^63 - 2 instead of the whole file. No file reaches that offset, so no read or write touches the locked range, while two flock calls still meet on the same byte: shared and exclusive locks exclude each other exactly as before, between descriptors, duplicates and processes. Nothing else in the function changes.

Not changed: converting a lock from shared to exclusive still releases the first before taking the second, so another descriptor can take it in between; and descriptors that are not disk files fail as they do today.

guest_file_lock_conversion gains checks in each of its three duplicate kinds: with an exclusive lock held, sceKernelPwrite, sceKernelWrite and sceKernelPread through the other open must succeed and read back what was written, and flock(LOCK_SH|LOCK_NB) through it must still fail with errno 35.

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

  • With FileLock.cpp as on main and the test kept, guest_file_lock_conversion fails (exit 0xc0000409, the test's Require aborting). With the fix the six guest_file_lock_* tests (duplicate, lifetime, conversion, threads, streams, process) pass 5/5 (--repeat until-fail:5), and guest_filesystem, guest_kernel_fcntl, guest_dup, guest_standard_streams, file_streams and guest_file_vector pass 3/3. process takes the lock from a second process, so the byte is the same one across processes.
  • The probe behind the table, 54 lines, with main's libraries, the branch's and on the Linux build: ten lines change against main, the reads and writes in the table and a round of 2000 lock, contend, write, close, and with the branch every line equals the Linux build's. That includes each of pread, read, pwrite, write, a write past the end, ftruncate, fsync, fstat and a shared and an exclusive flock attempt through the other open, with no lock, with an exclusive and with a shared lock held; the holder's own reads and writes; a stream; renaming onto and away from the locked file and unlinking it; and a blocked flock that is granted when the holder unlocks 150 ms later.
  • A probe of 95 lines that covers shared against exclusive, the upgrade, a lock shared with a dup, unlocking through the dup, and closing one of the two: two lines change against main, the read and the write through the other open, and no lock line differs from the Linux build.
  • Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 15 over fcntl status flags and 313 over fdopen, run with both sets of libraries: no line differs, apart from a racy file size in the last that changes from run to run with either set.
  • Timing, 200,000 rounds, three passes in two alternating runs each: lock and unlock 1.32 to 1.52 µs with main's libraries and 1.33 to 1.71 µs with the branch's; shared to exclusive and back 2.69 to 2.90 µs and 2.67 to 3.00 µs. No difference I can separate from the spread.
  • 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), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) 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 (211e32b5b): full build, and the six guest_file_lock_* tests 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. The changed file and the guest_file_lock_* tests are Windows only, so this shows only that the Linux build is unaffected.

Not checked on a console or with a commercial title.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5825648638 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4957). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-13T06:27:47Z. --- On Windows, while one descriptor holds a `flock` on a file, reads and writes through any other open of that file fail: with an exclusive lock everything fails, with a shared lock every write. FreeBSD's [flock(2)](https://man.freebsd.org/cgi/man.cgi?query=flock&sektion=2) locks are advisory ("Advisory locks allow cooperating processes to perform consistent operations on files, but do not guarantee consistency (i.e., processes may still access files without using advisory locks possibly resulting in inconsistencies)"), and the Linux build reads and writes normally. A title that locks its save or database file through one descriptor and reads it through another, or through a `FILE*`, gets `EIO` or `EACCES`. This is the gap [docs/dev/debt/libc.md](https://github.com/boykopovar/AnyPS5/blob/e51428bf2/docs/dev/debt/libc.md?plain=1#L59) records: "The host's LockFileEx locks also restrict ordinary reads and writes through other handles, unlike FreeBSD's advisory locks". **Cause.** [`GuestFileLock_nid_no_patch`](https://github.com/boykopovar/AnyPS5/blob/e51428bf2/core/libs/prx/libc/src/FileLock.cpp#L37-L68) locks the whole file (`LockFileEx` from offset 0 for `MAXDWORD:MAXDWORD` bytes), and a Windows byte-range lock is enforced on every read and write that touches the range. Observed on `main` e51428bf2, Windows 11 (10.0.26300), WinLibs GCC 15.2.0 r7, Release. `first` and `other` are two `sceKernelOpen(path, O_RDWR)` of one file; `first` holds the lock: | through `other` | `first` holds `LOCK_EX` | `first` holds `LOCK_SH` | Linux build, both | |---|---|---|---| | `sceKernelPread` | `0x80020005` (EIO) | 4 | 4 | | `sceKernelRead` | `0x8002000D` (EACCES) | 4 | 4 | | `sceKernelPwrite` | `0x80020005` | `0x80020005` | 1 | | `sceKernelWrite`, also past the end | `0x8002000D` | `0x8002000D` | 1 | | `fgetc` on a stream from `fopen(path, "r+")` | -1, errno 13 | not run | the byte | | `flock(other, LOCK_EX\|LOCK_NB)` | -1, errno 35 | -1, errno 35 | -1, errno 35 | 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/libc-flock-advisory-windows`](https://github.com/boykopovar/AnyPS5/compare/main...DavidSamir:AnyPS5:fix/libc-flock-advisory-windows) (e4a1b54ad, three commits on `main` e51428bf2, three files, +18/-3). I can't open it as a PR while #4532 is open (one open PR without a merged one). 1. `fix(libc): make flock advisory on Windows so other descriptors can read and write`: `FileLock.cpp`. 2. `test(libc): read and write through another descriptor while a flock is held`: `guest_file_lock_conversion`. 3. `docs(debt): describe the byte the Windows flock locks`. Windows only. The lock and the unlock take one byte at offset 2^63 - 2 instead of the whole file. No file reaches that offset, so no read or write touches the locked range, while two `flock` calls still meet on the same byte: shared and exclusive locks exclude each other exactly as before, between descriptors, duplicates and processes. Nothing else in the function changes. Not changed: converting a lock from shared to exclusive still releases the first before taking the second, so another descriptor can take it in between; and descriptors that are not disk files fail as they do today. `guest_file_lock_conversion` gains checks in each of its three duplicate kinds: with an exclusive lock held, `sceKernelPwrite`, `sceKernelWrite` and `sceKernelPread` through the other open must succeed and read back what was written, and `flock(LOCK_SH|LOCK_NB)` through it must still fail with errno 35. **Checked on Windows** (e4a1b54ad on `main` e51428bf2, Windows 11 10.0.26300, WinLibs GCC 15.2.0 r7, Release, full builds): - With `FileLock.cpp` as on `main` and the test kept, `guest_file_lock_conversion` fails (exit 0xc0000409, the test's `Require` aborting). With the fix the six `guest_file_lock_*` tests (`duplicate`, `lifetime`, `conversion`, `threads`, `streams`, `process`) pass 5/5 (`--repeat until-fail:5`), and `guest_filesystem`, `guest_kernel_fcntl`, `guest_dup`, `guest_standard_streams`, `file_streams` and `guest_file_vector` pass 3/3. `process` takes the lock from a second process, so the byte is the same one across processes. - The probe behind the table, 54 lines, with `main`'s libraries, the branch's and on the Linux build: ten lines change against `main`, the reads and writes in the table and a round of 2000 lock, contend, write, close, and with the branch every line equals the Linux build's. That includes each of pread, read, pwrite, write, a write past the end, `ftruncate`, `fsync`, `fstat` and a shared and an exclusive `flock` attempt through the other open, with no lock, with an exclusive and with a shared lock held; the holder's own reads and writes; a stream; renaming onto and away from the locked file and unlinking it; and a blocked `flock` that is granted when the holder unlocks 150 ms later. - A probe of 95 lines that covers shared against exclusive, the upgrade, a lock shared with a `dup`, unlocking through the `dup`, and closing one of the two: two lines change against `main`, the read and the write through the other open, and no lock line differs from the Linux build. - Probes of 122 lines over the descriptor functions, 188 over the file, directory, vector and mapping functions, 15 over `fcntl` status flags and 313 over `fdopen`, run with both sets of libraries: no line differs, apart from a racy file size in the last that changes from run to run with either set. - Timing, 200,000 rounds, three passes in two alternating runs each: lock and unlock 1.32 to 1.52 µs with `main`'s libraries and 1.33 to 1.71 µs with the branch's; shared to exclusive and back 2.69 to 2.90 µs and 2.67 to 3.00 µs. No difference I can separate from the spread. - 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), OpenNOW (PPSA99082) and ProsperoRadio (PPSA99001) 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 (211e32b5b): full build, and the six `guest_file_lock_*` tests 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. The changed file and the `guest_file_lock_*` tests are Windows only, so this shows only that the Linux build is unaffected. Not checked on a console or with a commercial title. AI-assisted: yes
milescoviello commented 2026-10-13 06:31:26 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-13T06:31:26Z.


Carried in #4958, keeping your three commits and authorship. I added a Linux run (Ubuntu 24.04, GCC 13.3): the build is clean and all 327 guest_* tests pass in two runs, and a MinGW-w64 -fsyntax-only -Wall -Wextra compile of FileLock.cpp and GuestFileLock.cpp is clean. The Windows results in the PR are yours from this issue. Thanks for the branch.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6139740183 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/4957#issuecomment-6139740183). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-13T06:31:26Z. --- Carried in #4958, keeping your three commits and authorship. I added a Linux run (Ubuntu 24.04, GCC 13.3): the build is clean and all 327 `guest_*` tests pass in two runs, and a MinGW-w64 `-fsyntax-only -Wall -Wextra` compile of `FileLock.cpp` and `GuestFileLock.cpp` is clean. 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#4957
No description provided.