From 64706bbd526c22e38b0be34f5d2b5a75766a6c02 Mon Sep 17 00:00:00 2001 From: Glenn Smith Date: Fri, 16 May 2025 16:24:46 -0400 Subject: [Python] Support HLIL collapsing and update PseudoPython Fixes Vector35/binaryninja-api#6679 --- python/highlevelil.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'python/highlevelil.py') diff --git a/python/highlevelil.py b/python/highlevelil.py index 8503bbe4..dc16e333 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -943,6 +943,36 @@ class HighLevelILInstruction(BaseILInstruction): finally: core.BNFreeDisassemblyTextLines(lines, count.value) + @property + def can_collapse(self) -> bool: + """If this instruction can be collapsed in rendered lines""" + return self.operation in [ + HighLevelILOperation.HLIL_IF, + HighLevelILOperation.HLIL_WHILE, + HighLevelILOperation.HLIL_WHILE_SSA, + HighLevelILOperation.HLIL_DO_WHILE, + HighLevelILOperation.HLIL_DO_WHILE_SSA, + HighLevelILOperation.HLIL_FOR, + HighLevelILOperation.HLIL_FOR_SSA, + HighLevelILOperation.HLIL_SWITCH, + HighLevelILOperation.HLIL_CASE + ] + + def get_instruction_hash(self, discriminator: int) -> int: + """ + Hash of instruction matching the C++ HighLevelILInstruction::GetInstructionHash, + used for collapsed region matching. + :param discriminator: Extra value to include in the hash to differentiate regions + """ + + def rotl(value, shift): + return ((value << shift) & 0xffffffffffffffff) | (value >> (64 - shift)) + + hash = self.operation.value + hash ^= rotl(self.address, 23) + hash ^= rotl(discriminator, 47) + return hash + @dataclass(frozen=True, repr=False, eq=False) class HighLevelILUnaryBase(HighLevelILInstruction, UnaryOperation): -- cgit v1.3.1