Skip to content

[AUTOMATED] fix(analysis): mark the libstdc++ std::__throw_* family no-return (DIV-78) - #311

Merged
mahaloz merged 1 commit into
mainfrom
fix/i273-throw-noreturn
Aug 17, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/i273-throw-noreturn

Conversation

@mahaloz

@mahaloz mahaloz commented Aug 17, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #273

What was wrong

Every helper in libstdc++'s <bits/functexcept.h> — std::__throw_length_error,
__throw_out_of_range, __throw_bad_alloc, __throw_logic_error, __throw_bad_cast,
__throw_invalid_argument, … (20 in total, plus __throw_regex_error from
<bits/regex_error.h>) — is declared __attribute__((__noreturn__)), and every body
ends in _GLIBCXX_THROW_OR_ABORT: a throw, or abort() when the library is built
-fno-exceptions. None of them can return.

Upstream Ghidra's ElfFunctionsThatDoNotReturn names __cxa_throw and the terminate
entry points but omits the whole family, and the source-level attribute survives into
no binary artifact: at the decompiler's boundary std::__throw_length_error is an
ordinary UND .dynsym import reached through a PLT stub. Nothing but the shipped list
can prove the call cannot return. So flow followed the fall-through and emitted whatever
the compiler had parked after the call — clang's call __stack_chk_fail
unreachable-trap, the next function's entry, or (apt-get sub_6480) the
exception-cleanup landing pad, rendered as
__cxa_begin_catch(std::__throw_logic_error(...)); … _Unwind_Resume().

The gap was masked until #272: noreturn_disc's evidence tally counts a call site whose
fall-through "is not a valid instruction start", and a failed decode satisfied that
test, so clang's 15-byte alignment NOP was silently voting std::__throw_length_error
no-return. With the padding decoding, the tally drops below its 3-site floor and the dead
code reappeared. That accidental crutch is what this PR replaces with the real fact.

Vendored-in-place vs. a supplementary list — and why in-place won

