Skip to content

feat(libc): implement the C11 mtx and cnd functions - #492

Closed
Amielle-Inside wants to merge 1 commit into
boykopovar:mainfrom
Amielle-Inside:feat/libc-c11-threads
Closed

Amielle-Inside wants to merge 1 commit into
boykopovar:mainfrom
Amielle-Inside:feat/libc-c11-threads

Conversation

@Amielle-Inside

Copy link
Copy Markdown

What

Twelve libc imports that shipped as throwing libcCyberUnknown* stubs (PR #358) are identified by NID hash and implemented in core/libs/prx/libc/src/Threads.cpp:

NID Function
YaHc3GS7y7g _Mtx_init
iS4aWbUonl0 _Mtx_lock
(new export) _Mtx_trylock
gTuXQwP9rrs _Mtx_unlock
5Lf51jvohTQ _Mtx_destroy
SreZybSRWpU _Cnd_init
vEaqE-7IZYc _Cnd_wait
VsP3daJgmVA _Cnd_broadcast
7yMFgcS8EPA _Cnd_destroy
H+8UBOwfScI __powidf2
JhVR7D4Ax6Y _WStoul
CyXs2l-1kNA _Iswctype
Ye20uNnlglA abs

Identification is hash-verified: every name was run forward through the project's own Nid::Compute SHA-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/plain std::mutex, std::condition_variable) behind an atomic slot in the guest-provided storage; disassembly of a title-bundled libc.prx shows _Mtx_init/_Cnd_init are 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 build clean (568/568), ctest 173/173 passed, 0 failed (skips are pre-existing GPU tests). nid_patcher output verified: all 13 renamed symbols hash to the exact previous NIDs in the patched libc.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 previous symbol lookup error chain is gone; the title still stops at the #476 bundled-libc iostream init, which is unrelated to this change.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes — written with AI assistance (GLM 5.3 via Hermes Agent); every line reviewed and tested by the PR author

Twelve libc imports that shipped as throwing libcCyberUnknown stubs are
identified by NID hash and implemented: _Mtx_init, _Mtx_lock, _Mtx_trylock,
_Mtx_unlock, _Mtx_destroy, _Cnd_init, _Cnd_wait, _Cnd_broadcast, _Cnd_destroy,
__powidf2, _WStoul, _Iswctype and abs. The mtx/cnd state is host-native
(recursive/plain mutex, condition_variable) stored through an atomic slot in
the guest-provided storage, matching the pointer-sized PS5 layout observed in
bundled libc binaries. The stub exports for the now-implemented NIDs are
removed from Export.cpp; quick_exit and _Execute_once stay NotImplemented.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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:

  1. _Mtx_init: Sony's version makes the mutex recursive when type & 0x100 is set (Dinkumware's _Mtx_recursive, through scePthreadMutexattrSettype(attr, 2)) and ignores the other bits, so _Mtx_plain (1), _Mtx_try and _Mtx_timed give a normal mutex. Here bit 0 selects recursive, so a plain mutex becomes recursive and a recursive one throws.
  2. The return codes are Dinkumware's (_Thrd_success 0, _Thrd_nomem 1, _Thrd_busy 3, _Thrd_error 4): _Mtx_trylock returns 3 on EBUSY and 4 on any other error, and _Mtx_lock returns 3 on EDEADLK and 4 otherwise. Here a busy _Mtx_trylock returns 1.
  3. abs is int abs(int) (mov eax, edi; neg eax; cmovs eax, edi). With a long parameter 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.
  4. __powidf2 is compiler-rt's square and multiply, and INT_MIN works (it returns 1/r). Multiplying exponent times rounds differently from x^4 on, and large exponents take very long.
  5. _WStoul reads 16-bit characters (the guest wchar_t is UTF-16, see WindowsFormatting.hpp), so std::wcstoul is only right on a Windows host. On Linux it reads two guest characters as one.
  6. _Iswctype's second argument is an index into Dinkumware's class table (Sony's _WStoul calls _Iswctype(c, 9) for white space), not a host wctype_t. On glibc a wctype_t is a pointer into the locale data, so iswctype(c, 9) would crash.

Since Sony's wrappers call scePthreadMutex* and scePthreadCond*, calling the libkernel versions here would also give the same error behaviour. Two smaller things: mtxMagic and mtxValid are unused, and abs, __powidf2, _WStoul and _Iswctype aren'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).

@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown

Progress report (9f9a8f5 → 88e1756)

📈 System libraries: 77.73% (+0.38%, +13 implemented, -12 removed functions)

✅ 13 implemented
Library Name
libc _Cnd_broadcast_nid_postfix
libc _Cnd_destroy_nid_postfix
libc _Cnd_init_nid_postfix
libc _Cnd_wait_nid_postfix
libc _Iswctype_nid_postfix
libc _Mtx_destroy_nid_postfix
libc _Mtx_init_nid_postfix
libc _Mtx_lock_nid_postfix
libc _Mtx_trylock_nid_postfix
libc _Mtx_unlock_nid_postfix
libc _WStoul_nid_postfix
libc __powidf2_nid_postfix
libc abs_nid_postfix
🗑️ 12 removed
Library Name
libc libcCyberUnknown02
libc libcCyberUnknown03
libc libcCyberUnknown05
libc libcCyberUnknown07
libc libcCyberUnknown08
libc libcCyberUnknown09
libc libcCyberUnknown10
libc libcCyberUnknown11
libc libcCyberUnknown12
libc libcCyberUnknown13
libc libcCyberUnknown14
libc libcCyberUnknown16

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 4, 2026
@adriadam10 adriadam10 mentioned this pull request Oct 5, 2026
9 tasks done
oneandonlydean pushed a commit that referenced this pull request Oct 5, 2026
The DQbtGaBKlaw import was a throwing stub (libcCyberUnknown17). It is
the NID of "strnlen_s", which C11 K.3.7.4.4 defines as strnlen that
returns 0 for a null pointer.

The removed stub comment from #358 described its call site as
float -> float, which contradicts the hash-verified name; #492 left the
stub untouched for that reason.
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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_init type bits, the Dinkumware return codes, abs, __powidf2, _WStoul and _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/cnd functions? If so, please name it with the log line where it stops on main. Splitting off abs, __powidf2, _WStoul and _Iswctype into 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.

@github-actions github-actions Bot mentioned this pull request Oct 7, 2026
9 tasks done
@github-actions

github-actions Bot commented Oct 7, 2026

Copy link
Copy Markdown

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.

Pull request Conflicting files (lines on main) Same exports Same new files
#1097 abs_nid_postfix

@adriadam10

Copy link
Copy Markdown
Collaborator

Closing this one, for these reasons:

  • The description says the return codes are "mapped from the disassembled wrappers", but they aren't. In Sony's libc.prx, _Mtx_trylock (k6pGNMwJB08) returns 0 on success, 3 (_Thrd_busy) when scePthreadMutexTrylock returns 0x80020010 and 4 otherwise. Here, a busy mutex returns 1 (Threads.cpp:93-94). This matches point 2 of @oneandonlydean's review.
  • The six points of that review (2026-10-04) and the follow-up are still unanswered, and the PR conflicts with main.
  • It mixes topics: abs, __powidf2, _WStoul and _Iswctype are unrelated to mtx/cnd, and abs is now also in feat(libc): implement abs #1097.
  • The PR adds no tests, so none of this behaviour is pinned.

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 libkernel scePthreadMutex*/scePthreadCond*), plus a guest test that checks the return codes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

build libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants