Skip to content

Commit 11ccff1

Browse files
committed
Add a reason field to MLIL_FORCE_VER for use in automated variable splitting
1 parent d9fd7a6 commit 11ccff1

5 files changed

Lines changed: 58 additions & 15 deletions

File tree

binaryninjaapi.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15912,8 +15912,10 @@ namespace BinaryNinja {
1591215912
ExprId SetVarAliasedField(size_t size, const Variable& dest, size_t newMemVersion, size_t prevMemVersion,
1591315913
uint64_t offset, ExprId src, const ILSourceLocation& loc = ILSourceLocation());
1591415914

15915-
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc = ILSourceLocation());
15916-
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc = ILSourceLocation());
15915+
ExprId ForceVer(size_t size, const Variable& dest, const Variable& src, BNForceVersionReason reason,
15916+
const ILSourceLocation& loc = ILSourceLocation());
15917+
ExprId ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, BNForceVersionReason raeson,
15918+
const ILSourceLocation& loc = ILSourceLocation());
1591715919

1591815920
ExprId Assert(size_t size, const Variable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());
1591915921
ExprId AssertSSA(size_t size, const SSAVariable& src, const PossibleValueSet& pvs, const ILSourceLocation& loc = ILSourceLocation());

binaryninjacore.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,12 @@ extern "C"
15901590
size_t operand;
15911591
} BNMediumLevelILLabel;
15921592

