feat(libkernel_sys): implement sceKernelFlock over flock #9097

Merged
DavidSamir merged 8 commits from upstream-pr/9097 into main 2026-11-16 01:07:10 +00:00
DavidSamir commented 2026-10-21 12:10:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T12:10:45Z.


What

sceKernelFlock was a discovery stub in libkernel_sys (Export.Discovery.cpp) that threw NotImplemented. It now wraps libkernel's flock, which already implements FreeBSD 11 flock(2): shared and exclusive locks, LOCK_NB, LOCK_UN, EBADF for a bad descriptor and EOPNOTSUPP for sockets and pipes.

The SCE form returns 0 on success, or SceKernelError of the guest errno that flock set. This is the mapping the other sceKernel* wrappers of guest POSIX calls use, for example sceKernelClose over a socket (Open.cpp) and the wrapper at Stdio.cpp:1290. Stdio.cpp's SceErrorFromErrno takes a host errno, which would turn the guest EWOULDBLOCK (35) into EIO on Windows, so it isn't used here.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. guest_kernel_sys gains SceKernelFlock:

  • an exclusive non-blocking lock through one descriptor returns 0;
  • the same lock through a second descriptor of the file returns 0x80020023 (EWOULDBLOCK);
  • after LOCK_UN on the first, the second descriptor's lock succeeds, then unlocks;
  • descriptor -1 returns 0x80020009 (EBADF).

On main the test aborts with sceKernelFlock not implemented. On this branch it passes. tools/check_conventions.py --base origin/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 under Silent stubs in their library's technical debt file
  • 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
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4951316906 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T12:10:45Z. --- ### What `sceKernelFlock` was a discovery stub in `libkernel_sys` (`Export.Discovery.cpp`) that threw `NotImplemented`. It now wraps libkernel's `flock`, which already implements FreeBSD 11 `flock(2)`: shared and exclusive locks, `LOCK_NB`, `LOCK_UN`, `EBADF` for a bad descriptor and `EOPNOTSUPP` for sockets and pipes. The SCE form returns 0 on success, or `SceKernelError` of the guest `errno` that `flock` set. This is the mapping the other `sceKernel*` wrappers of guest POSIX calls use, for example `sceKernelClose` over a socket (`Open.cpp`) and the wrapper at `Stdio.cpp:1290`. `Stdio.cpp`'s `SceErrorFromErrno` takes a host `errno`, which would turn the guest `EWOULDBLOCK` (35) into `EIO` on Windows, so it isn't used here. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. `guest_kernel_sys` gains `SceKernelFlock`: - an exclusive non-blocking lock through one descriptor returns 0; - the same lock through a second descriptor of the file returns `0x80020023` (`EWOULDBLOCK`); - after `LOCK_UN` on the first, the second descriptor's lock succeeds, then unlocks; - descriptor -1 returns `0x80020009` (`EBADF`). On `main` the test aborts with `sceKernelFlock not implemented`. On this branch it passes. `tools/check_conventions.py --base origin/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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
xXPBSXx commented 2026-10-21 14:18:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T14:18:04Z.


