feat(libkernel): share a ksem semaphore across dup, dup2 and F_DUPFD, as FreeBSD 11.4 does #22191

Merged
Dr-Coconut merged 5 commits from upstream-pr/22191 into main 2026-11-26 04:22:26 +00:00
Dr-Coconut commented 2026-11-17 08:27:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dr-Coconut on 2026-11-17T08:27:50Z.


What

dup of a ksem id, dup2 from one and fcntl(F_DUPFD) on one threw duplicating a ksem id is not supported, a gap the libkernel debt file records. In FreeBSD 11.4's uipc_sem.c a ksem id is a descriptor whose file holds the semaphore, so a duplicate shares it. This closes that gap:

  • dup, dup2 and fcntl(F_DUPFD) map the new descriptor to the same semaphore (DuplicateKsemDescriptor), on both the Windows and the POSIX paths of Stdio.cpp. dup2 over a ksem id still drops the old one first through the existing close notification.
  • ksem_destroy marks the semaphore dead before closing its id, so the other descriptors fail with EINVAL, as ksem_get does for KS_DEAD. The semaphore is freed when its last descriptor is closed (ksem_closef / ksem_drop).
  • The 30-semaphore limit counts semaphores, not descriptors, as nsems does: a duplicate takes no slot, and a destroyed semaphore keeps its slot until its last descriptor closes.
  • IsKsemDescriptor is kept unchanged (a second commit restores it after the first removed it): #24484, #24487, #24488, #24489 and #24490 call it, and a duplicated id answers it as the original does.
  • fstat and fchmod, which #24491 added for ksem descriptors, still reach a destroyed semaphore through its other descriptors (a third commit, FindOpen), as FreeBSD's ksem_stat and ksem_chmod do not check KS_DEAD; only ksem_get does.
  • The waits, ksem_post, ksem_getvalue and ksem_destroy recheck the dead mark once they hold the semaphore's lock (a fourth commit, from review), so an operation that found the semaphore just before ksem_destroy marked it fails with EINVAL instead of going on; a wait on a zero count could otherwise block on a semaphore nothing can post any more. EINVAL is what FreeBSD 11.4 gives when the destroy comes first.

The ksem test's Duplicates case, which required the throw, now checks that posts and waits through dup, fcntl(F_DUPFD) and dup2 copies reach one semaphore that fchmod through one copy shows in fstat of another, before and after ksem_destroy, and that every copy fails ksem_post, ksem_trywait, ksem_getvalue and ksem_destroy with EINVAL after it. A new DestroyedAfterLookup case destroys the semaphore right after each operation's lookup, through a hook that only the test build compiles in (ANYPS5_KSEM_TESTING, beside the existing waiter count), and requires EINVAL from ksem_post, ksem_trywait, ksem_timedwait, ksem_getvalue and ksem_destroy on a duplicate. A new DuplicatesCountOnce case pins the slot counting. The debt fragment rewrites the existing entry without the gap.

Tested

Commits ccfd949732, b472e6d1b1, 248080360b and 8105c2e044 on main b1ef52c45e, Release, in workflows on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/45123424265 (all four commits) and https://github.com/Dr-Coconut/AnyPS5/actions/runs/45036312458 (the first three, with the library restored to main)

  • Windows (GitHub-hosted windows-latest, image windows-2025-vs2026, WinLibs GCC 15.2.0 15.2.0posix-14.0.0-ucrt-r7) and Linux (GitHub-hosted ubuntu-latest, image ubuntu-26.04, GCC 15.2.0, with the packages release.yml installs plus ninja-build, libx11-dev and libxext-dev).
  • With the change, on both: guest_kernel_sys_ksem passes, and so do guest_dup, guest_kernel_fcntl and guest_kernel_web_forwards.
  • With only the four rechecks of the fourth commit deleted from Ksem.cpp, on both: guest_kernel_sys_ksem fails at line 324, the EINVAL check of DestroyedAfterLookup; the other three pass.
  • With Stdio.cpp, Ksem.cpp and Ksem.hpp restored to main and the test of the first three commits kept, on both: guest_kernel_sys_ksem aborts with dup_nid_postfix: duplicating a ksem id is not supported; the other three pass. That run stops at the first dup, so it does not show the fstat and fchmod checks the third commit adds failing without it.

