feat(libSceNpCommon): implement sce::np::Mutex and sce::np::Cond #13130
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/13130"
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-30T03:44:44Z.
What
Implements 26
sce::np::Mutexandsce::np::Condexports fromlibSceNpCommon.sprxof firmware 13.60 (read from disassembly, not measured on a console). Five of them wereNotImplementedstubs with placeholderint (void)declarations:Cond::ctor,Cond::Init,Cond::D0,Cond::D1andMutex::Destroy. The other 21 are exported by the module but were missing from AnyPS5.Mutex::IsInitstays a stub, because open #12823 implements it.Both objects are 24 bytes and are declared in
Api.hpp. Each vtable is{0, 0, D1, D0}.NpMutexObject(vtable,PthreadMutex mutex,bool isInitat +0x10):isInit = false.ctor: clears onlyisInit.dtor: callscePthreadMutexDestroywhen initialized. D1/D2 also reset the vtable.D0usessceNpMutexDestroyand then frees throughsce::np::Object::operator delete(void*), so it is stacked on #12836.Init(const char* name, uint32_t flags):0x80559E03when already initialized;sceNpMutexInit(&mutex, name, flags & 1), returns its error, or setsisInit.Destroy: destroys when initialized and always clearsisInit.Lock/Unlock: forward tosceNpMutexLock/sceNpMutexUnlock.TryLock: returns false on0x8055800F(busy) and true on success.NpCondObject(vtable,PthreadCond cond,PthreadMutex* mutex):ctor: clear both members.IsInitismutex != nullptr.Init(const char* name, Mutex* mutex):0x80559E03when already initialized;sceNpCondInit(&cond, name, 0)and stores the address of theMutex's kernel mutex (the object plus 8).Destroy: destroys when initialized and clears both members.dtorandD0: work as forMutex, usingscePthreadCondDestroy/sceNpCondDestroy.Wait(uint32_t usec),Signal,SignalAll,SignalTo(pthread*): forward tosceNpCondTimedwait(&cond, mutex, usec)/sceNpCondSignal/sceNpCondSignalAll/sceNpCondSignalToand return only negative results (result & (result >> 31)in the module).Where the module halts:
Init:Lock,UnlockandTryLockassertIsInit()and panic. AnyPS5 throwsstd::invalid_argument.HALT_ASSERT(ret == 0)after the kernel call (other than busy forTryLock), andLock/Unlockcallabort. AnyPS5 throwsstd::runtime_error, following the libSceJson2 exports.The debt file records both, together with two module behaviours this PR keeps:
Cond::Waitdoes not checkIsInit;Cond::Initwith a nullMutexstores the address 8 and then counts as initialized.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 s each.TestMutexAndCondObjectsinGuestNpCommonRuntime.cppuses real kernel mutexes, condition variables and threads to check:0x80559E03secondInit;Init;TryLock, while flags3make it recursive;Destroymay be repeated;Cond::Initkeeps&mutex.mutex;Waitreturning0x8055800B;SignalandSignalToeach waking a waiting thread;Initwith a null mutex;Object::operator new.Mutation check: I changed 8 single lines of the new functions, one at a time; the test fails for all 8. The changes were:
TryLockreporting busy as success;Waithiding its error;SignalToignoring the thread;Mutex::DestroyandCond::Destroynot clearing their state;Initcheck.No firmware differential: these methods wrap kernel objects. 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)D0usessce::np::Object::operator deletefrom #12836, which is merged;mainis merged in.NotImplementeddeclarations for 21 of these exports, the ones missing frommain, so whichever lands second must drop them fromInventoryExports.cpp; if #13120 lands first, I will do it here.NP_ERROR_OBJECT_ALREADY_INITIALIZEDconstant in the same place; #13270 and #13277 declareDepends onthis one and keep its copy.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 CnRJay on 2026-11-01T19:50:17Z.
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 XairRrR on 2026-11-03T23:51:57Z.
Review: APPROVED
Reviewed — approved
The new
sce::np::Mutexandsce::np::Condexports merge and build cleanly, they behave as the description says, and the new test passes and catches both mutations I tried.What I ran
Head
4d75a8a77a135d508f64b595566d96301aa0a3dd,main1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged with--no-ff(treee3626249063bd3b8bdd6f9b1969b9db6504a99de). Debian 13 VM, GCC 14.2.0, Release, Ninja.Results
git diff --checkcheck_conventions.pylibs(script)unwrap_self_UnwrapTest_test_new_output_inherits_the_destination_directory_group), which also fails onmainguest_np_common_runtime×3public_headersmaintoo, so this PR does not cause itNegative control
You can't just put the production files back from
main: the test calls the new typed declarations and no longer compiles against the oldint (void)stubs. JJenkx's comment already noted this. I mutated the production code instead:TryLockreturnstrueonNP_ERROR_MUTEX_BUSY(applied together with the offset mutation): the test aborts witha held plain mutex cannot be taken again.Cond::Initstores theMutexaddress without+ offsetof(NpMutexObject, mutex)(applied alone): the test aborts witha condition keeps the kernel mutex of its sce::np::Mutex.I restored
Runtime.cppfrom the merged tree.git status --porcelaincame back empty, and after the rebuild the test passed again.Findings
Blocking
None.
Non-blocking
core/libs/prx/libSceNpCommon/Runtime.cpp:1398-1399: the new block has no blank line betweenCond::SignalToandMemoryStreamReaderC1. The rest of the file puts one between functions. This is cosmetic.Code notes (checked, no action needed)
vtable = &NpXxxVtable[2], sovtable[0]is D1 andvtable[1]is D0. That matches the{0, 0, D1, D0}layout in the description, and the test callsvtable[1]and checks that the memory is freed through the stored allocator.std::min(result, 0)gives the same value as the module'sresult & (result >> 31)for every 32-bitint.Mutex::Initpassesflags & 1, andsceNpMutexInit(Export.cpp:83-92) turns bit 0 intoMUTEX_TYPE_RECURSIVE. The test covers this with flags3.TryLockcompares againstNP_ERROR_MUTEX_BUSY(0x8055800F,Runtime.cpp:472), the valuesceNpMutexTryLockmaps EBUSY to.Initand on kernel failure take the place of module halts, and both are recorded indocs/dev/debt/libSceNpCommon.md.PendingExportsRound7.cpp,Api.hppand the debt list, andpublic_headerspasses.static_assert(sizeof(...) == 0x18)next to each struct would lock that ABI in, but it isn't required.Not verified
libSceNpCommon.sprxdisassembly and no console. The contract comes from the author's reading of the disassembly.Cond::WaitbeforeInitsends a null mutex toscePthreadCondTimedwait, andInitwith a nullMutexstores the address 8. The debt file records both, as the module's behaviour. The test covers the second case only up toDestroy, and nothing waits on such a condition, so I did not check what the AnyPS5 kernel does with those pointers.8acf2c4809).