Repository navigation
feat(libkernel): implement the unnamed ksem semaphores - #9284
Conversation
Progress report (2227f13 → 735dd0b)📈 System libraries: 46.69% (+0.05%, +7 implemented, -7 removed functions) ✅ 7 implemented
🗑️ 7 removed
Changed libraries (implemented exports)
|
|
I had
Everything else I checked matches: the I also have |
|
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.
Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.
|
|
@pipe1os thank you, all four points check out against the source, and point 1 matters most.
On the named part: #9293 already has |
Here it is: https://github.com/pipe1os/AnyPS5/tree/feat/libkernel-ksem. One commit, all ten in libkernel; the tests are in Comparing #9293's
And you're right about |
The PS5 3.20 export lists have ksem_* in libkernel as well as libkernel_sys, and on Windows a libkernel import never reaches libkernel_sys, so titles importing them from libkernel did not find them. Following pipe1os's review on boykopovar#9284, also: ENOSPC from ksem_init when no descriptor is left, the SEM_MAX limit of 30 with ENOSPC, and dup/dup2 from a ksem id throwing as for a kqueue.
|
@pipe1os all four points are in 8db8924:
Negative controls for 3 and 4 are in Tested. I've retitled the PR AI-assisted: yes |
It's here: https://github.com/pipe1os/AnyPS5/tree/feat/libkernel-ksem (I linked it in my last comment too, easy to miss). The named cases are I also ran 8db8924 on Windows: Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 (ucrt-posix-seh r7), Ninja, Release,
One thing for #9293: the limit here checks |
jotitadev
left a comment
There was a problem hiding this comment.
Linux run of b0d85e3c2, since the description only lists Windows.
Machine: Linux x86_64, i7-8700 (6 cores / 12 threads), Ubuntu 26.04, GCC 15.2, Release, BUILD_TESTING=ON, full build. Head b0d85e3c2 on its own base (main 4ec064d29).
- Build ok.
guest_kernel_sys_ksempasses. - Restoring
libkernel_sys/Export.Discovery.cppto that base and keeping the test aborts it withksem_init_nid_postfix not implemented, so the test covers the change.
Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
|
Linux: Ubuntu 24.04.1 LTS (WSL2), GCC 13.3.0, Release; head
Run from the PC (Windows host, Linux in WSL2), not from the PR author's machine. |
adriadam10
left a comment
There was a problem hiding this comment.
Two things stop this from merging on current main:
core/libs/prx/libkernel_sys/Export.Discovery.cppconflicts: main moved the neighbouringksem_*stubs, so the removal hunk no longer applies. Please rebase ontomain.- Main now requires each exported function to be declared exactly once, in the library's public header, and
public_headers(core/libs/tests/test_public_headers.py) checks it. The sevenksem_*definitions inlibkernel/Semaphore/src/Ksem.cpp(lines 111-173) have no declaration inlibkernel/include/Api.hpp, so the audit reportsexpected exactly one header declaration; found 0. Please add them there (next tosem_*,Api.hpp:467), reachable from the libkernel umbrella. - For the same reason, drop the local
extern "C"declarations of exports that Api.hpp already declares:close_nid_postfixinKsem.cpp:20(Api.hpp:302) and the ksem/close/dup/dup2/open/__errordeclarations inGuestKernelSysKsem.cpp:16-27; includeprx/libkernel/include/Api.hppinstead. A second declaration in a.cppcounts as a second declaration of the export. RegisterDescriptorCloseObserver_nid_no_patch(Ksem.cpp:21,Equeue.cpp:770) is_nid_no_patchand exempt from the audit, but declare it in a header too rather than twice by hand.
After the rebase, please rerun public_headers, guest_kernel_sys_ksem and guest_kernel_sys. The rest checked out against FreeBSD uipc_sem.c (init/post/wait/timedwait/getvalue/destroy errors, SEM_MAX, id-as-descriptor with the close observer), and the tests fail without the change.
|
@DavidSamir thanks for this one. It conflicts with main in core/libs/prx/libkernel_sys/Export.Discovery.cpp, so it needs a rebase, and adriadam10's earlier notes on the public header are still open: the seven ksem_* exports need exactly one declaration in core/libs/prx/libkernel/include/Api.hpp (the libkernel_sys/include/Api.hpp declarations of the same names as (void) have to go, since the definitions moved), and the extra local extern "C" declarations in Ksem.cpp and GuestKernelSysKsem.cpp should use that header instead. We also keep tests to real behaviour and argument validation, not asserting that a not-yet-implemented call throws, since that kind of test breaks the moment someone implements the function. In GuestKernelSysKsem.cpp Duplicates() please drop the two Require(Throws(...)) lines for dup of a ksem id and dup2 from one; the guard in Stdio.cpp can stay. |
…sed and replaced ids
…elative name An absolute host temp path is not mapped by the Linux guest resolver. Found by xXPBSXx on boykopovar#9097.
The PS5 3.20 export lists have ksem_* in libkernel as well as libkernel_sys, and on Windows a libkernel import never reaches libkernel_sys, so titles importing them from libkernel did not find them. Following pipe1os's review on boykopovar#9284, also: ENOSPC from ksem_init when no descriptor is left, the SEM_MAX limit of 30 with ENOSPC, and dup/dup2 from a ksem id throwing as for a kqueue.
…eader Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
b0d85e3 to
735dd0b
Compare
|
Rebased onto
|
What
Implements the unnamed
ksem_*exports in libkernel, in a newlibkernel/Semaphore/src/Ksem.cpp, following FreeBSD 11sys/kern/uipc_sem.c. They were discovery stubs inlibkernel_systhat threwNotImplemented.Moved to libkernel so libkernel imports reach it on Windows. The PS5 3.20 export lists have
ksem_*in libkernel as well as in libkernel_sys and libkernel_web. On Windows a libkernel import never searches libkernel_sys:WindowsEntryStubBuilder.cpp:175only falls back the other way, from libkernel_sys and libkernel_web to libkernel. With the implementation in libkernel and the libkernel_sys stubs removed, both imports reach it, and the duplicate-export rule holds. On Linux and macOS, ELF lookup is global, so either import already worked. (Review by @pipe1os.)ksem_init(semid_t* idp, u_int value)EINVALforvalue > SEM_VALUE_MAX(0x7fffffff).ENOSPCwhen no descriptor is left, whatever the cause (uipc_sem.c:475-479).EFAULTfor a NULLidp.ENOSPConce 30 semaphores (SEM_MAX,p1003_1b.sem_nsems_max) exist (:300-304,:496-498); that check comes after the id is written, as in FreeBSD.ksem_post(semid_t)EOVERFLOWatSEM_VALUE_MAXksem_wait(semid_t)ksem_trywait(semid_t)EAGAINat 0ksem_timedwait(semid_t, const timespec*)CLOCK_REALTIMEdeadline, givingETIMEDOUT. Atv_nsecoutside 0..999999999 givesEINVALbefore any wait, assys_ksem_timedwaitdoes.ksem_getvalue(semid_t, int*)EFAULTfor a NULL out pointerksem_destroy(semid_t)EBUSYwhile a thread waitsAn id that isn't a live ksem gives
EINVAL, asksem_getmapsfget's failure.semid_tisintptr_t(sys/_semaphore.h).The id is a descriptor, as in FreeBSD:
kqueuereserves one, and the semaphore lives in a table keyed by that descriptor.EqueueDescriptorClosed) gains a small observer list. It is already called fromclose, fromdup2replacing a descriptor, and from sockets.Ksem.cppregisters throughRegisterDescriptorCloseObserver_nid_no_patchto drop the entry when its descriptor is closed or replaced.ksem_destroyreleases the id through the guestclose.Duplicating an id throws:
dupof a ksem id, ordup2from one, throws, as the same spot does for a kqueue (Stdio.cpp). This avoids handing back a descriptor that isn't a semaphore, where FreeBSD would share it.dup2onto an id is exact: it closes the semaphore.The named forms (
ksem_open,ksem_close,ksem_unlink) follow in #9293, which depends on this one. A debt line inlibkernel.mdrecords thedupthrow and that a guest signal doesn't interrupt a wait withEINTR.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_ksem(core/libs/tests/GuestKernelSysKsem.cpp) covers:EAGAIN,EOVERFLOWat the maximum, andEFAULTfor NULL pointers;timedwaitwith a badtv_nsec(EINVAL), a 150 ms real-time deadline (ETIMEDOUT), and a later post that satisfies a wait;ksem_post, withksem_destroygivingEBUSYwhile it waits;EINVAL;closeof an id, a regular file that reuses the same number getsEINVALfromksem_postandksem_getvalue; afterdup2of a file over an id,ksem_postandksem_trywaitgiveEINVAL;dupof an id anddup2from an id both throw, whiledup2(id, id)keeps the semaphore;ksem_initgivesENOSPCwith the id written but dead; destroying one makes room again.The stale-descriptor file is opened by a guest-relative name, not a host temp path, so the test also runs on Linux (found by @xXPBSXx on #9097).
Results:
NotifyDescriptorCloseObserverscall removed fromEqueueDescriptorClosed, the test fails, because a destroyed id still answers;SEM_MAXcheck removed, it fails at line 166;dupcheck removed, it fails at line 153.guest_kernel_sys, semaphore, kqueue, equeue, dup, descriptor, stdio andguest_filepass, 4 runs out of 5. The other run had one failure that didn't repeat in three more runs of the set or ten runs of the ksem test.tools/check_conventions.pypasses, including the duplicate-export rule.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)