Not run on macOS. No title tested. tools/check_conventions.py passes, and tools/debt_fold.py --check accepts the fragment. tools/pr_overlap.py --branch merged the open pull requests that share files with this branch and reported #24487 and #24488, which add test cases at the end of GuestKernelSysKsem.cpp where this branch adds DuplicatesCountOnce, so they overlap textually. It could not compare 11 that conflict with main, so I compared them by hand:

  • #9293, #9297 and #9302 (stacked) carry an older copy of Ksem.cpp, Ksem.hpp, the two dup throws in Stdio.cpp and a Duplicates test that requires the throw; none share duplicates. Whichever of #9293 and this lands second needs that test case changed.
  • #12682, #12688, #12700 and #12965 restructure dup2's validation and locking without touching ksem; #7121 adds close notes to dup2; #7447 only calls dup in a test; #6936 changes lseek; #15037 changes fchflags. These overlap textually only.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5353402636 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191). Originally posted by [Dr-Coconut](https://github.com/Dr-Coconut) on 2026-11-17T08:27:50Z. --- ### What `dup` of a ksem id, `dup2` from one and `fcntl(F_DUPFD)` on one threw `duplicating a ksem id is not supported`, a gap the libkernel debt file records. In FreeBSD 11.4's [`uipc_sem.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/sys/kern/uipc_sem.c) a ksem id is a descriptor whose file holds the semaphore, so a duplicate shares it. This closes that gap: - `dup`, `dup2` and `fcntl(F_DUPFD)` map the new descriptor to the same semaphore (`DuplicateKsemDescriptor`), on both the Windows and the POSIX paths of `Stdio.cpp`. `dup2` over a ksem id still drops the old one first through the existing close notification. - `ksem_destroy` marks the semaphore dead before closing its id, so the other descriptors fail with `EINVAL`, as `ksem_get` does for `KS_DEAD`. The semaphore is freed when its last descriptor is closed (`ksem_closef` / `ksem_drop`). - The 30-semaphore limit counts semaphores, not descriptors, as `nsems` does: a duplicate takes no slot, and a destroyed semaphore keeps its slot until its last descriptor closes. - `IsKsemDescriptor` is kept unchanged (a second commit restores it after the first removed it): #24484, #24487, #24488, #24489 and #24490 call it, and a duplicated id answers it as the original does. - `fstat` and `fchmod`, which #24491 added for ksem descriptors, still reach a destroyed semaphore through its other descriptors (a third commit, `FindOpen`), as FreeBSD's `ksem_stat` and `ksem_chmod` do not check `KS_DEAD`; only `ksem_get` does. - The waits, `ksem_post`, `ksem_getvalue` and `ksem_destroy` recheck the dead mark once they hold the semaphore's lock (a fourth commit, from review), so an operation that found the semaphore just before `ksem_destroy` marked it fails with `EINVAL` instead of going on; a wait on a zero count could otherwise block on a semaphore nothing can post any more. `EINVAL` is what FreeBSD 11.4 gives when the destroy comes first. The ksem test's `Duplicates` case, which required the throw, now checks that posts and waits through `dup`, `fcntl(F_DUPFD)` and `dup2` copies reach one semaphore that `fchmod` through one copy shows in `fstat` of another, before and after `ksem_destroy`, and that every copy fails `ksem_post`, `ksem_trywait`, `ksem_getvalue` and `ksem_destroy` with `EINVAL` after it. A new `DestroyedAfterLookup` case destroys the semaphore right after each operation's lookup, through a hook that only the test build compiles in (`ANYPS5_KSEM_TESTING`, beside the existing waiter count), and requires `EINVAL` from `ksem_post`, `ksem_trywait`, `ksem_timedwait`, `ksem_getvalue` and `ksem_destroy` on a duplicate. A new `DuplicatesCountOnce` case pins the slot counting. The debt fragment rewrites the existing entry without the gap. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Commits ccfd949732, b472e6d1b1, 248080360b and 8105c2e044 on `main` b1ef52c45e, Release, in workflows on my fork: https://github.com/Dr-Coconut/AnyPS5/actions/runs/45123424265 (all four commits) and https://github.com/Dr-Coconut/AnyPS5/actions/runs/45036312458 (the first three, with the library restored to `main`) - Windows (GitHub-hosted `windows-latest`, image windows-2025-vs2026, WinLibs GCC 15.2.0 `15.2.0posix-14.0.0-ucrt-r7`) and Linux (GitHub-hosted `ubuntu-latest`, image ubuntu-26.04, GCC 15.2.0, with the packages `release.yml` installs plus `ninja-build`, `libx11-dev` and `libxext-dev`). - With the change, on both: `guest_kernel_sys_ksem` passes, and so do `guest_dup`, `guest_kernel_fcntl` and `guest_kernel_web_forwards`. - With only the four rechecks of the fourth commit deleted from `Ksem.cpp`, on both: `guest_kernel_sys_ksem` fails at line 324, the `EINVAL` check of `DestroyedAfterLookup`; the other three pass. - With `Stdio.cpp`, `Ksem.cpp` and `Ksem.hpp` restored to `main` and the test of the first three commits kept, on both: `guest_kernel_sys_ksem` aborts with `dup_nid_postfix: duplicating a ksem id is not supported`; the other three pass. That run stops at the first `dup`, so it does not show the `fstat` and `fchmod` checks the third commit adds failing without it. Not run on macOS. No title tested. `tools/check_conventions.py` passes, and `tools/debt_fold.py --check` accepts the fragment. `tools/pr_overlap.py --branch` merged the open pull requests that share files with this branch and reported #24487 and #24488, which add test cases at the end of `GuestKernelSysKsem.cpp` where this branch adds `DuplicatesCountOnce`, so they overlap textually. It could not compare 11 that conflict with `main`, so I compared them by hand: - #9293, #9297 and #9302 (stacked) carry an older copy of `Ksem.cpp`, `Ksem.hpp`, the two `dup` throws in `Stdio.cpp` and a `Duplicates` test that requires the throw; none share duplicates. Whichever of #9293 and this lands second needs that test case changed. - #12682, #12688, #12700 and #12965 restructure `dup2`'s validation and locking without touching ksem; #7121 adds close notes to `dup2`; #7447 only calls `dup` in a test; #6936 changes `lseek`; #15037 changes `fchflags`. These overlap textually only. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
