fix(libkernel): give flock BSD semantics on Windows #1755

Merged
Zurisen merged 2 commits from upstream-pr/1755 into main 2026-10-10 05:53:55 +00:00
Zurisen commented 2026-10-08 18:33:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-08T18:33:42Z.


What

Fixes #1754. On Windows, flock now tracks the one lock each descriptor holds (FileLock.cpp), as FreeBSD flock(2) defines it:

  • LOCK_UN without a lock and a repeat of the held mode return 0.
  • A conversion between LOCK_SH and LOCK_EX releases the held lock and takes the new one. As on FreeBSD, the conversion is not atomic, and a failed LOCK_NB conversion leaves no lock.
  • close and sceKernelClose forget the descriptor's lock, so a reused descriptor number starts unlocked.
  • The operation is decoded as FreeBSD sys_flock does, before either host sees it: LOCK_UN over LOCK_EX over LOCK_SH, other bits ignored, and an operation with none of the three fails with EBADF. Before, Linux rejected such operations with EINVAL and Windows took them as a shared lock. An invalid descriptor sets EBADF.
  • The state lock is not held during a blocking LockFileEx, so a wait on one descriptor does not block flock on others.

Contention still throws from flock_nid_postfix, as before and as on Linux. On Linux only the operation decoding changes.

Tested

Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, RTX 4070 Laptop:

  • guest_filesystem gains flock cases: unlock without a lock, repeated LOCK_EX, LOCK_EX → LOCK_SH → LOCK_EX conversions, shared locks on two descriptors, repeated LOCK_SH released by one LOCK_UN, descriptor reuse after close and sceKernelClose, and malformed operations (0 and LOCK_NB | 0x10 fail with EBADF, LOCK_EX | LOCK_NB | 0x10 locks, LOCK_UN | LOCK_EX unlocks).
  • On main the new cases abort at the first LOCK_UN (error=158); with the change they pass. Without the decoding commit the malformed cases abort (error=87).
  • Full ctest with ANYPS5_REQUIRE_VULKAN=1 --repeat until-fail:3: 506/507 passed on d70b8998. libs target builds. The failure, pr_overlap_encoding, also fails on main locally: it patches subprocess._text_encoding, which Python 3.10 does not have.
  • Linux: not run locally. CI Linux passed guest_filesystem on the earlier head, and nauroth ran it on Ubuntu 24.04 in #1764.

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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4791197914 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-08T18:33:42Z. --- ### What Fixes #1754. On Windows, `flock` now tracks the one lock each descriptor holds ([FileLock.cpp](core/libs/prx/libkernel/File/src/FileLock.cpp)), as FreeBSD `flock(2)` defines it: - `LOCK_UN` without a lock and a repeat of the held mode return 0. - A conversion between `LOCK_SH` and `LOCK_EX` releases the held lock and takes the new one. As on FreeBSD, the conversion is not atomic, and a failed `LOCK_NB` conversion leaves no lock. - `close` and `sceKernelClose` forget the descriptor's lock, so a reused descriptor number starts unlocked. - The operation is decoded as FreeBSD [`sys_flock`](https://github.com/freebsd/freebsd-src/blob/39f7dbff606d/sys/kern/kern_descrip.c#L4053-L4066) does, before either host sees it: `LOCK_UN` over `LOCK_EX` over `LOCK_SH`, other bits ignored, and an operation with none of the three fails with `EBADF`. Before, Linux rejected such operations with `EINVAL` and Windows took them as a shared lock. An invalid descriptor sets `EBADF`. - The state lock is not held during a blocking `LockFileEx`, so a wait on one descriptor does not block `flock` on others. Contention still throws from `flock_nid_postfix`, as before and as on Linux. On Linux only the operation decoding changes. ### Tested Windows 11 (10.0.26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, RTX 4070 Laptop: - `guest_filesystem` gains `flock` cases: unlock without a lock, repeated `LOCK_EX`, `LOCK_EX` → `LOCK_SH` → `LOCK_EX` conversions, shared locks on two descriptors, repeated `LOCK_SH` released by one `LOCK_UN`, descriptor reuse after `close` and `sceKernelClose`, and malformed operations (`0` and `LOCK_NB | 0x10` fail with `EBADF`, `LOCK_EX | LOCK_NB | 0x10` locks, `LOCK_UN | LOCK_EX` unlocks). - On main the new cases abort at the first `LOCK_UN` (`error=158`); with the change they pass. Without the decoding commit the malformed cases abort (`error=87`). - Full `ctest` with `ANYPS5_REQUIRE_VULKAN=1 --repeat until-fail:3`: 506/507 passed on `d70b8998`. `libs` target builds. The failure, `pr_overlap_encoding`, also fails on main locally: it patches `subprocess._text_encoding`, which Python 3.10 does not have. - Linux: not run locally. CI Linux passed `guest_filesystem` on the earlier head, and nauroth ran it on Ubuntu 24.04 in #1764. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [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] AI-assisted: yes
MarcZX10 commented 2026-10-08 20:58:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T20:58:54Z.


