Skip to content

Commit 37d52c9

Browse files
committed
Add UI action and API to flip conditions during HLIL restructuring
1 parent 8ef932b commit 37d52c9

File tree

7 files changed

+38
-1
lines changed

7 files changed

+38
-1
lines changed

binaryninjaapi.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11430,6 +11430,9 @@ namespace BinaryNinja {
1143011430
BNExprFolding GetExprFolding(uint64_t addr);
1143111431
void SetExprFolding(uint64_t addr, BNExprFolding mode);
1143211432

11433+
bool IsConditionInverted(uint64_t addr);
11434+
void SetConditionInverted(uint64_t addr, bool invert);
11435+
1143311436
std::map<Variable, std::set<Variable>> GetMergedVariables();
1143411437
void MergeVariables(const Variable& target, const std::set<Variable>& sources);
1143511438
void UnmergeVariables(const Variable& target, const std::set<Variable>& sources);

binaryninjacore.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 108
40+
#define BN_CURRENT_CORE_ABI_VERSION 109
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -1024,6 +1024,9 @@ extern "C"
10241024

10251025
// HLIL expression can be folded into other expressions or has been folded
10261026
HLILFoldableExpr = 0x100,
1027+
1028+
// HLIL condition can be displayed as the inverse
1029+
HLILInvertableCondition = 0x200,
10271030
} BNILInstructionAttribute;
10281031

10291032
typedef enum BNIntrinsicClass
@@ -4993,6 +4996,8 @@ extern "C"
49934996
BNFunction* func, const BNVariable* var, BNDeadStoreElimination mode);
49944997
BINARYNINJACOREAPI BNExprFolding BNGetExprFolding(BNFunction* func, uint64_t addr);
49954998
BINARYNINJACOREAPI void BNSetExprFolding(BNFunction* func, uint64_t addr, BNExprFolding mode);
4999+
BINARYNINJACOREAPI bool BNIsConditionInverted(BNFunction* func, uint64_t addr);
5000+
BINARYNINJACOREAPI void BNSetConditionInverted(BNFunction* func, uint64_t addr, bool invert);
49965001
BINARYNINJACOREAPI BNMergedVariable* BNGetMergedVariables(BNFunction* func, size_t* count);
49975002
BINARYNINJACOREAPI void BNFreeMergedVariableList(BNMergedVariable* vars, size_t count);
49985003
BINARYNINJACOREAPI void BNMergeVariables(BNFunction* func, const BNVariable* target, const BNVariable* sources,

function.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,6 +2825,18 @@ void Function::SetExprFolding(uint64_t addr, BNExprFolding mode)
28252825
}
28262826

28272827

2828+
bool Function::IsConditionInverted(uint64_t addr)
2829+
{
2830+
return BNIsConditionInverted(m_object, addr);
2831+
}
2832+
2833+
2834+
void Function::SetConditionInverted(uint64_t addr, bool invert)
2835+
{
2836+
BNSetConditionInverted(m_object, addr, invert);
2837+
}
2838+
2839+
28282840
std::map<Variable, std::set<Variable>> Function::GetMergedVariables()
28292841
{
28302842
size_t count;

python/function.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3466,6 +3466,16 @@ def set_expr_folding(self, addr: Union[int, highlevelil.HighLevelILInstruction],
34663466
addr = addr.address
34673467
core.BNSetExprFolding(self.handle, addr, value)
34683468

3469+
def is_condition_inverted(self, addr: Union[int, highlevelil.HighLevelILInstruction]) -> bool:
3470+
if isinstance(addr, highlevelil.HighLevelILInstruction):
3471+
addr = addr.address
3472+
return core.BNIsConditionInverted(self.handle, addr)
3473+
3474+
def set_condition_inverted(self, addr: Union[int, highlevelil.HighLevelILInstruction], invert: bool):
3475+
if isinstance(addr, highlevelil.HighLevelILInstruction):
3476+
addr = addr.address
3477+
core.BNSetConditionInverted(self.handle, addr, invert)
3478+
34693479

34703480
class AdvancedFunctionAnalysisDataRequestor:
34713481
def __init__(self, func: Optional['Function'] = None):

ui/commands.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ TypeRef GetFunctionType(BinaryViewRef data, TypeRef type);
9797

9898
std::optional<uint64_t> getFoldableExprAddress(
9999
BinaryNinja::HighLevelILFunction* hlil, const HighlightTokenState& highlight);
100+
std::optional<uint64_t> getInvertableConditionAddress(BinaryNinja::HighLevelILFunction* hlil, size_t instrIndex);
100101

101102
/*!
102103
@}

ui/flowgraphwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class BINARYNINJAUIAPI FlowGraphWidget :
168168
BNDeadStoreElimination getCurrentVariableDeadStoreElimination();
169169
std::optional<uint64_t> getCurrentFoldableExprAddress();
170170
BNExprFolding getCurrentExprFolding();
171+
std::optional<uint64_t> getCurrentInvertableConditionAddress();
172+
bool getCurrentConditionInverted();
171173
std::optional<std::pair<BinaryNinja::Variable, BinaryNinja::Variable>> getMergeVariablesAtCurrentLocation();
172174

173175
protected:
@@ -398,6 +400,7 @@ class BINARYNINJAUIAPI FlowGraphWidget :
398400

399401
void setCurrentVariableDeadStoreElimination(BNDeadStoreElimination elimination);
400402
void setCurrentExprFolding(BNExprFolding folding);
403+
void toggleConditionInverted();
401404
void splitToNewTabAndNavigateFromCursorPosition();
402405
void splitToNewWindowAndNavigateFromCursorPosition();
403406
void splitToNewPaneAndNavigateFromCursorPosition();

ui/linearview.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,8 @@ class BINARYNINJAUIAPI LinearView : public QAbstractScrollArea, public View, pub
318318
BNDeadStoreElimination getCurrentVariableDeadStoreElimination();
319319
std::optional<uint64_t> getCurrentFoldableExprAddress();
320320
BNExprFolding getCurrentExprFolding();
321+
std::optional<uint64_t> getCurrentInvertableConditionAddress();
322+
bool getCurrentConditionInverted();
321323

322324
void setDataButtonVisible(bool visible);
323325
std::optional<std::pair<BinaryNinja::Variable, BinaryNinja::Variable>> getMergeVariablesAtCurrentLocation();
@@ -409,6 +411,7 @@ private Q_SLOTS:
409411

410412
void setCurrentVariableDeadStoreElimination(BNDeadStoreElimination elimination);
411413
void setCurrentExprFolding(BNExprFolding folding);
414+
void toggleConditionInverted();
412415

413416
Q_SIGNALS:
414417
void notifyResizeEvent(int width, int height);

0 commit comments

Comments
 (0)