feat(libkernel): implement fcntl record locks on Windows #4480

Merged
milescoviello merged 1 commit from upstream-pr/4480 into main 2026-10-13 02:36:04 +00:00
milescoviello commented 2026-10-12 13:09:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-12T13:09:41Z.


What

Fixes #4479. Carries @screamm's fix commit from that issue (screamm:feat/libkernel-record-locks-windows, b7cc2856), keeping the commit and its authorship, as the issue invites. The branch sits on top of #3649's two commits, which this commit does not need (#3649 is only how the titles reach the call); it cherry-picks onto current main without changes and builds there.

On Windows, fcntl record locks (F_GETLK, F_SETLK, F_SETLKW and the old F_OGETLK/F_OSETLK/F_OSETLKW forms) throw fcntl: record locks are not implemented on Windows, which #3840 recorded as a limit. With #3649 applied, ProsperoRadio (PPSA99001) stops there on F_SETLK, and ProsperoTV (PPSA99003) does too once #4478's fix is applied. This commit applies FreeBSD 11.4's argument checks in FreeBSD's order (kern_fcntl, lf_advlockasync) and then follows lf_blocks, where only locks of different owners conflict: every lock belongs to the one guest process, so F_SETLK and F_SETLKW succeed without waiting and F_GETLK reports F_UNLCK. Nothing is held on the host, because Windows byte-range locks are mandatory and would fail the guest's own reads and writes through its other descriptors; the fcntl TechnicalDebt entry records that other host processes neither see nor are seen by these locks. A SEEK_END lock on a directory and locks on character devices still throw. Linux is unchanged.

