Skip to content

Listing/xref tier (scope-B) PR0: recursive-descent disassembly core + design doc - #24

Merged
mahaloz merged 1 commit into
mainfrom
listing-tier-core
Jun 24, 2026
Merged

mahaloz merged 1 commit into
mainfrom
listing-tier-core

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

What

Lands PR0 of the Listing/xref tier ("scope-B") — the keystone of a post-disassembly subsystem in kuna-analysis: a program-wide recursive-descent disassembler plus an instruction / cross-reference / discovered-function model, built by reusing the ported SLEIGH decoder and a lifted copy of the S2 flow classifier. This is the foundation the rest of the tier's PRs (the --option listing flag, the engine build wiring, the CodeUnit partition, the FindNoReturnFunctionsAnalyzer consumer) build on.

The design doc

docs/listing-tier-design.md is the canonical spec for the whole tier (§1 module layout, §2 data model, §3 the recursive-descent algorithm, §4 decoder reuse + the flow classifier, §5 the flag + parity-safety argument, §6 consumer API, §7 the ordered PR breakdown, §8 deferrals). It is transcribed faithfully from the design fan-out.

The subsystem (kuna-analysis/src/listing/)

  • model.rs — Insn (addr/len/fall_through/flow/flows/mnemonic/lazy-pcode), FlowType (the faithful FlowType predicate set) + FlowKind, RawOp, Reference/RefKind (Call/Code populated; Data/Read/Write reserved), DiscoveredFunction (ordered BTreeMap model), CodeUnit.
  • decode.rs::decode_one — drives Translate::one_instruction with a capturing PcodeEmit for (len, ops) and print_assembly with a capturing AssemblyEmit for the mnemonic; builds the Address in the default code space.
  • classify.rs::classify — a faithful transliteration of s2_lift/flow.rs::xref_control_flow (flow.rs:1039-1185), honoring the three gotchas: constant-space in0 is p-code-relative (not a VMA), fall-through is the last-op test, delay slots are already folded into len. BRANCHIND/CALLIND ⇒ computed/indirect with no static target (deferred jump-table resolution).
  • walk.rs — the two-level recursive-descent (function worklist + per-function instruction worklist), BTreeMap-membership dedup, exec-range bounds gate, decode-error = stop-this-path, CALL targets → new function entries + RefKind::Call edges, branch/fall-through → same-function successors + RefKind::Code edges.
  • mod.rs — the Listing facade + Listing::build(file, image, arch, translate, seeds) and the read-only query API.

The s1_entry seed helpers (executable_sections/existing_function_addrs/in_executable_section) are promoted to pub(crate) so PR2 can build the seed set.

Scope discipline (PR0 ONLY)

The module is dormant: no --option listing flag (PR1), no AnalysisCtx::listing / driver build (PR1-PR2), no context.rs ARM/MIPS decode-paint (PR5). It touches no engine path the XML datatests use — so the 675/675 + 158/158 oracles are structurally untouched.

Demonstration — what Listing::build recovers on the fixture

The real-decode integration test (kuna-console/tests/verify_listing_core.rs) bootstraps the vendored x86-64 fauxware ELF, obtains a real Translate (prog.arch().translate()) + code space (prog.arch().manage()), seeds with collect_entries ∪ {main}, and drives Listing::build end-to-end:

decoded 108 instructions, discovered 11 functions, 16 call edges (seeds = 2)
  0x40071d: PUSH     kind=Fallthrough          flows=[]          fall=0x40071e
  0x4007ae: CALL     kind=Call                 flows=[0x400664]  fall=0x4007b3   (-> authenticate)
  0x4007bb: JZ       kind=ConditionalBranch    flows=[0x4007c9]  fall=0x4007bd   (two successors)
  0x4007d4: RET      kind=Return               flows=[]          fall=None       (terminal)

From 2 entry seeds the walk discovered the rest of the call graph (11 functions, 16 call edges) by following CALL targets. The je@0x4007bb yields two successors + ConditionalBranch; the ret is terminal; the call@0x4007ae files a RefKind::Call edge to authenticate@0x400664 and seeds it as a function.

Tests (both layers REAL, not skipped)

  • 9 classifier unit tests (classify.rs) pin classify against the flow.rs rules: CBRANCH (two successors), unconditional BRANCH (no fall-through), CALL (target + falls through), RETURN (terminal), BRANCHIND (computed, no target), CALLIND (computed, falls through), a constant-space BRANCH in0 (intra-insn, NOT a target), and the last-op fall-through rule.
  • verify_listing_core.rs — the end-to-end drive above. Ran (not skipped) — x86-64.sla is built.

Gates

  • make test — 675/675 PARITY OK
  • make test-stages — 158/158 PARITY OK
  • make rust-test — green (incl. the 9 classifier tests + verify_listing_core)

Deviations from the plan

  • The integration test lives in kuna-console/tests/ (a kuna-console integration test, exactly as the task suggested) — that is the realistic place to obtain a built Translate. It is reached via prog.arch().translate() (a &Sleigh: Translate) + prog.arch().manage().get_default_code_space(), mirroring the verify_*.rs bootstrap pattern. The keystone's image parameter is part of the contract but not read by the PR0 decode path (the decode drives translate, which reads through the engine loader the bootstrap already attached), so the test threads a throwaway ObjectLoadImage over the same bytes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

… design doc

