Skip to content

fix(libc): give Windows frames without a DWARF record their function range from native unwind data - #24502

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
einfachama:fix/libc-unwind-seh-function-range
Nov 21, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
einfachama:fix/libc-unwind-seh-function-range

Conversation

@einfachama

Copy link
Copy Markdown
Contributor

What

unwind_cursor and unwind_enclosing_function fail on every Windows build with an SEH toolchain (MSYS2 UCRT64 GCC):

  • unwind cursor check failed: getInfo gives the range of the function holding the IP
  • enclosing function check failed: an address inside a function reports that function's first byte

With an SEH toolchain, the host binaries carry only .pdata/.xdata and no .eh_frame, so DecodeFrame finds no DWARF record for a host function. Stepping already falls back to RtlVirtualUnwind (VirtualUnwind), but:

  • the cursor's getInfo got only the start from that fallback: end 0, segment 0;
  • _Unwind_FindEnclosingFunction used only DecodeFrame and returned 0.

The same happens on every Windows build, WinLibs included, for any frame without a DWARF record, such as frames in system DLLs (ntdll, kernel32).

For a Windows frame without a DWARF record:

  • VirtualUnwind also sets the region end (EndAddress of the RUNTIME_FUNCTION that RtlLookupFunctionEntry returns) and the segment (image base). The only reader of these two fields is the cursor's getInfo. The personality and LSDA path reads region, textBase and dataBase, which are unchanged.
  • _Unwind_FindEnclosingFunction falls back to the BeginAddress of the RUNTIME_FUNCTION holding the address minus one, the same address rule as the DWARF lookup.
  • A chained entry (UNW_FLAG_CHAININFO) reports its own range, not the primary entry's. That matches how LLVM 18.1.8 getInfoFromSEH takes the range (libunwind/src/UnwindCursor.hpp), and it keeps getInfo's start equal to _Unwind_GetRegionStart. GCC emits no chained entries: none in libc.prx (3824 entries) or the test executables.
  • getInfo reports format 0 and no unwind info for these frames, as for macOS compact unwind (7d6091c). LLVM reports the RUNTIME_FUNCTION and its size there. This is recorded as a difference in the debt entry.

Sources: x64 exception handling (RUNTIME_FUNCTION, chained unwind info) and LLVM 18.1.8 UnwindCursor.hpp. There is no console behaviour to match: console code has no SEH frames.

The DWARF lookup is unchanged. Every new line in Unwind.cpp is inside #ifdef _WIN32 and runs only after DecodeFrame has found no record. On Linux and macOS, _Unwind_FindEnclosingFunction still returns 0 when DecodeFrame fails; only the order of its two return statements changed.

unwind_cursor accepts a frame without an FDE on SEH toolchains, as it already does for macOS compact unwind. It still requires start == probe && start < ip && ip < end. The two debt entries (cursor slots, _Unwind_FindEnclosingFunction) are updated through a fragment.

Related: #24354 (same SEH toolchain cause, test-side), #24358.

Tested

Windows 11 Pro 26H2 (10.0.26300), MSYS2 UCRT64 GCC 16.2.0 (SEH), Release, branch on main c8f72e9:

CI evidence:

WinLibs not tested locally, CI covers it. Linux and macOS were not built locally; their code is unchanged apart from the reordered return in _Unwind_FindEnclosingFunction.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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

…range from native unwind data

A Windows frame without a DWARF record (every host frame of an SEH build such
as MSYS2, and system DLLs on every Windows build) took only its start from
RtlLookupFunctionEntry: the cursor's getInfo reported end 0 and no segment,
and _Unwind_FindEnclosingFunction returned 0. Both now use the start and end
of the RUNTIME_FUNCTION RtlLookupFunctionEntry returns, as LLVM 18.1.8
getInfoFromSEH does, with the image base as the segment. A chained fragment
reports its own range. The DWARF lookup is unchanged.