Linux data point (WSL2 Ubuntu 24.04, virtualised kernel, not bare-metal; GCC 13.3, Release): at head 41ab6b4c1 guest_kernel_sys aborts with Kernel sys check failed at line 264 (guest errno 2), which is Require(first >= 0 && second >= 0) in SceKernelFlock; at the merge base 927076049 the same test passes 10 of 10 runs. The cause is the path: std::filesystem::temp_directory_path() / "anyps5-sce-flock.bin" is an absolute host path, and on Linux the guest resolver does not map it, so open_nid_postfix(path, 0x602, 0644) answers ENOENT (a standalone probe shows the same for flags 0x602, 0x202, 0x2 and 0x201 on that path; it never reaches sceKernelFlock). With only that line changed to a relative name, const std::string path = "anyps5-sce-flock.bin";, guest_kernel_sys passes 10 of 10 runs on Linux, so the sceKernelFlock expectations themselves (EWOULDBLOCK for the second descriptor, the unlock hand-over, EBADF for -1) hold there. Not tested: macOS, a console, Windows.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6318342007 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#issuecomment-6318342007). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T14:18:04Z. --- Linux data point (WSL2 Ubuntu 24.04, virtualised kernel, not bare-metal; GCC 13.3, Release): at head 41ab6b4c1 `guest_kernel_sys` aborts with `Kernel sys check failed at line 264 (guest errno 2)`, which is `Require(first >= 0 && second >= 0)` in `SceKernelFlock`; at the merge base 927076049 the same test passes 10 of 10 runs. The cause is the path: `std::filesystem::temp_directory_path() / "anyps5-sce-flock.bin"` is an absolute host path, and on Linux the guest resolver does not map it, so `open_nid_postfix(path, 0x602, 0644)` answers ENOENT (a standalone probe shows the same for flags 0x602, 0x202, 0x2 and 0x201 on that path; it never reaches `sceKernelFlock`). With only that line changed to a relative name, `const std::string path = "anyps5-sce-flock.bin";`, `guest_kernel_sys` passes 10 of 10 runs on Linux, so the `sceKernelFlock` expectations themselves (`EWOULDBLOCK` for the second descriptor, the unlock hand-over, `EBADF` for -1) hold there. Not tested: macOS, a console, Windows. AI-assisted (Claude Code).
xXPBSXx commented 2026-10-21 16:14:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T16:14:43Z.


Independent Windows run of this PR (data only).

Environment: Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against main is clean. I built only guest_kernel_sys_tests; ctest -N -R guest_kernel_sys lists exactly one test.

PR head tested: 41ab6b4c15bd6d97cdf5ef62f2f1df402c8deb5e.

Results (3 runs each):

  • PR head: guest_kernel_sys passes 3/3.
  • Baseline: I kept the PR's test and replaced only these production files: core/libs/prx/libkernel_sys/Export.Discovery.cpp, core/libs/prx/libkernel_sys/Export.cpp with the version from the PR's merge base (aed67d7e55), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards.

GPU: the run output has no Selected GPU line; no GPU claim is made.

Not run: other platforms, the other tests in the same family, the full repository CTest, and any title or console run.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6320724094 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#issuecomment-6320724094). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T16:14:43Z. --- Independent Windows run of this PR (data only). **Environment:** Windows 10 Pro 10.0.19045, Ryzen 7 5800X3D, WinLibs GCC 15.2.0 r7 (MinGW-w64 UCRT), Release, BUILD_TESTING=ON. Merge-tree of the PR head against `main` is clean. I built only `guest_kernel_sys_tests`; `ctest -N -R guest_kernel_sys` lists exactly one test. **PR head tested:** `41ab6b4c15bd6d97cdf5ef62f2f1df402c8deb5e`. **Results (3 runs each):** - PR head: `guest_kernel_sys` passes 3/3. - Baseline: I kept the PR's test and replaced only these production files: `core/libs/prx/libkernel_sys/Export.Discovery.cpp`, `core/libs/prx/libkernel_sys/Export.cpp` with the version from the PR's merge base (`aed67d7e55`), then rebuilt. The test fails 3/3 (exit 0xc0000409). I restored the PR's files afterwards. **GPU:** the run output has no `Selected GPU` line; no GPU claim is made. **Not run:** other platforms, the other tests in the same family, the full repository CTest, and any title or console run. AI-assisted (Claude Code).
DavidSamir commented 2026-10-21 20:38:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T20:38:54Z.