1593+
BN_ENUM(uint8_t, BNForceVersionReason)
1594+
{
1595+
UserForceVersionReason,
1596+
PartialAccessAnalysisForceVersionReason
1597+
};
1598+
15931599
BN_ENUM(uint8_t, BNVariableSourceType)
15941600
{
15951601
StackVariableSourceType,

mediumlevelilinstruction.cpp

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ static constexpr std::array s_operandTypeForUsage = {
9696
OperandUsageType{ParameterSSAMemoryVersionMediumLevelOperandUsage, IndexMediumLevelOperand},
9797
OperandUsageType{SourceSSAVariablesMediumLevelOperandUsages, SSAVariableListMediumLevelOperand},
9898
OperandUsageType{ConstraintMediumLevelOperandUsage, ConstraintMediumLevelOperand},
99+
OperandUsageType{ForceVersionReasonMediumLevelOperandUsage, ForceVersionReasonMediumLevelOperand},
99100
};
100101

101102
static_assert(std::is_sorted(s_operandTypeForUsage.begin(), s_operandTypeForUsage.end()),
@@ -160,7 +161,7 @@ static constexpr std::array s_instructionOperandUsage = {
160161
OperandUsage{MLIL_SET_VAR_FIELD, {DestVariableMediumLevelOperandUsage, OffsetMediumLevelOperandUsage, SourceExprMediumLevelOperandUsage}},
161162
OperandUsage{MLIL_SET_VAR_SPLIT, {HighVariableMediumLevelOperandUsage, LowVariableMediumLevelOperandUsage, SourceExprMediumLevelOperandUsage}},
162163
OperandUsage{MLIL_ASSERT, {SourceVariableMediumLevelOperandUsage, ConstraintMediumLevelOperandUsage}},
163-
OperandUsage{MLIL_FORCE_VER, {DestVariableMediumLevelOperandUsage, SourceVariableMediumLevelOperandUsage}},
164+
OperandUsage{MLIL_FORCE_VER, {DestVariableMediumLevelOperandUsage, SourceVariableMediumLevelOperandUsage, ForceVersionReasonMediumLevelOperandUsage}},
164165
OperandUsage{MLIL_LOAD, {SourceExprMediumLevelOperandUsage}},
165166
OperandUsage{MLIL_LOAD_STRUCT, {SourceExprMediumLevelOperandUsage, OffsetMediumLevelOperandUsage}},
166167
OperandUsage{MLIL_STORE, {DestExprMediumLevelOperandUsage, SourceExprMediumLevelOperandUsage}},
@@ -280,7 +281,7 @@ static constexpr std::array s_instructionOperandUsage = {
280281
OperandUsage{MLIL_VAR_ALIASED_FIELD, {SourceSSAVariableMediumLevelOperandUsage, OffsetMediumLevelOperandUsage}},
281282
OperandUsage{MLIL_VAR_SPLIT_SSA, {HighSSAVariableMediumLevelOperandUsage, LowSSAVariableMediumLevelOperandUsage}},
282283
OperandUsage{MLIL_ASSERT_SSA, {SourceSSAVariableMediumLevelOperandUsage, ConstraintMediumLevelOperandUsage}},
283-
OperandUsage{MLIL_FORCE_VER_SSA, {DestSSAVariableMediumLevelOperandUsage, SourceSSAVariableMediumLevelOperandUsage}},
284+
OperandUsage{MLIL_FORCE_VER_SSA, {DestSSAVariableMediumLevelOperandUsage, SourceSSAVariableMediumLevelOperandUsage, ForceVersionReasonMediumLevelOperandUsage}},
284285
OperandUsage{MLIL_CALL_SSA, {OutputExprsSubExprMediumLevelOperandUsage, OutputSSAMemoryVersionMediumLevelOperandUsage, DestExprMediumLevelOperandUsage, ParameterExprsMediumLevelOperandUsage, SourceMemoryVersionMediumLevelOperandUsage}},
285286
OperandUsage{MLIL_CALL_UNTYPED_SSA, {OutputExprsSubExprMediumLevelOperandUsage, OutputSSAMemoryVersionMediumLevelOperandUsage, DestExprMediumLevelOperandUsage, UntypedParameterSSAExprsMediumLevelOperandUsage, ParameterSSAMemoryVersionMediumLevelOperandUsage, StackExprMediumLevelOperandUsage}},
286287
OperandUsage{MLIL_SYSCALL_SSA, {OutputExprsSubExprMediumLevelOperandUsage, OutputSSAMemoryVersionMediumLevelOperandUsage, ParameterExprsMediumLevelOperandUsage, SourceMemoryVersionMediumLevelOperandUsage}},
@@ -1724,9 +1725,11 @@ ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest,
17241725
GetSourceSSAVariable<MLIL_VAR_OUTPUT_ALIASED_FIELD>().version,
17251726
GetOffset<MLIL_VAR_OUTPUT_ALIASED_FIELD>(), loc);
17261727
case MLIL_FORCE_VER:
1727-
return dest->ForceVer(size, GetDestVariable<MLIL_FORCE_VER>(), GetSourceVariable<MLIL_FORCE_VER>(), loc);
1728+
return dest->ForceVer(size, GetDestVariable<MLIL_FORCE_VER>(), GetSourceVariable<MLIL_FORCE_VER>(),
1729+
GetForceVersionReason<MLIL_FORCE_VER>(), loc);
17281730
case MLIL_FORCE_VER_SSA:
1729-
return dest->ForceVerSSA(size, GetDestSSAVariable<MLIL_FORCE_VER_SSA>(), GetSourceSSAVariable<MLIL_FORCE_VER_SSA>(), loc);
1731+
return dest->ForceVerSSA(size, GetDestSSAVariable<MLIL_FORCE_VER_SSA>(), GetSourceSSAVariable<MLIL_FORCE_VER_SSA>(),
1732+
GetForceVersionReason<MLIL_FORCE_VER_SSA>(), loc);
17301733
case MLIL_ASSERT:
17311734
return dest->Assert(size, GetSourceVariable<MLIL_ASSERT>(), GetConstraint<MLIL_ASSERT>(), loc);
17321735
case MLIL_ASSERT_SSA:
@@ -2375,6 +2378,15 @@ MediumLevelILSSAVariableList MediumLevelILInstruction::GetSourceSSAVariables() c
23752378
}
23762379

23772380

2381+
BNForceVersionReason MediumLevelILInstruction::GetForceVersionReason() const
2382+
{
2383+
size_t operandIndex;
2384+
if (GetOperandIndexForUsage(ForceVersionReasonMediumLevelOperandUsage, operandIndex))
2385+
return (BNForceVersionReason)GetRawOperandAsInteger(operandIndex);
2386+
throw MediumLevelILInstructionAccessException();
2387+
}
2388+
2389+
23782390
vector<Variable> MediumLevelILCallInstruction::GetOutputVariables() const
23792391
{
23802392
vector<Variable> result;
@@ -2486,15 +2498,18 @@ ExprId MediumLevelILFunction::SetVarAliasedField(size_t size, const Variable& de
24862498
}
24872499

24882500

2489-
ExprId MediumLevelILFunction::ForceVer(size_t size, const Variable& dest, const Variable& src, const ILSourceLocation& loc)
2501+
ExprId MediumLevelILFunction::ForceVer(size_t size, const Variable& dest, const Variable& src,
2502+
BNForceVersionReason reason, const ILSourceLocation& loc)
24902503
{
2491-
return AddExprWithLocation(MLIL_FORCE_VER, loc, size, dest.ToIdentifier(), src.ToIdentifier());
2504+
return AddExprWithLocation(MLIL_FORCE_VER, loc, size, dest.ToIdentifier(), src.ToIdentifier(), reason);
24922505
}
24932506

24942507

2495-
ExprId MediumLevelILFunction::ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src, const ILSourceLocation& loc)
2508+
ExprId MediumLevelILFunction::ForceVerSSA(size_t size, const SSAVariable& dest, const SSAVariable& src,
2509+
BNForceVersionReason reason, const ILSourceLocation& loc)
24962510
{
2497-
return AddExprWithLocation(MLIL_FORCE_VER_SSA, loc, size, dest.var.ToIdentifier(), dest.version, src.var.ToIdentifier(), src.version);
2511+
return AddExprWithLocation(MLIL_FORCE_VER_SSA, loc, size, dest.var.ToIdentifier(), dest.version,
2512+
src.var.ToIdentifier(), src.version, reason);
24982513
}
24992514

25002515

mediumlevelilinstruction.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,8 @@ namespace BinaryNinja
105105
VariableListMediumLevelOperand,
106106
SSAVariableListMediumLevelOperand,
107107
ExprListMediumLevelOperand,
108-
ConstraintMediumLevelOperand
108+
ConstraintMediumLevelOperand,
109+
ForceVersionReasonMediumLevelOperand
109110
};
110111

111112
/*!
@@ -154,7 +155,8 @@ namespace BinaryNinja
154155
UntypedParameterSSAExprsMediumLevelOperandUsage,
155156
ParameterSSAMemoryVersionMediumLevelOperandUsage,
156157
SourceSSAVariablesMediumLevelOperandUsages,
157-
ConstraintMediumLevelOperandUsage
158+
ConstraintMediumLevelOperandUsage,
159+
ForceVersionReasonMediumLevelOperandUsage
158160
};
159161
} // namespace BinaryNinjaCore
160162

@@ -800,6 +802,11 @@ namespace BinaryNinja
800802
{
801803
return As<N>().GetConstraint();
802804
}
805+
template <BNMediumLevelILOperation N>
806+
BNForceVersionReason GetForceVersionReason() const
807+
{
808+
return As<N>().GetForceVersionReason();
809+
}
803810

804811
template <BNMediumLevelILOperation N>
805812
void SetDestSSAVersion(size_t version)
@@ -899,6 +906,7 @@ namespace BinaryNinja
899906
MediumLevelILInstructionList GetParameterExprs() const;
900907
MediumLevelILInstructionList GetSourceExprs() const;
901908
MediumLevelILSSAVariableList GetSourceSSAVariables() const;
909+
BNForceVersionReason GetForceVersionReason() const;
902910
};
903911

904912
/*!
@@ -1122,6 +1130,7 @@ namespace BinaryNinja
11221130
{
11231131
Variable GetDestVariable() const { return GetRawOperandAsVariable(0); }
11241132
Variable GetSourceVariable() const { return GetRawOperandAsVariable(1); }
1133+
BNForceVersionReason GetForceVersionReason() const { return (BNForceVersionReason)GetRawOperandAsInteger(2); }
11251134
};
11261135
template <>
11271136
struct MediumLevelILInstructionAccessor<MLIL_FORCE_VER_SSA> : public MediumLevelILInstructionBase
@@ -1130,6 +1139,7 @@ namespace BinaryNinja
11301139
void SetDestSSAVersion(size_t version) { UpdateRawOperand(1, version); }
11311140
SSAVariable GetSourceSSAVariable() const { return GetRawOperandAsSSAVariable(2); }
11321141
void SetSourceSSAVersion(size_t version) { UpdateRawOperand(3, version); }
1142+
BNForceVersionReason GetForceVersionReason() const { return (BNForceVersionReason)GetRawOperandAsInteger(4); }
11331143
};
11341144
template <>
11351145
struct MediumLevelILInstructionAccessor<MLIL_ASSERT> : public MediumLevelILInstructionBase

python/mediumlevelil.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
# Binary Ninja components
2929
from . import _binaryninjacore as core
30-
from .enums import MediumLevelILOperation, ILBranchDependence, DataFlowQueryOption, FunctionGraphType, DeadStoreElimination, ILInstructionAttribute, StringType
30+
from .enums import MediumLevelILOperation, ILBranchDependence, DataFlowQueryOption, FunctionGraphType, DeadStoreElimination, ILInstructionAttribute, StringType, ForceVersionReason
3131
from . import basicblock
3232
from . import function
3333
from . import types
@@ -3444,6 +3444,10 @@ def dest(self) -> variable.Variable:
34443444
def src(self) -> variable.Variable:
34453445
return self._get_var(1)
34463446

3447+
@property
3448+
def reason(self) -> ForceVersionReason:
3449+
return ForceVersionReason(self._get_int(2))
3450+
34473451
@dataclass(frozen=True, repr=False, eq=False)
34483452
class MediumLevelILForceVerSsa(MediumLevelILInstruction, SSA):
34493453
@property
@@ -3454,6 +3458,10 @@ def dest(self) -> SSAVariable:
34543458
def src(self) -> SSAVariable:
34553459
return self._get_var_ssa(2, 3)
34563460

3461+
@property
3462+
def reason(self) -> ForceVersionReason:
3463+
return ForceVersionReason(self._get_int(4))
3464+
34573465
@dataclass(frozen=True, repr=False, eq=False)
34583466
class MediumLevelILBlockToExpand(MediumLevelILInstruction):
34593467
@property
@@ -4117,7 +4125,7 @@ def do_copy(
41174125
return dest.var_output_field(expr.size, expr.dest, expr.offset, loc)
41184126
if expr.operation == MediumLevelILOperation.MLIL_FORCE_VER:
41194127
expr: MediumLevelILForceVer
4120-
return dest.force_ver(expr.size, expr.dest, expr.src, loc)
4128+
return dest.force_ver(expr.size, expr.dest, expr.src, expr.reason, loc)
41214129
if expr.operation == MediumLevelILOperation.MLIL_ASSERT:
41224130
expr: MediumLevelILAssert
41234131
return dest.assert_expr(expr.size, expr.src, expr.constraint, loc)
@@ -4796,6 +4804,7 @@ def force_ver(
47964804
size: int,
47974805
dest: 'variable.CoreVariable',
47984806
src: 'variable.CoreVariable',
4807+
reason: ForceVersionReason = ForceVersionReason.UserForceVersionReason,
47994808
loc: Optional['ILSourceLocation'] = None
48004809
) -> ExpressionIndex:
48014810
"""
@@ -4806,11 +4815,12 @@ def force_ver(
48064815
:param int size: size of the variable
48074816
:param Variable dest: the variable to force a new version of
48084817
:param Variable src: the variable created with the new version
4818+
:param ForceVersionReason reason: reason for forcing the version
48094819
:param ILSourceLocation loc: location of returned expression
48104820
:return: The expression ``FORCE_VER(reg)``
48114821
:rtype: ExpressionIndex
48124822
"""
4813-
return self.expr(MediumLevelILOperation.MLIL_FORCE_VER, dest.identifier, src.identifier, size=size, source_location=loc)
4823+
return self.expr(MediumLevelILOperation.MLIL_FORCE_VER, dest.identifier, src.identifier, int(reason), size=size, source_location=loc)
48144824

48154825
def address_of(self, var: 'variable.CoreVariable', loc: Optional['ILSourceLocation'] = None) -> ExpressionIndex:
48164826
"""

0 commit comments

Comments
 (0)