The Linux Build run for this PR has one failure: agc_driver_buffer_format timed out; the other tests passed. This is the slow lavapipe case tracked in #1762, so it may be unrelated to this Windows-only change. The comparison Build run on main aa277b3 (37840594063) was cancelled during cmake --build before CTest started. A new Build run on current main 0447793 (37844230361) is queued and may provide a CTest baseline. This PR head is 21 commits behind current main; CONTRIBUTING asks to keep the branch current, so please rebase and rerun the final checks.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6068936867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6068936867). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T20:58:54Z. --- The Linux Build run for this PR has one failure: `agc_driver_buffer_format` timed out; the other tests passed. This is the slow lavapipe case tracked in #1762, so it may be unrelated to this Windows-only change. The comparison Build run on `main` aa277b3 (37840594063) was cancelled during `cmake --build` before CTest started. A new Build run on current `main` 0447793 (37844230361) is queued and may provide a CTest baseline. This PR head is 21 commits behind current `main`; CONTRIBUTING asks to keep the branch current, so please rebase and rerun the final checks.
MarcZX10 commented 2026-10-08 21:36:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:36:04Z.


Update to the baseline noted above: the Build run on main 364a56b (37845561389) was cancelled before Linux CTest completed when #1826 advanced main. The replacement main Build on aa258760 (37847684141) is running. The PR Linux failure remains agc_driver_buffer_format; there is still no completed current-main baseline to determine whether it reproduces without this PR.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069513198 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069513198). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:36:04Z. --- Update to the baseline noted above: the Build run on `main` `364a56b` (`37845561389`) was cancelled before Linux CTest completed when #1826 advanced `main`. The replacement main Build on `aa258760` (`37847684141`) is running. The PR Linux failure remains `agc_driver_buffer_format`; there is still no completed current-main baseline to determine whether it reproduces without this PR.
MarcZX10 commented 2026-10-08 21:37:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:37:13Z.


Further baseline update: main advanced to dc79e9b1 when #938 merged, cancelling the aa258760 Build run (37847684141) before CTest. A replacement Build (37847982017) is queued on dc79e9b1. There is still no completed current-main Linux baseline for agc_driver_buffer_format.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069529974 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069529974). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:37:13Z. --- Further baseline update: `main` advanced to `dc79e9b1` when #938 merged, cancelling the `aa258760` Build run (`37847684141`) before CTest. A replacement Build (`37847982017`) is queued on `dc79e9b1`. There is still no completed current-main Linux baseline for `agc_driver_buffer_format`.
MarcZX10 commented 2026-10-08 21:43:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:43:05Z.