PrivacyOnion commented 2026-11-17 08:29:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-17T08:29:47Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 7fba7a7138:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_sys_ksem_tests built cleanly.
  • CTest (guest_kernel_sys_ksem):
    1/1 Test #938: guest_kernel_sys_ksem ............   Passed    5.31 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/File/src/Stdio.cpp, core/libs/prx/libkernel/Semaphore/include/Ksem.hpp, core/libs/prx/libkernel/Semaphore/src/Ksem.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #938: guest_kernel_sys_ksem ............Subprocess aborted***Exception:   5.43 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified that Find() marks ksem lookups as failed when dead is true, returning EINVAL on post/wait/trywait/timedwait/getvalue/destroy as required by FreeBSD 11 uipc_sem.c
    • Verified ksem_destroy marks dead=true under ksem->lock when waiters==0, preventing further acquisitions across all duplicate descriptors while waiting for all fds to close
    • Verified SemaphoreCount() computes the number of unique Ksem pointers using std::set<const Ksem*>, accurately enforcing SEM_MAX (30) across shared descriptor duplicates
    • Verified DuplicateKsemDescriptor correctly binds the new descriptor to the existing std::shared_ptr in g_ksems under g_ksemLock across dup, dup2, and fcntl(F_DUPFD) on both Windows and POSIX paths
    • Verified that closing a ksem descriptor triggers DescriptorClosed via RegisterDescriptorCloseObserver, safely erasing the entry from g_ksems and freeing the Ksem once the last duplicate descriptor closes

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5935185975 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-5935185975). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-17T08:29:47Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `7fba7a7138`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_sys_ksem_tests` built cleanly. - **CTest** (`guest_kernel_sys_ksem`): ``` 1/1 Test #938: guest_kernel_sys_ksem ............ Passed 5.31 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/File/src/Stdio.cpp`, `core/libs/prx/libkernel/Semaphore/include/Ksem.hpp`, `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #938: guest_kernel_sys_ksem ............Subprocess aborted***Exception: 5.43 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified that Find() marks ksem lookups as failed when dead is true, returning EINVAL on post/wait/trywait/timedwait/getvalue/destroy as required by FreeBSD 11 uipc_sem.c - Verified ksem_destroy marks dead=true under ksem->lock when waiters==0, preventing further acquisitions across all duplicate descriptors while waiting for all fds to close - Verified SemaphoreCount() computes the number of unique Ksem pointers using std::set<const Ksem*>, accurately enforcing SEM_MAX (30) across shared descriptor duplicates - Verified DuplicateKsemDescriptor correctly binds the new descriptor to the existing std::shared_ptr<Ksem> in g_ksems under g_ksemLock across dup, dup2, and fcntl(F_DUPFD) on both Windows and POSIX paths - Verified that closing a ksem descriptor triggers DescriptorClosed via RegisterDescriptorCloseObserver, safely erasing the entry from g_ksems and freeing the Ksem once the last duplicate descriptor closes Looks Good To Me
MarcZX10 commented 2026-11-19 02:06:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-19T02:06:36Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed code and author-reported checks support merging.

