Skip to content

fix(cli): report each function entry once on a whole-binary run - #198

Merged
mahaloz merged 1 commit into
mainfrom
fix/dedupe-function-entries
Jul 30, 2026
Merged

mahaloz merged 1 commit into
mainfrom
fix/dedupe-function-entries

Conversation

@mahaloz

@mahaloz mahaloz commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes the whole-binary duplication reported in #197. Closes #197.

A whole-binary run now reports — and decompiles — each function entry exactly once.

The gap

arm_thumb_linked_le32 (in-repo fixture) defines exactly two functions. decompile-all
reported six:

 BEFORE                                        AFTER
 compute    @ 0x100b8                          compute  @ 0x100b8  aliases=["sub_100b8"]
 sub_100b8  @ 0x100b8   <- alias               _start   @ 0x100d6  aliases=["sub_100d6"]
 sub_100b9  @ 0x100b9   <- Thumb entry|1
 _start     @ 0x100d6
 sub_100d6  @ 0x100d6   <- alias
 sub_100d7  @ 0x100d7   <- Thumb entry|1

The issue described the duplicates as byte-identical and therefore only wasted work. That
holds for the same-address aliases, but not for the ARM twins — those are wrong content.
decompile-project exported this .c, which has both a duplicate definition and two
functions that do not exist (0x100b9 is mid-push {r7}, not an entry):

int4 compute(int4 a0)   { return a0 * 3 + 7; }
int4 sub_100b8(int4 a0) { return a0 * 3 + 7; }   // duplicate definition — will not compile
void sub_100b9(void)    { return; }              // phantom

Root cause

Three producers, all upstream of one enumeration (ConsoleProgram::function_entries, a flat
Vec<ProgramSymbol>):

  1. A generated alias at the same address. In commit_analysis_output the symbol-table
    insert is guarded (if find_function(scope,&addr).is_none() { add_function(...) }) but the
    following register_symbol(&name, addr) runs unconditionally, and register_symbol
    retains by name. An already-named entry therefore gains a second record under the
    generated sub_<addr>. Every FID/PDB/objc rename leaves its old placeholder behind the
    same way.
  2. The ARM/Thumb entry|1 twin. existing_function_addrs recorded the raw
    sym.address(). On 32-bit ARM the Thumb mode bit lives in st_value — readelf on
    the fixture shows compute at 0x100b9 and _start at 0x100d7, while objdump shows
    both functions starting at the even VMA. Those odd addresses became Listing seeds and were
    re-emitted by funcdisc_recursive as discovered entries. The same defect broke the
    funcsym-skip: PR feat(analysis): stripped ARM Cortex-M Thumb function discovery #168 masks the e_entry candidate, but compared it against this
    unmasked set, so 0x100d6 never matched the recorded 0x100d7 and _start was
    re-emitted as "new" — which is why kuna functions duplicated even with the Listing off.
  3. A second, genuinely different name for one function — a debug-info pass emits a name
    the linker symbol table lacks (macho_dwarf.o carries _l0+first_byte at 0x0,
    _main+main at 0x40). This one is information, not noise.

Two corrections to the issue's analysis, both measured:

  • It is not limited to non-x86-64. The x86-64 PE pe_dwarf.exe duplicates through
    producer 1 via the DWARF pass (first_byte + sub_140001550), which the
    funcstart_patterns architecture gate cannot explain. RISC-V also duplicates
    (fmt_riscv64: 17 records for 9 functions).
  • Producer 3 is not loader-side. The loader funcsym stream is address-deduped as it is
    read, so it cannot emit two names at one address; the pair arrives from the analysis tier.

