From 3d5a45d8bcc7f11f89b9da6cf63be95f6d8a2a52 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 17 Jan 2020 19:45:31 -0500 Subject: Add member index for structure field HLIL instructions --- binaryninjaapi.h | 6 ++--- highlevelilinstruction.cpp | 55 ++++++++++++++++++++++++++++++++++------------ highlevelilinstruction.h | 6 +++++ python/highlevelil.py | 10 ++++++--- 4 files changed, 57 insertions(+), 20 deletions(-) diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 19143c13..ea74a466 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -4079,18 +4079,18 @@ __attribute__ ((format (printf, 1, 2))) const ILSourceLocation& loc = ILSourceLocation()); ExprId MemPhi(size_t dest, const std::vector& sources, const ILSourceLocation& loc = ILSourceLocation()); - ExprId StructField(size_t size, ExprId src, uint64_t offset, + ExprId StructField(size_t size, ExprId src, uint64_t offset, size_t memberIndex, const ILSourceLocation& loc = ILSourceLocation()); ExprId ArrayIndex(size_t size, ExprId src, ExprId idx, const ILSourceLocation& loc = ILSourceLocation()); ExprId ArrayIndexSSA(size_t size, ExprId src, size_t srcMemVersion, ExprId idx, const ILSourceLocation& loc = ILSourceLocation()); ExprId Split(size_t size, ExprId high, ExprId low, const ILSourceLocation& loc = ILSourceLocation()); ExprId Deref(size_t size, ExprId src, const ILSourceLocation& loc = ILSourceLocation()); - ExprId DerefField(size_t size, ExprId src, uint64_t offset, + ExprId DerefField(size_t size, ExprId src, uint64_t offset, size_t memberIndex, const ILSourceLocation& loc = ILSourceLocation()); ExprId DerefSSA(size_t size, ExprId src, size_t srcMemVersion, const ILSourceLocation& loc = ILSourceLocation()); - ExprId DerefFieldSSA(size_t size, ExprId src, size_t srcMemVersion, uint64_t offset, + ExprId DerefFieldSSA(size_t size, ExprId src, size_t srcMemVersion, uint64_t offset, size_t memberIndex, const ILSourceLocation& loc = ILSourceLocation()); ExprId AddressOf(ExprId src, const ILSourceLocation& loc = ILSourceLocation()); ExprId Const(size_t size, uint64_t val, const ILSourceLocation& loc = ILSourceLocation()); diff --git a/highlevelilinstruction.cpp b/highlevelilinstruction.cpp index 80d30a92..65b51ac4 100644 --- a/highlevelilinstruction.cpp +++ b/highlevelilinstruction.cpp @@ -54,6 +54,7 @@ unordered_map {HighExprHighLevelOperandUsage, ExprHighLevelOperand}, {LowExprHighLevelOperandUsage, ExprHighLevelOperand}, {OffsetHighLevelOperandUsage, IntegerHighLevelOperand}, + {MemberIndexHighLevelOperandUsage, IndexHighLevelOperand}, {ConstantHighLevelOperandUsage, IntegerHighLevelOperand}, {VectorHighLevelOperandUsage, IntegerHighLevelOperand}, {IntrinsicHighLevelOperandUsage, IntrinsicHighLevelOperand}, @@ -104,16 +105,18 @@ unordered_map> {HLIL_VAR_SSA, {SSAVariableHighLevelOperandUsage}}, {HLIL_VAR_PHI, {DestSSAVariableHighLevelOperandUsage, SourceSSAVariablesHighLevelOperandUsage}}, {HLIL_MEM_PHI, {DestMemoryVersionHighLevelOperandUsage, SourceMemoryVersionsHighLevelOperandUsage}}, - {HLIL_STRUCT_FIELD, {SourceExprHighLevelOperandUsage, OffsetHighLevelOperandUsage}}, + {HLIL_STRUCT_FIELD, {SourceExprHighLevelOperandUsage, OffsetHighLevelOperandUsage, + MemberIndexHighLevelOperandUsage}}, {HLIL_ARRAY_INDEX, {SourceExprHighLevelOperandUsage, IndexExprHighLevelOperandUsage}}, {HLIL_ARRAY_INDEX_SSA, {SourceExprHighLevelOperandUsage, SourceMemoryVersionHighLevelOperandUsage, IndexExprHighLevelOperandUsage}}, {HLIL_SPLIT, {HighExprHighLevelOperandUsage, LowExprHighLevelOperandUsage}}, {HLIL_DEREF, {SourceExprHighLevelOperandUsage}}, - {HLIL_DEREF_FIELD, {SourceExprHighLevelOperandUsage, OffsetHighLevelOperandUsage}}, + {HLIL_DEREF_FIELD, {SourceExprHighLevelOperandUsage, OffsetHighLevelOperandUsage, + MemberIndexHighLevelOperandUsage}}, {HLIL_DEREF_SSA, {SourceExprHighLevelOperandUsage, SourceMemoryVersionHighLevelOperandUsage}}, {HLIL_DEREF_FIELD_SSA, {SourceExprHighLevelOperandUsage, SourceMemoryVersionHighLevelOperandUsage, - OffsetHighLevelOperandUsage}}, + OffsetHighLevelOperandUsage, MemberIndexHighLevelOperandUsage}}, {HLIL_ADDRESS_OF, {SourceExprHighLevelOperandUsage}}, {HLIL_CALL, {DestExprHighLevelOperandUsage, ParameterExprsHighLevelOperandUsage}}, {HLIL_SYSCALL, {ParameterExprsHighLevelOperandUsage}}, @@ -1155,7 +1158,7 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, return dest->MemPhi(GetDestMemoryVersion(), GetSourceMemoryVersions(), *this); case HLIL_STRUCT_FIELD: return dest->StructField(size, subExprHandler(GetSourceExpr()), - GetOffset(), *this); + GetOffset(), GetMemberIndex(), *this); case HLIL_ARRAY_INDEX: return dest->ArrayIndex(size, subExprHandler(GetSourceExpr()), subExprHandler(GetIndexExpr()), *this); @@ -1170,13 +1173,14 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, return dest->Deref(size, subExprHandler(GetSourceExpr()), *this); case HLIL_DEREF_FIELD: return dest->DerefField(size, subExprHandler(GetSourceExpr()), - GetOffset(), *this); + GetOffset(), GetMemberIndex(), *this); case HLIL_DEREF_SSA: return dest->DerefSSA(size, subExprHandler(GetSourceExpr()), GetSourceMemoryVersion(), *this); case HLIL_DEREF_FIELD_SSA: return dest->DerefFieldSSA(size, subExprHandler(GetSourceExpr()), - GetSourceMemoryVersion(), GetOffset(), *this); + GetSourceMemoryVersion(), GetOffset(), + GetMemberIndex(), *this); case HLIL_ADDRESS_OF: return dest->AddressOf(subExprHandler(GetSourceExpr()), *this); case HLIL_CALL: @@ -1474,7 +1478,11 @@ bool HighLevelILInstruction::operator<(const HighLevelILInstruction& other) cons return true; if (other.GetSourceExpr() < GetSourceExpr()) return false; - return GetOffset() < other.GetOffset(); + if (GetOffset() < other.GetOffset()) + return true; + if (other.GetOffset() < GetOffset()) + return false; + return GetMemberIndex() < other.GetMemberIndex(); case HLIL_ARRAY_INDEX: if (size < other.size) return true; @@ -1518,7 +1526,11 @@ bool HighLevelILInstruction::operator<(const HighLevelILInstruction& other) cons return true; if (other.GetSourceExpr() < GetSourceExpr()) return false; - return GetOffset() < other.GetOffset(); + if (GetOffset() < other.GetOffset()) + return true; + if (other.GetOffset() < GetOffset()) + return false; + return GetMemberIndex() < other.GetMemberIndex(); case HLIL_DEREF_SSA: if (size < other.size) return true; @@ -1542,6 +1554,10 @@ bool HighLevelILInstruction::operator<(const HighLevelILInstruction& other) cons return true; if (other.GetOffset() < GetOffset()) return false; + if (GetMemberIndex() < other.GetMemberIndex()) + return true; + if (other.GetMemberIndex() < GetMemberIndex()) + return false; return GetSourceMemoryVersion() < other.GetSourceMemoryVersion(); case HLIL_ADDRESS_OF: return GetSourceExpr() < other.GetSourceExpr(); @@ -1958,6 +1974,15 @@ uint64_t HighLevelILInstruction::GetOffset() const } +size_t HighLevelILInstruction::GetMemberIndex() const +{ + size_t operandIndex; + if (GetOperandIndexForUsage(MemberIndexHighLevelOperandUsage, operandIndex)) + return GetRawOperandAsIndex(operandIndex); + throw HighLevelILInstructionAccessException(); +} + + int64_t HighLevelILInstruction::GetConstant() const { size_t operandIndex; @@ -2233,9 +2258,10 @@ ExprId HighLevelILFunction::MemPhi(size_t dest, const vector& sources, c } -ExprId HighLevelILFunction::StructField(size_t size, ExprId src, uint64_t offset, const ILSourceLocation& loc) +ExprId HighLevelILFunction::StructField(size_t size, ExprId src, uint64_t offset, size_t memberIndex, + const ILSourceLocation& loc) { - return AddExprWithLocation(HLIL_STRUCT_FIELD, loc, size, src, offset); + return AddExprWithLocation(HLIL_STRUCT_FIELD, loc, size, src, offset, memberIndex); } @@ -2264,9 +2290,10 @@ ExprId HighLevelILFunction::Deref(size_t size, ExprId src, const ILSourceLocatio } -ExprId HighLevelILFunction::DerefField(size_t size, ExprId src, uint64_t offset, const ILSourceLocation& loc) +ExprId HighLevelILFunction::DerefField(size_t size, ExprId src, uint64_t offset, size_t memberIndex, + const ILSourceLocation& loc) { - return AddExprWithLocation(HLIL_DEREF_FIELD, loc, size, src, offset); + return AddExprWithLocation(HLIL_DEREF_FIELD, loc, size, src, offset, memberIndex); } @@ -2277,9 +2304,9 @@ ExprId HighLevelILFunction::DerefSSA(size_t size, ExprId src, size_t srcMemVersi ExprId HighLevelILFunction::DerefFieldSSA(size_t size, ExprId src, size_t srcMemVersion, - uint64_t offset, const ILSourceLocation& loc) + uint64_t offset, size_t memberIndex, const ILSourceLocation& loc) { - return AddExprWithLocation(HLIL_DEREF_FIELD_SSA, loc, size, src, srcMemVersion, offset); + return AddExprWithLocation(HLIL_DEREF_FIELD_SSA, loc, size, src, srcMemVersion, offset, memberIndex); } diff --git a/highlevelilinstruction.h b/highlevelilinstruction.h index 6c249d94..cfc917e8 100644 --- a/highlevelilinstruction.h +++ b/highlevelilinstruction.h @@ -85,6 +85,7 @@ namespace BinaryNinja HighExprHighLevelOperandUsage, LowExprHighLevelOperandUsage, OffsetHighLevelOperandUsage, + MemberIndexHighLevelOperandUsage, ConstantHighLevelOperandUsage, VectorHighLevelOperandUsage, IntrinsicHighLevelOperandUsage, @@ -390,6 +391,7 @@ namespace BinaryNinja template HighLevelILInstruction GetHighExpr() const { return As().GetHighExpr(); } template HighLevelILInstruction GetLowExpr() const { return As().GetLowExpr(); } template uint64_t GetOffset() const { return As().GetOffset(); } + template size_t GetMemberIndex() const { return As().GetMemberIndex(); } template int64_t GetConstant() const { return As().GetConstant(); } template int64_t GetVector() const { return As().GetVector(); } template uint32_t GetIntrinsic() const { return As().GetIntrinsic(); } @@ -443,6 +445,7 @@ namespace BinaryNinja HighLevelILInstruction GetHighExpr() const; HighLevelILInstruction GetLowExpr() const; uint64_t GetOffset() const; + size_t GetMemberIndex() const; int64_t GetConstant() const; int64_t GetVector() const; uint32_t GetIntrinsic() const; @@ -628,11 +631,13 @@ namespace BinaryNinja { HighLevelILInstruction GetSourceExpr() const { return GetRawOperandAsExpr(0); } uint64_t GetOffset() const { return GetRawOperandAsInteger(1); } + size_t GetMemberIndex() const { return GetRawOperandAsIndex(2); } }; template <> struct HighLevelILInstructionAccessor: public HighLevelILInstructionBase { HighLevelILInstruction GetSourceExpr() const { return GetRawOperandAsExpr(0); } uint64_t GetOffset() const { return GetRawOperandAsInteger(1); } + size_t GetMemberIndex() const { return GetRawOperandAsIndex(2); } }; template <> struct HighLevelILInstructionAccessor: public HighLevelILInstructionBase { @@ -646,6 +651,7 @@ namespace BinaryNinja size_t GetSourceMemoryVersion() const { return GetRawOperandAsIndex(1); } void SetSourceMemoryVersion(size_t version) { UpdateRawOperand(1, version); } uint64_t GetOffset() const { return GetRawOperandAsInteger(2); } + size_t GetMemberIndex() const { return GetRawOperandAsIndex(3); } }; template <> struct HighLevelILInstructionAccessor: public HighLevelILInstructionBase { diff --git a/python/highlevelil.py b/python/highlevelil.py index a4140f85..505b2b97 100644 --- a/python/highlevelil.py +++ b/python/highlevelil.py @@ -102,14 +102,14 @@ class HighLevelILInstruction(object): HighLevelILOperation.HLIL_VAR_SSA: [("var", "var_ssa")], HighLevelILOperation.HLIL_VAR_PHI: [("dest", "var_ssa"), ("src", "var_ssa_list")], HighLevelILOperation.HLIL_MEM_PHI: [("dest", "int"), ("src", "int_list")], - HighLevelILOperation.HLIL_STRUCT_FIELD: [("src", "expr"), ("offset", "int")], + HighLevelILOperation.HLIL_STRUCT_FIELD: [("src", "expr"), ("offset", "int"), ("member_index", "member_index")], HighLevelILOperation.HLIL_ARRAY_INDEX: [("src", "expr"), ("index", "expr")], HighLevelILOperation.HLIL_ARRAY_INDEX_SSA: [("src", "expr"), ("src_memory", "int"), ("index", "expr")], HighLevelILOperation.HLIL_SPLIT: [("high", "expr"), ("low", "expr")], HighLevelILOperation.HLIL_DEREF: [("src", "expr")], - HighLevelILOperation.HLIL_DEREF_FIELD: [("src", "expr"), ("offset", "int")], + HighLevelILOperation.HLIL_DEREF_FIELD: [("src", "expr"), ("offset", "int"), ("member_index", "member_index")], HighLevelILOperation.HLIL_DEREF_SSA: [("src", "expr"), ("src_memory", "int")], - HighLevelILOperation.HLIL_DEREF_FIELD_SSA: [("src", "expr"), ("src_memory", "int"), ("offset", "int")], + HighLevelILOperation.HLIL_DEREF_FIELD_SSA: [("src", "expr"), ("src_memory", "int"), ("offset", "int"), ("member_index", "member_index")], HighLevelILOperation.HLIL_ADDRESS_OF: [("src", "expr")], HighLevelILOperation.HLIL_CONST: [("constant", "int")], HighLevelILOperation.HLIL_CONST_PTR: [("constant", "int")], @@ -257,6 +257,10 @@ class HighLevelILInstruction(object): value.append(mediumlevelil.SSAVariable(function.Variable.from_identifier(self._function.source_function, var_id), var_version)) core.BNHighLevelILFreeOperandList(operand_list) + elif operand_type == "member_index": + value = instr.operands[i] + if (value & (1 << 63)) != 0: + value = None self._operands.append(value) self.__dict__[name] = value i += 1 -- cgit v1.3.1