What I ran

I reviewed head 7fba7a7138a48d197ffe92a93d2f037cf8530986. No commands or tests were run in this review; the reader repo_read.py was used to inspect the prepared diff and repository sources at the pinned head. The base was main; no merged-preview check was run.

Results

Check Result Note
Source review passed All five changed files read
Automated checks not run Author-reported results are not independently verified
Negative control not run No test was changed for this review

Findings

Blocking

None.

Non-blocking

None.

Not verified

No automated tests, builds, or conventions checks were run. Windows, Linux, macOS, and console behavior were not independently verified. The author reports passing ksem and related guest tests on Windows and Linux, and reports that the ksem test fails against the prior implementation.

Files read: core/libs/prx/libkernel/File/src/Stdio.cpp, core/libs/prx/libkernel/Semaphore/include/Ksem.hpp, core/libs/prx/libkernel/Semaphore/src/Ksem.cpp, core/libs/tests/GuestKernelSysKsem.cpp, and docs/dev/debt/changes/feat-libkernel-ksem-shared-duplicates.md. Excluded files: none listed in the prepared manifest.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5968262815 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-5968262815). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-19T02:06:36Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed code and author-reported checks support merging. ## What I ran I reviewed head `7fba7a7138a48d197ffe92a93d2f037cf8530986`. No commands or tests were run in this review; the reader `repo_read.py` was used to inspect the prepared diff and repository sources at the pinned head. The base was `main`; no merged-preview check was run. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All five changed files read | | Automated checks | not run | Author-reported results are not independently verified | | Negative control | not run | No test was changed for this review | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No automated tests, builds, or conventions checks were run. Windows, Linux, macOS, and console behavior were not independently verified. The author reports passing ksem and related guest tests on Windows and Linux, and reports that the ksem test fails against the prior implementation. Files read: `core/libs/prx/libkernel/File/src/Stdio.cpp`, `core/libs/prx/libkernel/Semaphore/include/Ksem.hpp`, `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp`, `core/libs/tests/GuestKernelSysKsem.cpp`, and `docs/dev/debt/changes/feat-libkernel-ksem-shared-duplicates.md`. Excluded files: none listed in the prepared manifest. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-21 12:28:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T12:28:04Z.

Review: COMMENTED


Reviewed — no blocking findings

