Skip to content

Listing/xref tier: read API (code-unit / function-model / xref queries) - #26

Merged
mahaloz merged 2 commits into
mainfrom
listing-tier-queryapi
Jun 24, 2026
Merged

mahaloz merged 2 commits into
mainfrom
listing-tier-queryapi

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Increment 30 — the read/query surface over the PR0-built Listing
(docs/listing-tier-design.md §6 consumer API + §7 PR3 and PR4, combined
into one increment because both finalize read queries over the already-built model
and would self-conflict on listing/mod.rs).

Purely additive read API over a dormant module: no engine path touched, still
no --option flag (PR1) and no engine invocation (PR2). The PR0 walk already fills
the insns / refs_to / refs_from / funcs maps + covered / exec_ranges;
this PR locks the public read API + ordering/dedup on top. Signatures match §6
verbatim so the future discovered-no-return consumer transliterates cleanly.

Query surface added (on Listing)

Code-unit partition (PR3) — the partition the walk leaves behind
(partition_code_units):

  • code_unit_at(vma) -> CodeUnit — start-or-interior of a decoded insn ⇒
    Instruction(start) (ordered interior lookup insns.range(..=vma).next_back() +
    an [addr, addr+len) span test); any other exec-range VMA ⇒ Undefined;
    outside the exec ranges ⇒ Data (the conservative split; the keystone has no
    per-byte symbol-typed-data model).
  • is_data, is_undefined, num_instructions, first_undefined_after(vma) (the
    AIF gap walk — forward scan skipping each instruction's byte span).

Ordered function model (PR3):

  • function_containing(vma) — funcs.range(..=vma).next_back() (nearest preceding
    entry; the faithful analog of Ghidra's ordered FunctionManager).
  • function_at, next_function_after(vma) (range(vma+1..).next()),
    function_count.

Xref read API (PR4) over the bidirectional multimap:

  • refs_to, refs_from, ref_source_iter() (distinct reference sources,
    ascending — the call-site worklist), has_refs_to, ref_count_to.
  • Ordering/dedup locked at build (finalize_refs): each bucket is sorted
    (refs_to by source VMA then kind, refs_from by target VMA then kind) and
    de-duplicated on (from, to, kind), so a target referenced twice from one site
    contributes one edge and ref_count_to equals the number of distinct sites.
    exec_ranges is sorted at build for the partition/gap scan.

Sample query results on the fixture (fauxware, x86-64)

The new verify_listing_queries.rs reuses the PR0 bootstrap, seeds
collect_entries ∪ {main, __libc_csu_init}, builds the Listing, and recovers
185 instructions / 16 functions / 169 ref sources:

code_unit_at(main 0x40071d)            = Instruction(0x40071d)
code_unit_at(0x40071f)                 = Instruction(0x40071e)   # interior of mov %rsp,%rbp
is_data(0x400000)                      = true                    # non-exec image base
function_containing(0x40073e)          = main (0x40071d)
next_function_after(main 0x40071d)     = __libc_csu_init (0x4007e0)
refs_to(authenticate 0x400664)         = [0x4007ae Call]
ref_count_to(authenticate) (Call)      = 1                       # the sole call site
refs_from(0x4007ae)                    = [-> 0x400664 Call]
refs_from(ret 0x4007d4)                = []                      # terminal, no outgoing edge

Testcase

decompiler/crates/kuna-console/tests/verify_listing_queries.rs (sibling of the
PR0 verify_listing_core.rs) asserts §7 PR3 + PR4: the partition (instruction
start + interior byte ⇒ Instruction; non-exec ⇒ Data; first_undefined_after
lands on a genuine Undefined byte), the ordered function model
(function_containing(0x40073e)=main, next_function_after(main)=0x4007e0,
function_count==functions().count()), and the xref API
(refs_to(authenticate)=[0x4007ae Call], ref_source_iter includes & is ascending,
ref_count_to=1, refs_from carries the outgoing Call, terminal ret files none).
The test RAN (not skipped) against the built x86-64.sla.

Gates

  • make test — 675/675 PARITY OK
  • make test-stages — 158/158 PARITY OK
  • make rust-test — green (incl. verify_listing_queries + verify_listing_core)

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 24, 2026 20:44
Increment 30 — the read/query surface over the PR0-built Listing
(design listing-tier-design.md §6 + §7 PR3 + PR4, combined into one
increment since both finalize read queries over the already-built model
and would self-conflict on listing/mod.rs).

Code-unit partition (PR3): code_unit_at (Instruction start-or-interior /
Undefined in exec ranges / Data outside), is_data, is_undefined,
first_undefined_after, num_instructions.

Ordered function model (PR3): function_containing (range(..=vma).next_back()),
function_at, next_function_after, function_count.

Xref read API (PR4): refs_to / refs_from / ref_source_iter / has_refs_to /
ref_count_to over the bidirectional multimap; ordering+dedup locked at build
(finalize_refs sorts each bucket and de-dups on (from,to,kind)). Signatures
match §6 verbatim so the future discovered-no-return consumer transliterates.

Additive read API over a dormant module: no engine path, no flag, no
AnalysisCtx change. New verify_listing_queries.rs reuses the PR0 fauxware
bootstrap and asserts the partition / ordered-function / xref behaviors;
it RUNS (not skipped) against the built x86-64.sla.

Gates: make test 675/675 PARITY OK; make test-stages 158/158 PARITY OK;
make rust-test green (incl. the new test).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit 28510ec into main Jun 24, 2026
@mahaloz
mahaloz deleted the listing-tier-queryapi branch July 29, 2026 08:01
mahaloz added a commit that referenced this pull request Sep 23, 2026
…ending)

Union #26 (union_datatype.xml) pins upstream's `res = (int4)ptr->b->bCharField;`
and opts out with `option castimplied off`. Ten stage assertions of other
options pinned a widening this option leaves out and now match the new form:
undefname #4, charbyte #1 #2 #4 #7 #8, PEBNAMES #18, ptrfromuse #10,
TIEDPHITRIM #1 #2. docs/baseline.json and docs/baseline-stages.json unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Q4qgBGM28rgY6CEh5pird
mahaloz added a commit that referenced this pull request Sep 23, 2026
…ending)

