Skip to content

Commit cd3028d

Browse files
committed
Use default ABB from Python plugins
1 parent 3f82d20 commit cd3028d

File tree

2 files changed

+35
-9
lines changed

2 files changed

+35
-9
lines changed

defaultabb.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
#include <map>
2+
#include <set>
3+
#include <queue>
14
#include <inttypes.h>
25
#include "binaryninjaapi.h"
36
#include "binaryninjacore.h"
@@ -235,7 +238,8 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function& function, BNBasicBlockAna
235238
size_t maxLen = data->Read(opcode, location.address, location.arch->GetMaxInstructionLength());
236239
if (maxLen == 0)
237240
{
238-
//string text = fmt::bnformat("Could not read instruction at {:#x}", location.address);
241+
string text = fmt::format("Could not read instruction at {:#x}", location.address);
242+
function.CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
239243
if (location.arch->GetInstructionAlignment() == 0)
240244
location.address++;
241245
else
@@ -248,8 +252,8 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function& function, BNBasicBlockAna
248252
info.delaySlots = delaySlotCount;
249253
if (!location.arch->GetInstructionInfo(opcode, location.address, maxLen, info))
250254
{
251-
//string text = fmt::bnformat("Could not get instruction info at {:#x}", location.address);
252-
//function->CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
255+
string text = fmt::format("Could not get instruction info at {:#x}", location.address);
256+
function.CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
253257
if (location.arch->GetInstructionAlignment() == 0)
254258
location.address++;
255259
else
@@ -261,8 +265,8 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function& function, BNBasicBlockAna
261265
// The instruction is invalid if it has no length or is above maximum length
262266
if ((info.length == 0) || (info.length > maxLen))
263267
{
264-
//string text = fmt::bnformat("Instruction of invalid length at {:#x}", location.address);
265-
//function->CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
268+
string text = fmt::format("Instruction of invalid length at {:#x}", location.address);
269+
function.CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
266270
if (location.arch->GetInstructionAlignment() == 0)
267271
location.address++;
268272
else
@@ -278,8 +282,8 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function& function, BNBasicBlockAna
278282
((!IsOffsetCodeSemanticsFast(data, readOnlySections, dataExternSections, instrEnd) && IsOffsetCodeSemanticsFast(data, readOnlySections, dataExternSections,location.address)) ||
279283
(!data->IsOffsetBackedByFile(instrEnd) && data->IsOffsetBackedByFile(location.address))))
280284
{
281-
//string text = fmt::bnformat("Instruction at {:#x} straddles a non-code section", location.address);
282-
//function->CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
285+
string text = fmt::format("Instruction at {:#x} straddles a non-code section", location.address);
286+
function.CreateAutoAddressTag(location.arch, location.address, "Invalid Instruction", text, true);
283287
if (location.arch->GetInstructionAlignment() == 0)
284288
location.address++;
285289
else
@@ -420,8 +424,8 @@ void Architecture::DefaultAnalyzeBasicBlocks(Function& function, BNBasicBlockAna
420424
if (!fastPath && !IsOffsetCodeSemanticsFast(data, readOnlySections, dataExternSections, target.address) &&
421425
IsOffsetCodeSemanticsFast(data, readOnlySections, dataExternSections, location.address))
422426
{
423-
//string message = fmt::bnformat("Non-code call target {:#x}", target.address);
424-
//function->CreateAutoAddressTag(target.arch, location.address, "Non-code Branch", message, true);
427+
string message = fmt::format("Non-code call target {:#x}", target.address);
428+
function.CreateAutoAddressTag(target.arch, location.address, "Non-code Branch", message, true);
425429
break;
426430
}
427431

python/architecture.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ def __init__(self):
246246
self._cb.getInstructionLowLevelIL = self._cb.getInstructionLowLevelIL.__class__(
247247
self._get_instruction_low_level_il
248248
)
249+
self._cb.analyzeBasicBlocks = self._cb.analyzeBasicBlocks.__class__(self._analyze_basic_blocks)
249250
self._cb.getRegisterName = self._cb.getRegisterName.__class__(self._get_register_name)
250251
self._cb.getFlagName = self._cb.getFlagName.__class__(self._get_flag_name)
251252
self._cb.getFlagWriteTypeName = self._cb.getFlagWriteTypeName.__class__(self._get_flag_write_type_name)
@@ -710,6 +711,12 @@ def _get_instruction_low_level_il(self, ctxt, data, addr, length, il):
710711
log_error(traceback.format_exc())
711712
return False
712713

714+
def _analyze_basic_blocks(self, ctx, func, context):
715+
try:
716+
self.analyze_basic_blocks(function.Function(handle=core.BNNewFunctionReference(func)), context)
717+
except:
718+
log_error(traceback.format_exc())
719+
713720
def _get_register_name(self, ctxt, reg):
714721
try:
715722
if reg in self._regs_by_index:
@@ -1425,6 +1432,21 @@ def get_instruction_low_level_il(self, data: bytes, addr: int, il: lowlevelil.Lo
14251432
"""
14261433
raise NotImplementedError
14271434

1435+
def analyze_basic_blocks(self, func, context):
1436+
"""
1437+
``analyze_basic_blocks`` performs function-level basic block recovery and commits the blocks to analysis
1438+
1439+
.. note:: Architecture subclasses should not implement this method unless function-level lifting is required
1440+
1441+
:param Function func: the function to analyze
1442+
:param BNBasicBlockAnalysisContext context: the analysis context
1443+
"""
1444+
1445+
try:
1446+
core.BNArchitectureDefaultAnalyzeBasicBlocks(func.handle, context)
1447+
except:
1448+
log_error(traceback.format_exc())
1449+
14281450
def get_low_level_il_from_bytes(self, data: bytes, addr: int) -> 'lowlevelil.LowLevelILInstruction':
14291451
"""
14301452
``get_low_level_il_from_bytes`` converts the instruction in bytes to ``il`` at the given virtual address

0 commit comments

Comments
 (0)