11# -*- coding: UTF-8 -*-
2- try :
3- import angr
4- import pygraphviz as pgv
5- import networkx as nx
6- _IMP = True
7- except ImportError :
8- _IMP = False
92import matplotlib .pyplot as plt
103
114from .__common__ import Binary , CACHE_DIR , COLORS , MIN_ZONE_WIDTH
12- from ..__conf__ import save_figure
5+ from ..__conf__ import check_imports , save_figure
136
7+ try :
8+ # dirty fix to known issue with angr: AttributeError: module 'unicorn' has no attribute 'UC_ARCH_RISCV'.
9+ __import__ ("unicorn" ).UC_ARCH_RISCV = 8
10+ except ModuleNotFoundError : # pragma: no cover
11+ pass # 'unicorn' is an optional dependency of 'angr' ; fix it only if it is already installed
12+
13+ check_imports ("angr" , "networkx" , "pygraphviz" )
1414
1515_DEFAULT_ALGORITHM , _DEFAULT_ENGINE = "fast" , "default"
1616_ENGINES = ["default" , "pcode" , "vex" ]
@@ -28,6 +28,9 @@ def arguments(parser):
2828@save_figure
2929def plot (executable , algorithm = _DEFAULT_ALGORITHM , engine = _DEFAULT_ENGINE , ** kwargs ):
3030 """ plot the Control Flow Graph (CFG) of an executable """
31+ import angr
32+ import networkx as nx
33+ import pygraphviz as pgv
3134 from math import ceil , log2
3235 engine = {k : getattr (angr .engines , "UberEngine" if k != "pcode" else f"UberEngine{ k .capitalize ()} " ) \
3336 for k in _ENGINES }[engine ]
@@ -38,7 +41,7 @@ def plot(executable, algorithm=_DEFAULT_ALGORITHM, engine=_DEFAULT_ENGINE, **kwa
3841 labels [node ] = f"{ node .name } \n 0x{ node .addr :x} " if hasattr (node , "name" ) and node .name else f"0x{ node .addr :x} "
3942 node_colors .append ("red" if node .function_address == node .addr else "lightblue" )
4043 n = max (10 , min (30 , ceil (log2 (n_nodes := len (cfg .graph .nodes ()) + 1 ) * 2 )))
41- plt .figure (figsize = (n , n ))
42- nx .draw (cfg .graph , nx .kamada_kawai_layout (cfg .graph ), font_size = 8 , with_labels = True , labels = labels ,
44+ fig = plt .figure (figsize = (n , n ))
45+ nx .draw (cfg .graph , nx .kamada_kawai_layout (cfg .graph ), fig . gca (), font_size = 8 , with_labels = True , labels = labels ,
4346 node_size = max (300 , 15000 // n_nodes ), node_color = node_colors )
4447
0 commit comments