fix(libkernel): return ESRCH from thread name, priority and affinity calls on a released thread handle #10816
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10816"
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 MarcZX10 on 2026-10-25T02:43:42Z.
What
scePthreadGetname,scePthreadGetprio(and soscePthreadGetschedparam),scePthreadGetaffinity,scePthreadSetaffinityandscePthreadRenamedereferenced thePthreadhandle with no check that the thread object still exists. A detached thread frees itsPthreadPrivatewhen it ends, so calling any of them on that handle read or wrote freed memory. AddressSanitizer reportsheap-use-after-freeinscePthreadGetprio(Thread.cpp, read of the freed object, freed in~ThreadGuard) and inscePthreadSetaffinity(write).Each now looks the handle up in the guest thread registry, which the object leaves before it is freed, and returns
SCE_KERNEL_ERROR_ESRCHwhen it is gone. The lookup holds the registry lock for the access, so the object cannot be freed under the call. This is the answerscePthreadAttrGet(#7957) andpthread_setaffinity_npandpthread_getprioalready give for an ended thread.Only released handles change. A thread that is joined or finished but still retained keeps answering as before:
GuestPthreadGetnamepinsscePthreadGetnameon a joined thread as success, and no console measurement exists for the others.scePthreadSetprioandscePthreadSetschedparamare not touched because the open #10133 already adds the ESRCH check there with PS5 measurements.Tested
Rebase follow-up (2026-10-25): rebased the three commits onto current upstream
maincdcbd551341927ed91cafb479b2d1fd5fffac9efwithout conflicts. On the rebased head,git diff --check upstream/main...HEADandpython3 tools/check_conventions.py --base upstream/main --head HEADpassed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime or sanitizer result is claimed. The ASan/runtime evidence below is from the prior base.Linux x86-64, AddressSanitizer + UBSan build (
ANYPS5_ENABLE_ASAN=ONwith-fsanitize=undefined, RelWithDebInfo). The host CPU gate was bypassed locally for the run only (not committed). No Windows, no ThreadSanitizer run for this change, no console hardware.guest_pthread_handle_after_exitcreates a detached thread, waits untilscePthreadGetprioreports ESRCH, then checks all five calls return ESRCH. On currentmainit aborts under ASan withheap-use-after-freeinscePthreadGetpriocalled from the test; with the fix it passes in three runs.guest_pthread_*,guest_cond_*,guest_rwlock_*,guest_semaphoreandguest_kernel_sys*tests give the same results as onmain.guest_pthread_stackaborts onmaintoo under this ASan build, because it asserts the local frame sits within 4 KiB of the top of the guest stack and ASan frames are larger;guest_semaphore_tracewas not built in my selective build.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 (none added).mdfiles or images added to the repositoryRebase validation (2026-10-25)
Rebased the three commits onto current upstream
maina5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762without conflicts. On this head, strict GCC C++20 syntax-only checks with-Wall -Wextra -Werrorpassed forThread.cpp(suppressing its existing missing-field-initializer warning) andGuestPthreadHandleAfterExit.cpp.git diff --check upstream/main...HEADandpython3 tools/check_conventions.py --base upstream/main --head HEADalso passed. The full local CMake test build could not configure because the SDL2 CMake package is unavailable, so no post-rebase runtime or sanitizer result is claimed. The ASan/runtime evidence above is from the prior base.Imported from boykopovar/AnyPS5. Originally posted by thepoppfish on 2026-10-25T08:58:03Z.
Review: COMMENTED
Independent native Windows validation of exact head
f7350f42258f773ebbb515cae848bc2136b726b6.Windows 11 build 26200, MSYS2 UCRT64 GCC 16.2.0-6 (SEH), CMake 4.3.1, Ninja 1.13.2, CPU only:
Process.cppdoes, a throwing unimplemented entry, and Windows abort-report UI suppression.GuestPthreadHandleAfterExit.cpp,GuestPthreadGetname.cppandGuestPthreadJoinDetach.cppall pass. Three consecutive repeat runs of each also pass. This covers the new 20-round detached-thread case, preserves the SCE retained/joined-name answer alongside the POSIX joined-thread ESRCH answer, and checks join/detach compatibility.Thread.cppwith main's prior implementation and retain the PR's new test and other sources. The new test fails after 5.08 s. GDB catches its abort inRequire/main; the optimized backtrace does not identify the inlined wait helper, so no more precise backtrace claim is made. This is behavioral negative-control evidence, not an independent ASan use-after-free report.2742004ed28f765c7147db0e0269ff5868e9eb6f; among the production sources under test, only the intendedThread.cppadditions differ from that main. Builds emit existing unused-function/parameter and missing-initializer warnings in unchanged code, plus LTO's serial-partition notice; no-Werrorresult is claimed.The registry lock is held through each protected access, with the name lock released before the registry lock on the name paths. The distinction between registered and live threads preserves the retained-object behavior covered above. I found no additional blocking source issue in this pass. I am leaving this as validation evidence while the existing debt-entry update requested by @oneandonlydean remains pending; these tests do not establish the unmeasured console results.
This is target-level native Windows testing, not a full shared-libc/libkernel integration build, sanitizer/TSan run, console/game run or cross-PR composition test. No maintainer-triggered full CI is claimed. AI-assisted review and testing: yes (OpenAI Codex).
Out-of-tree replay files
CMakeLists.txt:
Support.cpp:
Use GCC 16 UCRT64 bin first on PATH, followed by CMake/Ninja. Configure with
cmake -S native-thread-review -B native-thread-review/build16 -G Ninja -DREPO=<exact-PR-checkout> -DCMAKE_CXX_COMPILER=<GCC16-bin>/g++.exe -DCMAKE_BUILD_TYPE=Release, build, then runctest --test-dir native-thread-review/build16 --output-on-failure --repeat until-fail:3. For the negative control use a separate build directory with-DTHREAD_SOURCE=<main-Thread.cpp-copy>and run onlyguest_pthread_handle_after_exit. The extra original-source include directory preserves Thread.cpp's relative header paths when using that copy.