The inspected changes at head e1864939b349eb70e5272f47dc5eb837313a340e consistently map duplicated descriptors to shared semaphore state and retain that state until its last descriptor closes; I did not run the reported checks.

What I ran

Read the prepared description, commits, manifest, diff, all five changed files, and targeted close-notification and duplication code through repo_read.py. No build or test command was run. Author-reported checks are not treated as checks I ran.

Results

Check Result Note
Source review passed All five changed files read
Tests and build not run Author reports Windows and Linux runs
Negative control not run Author reports the test fails without the change

Negative control

Could not run a negative control in this review session.

Findings

Blocking

None.

Non-blocking

None.

Sources

Behavior reviewed in Ksem.cpp at the reviewed commit and Stdio.cpp at the reviewed commit.

Not verified

Builds, tests, and console behavior were not independently verified. The author reports Windows and Linux test runs; macOS and title behavior were not tested. The prepared review listing included all five changed files; no exclusions were listed there.

Files read: core/libs/prx/libkernel/File/src/Stdio.cpp, core/libs/prx/libkernel/Semaphore/include/Ksem.hpp, core/libs/prx/libkernel/Semaphore/src/Ksem.cpp, core/libs/tests/GuestKernelSysKsem.cpp, and docs/dev/debt/changes/feat-libkernel-ksem-shared-duplicates.md. Excluded changed files: none listed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6005179052 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-6005179052). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T12:28:04Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The inspected changes at head `e1864939b349eb70e5272f47dc5eb837313a340e` consistently map duplicated descriptors to shared semaphore state and retain that state until its last descriptor closes; I did not run the reported checks. ## What I ran Read the prepared description, commits, manifest, diff, all five changed files, and targeted close-notification and duplication code through `repo_read.py`. No build or test command was run. Author-reported checks are not treated as checks I ran. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | All five changed files read | | Tests and build | not run | Author reports Windows and Linux runs | | Negative control | not run | Author reports the test fails without the change | ## Negative control Could not run a negative control in this review session. ## Findings ### Blocking None. ### Non-blocking None. ## Sources Behavior reviewed in [Ksem.cpp at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/e1864939b349eb70e5272f47dc5eb837313a340e/core/libs/prx/libkernel/Semaphore/src/Ksem.cpp) and [Stdio.cpp at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/e1864939b349eb70e5272f47dc5eb837313a340e/core/libs/prx/libkernel/File/src/Stdio.cpp). ## Not verified Builds, tests, and console behavior were not independently verified. The author reports Windows and Linux test runs; macOS and title behavior were not tested. The prepared review listing included all five changed files; no exclusions were listed there. Files read: `core/libs/prx/libkernel/File/src/Stdio.cpp`, `core/libs/prx/libkernel/Semaphore/include/Ksem.hpp`, `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp`, `core/libs/tests/GuestKernelSysKsem.cpp`, and `docs/dev/debt/changes/feat-libkernel-ksem-shared-duplicates.md`. Excluded changed files: none listed. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-22 15:35:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T15:35:37Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

Changes are required before merge: make ksem operations recheck the dead state after acquiring the semaphore lock.

What I ran

Reviewed head 248080360b15c72b9929f5b40b35375aa3c678f5. I used the prepared diff and the pinned repository reader; no tests or build commands were run. The reader was accessed through the review tools, so there is no shell command to report.

Results

Check Result Note
Diff and targeted source review passed All five changed files read
Tests and build not run Author reports CI runs; not independently verified

Negative control

Not run; no test or code alteration was made.

Findings

Blocking

  • core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:269: Find checks dead before the operation acquires ksem->lock, but operations do not check it again under that lock. If an operation obtains the shared pointer and pauses, ksem_destroy can set dead and release the lock first; the operation can then proceed on the destroyed semaphore. A wait with count zero can block indefinitely instead of returning EINVAL. Check dead while holding the semaphore lock in operations that use the semaphore, including Wait.

Non-blocking

None.

Sources

Not verified

