Skip to content

FID PR2: FNV-1a64 FidHashQuad hasher - #97

Merged
mahaloz merged 2 commits into
mainfrom
fid-hash-core
Jun 26, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fid-hash-core

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

The second PR of the FID (Function-ID fingerprinting) port — the pure hashing core. Mechanism only: no pass / DB / option / engine wiring (that is PR3/PR4). Because nothing here is reachable from a decompilation, this PR structurally cannot perturb the parity oracles. Builds on PR1's Sleigh::instruction_mask accessor (#94). Whole-feature plan: docs/fid-design.md.

The algorithm (byte-exact port of MessageDigestFidHasher / FNV1a64MessageDigest)

A one-bit divergence makes every hash miss silently, so the constants and the per-instruction loop order are reproduced verbatim. Three new pure modules under kuna-analysis/src/s1_fid/:

  • hash.rs — Fnv1a64 (offset basis 0xcbf29ce484222325, prime 0x100000001b3; update_byte/update_bytes; update_i32 = 4 bytes big-endian via AbstractMessageDigest.update(int); digest_long resets to basis) + FidHasher + FidHashQuad { code_unit_size:i16, full_hash:u64, specific_addl_size:u8, specific_hash:u64 }. FidHasher::hash ports the per-instruction loop line-for-line:
    • skippers run BEFORE the call count / operand hashing (code_unit_index -= 1; continue on skip — matching the Java order);
    • calls counted via is_call;
    • per operand: seed full_u = spec_u = (ii+1)*7777, then the 4-way scalar rule in order (relocation / whole-operand-address / large-non-whole / small-usable) — the full hash NEVER sees a scalar value, only the 0xfeeddead placeholder; registers mix (off+7654321)*98777 into both; address objects are value-suppressed placeholders into both; then full.update_i32(full_u); spec.update_i32(spec_u);
    • then the critical interleave: buf[i] &= fixed_mask[i] and feed the masked bytes to BOTH digests after that instruction's operand update_i32 writes.
    • Magic copied verbatim (0xfeeddead, 7777, 1234567, 67999, 7654321, 98777, 0xa5, 4, 127); wrapping_* everywhere (Java two's-complement overflow is observable, incl. the (int) val truncation); extents < 4 code units → None; empty/absent mask → Ghidra's NPE arm (fill 0xa5). The relocation check is consumed as a RelocationQuery trait (pure-core testable; PR4 supplies the elf_reloc.rs-backed adapter).
  • extent.rs — the FunctionBodyFunctionExtentGenerator analog: the address-contiguous [entry, next_function_after(entry)) clip over the flat instructions() map (the s1_noreturn_disc idiom). LOSS (documented): address-contiguous, not flow-reachable — same approximation s1_noreturn_disc accepts.
  • skipper.rs — InstructionSkipper trait + X86InstructionSkipper (17 NOP patterns copied byte-for-byte); non-x86 = empty skipper set → never skips.

Fidelity gate (honest accounting)

FNV-1a64 is validated against an external authority — the unit gate asserts the canonical published FNV-1a64 test vectors ("" → offset basis, a/b/c, foobar, hello), independently cross-checked against the reference algorithm, not mere self-consistency. The full FidHashQuad is gated by a hand-computed quad on a 5-instruction synthetic extent (register / small-usable-scalar / large-suppressed / whole-operand-address / no-operand + a call), asserted against an independent in-test reference reimplementation of the loop, plus self-consistency across two builds of the hasher and targeted invariants (code_unit_size = count − calls; a relocation suppresses the scalar in specific_count and changes only the specific hash, never the full hash).

Caveat — the Ghidra-validated golden-vector gate (design §7.1) is a documented follow-up. The ~/github/ghidra checkout has the FID sources but no built distribution (build/dist empty, no .class files), so dumping (function → FidHashQuad) vectors needs a multi-GB gradle build out of scope for PR2 — the design doc explicitly anticipates this fallback. PR3 (which builds a real lib.o fixture) is the natural place to add the Ghidra dump once a built Ghidra is available.

Sample FidHashQuad (the in-tree gate's 5-instruction sequence)

A 5-instruction x86-64 extent — push rbp / mov rbp,rsp (registers) / add eax,0x10 (small usable scalar, counts) / mov eax,0x12345678 (whole-operand scalar, counts) / call rel32 (whole-operand address scalar, suppressed; is_call) — hashes to a quad with code_unit_size = 5 − 1 call = 4, specific_addl_size = 2 (two usable scalars), and full_hash ≠ specific_hash (scalars folded only into the specific hash). The exact u64s are asserted in hash::tests::hand_computed_quad_matches_reference against the independent reference loop.

Gate results

  • make test — 675/675 PARITY OK
  • make test-stages — 205/205 PARITY OK (raised by the concurrent worker)
  • make rust-test — green; the new s1_fid unit gate RAN: 19 passed, 0 ignored (10 hash, 5 skipper, 4 extent)

Increment 53 in docs/analysis-port-log.md. Next: PR3 (the .fid DB format + kuna fid build generator), then PR4 (the FidPass + instruction_mask→InsnFingerprint assembly + rename/commit + --option fid surface + the stripped-binary re-identification e2e test).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 26, 2026 18:47
Port the pure FID hashing core, byte-exact to Ghidra's
MessageDigestFidHasher / FNV1a64MessageDigest. Mechanism only — no
pass/DB/option/engine wiring (PR3/PR4), so nothing here is reachable from a
decompilation and the parity oracles are structurally untouched.

New kuna-analysis/src/s1_fid/:
- hash.rs   — Fnv1a64 (offset basis 0xcbf29ce484222325, prime 0x100000001b3;
              update_byte/bytes/i32 [4 bytes big-endian]; digest_long resets to
              basis) + FidHasher + FidHashQuad. FidHasher::hash ports the
              per-instruction loop line-for-line: skippers BEFORE the call count
              (codeUnitIndex -= 1; continue), the 4-way scalar rule in order
              (relocation / whole-operand-address / large-non-whole /
              small-usable), the full hash never sees a scalar value (only the
              0xfeeddead placeholder), registers into both, then update_i32 the
              operand subhashes, then mask buf[i] &= fixed_mask[i] and feed the
              masked bytes to BOTH digests AFTER the operand writes (the critical
              interleave). Magic verbatim; wrapping_* everywhere; extents < 4 ->
              None. Relocation check consumed as a RelocationQuery trait.
- extent.rs — FunctionBodyFunctionExtentGenerator analog: the [entry,
              next_function_after(entry)) address-contiguous clip (the
              s1_noreturn_disc idiom); clip rule split out pure-testable.
- skipper.rs— InstructionSkipper trait + X86InstructionSkipper (17 NOP patterns
              copied byte-for-byte); non-x86 = empty set, never skips.

Fidelity gate: FNV-1a64 validated against the canonical published FNV test
vectors (external authority); FidHashQuad gated by a hand-computed quad on a
5-instruction synthetic extent vs an independent in-test reference loop, plus
self-consistency + targeted invariants. The Ghidra-validated golden-vector gate
(design §7.1) is a documented follow-up: the ~/github/ghidra checkout has no
built distribution, so a (function -> FidHashQuad) dump needs a gradle build out
of scope for PR2 (PR3, which builds a real lib.o fixture, is the natural place).

Gates: make test 675/675 PARITY OK; make test-stages 205/205 PARITY OK;
make rust-test green; the new s1_fid unit gate RAN (19 passed, 0 ignored).

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb
@mahaloz
mahaloz merged commit d1ee05a into main Jun 26, 2026
mahaloz added a commit that referenced this pull request Jun 26, 2026
Reconcile the taildup feature with main after the FID PRs (#97-#101) and
switchmultipred (#102) landed:

- ElementId: taildup collided with switchsharedcase (both 4106 on the merged
  tree). Renumbered taildup -> 4108 (next free above ifelseflatten's 4107).
- Count unions (taildup +1 settable, switchmultipred +1, fid +1 vs merge-base):
  settable 60 -> 63, surface stays 97 (taildup adds no surface row),
  corpus files 151 -> 153, universalaction leaves -> 264 (252 C++ + 5 kuna),
  bytecompat fixture 63, stages.toml header counts.
- kuna-catalog.xml provenance: source_decompiler angr 22, structure-recovery 12
  (+taildup), opt-in-tool 11 (+switchmultipred), use_when/change_kind max 64
  (settables + returnpair double-count in `stage catalog`).
- Regenerated docs/assertions.md (no change) and docs/baseline-stages.json
  (230 passing keys).

Gates: catalog --check OK; make test PARITY OK 675/675 (baseline.json untouched);
make test-stages PARITY OK 230/230; make rust-test green.
@mahaloz
mahaloz deleted the fid-hash-core branch July 29, 2026 08:01
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