fix(libc): keep thread names NUL-terminated in the Windows crash report #14406

Merged
roboterspinne merged 1 commit from upstream-pr/14406 into main 2026-11-02 19:49:58 +00:00
roboterspinne commented 2026-11-01 11:35:45 +00:00 (Migrated from github.com)

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 with WideCharToMultiByte(CP_UTF8, 0, description, -1, buf, sizeof(buf), ...) into a zero-initialised char[128] and ignore the result: the faulting thread at core/libs/prx/libc/src/specifics/windows/CrashReport.cpp:373-375, printed with '%s' at line 378, and every other thread in ReportAllThreads at lines 591-593, printed with '%s' by ReportThreadContext at line 537. When the converted name does not fit, WideCharToMultiByte returns 0 with ERROR_INSUFFICIENT_BUFFER, and on this machine it has then already filled all 128 bytes, leaving no NUL, so %s reads 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: scePthreadRename stores and applies a guest name of any length (core/libs/prx/libkernel/Pthread/src/Thread.cpp:1316-1322), its byte-to-wchar_t copy turns every byte from 0x80 up into at least two UTF-8 bytes, and host threads can carry descriptions of any length.

  • Rule. Both calls pass sizeof(buf) - 1. WideCharToMultiByte writes at most cbMultiByte bytes, 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 in Thread.cpp and the Linux crash report are not changed.
  • Tests. New windows_crash_thread_name (Windows only) loads libc, names the main thread 126 x a, Z, 70 x b and a worker sleeping in Sleep(INFINITE) 126 x c, Y, 70 x d, checks both SetThreadDescription results, then reads address 8. It expects on thread N 'a...aZ' and later thread N 'c...cY':; Zb, Yd or did not fault fail it. No existing crash report test sets a thread description or matches the printed name; windows_crash_text is unchanged.
  • Overlap. pr_overlap.py --branch finds no shared lines with the open pull requests it can compare and cannot compare 17 that do not merge with main: #7192, #11628, #11661, #11689 and #11698, which also change specifics/windows/CrashReport.cpp, and #5021, #7275, #8650, #9156, #9162, #9293, #9297, #9302, #9383, #12642, #13253 and #13256, which also change core/libs/CMakeLists.txt. Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files that main with it already conflicts in; CrashReport.cpp merges cleanly with all of them, and in core/libs/CMakeLists.txt, which already conflicts with main for #5021, #7275 and #8650, the new test block stays outside the conflict and is the same conflict as on main.

Tested