Tests and builds were not run. The author reports Windows and Linux CI results in the PR description; those results were not independently checked. macOS and physical-console behavior were not verified. Read: Stdio.cpp, Ksem.hpp, Ksem.cpp, GuestKernelSysKsem.cpp, and feat-libkernel-ksem-shared-duplicates.md. No changed files were excluded; all five were listed as reviewed in the manifest.

To get this to a good state

Recheck the dead flag under the semaphore lock before each operation proceeds, and add coverage for an operation racing with destroy. Rerun the relevant ksem tests on the updated head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013046525 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-6013046525). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T15:35:37Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested Changes are required before merge: make ksem operations recheck the dead state after acquiring the semaphore lock. ## What I ran Reviewed head `248080360b15c72b9929f5b40b35375aa3c678f5`. I used the prepared diff and the pinned repository reader; no tests or build commands were run. The reader was accessed through the review tools, so there is no shell command to report. ## Results | Check | Result | Note | |---|---|---| | Diff and targeted source review | passed | All five changed files read | | Tests and build | not run | Author reports CI runs; not independently verified | ## Negative control Not run; no test or code alteration was made. ## Findings ### Blocking - `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:269`: `Find` checks `dead` before the operation acquires `ksem->lock`, but operations do not check it again under that lock. If an operation obtains the shared pointer and pauses, `ksem_destroy` can set `dead` and release the lock first; the operation can then proceed on the destroyed semaphore. A wait with count zero can block indefinitely instead of returning `EINVAL`. Check `dead` while holding the semaphore lock in operations that use the semaphore, including `Wait`. ### Non-blocking None. ## Sources - [Ksem.cpp at reviewed commit](https://github.com/boykopovar/AnyPS5/blob/248080360b15c72b9929f5b40b35375aa3c678f5/core/libs/prx/libkernel/Semaphore/src/Ksem.cpp) - [GuestKernelSysKsem.cpp at reviewed commit](https://github.com/boykopovar/AnyPS5/blob/248080360b15c72b9929f5b40b35375aa3c678f5/core/libs/tests/GuestKernelSysKsem.cpp) ## Not verified Tests and builds were not run. The author reports Windows and Linux CI results in the PR description; those results were not independently checked. macOS and physical-console behavior were not verified. Read: `Stdio.cpp`, `Ksem.hpp`, `Ksem.cpp`, `GuestKernelSysKsem.cpp`, and `feat-libkernel-ksem-shared-duplicates.md`. No changed files were excluded; all five were listed as reviewed in the manifest. ## To get this to a good state Recheck the dead flag under the semaphore lock before each operation proceeds, and add coverage for an operation racing with destroy. Rerun the relevant ksem tests on the updated head.
MarcZX10 commented 2026-11-23 06:43:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T06:43:04Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the checked code supports sharing semaphore state across duplicated descriptors and rejecting operations after destruction.

What I ran

Reviewed head 8105c2e04466f9d3f53bcd97fbd02e6647853fd0 using the prepared diff and repo_read.py for targeted source reads. No commands or tests were run by me; build and test results below are author-reported.

Results

Check Result Note
Changed code review passed All five changed files read
Reported CI tests not run Author reports Windows and Linux passes
macOS and console not run Author reports neither tested

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not run builds, tests, convention checks, overlap checks, or console/hardware checks. The author reports guest_kernel_sys_ksem, guest_dup, guest_kernel_fcntl, and guest_kernel_web_forwards passing on Windows and Linux, plus the convention and debt checks passing. I read all changed files listed in manifest.json: Stdio.cpp, Ksem.hpp, Ksem.cpp, GuestKernelSysKsem.cpp, and the debt fragment. No changed files were excluded; the prepared bundle did not include files.json.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6017488349 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-6017488349). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T06:43:04Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the checked code supports sharing semaphore state across duplicated descriptors and rejecting operations after destruction. ## What I ran Reviewed head `8105c2e04466f9d3f53bcd97fbd02e6647853fd0` using the prepared diff and `repo_read.py` for targeted source reads. No commands or tests were run by me; build and test results below are author-reported. ## Results | Check | Result | Note | |---|---|---| | Changed code review | passed | All five changed files read | | Reported CI tests | not run | Author reports Windows and Linux passes | | macOS and console | not run | Author reports neither tested | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified I did not run builds, tests, convention checks, overlap checks, or console/hardware checks. The author reports `guest_kernel_sys_ksem`, `guest_dup`, `guest_kernel_fcntl`, and `guest_kernel_web_forwards` passing on Windows and Linux, plus the convention and debt checks passing. I read all changed files listed in `manifest.json`: `Stdio.cpp`, `Ksem.hpp`, `Ksem.cpp`, `GuestKernelSysKsem.cpp`, and the debt fragment. No changed files were excluded; the prepared bundle did not include `files.json`. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-26 00:23:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-26T00:23:12Z.

