feat(libkernel): implement the rtld thread atexit reference count #722

Merged
alfalfa-star merged 1 commit from upstream-pr/722 into main 2026-10-07 02:27:44 +00:00
alfalfa-star commented 2026-10-05 18:00:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by alfalfa-star on 2026-10-05T18:00:38Z.


What

Implement _sceKernelRtldThreadAtexitIncrement and _sceKernelRtldThreadAtexitDecrement (and the variants without the leading underscore), which threw.

A title's own libc.prx calls Increment from __cxa_thread_atexit with the dso address and fails the registration when it returns nonzero (seen in a debugger on PPSA14632). The previous uint64_t* signature was wrong: the argument is an address inside the module that registers the thread_local destructor. As in fpPS4, Increment takes a reference on the loaded image that contains the address, so the image stays mapped while its destructors are pending, and Decrement releases one. On Linux the reference is a dlopen(RTLD_NOLOAD) handle of the image found through dladdr1; on Windows it is GetModuleHandleExW(FROM_ADDRESS). The main executable is counted but needs no handle. An address outside every loaded image and a Decrement without a matching Increment throw. The assumed semantics are recorded in TechnicalDebt.

Tested

  • Arch Linux, GCC 16.2, Release. guest_dynamic_loader now pins the fixture module, closes every handle, and still calls into it; with the pin disabled the test segfaults. It also checks the main executable, the unbalanced Decrement and an address outside every image. Full ctest: 290/290 apart from the three wave64 pixel tests that fail on main on this GPU (RX 9070 XT, RADV).
  • LEGO Horizon Adventures (PPSA14632), together with #672 and local throwing stubs for its missing imports: the title's __cxa_thread_atexit now succeeds and it continues to sceKernelGetModuleInfoFromAddr (separate PR).
  • Windows path not built or tested.

#717 also edits core/libs/tests/GuestDynamicLoader.cpp (a different topic); whichever lands second needs a small rebase there.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4748935538 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/722). Originally posted by [alfalfa-star](https://github.com/alfalfa-star) on 2026-10-05T18:00:38Z. --- ### What Implement `_sceKernelRtldThreadAtexitIncrement` and `_sceKernelRtldThreadAtexitDecrement` (and the variants without the leading underscore), which threw. A title's own `libc.prx` calls Increment from `__cxa_thread_atexit` with the `dso` address and fails the registration when it returns nonzero (seen in a debugger on PPSA14632). The previous `uint64_t*` signature was wrong: the argument is an address inside the module that registers the `thread_local` destructor. As in fpPS4, Increment takes a reference on the loaded image that contains the address, so the image stays mapped while its destructors are pending, and Decrement releases one. On Linux the reference is a `dlopen(RTLD_NOLOAD)` handle of the image found through `dladdr1`; on Windows it is `GetModuleHandleExW(FROM_ADDRESS)`. The main executable is counted but needs no handle. An address outside every loaded image and a Decrement without a matching Increment throw. The assumed semantics are recorded in TechnicalDebt. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. --> - Arch Linux, GCC 16.2, Release. `guest_dynamic_loader` now pins the fixture module, closes every handle, and still calls into it; with the pin disabled the test segfaults. It also checks the main executable, the unbalanced Decrement and an address outside every image. Full ctest: 290/290 apart from the three wave64 pixel tests that fail on `main` on this GPU (RX 9070 XT, RADV). - LEGO Horizon Adventures (PPSA14632), together with #672 and local throwing stubs for its missing imports: the title's `__cxa_thread_atexit` now succeeds and it continues to `sceKernelGetModuleInfoFromAddr` (separate PR). - Windows path not built or tested. #717 also edits `core/libs/tests/GuestDynamicLoader.cpp` (a different topic); whichever lands second needs a small rebase there. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
oneandonlydean commented 2026-10-05 23:37:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T23:37:45Z.


This needs a rebase now that #717 has landed: core/libs/tests/GuestDynamicLoader.cpp conflicts with current main, in the includes (main adds <filesystem>/<fstream>, you add <stdexcept>) and at the end of main(), where #717's relinked-module case and your new cases were both appended. Keeping both sides (all three includes, #717's case and then yours) should resolve it. Nothing else conflicts.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6005566325 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/722#issuecomment-6005566325). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T23:37:45Z. --- This needs a rebase now that #717 has landed: `core/libs/tests/GuestDynamicLoader.cpp` conflicts with current `main`, in the includes (`main` adds `<filesystem>`/`<fstream>`, you add `<stdexcept>`) and at the end of `main()`, where #717's relinked-module case and your new cases were both appended. Keeping both sides (all three includes, #717's case and then yours) should resolve it. Nothing else conflicts.
github-actions[bot] commented 2026-10-06 13:10:16 +00:00 (Migrated from github.com)

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


Progress report (5895ea7 → 5e009d7)

📈 System libraries: 87.14% (+0.14%, +4 implemented functions)

✅ 4 implemented
Library Name
libkernel _sceKernelRtldThreadAtexitDecrement_nid_postfix
libkernel _sceKernelRtldThreadAtexitIncrement_nid_postfix
libkernel sceKernelRtldThreadAtexitDecrement
libkernel sceKernelRtldThreadAtexitIncrement
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6016991298 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/722#issuecomment-6016991298). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-06T13:10:16Z. --- <!-- progress-report --> ### Progress report (5895ea7 → 5e009d7) 📈 **System libraries**: 87.14% (+0.14%, +4 implemented functions) <details> <summary>✅ 4 implemented</summary> | Library | Name | | - | - | | libkernel | `_sceKernelRtldThreadAtexitDecrement_nid_postfix` | | libkernel | `_sceKernelRtldThreadAtexitIncrement_nid_postfix` | | libkernel | `sceKernelRtldThreadAtexitDecrement` | | libkernel | `sceKernelRtldThreadAtexitIncrement` | </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!722
No description provided.