<s> import nose <EOL> import angr <EOL> import time <EOL> import pickle <EOL> import networkx <EOL> import simuvex <EOL> import logging <EOL> l = logging . getLogger ( "<STR_LIT>" ) <EOL> import os <EOL> test_location = str ( os . path . join ( os . path . dirname ( os . path . realpath ( __file__ ) ) , '<STR_LIT>' ) ) <EOL> def compare_cfg ( standard , g , function_list ) : <EOL> """<STR_LIT>""" <EOL> def get_function_name ( addr ) : <EOL> start = <NUM_LIT:0> <EOL> end = len ( function_list ) - <NUM_LIT:1> <EOL> while start <= end : <EOL> mid = ( start + end ) / <NUM_LIT:2> <EOL> f = function_list [ mid ] <EOL> if addr < f [ '<STR_LIT:start>' ] : <EOL> end = mid - <NUM_LIT:1> <EOL> elif addr > f [ '<STR_LIT:end>' ] : <EOL> start = mid + <NUM_LIT:1> <EOL> else : <EOL> return f [ '<STR_LIT:name>' ] <EOL> return None <EOL> function_list = sorted ( function_list , key = lambda x : x [ '<STR_LIT:start>' ] ) <EOL> s_graph = networkx . DiGraph ( ) <EOL> all_nodes = sorted ( standard . nodes ( ) ) <EOL> addr_to_basicblock = { } <EOL> last_basicblock = None <EOL> for n in all_nodes : <EOL> if last_basicblock is None : <EOL> last_basicblock = ( n , n ) <EOL> block = last_basicblock <EOL> successors = standard . successors ( n ) <EOL> if len ( successors ) == <NUM_LIT:1> and successors [ <NUM_LIT:0> ] >= block [ <NUM_LIT:0> ] : <EOL> last_basicblock = ( block [ <NUM_LIT:0> ] , successors [ <NUM_LIT:0> ] ) <EOL> else : <EOL> addr_to_basicblock [ block [ <NUM_LIT:0> ] ] = block <EOL> for s in successors : <EOL> s_graph . add_edge ( block [ <NUM_LIT:0> ] , s ) <EOL> last_basicblock = None <EOL> graph = networkx . DiGraph ( ) <EOL> for src , dst in g . edges ( ) : <EOL> graph . add_edge ( src . addr , dst . addr ) <EOL> for src , dst in s_graph . edges ( ) : <EOL> if graph . has_edge ( src , dst ) : <EOL> continue <EOL> else : <EOL> l . error ( "<STR_LIT>" , get_function_name ( src ) , src , get_function_name ( dst ) , dst ) <EOL> for src , dst in graph . edges ( ) : <EOL> if s_graph . has_edge ( src , dst ) : <EOL> continue <EOL> else : <EOL> l . error ( "<STR_LIT>" , get_function_name ( src ) , src , get_function_name ( dst ) , dst ) <EOL> def perform_single ( binary_path , cfg_path = None ) : <EOL> proj = angr . Project ( binary_path , <EOL> use_sim_procedures = True , <EOL> default_analysis_mode = '<STR_LIT>' , <EOL> load_options = { '<STR_LIT>' : False } ) <EOL> start = time . time ( ) <EOL> cfg = proj . analyses . CFG ( context_sensitivity_level = <NUM_LIT:1> ) <EOL> end = time . time ( ) <EOL> duration = end - start <EOL> bbl_dict = cfg . get_bbl_dict ( ) <EOL> l . info ( "<STR_LIT>" , duration ) <EOL> l . info ( "<STR_LIT>" , len ( bbl_dict ) ) <EOL> if cfg_path is not None and os . path . isfile ( cfg_path ) : <EOL> info = pickle . load ( open ( cfg_path , "<STR_LIT:rb>" ) ) <EOL> standard = info [ '<STR_LIT>' ] <EOL> functions = info [ '<STR_LIT>' ] <EOL> graph = cfg . graph <EOL> compare_cfg ( standard , graph , functions ) <EOL> else : <EOL> l . warning ( "<STR_LIT>" ) <EOL> def test_cfg_0 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def test_cfg_1 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def test_cfg_2 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def test_cfg_3 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def disabled_cfg_4 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def test_additional_edges ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> proj = angr . Project ( binary_path , <EOL> use_sim_procedures = True , <EOL> default_analysis_mode = '<STR_LIT>' , <EOL> load_options = { '<STR_LIT>' : False } ) <EOL> additional_edges = { <EOL> <NUM_LIT> : [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> } <EOL> cfg = proj . analyses . CFG ( context_sensitivity_level = <NUM_LIT:0> , additional_edges = additional_edges ) <EOL> nose . tools . assert_not_equal ( cfg . get_any_node ( <NUM_LIT> ) , None ) <EOL> nose . tools . assert_not_equal ( cfg . get_any_node ( <NUM_LIT> ) , None ) <EOL> nose . tools . assert_not_equal ( cfg . get_any_node ( <NUM_LIT> ) , None ) <EOL> nose . tools . assert_equal ( cfg . get_any_node ( <NUM_LIT> ) , None ) <EOL> def test_not_returning ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> proj = angr . Project ( binary_path , <EOL> use_sim_procedures = True , <EOL> load_options = { '<STR_LIT>' : False } <EOL> ) <EOL> proj . analyses . CFG ( context_sensitivity_level = <NUM_LIT:0> ) <EOL> nose . tools . assert_not_equal ( proj . kb . functions . function ( name = '<STR_LIT>' ) , None ) <EOL> nose . tools . assert_true ( proj . kb . functions . function ( name = '<STR_LIT>' ) . returning ) <EOL> nose . tools . assert_not_equal ( proj . kb . functions . function ( name = '<STR_LIT>' ) , None ) <EOL> nose . tools . assert_false ( proj . kb . functions . function ( name = '<STR_LIT>' ) . returning ) <EOL> nose . tools . assert_not_equal ( proj . kb . functions . function ( name = '<STR_LIT>' ) , None ) <EOL> nose . tools . assert_false ( proj . kb . functions . function ( name = '<STR_LIT>' ) . returning ) <EOL> nose . tools . assert_not_equal ( proj . kb . functions . function ( name = '<STR_LIT>' ) , None ) <EOL> nose . tools . assert_false ( proj . kb . functions . function ( name = '<STR_LIT>' ) . returning ) <EOL> nose . tools . assert_equal ( proj . kb . functions . function ( name = '<STR_LIT>' ) , None ) <EOL> def disabled_cfg_5 ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def test_cfg_6 ( ) : <EOL> simuvex . o . modes [ '<STR_LIT>' ] |= { simuvex . s_options . DO_CCALLS } <EOL> binary_path = test_location + "<STR_LIT>" <EOL> proj = angr . Project ( binary_path , <EOL> use_sim_procedures = True , <EOL> load_options = { '<STR_LIT>' : False } ) <EOL> proj . analyses . CFG ( context_sensitivity_level = <NUM_LIT:1> ) <EOL> nose . tools . assert_greater_equal ( len ( proj . kb . functions ) , <NUM_LIT> ) <EOL> simuvex . o . modes [ '<STR_LIT>' ] ^= { simuvex . s_options . DO_CCALLS } <EOL> def test_fauxware ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> cfg_path = binary_path + "<STR_LIT>" <EOL> perform_single ( binary_path , cfg_path ) <EOL> def disabled_loop_unrolling ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> p = angr . Project ( binary_path ) <EOL> cfg = p . analyses . CFG ( ) <EOL> cfg . normalize ( ) <EOL> cfg . unroll_loops ( <NUM_LIT:5> ) <EOL> nose . tools . assert_equal ( len ( cfg . get_all_nodes ( <NUM_LIT> ) ) , <NUM_LIT:7> ) <EOL> def test_thumb_mode ( ) : <EOL> binary_path = test_location + "<STR_LIT>" <EOL> p = angr . Project ( binary_path ) <EOL> cfg = p . analyses . CFG ( ) <EOL> def check_addr ( a ) : <EOL> if a % <NUM_LIT:2> == <NUM_LIT:1> : <EOL> nose . tools . assert_true ( cfg . is_thumb_addr ( a ) ) <EOL> else : <EOL> nose . tools . assert_false ( cfg . is_thumb_addr ( a ) ) <EOL> cfg_node_addrs = [ n . addr for n in cfg . graph . nodes ( ) ] <EOL> for a in cfg_node_addrs : <EOL> check_addr ( a ) <EOL> for f_addr , f in p . kb . functions . items ( ) : <EOL> check_addr ( f_addr ) <EOL> if f . startpoint is not None : <EOL> check_addr ( f . startpoint . addr ) <EOL> def test_fakeret_edges_0 ( ) : <EOL> binary_path = os . path . join ( test_location , "<STR_LIT>" , "<STR_LIT>" ) <EOL> p = angr . Project ( binary_path ) <EOL> cfg = p . analyses . CFGAccurate ( context_sensitivity_level = <NUM_LIT:3> ) <EOL> putchar = cfg . functions . function ( name = "<STR_LIT>" ) <EOL> putchar_cfgnodes = cfg . get_all_nodes ( putchar . addr ) <EOL> nose . tools . assert_equal ( len ( putchar_cfgnodes ) , <NUM_LIT:2> ) <EOL> plt_entry_0 = cfg . get_predecessors ( putchar_cfgnodes [ <NUM_LIT:0> ] ) <EOL> nose . tools . assert_equal ( len ( plt_entry_0 ) , <NUM_LIT:1> ) <EOL> plt_entry_0 = plt_entry_0 [ <NUM_LIT:0> ] <EOL> plt_entry_1 = cfg . get_predecessors ( putchar_cfgnodes [ <NUM_LIT:1> ] ) <EOL> nose . tools . assert_equal ( len ( plt_entry_1 ) , <NUM_LIT:1> ) <EOL> plt_entry_1 = plt_entry_1 [ <NUM_LIT:0> ] <EOL> nose . tools . assert_true ( plt_entry_0 is not plt_entry_1 ) <EOL> preds_0 = cfg . get_predecessors ( plt_entry_0 ) <EOL> nose . tools . assert_equal ( len ( preds_0 ) , <NUM_LIT:1> ) <EOL> preds_1 = cfg . get_predecessors ( plt_entry_1 ) <EOL> nose . tools . assert_equal ( len ( preds_1 ) , <NUM_LIT:1> ) <EOL> edges_0 = cfg . get_successors_and_jumpkind ( preds_0 [ <NUM_LIT:0> ] , excluding_fakeret = False ) <EOL> nose . tools . assert_equal ( len ( edges_0 ) , <NUM_LIT:2> ) <EOL> jumpkinds = set ( [ jumpkind for _ , jumpkind in edges_0 ] ) <EOL> nose . tools . assert_set_equal ( jumpkinds , { '<STR_LIT>' , '<STR_LIT>' } ) <EOL> edges_1 = cfg . get_successors_and_jumpkind ( preds_1 [ <NUM_LIT:0> ] , excluding_fakeret = False ) <EOL> nose . tools . assert_equal ( len ( edges_1 ) , <NUM_LIT:2> ) <EOL> jumpkinds = set ( [ jumpkind for _ , jumpkind in edges_1 ] ) <EOL> nose . tools . assert_set_equal ( jumpkinds , { '<STR_LIT>' , '<STR_LIT>' } ) <EOL> def test_string_references ( ) : <EOL> binary_path = os . path . join ( test_location , "<STR_LIT>" , "<STR_LIT>" ) <EOL> b = angr . Project ( binary_path , load_options = { '<STR_LIT>' : False } ) <EOL> cfg = b . analyses . CFG ( keep_state = True ) <EOL> string_references = [ ] <EOL> for f in cfg . functions . values ( ) : <EOL> string_references . append ( f . string_references ( ) ) <EOL> def run_all ( ) : <EOL> functions = globals ( ) <EOL> all_functions = dict ( filter ( ( lambda ( k , v ) : k . startswith ( '<STR_LIT>' ) ) , functions . items ( ) ) ) <EOL> for f in sorted ( all_functions . keys ( ) ) : <EOL> if hasattr ( all_functions [ f ] , '<STR_LIT>' ) : <EOL> print f <EOL> all_functions [ f ] ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> logging . getLogger ( "<STR_LIT>" ) . setLevel ( logging . DEBUG ) <EOL> logging . getLogger ( "<STR_LIT>" ) . setLevel ( logging . DEBUG ) <EOL> logging . getLogger ( "<STR_LIT>" ) . setLevel ( logging . DEBUG ) <EOL> import sys <EOL> if len ( sys . argv ) > <NUM_LIT:1> : <EOL> globals ( ) [ '<STR_LIT>' + sys . argv [ <NUM_LIT:1> ] ] ( ) <EOL> else : <EOL> run_all ( ) </s>