Thanks @xXPBSXx, you're right. The test gave a guest call an absolute host temp path (temp_directory_path()), which the Linux guest resolver doesn't map. a9a749b99 now opens a guest-relative name (anyps5-sce-flock-<steady clock>.bin, in the working directory), as the rest of GuestKernelSys.cpp does. guest_kernel_sys passes on Windows; I don't have a Linux machine here, so I haven't run it there.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6326038885 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#issuecomment-6326038885). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-21T20:38:54Z. --- Thanks @xXPBSXx, you're right. The test gave a guest call an absolute host temp path (`temp_directory_path()`), which the Linux guest resolver doesn't map. a9a749b99 now opens a guest-relative name (`anyps5-sce-flock-<steady clock>.bin`, in the working directory), as the rest of `GuestKernelSys.cpp` does. `guest_kernel_sys` passes on Windows; I don't have a Linux machine here, so I haven't run it there. AI-assisted: yes
DavidSamir commented 2026-10-22 02:06:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:06:12Z.


Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head a9a749b99 merged onto main 8c26eac3c.

  • guest_kernel_sys (with the flock test) passes 3/3.
  • No negative control is listed in this PR's text, so none was run.

Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6330941084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#issuecomment-6330941084). Originally posted by [DavidSamir](https://github.com/DavidSamir) on 2026-10-22T02:06:12Z. --- Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head `a9a749b99` merged onto `main` `8c26eac3c`. - `guest_kernel_sys` (with the flock test) passes 3/3. - No negative control is listed in this PR's text, so none was run. Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
Gruzzberry commented 2026-10-22 06:49:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Gruzzberry on 2026-10-22T06:49:15Z.


#9442 adds lchflags cases in GuestKernelSys.cpp next to the lines this PR changes there; text only, whichever lands second rebases.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6334634145 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#issuecomment-6334634145). Originally posted by [Gruzzberry](https://github.com/Gruzzberry) on 2026-10-22T06:49:15Z. --- #9442 adds `lchflags` cases in `GuestKernelSys.cpp` next to the lines this PR changes there; text only, whichever lands second rebases.
harisibrar1 commented 2026-10-24 13:25:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:25:47Z.

Review: COMMENTED


Checked against main 6cd36e6517:

  • The branch no longer merges cleanly into current main; there are two text-only conflicts. In Export.cpp's includes, main added getpid_nid_postfix there, and in GuestKernelSys.cpp, main added CheckProcessQueries() where SceKernelFlock() goes. A rebase should fix both.
  • When rebasing, the new #include "prx/libkernel/KernelErrors.hpp" can go: main's libkernel_sys/Export.cpp already includes it (lines 24 and 26).
  • Small nit: main's GuestKernelSys.cpp now includes KernelErrors.hpp and uses the SCE_KERNEL_ERROR_* names, so SCE_KERNEL_ERROR_EAGAIN and SCE_KERNEL_ERROR_EBADF would read better than the hex literals.
  • The wrapper itself looks right to me: main's flock_nid_postfix sets the guest errno through PosixFailure (Stdio.cpp:489-492), so SceKernelError(*__error_nid_postfix()) gives 0x80020023 / 0x80020009 as your test expects.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638406653 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9097#pullrequestreview-5638406653). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T13:25:47Z. **Review:** COMMENTED --- Checked against main 6cd36e6517: - The branch no longer merges cleanly into current main; there are two text-only conflicts. In `Export.cpp`'s includes, main added `getpid_nid_postfix` there, and in `GuestKernelSys.cpp`, main added `CheckProcessQueries()` where `SceKernelFlock()` goes. A rebase should fix both. - When rebasing, the new `#include "prx/libkernel/KernelErrors.hpp"` can go: main's `libkernel_sys/Export.cpp` already includes it (lines 24 and 26). - Small nit: main's `GuestKernelSys.cpp` now includes `KernelErrors.hpp` and uses the `SCE_KERNEL_ERROR_*` names, so `SCE_KERNEL_ERROR_EAGAIN` and `SCE_KERNEL_ERROR_EBADF` would read better than the hex literals. - The wrapper itself looks right to me: main's `flock_nid_postfix` sets the guest errno through `PosixFailure` (Stdio.cpp:489-492), so `SceKernelError(*__error_nid_postfix())` gives `0x80020023` / `0x80020009` as your test expects. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!9097
No description provided.