fix(libkernel): copy scePthreadOnce control state through checked guest access #25283

Merged
jvexxxx merged 2 commits from upstream-pr/25283 into main 2026-11-28 21:14:43 +00:00
jvexxxx commented 2026-11-21 21:03:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jvexxxx on 2026-11-21T21:03:25Z.


What

Read and write the guest control word of scePthreadOnce (and its cancellation/exception cleanup helper ResetOnce) through GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch while preserving errno, matching the checked-copy pattern already used by pthread_once. Return SCE_KERNEL_ERROR_EFAULT without running the initializer if the control pointer is unreadable or unwritable, and add fault-handling test cases to GuestKernelPthreadOnce.cpp.

Tested

Windows (MinGW-w64 WinLibs GCC 15.2.0, Ninja, Release):

  • Built libkernel.prx, guest_kernel_pthread_once_tests.exe, guest_once_tests.exe, guest_kernel_pthread_sync_tests.exe, guest_execute_once_tests.exe, and guest_std_execute_once_tests.exe.
  • Passed ctest for guest_execute_once and guest_std_execute_once.
  • Passed python tools/check_conventions.py --base origin/main, python core/libs/tests/test_public_headers.py --audit-only, and verified scePthreadOnce is no longer reported by python tools/guest_pointer_audit.py libkernel.
  • Runtime execution of guest_kernel_pthread_once, guest_once, and guest_kernel_pthread_sync was not run locally because the host CPU (Intel Core i5-3330) lacks AVX2/BMI1/BMI2/FMA/LZCNT/MOVBE required by libkernel's HostCpu startup 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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5432387097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25283). Originally posted by [jvexxxx](https://github.com/jvexxxx) on 2026-11-21T21:03:25Z. --- ### What Read and write the guest control word of `scePthreadOnce` (and its cancellation/exception cleanup helper `ResetOnce`) through `GuestCopyIn_nid_no_patch` and `GuestCopyOut_nid_no_patch` while preserving `errno`, matching the checked-copy pattern already used by `pthread_once`. Return `SCE_KERNEL_ERROR_EFAULT` without running the initializer if the control pointer is unreadable or unwritable, and add fault-handling test cases to `GuestKernelPthreadOnce.cpp`. ### Tested Windows (MinGW-w64 WinLibs GCC 15.2.0, Ninja, Release): - Built `libkernel.prx`, `guest_kernel_pthread_once_tests.exe`, `guest_once_tests.exe`, `guest_kernel_pthread_sync_tests.exe`, `guest_execute_once_tests.exe`, and `guest_std_execute_once_tests.exe`. - Passed `ctest` for `guest_execute_once` and `guest_std_execute_once`. - Passed `python tools/check_conventions.py --base origin/main`, `python core/libs/tests/test_public_headers.py --audit-only`, and verified `scePthreadOnce` is no longer reported by `python tools/guest_pointer_audit.py libkernel`. - Runtime execution of `guest_kernel_pthread_once`, `guest_once`, and `guest_kernel_pthread_sync` was not run locally because the host CPU (Intel Core i5-3330) lacks AVX2/BMI1/BMI2/FMA/LZCNT/MOVBE required by `libkernel`'s `HostCpu` startup 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) and the licence notice of code adapted from upstream (see AGENTS.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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
PrivacyOnion commented 2026-11-21 21:55:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-21T21:55:42Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against f0d5d82ac4:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_pthread_once_tests built cleanly.
  • CTest (guest_kernel_pthread_once):
    1/1 Test #5813: guest_kernel_pthread_once ........   Passed    0.05 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libkernel/Pthread/src/Thread.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #5813: guest_kernel_pthread_once ........***Exception: SegFault  0.44 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch preserve errno properly across calls via savedErrno.
    • Verified sceOnceMutex protects reading, updating, and condition variable waits without deadlocks across concurrent threads.
    • Verified ResetOnce pthread cleanup handler correctly acquires sceOnceMutex, clears state to Never (0), and notifies waiters on sceOnceChanged.
    • Verified error handling returns SCE_KERNEL_ERROR_EFAULT (0x8002000e) when guest memory is unreadable, unwritable, or faults during initialization.
    • Verified AGENTS.md rules, technical debt fragment in docs/dev/debt/changes/fix-libkernel-sce-pthread-once-guest-control.md, and test coverage in GuestKernelPthreadOnce.cpp.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6008452901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25283#pullrequestreview-6008452901). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T21:55:42Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `f0d5d82ac4`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_pthread_once_tests` built cleanly. - **CTest** (`guest_kernel_pthread_once`): ``` 1/1 Test #5813: guest_kernel_pthread_once ........ Passed 0.05 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libkernel/Pthread/src/Thread.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #5813: guest_kernel_pthread_once ........***Exception: SegFault 0.44 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified GuestCopyIn_nid_no_patch and GuestCopyOut_nid_no_patch preserve errno properly across calls via savedErrno. - Verified sceOnceMutex protects reading, updating, and condition variable waits without deadlocks across concurrent threads. - Verified ResetOnce pthread cleanup handler correctly acquires sceOnceMutex, clears state to Never (0), and notifies waiters on sceOnceChanged. - Verified error handling returns SCE_KERNEL_ERROR_EFAULT (0x8002000e) when guest memory is unreadable, unwritable, or faults during initialization. - Verified AGENTS.md rules, technical debt fragment in docs/dev/debt/changes/fix-libkernel-sce-pthread-once-guest-control.md, and test coverage in GuestKernelPthreadOnce.cpp. Looks Good To Me
MarcZX10 commented 2026-11-22 18:05:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T18:05:04Z.

Review: DISMISSED


Reviewed — changes requested

Changes are required before merge: ensure a failed final state write cannot leave the control permanently marked Running and block future callers.

What I ran

  • Reviewed head f0d5d82ac4134b9ea6819bb6bf09c7eed6b74e9b using repo_read.py.
  • No commands or tests were run; author-reported checks are not treated as checks performed here.

Results

Check Result Note
Source review failed Failed final write leaves state Running
Tests not run No runtime verification performed

Findings

Blocking

  • core/libs/prx/libkernel/Pthread/src/Thread.cpp:1792: If the initializer makes the control page read-only, the final write of Done fails and the function returns SCE_KERNEL_ERROR_EFAULT while the control word remains Running. The test added for this case verifies the word remains 2 (core/libs/tests/GuestKernelPthreadOnce.cpp:362-365). A later call sees Running and waits indefinitely, because no cleanup/reset is performed after this failed write. Ensure failure wakes waiters and does not leave the control stuck in a state that makes later calls wait forever.

Non-blocking

None.

Not verified

  • Read all changed files listed in manifest.json: Thread.cpp, GuestKernelPthreadOnce.cpp, and the debt change note. No changed files were excluded (files.json was not available in the reader listing).
  • Did not run builds, tests, convention checks, or console execution. The description reports author-run Windows checks and notes that several runtime tests were not run locally.
  • Did not inspect behavior on a physical console.

To get this to a good state

  • Fix the failed final-write path so later callers cannot wait indefinitely on a stale Running state; add or update coverage for a subsequent call after the write failure.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013656862 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25283#pullrequestreview-6013656862). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T18:05:04Z. **Review:** DISMISSED --- # Reviewed — changes requested Changes are required before merge: ensure a failed final state write cannot leave the control permanently marked Running and block future callers. ## What I ran - Reviewed head `f0d5d82ac4134b9ea6819bb6bf09c7eed6b74e9b` using `repo_read.py`. - No commands or tests were run; author-reported checks are not treated as checks performed here. ## Results | Check | Result | Note | |---|---|---| | Source review | failed | Failed final write leaves state Running | | Tests | not run | No runtime verification performed | ## Findings ### Blocking - `core/libs/prx/libkernel/Pthread/src/Thread.cpp:1792`: If the initializer makes the control page read-only, the final write of `Done` fails and the function returns `SCE_KERNEL_ERROR_EFAULT` while the control word remains `Running`. The test added for this case verifies the word remains `2` (`core/libs/tests/GuestKernelPthreadOnce.cpp:362-365`). A later call sees `Running` and waits indefinitely, because no cleanup/reset is performed after this failed write. Ensure failure wakes waiters and does not leave the control stuck in a state that makes later calls wait forever. ### Non-blocking None. ## Not verified - Read all changed files listed in `manifest.json`: `Thread.cpp`, `GuestKernelPthreadOnce.cpp`, and the debt change note. No changed files were excluded (`files.json` was not available in the reader listing). - Did not run builds, tests, convention checks, or console execution. The description reports author-run Windows checks and notes that several runtime tests were not run locally. - Did not inspect behavior on a physical console. ## To get this to a good state - Fix the failed final-write path so later callers cannot wait indefinitely on a stale `Running` state; add or update coverage for a subsequent call after the write failure.
MarcZX10 commented 2026-11-22 18:31:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-22T18:31:16Z.

Review: COMMENTED


Reviewed — comments

The change handles the reported guest memory faults, but scePthreadOnce can deadlock when an initializer recursively calls it with the same control pointer.

What I ran

  • Reviewed head 842c086d92d337b51e4126c8d3fd6467df17439d; base is main.
  • Read description.md, commits.md, manifest.json, diff.patch, and all three changed files at the pinned head using repo_read.py.
  • No commands or tests were run. The author reports builds and checks in the PR description; those were not run by me.

Results

Check Result Note
Diff and changed files passed All three changed files read
Build and tests not run Only author-reported results available
Runtime on console not run Not confirmed

Findings

Blocking

None.

Non-blocking

  • Recursive calls with the same once pointer deadlock. The thread that owns the active initializer blocks in the condition-variable wait at core/libs/prx/libkernel/Pthread/src/Thread.cpp:1773-1775, waiting for its own initializer to erase the pointer. Recursive scePthreadOnce calls were previously exercised by the test suite for the POSIX implementation, but the new SCE concurrency test does not cover this case. Consider tracking the owner thread or otherwise detecting same-thread recursion.

Not verified

  • Build, tests, convention checks, public-header audit, and guest-pointer audit were not run; results in the description are author-reported.
  • Console behavior was not tested.
  • All files listed in the manifest were read. files.json was not available through the reader; no excluded files were reported in the manifest.

To get this to a good state

Nothing is required. Consider adding a same-pointer recursive-call test and handling that case without deadlocking.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6013757318 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/25283#pullrequestreview-6013757318). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-22T18:31:16Z. **Review:** COMMENTED --- # Reviewed — comments The change handles the reported guest memory faults, but `scePthreadOnce` can deadlock when an initializer recursively calls it with the same control pointer. ## What I ran - Reviewed head `842c086d92d337b51e4126c8d3fd6467df17439d`; base is `main`. - Read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, and all three changed files at the pinned head using `repo_read.py`. - No commands or tests were run. The author reports builds and checks in the PR description; those were not run by me. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | All three changed files read | | Build and tests | not run | Only author-reported results available | | Runtime on console | not run | Not confirmed | ## Findings ### Blocking None. ### Non-blocking - Recursive calls with the same `once` pointer deadlock. The thread that owns the active initializer blocks in the condition-variable wait at `core/libs/prx/libkernel/Pthread/src/Thread.cpp:1773-1775`, waiting for its own initializer to erase the pointer. Recursive `scePthreadOnce` calls were previously exercised by the test suite for the POSIX implementation, but the new SCE concurrency test does not cover this case. Consider tracking the owner thread or otherwise detecting same-thread recursion. ## Not verified - Build, tests, convention checks, public-header audit, and guest-pointer audit were not run; results in the description are author-reported. - Console behavior was not tested. - All files listed in the manifest were read. `files.json` was not available through the reader; no excluded files were reported in the manifest. ## To get this to a good state Nothing is required. Consider adding a same-pointer recursive-call test and handling that case without deadlocking.
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!25283
No description provided.