Skip to content

FID PR1: SLEIGH instruction-mask accessor + design doc - #94

Merged
mahaloz merged 2 commits into
mainfrom
fid-mask-accessor
Jun 26, 2026
Merged

mahaloz merged 2 commits into
mainfrom
fid-mask-accessor

Conversation

@mahaloz

@mahaloz mahaloz commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

FID PR1 — the SLEIGH instruction-mask accessor (the fingerprinting prerequisite)

The first PR of the FID (Function-ID fingerprinting) port. FID's hashes are
operand-independent: they fingerprint a function by the bits SLEIGH pins to a
constant (opcode / addressing-mode bits) while masking out the bits that carry
operand values (immediates, displacements, register selectors). kuna's decoder
discards the matched DisjointPattern after DecisionNode::resolve, so there was
no way to recover which bits of a decoded instruction are fixed. This PR adds
that accessor — and the whole-feature design doc, docs/fid-design.md.

PR1 is the accessor only — no FID hashing/DB/pass (those are PR2+ per the
design doc's ordered breakdown).

What the accessor does

Sleigh::instruction_mask(addr) -> InsnMask { bytes, fixed_mask, length, operands }:

  • fixed_mask[i] has a 1 wherever SLEIGH pins a fixed encoding bit; operand_mask = !fixed_mask.
  • operands: Vec<OperandView> carries each operand's value-bit mask and its
    classified OpObjects — Scalar { signed, .. } / Register { offset } / Address,
    read off the resolved FixedHandles.

It re-uses obtain_context verbatim, then walks the matched ConstructState
arena: per node it re-runs the decision tree (new DecisionNode::resolve_matched)
to recover the specific leaf DisjointPattern, then ORs that pattern's fixed
instruction bits (get_mask(.., context=false)) into fixed_mask, shifted by
the node's byte offset, byte-by-byte. Context-stream bits are never folded in;
MAX_INSTRUCTION_LEN=16 caps the buffer.

Sample (hand-checked against objdump -d -M intel)

insn bytes fixed_mask note
add eax,0x12345678 05 78 56 34 12 [ff,00,00,00,00] opcode pinned, imm32 zeroed + Scalar{0x12345678}
mov ecx,imm32 b9 44 33 22 11 [f8,00,00,00,00] b8+rd opcode — low-3 register selector free
add eax,ecx 01 c8 [ff,c0] modrm reg/rm selectors are operand bits
mov eax,[rbx+disp32] 8b 83 78 56 34 12 [ff,c0,00,00,00,00] disp32 zeroed + Scalar{0x12345678}

Each row pins its opcode bits and zeroes its operand bytes — exactly the
operand-independence FID's full hash needs.

Additive / byte-identical proof

PR1 touches the proven kuna-sleigh core but is purely additive — a new
DecisionNode method (resolve_matched) and a new Sleigh accessor
(instruction_mask); no existing decode path is modified. The diff is two
kuna-sleigh source files + the design doc + a new unit test + the port log. No
.sla and no datatest changed. As a result:

  • datatest parity is byte-identical — make test 675/675 PARITY OK.
  • .sla content-parity is untouched — make rust-test green (the SLEIGH
    compiler emits the same bytes; nothing in the spec or compiler changed).

Gates

  • make test — 675/675 PARITY OK
  • make test-stages — PARITY OK (203/203; count raised by the concurrent worker)
  • make rust-test — green, including the .sla content-parity tests
  • New kuna-sleigh/tests/instruction_mask.rs (4 x86-64 cases) — RAN: 4 passed, 0 ignored

Note / gotcha

RIP-relative [rip+disp32] forms (mod=00 rm=101) need address arithmetic the
minimal standalone test harness does not supply, so the disp32 test uses the
register-base mov reg,[base+disp32] form. This is a harness limitation, not an
accessor limitation (the same harness would fail RIP-relative in print_assembly
too).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FvRnfTgYKoZvS2TEvdYpWb

mahaloz and others added 2 commits June 26, 2026 18:04
The first PR of the FID (Function-ID fingerprinting) port: the SLEIGH
instruction-mask accessor that every FID hash consumes, plus the whole-feature
design doc (docs/fid-design.md). FID's hashes are operand-independent — they
fingerprint a function by the bits SLEIGH pins to a constant (opcode /
addressing-mode bits) while masking out operand-value bits. kuna had no way to
ask "which bits of this decoded instruction are fixed?"; this accessor adds it.

Purely additive, zero decode-behavior change. PR1 touches the proven kuna-sleigh
core but adds ONLY a new DecisionNode method and a new Sleigh accessor; no
existing decode path is modified. instruction_mask re-uses obtain_context
verbatim and then reads the resolved constructor tree. Proof: make test 675/675
PARITY OK, and make rust-test's .sla content-parity + datatest parity are
byte-identical.

Two additions:
- DecisionNode::resolve_matched (slghsymbol.rs, beside resolve): a variant of
  resolve that walks the decision tree byte-for-byte identically but at the
  terminal node returns the matched (DisjointPattern, ct) pair (re-running
  pat.is_match to capture the specific leaf). SubtableSymbol::resolve_matched
  forwards to it. No decode path calls these.
- Sleigh::instruction_mask (sleigh.rs, beside instruction_length) returning
  InsnMask { bytes, fixed_mask, length, operands } (+ OperandView, OpObject
  {Scalar/Register/Address}). Lives in sleigh.rs because obtain_context/
  ParserContext/ConstructState/ParserWalker are private. It obtain_contexts at
  Pcode state, walks the matched ConstructState arena, ORs each node's fixed
  instruction bits (get_mask context=false, shifted by the node offset) into
  fixed_mask, and classifies each operand's resolved FixedHandle.

Gotchas honored: multi-word masks walked byte-by-byte respecting node offsets;
context-stream bits never folded into the byte mask; MAX_INSTRUCTION_LEN=16 cap;
delay slots handled by the per-addr granularity.

Sample (hand-checked vs objdump):
  add eax,0x12345678  = 05 78 56 34 12  -> fixed [ff,00,00,00,00] + Scalar{0x12345678}
  mov ecx,imm32       = b9 ...          -> [f8,...]  (b8+rd reg selector free)
  add eax,ecx         = 01 c8           -> [ff,c0]   (modrm reg/rm = operand bits)
  mov eax,[rbx+disp32]= 8b 83 78 56 34 12 -> [ff,c0,00,00,00,00] + Scalar{0x12345678}

This is the AIF/FID-family prerequisite the build-plan flagged. No FID
hashing/DB/pass here — PR1 is the accessor only.

Gates: make test 675/675 PARITY OK; make test-stages PARITY OK (203/203);
make rust-test green (incl. .sla content-parity). New unit gate
(kuna-sleigh/tests/instruction_mask.rs, 4 x86-64 cases) RAN: 4 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 f383e04 into main Jun 26, 2026
@mahaloz
mahaloz deleted the fid-mask-accessor 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