fix(libkernel): start the pthread object with the thread id #2184

Merged
EdouardSence merged 2 commits from upstream-pr/2184 into main 2026-10-10 08:27:45 +00:00
EdouardSence commented 2026-10-09 11:05:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-09T11:05:07Z.


What

Ratchet & Clank: Rift Apart (PPSA01474) reads its thread id straight from the pthread object. It calls pthread_self() and loads the first 32 bits it points to. That matches FreeBSD's libthr, which PS5 libkernel derives from: struct pthread starts with long tid.

The title then uses that id to find a per-thread allocator in its own table:

  • a worker thread registers itself under its id;
  • the main thread looks up its own id later.

On Linux, PthreadPrivate currently starts with std::thread _thr:

  • created threads: the title reads the low 32 bits of the glibc pthread_t;
  • adopted main thread: _thr is empty, so the title reads 0. It matches an empty table slot, gets a null allocator and crashes with SIGSEGV about 19 s in.

Changes:

  • PthreadPrivate now starts with long tid, taken from a process-wide counter that starts at 100000, the first FreeBSD thread id. Every thread object gets one, including adopted threads.
  • scePthreadGetthreadid returns the calling thread's tid, as FreeBSD's pthread_getthreadid_np returns TID(curthread). Before, it returned a hash of std::thread::id on Linux and GetCurrentThreadId() on Windows, so the two ids could disagree.

Tested

Linux (CachyOS, GCC), RX 9070 XT.

  • guest_thread_self now checks three things:
    • the first long of the main thread's and a created thread's pthread objects is non-zero, and differs between the two threads;
    • on each thread, it equals scePthreadGetthreadid();
    • without the change, the test aborts.
  • Full ctest: 511/512. The failing test is import_audit, which can't read the commit hash in my worktree and is unrelated.
  • tools/check_conventions.py passes.
  • Ratchet & Clank: Rift Apart, on main 43972e69 + #1673, #1339, #1836, #1837, #1893, #2105, plus a local placeholder for munlock:
    • Before this change, the main thread crashes about 19 s in, at cmpq $0x0,(%rbx) with rbx = 0, inside the title's hashmap32<T> construction. The allocator came from that thread-id lookup.
    • After it, the title gets past that point, registers its shaders and reaches its first draws. It then stops on an unrelated PA_SC_CONSERVATIVE_RASTERIZATION_CNTL=0xe6000 check.

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. The investigation, code, tests and this description were written by Claude (Claude Code) on my machine.

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4799557195 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2184). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-09T11:05:07Z. --- ### What Ratchet & Clank: Rift Apart (PPSA01474) reads its thread id straight from the pthread object. It calls `pthread_self()` and loads the first 32 bits it points to. That matches FreeBSD's libthr, which PS5 libkernel derives from: `struct pthread` starts with `long tid`. The title then uses that id to find a per-thread allocator in its own table: - a worker thread registers itself under its id; - the main thread looks up its own id later. On Linux, `PthreadPrivate` currently starts with `std::thread _thr`: - **created threads:** the title reads the low 32 bits of the glibc `pthread_t`; - **adopted main thread:** `_thr` is empty, so the title reads 0. It matches an empty table slot, gets a null allocator and crashes with SIGSEGV about 19 s in. Changes: - `PthreadPrivate` now starts with `long tid`, taken from a process-wide counter that starts at 100000, the first FreeBSD thread id. Every thread object gets one, including adopted threads. - `scePthreadGetthreadid` returns the calling thread's `tid`, as FreeBSD's `pthread_getthreadid_np` returns `TID(curthread)`. Before, it returned a hash of `std::thread::id` on Linux and `GetCurrentThreadId()` on Windows, so the two ids could disagree. ### Tested Linux (CachyOS, GCC), RX 9070 XT. - `guest_thread_self` now checks three things: - the first `long` of the main thread's and a created thread's pthread objects is non-zero, and differs between the two threads; - on each thread, it equals `scePthreadGetthreadid()`; - without the change, the test aborts. - Full `ctest`: 511/512. The failing test is `import_audit`, which can't read the commit hash in my worktree and is unrelated. - `tools/check_conventions.py` passes. - Ratchet & Clank: Rift Apart, on `main` 43972e69 + #1673, #1339, #1836, #1837, #1893, #2105, plus a local placeholder for `munlock`: - Before this change, the main thread crashes about 19 s in, at `cmpq $0x0,(%rbx)` with `rbx = 0`, inside the title's `hashmap32<T>` construction. The allocator came from that thread-id lookup. - After it, the title gets past that point, registers its shaders and reaches its first draws. It then stops on an unrelated `PA_SC_CONSERVATIVE_RASTERIZATION_CNTL=0xe6000` check. ### Checklist - [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. The investigation, code, tests and this description were written by Claude (Claude Code) on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
rmz-24 commented 2026-10-09 12:26:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T12:26:03Z.


