feat(libc): implement the C11 mtx and cnd functions #492
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/492"
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 Amielle-Inside on 2026-10-04T08:51:25Z.
What
Twelve libc imports that shipped as throwing
libcCyberUnknown*stubs (PR #358) are identified by NID hash and implemented incore/libs/prx/libc/src/Threads.cpp:YaHc3GS7y7g_Mtx_initiS4aWbUonl0_Mtx_lock_Mtx_trylockgTuXQwP9rrs_Mtx_unlock5Lf51jvohTQ_Mtx_destroySreZybSRWpU_Cnd_initvEaqE-7IZYc_Cnd_waitVsP3daJgmVA_Cnd_broadcast7yMFgcS8EPA_Cnd_destroyH+8UBOwfScI__powidf2JhVR7D4Ax6Y_WStoulCyXs2l-1kNA_IswctypeYe20uNnlglAabsIdentification is hash-verified: every name was run forward through the project's own
Nid::ComputeSHA-1/salt/base64 algorithm and all 13 NIDs match (22/22 across all previously-unknown libc exports, including names left unchanged here). The mtx/cnd implementations keep host-native state (recursive/plainstd::mutex,std::condition_variable) behind an atomic slot in the guest-provided storage; disassembly of a title-bundledlibc.prxshows_Mtx_init/_Cnd_initare pointer-passing wrappers, so the storage only needs to hold the private pointer. Error codes follow C11 (thrd_success/thrd_busy/thrd_error), mapped from the disassembled wrappers.Left untouched on purpose:
qdGFBoLVNKI(quick_exit— open PR #405 owns it),DiGVep5yB5w(_ZSt13_Execute_once...),Pu0Ecyk-7FU/u2tMGOLaqnE(libc++ internals that throw), and the three file-idiom Cyberpunk stubs whose aerolib names (strnlen_s/vsnprintf_s/vsscanf_s) contradict the observed signatures in #358 — those need signature confirmation first.Tested
Linux (Bazzite/Fedora Atomic, GCC in distrobox),
cmake --build buildclean (568/568),ctest173/173 passed, 0 failed (skips are pre-existing GPU tests).nid_patcheroutput verified: all 13 renamed symbols hash to the exact previous NIDs in the patchedlibc.prx(readelf), no duplicate definitions introduced. Runtime smoke: PPSA16951 (Tales of Xillia Remastered, Unity IL2CPP title with bundled libc.prx) relinked with this build — C11 imports resolve and the previoussymbol lookup errorchain is gone; the title still stops at the #476 bundled-libc iostream init, which is unrelated to this change.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T09:52:44Z.
Thanks for identifying these, the names match the NIDs. I compared the code with Sony's own libc.prx (the one PPSA21564 ships in sce_module), and a few things differ from it:
_Mtx_init: Sony's version makes the mutex recursive whentype & 0x100is set (Dinkumware's_Mtx_recursive, throughscePthreadMutexattrSettype(attr, 2)) and ignores the other bits, so_Mtx_plain(1),_Mtx_tryand_Mtx_timedgive a normal mutex. Here bit 0 selects recursive, so a plain mutex becomes recursive and a recursive one throws._Thrd_success0,_Thrd_nomem1,_Thrd_busy3,_Thrd_error4):_Mtx_trylockreturns 3 on EBUSY and 4 on any other error, and_Mtx_lockreturns 3 on EDEADLK and 4 otherwise. Here a busy_Mtx_trylockreturns 1.absisint abs(int)(mov eax, edi; neg eax; cmovs eax, edi). With alongparameter the upper half of rdi is undefined for an int argument; with a zero upper half, abs(-5) returns 0xfffffffb, which the caller reads as -5.__powidf2is compiler-rt's square and multiply, and INT_MIN works (it returns 1/r). Multiplyingexponenttimes rounds differently from x^4 on, and large exponents take very long._WStoulreads 16-bit characters (the guest wchar_t is UTF-16, seeWindowsFormatting.hpp), sostd::wcstoulis only right on a Windows host. On Linux it reads two guest characters as one._Iswctype's second argument is an index into Dinkumware's class table (Sony's_WStoulcalls_Iswctype(c, 9)for white space), not a hostwctype_t. On glibc awctype_tis a pointer into the locale data, soiswctype(c, 9)would crash.Since Sony's wrappers call
scePthreadMutex*andscePthreadCond*, calling the libkernel versions here would also give the same error behaviour. Two smaller things:mtxMagicandmtxValidare unused, andabs,__powidf2,_WStouland_Iswctypearen't part of the mutex and condition topic, so I'd move them to their own PR (or leave them throwing until they're exact).Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-04T09:53:20Z.
Progress report (
9f9a8f5→88e1756)📈 System libraries: 77.73% (+0.38%, +13 implemented, -12 removed functions)
✅ 13 implemented
_Cnd_broadcast_nid_postfix_Cnd_destroy_nid_postfix_Cnd_init_nid_postfix_Cnd_wait_nid_postfix_Iswctype_nid_postfix_Mtx_destroy_nid_postfix_Mtx_init_nid_postfix_Mtx_lock_nid_postfix_Mtx_trylock_nid_postfix_Mtx_unlock_nid_postfix_WStoul_nid_postfix__powidf2_nid_postfixabs_nid_postfix🗑️ 12 removed
libcCyberUnknown02libcCyberUnknown03libcCyberUnknown05libcCyberUnknown07libcCyberUnknown08libcCyberUnknown09libcCyberUnknown10libcCyberUnknown11libcCyberUnknown12libcCyberUnknown13libcCyberUnknown14libcCyberUnknown16Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-07T01:20:39Z.
Following up on this one: are you still working on it? It now conflicts with
main, so it needs a rebase, and the six points above are still open (the_Mtx_inittype bits, the Dinkumware return codes,abs,__powidf2,_WStouland_Iswctype).The owner has also asked us to check on every PR which title needs it. Do you know a title that calls the C11
mtx/cndfunctions? If so, please name it with the log line where it stops onmain. Splitting offabs,__powidf2,_WStouland_Iswctypeinto their own PR (or leaving them throwing) would keep this one to the mutex and condition topic.If you don't have time for it, no problem, just say so and we'll close it for now; it can be reopened later.
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-07T11:45:46Z.
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.
main)abs_nid_postfixImported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-07T14:24:12Z.
Closing this one, for these reasons:
libc.prx,_Mtx_trylock(k6pGNMwJB08) returns 0 on success, 3 (_Thrd_busy) whenscePthreadMutexTrylockreturns0x80020010and 4 otherwise. Here, a busy mutex returns 1 (Threads.cpp:93-94). This matches point 2 of @oneandonlydean's review.main.abs,__powidf2,_WStouland_Iswctypeare unrelated to mtx/cnd, andabsis now also in #1097.You're welcome to reopen (or open a new PR) with only the mtx/cnd functions, following Sony's wrappers (
type & 0x100→ recursive, Dinkumware return codes, ideally through the libkernelscePthreadMutex*/scePthreadCond*), plus a guest test that checks the return codes.Pull request closed