Repository navigation
fix(cli): report each function entry once on a whole-binary run - #198
Merged
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-allreported six:
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-projectexported this.c, which has both a duplicate definition and twofunctions that do not exist (0x100b9 is mid-
push {r7}, not an entry):Root cause
Three producers, all upstream of one enumeration (
ConsoleProgram::function_entries, a flatVec<ProgramSymbol>):commit_analysis_outputthe symbol-tableinsert is guarded (
if find_function(scope,&addr).is_none() { add_function(...) }) but thefollowing
register_symbol(&name, addr)runs unconditionally, andregister_symbolretains 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 thesame way.
entry|1twin.existing_function_addrsrecorded the rawsym.address(). On 32-bit ARM the Thumb mode bit lives inst_value—readelfonthe fixture shows
computeat0x100b9and_startat0x100d7, whileobjdumpshowsboth functions starting at the even VMA. Those odd addresses became Listing seeds and were
re-emitted by
funcdisc_recursiveas discovered entries. The same defect broke thefuncsym-skip: PR feat(analysis): stripped ARM Cortex-M Thumb function discovery #168 masks the
e_entrycandidate, but compared it against thisunmasked set, so
0x100d6never matched the recorded0x100d7and_startwasre-emitted as "new" — which is why
kuna functionsduplicated even with the Listing off.the linker symbol table lacks (
macho_dwarf.ocarries_l0+first_byteat0x0,_main+mainat0x40). This one is information, not noise.Two corrections to the issue's analysis, both measured:
pe_dwarf.exeduplicates throughproducer 1 via the DWARF pass (
first_byte+sub_140001550), which thefuncstart_patternsarchitecture gate cannot explain. RISC-V also duplicates(
fmt_riscv64: 17 records for 9 functions).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) — onerecord 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 Thumbfold in the grouping key, the same
vma & !1on an ARM-family spec thatbuild_asmalreadyapplies to its labels.
thumb_masked(new,kuna-analysis/.../entry/mod.rs) — folds the mode bit out of thesymbol-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.entry_name_rank): a real symbol beats a synthesized dynamic-tablename (
_INIT_<i>/_FINI_<i>/_DT_INIT/_DT_FINI), which beats a generatedsub_/func_/FUN_/LAB_placeholder; ties prefer the unprefixed spelling (mainover_main,first_byteover_l0), then the shorter name, then lexicographic — total andindependent of symbol-stream order.
find_entry_by_namesearches aliases too, so--functions sub_100b8still selectscompute— the lookup the existing "preservingdistinct alias names ... for the decbench name-narrowing" comment was keeping duplicate
records for.
decompile-all,functions,decompile-project, andkuna-wasm(whose hand-copiedresolve_targetsis the drift risk — the grouping rule andthe alias lookup now both live in
kuna-console).aliasesis additive in every JSON document, immediately afteraddress_hex, alwayspresent (
[]when the entry has one name).Beyond the issue's stated scope (flagged for review — drop it if unwanted):
--addrnowtolerates an odd ARM address.
--addr 0x100b9used to decompile literally there and returnan empty
void compute(void) { return; }; it now resolves to the real entry. Deliberatelyconservative — 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_64elaborate_debug_symbol@0x1357) isuntouched. This matters because decbench drives
--addrwith 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, soper
CLAUDE.md("a strict bug fix that only corrects wrong output needs no flag") it shipsunflagged; there is also no driver-tier option machinery to hang it on (all 76
phases.tomlsettables carry an engine phase). Recorded as DIV-32. No catalog counts, no
docs/options.mdregen, 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):arm_thumb_linked_le32fmt_armfmt_aarch64fmt_riscv64fmt_x86_64fauxwarecet_pie_x86_64libselinux.so.1Every address that disappeared is an odd ARM Thumb phantom (
0x100b9,0x100d7,0x409,0x461,0x48d,0x4c1,0x501,0x505); zero addresses gained. Across all of theabove, 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
(
fauxware0x40071d,cet_pie0x1357), 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_0andframe_dummy→_INIT_0. No test asserted on either; the code diff did. Hence the structural-name rank.Verification
make test— 675/675, PARITY OKmake test-stages— 261/261, PARITY OKmake rust-test— green, 4308 passed / 0 failed (4304 before, +4 new)make check-spec— OK;kuna catalog --check— OKkuna-cli/tests/decompile_all_cli.rs— entry-dedup, alias lookup, and theARM-
--addrfold 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-wasmgets, having notests/dir of its own).arm_decompile_all_defaults_funcstart_patterns_onasserted
default > funcstart_patterns offon the two-function ARM fixture, where the"extra" entries it counted were the duplicates. It now runs on
entrymain_arm, where thepass finds three functions nothing else does (
0x3e0,0x410,0x3c520): 10 vs 13. Theassertion is meaningful again rather than passing on noise.
kuna_wasm(glue.mjsassertionssum_to,≥3 bodied,
_computeonsample_macho.o,proj.count > 0— all hold; those fixtures haveno alias groups).
docs/spec/00-overview.md§0.1–§0.2 (the §0.1 idempotence sentence was activelyfalse — it claimed every commit arm is idempotent against the loader symbols, which holds
for the symbol table but not for the name-keyed
register_symbolstream) anddocs/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