Windows run: Windows 11 Pro 25H2 (10.0.26200.9457), RTX 5060 Ti (NVIDIA 617.42), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON, full build plus libs. 3a4bc11d cherry-picked onto current main d70b8998 (applies cleanly).

  • guest_thread_self passes.
  • Full ctest: 510/510.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080845611 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2184#issuecomment-6080845611). Originally posted by [rmz-24](https://github.com/rmz-24) on 2026-10-09T12:26:03Z. --- Windows run: Windows 11 Pro 25H2 (10.0.26200.9457), RTX 5060 Ti (NVIDIA 617.42), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`, full build plus `libs`. 3a4bc11d cherry-picked onto current `main` d70b8998 (applies cleanly). - `guest_thread_self` passes. - Full ctest: 510/510. AI-assisted: yes
oneandonlydean commented 2026-10-09 20:15:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:15:13Z.


long tid is 32 bits on Windows, so there the first 8 bytes of the pthread object are the id plus 4 bytes of padding, while FreeBSD's long tid is 64 bits. Please make it std::int64_t (Pthread.hpp:81, and the counter on line 78), and read std::int64_t in ThreadIdField in the test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088517505 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2184#issuecomment-6088517505). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:15:13Z. --- `long tid` is 32 bits on Windows, so there the first 8 bytes of the pthread object are the id plus 4 bytes of padding, while FreeBSD's `long tid` is 64 bits. Please make it `std::int64_t` (`Pthread.hpp:81`, and the counter on line 78), and read `std::int64_t` in `ThreadIdField` in the test.
EdouardSence commented 2026-10-10 08:00:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EdouardSence on 2026-10-10T08:00:13Z.


Done in 94409c9f: nextThreadId and Pthread::tid are std::int64_t now, and ThreadIdField (and the two values compared in the test) read std::int64_t. The first 8 bytes of the pthread object are the id on every host, as on FreeBSD.

Rebased onto current main (a29bae31). Linux, GCC, Release: full ctest passes 641 of 642, guest_thread_self included. The one failure is import_audit (source_commit is None because my build tree is a git worktree), unrelated. tools/check_conventions.py passes.

Written by Claude (AI assistant) on a contributor's machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095415312 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2184#issuecomment-6095415312). Originally posted by [EdouardSence](https://github.com/EdouardSence) on 2026-10-10T08:00:13Z. --- Done in 94409c9f: `nextThreadId` and `Pthread::tid` are `std::int64_t` now, and `ThreadIdField` (and the two values compared in the test) read `std::int64_t`. The first 8 bytes of the pthread object are the id on every host, as on FreeBSD. Rebased onto current `main` (a29bae31). Linux, GCC, Release: full ctest passes 641 of 642, `guest_thread_self` included. The one failure is `import_audit` (`source_commit` is `None` because my build tree is a git worktree), unrelated. `tools/check_conventions.py` passes. *Written by Claude (AI assistant) on a contributor's machine.*
adriadam10 commented 2026-10-10 08:27:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T08:27:41Z.


Checked on Linux (GCC, Release): guest_thread_self, guest_sched_priority and guest_pthread_rename pass on the PR merged onto main; guest_thread_self fails without the change. The std::int64_t tid Dean asked for is in 94409c9f, so the first 8 bytes of the pthread object are the id on every host. Windows run by @rmz-24 was on the previous commit. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6095674346 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2184#issuecomment-6095674346). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-10T08:27:41Z. --- Checked on Linux (GCC, Release): `guest_thread_self`, `guest_sched_priority` and `guest_pthread_rename` pass on the PR merged onto `main`; `guest_thread_self` fails without the change. The `std::int64_t` tid Dean asked for is in 94409c9f, so the first 8 bytes of the pthread object are the id on every host. Windows run by @rmz-24 was on the previous commit. Merging.
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!2184
No description provided.