One more main-baseline update: main advanced to 94b1eecb (#533), cancelling the dc79e9b1 Build (37847982017) before CTest finished. The replacement Build (37848560105) is running on 94b1eecb. No completed current-main comparison is available yet.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069618898 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069618898). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:43:05Z. --- One more main-baseline update: `main` advanced to `94b1eecb` (#533), cancelling the `dc79e9b1` Build (`37847982017`) before CTest finished. The replacement Build (`37848560105`) is running on `94b1eecb`. No completed current-main comparison is available yet.
MarcZX10 commented 2026-10-08 21:46:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:46:19Z.


Additional local evidence for #1762: after rebuilding agc_driver_buffer_format_tests on Debian 12/GCC 12.2 with llvmpipe LLVM 15.0.6, ctest -R ^agc_driver_buffer_format$ --timeout 120 passed in 1.40 s (ANYPS5_NO_SHADER_CACHE=1). This does not reproduce the 93–108 s GitHub runner timing and cannot establish whether the PR failure is baseline behavior. The latest-main Build baseline is still running on 94b1eecb.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069664802 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069664802). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:46:19Z. --- Additional local evidence for #1762: after rebuilding `agc_driver_buffer_format_tests` on Debian 12/GCC 12.2 with llvmpipe LLVM 15.0.6, `ctest -R ^agc_driver_buffer_format$ --timeout 120` passed in 1.40 s (`ANYPS5_NO_SHADER_CACHE=1`). This does not reproduce the 93–108 s GitHub runner timing and cannot establish whether the PR failure is baseline behavior. The latest-main Build baseline is still running on `94b1eecb`.
MarcZX10 commented 2026-10-08 21:51:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:51:44Z.


The current-main baseline has completed: Build 37848560105 on 94b1eecb passed Linux and Windows, and Linux CTest passed 492/492. agc_driver_buffer_format passed all three --repeat until-fail:3 --parallel 4 runs in 16.03 s, 15.97 s and 16.03 s with ANYPS5_NO_SHADER_CACHE=1. This does not reproduce the earlier PR-branch timeout. Please rebase this PR onto current main and rerun its checks to determine whether the old-head timeout remains.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069740221 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069740221). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:51:44Z. --- The current-main baseline has completed: Build `37848560105` on `94b1eecb` passed Linux and Windows, and Linux CTest passed 492/492. `agc_driver_buffer_format` passed all three `--repeat until-fail:3 --parallel 4` runs in 16.03 s, 15.97 s and 16.03 s with `ANYPS5_NO_SHADER_CACHE=1`. This does not reproduce the earlier PR-branch timeout. Please rebase this PR onto current `main` and rerun its checks to determine whether the old-head timeout remains.
MarcZX10 commented 2026-10-08 21:55:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-08T21:55:33Z.


Current-base update: this PR head is now 30 commits behind main 94b1eecb (merge base 4ddbf049). The latest-main Build completed successfully (37848560105); agc_driver_buffer_format passed its three parallel CI repeats in about 16 s each. Please rebase this Windows flock change onto current main and rerun CI; the old head’s timeout does not match the current-main result.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069792531 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6069792531). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-08T21:55:33Z. --- Current-base update: this PR head is now 30 commits behind `main` `94b1eecb` (merge base `4ddbf049`). The latest-main Build completed successfully (`37848560105`); `agc_driver_buffer_format` passed its three parallel CI repeats in about 16 s each. Please rebase this Windows flock change onto current `main` and rerun CI; the old head’s timeout does not match the current-main result.
Zurisen commented 2026-10-09 06:38:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-09T06:38:47Z.


Rebased onto f1451396; the full suite passes locally on Windows (488/488). CI re-runs on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6075776185 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6075776185). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-09T06:38:47Z. --- Rebased onto `f1451396`; the full suite passes locally on Windows (488/488). CI re-runs on the new head.
MarcZX10 commented 2026-10-09 13:45:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T13:45:59Z.