Union #26 (union_datatype.xml) pins upstream's `res = (int4)ptr->b->bCharField;`
and opts out with `option castimplied off`. Ten stage assertions of other
options pinned a widening this option leaves out and now match the new form:
undefname #4, charbyte #1 #2 #4 #7 #8, PEBNAMES #18, ptrfromuse #10,
TIEDPHITRIM #1 #2. docs/baseline.json and docs/baseline-stages.json unchanged.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Q4qgBGM28rgY6CEh5pird
mahaloz added a commit that referenced this pull request Sep 23, 2026
…d) (#721)

kuna printed a cast where C's own conversion already performs it: a value
widened into a type-locked parameter of that type, into a variable declared
with it, out through `return`, or under another integer widening. The new
option `castimplied` (P9 cast-policy, default on) leaves such a cast out at
print time; the IR and CastStrategyC are unchanged. Only a value-preserving
integer conversion goes, judged on the operand's printed C type; truncations,
sign changes, float/pointer/bool/enum conversions, varargs slots and
truncarg/boolbyte casts always stay. Rust output is unaffected.

On the landing base: casts on the 4,815 functions kuna and IDA both emit go
45,039 -> 43,673 (1.191x -> 1.155x IDA), 570 functions fewer, 0 more; every
changed line in the 45-binary corpus is the old line with integer casts
deleted (3,360 casts). The 444-slice typesweep stays at 1,615 perfect with
variables[] identical in all 10,748 functions. A gcc/clang round-trip test
compiles the printed C with the option off and on and requires the binary's
output. Union #26 (upstream datatest) opts out; ten stage assertions of other
options now match the new form.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_011Q4qgBGM28rgY6CEh5pird
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 14, 2026
…t, a wide reload or ECX, and from reusing walks across the option

Three lost-read shapes the stack-address tracking still missed each dropped a
real argument: a local's address stored over the callee's own argument slot
and loaded back, a slot holding one copied by an 8-byte move, and a stack
address a nested call leaves in ECX. A store of a stack address to a stack
location whose slot is not followed now leaves the extent unstated, a load is
lost when it overlaps any followed slot, and every caller-saved register a
returning callee leaves a stack address in is lost.

The walk summaries are cached on the architecture, and the tracking only runs
with calleeargs on, so turning the option on in a session that had walked a
callee with it off reused the untracked walk and dropped the arguments again.
The cache now drops its walks when the option changes.

Stage cases CALLEEARGS Noelo-Lab#26-Noelo-Lab#29 (calleelost_pe_i386 extended, recipe in its
header) fail on the previous head and pass now; the 62-binary i386 corpus is
byte-identical to the previous head.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
mahaloz added a commit to borancar/kuna that referenced this pull request Nov 14, 2026
…ious call