The 20 helpers are appended to the vendored file in place, fenced by a
# (kuna divergence, DIV-78) comment block. The reasoning, having checked rather than
assumed:

  1. There is no sync to conflict with. tools/sync_upstream.py's vendored scope is
    exactly Ghidra/Features/Decompiler/src/decompile/datatests/ → tests/datatests/ and
    Ghidra/Processors/*/data/languages/. Ghidra/Features/Base/data/ is not in
    PATH_MAP, not in UPSTREAM_PREFIXES, and there is no verbatim-hash guard anywhere in
    the tree. This file is vendored by hand, once, and a future sync will never touch it.
  2. In-place with a fenced DIV block is already the convention for this exact file.
    DIV-21 added the BSD err/errx family and the glibc fatal internals the same way,
    with the same style of comment header. A second, differently-shaped divergence
    mechanism alongside it would be less self-documenting, not more.
  3. A supplement would need new wiring in every consumer and would introduce a second
    place for the two matchers to drift apart. Both include_str! consumers
    (kuna-analysis::loader::noreturn for noreturn_known, and
    kuna-decomp::p2_lift::kuna_noreturn_externmatch for noreturn_externmatch) pick the
    in-place block up with zero code change.

The file's "vendored verbatim" header is preserved as provenance for the upstream half;
both divergence blocks sit below it, each labelled with its DIV number.

Shape of the entries

Each helper is listed twice, because the two matchers see two spellings of the same
symbol:

  • the mangled .dynsym name, which the analysis-tier scan matches before
    demangling — written as a trailing-* wildcard over the Itanium ZSt<len>__throw_<name>
    prefix, so a signature change cannot age the entry out (__throw_ios_failure gained a
    const char*, int overload in GCC 7; __throw_regex_error gained one in GCC 11). The
    prefix is an exact ABI encoding of std::__throw_<name>, so the wildcard cannot reach
    any other symbol.
  • the demangled display name, which the flow-time matcher sees: the std-only
    namespace guard admits std::__throw_length_error and the leading-underscore strip
    reduces it to throw_length_error.

A same-named method on a user class (Buf::__throw_length_error) stays out via the
existing namespace guard; both are pinned by tests.

No new option

The behavior is already gated twice over by existing default-ON options, so a third flag
would only add catalog surface for no control:

  • noreturn_known (analysis tier) reads the file,
  • noreturn_externmatch (flow seam, DIV-13) include_str!s the same file.

--option noreturn_known off --option noreturn_externmatch off reproduces the pre-fix
rendering byte-identically, and that is exactly the two-pass the new e2e test drives. No
catalog counts move (kuna catalog --json on the rebased tree reports 109, the value
#309 left, and git diff touches none of phases.toml / options.rs /
catalog_bytecompat.rs / phase_catalog.json / docs/options.md / kuna-catalog.xml),
no phases.toml row, no docs/options.md regen. xml.rs's corpus count is likewise
untouched at 207 (no stages XML added).

One finding worth recording: contrary to the issue text, noreturn_extern (DIV-14) does
not read this file — it carries a frozen hard-coded copy of upstream's 21 names, and
has already drifted (it is missing DIV-21's additions too). It is queried only after
noreturn_externmatch has declined, so for any name in the file it is subsumed and the
fix reaches the undefined-extern path regardless. That pre-existing duplication is left
alone here rather than bundled in.

Witnesses (base = origin/main release build, fixed = this branch)

Witness 1 — /usr/bin/FileCheck-14 (clang-14, 528 KB, 1,155 functions; the GH-273
binary), whole-binary decompile-all --json:

base fixed
functions changed — 72 / 1,155
of those, functions that grew — 0
// no-return annotations 69 102
emitted lines 63,247 63,141 (−106)

All 72 changed functions contain a std::__throw_* call in the base output — there is no
collateral change anywhere else in the binary. Normalising away the variable renumbering
cascade, no changed function gains a net line.

The issue's smallest case, sub_44eb10:

// base
  std::__throw_length_error("basic_string::append"); // warn: funcboundflow: fall-through reached the next function entry; truncating flow here
// fixed
  std::__throw_length_error("basic_string::append"); // no-return

(The dead tail itself was already being cut here by funcboundflow/DIV-67, which landed
after the issue was filed — it papers over the symptom whenever the fall-through happens
to land on a function entry, with a warning comment. This PR supplies the actual fact, and
covers the far more common case where the fall-through lands on an unreachable trap or
mid-function code.)

Witness 2 — /usr/bin/apt-get (gcc, PIE, 124 functions; a different compiler and a
different set of family members: __throw_logic_error, __throw_bad_cast,
__throw_out_of_range_fmt, __throw_length_error): 6 changed, all shrink,
annotations 11 → 17, lines 2,493 → 2,469. Two of the removals are outright nonsense
disappearing:

// sub_7890, base: the fall-through cascaded through three adjacent throw stubs
      std::__throw_length_error("basic_string::append");
label_9355:
      std::__throw_length_error("basic_string::append");
      std::__throw_length_error("basic_string::append");
      __stack_chk_fail();
// fixed
      std::__throw_length_error("basic_string::append"); // no-return

// sub_6480, base: the fall-through ran into the exception-cleanup landing pad
      __cxa_begin_catch(std::__throw_logic_error("basic_string::_M_construct null not valid"));
      operator delete(v5,0x60);
      v15 = __cxa_rethrow();
      __cxa_end_catch();
      _Unwind_Resume(v15); // no-return
// fixed
      std::__throw_logic_error("basic_string::_M_construct null not valid"); // no-return

Witness 3 — /usr/bin/clang-format-14 (242 functions): 13 changed, all shrink,
annotations 12 → 16, lines 6,867 → 6,834.

Collateral

Whole-binary base-vs-branch decompile-all --json, every changed function reviewed:

binary changed / total
tests/bug-repro/grep (C) 0 / 449
tests/bug-repro/sort (C) 0 / 490
tests/bug-repro/faillog (C) 0 / 69
tests/bug-repro/libselinux.so.1 (C) 0 / 502
/usr/bin/FileCheck-14 (C++) 72 / 1,155
/usr/bin/apt-get (C++) 6 / 124
/usr/bin/clang-format-14 (C++) 13 / 242

The C binaries are byte-identical, as they must be — no C ELF imports a std::__throw_*.

Three removals spot-checked against objdump:

  • FileCheck 0x40e5ab: call _ZSt20__throw_length_errorPKc@plt immediately followed by
    0x40e5b0: call __stack_chk_fail@plt — clang's unreachable trap, dead by construction.
  • FileCheck 0x44eb35: call _ZSt20__throw_length_errorPKc@plt, then the 15-byte alignment
    NOP, then 0x44eb40 which is the next function's entry.
  • FileCheck 0x43a7e3: call _ZSt20__throw_system_errori@plt — here the successor
    0x43a7e8 is an independent branch target (0x43a735: jne 43a7e8), and the fixed
    output correctly still emits it. Only the fall-through edge is cut, never reachable code.

Speed

Interleaved min-of-11 kuna decompile-all /usr/bin/FileCheck-14 --json, alternating
base/branch on the same box (siblings were building concurrently, so the run-to-run spread
is wide and the min is the signal):

min median mean
base 68.64 s 75.83 s 76.30 s
branch 66.01 s 72.94 s 74.19 s
delta -3.83% -3.81% -2.76%

A small win, and in the expected direction: 106 fewer emitted lines over 72 functions with
shorter CFGs is less work, not more. The cost side is 40 extra lines parsed out of the list
once per load.

Tests

  • decompiler/crates/kuna-console/tests/verify_cxx_throw_noreturn.rs — two-pass e2e over
    a new clang++-built fixture
    decompiler/crates/kuna-analysis/tests/fixtures/cxxthrow_noreturn_x86_64 (+.cpp
    source committed alongside): default = the throw terminates the function, both list
    consumers off = the dead * 7 / * 11 tail returns. Two family members
    (__throw_length_error, __throw_out_of_range) so the fix is not proven on one symbol.
    A tests/stages/*.xml case is not possible here: the no-return list is consumed by the
    analysis tier, which the XML <binaryimage> path does not run — this follows the
    verify_noreturn_disc.rs precedent instead, and xml.rs's corpus count stays at 207.
  • kuna-analysis loader::noreturn: div78_cxx_throw_family_present_both_spellings
    (13 mangled + 13 demangled spellings, the namespace guard, and the "must NOT match"
    set), cxx_throw_family_emits_plt_stub_address (the fixture's real PLT-stub addresses),
    and list_parses_to_exact_names_and_cxx_throw_wildcards (the ELF list's only wildcards
    are the DIV-78 mangled prefixes).
  • kuna-decomp kuna_noreturn_externmatch:
    matches_cxx_throw_family_demangled_and_mangled.

Gates (all four, local, on this branch)

  • make test → datatests: 675/675 assertions passed / PARITY OK (no re-pin — no
    datatest imports a libstdc++ throw helper)
  • make test-stages → datatests: 481/481 assertions passed / PARITY OK
  • make rust-test → green, 4,599 passed / 0 failed
  • make check-spec → check-spec OK (lenient mode)
  • kuna catalog --check → OK

Docs

  • docs/history.md — DIV-78 row (see the DIV-number caveat below).
  • docs/spec/01-program-prep.md §1.7 — the divergence in prose, plus a correction: the
    chapter said noreturn_extern differs from noreturn_externmatch "only in gate flag
    and name-resolution path", which is not true — it also has a different, frozen name set.
  • docs/missing-ghidra-analyses.md — the no-return row now names both divergence blocks.

DIV number: claimed DIV-78 (tail on this branch was DIV-77). If another PR claims
it first, the row and its three in-tree references (the data file comment, the spec prose,
missing-ghidra-analyses.md) need renumbering together.

Follow-up filed separately

#312 — the issue's secondary point, that noreturn_disc treating an
undecodable fall-through as no-return evidence is fragile (a decode bug can forge the
same signal, which is exactly what happened here), deliberately not bundled.

🤖 Generated with Claude Code

https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq

…V-78)

[AUTOMATED]

Every helper in libstdc++'s <bits/functexcept.h> (plus __throw_regex_error from
<bits/regex_error.h>) is declared __attribute__((__noreturn__)) and every body
ends in _GLIBCXX_THROW_OR_ABORT -- a throw, or abort() under -fno-exceptions --
so none of the 20 can return. Upstream Ghidra's ElfFunctionsThatDoNotReturn names
__cxa_throw and the terminate entry points but omits the whole family, and the
source attribute survives into no binary artifact: at the decompiler's boundary
std::__throw_length_error is an ordinary UND .dynsym import behind a PLT stub, so
nothing but the shipped list can prove the call cannot return. Flow followed the
fall-through and emitted whatever sat after the call -- clang's
`call __stack_chk_fail` unreachable-trap, the next function's entry, or the
exception-cleanup landing pad. The gap was masked until #272, when a failed
decode of clang's 15-byte alignment NOP stopped forging noreturn_disc evidence.

The 20 helpers are appended to the vendored list in place, fenced by a
`# (kuna divergence, DIV-78)` block, exactly as DIV-21's libc additions are:
tools/sync_upstream.py's vendored scope is tests/datatests/ plus
Ghidra/Processors/*/data/languages/, so Ghidra/Features/Base/data/ is never
synced and an in-place block cannot conflict. Each helper is listed twice --
the mangled .dynsym spelling the analysis-tier scan matches pre-demangle (a
trailing-* wildcard over the Itanium ZSt<len>__throw_<name> prefix, so a
signature change cannot age it out) and the demangled base name the flow-time
matcher sees. No new option: noreturn_known and noreturn_externmatch are both
default-on and both read this file.

Witnesses: FileCheck-14 (clang, 1,155 fns) 72 change, 0 grow, no-return
annotations 69 -> 102, lines 63,247 -> 63,141; apt-get (gcc, 124 fns) 6 change,
11 -> 17; clang-format-14 (242 fns) 13 change, 12 -> 16. Zero collateral on C
binaries (grep 0/449, sort 0/490, faillog 0/69, libselinux.so.1 0/502). Speed
min-of-11 68.64 s -> 66.01 s (-3.83%).

Fixes #273

Co-Authored-By: Claude Fable 5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01VBn8vgwoHqPFnnV4ZMApfq
@mahaloz
mahaloz merged commit 60e3713 into main Aug 17, 2026
9 checks passed
@mahaloz
mahaloz deleted the fix/i273-throw-noreturn branch August 17, 2026 00:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[AUTOMATED] std::__throw_* family missing from the ELF no-return list: dead code after libstdc++ throw helpers

1 participant