What this does

  • ConsoleProgram::function_entries_canonical (new, kuna-console/src/engine.rs) — one
    record per entry address, address-ordered, keeping the most informative name and carrying
    the rest as aliases. Producers 1 and 3 collapse by address; the ARM twin needs the Thumb
    fold in the grouping key, the same vma & !1 on an ARM-family spec that build_asm already
    applies to its labels.
  • thumb_masked (new, kuna-analysis/.../entry/mod.rs) — folds the mode bit out of the
    symbol-address stream, killing producer 2 at the source (no phantom is ever seeded, decoded
    or decompiled) and repairing the PR feat(analysis): stripped ARM Cortex-M Thumb function discovery #168 mask/skip asymmetry. Hard-gated to
    object::Architecture::Arm.
  • Name preference (entry_name_rank): a real symbol beats a synthesized dynamic-table
    name (_INIT_<i>/_FINI_<i>/_DT_INIT/_DT_FINI), which beats a generated
    sub_/func_/FUN_/LAB_ placeholder; ties prefer the unprefixed spelling (main over
    _main, first_byte over _l0), then the shorter name, then lexicographic — total and
    independent of symbol-stream order.
  • Nothing stops resolving. find_entry_by_name searches aliases too, so
    --functions sub_100b8 still selects compute — the lookup the existing "preserving
    distinct alias names ... for the decbench name-narrowing" comment was keeping duplicate
    records for.
  • All four surfaces share it: decompile-all, functions, decompile-project, and
    kuna-wasm (whose hand-copied resolve_targets is the drift risk — the grouping rule and
    the alias lookup now both live in kuna-console).
  • aliases is additive in every JSON document, immediately after address_hex, always
    present ([] when the entry has one name).

Beyond the issue's stated scope (flagged for review — drop it if unwanted): --addr now
tolerates an odd ARM address. --addr 0x100b9 used to decompile literally there and return
an empty void compute(void) { return; }; it now resolves to the real entry. Deliberately
conservative — it only folds onto an address the enumeration already knows, and it is ARM-only,
so a genuinely odd x86-64 entry (cet_pie_x86_64 elaborate_debug_symbol @ 0x1357) is
untouched. This matters because decbench drives --addr with case addresses.

Policy

No --option. Emitting one function several times — as duplicate definitions in an exported
.c, or as an entry that is not a function at all — is wrong output, not a judgment call, so
per CLAUDE.md ("a strict bug fix that only corrects wrong output needs no flag") it ships
unflagged; there is also no driver-tier option machinery to hang it on (all 76 phases.toml
settables carry an engine phase). Recorded as DIV-32. No catalog counts, no
docs/options.md regen, no baseline re-pin.

Deliberately not done: suppressing the placeholder at the producer. It would delete the
alias data #197 asks to preserve, and with the collapse in place it saves no work.

Blast radius (measured)

decompile-all, before → after (count / distinct addresses):

fixture before after note
arm_thumb_linked_le32 6 / 4 2 / 2 2 real functions
fmt_arm 32 / 20 14 / 14 6 odd phantoms + 12 aliases removed
fmt_aarch64 28 / 14 14 / 14 pure alias collapse
fmt_riscv64 17 / 9 9 / 9 pure alias collapse
fmt_x86_64 11 / 11 11 / 11 unchanged
fauxware 21 / 21 21 / 21 unchanged
cet_pie_x86_64 21 / 21 21 / 21 unchanged
libselinux.so.1 621 / 621 621 / 621 unchanged

Every address that disappeared is an odd ARM Thumb phantom (0x100b9, 0x100d7, 0x409,
0x461, 0x48d, 0x4c1, 0x501, 0x505); zero addresses gained. Across all of the
above, 713/713 surviving functions decompile to byte-identical code, and no reported name is
one that did not already exist at that address.
x86-64 keeps its genuinely-odd entries
(fauxware 0x40071d, cet_pie 0x1357), confirming the ARM gate.

That last check is what caught the one real regression during development: an early "shorter
name wins" tie-break silently renamed __do_global_dtors_aux → _FINI_0 and frame_dummy →
_INIT_0. No test asserted on either; the code diff did. Hence the structural-name rank.

Verification

  • make test — 675/675, PARITY OK
  • make test-stages — 261/261, PARITY OK
  • make rust-test — green, 4308 passed / 0 failed (4304 before, +4 new)
  • make check-spec — OK; kuna catalog --check — OK
  • Testcases: kuna-cli/tests/decompile_all_cli.rs — entry-dedup, alias lookup, and the
    ARM---addr fold with an x86-64 negative control; kuna-console/tests/verify_decompile_all.rs
    — the shared-seam invariant on both an x86-64 ELF and the ARM fixture (this is also the only
    coverage kuna-wasm gets, having no tests/ dir of its own).
  • One existing test changed fixture: arm_decompile_all_defaults_funcstart_patterns_on
    asserted default > funcstart_patterns off on the two-function ARM fixture, where the
    "extra" entries it counted were the duplicates. It now runs on entrymain_arm, where the
    pass finds three functions nothing else does (0x3e0, 0x410, 0x3c520): 10 vs 13. The
    assertion is meaningful again rather than passing on noise.
  • Web gates checked by hand against the native kuna_wasm (glue.mjs assertions sum_to,
    ≥3 bodied, _compute on sample_macho.o, proj.count > 0 — all hold; those fixtures have
    no alias groups).
  • Spec: docs/spec/00-overview.md §0.1–§0.2 (the §0.1 idempotence sentence was actively
    false — it claimed every commit arm is idempotent against the loader symbols, which holds
    for the symbol table but not for the name-keyed register_symbol stream) and
    docs/spec/01-program-prep.md §1.5. Docs: docs/cli.md, docs/web-integration.md,
    docs/history.md (DIV-32).