The vtable's object was followed back through copies past intervening
calls, so `mov esi,ecx; call f; mov eax,[esi]; push esi; call [eax+k]`
(COM calls through ESI, and a C table through ESI) took ECX as the
receiver although the earlier call clobbered it; the COM push check also
compared two scans of different reach and missed the pushed interface.
The object, the last push and EAX are now compared as values over the run
of ops since the previous call, carried forward through copies. The
vcall fixture gains comesi and crosscall (stage Noelo-Lab#26, Noelo-Lab#27, the custom test).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQeLQDThvbHGNoGw8h
mahaloz added a commit that referenced this pull request Nov 22, 2026
libpng's png_decompress_chunk (Ultra, plain VMProtect and Themida builds)
lost `if (*a3 != size) ret = -7` and `if (ret == 1 && a1 - a2 != avail)
png_chunk_benign_error(...)` after the second png_inflate call, with no
warning. png_inflate gets &avail and the pointer parameter a3 in its 5th and
7th arguments, which Windows x86-64 passes on the stack.

Exploration: the call summary looked at no stack-passed argument on x86-64
(eight words on x86) and made only the 16 bytes behind each pointer argument
unknown, so both reads after the call folded to the values before it and the
paths to -7 and to png_chunk_benign_error were pruned (the overlay had 23
CBRANCH and no call to it). Now the argument registers and the 16 words of
the outgoing argument area (past the home space on Win64) are the pointers;
every frame offset among them escapes, and a round that escapes a new one is
followed by another. State::clobber_call leaves the call's output in all
mutable image memory, in every region under a symbolic base but the entry
stack pointer, and in the frame from the lowest escaped local up to the
return address (and from the lowest escaped incoming argument up), the
same bytes the emitter keeps alive across a call. CallOutputAt carries the
base.

Decompiler: with the paths back (25 CBRANCH, the call present), the
benign-error test still folded. The store that passed &avail has no reader
in the body and is gone before the stack alias mapping, which then marked
avail `nolocalalias`, and RuleIndirectCollapse removed the call's INDIRECT
on it. The exploration's escaped offsets are now kept on the Funcdata
(kuna_call_escapes), and syncVarnodesWithSymbols never gives a slot from the
lowest escaped local up to the return address `nolocalalias`
(kuna_pushan::frame_escaped).

Fixture: pushan_vm2_x86_64 gets ext7 (writes through its stack-passed 7th
argument and 32 bytes past its pointer argument) and f_out; stage
kuna-pushan-sound #26/#27 pin both tests (before: `ext7(); return 3;`).
Stage baseline 5035 -> 5037.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mahaloz added a commit that referenced this pull request Nov 22, 2026
[AUTOMATED] Re-recorded after adding PUSHAN-SOUND #26 and #27 and merging main.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.

1 participant