fix(libkernel): allocate thread objects in guest memory #21969
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21969"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by B-Howell on 2026-11-16T23:38:57Z.
What
scePthreadSelf,scePthreadCreateandpthread_selfgive the title the address of itsPthreadPrivate. That object came from the host heap. On Windows the guest arena reserves everything below0xfc00000000at start-up, so later host allocations land above it, and some land above 2^40. Titles keep that address in 40-bit fields.Bloodborne stalled silently in 5 of 6 runs, right after its first compute dispatch. A
APS5_HANG_DUMP_SECS=25dump of a stalled run shows:bloodborne.exe+0x21c8424(cmp 0x18(%r13),%eax; jgback). It waits for a counter that theGXWorkerthreads raise.GXWorker:1sits insceKernelWaitSema(libkernel.prx+0x5e066), called frombloodborne.exe+0x2046988. That caller is the title's own adaptive mutex: its error string isDLAdaptiveMutex::Lock logical error in dantelion2., fromCSPlainAdaptiveMutexImp_sce.cpp.GXWorker:2toGXWorker:5wait inscePthreadMutexLock.The mutex stores
scePthreadSelf()masked to 40 bits (movabs $0xffffffffff) in its lock word. After thecmpxchgsucceeds, it compares the masked owner with the fullscePthreadSelf(). If they differ, it treats the lock as contended and waits on the semaphore. Its try-lock path does the same: it takes the lock and then reports failure. So a thread whose object lies above 2^40 waits forever on a lock it owns.In the stalled runs the
GXWorkerobjects were at0x0000013cdaacbc10; in the run that got further they were at0x000000fc094b74a0, below 2^40. With this change they are at0x0000000200221de0.PthreadPrivatenow has classoperator newandoperator deletebacked byGuestHeap. That is libc's allocator for host-made objects the guest sees, already used by AvPlayer, Net and Ampr. EveryPthreadPrivatecomes fromstd::make_uniqueordelete: created threads, threads adopted byscePthreadSelfon Windows and on POSIX hosts, and the final release. So all of them now lie in registered guest memory. Freeing goes throughGuestHeapFree, under the same unpinned-range rule as every other guest heap block, including the title's ownfree.An independent review asked whether
deleteof aPthreadPrivatecan end the process, becauseGuestHeapFree_nid_postfixthrows once a lease pins the block pastAPS5_PIN_WAIT_MSandoperator deleteisnoexcept. It does not add a new risk. Leases fromGuestAllocationsAcquire_nid_postfix/AcquireAllsnapshot every readable registered range, not ones the GPU uses (GuestAllocations.cppL794-803, L865-874), so every guest heap block is pinned the same way, and the title's ownfree(ApplicationHeap.cppL41defaultFree) hits the same throw with guest frames above it that cannot catch it. The block is allocated withgpu = falseand is never handed to the GPU; the wait defaults to 60 s while the pin waiter retires GPU work, so a throw there means the GPU is already stuck.The debt fragment notes that the console's placement of these objects is not measured.
The test
GuestThreadSelf.cpprequires the main thread object, a host thread adopted throughscePthreadSelf(Windows), and a created thread to be registered guest allocations below 2^40.Tested
Windows 11 Pro 10.0.26300, WinLibs GCC 15.2.0 r7, Release.
guest_thread_self: onmainthe new check aborts (exit0xc0000409). With this change it passes.Related tests: 104 pthread, thread, kernel-sys thread and guest heap ctest entries pass with this change. This includes
guest_pthread_mutex_contention,guest_pthread_final_release_race,guest_pthread_destructor_lifetime,guest_pthread_cancel*andguest_heap_*.Full ctest suite on GitHub-hosted Windows (ycbuzi, report, head
9ceb8ecf07merged into main, WinLibs GCC 15.2.0, run): Windows Server 2025 passes all 6133 tests in both runs. Windows Server 2022 has 2 and 3 failures out of 6133; all of them also fail on main on the same runner.Title: Bloodborne CUSA00900 01.09 (my own copy), on
mainplus the open fixes it needs (#17105, #17711, #20156, #21944; #21832 has since merged)./data/cache.bin, load the shader bundles and reach the next stop at about 1.7 s:compute shader 0x2d1f00900: buffer specialization has an unsupported format. That stop is unrelated to this change.guest_raise_released_thread(Linux only): oneandonlydean found that it aborts with this PR on head9ceb8ecf07. The test noticed a thread object's release by replacing the globaloperator delete;PthreadPrivatenow frees through its ownoperator deleteintoGuestHeapFree, so the test never saw the release. Headb13fbdf052changes only the test: it interposesGuestHeapFree_nid_postfix, keeps the watched object zero-filled and unreused, and passes every other block on. WSL Ubuntu, GCC, Release, Ryzen 7 7800X3D: the old test aborts on this PR (Subprocess aborted, as reported); the new one passes 5 of 5 runs, andguest_thread_selfpasses 5 of 5.check_conventions.pypasses.python3 tools/check_conventions.py --base origin/mainandtools/debt_fold.py --checkpass.tools/pr_overlap.py --branchcould not compare 13 conflicting PRs that touchPthread.hpporThread.cpp: #4563 #7149 #8592 #9938 #12642 #12682 #12688 #12700 #12965 #15040 #16790 #19181 #19453. I read their diffs. None changes howPthreadPrivateis allocated. #7149 moves thread stacks into the guest arena, which is a different object.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-16T23:42:09Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
9ceb8ecf07:tools/check_conventions.pypassed.guest_thread_self_testsbuilt cleanly.guest_thread_self):core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-16T23:59:21Z.
Review: COMMENTED
Reviewed — no blocking findings
The allocation change and the changed test are consistent with the stated goal at head
9ceb8ecf077d603c1166acd8cd4d23ebafe157a7; I found no verified blocking defect, but did not run checks.What I ran
No commands or tests were run. I reviewed the prepared diff and targeted source context at the pinned head. The changed files listed in the manifest were all read; no excluded files were listed in the manifest.
files.jsonwas not available through the reader, so excluded-file status is not verified.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
Builds, tests, conventions, debt checks, runtime behavior, and author-reported results were not independently verified.
files.jsonwas unavailable, so I could not confirm whether the bundle has additional excluded files.To get this to a good state
Nothing is required.
Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T16:41:54Z.
Review: APPROVED
Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against
b13fbdf052:tools/check_conventions.pypassed.guest_raise_released_thread_tests,guest_thread_self_testsbuilt cleanly.guest_thread_self,guest_raise_released_thread):core/libs/prx/libkernel/Pthread/include/Pthread.hpp,core/libs/prx/libkernel/Pthread/src/Thread.cppreverted tomainand the new tests kept, the suite fails: Restoring the change makes it pass again.Looks Good To Me
Imported from boykopovar/AnyPS5. Originally posted by Superseabas on 2026-11-20T13:20:09Z.
Review: APPROVED
Windows check of head
b13fbdf052with GCC 16, merged ontomain0a025bd3c6(clean merge).Setup: Windows 11 Home 10.0.26200, Ryzen 7 7700X, WinLibs GCC 16.2.0 (
x86_64-ucrt-posix-seh, r3), CMake 4.4.4, Ninja 1.13.2, Release full build, run from PowerShell. This was built together with #23341 and #24190, which touch other files.^guest_(thread|pthread|raise_released), 70 tests includingguest_thread_self):main70 of 70, and with this PR 70 of 70 in 3 of 3 runs.guest_raise_released_threadis registered for Linux only, so itsdlsym(RTLD_NEXT, …)change doesn't run here.PthreadPrivate'soperator new/operator deletegoing back to the host heap (::operator new/::operator delete),guest_thread_selfaborts (0xc0000409) at the newRequireGuestObject. So the test does catch thread objects outside the guest allocations.AI-assisted: yes (Claude Code)
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:16:39Z.
Review: COMMENTED
Reviewed — no blocking findings
The change is coherent at head
b13fbdf052ef1f77ee048b89ac6164a24129bee4; no blocking defect was verified, but checks were not run in this review.What I ran
Inspected the prepared diff and all five changed files, plus the allocator and thread creation/adoption code they touch. No test or build commands were run.
Results
Negative Control
Could not run a negative control in this review.
Findings
Blocking
None.
Non-blocking
None.
Sources
Not verified
No build or tests were run. The PR description’s reported test results were not independently verified. All files marked reviewed in the manifest were read; no changed files were excluded.
To get this to a good state
Nothing is required. Optional: run the project checks on this head before merging.