Skip to content

fix: HLIL OperationOperands undercounts + ROR bogus Carry#26

Merged
tinysec merged 1 commit into
masterfrom
fix/hlil-operand-count-table
Jul 8, 2026
Merged

fix: HLIL OperationOperands undercounts + ROR bogus Carry#26
tinysec merged 1 commit into
masterfrom
fix/hlil-operand-count-table

Conversation

@tinysec

@tinysec tinysec commented Jul 8, 2026

Copy link
Copy Markdown
Owner

Problem

The OperationOperands count table in Struct/BNHighLevelILInstruction.cs sizes each instruction's RawOperands[] array. 21 entries undercounted their operands, so accessing the affected typed-accessor property (e.g. DestMemoryVersion, SourceMemoryVersion, Carry, Loop, Cond) threw IndexOutOfRangeException at runtime — the operand was never readable.

HLIL_ROR additionally exposed a bogus Carry property reading operands[2] past its two operands (C++ ROR is TwoOperandInstruction; only ADC/SBB/RLC/RRC are TwoOperandWithCarry).

Root cause

Counts verified 1:1 against binaryninja-api/highlevelilinstruction.h raw operand layouts. The misses fall into groups:

  • TwoOperandWithCarry (ADC/SBB/RLC/RRC): omitted carry operand → 2→3
  • SSA stores: omitted dest memory-version slot (CALL_SSA, INTRINSIC_SSA, SYSCALL_SSA, ASSIGN_MEM_SSA, ASSIGN_UNPACK_MEM_SSA, VAR_INIT_SSA)
  • SSAVariable ops: a variable occupies two raw slots (var, version); omitted the version slot (FORCE_VER_SSA, ASSERT_SSA, VAR_PHI)
  • Phi/control: omitted loop/phi operands (MEM_PHI, WHILE_SSA, DO_WHILE_SSA, FOR_SSA, CASE, ASSIGN_UNPACK)
  • Plain force/assert: omitted second operand (FORCE_VER, ASSERT)

This is the systematic-miscount cluster flagged from PR #24 (which fixed only STRUCT_FIELD/DEREF_FIELD/DEREF_FIELD_SSA).

Changes

  • Struct/BNHighLevelILInstruction.cs: 21 count bumps in the OperationOperands table, each with an operand-list comment.
  • HighLevelIL/HLILRotateRight.cs: removed bogus Carry property.

Verification

  • dotnet build -c Release clean.
  • Static audit (crash_audit.py, op→accessor switch): NO OOB after the fix (was 22 OOB).
  • New E2E regression in the harness repo: walks every HLIL expression in cat.bndb via MustGetExpression, invokes each declared operand-accessor getter, asserts none throws IndexOutOfRangeException.
  • Full harness regression: 40/40 pass.

Scope

Same bug class likely exists in the MLIL and LLIL operand-count tables — follow-up PRs will address those. (Also noted: the cross-IL mapping properties MediumLevelILs/LowLevelILs throw IndexOutOfRange for unmapped instructions — a separate latent bug, out of scope here.)

The OperationOperands count table in Struct/BNHighLevelILInstruction.cs
sized the per-instruction RawOperands[] array too small for 21 operations,
so accessing the affected typed-accessor properties threw
IndexOutOfRangeException at runtime. The undercounts fell into groups:

- TwoOperandWithCarry ops (ADC/SBB/RLC/RRC) omitted the carry operand (2->3)
- SSA stores omitted the dest memory-version slot (VAR_INIT_SSA, ASSIGN_MEM_SSA,
  ASSIGN_UNPACK_MEM_SSA, CALL_SSA, INTRINSIC_SSA, SYSCALL_SSA)
- SSAVariable-occupying ops omitted the version's second raw slot
  (FORCE_VER_SSA, ASSERT_SSA, VAR_PHI)
- Phi/control ops omitted loop and phi operands (MEM_PHI, WHILE_SSA,
  DO_WHILE_SSA, FOR_SSA, CASE, ASSIGN_UNPACK)
- Plain non-SSA force/assert omitted their second operand
  (FORCE_VER, ASSERT)

HLIL_ROR additionally exposed a bogus Carry property that read operands[2]
past its two operands -- C++ ROR is TwoOperandInstruction (no carry; only
ADC/SBB/RLC/RRC are TwoOperandWithCarry). Removed.

Counts verified 1:1 against binaryninja-api/highlevelilinstruction.h raw
operand layouts; SSAVariable occupies two raw slots (variable, version).
@tinysec tinysec merged commit 56ea1a7 into master Jul 8, 2026
1 check passed
@tinysec tinysec deleted the fix/hlil-operand-count-table branch July 8, 2026 07:27
tinysec added a commit that referenced this pull request Jul 9, 2026
#41)

Mirror Python HighLevelILInstruction.detailed_operands / operands / traverse
(highlevelil.py:795/786/802). A static descriptor table (operation -> named,
typed operands) drives a kind-dispatched reader; Operands and Traverse derive
from it, exactly as in Python.

- HighLevelILOperand: enum Kind + readonly Operand/Descriptor structs.
- HighLevelILDetailedOperandsTable: 114-op descriptor table generated from the
  Python detailed_operands overrides, abstract bases expanded to concrete ops.
- HighLevelILInstruction: GetOperandAsInteger, virtual DetailedOperands,
  ReadDetailedOperandByKind (SSA var + constant data occupy two raw slots),
  Operands, generic Traverse<T> (DFS, shallow blacklist matches Python).
- HLILVariableDeclare: expose the .Variable accessor (the one missing accessor).

Divergence from Python (intentional, documented in the table header): HLIL_ROR
omits the "carry" operand -- Python's HighLevelILRor inherits CarryBase but the
core (highlevelilinstruction.cpp:189) defines ROR with only {left,right}, so
Python's Ror.carry reads a non-existent slot. C# follows the core (OperationOperands
= 2, matching PR #26's removal of the bogus Carry accessor).

E2E (harness, not in this repo): DetailedOperands parity vs the Python spec for
ADD/STRUCT_FIELD/CONST(+PTR/FLOAT); Operands + Traverse; full cat.bndb sweep
no-throw; static consistency test reflecting over the table + OperationOperands
catches out-of-bounds indices (the ROR bug above was found + fixed by it).
76/76 pass.
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