Binary Ninja Intermediate Language Series, Part 3: High Level IL¶
Binary Ninja Intermediate Language: High Level IL¶
The High Level Intermediate Language (HLIL) is Binary Ninja's decompiler output. Much like LLIL and MLIL, this representation is tree based and has many of the same instructions. This representation is distinct in a few key ways.
- High level language concepts are recovered.
- Expressions are folded
Purposes of HLIL¶
- Simplified representation
- Small discrete operations
- Enables source-level forms of queries and analysis
Debug Report¶
To observe the transformations that occur from MLIL to HLIL, you can use the built-in debug report API:
> current_function.request_debug_report("hlil")
The Instruction Set¶
The instruction set is made up of HighLevelILInstruction objects. Let's start exploring by using the python console to poke around at some instructions. Open up a binary in Binary Ninja and retrieve an HLIL instruction:+
>>> current_il_instruction
<HighLevelILVarInit: uint64_t rax_2 = zx.q(rax_1 - 0x6c)>
>>> type(current_il_instruction)
<class 'binaryninja.highlevelil.HighLevelILVarInit'>
current_il_instruction is mapped to whatever il instruction is currently selected viewed and is not generally available to those writing plugins or scripts. You can see a full list of our magic variables here.
There are a number of properties that can be queried on the HighLevelILInstruction object, and the validity of these properties changes depending on what the current operation is. The properties in HLIL are extremely similar to those in MLIL, with one notable exception being that HLIL_CALL instruction objects do not have a .output property, but instead HLIL_CALLs that have return values will be the inst.right of a HighLevelILVarInit, HighLevelILVarAssign, or similar.
Control Flow¶
HLIL_JUMP- Branch to thedestexpression's addressHLIL_CALL- Branch to thedestexpression function, saving the return address, with the list of parametersparamsHLIL_RET- Return to the calling function.HLIL_NORET- This instruction will never be executed, the instruction before it is a call that doesn't returnHLIL_IF- Branch to thetrue/falseHLIL instruction identifier depending on the result of theconditionexpressionHLIL_GOTO- Branch to thedestexpression idHLIL_TAILCALL- This instruction calls the expressiondestusingparamsas input andoutputfor return values not existHLIL_SYSCALL- Make a system/service call with parametersparamsand outputoutputHLIL_WHILE-HLIL_DO_WHILE-HLIL_FOR-HLIL_SWITCH-HLIL_CASE-HLIL_BREAK-HLIL_CONTINUE-
Variable Reads and Writes¶
HLIL_VAR_DECLARE- A declaration ofvarHLIL_VAR_INIT- Initializesdestto the result of an expressionsrcHLIL_ASSIGN- Sets a variabledestto the result of an expressionsrcHLIL_ASSIGN_UNPACK-HLIL_VAR- A variable expressionsrcHLIL_VAR_PHI- APHIrepresents the combination of several prior versions of a variable when different basic blocks coalesce into a single destination and it's unknown which path was taken.HLIL_MEM_PHI- A memoryPHIrepresents memory modifications that could have occured down different source basic blocks similar to aVAR_PHI.HLIL_ADDRESS_OF- The address of variablesrcHLIL_CONST- A constant integral valueconstantHLIL_CONST_DATA- A constant data referenceconstant data referenceHLIL_CONST_PTR- A constant integral value which is used as a pointerconstantHLIL_EXTERN_PTR- A symbolic pointerconstant+offsetto a symbol that exists outside the binaryHLIL_FLOAT_CONST- A floating point constantconstantHLIL_IMPORT- Aconstantintegral value representing an imported addressHLIL_LOW_PART-sizebytes from the low end ofsrcexpressionHLIL_STRUCT_FIELD-HLIL_ARRAY_INDEX-HLIL_SPLIT- A split pair of variableshigh:lowwhich can be used a single expressionHLIL_DEREF- DereferencessrcHLIL_DEREF_FIELD-
Arithmetic Operations¶
HLIL_ADD- Addsleftexpression torightexpressionHLIL_ADC- Adds with carry theleftexpression to therightexpression with carry from thecarryexpressionHLIL_SUB- Subtracts therightexpression from theleftexpressionHLIL_SBB- Subtraction with borrow therightexpression from theleftexpression with carry from thecarryexpressionHLIL_AND- Bitwise ANDleftexpression with therightexpressionHLIL_OR- Bitwise ORleftexpression with therightexpressionHLIL_XOR- Bitwise XORleftexpression with therightexpressionHLIL_LSL- Logical shift left theleftexpression by the number of bits stored in therightexpressionHLIL_LSR- Logical shift right theleftexpression by the number of bits stored in therightexpressionHLIL_ASR- Arithmetic shift right theleftexpression by the number of bits stored in therightexpressionHLIL_ROL- Rotate left theleftexpression by the number of bits stored in therightexpressionHLIL_RLC- Rotate left with carry theleftexpression and thecarryexpression by the number of bits stored in therightexpressionHLIL_ROR- Rotate right theleftexpression by the number of bits stored in therightexpressionHLIL_RRC- Rotate right with carry theleftexpression and thecarryexpression by the number of bits stored in therightexpressionHLIL_MUL- Single-precision multiply theleftexpression with therightexpressionHLIL_MULU_DP- Double-precision unsigned multiply theleftexpression with therightexpression, result expression is twice the size of the input expressionsHLIL_MULS_DP- Double-precision signed multiply theleftexpression with therightexpression, result expression is twice the size of the input expressionsHLIL_DIVU- Unsigned single-precision divideleftexpression by therightexpressionHLIL_DIVU_DP- Unsigned double-precision divideleftexpression by therightexpressionHLIL_DIVS- Signed single-precision divideleftexpression by therightexpressionHLIL_DIVS_DP- Signed double-precision divideleftexpression by therightexpressionHLIL_MODU- Unsigned single-precision modulus ofleftexpression by therightexpressionHLIL_MODU_DP- Unsigned double-precision modulus ofleftexpression by therightexpressionHLIL_MODS- Signed single-precision modulus ofleftexpression by therightexpressionHLIL_MODS_DP- Signed double-precision modulus ofleftexpression by therightexpressionHLIL_NEG- Sign inversion ofsrcexpressionHLIL_NOT- Bitwise inversion ofsrcexpressionHLIL_FADD- IEEE754 floating point addition ofleftexpression withrightexpressionHLIL_FSUB- IEEE754 floating point subtraction ofleftexpression withrightexpressionHLIL_FMUL- IEEE754 floating point multiplication ofleftexpression withrightexpressionHLIL_FDIV- IEEE754 floating point division ofleftexpression withrightexpressionHLIL_FSQRT- IEEE754 floating point square root ofleftexpression withrightexpressionHLIL_FNEG- IEEE754 floating point sign negation ofsrcexpressionHLIL_FABS- IEEE754 floating point absolute value ofsrcexpressionHLIL_FLOAT_TO_INT- IEEE754 floating point to integer conversion ofsrcexpressionHLIL_INT_TO_FLOAT- Integer to IEEE754 floating point conversion ofsrcexpressionHLIL_FLOAT_CONV- Convert bytes insrcexpression to IEEE754 floating pointHLIL_ROUND_TO_INT- Rounds the IEEE754 floating point numbersrcexpressionHLIL_FLOOR- Computes the floating point floor of the IEEE754 number insrcHLIL_CEIL- Computes the floating point floor of the IEEE754 number insrcHLIL_FTRUNC- Computes the floating point truncation of the IEEE754 number insrcHLIL_SX- Sign extends thesrcexpressionHLIL_ZX- Zero extends thesrcexpressionHLIL_ADD_OVERFLOW- Calculates overflow of the addition ofleftexpression withrightexpressionHLIL_BOOL_TO_INT- Converts a boolsrcto an integer
Comparison Instructions¶
HLIL_CMP_E- Compare expression evaluates to true ifleftexpression is equal torightHLIL_CMP_NE- Compare expression evaluates to true ifleftexpression is not equal torightHLIL_CMP_SLT- Compare expression evaluates to true ifleftexpression is signed less thanrightHLIL_CMP_ULT- Compare expression evaluates to true ifleftexpression is unsigned less thanrightHLIL_CMP_SLE- Compare expression evaluates to true ifleftexpression is signed less than or equal torightHLIL_CMP_ULE- Compare expression evaluates to true ifleftexpression is unsigned less than or equal torightHLIL_CMP_SGE- Compare expression evaluates to true ifleftexpression is signed greater than or equal torightHLIL_CMP_UGE- Compare expression evaluates to true ifleftexpression is unsigned greater than or equal torightHLIL_CMP_SGT- Compare expression evaluates to true ifleftexpression is signed greater thanrightHLIL_CMP_UGT- Compare expression evaluates to true ifleftexpression is unsigned greater thanrightHLIL_TEST_BIT- Test if bitrightin expressionleftis setHLIL_FCMP_E- Floating point compare expressions - evaluates to true ifleftexpression is equal torightHLIL_FCMP_NE- Floating point compare expressions - evaluates to true ifleftexpression is not equal torightHLIL_FCMP_LT- Floating point compare expressions - evaluates to true ifleftexpression is less thanrightHLIL_FCMP_LE- Floating point compare expressions - evaluates to true ifleftexpression is less than or equal torightHLIL_FCMP_GE- Floating point compare expressions - evaluates to true ifleftexpression is greater than or equal torightHLIL_FCMP_GT- Floating point compare expressions - evaluates to true ifleftexpression is greater thanrightHLIL_FCMP_O- Floating point compare expressions - evaluates to true if bothleftandrightexpressions are ordered (not NaN)HLIL_FCMP_UO- Floating point compare expressions - evaluates to true if eitherleftorrightexpression is unordered (NaN)
Miscellaneous Instructions¶
HLIL_NOP- No operationHLIL_BP- Breakpoint instructionHLIL_TRAP- Interrupt/trap instruction withvectorexpressionHLIL_INTRINSIC- Intrinsic instruction defined by the architectureHLIL_UNDEF- The expression performs undefined behaviorHLIL_UNIMPL- The expression is not implementedHLIL_UNIMPL_MEM- The expression is not implemented but does accesssrcmemoryHLIL_BLOCK-HLIL_LABEL-HLIL_UNREACHABLE-