🤖 Generated with Claude Code

https://claude.ai/code/session_01279bXJ6UBQdhfHE4tUQTKg

A whole-binary run reported the same function several times -- once per name
the entry carried, and on ARM once more per odd-address Thumb twin. The
in-repo arm_thumb_linked_le32 fixture holds exactly two functions and
decompile-all emitted six records for them; decompile-project exported a .c
with a duplicate definition and two functions that do not exist.

Three producers, all upstream of one enumeration:

1. commit_analysis_output guards the symbol-table insert with find_function
   but calls register_symbol unconditionally, and register_symbol retains by
   NAME -- so an already-named entry gains a second record under the
   generated sub_<addr>. FID/PDB/objc renames leave their old placeholder
   behind the same way.

2. existing_function_addrs recorded the raw sym.address(). On 32-bit ARM the
   Thumb mode bit lives IN st_value (the fixture records compute at 0x100b9,
   _start at 0x100d7, while both functions start at the even VMA), so the odd
   address was seeded as a function start and re-emitted by
   funcdisc_recursive. 0x100b9 is not an instruction boundary, so it
   decompiled to a bogus empty body. The same defect broke the funcsym-skip:
   PR #168 masks the e_entry candidate but compared it against this unmasked
   set, which is why kuna functions duplicated even with the Listing off.

3. A debug-info pass emits a name the linker symbol table lacks
   (macho_dwarf.o carries _l0+first_byte at 0x0). That one is information.

ConsoleProgram::function_entries_canonical now yields one record per entry
address, keeping the most informative name and carrying the rest as aliases;
thumb_masked folds the mode bit out of the symbol stream at the source, so no
phantom is seeded, decoded or decompiled. Name preference ranks a real symbol
above a synthesized _INIT_<i>/_FINI_<i>/_DT_INIT/_DT_FINI table name, which
outranks a sub_/func_/FUN_/LAB_ placeholder; ties prefer the unprefixed
spelling, then the shorter name, then lexicographic order. find_entry_by_name
searches aliases, so --functions sub_100b8 still selects compute. All four
surfaces share it -- decompile-all, functions, decompile-project and
kuna-wasm. The aliases array is additive in every JSON document.

--addr additionally tolerates an odd ARM address, folding onto the entry only
when the enumeration already knows it; a genuinely odd x86-64 entry is
untouched.

No --option: emitting one function several times is wrong output, not a
judgment call, and there is no driver-tier option machinery. Recorded as
DIV-32. No catalog counts, no options.md regen, no baseline re-pin.

Measured: arm_thumb_linked_le32 6->2 entries, fmt_arm 32->14,
fmt_aarch64 28->14, fmt_riscv64 17->9; x86-64 and libselinux.so.1 (621)
unchanged. Every address removed is an odd ARM phantom, none gained, and
713/713 surviving functions decompile to byte-identical code with no reported
name that did not already exist at that address.

arm_decompile_all_defaults_funcstart_patterns_on moves to entrymain_arm: it
asserted default > funcstart_patterns off on the two-function ARM fixture,
where the extra entries it counted WERE the duplicates. On entrymain_arm the
pass finds three functions nothing else does (10 vs 13), so the assertion is
meaningful again.

Fixes #197.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01279bXJ6UBQdhfHE4tUQTKg
@mahaloz
mahaloz merged commit a9e72a9 into main Jul 30, 2026
7 checks passed
@mahaloz
mahaloz deleted the fix/dedupe-function-entries branch July 30, 2026 19:46
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.

Whole-binary runs decompiles one function once per alias name, not per address, generating duplicates

1 participant