feat(libkernel): implement the named ksem semaphores #9293
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9293"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-21T17:30:58Z.
What
Builds on #9284 and adds the named
ksem_*exports tolibkernel/Semaphore/src/Ksem.cpp, following FreeBSD 11sys/kern/uipc_sem.c. Until #9284 merges, this PR's diff also shows #9284's commits.Moved to libkernel so libkernel imports reach it on Windows, as #9284 now is. The PS5 3.20 lists export
ksem_*from libkernel too, and a Windows libkernel import never searches libkernel_sys. The libkernel_sys stubs forksem_open,ksem_closeandksem_unlinkare removed.ksem_open(semid_t* idp, const char* name, int oflag, mode_t mode, u_int value)ksem_close(semid_t)EINVALon an unnamed id, which needsksem_destroyksem_unlink(const char* name)ENOENTif it is absent; ids already open keep working, and a laterO_CREATopen makes a new semaphoreksem_open's rules, in the order ofsys_ksem_openandksem_create:EINVALforoflagbits other thanO_CREAT | O_EXCL, then forvalue > SEM_VALUE_MAX.EMFILEorENFILEwhen none is left), and the id is written to*idp(EFAULTfor a NULLidp) before the name is checked, as FreeBSD's early copyout does.EFAULTfor a NULL name,ENAMETOOLONGfor 1024 bytes or more, andEINVALfor a name that doesn't start with/.ENOENTfor a missing name withoutO_CREAT, andEEXISTfor an existing name withO_CREAT | O_EXCL.ENFILEwhen a new name would exceed the 30 semaphores (SEM_MAX).Each successful open returns a new descriptor id that shares the semaphore.
The limit counts semaphore objects, as FreeBSD's
nsemsdoes. Two opens of one name are one semaphore, and a name still linked counts untilksem_unlink, even with no open id. Unnamedksem_initgivesENOSPCpast the limit, as in #9284. (Points from @pipe1os's review on #9284.)ksem_destroynow givesEINVALon a named id, which needsksem_close, as FreeBSD'sKS_ANONYMOUScheck does. A debt line inlibkernel.mdrecords that named semaphores are kept per process, which fits a single guest process:modeis accepted, and the owner and permission checks are not modelled.Tested
Windows 11 Pro 26200, WinLibs GCC 15.2.0 r7, Release. These are CPU-side exports, so no GPU is involved.
guest_kernel_sys_ksemgainsNamed():ksem_destroyon a named id andksem_closeon an unnamed id giveEINVAL;ksem_unlinkthe name is gone (ENOENT), but the open ids still work;ksem_closeinvalidates its id, and the other id is unaffected;O_CREATopen after the unlink starts from its own value.NamedLimit():EFAULTwith the id already written but dead;ksem_initgivesENOSPCand a new name givesENFILE;ksem_unlink, there's room again.ksem_open_nid_postfix not implemented. On this branch it passes, 5 of 5 runs.tools/check_conventions.pypasses, including the duplicate-export rule (all tenksem_*are now exported by libkernel only).tools/pr_overlap.pycrashes on this machine's partial clone, so it was replaced by a check against an index of every open PR's diff plusgit merge-tree. This branch merges cleanly withmainand with #9160, #9195, #9162, #9161, #9156, #9171, #9273, #9242, #9019, #9008, #9097, #9141, #9103, #8987 and #7121.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-21T17:31:56Z.
Progress report (
0c6a5bd→b208fd6)📈 System libraries: 42.19% (+0.09%, +10 implemented, -10 removed functions)
✅ 10 implemented
ksem_close_nid_postfixksem_destroy_nid_postfixksem_getvalue_nid_postfixksem_init_nid_postfixksem_open_nid_postfixksem_post_nid_postfixksem_timedwait_nid_postfixksem_trywait_nid_postfixksem_unlink_nid_postfixksem_wait_nid_postfix🗑️ 10 removed
ksem_close_nid_postfixksem_destroy_nid_postfixksem_getvalue_nid_postfixksem_init_nid_postfixksem_open_nid_postfixksem_post_nid_postfixksem_timedwait_nid_postfixksem_trywait_nid_postfixksem_unlink_nid_postfixksem_wait_nid_postfixImported from boykopovar/AnyPS5. Originally posted by DavidSamir on 2026-10-22T02:06:20Z.
Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head
b208fd6b2merged ontomain8c26eac3c.guest_kernel_sys_ksempasses 3/3.if (Objects() >= SemMax)replaced byif (false)), it fails at line 232, as listed.Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine.
Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:37:55Z.
Review: COMMENTED
Checked against main
522e32e3b6and FreeBSD release/11.4.0sys/kern/uipc_sem.c/lib/libc/gen/sem_new.c:ksem_openwith a NULL name: in 11.4,sys_ksem_openpassesuap->namestraight toksem_create, andname == NULLis its anonymous branch (uipc_sem.c:494-500). It allocates aKS_ANONYMOUSsemaphore and returns 0, and givesENOSPCif falloc fails (L477-478).Ksem.cpp:208returnsEFAULTinstead, andGuestKernelSysKsem.cpp:157/:220pin that. Could this follow FreeBSD (an unnamed semaphore thatksem_destroyaccepts andksem_closerejects with EINVAL), or throw if you'd rather not assume the PS5 matches?/, ENOENT/EEXIST/ENFILE. So do thensems-style counting (a linked name counts until unlink), the KS_ANONYMOUS checks inksem_close/ksem_destroy, andksem_unlink(no/check, ENOENT).git merge-treeof this head with current main now conflicts incore/libs/prx/libkernel_sys/Export.Discovery.cpp, because main dropped the neighbouringktimer_*stubs, so it needs a rebase (along with #9284, which is still open).sem_open(sem_new.c) is file-backed and never callsksem_open, so it doesn't bear on these rules either way.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T20:04:55Z.
Review: COMMENTED
Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (
Unblocks: noneis allowed), and it contained no code review.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:22:12Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
Changes are required before merge: handle an empty semaphore name before indexing its first character.
What I ran
Reviewed head
b208fd6b210d26e58b969d2341404b4cf085f5b0. I did not run tests or build commands. I read the prepared description, commits, diff, manifest, and all changed file paths; the file listing showed all nine changed files included. The review bundle did not providefiles.json.Results
Findings
Blocking
core/libs/prx/libkernel/Semaphore/src/Ksem.cpp:211: An empty string is a valid non-nullname, but after constructing an emptypath, this line readspath[0]. That is out-of-bounds access and can cause undefined behavior instead of returningEINVAL. Checkpath.empty()before indexing.Non-blocking
None.
Not verified
Build and test results were not independently verified. I reviewed all changed paths listed by the reader; the listing showed no excluded changed files.
To get this to a good state
Check for an empty path before reading
path[0], then rerun the build and ksem tests on the updated head.Pull request closed