Review: COMMENTED


Reviewed — comments

Nothing blocks this review, but dup2 can leave a ksem descriptor mapped to the wrong semaphore when the host descriptor replacement fails.

What I ran

Reviewed head 041dd013fe073c284152299fc8693196d0a35947 from the prepared diff and pinned repository reader. No commands, tests, or builds were run. Read description.md, commits.md, manifest.json, diff.patch, all changed repository files listed in the manifest, and the relevant close implementation in Stdio.cpp. files.json was unavailable through the provided reader.

Results

Check Result Note
Diff and changed source review passed All five manifest files read
Tests and builds not run Author reports Windows and Linux CI results
Excluded files not verified files.json could not be read

Findings

Blocking

None.

Non-blocking

  • In both dup2 paths, DuplicateKsemDescriptor(from, to) runs after the host duplication call without checking that it succeeded. The helper unconditionally assigns g_ksems[to], so a failed host duplication can overwrite an existing ksem mapping while to still refers to its prior host descriptor (or is closed). Future operations on that descriptor can then target a semaphore unrelated to the host descriptor. Only register the ksem mapping after a successful replacement. See core/libs/prx/libkernel/File/src/Stdio.cpp:1154 and core/libs/prx/libkernel/File/src/Stdio.cpp:1173, and helper core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:166.

Not verified

No tests or builds were run in this review. The author reports Windows and Linux CI success, but those runs were not independently inspected. files.json and any files it might list as excluded were not verified.

To get this to a good state

Nothing is required. Optional: guard ksem descriptor mapping in both dup2 paths on host replacement success.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6066874082 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/22191#pullrequestreview-6066874082). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-26T00:23:12Z. **Review:** COMMENTED --- # Reviewed — comments Nothing blocks this review, but `dup2` can leave a ksem descriptor mapped to the wrong semaphore when the host descriptor replacement fails. ## What I ran Reviewed head `041dd013fe073c284152299fc8693196d0a35947` from the prepared diff and pinned repository reader. No commands, tests, or builds were run. Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, all changed repository files listed in the manifest, and the relevant close implementation in `Stdio.cpp`. `files.json` was unavailable through the provided reader. ## Results | Check | Result | Note | |---|---|---| | Diff and changed source review | passed | All five manifest files read | | Tests and builds | not run | Author reports Windows and Linux CI results | | Excluded files | not verified | `files.json` could not be read | ## Findings ### Blocking None. ### Non-blocking - In both `dup2` paths, `DuplicateKsemDescriptor(from, to)` runs after the host duplication call without checking that it succeeded. The helper unconditionally assigns `g_ksems[to]`, so a failed host duplication can overwrite an existing ksem mapping while `to` still refers to its prior host descriptor (or is closed). Future operations on that descriptor can then target a semaphore unrelated to the host descriptor. Only register the ksem mapping after a successful replacement. See `core/libs/prx/libkernel/File/src/Stdio.cpp:1154` and `core/libs/prx/libkernel/File/src/Stdio.cpp:1173`, and helper `core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:166`. ## Not verified No tests or builds were run in this review. The author reports Windows and Linux CI success, but those runs were not independently inspected. `files.json` and any files it might list as excluded were not verified. ## To get this to a good state Nothing is required. Optional: guard ksem descriptor mapping in both `dup2` paths on host replacement success.
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!22191
No description provided.