Skip to content

Commit 5b575ff

Browse files
committed
Various improvements for guided disassembly mode.
1 parent 33e9108 commit 5b575ff

File tree

6 files changed

+50
-9
lines changed

6 files changed

+50
-9
lines changed

binaryninjaapi.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8178,9 +8178,10 @@ namespace BinaryNinja {
81788178
BasicBlockAnalysisContext(BNBasicBlockAnalysisContext* context);
81798179

81808180
BNFunctionAnalysisSkipOverride GetAnalysisSkipOverride() const { return m_context->analysisSkipOverride; }
8181+
bool GetGuidedAnalysisMode() const { return m_context->guidedAnalysisMode; }
8182+
bool GetTriggerGuidedOnInvalidInstruction() const { return m_context->triggerGuidedOnInvalidInstruction; }
81818183
bool GetTranslateTailCalls() const { return m_context->translateTailCalls; }
81828184
bool GetDisallowBranchToString() const { return m_context->disallowBranchToString; }
8183-
bool GetHaltOnInvalidInstructions() const { return m_context->haltOnInvalidInstructions; }
81848185
uint64_t GetMaxFunctionSize() const { return m_context->maxFunctionSize; }
81858186

81868187
bool GetMaxSizeReached() const { return m_context->maxSizeReached; }
@@ -11523,6 +11524,7 @@ namespace BinaryNinja {
1152311524
void SetGuidedSourceBlocks(const std::vector<ArchAndAddr>& addresses);
1152411525
void AddGuidedSourceBlocks(const std::vector<ArchAndAddr>& addresses);
1152511526
void RemoveGuidedSourceBlocks(const std::vector<ArchAndAddr>& addresses);
11527+
bool IsGuidedSourceBlock(Architecture* arch, uint64_t addr) const;
1152611528
std::vector<ArchAndAddr> GetGuidedSourceBlocks();
1152711529

1152811530
std::vector<IndirectBranchInfo> GetIndirectBranches();

binaryninjacore.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@
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 120
40+
#define BN_CURRENT_CORE_ABI_VERSION 121
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
4444
// will require rebuilding. The minimum version is increased when there are
4545
// incompatible changes that break binary compatibility, such as changes to
4646
// existing types or functions.
47-
#define BN_MINIMUM_CORE_ABI_VERSION 117
47+
#define BN_MINIMUM_CORE_ABI_VERSION 121
4848

4949
#ifdef __GNUC__
5050
#ifdef BINARYNINJACORE_LIBRARY
@@ -1872,9 +1872,10 @@ extern "C"
18721872

18731873
// IN
18741874
BNFunctionAnalysisSkipOverride analysisSkipOverride;
1875+
bool guidedAnalysisMode;
1876+
bool triggerGuidedOnInvalidInstruction;
18751877
bool translateTailCalls;
18761878
bool disallowBranchToString;
1877-
bool haltOnInvalidInstructions;
18781879
uint64_t maxFunctionSize;
18791880

18801881
size_t indirectBranchesCount;
@@ -5154,6 +5155,7 @@ extern "C"
51545155
BINARYNINJACOREAPI void BNSetGuidedSourceBlocks(BNFunction* func, BNArchitectureAndAddress* addresses, size_t count);
51555156
BINARYNINJACOREAPI void BNAddGuidedSourceBlocks(BNFunction* func, BNArchitectureAndAddress* addresses, size_t count);
51565157
BINARYNINJACOREAPI void BNRemoveGuidedSourceBlocks(BNFunction* func, BNArchitectureAndAddress* addresses, size_t count);
5158+
BINARYNINJACOREAPI bool BNIsGuidedSourceBlock(BNFunction* func, BNArchitecture* arch, uint64_t addr);
51575159
BINARYNINJACOREAPI BNArchitectureAndAddress* BNGetGuidedSourceBlocks(BNFunction* func, size_t* count);
51585160
BINARYNINJACOREAPI void BNFreeArchitectureAndAddressList(BNArchitectureAndAddress* addresses);
51595161

defaultabb.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
6363
map<ArchAndAddr, Ref<BasicBlock>> instrBlocks;
6464
set<ArchAndAddr> seenBlocks;
6565

66+
bool guidedAnalysisMode = context.GetGuidedAnalysisMode();
67+
bool triggerGuidedOnInvalidInstruction = context.GetTriggerGuidedOnInvalidInstruction();
6668
bool translateTailCalls = context.GetTranslateTailCalls();
6769
bool disallowBranchToString = context.GetDisallowBranchToString();
68-
bool haltOnInvalidInstructions = context.GetHaltOnInvalidInstructions();
6970

7071
auto& indirectBranches = context.GetIndirectBranches();
7172
auto& indirectNoReturnCalls = context.GetIndirectNoReturnCalls();
@@ -674,10 +675,10 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function* function, BasicBlockAnaly
674675
if (maxSizeReached)
675676
break;
676677

677-
if (haltOnInvalidInstructions && block->HasInvalidInstructions())
678+
if (triggerGuidedOnInvalidInstruction && block->HasInvalidInstructions())
678679
hasInvalidInstructions = true;
679680

680-
if (hasInvalidInstructions)
681+
if (guidedAnalysisMode || hasInvalidInstructions || guidedSourceBlocksSet.size())
681682
{
682683
queue<ArchAndAddr> guidedBlocksToProcess;
683684
while (!blocksToProcess.empty())

function.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,6 +1780,12 @@ void Function::RemoveGuidedSourceBlocks(const std::vector<ArchAndAddr>& addresse
17801780
}
17811781

17821782

1783+
bool Function::IsGuidedSourceBlock(Architecture* arch, uint64_t addr) const
1784+
{
1785+
return BNIsGuidedSourceBlock(m_object, arch->GetObject(), addr);
1786+
}
1787+
1788+
17831789
std::vector<ArchAndAddr> Function::GetGuidedSourceBlocks()
17841790
{
17851791
size_t count;

python/architecture.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ class BasicBlockAnalysisContext:
8181
_indirect_branches: List["variable.IndirectBranchInfo"]
8282
_indirect_no_return_calls: Set["function.ArchAndAddr"]
8383
_analysis_skip_override: core.FunctionAnalysisSkipOverride
84+
_guided_analysis_mode: bool
85+
_trigger_guided_on_invalid_instruction: bool
8486
_translate_tail_calls: bool
8587
_disallow_branch_to_string: bool
8688
_max_function_size: int
87-
_halt_on_invalid_instruction: bool
8889
_max_size_reached: bool
8990

9091
# In/Out
@@ -157,10 +158,11 @@ def from_core_struct(bn_bb_context: core.BNBasicBlockAnalysisContext) -> "BasicB
157158
_indirect_branches=indirect_branches,
158159
_indirect_no_return_calls=indirect_no_return_calls,
159160
_analysis_skip_override=bn_bb_context.analysisSkipOverride,
161+
_guided_analysis_mode=bn_bb_context.guidedAnalysisMode,
162+
_trigger_guided_on_invalid_instruction=bn_bb_context.triggerGuidedOnInvalidInstruction,
160163
_translate_tail_calls=bn_bb_context.translateTailCalls,
161164
_disallow_branch_to_string=bn_bb_context.disallowBranchToString,
162165
_max_function_size=bn_bb_context.maxFunctionSize,
163-
_halt_on_invalid_instruction=bn_bb_context.haltOnInvalidInstructions,
164166
_max_size_reached=bn_bb_context.maxSizeReached,
165167
_contextual_returns=contextual_returns,
166168
_contextual_returns_dirty=False,
@@ -187,6 +189,18 @@ def analysis_skip_override(self) -> core.FunctionAnalysisSkipOverride:
187189

188190
return self._analysis_skip_override
189191

192+
@property
193+
def guided_analysis_mode(self) -> bool:
194+
"""Get the setting that determines if functions start in guided analysis mode."""
195+
196+
return self._guided_analysis_mode
197+
198+
@property
199+
def trigger_guided_on_invalid_instruction(self) -> bool:
200+
"""Get the setting that determines if guided mode should be triggered on invalid instructions."""
201+
202+
return self._trigger_guided_on_invalid_instruction
203+
190204
@property
191205
def translate_tail_calls(self) -> bool:
192206
"""Get setting from context that determines if tail calls should be translated."""

python/function.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,22 @@ def remove_guided_source_blocks(
23262326
address_list[i].address = addresses[i][1]
23272327
core.BNRemoveGuidedSourceBlocks(self.handle, address_list, len(addresses))
23282328

2329+
def is_guided_source_block(
2330+
self, arch: 'architecture.Architecture', addr: int
2331+
) -> bool:
2332+
"""
2333+
``is_guided_source_block`` checks if the given address is a guided source block.
2334+
2335+
:param architecture.Architecture arch: Architecture of the address to check
2336+
:param int addr: Address to check
2337+
:rtype: bool
2338+
:Example:
2339+
2340+
>>> current_function.is_guided_source_block(arch, 0x400000)
2341+
True
2342+
"""
2343+
return core.BNIsGuidedSourceBlock(self.handle, arch.handle, addr)
2344+
23292345
def get_guided_source_blocks(
23302346
self
23312347
) -> List[Tuple['architecture.Architecture', int]]:

0 commit comments

Comments
 (0)