fix(libkernel): keep the pthread_mutexattr_setkind_np kind as firmware 13.42 does #10725

Merged
dwk001 merged 3 commits from upstream-pr/10725 into main 2026-10-28 09:46:49 +00:00
dwk001 commented 2026-10-24 19:50:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by dwk001 on 2026-10-24T19:50:54Z.


What

pthread_mutexattr_setkind_np threw for a kind outside 1 to 4. It now behaves as the firmware 13.42 libkernel.sprx measured in this thread: it keeps the low 16 bits of the kind as a signed value (0x8000 reads back as -32768, 0x12345 as 0x2345, 0x10001 as 1); pthread_mutexattr_gettype returns EINVAL only for a kind above 4, so 0 and negative kinds are returned with success; and pthread_mutex_init and scePthreadMutexInit reject a kind outside 1 to 4 with EINVAL without touching the mutex. This differs from FreeBSD 11.4, which stores the full int. The measurement was taken in an emulator, not on a console; noted in docs/dev/debt/libkernel.md.

Tested

Linux, full build with -DBUILD_TESTING=ON: ctest --test-dir build -R guest_pthread_mutex passes (guest_pthread_mutexattr, guest_pthread_mutex_errors), covering each case above. Windows (GCC 15.2.0) reported passing by wachi35676. tools/check_conventions.py --base origin/main passes.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4998602751 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10725). Originally posted by [dwk001](https://github.com/dwk001) on 2026-10-24T19:50:54Z. --- ### What `pthread_mutexattr_setkind_np` threw for a kind outside 1 to 4. It now behaves as the firmware 13.42 `libkernel.sprx` measured in this thread: it keeps the low 16 bits of the kind as a signed value (0x8000 reads back as -32768, 0x12345 as 0x2345, 0x10001 as 1); `pthread_mutexattr_gettype` returns `EINVAL` only for a kind above 4, so 0 and negative kinds are returned with success; and `pthread_mutex_init` and `scePthreadMutexInit` reject a kind outside 1 to 4 with `EINVAL` without touching the mutex. This differs from FreeBSD 11.4, which stores the full `int`. The measurement was taken in an emulator, not on a console; noted in `docs/dev/debt/libkernel.md`. ### Tested Linux, full build with `-DBUILD_TESTING=ON`: `ctest --test-dir build -R guest_pthread_mutex` passes (`guest_pthread_mutexattr`, `guest_pthread_mutex_errors`), covering each case above. Windows (GCC 15.2.0) reported passing by wachi35676. `tools/check_conventions.py --base origin/main` passes.
MarcZX10 commented 2026-10-24 20:03:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:03:14Z.

Review: APPROVED


Reviewed the invalid-kind paths and tests. The setter now stores arbitrary kinds; the getter rejects unsupported stored values without writing the output, and initialization rejects them before replacing the mutex. This matches the pinned FreeBSD 11.4 behavior cited in the PR. The author reports the focused Linux mutex tests pass, and hosted conventions/intake checks pass. The full hosted build was not available to this account (workflow dispatch returned HTTP 403). I found no blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639749212 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10725#pullrequestreview-5639749212). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:03:14Z. **Review:** APPROVED --- Reviewed the invalid-kind paths and tests. The setter now stores arbitrary kinds; the getter rejects unsupported stored values without writing the output, and initialization rejects them before replacing the mutex. This matches the pinned FreeBSD 11.4 behavior cited in the PR. The author reports the focused Linux mutex tests pass, and hosted conventions/intake checks pass. The full hosted build was not available to this account (workflow dispatch returned HTTP 403). I found no blocking issue.
MarcZX10 commented 2026-10-24 20:20:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:20:15Z.

Review: COMMENTED


I found an interaction the current tests do not cover: pthread_mutexattr_gettype_nid_postfix now rejects a stored kind outside 1–4 using static_cast<std::uint32_t>((*attr)->type) > 4. That correctly rejects the tested 5 and -1, but it treats stored kind 0 as valid and returns success. The issue is observable because CheckKind explicitly sets kind 0 and currently expects gettype to return 0, while the stated FreeBSD behavior is that only recognized mutex types are valid at gettype/init time. Could you confirm the console/FreeBSD contract for kind 0 and, if it is invalid, reject values below MutexType::ErrorCheck too and assert that pthread_mutexattr_gettype returns EINVAL for 0? The code and tests otherwise cover storage and init rejection for the out-of-range values well.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639802687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10725#pullrequestreview-5639802687). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:20:15Z. **Review:** COMMENTED --- I found an interaction the current tests do not cover: `pthread_mutexattr_gettype_nid_postfix` now rejects a stored kind outside 1–4 using `static_cast<std::uint32_t>((*attr)->type) > 4`. That correctly rejects the tested 5 and -1, but it treats stored kind 0 as valid and returns success. The issue is observable because `CheckKind` explicitly sets kind 0 and currently expects `gettype` to return 0, while the stated FreeBSD behavior is that only recognized mutex types are valid at gettype/init time. Could you confirm the console/FreeBSD contract for kind 0 and, if it is invalid, reject values below `MutexType::ErrorCheck` too and assert that `pthread_mutexattr_gettype` returns EINVAL for 0? The code and tests otherwise cover storage and init rejection for the out-of-range values well.
MarcZX10 commented 2026-10-24 20:30:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T20:30:52Z.

Review: APPROVED


Re-reviewed exact head 0eb3a8c6ca14800c63e6581f8e02a145c0575606 after the upstream merge. The author’s FreeBSD 11.4 distinction is reflected in both behavior and tests: gettype returns stored kind 0 but rejects 5 and negative values; mutex initialization rejects every kind outside 1–4 without installing a mutex. I compiled the two changed mutex translation units and GuestPthreadMutexattr.cpp with GCC 12.2, and ran git diff --check plus tools/check_conventions.py --base upstream/main --head HEAD; all passed. The focused Linux CTest result is author-reported; this isolated checkout has no configured build tree, and hosted product tests are unavailable/skipped. I found no remaining blocking issue.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5639844554 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10725#pullrequestreview-5639844554). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T20:30:52Z. **Review:** APPROVED --- Re-reviewed exact head `0eb3a8c6ca14800c63e6581f8e02a145c0575606` after the upstream merge. The author’s FreeBSD 11.4 distinction is reflected in both behavior and tests: `gettype` returns stored kind 0 but rejects 5 and negative values; mutex initialization rejects every kind outside 1–4 without installing a mutex. I compiled the two changed mutex translation units and `GuestPthreadMutexattr.cpp` with GCC 12.2, and ran `git diff --check` plus `tools/check_conventions.py --base upstream/main --head HEAD`; all passed. The focused Linux CTest result is author-reported; this isolated checkout has no configured build tree, and hosted product tests are unavailable/skipped. I found no remaining blocking issue.
MarcZX10 commented 2026-10-25 15:00:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T15:00:46Z.

Review: APPROVED


Re-reviewed exact current head e632137d7cd837ff203719fb7581669a5c405ed4 after the emulator measurements were added. The new tests now match the reported firmware 13.42 behavior: the setter stores a signed 16-bit kind; gettype accepts negative values and rejects values above 4; initialization still rejects kinds outside 1–4 without installing the mutex. The changed translation units and test compile with GCC 12.2; exact-head git diff --check upstream/main...HEAD and conventions pass. Focused runtime CTest remains author-reported. The PR description correctly says not measured on a retail PS5.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5643773890 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10725#pullrequestreview-5643773890). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-25T15:00:46Z. **Review:** APPROVED --- Re-reviewed exact current head `e632137d7cd837ff203719fb7581669a5c405ed4` after the emulator measurements were added. The new tests now match the reported firmware 13.42 behavior: the setter stores a signed 16-bit kind; `gettype` accepts negative values and rejects values above 4; initialization still rejects kinds outside 1–4 without installing the mutex. The changed translation units and test compile with GCC 12.2; exact-head `git diff --check upstream/main...HEAD` and conventions pass. Focused runtime CTest remains author-reported. The PR description correctly says not measured on a retail PS5.
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!10725
No description provided.