fix(libkernel): answer the thread atexit references as measured on a PS5 #10243

Merged
orezraey merged 5 commits from upstream-pr/10243 into main 2026-10-26 03:23:14 +00:00
orezraey commented 2026-10-23 15:17:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by orezraey on 2026-10-23T15:17:59Z.


What

_sceKernelRtldThreadAtexitIncrement and _sceKernelRtldThreadAtexitDecrement measured on a retail PS5 (firmware 13.60) through libkernel_sys.sprx from a payload process, on a watchdog thread. The source and console log are in this gist (prtldat).

Case Console Before
Increment on an address outside every module (null, 0x1000, stack, heap, and the payload's own main, as a payload is not an rtld module) -1 throws
Increment on a module's address 0 0
Decrement outside every module, or after a matching Increment no failure throws
Decrement on a module's address without a matching Increment not measured throws (unchanged)

The console's Decrement returns nothing: rax held the same leftover value (0xe6a75fc1) in every case, so it is a void function. AnyPS5 keeps returning 0. An unbalanced Decrement on a module's address was not measured (every unbalanced Decrement in the log is outside every module), so it still throws, and the debt entry says so. The module's ref_count from sceKernelGetModuleInfoFromAddr stayed 5 through two Increments and two Decrements, so the console counts these apart from module references; AnyPS5 still pins the image, as fpPS4 does. The variants without the leading underscore are not exported by firmware 13.60's libkernel_sys; the debt entry says so.

#9298 edits the same debt line; whichever lands second keeps both.

Tested

Windows 11, MinGW (winlibs), and Linux (WSL Ubuntu, GCC), at head b3b0760369 (main merged after #9298 landed; only the debt entry met, keeping this PR's measured wording and main's new pthread_cleanup_push entry). At this head guest_dynamic_loader passes three runs in a row on both.

  • guest_dynamic_loader checks -1 for a stack and a null address, 0 for Decrements outside every module, and a throw for an unbalanced Decrement on a module's address. It fails on main and passes three runs in a row with the change, as do the 73 other loader, TLS, atexit and module tests.
  • tools/check_conventions.py passes.

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 under Silent stubs in their library's technical debt file
  • 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: #9298 (merge order only: it edits the same lines; this PR is refreshed after it)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4985024760 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10243). Originally posted by [orezraey](https://github.com/orezraey) on 2026-10-23T15:17:59Z. --- ### What `_sceKernelRtldThreadAtexitIncrement` and `_sceKernelRtldThreadAtexitDecrement` measured on a retail PS5 (firmware 13.60) through `libkernel_sys.sprx` from a payload process, on a watchdog thread. The source and console log are in [this gist](https://gist.github.com/orezraey/97a05c49137bbeb19e071a2f88e2a161) (`prtldat`). | Case | Console | Before | |---|---|---| | Increment on an address outside every module (null, 0x1000, stack, heap, and the payload's own `main`, as a payload is not an rtld module) | -1 | throws | | Increment on a module's address | 0 | 0 | | Decrement outside every module, or after a matching Increment | no failure | throws | | Decrement on a module's address without a matching Increment | not measured | throws (unchanged) | The console's Decrement returns nothing: `rax` held the same leftover value (`0xe6a75fc1`) in every case, so it is a `void` function. AnyPS5 keeps returning 0. An unbalanced Decrement on a module's address was not measured (every unbalanced Decrement in the log is outside every module), so it still throws, and the debt entry says so. The module's `ref_count` from `sceKernelGetModuleInfoFromAddr` stayed 5 through two Increments and two Decrements, so the console counts these apart from module references; AnyPS5 still pins the image, as fpPS4 does. The variants without the leading underscore are not exported by firmware 13.60's `libkernel_sys`; the debt entry says so. #9298 edits the same debt line; whichever lands second keeps both. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, MinGW (winlibs), and Linux (WSL Ubuntu, GCC), at head b3b0760369 (`main` merged after #9298 landed; only the debt entry met, keeping this PR's measured wording and `main`'s new `pthread_cleanup_push` entry). At this head `guest_dynamic_loader` passes three runs in a row on both. - `guest_dynamic_loader` checks -1 for a stack and a null address, 0 for Decrements outside every module, and a throw for an unbalanced Decrement on a module's address. It fails on `main` and passes three runs in a row with the change, as do the 73 other loader, TLS, atexit and module tests. - `tools/check_conventions.py` passes. ### 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 under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [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: #9298 (merge order only: it edits the same lines; this PR is refreshed after it) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 05:35:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T05:35:57Z.

Review: COMMENTED


Checked against main 7c2400d29b and the prtldat gist (source and log, fw 13.60):

  • Thanks, the log backs the change. Increment returns 0xffffffff for null, 0x1000, stack and heap, and 0 for libSceLibcInternal. Decrement's rax is always 0xe6a75fc1, so it does look void, ref_count stays 5, and the non-underscore names are unexported.
  • One gap: every unbalanced Decrement in the log is on an address outside every module, and the libc case is balanced (two Increments, two Decrements). So "Decrement without a matching Increment" on a module address, which is the &Require case at GuestDynamicLoader.cpp:232 and the case this PR changes from a throw to a log line, isn't measured yet. Could you add dec(libc_addr) with no Increment to prtldat (and maybe a dec after the two balanced ones)? Otherwise the debt line could say that this case is unmeasured.
  • Small one: the payload's own main also returned -1, presumably because a payload isn't an rtld module. It might be worth adding to the table.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5637027764 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10243#pullrequestreview-5637027764). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T05:35:57Z. **Review:** COMMENTED --- Checked against main 7c2400d29b and the prtldat gist (source and log, fw 13.60): - Thanks, the log backs the change. Increment returns `0xffffffff` for null, 0x1000, stack and heap, and 0 for libSceLibcInternal. Decrement's `rax` is always `0xe6a75fc1`, so it does look void, `ref_count` stays 5, and the non-underscore names are unexported. - One gap: every unbalanced Decrement in the log is on an address outside every module, and the libc case is balanced (two Increments, two Decrements). So "Decrement without a matching Increment" on a module address, which is the `&Require` case at GuestDynamicLoader.cpp:232 and the case this PR changes from a throw to a log line, isn't measured yet. Could you add `dec(libc_addr)` with no Increment to prtldat (and maybe a dec after the two balanced ones)? Otherwise the debt line could say that this case is unmeasured. - Small one: the payload's own `main` also returned -1, presumably because a payload isn't an rtld module. It might be worth adding to the table. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!10243
No description provided.