feat(libSceNpCommon): implement sce::np::Semaphore and sceNpWaitSema #13277
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13277"
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 hypermoist on 2026-10-30T11:23:30Z.
What
Implements
sce::np::Semaphore(11 exports) and the C exportsceNpWaitSemafromlibSceNpCommon.sprxof firmware 13.60 (read from disassembly, not measured on a console).main: 3 of them had placeholderint (void)declarations:Create,WaitandD2.C1,C2,D0,D1,IsInit,Open,Destroy,SignalandsceNpWaitSema.sceNpWaitSema(KernelSema sema, KernelUseconds* timeout): callssceKernelWaitSema(sema, 1, timeout), maps the kernel timeout0x8002003Cto0x8055800B, and otherwise returns only negative results.NpSemaphoreObject(declared inApi.hpp, 24 bytes): vtable,KernelSema sema, thenint32_t stateat +0x10. The state is 0 when uninitialized, 1 when created and 2 when opened. The vtable is{0, 0, D1, D0}.IsInitisstate != 0.sceKernelDeleteSema.D0usessceNpDeleteSemaand then frees throughsce::np::Object::operator delete(void*).Create(int initial, int maximum, const char* name):0x80559E03when already initialized;sceNpCreateSema(&sema, 0, initial, maximum, name)and returns its error, or sets state 1 and returns 0.Destroy: releases by state and always clears it.Wait(uint32_t usec): callssceNpWaitSema, passing a pointer to its own copy of the timeout, or null whenusecis 0, so 0 waits without a timeout.Signal: callssceNpSignalSema. BothWaitandSignalkeep only negative results.Not implemented:
Open(const char* name). In the module it callssceNpOpenSema→sceKernelOpenSema. AnyPS5's libkernel has no named semaphores:sceKernelOpenSemaandsceKernelCloseSemaare unknown-signature stubs.0x80559E03when already initialized, as the module does, and otherwise throws throughNotImplemented_nid_no_patch.Semaphoreever reaches the opened state. The close branch ofDestroyand the destructors throws the same way, and the debt file says so.Where the module halts: before
Create,WaitandSignalassertIsInit()and panic. AnyPS5 throwsstd::invalid_argument, following the libSceJson2 exports, and the debt file records it.Tested
Linux (CachyOS, GCC 16.2, Release):
ctest --test-dir build -R 'np_common|public_headers' --output-on-failure, 70/70 passed.guest_np_common_runtimepassed 3 of 3 runs, in 0.03–0.04 s each.TestSemaphoreObjectsinGuestNpCommonRuntime.cppuses real kernel semaphores and a thread to check:Create;Create(initial count above the maximum) leaves the object uninitialized;CreateorOpenreturning0x80559E03;WaitandsceNpWaitSema's timeout mapping;SignalthenWaitwith timeout 0;Signalwaking a waiter blocked inWait(0)on another thread;Destroy;D0freeing through the allocator stored byObject::operator new.Mutation check: I changed 8 single lines of the new functions, one at a time, and the test fails for 7. The changes were:
Wait(0)given a timeout;Createcounted as created;Createcheck removed;Destroynot clearing the state;D0not deleting the semaphore.The one survivor is the last: AnyPS5's libkernel does not validate a deleted semaphore handle (a second
sceKernelDeleteSemaon it never returns), so the test can't observe the deletion safely.No firmware differential: these methods wrap kernel semaphores. A unicorn run would only exercise the glue against a fake kernel, and I can't swap the kernel under AnyPS5. The contract above comes from the disassembly, including the vtable relocations and the C1/C2 and D1/D2 aliases (one address each).
Windows not built. Not run on a console.
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)mainhere and keep its copy ofNP_ERROR_OBJECT_ALREADY_INITIALIZED.NotImplementedstubs (the ones missing frommain), so it drops them fromInventoryExports.cppafter this lands. Agreed with its author (comment): #13120 has removed these declarations and declaresDepends on: #13130, #13270, #13277, #13281.Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console.
🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T12:02:35Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The implementation and reported test evidence look reasonable, but the overlapping exports with #13120 do not yet have the landing order required by the repository rules.
What I ran
Reviewed head
8007fe1d8b6a79868802a5a05b23b67786f88414against PR basee471817c68d4e1b1e44b0ce369ed88bafe4ae8ed. The merge preview onto currentmain51c31ea17e9f66cb36fe9655ec31c81610bfcfc9was clean (tree29ba750a88c4c952cec9c0cefb45cb0840be8a78). Environment: Debian 12 x86-64, Git 2.39.5, Python 3.11.2. I reviewed the semaphore wrappers, object lifecycle, and tests. I did not build or run C++ tests locally.Results
51c31ea17e9f66cb36fe9655ec31c81610bfcfc9.Unedited local output
Findings
Blocking
The PR overlap analysis for #13120 lists nine of the semaphore exports implemented here as duplicate exports. The PR body acknowledges that one side must remove those declarations, but the landing order has not been agreed with #13120's author and #13120 is not listed on this PR's
Depends online. The repository rule requires the authors to agree which PR lands first and record that order withDepends on. Please agree the order with the #13120 author and update the checklist (and the affected PR's declaration list if required) so the second merge cannot introduce duplicate symbol definitions.Non-blocking
No code blocker found in the reviewed implementation. The local Linux tests and Windows build/test results above are reported by the authors/contributor, not independently reproduced in this review.
Sources
Not verified
I did not compile, run the reported Linux tests, or run on a console. Hosted conventions and labels were still pending at review time; the hosted Linux and specialized jobs were skipped. Firmware behavior is based on the PR's disassembly analysis and was not independently verified on a console.
To get this to a good state
Please agree and record the landing order with #13120's author, then update the relevant
Depends onchecklist entry and declarations if needed. I will recheck the overlap and conventions on the updated head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T12:24:59Z.
Review: CHANGES_REQUESTED
Re-checked — new head, changes still needed
The update records the intended order, but the current head conflicts with
main, the declared #13130/#13270 dependency heads are not included, and eligibility now fails on the merge conflict.What I ran
Head
71e0448f63f7ab494e61ffc217eb1f86071dae00; PR base51c31ea17e9f66cb36fe9655ec31c81610bfcfc9; currentmain42d009f5f37c8f388b01fbfe38d2e704c792c46f. Environment: Linux x86_64, Python 3.11.2. No local build or C++ tests were run.Results
passed_with_warnings: 322 symbols; seven lexical warnings for the same private helper definitions, with no semaphore-specific finding.Runtime.cpp,include/Api.hpp,GuestNpCommonRuntime.cpp, anddocs/dev/debt/libSceNpCommon.md. The current heads of #13130 and #13270 are not included in this branch.Pull request #13277 does not merge cleanly into main; rebase it first. Report passed; conventions, portability and labels were pending. Windows, Linux and specialized build/test jobs were skipped.Unedited hosted eligibility output
Findings
Blocking
#13130 → #13270 → #13277by incorporating the appropriate dependency heads, or wait for them to merge before updating this PR.Non-blocking
The semaphore implementation is unchanged by the merge update. The focused test results are author-reported and were not reproduced locally.
Sources
Not verified
No local build, C++ test run, Windows/macOS test, or console test. Linux and specialized hosted jobs were skipped. ABI audit warnings are lexical and do not prove binary ABI correctness.
To get this to a good state
After #13130 and #13270 are settled, update this branch in the declared order, resolve current-main conflicts, and obtain #13120's agreement on the shared exports. I will rerun conventions, ABI audit, diff check, the stack/mergeability check, and review the exact new head; report the same focused test command on it.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T13:31:25Z.
Review: APPROVED
Re-checked — new head, looks good
The merge conflict and unconfirmed #13120 order from my previous review are resolved; source review and the current-main preview show no remaining blocker in this head.
What I ran
Reviewed head
e058de81c03ef381405375b07dd813d5330ee630against PR base42d009f5f37c8f388b01fbfe38d2e704c792c46f. The stack preview against currentmainda026c70655a132031d45e2008c24f993870d801was clean. The declared order is #13130 → #13270 → #13277; the first two dependency heads are not yet included because they remain open. Environment: Debian GNU/Linux x86_64, Python 3.11.2. I did not configure, build, compile, or run C++ tests.Results
passed_with_warnings; seven lexical warnings concern the same unchanged helper definitions inRuntime.cpp, not semaphore-specific findings.e058de81c03ef381405375b07dd813d5330ee630, the author reports 70/70 `np_commonUnedited output from checks I ran
The precheck reported these unavailable helper files in its isolated checkout:
Negative control
Findings
Blocking
None found in the reviewed head.
Non-blocking
Do not land #13277 before #13130 and #13270. The current dependency heads are not in this branch, but the PR declares that order and the individual merge previews against
mainare clean. Once those dependencies land, refresh this branch and rerun the relevant integration checks.Sources
Not verified
I did not independently run Linux tests or mutation checks, run Windows tests, or verify firmware behavior on a console. The hosted Windows job was still in progress at the final refresh.
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T09:51:14Z.
Review: APPROVED
Reviewed — approved
The
sce::np::Semaphorewrappers andsceNpWaitSemabuild and pass their test on the merged tree. Two one-line production mutations each make the new test fail, and I found nothing blocking.What I ran
Head
8942f0b323347daaa1a33c325d7c4569e84869ac, merged with--no-ffintomain4f41f4c375f5abb487dbd184fbc150f31994d762(merged tree6f4a4983a30f456a87f2fc2b73462b00737653e2). Debian 13 VM (32 vCPU, Intel Xeon Gold 6136), GCC 14.2.0, Release, Ninja,BUILD_TESTING=ON.Results
git diff --checkcheck_conventions.pyAll conventions checks passed.libscheck_skips.py). 0 new failures, 0 timeouts. The 1 known failure (unwrap_self_UnwrapTest_test_new_output_inherits_the_destination_directory_group) also fails onmainand is unrelated to this change.guest_np_common_runtime--repeat until-fail:5(0.46 s total)public_headersmainin the AMD CI comment; unaffected by this change)Negative control
A revert-only control can't compile, because the test uses
NpSemaphoreObject, which this PR adds (JJenkx reported the same on Windows). I mutated single production lines incore/libs/prx/libSceNpCommon/Runtime.cppinstead, rebuiltguest_np_common_runtime_tests, and ran it:sceNpWaitSemareturnsNpResult(status), which drops the0x8002003C→0x8055800Bmappinga timed wait on an empty semaphore times out, abortedCreatesets the state toNP_SEMAPHORE_CREATEDbefore returning the errora refused Create leaves the semaphore uninitialized, abortedI then restored the merged
Runtime.cppfrom6f4a4983a3, rebuilt, and the test passed again.git status --porcelainprinted nothing. These two runs confirm part of the author's 8-mutation table. I did not repeat the other six.Findings
Blocking
None.
Non-blocking
NpSemaphoreObject(core/libs/prx/libSceNpCommon/include/Api.hpp:95-99) is described as 24 bytes withstateat +0x10, but nostatic_assertpins this.Runtime.cpp:24-30already pinsNpCallout,NpKernelMemoryand similar objects this way. A guest that embedssce::np::Semaphorein its own classes depends on this layout. Suggested fix: addstatic_assert(sizeof(NpSemaphoreObject) == 0x18)and anoffsetof(..., state) == 0x10check next to the others.core/libs/prx/libSceNpCommon/Runtime.cpp:1328-1329:Signalends right againstMemoryStreamReaderC1with no blank line. This is probably left over from the merge. It is cosmetic only.D1(Runtime.cpp:1280-1284) repeats the opened/created branch inline withsceKernelDeleteSema, whileD0andDestroygo throughReleaseSemaphorewithsceNpDeleteSema(Runtime.cpp:297-300). The description says the module does it this way (the kernel call directly in D1/D2). BecausesceNpDeleteSemathrows its result away there, the behaviour is the same, so this needs no change unless the author wants the asymmetry to read as intentional.Earlier review points: MarcZX10's blocking items (landing order with #13120, and the conflicts with
main) were resolved in their own re-review. The order is recorded on the checklist line. This head merges cleanly withmain4f41f4c375.Rules check: the only code changes are exports and helpers, with no new comments.
Openand the opened-state close branches throw throughNotImplemented_nid_no_patch, and the debt file lists both. Thestd::invalid_argumentbeforeCreateis also recorded as a deviation. The checklist lines (Depends on, Fixes, Unblocks, GPUs tested, AI-assisted) are present. The test runs well under 30 s.Not verified
{0, 0, D1, D0}, D1 calling the kernel directly,Wait(0)meaning no timeout). It comes from the author's disassembly, and I did not check it against the module or a console.D0releases the kernel semaphore. As the author says, the test can't observe this, and I did not add a check.