fix(libkernel): convert guest thread names from UTF-8 for the Windows thread description #14402
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14402"
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 roboterspinne on 2026-11-01T11:34:39Z.
What
On
26fa1212b8(line numbers below refer to it) the Windows description of a guest thread garbles every non-ASCII name.StartNativeThread(core/libs/prx/libkernel/Pthread/src/Thread.cpp:912) andscePthreadRename(Thread.cpp:1321, also reached bypthread_rename_npandpthread_set_name_np,Pthread/Posix/Thread.cpp:355and:368) build theSetThreadDescriptionstring withstd::wstring(name.begin(), name.end()), which sign-extends eachchartowchar_t. Every byte from 0x80 up becomes a unit in U+FF80..U+FFFF, which is wrong under any encoding: a thread created or renamed as"Z\xc3\xa4hler-\xe6\x97\xa5"("Zähler-日") reads back as005a ffc3 ffa4 0068 006c 0065 0072 002d ffe6 ff97 ffa5instead of005a 00e4 0068 006c 0065 0072 002d 65e5. The repository already treats the description as the UTF-16 form of a UTF-8 name: the Windows crash report reads it back withWideCharToMultiByte(CP_UTF8, ...)for the faulting thread (core/libs/prx/libc/src/specifics/windows/CrashReport.cpp:374-376) and for the all-threads list (CrashReport.cpp:592-594), so today both lines print each non-ASCII byte as a three-byte sequence, and debuggers and ETW/profilers show the same garbage.MultiByteToWideChar(CP_UTF8, 0, ...)of the guest name bytes, as the host log file path conversion does (core/libs/prx/libc/src/LogFilePlatform.cpp:47-53); an empty name gives an empty description. Flags are 0 rather thanMB_ERR_INVALID_CHARSbecause the description is host-only and best-effort: invalid UTF-8 becomes U+FFFD (checked with a probe:"bad\xff\xc3"gives0062 0061 0064 fffd fffd) and never fails the guest call, and theSetThreadDescriptionresult stays ignored as before. These two lines are the only places that set the description from a guest name. Guest-visible results do not change:scePthreadGetnameandpthread_getname_npreadthread->name, which this change does not touch.ThreadName::SetCurrent(core/libs/prx/libc/include/general/ThreadName.hpp:22-36) is not reused: it widens bytes as Latin-1 with a 63-character cap, which would still garble UTF-8, and its only production caller (libSceAgcDriver/Execution/src/Driver/Queues/WorkerAffinity.cpp:27) passes fixed ASCII role labels, so it stays unchanged.guest_pthread_renamecreates its worker as"Z\xc3\xa4hler"and, on Windows, requiresGetThreadDescriptionin the worker to beL"Zähler"(creation path,Thread.cpp:912), then renames the worker withscePthreadRenameto"Z\xc3\xa4hler-\xe6\x97\xa5"and requiresL"Zähler-日"(rename path,Thread.cpp:1321). On a mismatch it prints the UTF-16 units it read and the expected ones. The stored-name checks of the test are unchanged.pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare and cannot compare 19 that also changeThread.cppbut do not merge withmain(#3986, #4563, #5021, #7149, #7804, #8477, #8592, #9938, #11624, #11628, #11661, #11689, #11698, #12544, #12642, #12682, #12688, #12700, #12965). Checked by hand: a trial merge of this branch with each of them conflicts only in the files thatmainwith it already conflicts in; withinThread.cpp, #4563, #8477 and #12642 conflict in the same hunks as withmain, and #7149 and #9938 add one more conflict because each inserts its own helper directly after the#ifdef _WIN32that precedesStartNativeThread, where this change addsThreadDescription; keeping both helpers resolves it.Tested
Windows 11, WinLibs GCC 15.2.0 (
gcc.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders, r7) 15.2.0), Ninja, Release,BUILD_TESTING=ON, based on26fa1212b8:cmake --build build --target guest_pthread_rename_tests, thenctest --test-dir build -R '^guest_pthread_rename$' -V.Thread.cppof26fa1212b8: fails withthread description units: 005a ffc3 ffa4 0068 006c 0065 0072; expected: 005a 00e4 0068 006c 0065 0072(Exit code 0xc0000409). With only the creation path converted (run with an earlier draft of the test that had the same two checks): fails at the rename check withthread description units: 005a ffc3 ffa4 0068 006c 0065 0072 002d ffe6 ff97 ffa5; expected: 005a 00e4 0068 006c 0065 0072 002d 65e5. With this change:Passed. The unchanged test of26fa1212b8passes on26fa1212b8.ctest --test-dir build -R '^(guest_pthread_rename|thread_name|guest_kernel_sys_pthread|guest_kernel_sys_pthread_np_rwlockattr|guest_kernel_sys_pthread_np_kind|guest_posix_thread_name|guest_pthread_cond_setname|guest_pthread_create_suspended|guest_pthread_getname|guest_pthread_setname|guest_pthread_start)$'with this change: 11 of 11 passed.python tools/check_conventions.py --base 26fa1212b8with this title and description passed.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 MarcZX10 on 2026-11-01T11:45:25Z.
Review: COMMENTED
Reviewed — looks good, one non-blocking note
The fix matches the code it replaces and the new test checks it on Windows, but the new test calls a Windows 10 API directly, which the repository handles differently in its other Windows test targets.
What I ran
Exact head
fd1ec933155acf3aec5215c3a95b069333b183a8, baseb2620e56d114ef053f8b45433b1d48aff7ec37ba(currentmain, so no merge preview was needed). Nothing was built or compiled, and no test was run, per the no-build protocol for initial review.The precheck's ABI and test-quality helpers could not run in its isolated head tree (their scripts were missing there), so I ran both helpers by hand from
/marc/AnyPS5, as shown above. The precheck's diff, conventions, and live dependency and mergeability checks ran inside the precheck.Results
passed_with_warnings, one lexical finding (ResetOnce, defined inPthread/Posix/Once.cpp, which this PR does not touch)GuestPthreadRename.cppnot found literally in CMake; false positive, becausecore/libs/prx/libkernel/CMakeLists.txt:2307registers it through${LIBS_INCLUDE_DIR}/tests/GuestPthreadRename.cppTest-quality helper, run by hand (unedited JSON)
Findings
Blocking: none.
Non-blocking:
GuestPthreadRename.cppcallsGetThreadDescriptiondirectly, and its target sets no Windows version macro. The call is atcore/libs/tests/GuestPthreadRename.cpp:19, with<windows.h>at line 11. The target is defined atcore/libs/prx/libkernel/CMakeLists.txt:2307-2309and sets no_WIN32_WINNT. The repository handles this API differently elsewhere. The two Windows test targets that use Windows 10 APIs set_WIN32_WINNT=0x0A00explicitly (core/libs/CMakeLists.txt:607and:617). The other callers resolve the function at run time (core/libs/tests/ThreadName.cpp:10andcore/libs/prx/libc/src/specifics/windows/CrashReport.cpp:240). With a direct call, the declaration depends on the toolchain's default header version, and on a Windows version withoutGetThreadDescriptionthe test executable would fail at load time, not report a test result. Smallest fix: add_WIN32_WINNT=0x0A00toguest_pthread_rename_testsunderif(WIN32), as lines 607 and 617 do, or resolve the function at run time asThreadName.cppdoes. This does not block the change: the author's Windows 11 build compiled it.Checked from source with no issue found: the old
std::wstring(name.begin(), name.end())widens eachcharby sign extension, as the PR says.ThreadDescriptionusesCP_UTF8with flags 0 and a buffer sized by a first call, so an empty name gives an empty description and invalid bytes become U+FFFD without failing the call. The name is stored atThread.cpp:1001, before_beginthreadexat line 1015, so the creation path sees it. The rename path uses the same helper undernameLock.Sources
Links pinned to the reviewed head:
github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/tests/GuestPthreadRename.cpp (L19)github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/CMakeLists.txt (L607)github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/CMakeLists.txt (L617)github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/prx/libkernel/CMakeLists.txt (L2307-L2309)github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/tests/ThreadName.cpp (L10)github.com/boykopovar/AnyPS5@fd1ec93315/core/libs/prx/libc/src/specifics/windows/CrashReport.cpp (L240)Not verified
To get this to a good state
No change is required for approval beyond the optional fix in finding 1. If the author adds the Windows version define or the run-time resolution, the same test and the hosted Windows run of
guest_pthread_renameon the new head are the evidence needed. I will re-check the head and its results before any approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-01T12:16:52Z.
Review: APPROVED
Reviewed — looks good
The Windows thread-name conversion is correct at
fd1ec93315, and the hosted Windows run for this exact head passed the new rename test, which closes the approval gap left by my earlier comment.What I ran
Exact head
fd1ec933155acf3aec5215c3a95b069333b183a8; PR baseb2620e56d114ef053f8b45433b1d48aff7ec37ba, unchanged at completion. No build and no local test run. Commands:The precheck's ABI and test-quality helpers were missing from the isolated head tree; the ABI helper was run by hand from
/marc/AnyPS5/tools.Results
ResetOnce, a file-local helper inPthread/Posix/Once.cppthat is not an export; no export declaration changedcore/libs/prx/libkernel/CMakeLists.txt(L2307-L2311); the heuristic's finding is a path false positiveguest_pthread_rename_testsbuilt and passed (1 of 1 in that job)Unedited hosted log lines (Windows job)
Findings
Blocking: none.
Non-blocking: the rename test calls
GetThreadDescriptionstatically without a_WIN32_WINNTdefinition for its own target (core/libs/tests/GuestPthreadRename.cppL19; target atcore/libs/prx/libkernel/CMakeLists.txtL2307-L2309). The hosted GCC 15 build accepted it, so this is only a portability note for other toolchains, as noted in my earlier comment.Sources
GuestPthreadRename.cppat the reviewed headNot verified
To get this to a good state
Nothing is required for the approval. The
_WIN32_WINNTnote is optional.