feat(libc): implement the Dinkumware mtx and cnd functions #1952
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1952"
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 overlordxrz-source on 2026-10-09T01:50:51Z.
What
Cyberpunk 2077 (PPSA04029) imports eight Dinkumware thread functions that
libconly has as throwinglibcCyberUnknown*stubs. Each name hashes to its stub's NID withtools/nid_names.py'scompute_nid, andpython3 tools/nid_names.pyalready lists them as rename-ready:YaHc3GS7y7glibcCyberUnknown12_Mtx_init5Lf51jvohTQlibcCyberUnknown03_Mtx_destroyiS4aWbUonl0libcCyberUnknown14_Mtx_lockgTuXQwP9rrslibcCyberUnknown13_Mtx_unlockSreZybSRWpUlibcCyberUnknown10_Cnd_init7yMFgcS8EPAlibcCyberUnknown05_Cnd_destroyvEaqE-7IZYclibcCyberUnknown16_Cnd_waitVsP3daJgmVAlibcCyberUnknown11_Cnd_broadcastIt also adds
_Mtx_trylock(k6pGNMwJB08, which Minecraft Legends PPSA05510 imports according to #268) and_Cnd_signal(0uuqgRz9qfo, imported by GTA III Definitive, PPSA05510 and Deathloop according to #267, #268 and #272). The eight Cyberpunk NIDs are also in the import lists of #267, #268 and #272.This follows the review on #492, which was closed with an invitation to reopen with only the mtx/cnd functions:
_Mtx_initmakes the mutex recursive only whentype & 0x100is set._Mtx_plain,_Mtx_tryand_Mtx_timedgive a plain mutex._Mtx_lockand_Mtx_trylockreturn 3 (_Thrd_busy) when a plain mutex is relocked by its owner or when trylock finds it busy._Mtx_unlockand_Cnd_waitreturn 4 (_Thrd_error) when the caller does not own the mutex, and so does a call with a null pointer._Mtx_t/_Cnd_t, which holds one pointer to host state. This is the pre-VS2015 Dinkumware<xthreads.h>ABI (_Mtx_lock(_Mtx_t *),_Cnd_wait(_Cnd_t *, _Mtx_t *)) that fpPS4's reverse-engineeredlibSceLibcInternalfollows under the same NIDs. A zeroed one is set up on first use, the way libkernel handles a static mutex._Mtx_destroyand_Cnd_destroyreturnvoidand clear the slot._Cnd_waiton a recursive mutex releases every level and restores the count after waking.The review suggested going through libkernel's
scePthreadMutex*/scePthreadCond*. That isn't possible here becauselibkernellinkslibc(core/libs/prx/libkernel/CMakeLists.txt), solibc/src/Threads.cppkeeps its own host state and returns the codes that the error-checking pthread mutex would produce through Sony's wrapper.Not included, because they are separate topics:
abs(Ye20uNnlglA, in #1097),__powidf2,_WStoul,_Iswctype,vsnprintf_sandvsscanf_s. #1498 also changes these eight stubs, without renaming them. ItslibcCyberUnknown13locks and itslibcCyberUnknown14unlocks, but the NIDs say 13 is_Mtx_unlockand 14 is_Mtx_lock.Log
I don't own Cyberpunk 2077, so I can't paste the line where PPSA04029 stops on
main, and I found no issue or discussion that names one of these NIDs as a stop. The evidence is the PPSA04029 relinker import list in #266, where all eight NIDs appear, and the existing stubs onmainthat were added for this title in #358. If someone with the title can post theNotImplementedline it reaches onmain, I'll add it here.Tested
aarch64 Debian 13 host. The full project can't be built here because it needs x86-64.
core/libs/tests/GuestLibcThreads.cpp(guest_libc_threads), linked natively againstThreads.cpp, passes 30 out of 30 runs, and also passes under ASan and UBSan. It checks plain types 1, 2, 3 and 5 (relock and busy trylock return 3; unlock and trylock from another thread return 4 and 3; a second unlock returns 4), a recursive mutex with nested lock and trylock that blocks another thread until fully released, eight-thread contention, zeroed slots, broadcast waking four waiters, signal handing out three tokens, waiting on an unowned mutex returning 4, a wait on a twice-locked recursive mutex restoring its count, and null arguments.Threads.cpp,Export.cppand the test compile for x86_64-linux-gnu and x86_64-windows-gnu with-Wall -Wextra(Zig 0.17 clang). The only warning is the existing-Wreturn-type-c-linkagefromGeneral.hpp.python3 tools/check_conventions.py --base origin/mainpasses.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 github-actions[bot] on 2026-10-09T01:51:17Z.
Progress report (
f145139→9a33c0b)📈 System libraries: 85.03% (+0.27%, +10 implemented, -8 removed functions)
✅ 10 implemented
_Cnd_broadcast_nid_postfix_Cnd_destroy_nid_postfix_Cnd_init_nid_postfix_Cnd_signal_nid_postfix_Cnd_wait_nid_postfix_Mtx_destroy_nid_postfix_Mtx_init_nid_postfix_Mtx_lock_nid_postfix_Mtx_trylock_nid_postfix_Mtx_unlock_nid_postfix🗑️ 8 removed
libcCyberUnknown03libcCyberUnknown05libcCyberUnknown10libcCyberUnknown11libcCyberUnknown12libcCyberUnknown13libcCyberUnknown14libcCyberUnknown16Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T02:08:25Z.
Review: CHANGES_REQUESTED
Blocking ABI question: every operation export in
Threads.cppcurrently acceptsLibcMutex**/LibcCondition**and dereferences the argument as a slot. The corresponding Dinkumware handle types are_Mtx_t = _Mtx_internal_imp_t*and_Cnd_t = _Cnd_internal_imp_t*;_Mtx_lock,_Mtx_trylock,_Mtx_unlock,_Mtx_destroy,_Cnd_wait,_Cnd_signal,_Cnd_broadcast, and_Cnd_destroytake those handles by value. Only_Mtx_initand_Cnd_inittake pointer-to-handle. The official declarations are in Microsoft STL xthreads.h and the handle definitions in __msvc_threads_core.hpp.The new test declares all operations as
void**and passes&mutex/&condition, so it reproduces the implementation's assumption instead of checking the caller ABI. Please provide disassembly or another source showing that the PS5 wrappers intentionally use pointer-to-slot for these non-init functions. If they use the Dinkumware handle ABI, adjust the export signatures and tests so callers pass the stored handle itself; otherwise a real title passes an object pointer that this code interprets as a pointer slot.Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-09T03:40:48Z.
Thanks for checking this against the headers. I looked into it, and the PS4/PS5 libc uses the older Dinkumware signatures, where every
_Mtx_*/_Cnd_*call takes a pointer to the handle. The by-value form you quoted comes from Microsoft's rework of<xthreads.h>, which shipped after the Dinkumware version Sony's SDK is based on.int _Mtx_lock(_Mtx_t *),_Mtx_trylock(_Mtx_t *),_Mtx_unlock(_Mtx_t *),void _Mtx_destroy(_Mtx_t *),_Cnd_wait(_Cnd_t *, _Mtx_t *),_Cnd_signal(_Cnd_t *),_Cnd_broadcast(_Cnd_t *),void _Cnd_destroy(_Cnd_t *), with_Mtx_t = struct _Mtx_internal_imp_t *(lines 40-71): http://sandbox.hlt.bme.hu/~gaebor/STLdoc/VS2013/xthreads_8h_source.html. The callers in that library pass the address of the member, for example_Mtx_lockX(&_Mtx),_Cnd_waitX(&_Cnd, &_Mtx)and_Mtx_destroy(&_Mtx)incondition_variable_any: http://sandbox.hlt.bme.hu/~gaebor/STLdoc/VS2013/classcondition__variable__any.html. You can see the change in microsoft/STL itself. The initial commit still has_Mtx_init(_Mtx_t*, int)paired with by-value_Mtx_destroy(_Mtx_t)and_Mtx_init_in_situ(xthreads.h@2195148, L77-L109), and the lines you linked onmainno longer have_Mtx_init/_Cnd_initat all.ps4__Mtx_lock(pmtx:pp_mtx),_trylock,_unlock,_destroy,_timedlockand_current_ownsall takepp_mtx = ^p_mtxand start with_mtx := pmtx^._Mtx_lockeven reloadspmtx^after each cond wait (ps4_mtx_internal.pas L19, L36-L43, L167-L189). They are registered under the same NIDs this PR exports. For example$892E1A59B5289E5DisiS4aWbUonl0=_Mtx_lock, and$61A1DCDC64BBCBB8isYaHc3GS7y7g=_Mtx_init(ps4_libscelibcinternal.pas L450-L457).<threads.h>in Dinkumware's library, wheremtx_lock(mtx_t *)maps straight onto_Mtx_lock. That only works if_Mtx_locktakes a pointer.fpPS4 doesn't implement
_Cnd_*. I'm following the same header for those, where_Cnd_wait(_Cnd_t *, _Mtx_t *)and the other calls also take the handle by address.So I've kept the
LibcMutex**/LibcCondition**exports, and the test still callsinit(&h)and thenlock(&h), which is what Sony'sstd::mutexdoes with its_Mtx_tmember. I don't have a PS5 libc disassembly to paste. If you have one that shows a by-value_Mtx_lock, I'll switch it.Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:30:40Z.
Closing: only import lists show these, and Cyberpunk's PPSA03974 build ships its own
libc.prx, which its imports bind to first (#1980). Reopen with thenot implementedline from main when a title stops here.Pull request closed