Land the keystone of the post-disassembly Listing/xref tier: a program-wide
recursive-descent disassembler + an instruction / cross-reference /
discovered-function model, reusing the ported SLEIGH decoder and a lifted copy
of the S2 flow classifier. PR0 only — the module is dormant (no engine
invocation, no --option flag, no AnalysisCtx change), so all three parity gates
are structurally untouched.

- docs/listing-tier-design.md: the canonical tier spec (transcribed from the
  design fan-out).
- kuna-analysis/src/listing/{model,decode,classify,walk,mod}.rs: the §2 data
  model (Insn/FlowType/FlowKind/RawOp/Reference/RefKind/DiscoveredFunction/
  CodeUnit), decode_one (Translate::one_instruction + print_assembly capture),
  classify (faithful xref_control_flow transliteration, flow.rs:1039-1185, with
  the constant-space / last-op / delay-slot gotchas), the two-level
  recursive-descent walk, and the Listing facade + Listing::build.
- s1_entry seed helpers (executable_sections/existing_function_addrs/
  in_executable_section) promoted to pub(crate) for PR2's seed set.

Tests (both REAL, not skipped):
- 9 classifier unit tests pin classify against the flow.rs rules.
- kuna-console/tests/verify_listing_core.rs drives Listing::build end-to-end on
  the bootstrapped x86-64 fauxware fixture (Translate via prog.arch().translate()):
  recovered 108 instructions, 11 functions, 16 call edges from 2 seeds; the
  je/call/ret/push sites classify correctly. Ran (x86-64.sla built), not skipped.

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

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit cdf598d into main Jun 24, 2026
@mahaloz
mahaloz deleted the listing-tier-core branch July 29, 2026 08:01
mahaloz added a commit that referenced this pull request Oct 2, 2026
…'s earlier value is used after it

Keeping the store COPY at the binary's position while the global's earlier
value is still read after it made Merge copy that earlier value out where it
is defined (the entry, a call, a loop head), above a pointer store the binary
reads the global after.  The decline now checks for such a read first, through
the copies a later propagation can fold, and leaves the store to upstream.

Witnesses w_ohoist (gcc -O2), w_oif (gcc -O0, clang -O2) and w_oloop (gcc -O0)
added to the globalstore fixtures; stage assertions #22-#24.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Oct 2, 2026
…'s earlier value is used after it

Keeping the store COPY at the binary's position while the global's earlier
value is still read after it made Merge copy that earlier value out where it
is defined (the entry, a call, a loop head), above a pointer store the binary
reads the global after.  The decline now checks for such a read first, through
the copies a later propagation can fold, and leaves the store to upstream.

Witnesses w_ohoist (gcc -O2), w_oif (gcc -O0, clang -O2) and w_oloop (gcc -O0)
added to the globalstore fixtures; stage assertions #22-#24.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Ps66ZCFPLywq9AKHBuDXNU
mahaloz added a commit that referenced this pull request Nov 22, 2026
…t tail call off the entry depth

Themida (plain) computes every stack and VM-context address from the entry
stack pointer and other entry registers through mixed boolean-arithmetic
identities, with flags words of junk arithmetic mixed in and cancelled again.
The linear normal form and the one-variable three-point probe saw none of
them, so loads and stores went to unknown locations and the VM exit's return
target was lost (4 rows unresolved, 9 rows over budget).

- ExprArena::identity_probe decides `e == base + k` or `e == k` by
  evaluating the DAG at 96 fixed points (splitmix64, fixed seed) assigning
  every variable and every innermost boolean atom (as a free bit) at once:
  20 structured lane-edge points, 76 uniform; exhaustive at <= 8 open bits.
  affine_probe uses it for several variables and to confirm the
  one-variable answer when no atom is involved.  A per-node cache of 4
  quick points (ExprArena::quick_vals) rejects most values with no walk.
- Explorer::same_on_stack: two values whose difference is the constant 0
  by that test are no difference at a merge.
- ExprArena::decide_atoms: a guard, address or computed target has each
  atom an identity decides replaced (x == y with x - y constant; ordered
  compares of constants or of one pointer plus constants).
- Linear form: zext(b ^ 1) == 1 - zext(b) for a boolean b.
- PushanTarget::user_stack_clear_bits: a second identity run with the
  stack pointer's top bits clear (Windows x86 without
  IMAGE_FILE_LARGE_ADDRESS_AWARE: bit 31; Windows x64: bits 47-63); used
  answers are counted and the header says the function "assumes the stack
  lies where the platform puts a user-mode stack".
- Explorer::misplaced_tail_call: a tail call (to a function or through an
  import slot) is accepted only with the stack pointer at its entry value;
  otherwise the exit stays unresolved and the header counts it
  (newstar_backdoor_themida ended at WSAStartup without a warning).

Fixture pushan_vm2_x86_64: f_mba (arguments read through an MBA identity
over rbp, rdx and rbx; exit behind an opaque predicate on rsp mixed with the
result) and f_spbit (an exit on bit 16 of rsp whose other arm jumps to fin
with the VM frame pushed).  bin-t1-final2 prints garbage loads plus a bogus
`fin(...)` path for f_mba with no warning, and accepts f_spbit's arm as a
tail call; now `return a0 + a1;` and an incomplete header.  Stage
kuna-pushan-sound #19-#21 recounted, #24, #25 new; baseline-stages
re-recorded (4957).  Spec 02 2.11: identity test, platform stack, tail calls.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_0179iMKQR2hseBprKvbvZRUv
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