One Windows/Linux parity case is missing in File::Flock: mode masks the three operation bits, and the function only tests that masked value, so any other bits are silently ignored. For example, LOCK_EX | 0x10 acquires an exclusive lock on Windows, while the native Linux flock rejects an invalid operation with EINVAL. Please reject operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN) and add a regression case; otherwise malformed guest flags behave differently by host. Linux documents EINVAL for an invalid op: https://www.man7.org/linux/man-pages/man2/flock.2.html.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082188073 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6082188073). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T13:45:59Z. --- One Windows/Linux parity case is missing in `File::Flock`: `mode` masks the three operation bits, and the function only tests that masked value, so any other bits are silently ignored. For example, `LOCK_EX | 0x10` acquires an exclusive lock on Windows, while the native Linux `flock` rejects an invalid operation with `EINVAL`. Please reject `operation & ~(LOCK_SH | LOCK_EX | LOCK_NB | LOCK_UN)` and add a regression case; otherwise malformed guest flags behave differently by host. Linux documents `EINVAL` for an invalid `op`: https://www.man7.org/linux/man-pages/man2/flock.2.html.
Zurisen commented 2026-10-09 16:22:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-09T16:22:17Z.


@MarcZX100 Agreed that the masked bits made the hosts differ. I matched FreeBSD rather than Linux, since the console's flock is FreeBSD's sys_flock: it takes LOCK_UN over LOCK_EX over LOCK_SH, ignores other bits, and fails with EBADF when none of the three is set. Rejecting unknown bits with EINVAL would move both hosts away from the console.

flock_nid_postfix now decodes the operation that way before either host sees it (e9f28257), so Windows, Linux and the console agree. Regression cases in guest_filesystem: 0 and LOCK_NB | 0x10 fail with EBADF, LOCK_EX | LOCK_NB | 0x10 locks, and LOCK_UN | LOCK_EX unlocks; they abort without the commit. Rebased onto d70b8998; 506/507 pass locally on Windows, the failure being pr_overlap_encoding, which also fails on main with Python 3.10.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6084870476 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6084870476). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-09T16:22:17Z. --- @MarcZX100 Agreed that the masked bits made the hosts differ. I matched FreeBSD rather than Linux, since the console's `flock` is FreeBSD's [`sys_flock`](https://github.com/freebsd/freebsd-src/blob/39f7dbff606d/sys/kern/kern_descrip.c#L4053-L4066): it takes `LOCK_UN` over `LOCK_EX` over `LOCK_SH`, ignores other bits, and fails with `EBADF` when none of the three is set. Rejecting unknown bits with `EINVAL` would move both hosts away from the console. `flock_nid_postfix` now decodes the operation that way before either host sees it (e9f28257), so Windows, Linux and the console agree. Regression cases in `guest_filesystem`: `0` and `LOCK_NB | 0x10` fail with `EBADF`, `LOCK_EX | LOCK_NB | 0x10` locks, and `LOCK_UN | LOCK_EX` unlocks; they abort without the commit. Rebased onto `d70b8998`; 506/507 pass locally on Windows, the failure being `pr_overlap_encoding`, which also fails on main with Python 3.10.
Zurisen commented 2026-10-10 04:58:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Zurisen on 2026-10-10T04:58:42Z.


Rebased onto bc40a6b5 to resolve the include conflict with #1727 in Open.cpp (kept both includes). Full suite locally on Windows: 604/606. The failures are pr_overlap_encoding, which also fails on main with Python 3.10, and one video_out_flip_controls crash under --parallel (#2520); the latter passes 20/20 alone.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093990861 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6093990861). Originally posted by [Zurisen](https://github.com/Zurisen) on 2026-10-10T04:58:42Z. --- Rebased onto `bc40a6b5` to resolve the include conflict with #1727 in `Open.cpp` (kept both includes). Full suite locally on Windows: 604/606. The failures are `pr_overlap_encoding`, which also fails on main with Python 3.10, and one `video_out_flip_controls` crash under `--parallel` (#2520); the latter passes 20/20 alone.
github-actions[bot] commented 2026-10-10 05:40:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-10T05:40:48Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#2561 core/libs/prx/libkernel/File/include/FileLock.hpp
core/libs/prx/libkernel/File/src/FileLock.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6094298775 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1755#issuecomment-6094298775). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-10T05:40:48Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #2561 | | | `core/libs/prx/libkernel/File/include/FileLock.hpp`<br>`core/libs/prx/libkernel/File/src/FileLock.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!1755
No description provided.