Skip to content

Commit

Permalink
Add special treatment for external codeblocks.
Browse files Browse the repository at this point in the history
- Support zero-sized codeblocks
- Add a flag to ignore external blocks in flowchart generation.
  • Loading branch information
tmr232 committed Sep 2, 2019
1 parent 421f221 commit 83cb255
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions sark/codeblock.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,14 @@ def __eq__(self, other):


class FlowChart(idaapi.FlowChart):
def __init__(self, f=None, bounds=None, flags=idaapi.FC_PREDS):
def __init__(self, f=None, bounds=None, flags=idaapi.FC_PREDS, ignore_external=False):
if f is None and bounds is None:
f = idaapi.get_screen_ea()
if f is not None:
f = get_func(f)
if ignore_external:
flags |= idaapi.FC_NOEXT

super(FlowChart, self).__init__(f=f, bounds=bounds, flags=flags)

def _getitem(self, index):
Expand All @@ -88,6 +91,8 @@ def get_codeblock(ea=None):
ea = idaapi.get_screen_ea()
flowchart_ = get_flowchart(ea)
for code_block in flowchart_:
if code_block.startEA == ea: # External blocks can be zero-sized.
return code_block
if code_block.startEA <= ea < code_block.endEA:
return code_block

Expand All @@ -97,11 +102,11 @@ def get_block_start(ea):
return get_codeblock(ea).startEA


def get_nx_graph(ea):
def get_nx_graph(ea, ignore_external=False):
"""Convert an IDA flowchart to a NetworkX graph."""
nx_graph = networkx.DiGraph()
func = idaapi.get_func(ea)
flowchart = FlowChart(func)
flowchart = FlowChart(func, ignore_external=ignore_external)
for block in flowchart:
# Make sure all nodes are added (including edge-less nodes)
nx_graph.add_node(block.startEA)
Expand Down

0 comments on commit 83cb255

Please sign in to comment.