fix(libc): keep thread names NUL-terminated in the Windows crash report #14406
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14406"
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:35:45Z.
What
On
26fa1212b8(line numbers below refer to it) the Windows crash report reads past a thread name buffer when a thread description needs 128 or more bytes in UTF-8. Both places that copy a description into the report convert it withWideCharToMultiByte(CP_UTF8, 0, description, -1, buf, sizeof(buf), ...)into a zero-initialisedchar[128]and ignore the result: the faulting thread atcore/libs/prx/libc/src/specifics/windows/CrashReport.cpp:373-375, printed with'%s'at line 378, and every other thread inReportAllThreadsat lines 591-593, printed with'%s'byReportThreadContextat line 537. When the converted name does not fit,WideCharToMultiBytereturns 0 withERROR_INSUFFICIENT_BUFFER, and on this machine it has then already filled all 128 bytes, leaving no NUL, so%sreads beyond the array, which C11 7.21.6.1p8 does not allow: without a precision "the array shall contain a null character". Such names are reachable:scePthreadRenamestores and applies a guest name of any length (core/libs/prx/libkernel/Pthread/src/Thread.cpp:1316-1322), its byte-to-wchar_tcopy turns every byte from 0x80 up into at least two UTF-8 bytes, and host threads can carry descriptions of any length.sizeof(buf) - 1.WideCharToMultiBytewrites at mostcbMultiBytebytes, so the last byte of the zero-initialised buffer always stays NUL whatever the call leaves in the rest, and a name that does not fit is printed cut to at most 127 bytes. Names of up to 127 UTF-8 bytes print exactly as before; a name of exactly 127 bytes now makes the call fail, and on this machine the bytes it leaves and the printed text are the same. That the cut falls on a character boundary, so the printed name stays valid UTF-8, is also what this machine shows (43 x U+65E5 into 127 bytes left 126 bytes and a NUL); the documentation promises neither. The Latin-1 widening inThread.cppand the Linux crash report are not changed.windows_crash_thread_name(Windows only) loads libc, names the main thread 126 xa,Z, 70 xband a worker sleeping inSleep(INFINITE)126 xc,Y, 70 xd, checks bothSetThreadDescriptionresults, then reads address 8. It expectson thread N 'a...aZ'and laterthread N 'c...cY':;Zb,Ydordid not faultfail it. No existing crash report test sets a thread description or matches the printed name;windows_crash_textis unchanged.pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare and cannot compare 17 that do not merge withmain: #7192, #11628, #11661, #11689 and #11698, which also changespecifics/windows/CrashReport.cpp, and #5021, #7275, #8650, #9156, #9162, #9293, #9297, #9302, #9383, #12642, #13253 and #13256, which also changecore/libs/CMakeLists.txt. Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files thatmainwith it already conflicts in;CrashReport.cppmerges cleanly with all of them, and incore/libs/CMakeLists.txt, which already conflicts withmainfor #5021, #7275 and #8650, the new test block stays outside the conflict and is the same conflict as onmain.Tested
Windows 11, WinLibs GCC 15.2.0, Ninja Release build (
BUILD_TESTING=ON) of the targetslibc,windows_crash_thread_name_testsandwindows_crash_text_testson067c700c57(based on26fa1212b8):cmake --build build --target libc windows_crash_thread_name_tests windows_crash_text_tests, thenctest --test-dir build -R "^windows_crash_(thread_name|text)$" -V.CrashReport.cppof26fa1212b8:windows_crash_thread_namefails withError regular expression found in output. Regex=[Zb|Yd|did not fault]; the report printsFATAL: unhandled exception 0xc0000005 on thread N 'a...aZb\x02'andthread N 'c...cYd': rip ..., 128 name bytes each plus, for the faulting thread, a stray byte 0x02, so the bytes after each array were read.windows_crash_textpasses.'a...aZ'and the worker as'c...cY':, 127 bytes each.ctest --test-dir build -R "^(windows_crash_thread_name|windows_crash_text|undeliverable_host_exception|crash_text|windows_exceptions|windows_crash_stack_words|windows_crash_without_stack_words)$": 7 of 7 passed.configure_host_test_executable(docs/dev/CONVENTIONS.md, host-only test executables), so it links with-static -static-libgcc -static-libstdc++andobjdump -pshows nolibstdc++-6.dllimport; with that,ctest --test-dir build -R '^(windows_crash_thread_name|windows_crash_text)$'passes 2/2.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-01T12:47:54Z.
Review: COMMENTED
Reviewed — no blocking findings
The Windows crash-report thread-name conversion now caps
WideCharToMultiByteatsizeof(buffer) - 1in both places, so the name stays NUL-terminated, and the hosted Windows run for this head passed all 2585 CTest tests, includingwindows_crash_thread_name_tests; I found no blocking issue at067c700c57.What I ran
Exact head
067c700c574e4f3079d38e91e0416670fe0f1d33; PR baseb2620e56d114ef053f8b45433b1d48aff7ec37ba(precheck base), unchanged at completion. No local build and no local test run. Commands:The precheck's ABI and test-quality helpers were missing from the isolated head tree, so both were run by hand. I also read every
WideCharToMultiBytecall incore/libs/prx/libc/src/specifics/windows/CrashReport.cpp(L375 and L593): both passsizeof(buffer) - 1as the output size.Results
possible-unregistered-cpp-testfinding; false positive, because the test is registered incore/libs/CMakeLists.txtL674-L675windows_crash_thread_name_testsFindings
Blocking: none.
Non-blocking: none.
Not verified
CrashReport.cppandcore/libs/CMakeLists.txt, in different regions) was not re-checked for merge order.To get this to a good state
No change is needed for this review.