feat(libcinternal): run thread_local destructors at thread exit #641

Merged
Celegans12 merged 1 commit from upstream-pr/641 into main 2026-10-05 16:29:12 +00:00
Celegans12 commented 2026-10-05 10:36:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Celegans12 on 2026-10-05T10:36:01Z.


What

A title that bundles its own libc.prx stops at startup on three unresolved libSceLibcInternalExt imports, which that libc uses for thread_local destructors.

  • _sceLibcInternalThreadAtexit (qBS714-Jr3g), int(void (*)(void*), void*, void*): appends {destructor, object, dso} to the calling thread's list and returns 0. The first call installs the pass as libkernel's thread exit hook (sceKernelSetThreadDtors).
  • _sceLibcInternalThreadDtors (OQ-dzhlnM28), void(): runs the thread's destructors newest first, including ones registered meanwhile.
  • _sceLibcInternalForceTlsDestructor (tB59hFLH3SA): throws; behaviour unknown.

Source: FreeBSD lib/libc/stdlib/cxa_thread_atexit_impl.c, except:

  • A null or non-image destructor throws; FreeBSD skips it as code of an unloaded DSO, while here unloading goes through the unimplemented _sceLibcInternalForceTlsDestructor.
  • An allocation failure throws instead of failing with ENOMEM.
  • Any loaded image is accepted, not only an executable segment of the dso's image.

Call sites in PPSA26344 v1.008 libc.prx: __cxa_thread_atexit (0x7ecd0) forwards to the first, exit calls the second (0x3ab19), __cxa_finalize passes a dso's module handle to the third (0x3aa5c).

Neither the executable nor libc.prx imports sceKernelSetThreadDtors (+mR4CLzg3S0) or _sceKernelSetThreadDtors (rNhWz+lvOMU), so the hook is free; if another module took it, the first registration throws. libc's LibcInternalExtCxaThreadAtexit (Ydzc7qlDvCw) is unchanged: neither module imports it, and it keeps using the host __cxa_thread_atexit.

TechnicalDebt: the assumptions under "Unknown function info"; under "Functional", threads libkernel did not start run destructors only via _sceLibcInternalThreadDtors. A new CMakeLists.txt registers the test and links ${CMAKE_DL_LIBS} (dladdr on Linux).

Tested

  • Windows 11, MinGW-w64 GCC 15.2.0, based on main 53dc19cf: configure -DBUILD_TESTING=ON, full build, --target libs, ctest:

  • 285/285 tests pass (ctest --output-on-failure)

  • New guest_thread_atexit covers:

    • newest-first order, including a destructor that registers another;
    • an empty second pass;
    • a scePthreadCreate thread's destructors running on that thread before join;
    • the throw for null and non-image destructors.

    It does not link on main.

  • PPSA26344 v1.008: the three imports that stop startup on main resolve, and the title gets past startup. That run predates one change: a non-image destructor was then logged and skipped instead of throwing.

  • Not tested: Linux (CI), module unloading, console behaviour.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4743601685 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/641). Originally posted by [Celegans12](https://github.com/Celegans12) on 2026-10-05T10:36:01Z. --- ### What A title that bundles its own `libc.prx` stops at startup on three unresolved libSceLibcInternalExt imports, which that libc uses for `thread_local` destructors. - `_sceLibcInternalThreadAtexit` (`qBS714-Jr3g`), `int(void (*)(void*), void*, void*)`: appends `{destructor, object, dso}` to the calling thread's list and returns 0. The first call installs the pass as libkernel's thread exit hook (`sceKernelSetThreadDtors`). - `_sceLibcInternalThreadDtors` (`OQ-dzhlnM28`), `void()`: runs the thread's destructors newest first, including ones registered meanwhile. - `_sceLibcInternalForceTlsDestructor` (`tB59hFLH3SA`): throws; behaviour unknown. Source: FreeBSD `lib/libc/stdlib/cxa_thread_atexit_impl.c`, except: - A null or non-image destructor throws; FreeBSD skips it as code of an unloaded DSO, while here unloading goes through the unimplemented `_sceLibcInternalForceTlsDestructor`. - An allocation failure throws instead of failing with `ENOMEM`. - Any loaded image is accepted, not only an executable segment of the dso's image. Call sites in PPSA26344 v1.008 `libc.prx`: `__cxa_thread_atexit` (0x7ecd0) forwards to the first, `exit` calls the second (0x3ab19), `__cxa_finalize` passes a dso's module handle to the third (0x3aa5c). Neither the executable nor `libc.prx` imports `sceKernelSetThreadDtors` (`+mR4CLzg3S0`) or `_sceKernelSetThreadDtors` (`rNhWz+lvOMU`), so the hook is free; if another module took it, the first registration throws. libc's `LibcInternalExtCxaThreadAtexit` (`Ydzc7qlDvCw`) is unchanged: neither module imports it, and it keeps using the host `__cxa_thread_atexit`. TechnicalDebt: the assumptions under "Unknown function info"; under "Functional", threads libkernel did not start run destructors only via `_sceLibcInternalThreadDtors`. A new `CMakeLists.txt` registers the test and links `${CMAKE_DL_LIBS}` (`dladdr` on Linux). ### Tested - Windows 11, MinGW-w64 GCC 15.2.0, based on `main` 53dc19cf: configure `-DBUILD_TESTING=ON`, full build, `--target libs`, `ctest`: - 285/285 tests pass (`ctest --output-on-failure`) - New `guest_thread_atexit` covers: - newest-first order, including a destructor that registers another; - an empty second pass; - a `scePthreadCreate` thread's destructors running on that thread before join; - the throw for null and non-image destructors. It does not link on `main`. - PPSA26344 v1.008: the three imports that stop startup on `main` resolve, and the title gets past startup. That run predates one change: a non-image destructor was then logged and skipped instead of throwing. - Not tested: Linux (CI), module unloading, console behaviour. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-05 16:06:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-05T16:06:47Z.


Progress report (e248f40 → 2dea716)

📉 System libraries: 79.19% (-0.01%, +2 implemented, +1 declared functions)

✅ 2 implemented
Library Name
libSceLibcInternal _sceLibcInternalThreadAtexit_nid_postfix
libSceLibcInternal _sceLibcInternalThreadDtors_nid_postfix
🆕 1 declared as stubs
Library Name
libSceLibcInternal _sceLibcInternalForceTlsDestructor_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5998276297 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/641#issuecomment-5998276297). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-05T16:06:47Z. --- <!-- progress-report --> ### Progress report (e248f40 → 2dea716) 📉 **System libraries**: 79.19% (-0.01%, +2 implemented, +1 declared functions) <details> <summary>✅ 2 implemented</summary> | Library | Name | | - | - | | libSceLibcInternal | `_sceLibcInternalThreadAtexit_nid_postfix` | | libSceLibcInternal | `_sceLibcInternalThreadDtors_nid_postfix` | </details> <details> <summary>🆕 1 declared as stubs</summary> | Library | Name | | - | - | | libSceLibcInternal | `_sceLibcInternalForceTlsDestructor_nid_postfix` | </details>
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!641
No description provided.