Windows 11, WinLibs GCC 15.2.0, Ninja Release build (BUILD_TESTING=ON) of the targets libc, windows_crash_thread_name_tests and windows_crash_text_tests on 067c700c57 (based on 26fa1212b8):

  • cmake --build build --target libc windows_crash_thread_name_tests windows_crash_text_tests, then ctest --test-dir build -R "^windows_crash_(thread_name|text)$" -V.
  • Counter-check with the new test and the CrashReport.cpp of 26fa1212b8: windows_crash_thread_name fails with Error regular expression found in output. Regex=[Zb|Yd|did not fault]; the report prints FATAL: unhandled exception 0xc0000005 on thread N 'a...aZb\x02' and thread 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_text passes.
  • With this change both pass: the faulting thread prints as '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.
  • The test executable is registered with configure_host_test_executable (docs/dev/CONVENTIONS.md, host-only test executables), so it links with -static -static-libgcc -static-libstdc++ and objdump -p shows no libstdc++-6.dll import; with that, ctest --test-dir build -R '^(windows_crash_thread_name|windows_crash_text)$' passes 2/2.
  • python tools/check_conventions.py --base 26fa1212b8 with this title and description passed.

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
  • 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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5106952798 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14406). Originally posted by [roboterspinne](https://github.com/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 with `WideCharToMultiByte(CP_UTF8, 0, description, -1, buf, sizeof(buf), ...)` into a zero-initialised `char[128]` and ignore the result: the faulting thread at `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp:373-375`, printed with `'%s'` at line 378, and every other thread in `ReportAllThreads` at lines 591-593, printed with `'%s'` by `ReportThreadContext` at line 537. When the converted name does not fit, `WideCharToMultiByte` returns 0 with `ERROR_INSUFFICIENT_BUFFER`, and on this machine it has then already filled all 128 bytes, leaving no NUL, so `%s` reads 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: `scePthreadRename` stores and applies a guest name of any length (`core/libs/prx/libkernel/Pthread/src/Thread.cpp:1316-1322`), its byte-to-`wchar_t` copy turns every byte from 0x80 up into at least two UTF-8 bytes, and host threads can carry descriptions of any length. - **Rule.** Both calls pass `sizeof(buf) - 1`. `WideCharToMultiByte` writes at most `cbMultiByte` bytes, 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 in `Thread.cpp` and the Linux crash report are not changed. - **Tests.** New `windows_crash_thread_name` (Windows only) loads libc, names the main thread 126 x `a`, `Z`, 70 x `b` and a worker sleeping in `Sleep(INFINITE)` 126 x `c`, `Y`, 70 x `d`, checks both `SetThreadDescription` results, then reads address 8. It expects `on thread N 'a...aZ'` and later `thread N 'c...cY':`; `Zb`, `Yd` or `did not fault` fail it. No existing crash report test sets a thread description or matches the printed name; `windows_crash_text` is unchanged. - **Overlap.** `pr_overlap.py --branch` finds no shared lines with the open pull requests it can compare and cannot compare 17 that do not merge with `main`: #7192, #11628, #11661, #11689 and #11698, which also change `specifics/windows/CrashReport.cpp`, and #5021, #7275, #8650, #9156, #9162, #9293, #9297, #9302, #9383, #12642, #13253 and #13256, which also change `core/libs/CMakeLists.txt`. Checked by hand: a trial merge of this branch with each of them conflicts in exactly the files that `main` with it already conflicts in; `CrashReport.cpp` merges cleanly with all of them, and in `core/libs/CMakeLists.txt`, which already conflicts with `main` for #5021, #7275 and #8650, the new test block stays outside the conflict and is the same conflict as on `main`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows 11, WinLibs GCC 15.2.0, Ninja Release build (`BUILD_TESTING=ON`) of the targets `libc`, `windows_crash_thread_name_tests` and `windows_crash_text_tests` on 067c700c57 (based on 26fa1212b8): - `cmake --build build --target libc windows_crash_thread_name_tests windows_crash_text_tests`, then `ctest --test-dir build -R "^windows_crash_(thread_name|text)$" -V`. - Counter-check with the new test and the `CrashReport.cpp` of 26fa1212b8: `windows_crash_thread_name` fails with `Error regular expression found in output. Regex=[Zb|Yd|did not fault]`; the report prints `FATAL: unhandled exception 0xc0000005 on thread N 'a...aZb\x02'` and `thread 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_text` passes. - With this change both pass: the faulting thread prints as `'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. - The test executable is registered with `configure_host_test_executable` (`docs/dev/CONVENTIONS.md`, host-only test executables), so it links with `-static -static-libgcc -static-libstdc++` and `objdump -p` shows no `libstdc++-6.dll` import; with that, `ctest --test-dir build -R '^(windows_crash_thread_name|windows_crash_text)$'` passes 2/2. - `python tools/check_conventions.py --base 26fa1212b8` with this title and description passed. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) - [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: <!-- #PR, or none --> none - [x] Fixes: <!-- issue it closes as Fixes #N, or none --> none - [x] Unblocks: <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> none - [x] GPUs tested: <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> n/a - [x] AI-assisted: <!-- yes / no --> yes (Claude Code)
MarcZX10 commented 2026-11-01 12:47:54 +00:00 (Migrated from github.com)

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 WideCharToMultiByte at sizeof(buffer) - 1 in both places, so the name stays NUL-terminated, and the hosted Windows run for this head passed all 2585 CTest tests, including windows_crash_thread_name_tests; I found no blocking issue at 067c700c57.

What I ran

Exact head 067c700c574e4f3079d38e91e0416670fe0f1d33; PR base b2620e56d114ef053f8b45433b1d48aff7ec37ba (precheck base), unchanged at completion. No local build and no local test run. Commands:

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14406
python3 tools/pr_abi_audit.py --base b2620e56d114ef053f8b45433b1d48aff7ec37ba --head 067c700c574e4f3079d38e91e0416670fe0f1d33 --json
python3 tools/pr_test_quality.py --root <git archive of 067c700c57: core/libs/tests, core/libs/prx/libc, core/libs/CMakeLists.txt> --json core/libs/tests/WindowsCrashThreadName.cpp

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 WideCharToMultiByte call in core/libs/prx/libc/src/specifics/windows/CrashReport.cpp (L375 and L593): both pass sizeof(buffer) - 1 as the output size.

Results

Check Result
Diff check (precheck) Passed
Conventions and PR template (precheck) Passed
Live dependency and mergeability (precheck) Passed
ABI audit (by hand) Not applicable: no export declaration changed
Test-quality heuristic (by hand) One possible-unregistered-cpp-test finding; false positive, because the test is registered in core/libs/CMakeLists.txt L674-L675
Hosted Windows, job at this head Success; 100% of 2585 CTest tests passed, including windows_crash_thread_name_tests
Hosted relinker (Ubuntu, Windows), relinker-asan, register-context (Ubuntu) Success
Hosted macOS jobs, write-watch, register-context (macOS) Queued at this head

Findings

Blocking: none.

Non-blocking: none.

Not verified

  • Nothing was compiled or run locally.
  • The macOS jobs are still queued at this head. Approval is deferred until they finish, which is why this review is COMMENTED.
  • The overlap with #14405 (both edit CrashReport.cpp and core/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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5728131083 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14406#pullrequestreview-5728131083). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-01T12:47:54Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The Windows crash-report thread-name conversion now caps `WideCharToMultiByte` at `sizeof(buffer) - 1` in both places, so the name stays NUL-terminated, and the hosted Windows run for this head passed all 2585 CTest tests, including `windows_crash_thread_name_tests`; I found no blocking issue at `067c700c57`. ## What I ran Exact head `067c700c574e4f3079d38e91e0416670fe0f1d33`; PR base `b2620e56d114ef053f8b45433b1d48aff7ec37ba` (precheck base), unchanged at completion. No local build and no local test run. Commands: ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14406 python3 tools/pr_abi_audit.py --base b2620e56d114ef053f8b45433b1d48aff7ec37ba --head 067c700c574e4f3079d38e91e0416670fe0f1d33 --json python3 tools/pr_test_quality.py --root <git archive of 067c700c57: core/libs/tests, core/libs/prx/libc, core/libs/CMakeLists.txt> --json core/libs/tests/WindowsCrashThreadName.cpp ``` 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 `WideCharToMultiByte` call in `core/libs/prx/libc/src/specifics/windows/CrashReport.cpp` (L375 and L593): both pass `sizeof(buffer) - 1` as the output size. ## Results | Check | Result | |---|---| | Diff check (precheck) | Passed | | Conventions and PR template (precheck) | Passed | | Live dependency and mergeability (precheck) | Passed | | ABI audit (by hand) | Not applicable: no export declaration changed | | Test-quality heuristic (by hand) | One `possible-unregistered-cpp-test` finding; false positive, because the test is registered in `core/libs/CMakeLists.txt` L674-L675 | | Hosted Windows, job at this head | Success; 100% of 2585 CTest tests passed, including `windows_crash_thread_name_tests` | | Hosted relinker (Ubuntu, Windows), relinker-asan, register-context (Ubuntu) | Success | | Hosted macOS jobs, write-watch, register-context (macOS) | Queued at this head | ## Findings **Blocking:** none. **Non-blocking:** none. ## Not verified - Nothing was compiled or run locally. - The macOS jobs are still queued at this head. Approval is deferred until they finish, which is why this review is COMMENTED. - The overlap with #14405 (both edit `CrashReport.cpp` and `core/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.
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!14406
No description provided.