libkernel: pthread keys start at 0, but FreeBSD libthr never returns key 0; fix branch ready #3118

Closed
opened 2026-10-10 22:35:15 +00:00 by milescoviello · 2 comments
milescoviello commented 2026-10-10 22:35:15 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-10T22:35:15Z.


scePthreadKeyCreate/pthread_key_create return the slot index, so the first key in a process is 0. FreeBSD libthr returns i + 1 (thr_spec.c, _pthread_key_create) and subtracts 1 in delete/get/set, so 0 is never valid. Code that uses 0 to mean "key not created yet" (if (!key) pthread_key_create(&key, …)) creates a new key on every call on AnyPS5 and loses what it stored: a probe gives first key 0, key after second call 1, value under first key lost.

Fix on my fork: milescoviello/AnyPS5:fix/pthread-key-numbering (one commit, 7acad0ca, on main 4d27834f; the 12 commits on main since then don't touch these files): keys start at 1, and 0 and out-of-range keys are rejected as before. guest_pthread_key gains checks that fail on main and pass with the fix. Linux only; Windows not tested (the change has no platform-specific code).

Setup: Gentoo, Linux 7.2.8, GCC 15.3.0, Release with CI's configure flags, main 4d27834f.

Tested: the new checks fail on main and pass with the fix. Full ctest with the fix on Linux (RTX 5090 Laptop GPU): no new failures; the only failures are the known agc_driver_dcc_keys_first_write (#2767) and two guest_video_out_* tests that time out with the displays powered off. tools/check_conventions.py passes. All five fixes I am filing today also pass their tests merged together on the current main.

Not opened as a PR because intake is paused for new contributors; a maintainer is welcome to carry the commit.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5795893157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3118). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-10T22:35:15Z. --- `scePthreadKeyCreate`/`pthread_key_create` return the slot index, so the first key in a process is 0. FreeBSD libthr returns `i + 1` (`thr_spec.c`, `_pthread_key_create`) and subtracts 1 in delete/get/set, so 0 is never valid. Code that uses 0 to mean "key not created yet" (`if (!key) pthread_key_create(&key, …)`) creates a new key on every call on AnyPS5 and loses what it stored: a probe gives `first key 0, key after second call 1, value under first key lost`. Fix on my fork: [`milescoviello/AnyPS5:fix/pthread-key-numbering`](https://github.com/milescoviello/AnyPS5/tree/fix/pthread-key-numbering) (one commit, `7acad0ca`, on `main` 4d27834f; the 12 commits on `main` since then don't touch these files): keys start at 1, and 0 and out-of-range keys are rejected as before. `guest_pthread_key` gains checks that fail on main and pass with the fix. Linux only; Windows not tested (the change has no platform-specific code). **Setup:** Gentoo, Linux 7.2.8, GCC 15.3.0, Release with CI's configure flags, `main` 4d27834f. **Tested:** the new checks fail on `main` and pass with the fix. Full `ctest` with the fix on Linux (RTX 5090 Laptop GPU): no new failures; the only failures are the known `agc_driver_dcc_keys_first_write` (#2767) and two `guest_video_out_*` tests that time out with the displays powered off. `tools/check_conventions.py` passes. All five fixes I am filing today also pass their tests merged together on the current `main`. Not opened as a PR because intake is paused for new contributors; a maintainer is welcome to carry the commit. AI-assisted: yes
hikazey commented 2026-10-11 00:53:13 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by hikazey on 2026-10-11T00:53:13Z.


Windows check, since the issue says Windows wasn't tested. This branch merged onto main f1ae427b, Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with CI's configure flags.

build guest_pthread_key
this branch 5/5 passed
Tsd.cpp reverted to main, test kept 5/5 failed
full suite with this branch 756/758 passed

The two full-suite failures also fail on plain main here (pr_overlap_encoding: the two *_error_preserves_unicode cases; guest_filesystem: line 276, guest errno 14), so they are unrelated.

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6103942427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3118#issuecomment-6103942427). Originally posted by [hikazey](https://github.com/hikazey) on 2026-10-11T00:53:13Z. --- Windows check, since the issue says Windows wasn't tested. This branch merged onto `main` `f1ae427b`, Windows 11 Pro (26200), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release with CI's configure flags. | build | `guest_pthread_key` | | --- | --- | | this branch | 5/5 passed | | `Tsd.cpp` reverted to `main`, test kept | 5/5 failed | | full suite with this branch | 756/758 passed | The two full-suite failures also fail on plain `main` here (`pr_overlap_encoding`: the two `*_error_preserves_unicode` cases; `guest_filesystem`: line 276, guest errno 14), so they are unrelated. AI-assisted: yes (Claude Code).
milescoviello commented 2026-10-11 01:01:25 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by milescoviello on 2026-10-11T01:01:25Z.


Thanks @hikazey for running these on Windows (here, #3119 and #3120), including the reverted-source checks. That covers the platform I could not test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6104002338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/3118#issuecomment-6104002338). Originally posted by [milescoviello](https://github.com/milescoviello) on 2026-10-11T01:01:25Z. --- Thanks @hikazey for running these on Windows (here, #3119 and #3120), including the reverted-source checks. That covers the platform I could not test.
Sign in to join this conversation.
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#3118
No description provided.