guest_kernel_fcntl replaces the old Windows "throws" expectation with the round trip and adds RecordLocksBelongToTheProcess (overlap merge and split, two descriptors on one range, another thread's F_SETLK/F_SETLKW/F_GETLK, SEEK_CUR/SEEK_END, negative lengths, the 24-byte old struct, EINVAL, EBADF on read-only and write-only descriptors, relock after close); the EOVERFLOW, error-order, F_GETLK write-back and directory cases are Windows-only.

Tested

  • From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: guest_kernel_fcntl, guest_poll, guest_dup, guest_file_vector 3/3; 620/620 of ctest -j8 -E "^agc_driver_|^video_out_flip"; ProsperoRadio relinked against the build (with #3649) gets past the fcntl stop and reaches #4478's.
  • screamm's combined Windows run (main 33ae282e + #3649 + #4478's fix + this commit): ProsperoRadio and ProsperoTV get past poll and the record locks and stop next on the graphics side (#4030).
  • Linux, added for this carry (the issue left the shared test part to Linux CI): Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on main 33ae282e. guest_kernel_fcntl, guest_poll, guest_dup and guest_file_vector pass, and all 306 guest_* tests pass. The new shared RecordLocksBelongToTheProcess cases also pass with main's DescriptorControl.cpp, as expected since Linux forwards record locks to the host. I could not run the Windows path myself.
  • Windows run of this head by @LeandroLP23 (comment): Windows 11, WinLibs GCC 15.2.0, Release: guest_kernel_fcntl 3/3 and fails with main's DescriptorControl.cpp (fcntl: record locks are not implemented on Windows, command=12, fd=3); ctest -E '^agc_' 554/554; ProsperoRadio with #3649 gets past the database's record lock to its HTTP code.
  • Title run by @babyhuey (comment), Windows 11, WinLibs GCC 15.2: ProsperoTV (PPSA99003) on main d1fd9372 + #4525 stops at what(): fcntl: record locks are not implemented on Windows, command=12, fd=4 (F_SETLK on a regular file); with this PR on top it gets past the lock and renders its menu for the full 60 s with no throw.
  • tools/check_conventions.py --base upstream/main passes.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: PPSA99003 (ProsperoTV) on Windows together with #4525, and PPSA99001 (ProsperoRadio) together with #3649; I found no Game bring-up discussion for them
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4829882916 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4480). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-12T13:09:41Z. --- ### What Fixes #4479. Carries @screamm's fix commit from that issue (`screamm:feat/libkernel-record-locks-windows`, `b7cc2856`), keeping the commit and its authorship, as the issue invites. The branch sits on top of #3649's two commits, which this commit does not need (#3649 is only how the titles reach the call); it cherry-picks onto current `main` without changes and builds there. On Windows, `fcntl` record locks (`F_GETLK`, `F_SETLK`, `F_SETLKW` and the old `F_OGETLK`/`F_OSETLK`/`F_OSETLKW` forms) throw `fcntl: record locks are not implemented on Windows`, which #3840 recorded as a limit. With #3649 applied, ProsperoRadio (PPSA99001) stops there on `F_SETLK`, and ProsperoTV (PPSA99003) does too once #4478's fix is applied. This commit applies FreeBSD 11.4's argument checks in FreeBSD's order (`kern_fcntl`, `lf_advlockasync`) and then follows `lf_blocks`, where only locks of different owners conflict: every lock belongs to the one guest process, so `F_SETLK` and `F_SETLKW` succeed without waiting and `F_GETLK` reports `F_UNLCK`. Nothing is held on the host, because Windows byte-range locks are mandatory and would fail the guest's own reads and writes through its other descriptors; the fcntl TechnicalDebt entry records that other host processes neither see nor are seen by these locks. A `SEEK_END` lock on a directory and locks on character devices still throw. Linux is unchanged. `guest_kernel_fcntl` replaces the old Windows "throws" expectation with the round trip and adds `RecordLocksBelongToTheProcess` (overlap merge and split, two descriptors on one range, another thread's `F_SETLK`/`F_SETLKW`/`F_GETLK`, `SEEK_CUR`/`SEEK_END`, negative lengths, the 24-byte old struct, `EINVAL`, `EBADF` on read-only and write-only descriptors, relock after close); the `EOVERFLOW`, error-order, `F_GETLK` write-back and directory cases are Windows-only. ### Tested - From the issue: Windows 11, WinLibs MinGW-w64 GCC 15.2.0, Release: `guest_kernel_fcntl`, `guest_poll`, `guest_dup`, `guest_file_vector` 3/3; 620/620 of `ctest -j8 -E "^agc_driver_|^video_out_flip"`; ProsperoRadio relinked against the build (with #3649) gets past the `fcntl` stop and reaches #4478's. - screamm's [combined Windows run](https://github.com/boykopovar/AnyPS5/issues/4479#issuecomment-6125309351) (`main` `33ae282e` + #3649 + #4478's fix + this commit): ProsperoRadio and ProsperoTV get past `poll` and the record locks and stop next on the graphics side (#4030). - Linux, added for this carry (the issue left the shared test part to Linux CI): Ubuntu 24.04, GCC 13.3 (CI's compiler), Release with CI's configure flags, on `main` 33ae282e. `guest_kernel_fcntl`, `guest_poll`, `guest_dup` and `guest_file_vector` pass, and all 306 `guest_*` tests pass. The new shared `RecordLocksBelongToTheProcess` cases also pass with `main`'s `DescriptorControl.cpp`, as expected since Linux forwards record locks to the host. I could not run the Windows path myself. - Windows run of this head by @LeandroLP23 ([comment](https://github.com/boykopovar/AnyPS5/pull/4480#issuecomment-6131572014)): Windows 11, WinLibs GCC 15.2.0, Release: `guest_kernel_fcntl` 3/3 and fails with `main`'s `DescriptorControl.cpp` (`fcntl: record locks are not implemented on Windows, command=12, fd=3`); `ctest -E '^agc_'` 554/554; ProsperoRadio with #3649 gets past the database's record lock to its HTTP code. - Title run by @babyhuey ([comment](https://github.com/boykopovar/AnyPS5/pull/4480#issuecomment-6136111414)), Windows 11, WinLibs GCC 15.2: ProsperoTV (PPSA99003) on `main` d1fd9372 + #4525 stops at `what(): fcntl: record locks are not implemented on Windows, command=12, fd=4` (`F_SETLK` on a regular file); with this PR on top it gets past the lock and renders its menu for the full 60 s with no throw. - `tools/check_conventions.py --base upstream/main` passes. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: PPSA99003 (ProsperoTV) on Windows together with #4525, and PPSA99001 (ProsperoRadio) together with #3649; I found no Game bring-up discussion for them - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
LeandroLP23 commented 2026-10-12 19:55:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by LeandroLP23 on 2026-10-12T19:55:10Z.


The PR body says the Windows path wasn't run for this carry, so I ran it. Windows 11 (26300), WinLibs GCC 15.2.0, Ninja, Release, BUILD_TESTING=ON, head 43ffba87e merged onto main 2f4cc8983.

Tests

  • guest_kernel_fcntl: 3/3.
  • ctest -E '^agc_' -j 4: 554/554.
  • With main's DescriptorControl.cpp restored, guest_kernel_fcntl fails with fcntl: record locks are not implemented on Windows, command=12, fd=3. That is the same message as in #4479.

ProsperoRadio (PPSA99001, catalog 01.000.010), 60 s runs with --windows:

Build Result
main + #3649 exit 0xc0000409 after 40 s. download0/prosperoradio.log ends at the sceKernelOpen of /download0/radio-browser.sqlite3 (125 lines)
main + #3649 + this PR exit 0xc0000409 after 36 s. The log goes past the database to [ProsperoRadio][http] non-blocking sockets: NOT set (143 lines)

The title's log redirect doesn't keep the what() line in my runs, so the table shows how far the log gets, not the stop message. That fits the description: the title gets past the record lock on the database and stops later in its HTTP code, where #4479 expects the POLLPRI stop (#4525).

ProsperoTV (PPSA99003) exits after 10 s both with and without this PR. It needs #4525 first, and #4525 doesn't merge together with #3649: both change Socket/src/Udp.cpp and tests/GuestPoll.cpp. So I couldn't build the combination for it.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6131572014 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4480#issuecomment-6131572014). Originally posted by [LeandroLP23](https://github.com/LeandroLP23) on 2026-10-12T19:55:10Z. --- The PR body says the Windows path wasn't run for this carry, so I ran it. Windows 11 (26300), WinLibs GCC 15.2.0, Ninja, Release, `BUILD_TESTING=ON`, head `43ffba87e` merged onto `main` `2f4cc8983`. **Tests** - `guest_kernel_fcntl`: 3/3. - `ctest -E '^agc_' -j 4`: 554/554. - With `main`'s `DescriptorControl.cpp` restored, `guest_kernel_fcntl` fails with `fcntl: record locks are not implemented on Windows, command=12, fd=3`. That is the same message as in #4479. **ProsperoRadio** (PPSA99001, catalog 01.000.010), 60 s runs with `--windows`: | Build | Result | |---|---| | `main` + #3649 | exit `0xc0000409` after 40 s. `download0/prosperoradio.log` ends at the `sceKernelOpen` of `/download0/radio-browser.sqlite3` (125 lines) | | `main` + #3649 + this PR | exit `0xc0000409` after 36 s. The log goes past the database to `[ProsperoRadio][http] non-blocking sockets: NOT set` (143 lines) | The title's log redirect doesn't keep the `what()` line in my runs, so the table shows how far the log gets, not the stop message. That fits the description: the title gets past the record lock on the database and stops later in its HTTP code, where #4479 expects the `POLLPRI` stop (#4525). ProsperoTV (PPSA99003) exits after 10 s both with and without this PR. It needs #4525 first, and #4525 doesn't merge together with #3649: both change `Socket/src/Udp.cpp` and `tests/GuestPoll.cpp`. So I couldn't build the combination for it. AI-assisted: yes (Claude Code)
Dextroid17 commented 2026-10-12 22:11:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-12T22:11:11Z.

Review: APPROVED


Review of #4480 (fcntl record locks on Windows), head 43ffba87e

Verdict: approve, relying on LeandroLP23's Windows run for the host path. One non-blocking note.

Scope first: I ran Linux only. On Linux the #else branch uses the host's POSIX locks, so the Windows ProcessRecordLock is not compiled there. What I could check is the range/ordering/errno logic, by copying it into a stub harness.

What I ran (PR head merged onto upstream/main ee4962190, clean env):

  • guest_kernel_fcntl_tests (Linux build): 10/10 runs exit 0.
  • Differential check of the range/ordering logic against my own transcription of FreeBSD fcntl() + lf_advlockasync() (freebsd-src 8c639945a): 2,352,000 cases (F_GETLK/F_SETLK, lock types incl. unknown, whence incl. invalid, start/length at int64 extremes, open mode, file size, file offset >= 0 only), built with UBSan. 0 mismatches. LockRangeError and the ProcessRecordLock checks are copied from the PR with the host calls stubbed. The reference is only as independent as my reading of FreeBSD, and it covers range and error order only, not F_SETLKW blocking, F_UNLCKSYS or the HostWrite write-back.
  • Mutation controls: dropping the length - 1 overflow term gives 6,912 mismatches; dropping the F_GETLK unknown-type check gives 241,704; dropping the start == 0 guard gives 0 (when length < 0, start + length < 0 already catches it, so the guard is redundant but harmless).
  • git merge-tree of this PR with #4555 (also edits DescriptorControl.cpp InitialEntry) is clean.

From LeandroLP23's comment, not run by me: Windows 554/554, guest_kernel_fcntl 3/3, and the failure message with main's DescriptorControl.cpp restored. In their ProsperoRadio runs the title exits 0xc0000409 both before and after this PR; with the PR the log gets past the database open to the HTTP code.

Not checked: the "SEEK_END on a directory still throws" case (the test asserts it under _WIN32 only).

Note: the start == 0 guard in LockRangeError is redundant when length < 0. Fine to leave.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5496479897 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4480#pullrequestreview-5496479897). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-12T22:11:11Z. **Review:** APPROVED --- Review of #4480 (fcntl record locks on Windows), head 43ffba87e Verdict: approve, relying on LeandroLP23's Windows run for the host path. One non-blocking note. Scope first: I ran Linux only. On Linux the `#else` branch uses the host's POSIX locks, so the Windows `ProcessRecordLock` is not compiled there. What I could check is the range/ordering/errno logic, by copying it into a stub harness. What I ran (PR head merged onto upstream/main ee4962190, clean env): - guest_kernel_fcntl_tests (Linux build): 10/10 runs exit 0. - Differential check of the range/ordering logic against my own transcription of FreeBSD fcntl() + lf_advlockasync() (freebsd-src 8c639945a): 2,352,000 cases (F_GETLK/F_SETLK, lock types incl. unknown, whence incl. invalid, start/length at int64 extremes, open mode, file size, file offset >= 0 only), built with UBSan. 0 mismatches. LockRangeError and the ProcessRecordLock checks are copied from the PR with the host calls stubbed. The reference is only as independent as my reading of FreeBSD, and it covers range and error order only, not F_SETLKW blocking, F_UNLCKSYS or the HostWrite write-back. - Mutation controls: dropping the `length - 1` overflow term gives 6,912 mismatches; dropping the F_GETLK unknown-type check gives 241,704; dropping the `start == 0` guard gives 0 (when length < 0, `start + length < 0` already catches it, so the guard is redundant but harmless). - `git merge-tree` of this PR with #4555 (also edits DescriptorControl.cpp InitialEntry) is clean. From LeandroLP23's comment, not run by me: Windows 554/554, guest_kernel_fcntl 3/3, and the failure message with main's DescriptorControl.cpp restored. In their ProsperoRadio runs the title exits 0xc0000409 both before and after this PR; with the PR the log gets past the database open to the HTTP code. Not checked: the "SEEK_END on a directory still throws" case (the test asserts it under _WIN32 only). Note: the `start == 0` guard in LockRangeError is redundant when length < 0. Fine to leave.
babyhuey commented 2026-10-13 01:30:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-13T01:30:49Z.


A title that needs this: ProsperoTV (PPSA99003) on Windows 11 (WinLibs GCC 15.2, RTX 3080). On main d1fd9372 + #4525 (POLLPRI under WSAPoll) it gets into its HUI and then stops at:

what():  fcntl: record locks are not implemented on Windows, command=12, fd=4

That's F_SETLK on a regular file. With this PR stacked on top (fork test/verify-tvlock, c28bb5e0; merges clean), it gets past the lock and renders its menu for the full 60 s with no throw: [TV] splash hidden=1, then [TV] frames=600 mean=23.50ms max=25.38ms ... draws=2 shapes=407. guest_kernel_fcntl passes 5/5 on that head, with no warnings in DescriptorControl.cpp or GuestKernelFcntl.cpp.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6136111414 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/4480#issuecomment-6136111414). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-13T01:30:49Z. --- A title that needs this: ProsperoTV (PPSA99003) on Windows 11 (WinLibs GCC 15.2, RTX 3080). On `main` d1fd9372 + #4525 (POLLPRI under WSAPoll) it gets into its HUI and then stops at: ``` what(): fcntl: record locks are not implemented on Windows, command=12, fd=4 ``` That's F_SETLK on a regular file. With this PR stacked on top (fork `test/verify-tvlock`, c28bb5e0; merges clean), it gets past the lock and renders its menu for the full 60 s with no throw: `[TV] splash hidden=1`, then `[TV] frames=600 mean=23.50ms max=25.38ms ... draws=2 shapes=407`. `guest_kernel_fcntl` passes 5/5 on that head, with no warnings in `DescriptorControl.cpp` or `GuestKernelFcntl.cpp`.
Sign in to join this conversation.
No reviewers
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!4480
No description provided.