unwind_cursor accepts a frame without an FDE on SEH toolchains, as it does
for macOS compact unwind, and still requires start < ip < end.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012VfFiztpSXVYPt12tPKGzZ

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against d420c8304c:

  • Conventions: tools/check_conventions.py passed.
  • Build: unwind_cursor_tests built cleanly.
  • CTest (unwind_cursor, unwind_cursor_get_register, unwind_cursor_get_float_register, unwind_cursor_set_register, unwind_cursor_set_float_register):
    1/5 Test #1400: unwind_cursor_get_float_register ...   Passed    0.03 sec
    2/5 Test #1401: unwind_cursor_set_register .........   Passed    0.03 sec
    3/5 Test #1399: unwind_cursor_get_register .........   Passed    0.03 sec
    4/5 Test #1398: unwind_cursor ......................   Passed    0.03 sec
    5/5 Test #1402: unwind_cursor_set_float_register ...   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 5
    
  • Negative control: with core/libs/prx/libc/src/exception/Unwind.cpp reverted to main and the new tests kept, the suite fails:
    1/5 Test #1399: unwind_cursor_get_register .........   Passed    0.02 sec
    2/5 Test #1401: unwind_cursor_set_register .........   Passed    0.02 sec
    3/5 Test #1400: unwind_cursor_get_float_register ...   Passed    0.02 sec
    4/5 Test #1398: unwind_cursor ......................   Passed    0.02 sec
    5/5 Test #1402: unwind_cursor_set_float_register ...   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 5
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libc/src/exception/Unwind.cpp: NativeFunctionStart correctly queries RtlLookupFunctionEntry with imageBase and computes imageBase + function->BeginAddress with proper null checking
    • Verified VirtualUnwind populates context.regionEnd (imageBase + function->EndAddress) and context.segment (imageBase) matching the libunwind AbstractUnwindCursor getInfo layout
    • Verified _Unwind_FindEnclosingFunction_nid_postfix correctly subtracts 1 from pc (context.registers[16] - 1) before lookup on Windows under #ifdef _WIN32 to ensure return addresses map to the calling function, with clean fallback to 0 on non-Windows/no record
    • Verified docs/dev/debt/changes/fix-libc-unwind-seh-function-range.md follows technical debt fragment conventions and correctly records the behavior difference from LLVM 18.1.8 and SEH chained entry handling
    • Verified core/libs/tests/UnwindCursor.cpp correctly tests the native SEH / compact unwind path without FDE when defined(SEH) or defined(APPLE)

Looks Good To Me

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this; the points below are optional.

What I ran

Reviewed head d420c8304c743864224bdf33ab127423b6dcbb6a. I read the prepared description, commit list, manifest, diff, and all three changed files. No tests or builds were run.

Results

Check Result Note
Diff and source review passed Three changed files read
Tests and builds not run Author-reported Windows results not independently verified

Findings

Blocking

None.

Non-blocking

None.

Not verified

The full test suite, Windows execution, CI evidence, and Linux/macOS builds were not independently verified. The review bundle included all changed files; no changed files were excluded.

To get this to a good state

Nothing is required.

@Kal0s-design

Copy link
Copy Markdown

A Windows run with the other toolchain, in case it is useful next to the MSYS2 UCRT64 results. Windows 11, WinLibs GCC 15 Release, head d420c8304c merged onto main c8d5f91168 (clean merge), ctest -R '^unwind_' (13 tests from the five unwind_*_tests executables) 5 times per tree.

tree rounds with all 13 passing
main c8d5f91168 5 of 5
main + only this PR's UnwindCursor.cpp 5 of 5
main + this PR 5 of 5

So with WinLibs the two failing checks from the description do not fail on main, with the old or the changed test, and this build cannot serve as a negative control. What it does show is that the new fallback changes nothing for the 13 unwind tests here.

I did not check why this toolchain's host functions still get their range on main, so I cannot say whether that is the DWARF record being present or something else.

AI-assisted: yes

@1tsethan

Copy link
Copy Markdown
Contributor

Native SEH unwind verification. Both unwind_cursor and unwind_enclosing_function failed on 732c68c9fd on Windows 11 with MSYS2 GCC 16.2.0. With the isolated diff from d420c8304c applied, both passed four consecutive CTest runs.

AI-assisted with OpenAI Codex.

@kiwe147

kiwe147 commented Nov 20, 2026

Copy link
Copy Markdown

Tested head d420c8304c, applied with git apply on main fd4aeed483 together with the other sixteen PRs of one batch (#24596, #24594, #24595, #24599, #24600, #24602, #24593, #24659, #24651, #24626, #24625, #24624, #24456, #24656, #22380, #24351). All seventeen applied without conflicts. The tree built except libSceAjm.prx: #24659 breaks it with duplicate patched export name: -qLsfDAywIY (checked alone on main, reported there). No other target failed to build.

Setup: Windows 11 Pro 10.0.26200, Ryzen 7 9800X3D, WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON. GPUs: NVIDIA GeForce RTX 3080 (driver 621.24) and the AMD Radeon iGPU of the CPU (gfx1036, driver 32.0.21036.18). No validation layer. iGPU run: ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1 ANYPS5_GPU="AMD Radeon", agc_/video tests only.

tests RTX 3080 iGPU
unwind_cursor_set_register, unwind_cursor_get_register, unwind_cursor_get_float_register and 2 more (5 tests) all pass outside the iGPU subset

Whole batch: the full ctest -j 12 (6524 tests) on the RTX 3080 has 4 failures, and all 4 also fail on main fd4aeed483 (agc_driver_stencil_plane_memory, agc_driver_msaa_dcc_unwatch, agc_storage_write_back_host_retile, and pr_overlap_names, which needs a newer Python than my 3.10). The 2125 agc_/video tests on the iGPU fail agc_driver_stencil_plane_memory (also on main), agc_driver_store_over_untracked (also fails alone on main on this iGPU, 6 of 6) and agc_driver_image_table_packed_guards (passes 5 of 5 alone on this tree, known flaky). None of this batch touches AGC.

This is one combined tree, not a run of this PR alone.

AI-assisted: yes (Claude Code)

@oneandonlydean
oneandonlydean merged commit a524e00 into boykopovar:main Nov 21, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants