@@ -85,6 +85,9 @@ def unpackage_file(self, filename):
85
85
assert os .path .exists (path )
86
86
return os .path .relpath (path )
87
87
88
+ def delete_package (self , filename ):
89
+ path = os .path .join (os .path .dirname (__file__ ), self .test_store , filename )
90
+ os .unlink (path )
88
91
89
92
class BinaryViewTestBuilder (Builder ):
90
93
""" The BinaryViewTestBuilder is for test that are verified against a binary.
@@ -551,52 +554,61 @@ def test_Enumeration(self):
551
554
def test_Types (self ):
552
555
"""Types produced different result"""
553
556
file_name = self .unpackage_file ("helloworld" )
554
- bv = binja .BinaryViewType .get_view_of_file (file_name )
555
-
556
- preprocessed = binja .preprocess_source ("""
557
- #ifdef nonexistant
558
- int foo = 1;
559
- long long foo1 = 1;
560
- #else
561
- int bar = 2;
562
- long long bar1 = 2;
563
- #endif
564
- """ )
565
- source = '\n ' .join ([i .decode ('charmap' ) for i in preprocessed [0 ].split (b'\n ' ) if not b'#line' in i and len (i ) > 0 ])
566
- typelist = bv .platform .parse_types_from_source (source )
567
- inttype = binja .Type .int (4 )
568
-
569
- tokens = inttype .get_tokens () + inttype .get_tokens_before_name () + inttype .get_tokens_after_name ()
570
- namedtype = binja .NamedTypeReference ()
571
-
572
- retinfo = []
573
- for i in range (len (typelist .variables )):
574
- for j in typelist .variables .popitem ():
575
- retinfo .append ("Type: " + str (j ))
576
- retinfo .append ("Named Type: " + str (namedtype ))
577
-
578
- retinfo .append ("Type equality: " + str ((inttype == inttype ) and not (inttype != inttype )))
579
- return retinfo
557
+ try :
558
+ bv = binja .BinaryViewType .get_view_of_file (file_name )
559
+
560
+ preprocessed = binja .preprocess_source ("""
561
+ #ifdef nonexistant
562
+ int foo = 1;
563
+ long long foo1 = 1;
564
+ #else
565
+ int bar = 2;
566
+ long long bar1 = 2;
567
+ #endif
568
+ """ )
569
+ source = '\n ' .join ([i .decode ('charmap' ) for i in preprocessed [0 ].split (b'\n ' ) if not b'#line' in i and len (i ) > 0 ])
570
+ typelist = bv .platform .parse_types_from_source (source )
571
+ inttype = binja .Type .int (4 )
572
+
573
+ tokens = inttype .get_tokens () + inttype .get_tokens_before_name () + inttype .get_tokens_after_name ()
574
+ namedtype = binja .NamedTypeReference ()
575
+
576
+ retinfo = []
577
+ for i in range (len (typelist .variables )):
578
+ for j in typelist .variables .popitem ():
579
+ retinfo .append ("Type: " + str (j ))
580
+ retinfo .append ("Named Type: " + str (namedtype ))
581
+
582
+ retinfo .append ("Type equality: " + str ((inttype == inttype ) and not (inttype != inttype )))
583
+ return retinfo
584
+ finally :
585
+ self .delete_package ("helloworld" )
580
586
581
587
def test_Plugin_bin_info (self ):
582
588
"""print_syscalls plugin produced different result"""
583
589
file_name = self .unpackage_file ("helloworld" )
584
- bin_info_path = os .path .join (os .path .dirname (__file__ ), '..' , 'python' , 'examples' , 'bin_info.py' )
585
- result = subprocess .Popen (["python" , bin_info_path , file_name ], stdout = subprocess .PIPE ).communicate ()[0 ]
586
- # normalize line endings and path sep
587
- return [line for line in result .replace (b"\\ " , b"/" ).replace (b"\r \n " , b"\n " ).decode ("charmap" ).split ("\n " )]
590
+ try :
591
+ bin_info_path = os .path .join (os .path .dirname (__file__ ), '..' , 'python' , 'examples' , 'bin_info.py' )
592
+ result = subprocess .Popen (["python" , bin_info_path , file_name ], stdout = subprocess .PIPE ).communicate ()[0 ]
593
+ # normalize line endings and path sep
594
+ return [line for line in result .replace (b"\\ " , b"/" ).replace (b"\r \n " , b"\n " ).decode ("charmap" ).split ("\n " )]
595
+ finally :
596
+ self .delete_package ("helloworld" )
588
597
589
598
def test_linear_disassembly (self ):
590
599
"""linear_disassembly produced different result"""
591
600
file_name = self .unpackage_file ("helloworld" )
592
- bv = binja .BinaryViewType ['ELF' ].open (file_name )
593
- disass = bv .linear_disassembly
594
- retinfo = []
595
- for i in disass :
596
- i = str (i )
597
- i = remove_low_confidence (i )
598
- retinfo .append (i )
599
- return retinfo
601
+ try :
602
+ bv = binja .BinaryViewType ['ELF' ].open (file_name )
603
+ disass = bv .linear_disassembly
604
+ retinfo = []
605
+ for i in disass :
606
+ i = str (i )
607
+ i = remove_low_confidence (i )
608
+ retinfo .append (i )
609
+ return retinfo
610
+ finally :
611
+ self .delete_package ("helloworld" )
600
612
601
613
# def test_partial_register_dataflow(self):
602
614
# """partial_register_dataflow produced different results"""
@@ -620,136 +632,145 @@ def test_linear_disassembly(self):
620
632
def test_low_il_stack (self ):
621
633
"""LLIL stack produced different output"""
622
634
file_name = self .unpackage_file ("jumptable_reordered" )
623
- bv = binja .BinaryViewType .get_view_of_file (file_name )
624
- reg_list = ['ch' , 'cl' , 'ah' , 'edi' , 'al' , 'cx' , 'ebp' , 'ax' , 'edx' , 'ebx' , 'esp' , 'esi' , 'dl' , 'dh' , 'di' , 'bl' , 'bh' , 'eax' , 'dx' , 'bx' , 'ecx' , 'sp' , 'si' ]
625
- flag_list = ['c' , 'p' , 'a' , 'z' , 's' , 'o' ]
626
- retinfo = []
627
- for func in bv .functions :
628
- for bb in func .low_level_il .basic_blocks :
629
- for ins in bb :
630
- retinfo .append ("LLIL first stack element: " + str (ins .get_stack_contents (0 ,1 )))
631
- retinfo .append ("LLIL second stack element: " + str (ins .get_stack_contents_after (0 ,1 )))
632
- retinfo .append ("LLIL possible first stack element: " + str (ins .get_possible_stack_contents (0 ,1 )))
633
- retinfo .append ("LLIL possible second stack element: " + str (ins .get_possible_stack_contents_after (0 ,1 )))
634
- for flag in flag_list :
635
- retinfo .append ("LLIL flag {} value at: " .format (flag , hex (ins .address )) + str (ins .get_flag_value (flag )))
636
- retinfo .append ("LLIL flag {} value after {}: " .format (flag , hex (ins .address )) + str (ins .get_flag_value_after (flag )))
637
- retinfo .append ("LLIL flag {} possible value at {}: " .format (flag , hex (ins .address )) + str (ins .get_possible_flag_values (flag )))
638
- retinfo .append ("LLIL flag {} possible value after {}: " .format (flag , hex (ins .address )) + str (ins .get_possible_flag_values_after (flag )))
639
- return fixOutput (retinfo )
635
+ try :
636
+ bv = binja .BinaryViewType .get_view_of_file (file_name )
637
+ reg_list = ['ch' , 'cl' , 'ah' , 'edi' , 'al' , 'cx' , 'ebp' , 'ax' , 'edx' , 'ebx' , 'esp' , 'esi' , 'dl' , 'dh' , 'di' , 'bl' , 'bh' , 'eax' , 'dx' , 'bx' , 'ecx' , 'sp' , 'si' ]
638
+ flag_list = ['c' , 'p' , 'a' , 'z' , 's' , 'o' ]
639
+ retinfo = []
640
+ for func in bv .functions :
641
+ for bb in func .low_level_il .basic_blocks :
642
+ for ins in bb :
643
+ retinfo .append ("LLIL first stack element: " + str (ins .get_stack_contents (0 ,1 )))
644
+ retinfo .append ("LLIL second stack element: " + str (ins .get_stack_contents_after (0 ,1 )))
645
+ retinfo .append ("LLIL possible first stack element: " + str (ins .get_possible_stack_contents (0 ,1 )))
646
+ retinfo .append ("LLIL possible second stack element: " + str (ins .get_possible_stack_contents_after (0 ,1 )))
647
+ for flag in flag_list :
648
+ retinfo .append ("LLIL flag {} value at: " .format (flag , hex (ins .address )) + str (ins .get_flag_value (flag )))
649
+ retinfo .append ("LLIL flag {} value after {}: " .format (flag , hex (ins .address )) + str (ins .get_flag_value_after (flag )))
650
+ retinfo .append ("LLIL flag {} possible value at {}: " .format (flag , hex (ins .address )) + str (ins .get_possible_flag_values (flag )))
651
+ retinfo .append ("LLIL flag {} possible value after {}: " .format (flag , hex (ins .address )) + str (ins .get_possible_flag_values_after (flag )))
652
+ return fixOutput (retinfo )
653
+ finally :
654
+ self .delete_package ("jumptable_reordered" )
640
655
641
656
def test_med_il_stack (self ):
642
657
"""MLIL stack produced different output"""
643
658
file_name = self .unpackage_file ("jumptable_reordered" )
644
- bv = binja .BinaryViewType .get_view_of_file (file_name )
645
- reg_list = ['ch' , 'cl' , 'ah' , 'edi' , 'al' , 'cx' , 'ebp' , 'ax' , 'edx' , 'ebx' , 'esp' , 'esi' , 'dl' , 'dh' , 'di' , 'bl' , 'bh' , 'eax' , 'dx' , 'bx' , 'ecx' , 'sp' , 'si' ]
646
- flag_list = ['c' , 'p' , 'a' , 'z' , 's' , 'o' ]
647
- retinfo = []
648
- for func in bv .functions :
649
- for bb in func .medium_level_il .basic_blocks :
650
- for ins in bb :
651
- retinfo .append ("MLIL stack begin var: " + str (ins .get_var_for_stack_location (0 )))
652
- retinfo .append ("MLIL first stack element: " + str (ins .get_stack_contents (0 , 1 )))
653
- retinfo .append ("MLIL second stack element: " + str (ins .get_stack_contents_after (0 , 1 )))
654
- retinfo .append ("MLIL possible first stack element: " + str (ins .get_possible_stack_contents (0 , 1 )))
655
- retinfo .append ("MLIL possible second stack element: " + str (ins .get_possible_stack_contents_after (0 , 1 )))
656
-
657
- for reg in reg_list :
658
- retinfo .append ("MLIL reg {} var at {}: " .format (reg , hex (ins .address )) + str (ins .get_var_for_reg (reg )))
659
- retinfo .append ("MLIL reg {} value at {}: " .format (reg , hex (ins .address )) + str (ins .get_reg_value (reg )))
660
- retinfo .append ("MLIL reg {} value after {}: " .format (reg , hex (ins .address )) + str (ins .get_reg_value_after (reg )))
661
- retinfo .append ("MLIL reg {} possible value at {}: " .format (reg , hex (ins .address )) + fixSet (str (ins .get_possible_reg_values (reg ))))
662
- retinfo .append ("MLIL reg {} possible value after {}: " .format (reg , hex (ins .address )) + fixSet (str (ins .get_possible_reg_values_after (reg ))))
663
-
664
- for flag in flag_list :
665
- retinfo .append ("MLIL flag {} value at: " .format (flag , hex (ins .address )) + str (ins .get_flag_value (flag )))
666
- retinfo .append ("MLIL flag {} value after {}: " .format (flag , hex (ins .address )) + str (ins .get_flag_value_after (flag )))
667
- retinfo .append ("MLIL flag {} possible value at {}: " .format (flag , hex (ins .address )) + fixSet (str (ins .get_possible_flag_values (flag ))))
668
- retinfo .append ("MLIL flag {} possible value after {}: " .format (flag , hex (ins .address )) + fixSet (str (ins .get_possible_flag_values (flag ))))
669
- return fixOutput (retinfo )
659
+ try :
660
+ bv = binja .BinaryViewType .get_view_of_file (file_name )
661
+ reg_list = ['ch' , 'cl' , 'ah' , 'edi' , 'al' , 'cx' , 'ebp' , 'ax' , 'edx' , 'ebx' , 'esp' , 'esi' , 'dl' , 'dh' , 'di' , 'bl' , 'bh' , 'eax' , 'dx' , 'bx' , 'ecx' , 'sp' , 'si' ]
662
+ flag_list = ['c' , 'p' , 'a' , 'z' , 's' , 'o' ]
663
+ retinfo = []
664
+ for func in bv .functions :
665
+ for bb in func .medium_level_il .basic_blocks :
666
+ for ins in bb :
667
+ retinfo .append ("MLIL stack begin var: " + str (ins .get_var_for_stack_location (0 )))
668
+ retinfo .append ("MLIL first stack element: " + str (ins .get_stack_contents (0 , 1 )))
669
+ retinfo .append ("MLIL second stack element: " + str (ins .get_stack_contents_after (0 , 1 )))
670
+ retinfo .append ("MLIL possible first stack element: " + str (ins .get_possible_stack_contents (0 , 1 )))
671
+ retinfo .append ("MLIL possible second stack element: " + str (ins .get_possible_stack_contents_after (0 , 1 )))
672
+
673
+ for reg in reg_list :
674
+ retinfo .append ("MLIL reg {} var at {}: " .format (reg , hex (ins .address )) + str (ins .get_var_for_reg (reg )))
675
+ retinfo .append ("MLIL reg {} value at {}: " .format (reg , hex (ins .address )) + str (ins .get_reg_value (reg )))
676
+ retinfo .append ("MLIL reg {} value after {}: " .format (reg , hex (ins .address )) + str (ins .get_reg_value_after (reg )))
677
+ retinfo .append ("MLIL reg {} possible value at {}: " .format (reg , hex (ins .address )) + fixSet (str (ins .get_possible_reg_values (reg ))))
678
+ retinfo .append ("MLIL reg {} possible value after {}: " .format (reg , hex (ins .address )) + fixSet (str (ins .get_possible_reg_values_after (reg ))))
679
+
680
+ for flag in flag_list :
681
+ retinfo .append ("MLIL flag {} value at: " .format (flag , hex (ins .address )) + str (ins .get_flag_value (flag )))
682
+ retinfo .append ("MLIL flag {} value after {}: " .format (flag , hex (ins .address )) + str (ins .get_flag_value_after (flag )))
683
+ retinfo .append ("MLIL flag {} possible value at {}: " .format (flag , hex (ins .address )) + fixSet (str (ins .get_possible_flag_values (flag ))))
684
+ retinfo .append ("MLIL flag {} possible value after {}: " .format (flag , hex (ins .address )) + fixSet (str (ins .get_possible_flag_values (flag ))))
685
+ return fixOutput (retinfo )
686
+ finally :
687
+ self .delete_package ("jumptable_reordered" )
670
688
671
689
def test_events (self ):
672
690
"""Event failure"""
673
691
file_name = self .unpackage_file ("helloworld" )
674
- bv = binja .BinaryViewType ['ELF' ].open (file_name )
675
- bv .update_analysis_and_wait ()
692
+ try :
693
+ bv = binja .BinaryViewType ['ELF' ].open (file_name )
694
+ bv .update_analysis_and_wait ()
676
695
677
- results = []
696
+ results = []
678
697
679
- def simple_complete (self ):
680
- results .append ("analysis complete" )
681
- evt = binja .AnalysisCompletionEvent (bv , simple_complete )
698
+ def simple_complete (self ):
699
+ results .append ("analysis complete" )
700
+ evt = binja .AnalysisCompletionEvent (bv , simple_complete )
682
701
683
- class NotifyTest (binja .BinaryDataNotification ):
684
- def data_written (self , view , offset , length ):
685
- results .append ("data written: offset {0} length {1}" .format (hex (offset ), hex (length )))
702
+ class NotifyTest (binja .BinaryDataNotification ):
703
+ def data_written (self , view , offset , length ):
704
+ results .append ("data written: offset {0} length {1}" .format (hex (offset ), hex (length )))
686
705
687
- def data_inserted (self , view , offset , length ):
688
- results .append ("data inserted: offset {0} length {1}" .format (hex (offset ), hex (length )))
706
+ def data_inserted (self , view , offset , length ):
707
+ results .append ("data inserted: offset {0} length {1}" .format (hex (offset ), hex (length )))
689
708
690
- def data_removed (self , view , offset , length ):
691
- results .append ("data removed: offset {0} length {1}" .format (hex (offset ), hex (length )))
709
+ def data_removed (self , view , offset , length ):
710
+ results .append ("data removed: offset {0} length {1}" .format (hex (offset ), hex (length )))
692
711
693
- def function_added (self , view , func ):
694
- results .append ("function added: {0}" .format (func .name ))
712
+ def function_added (self , view , func ):
713
+ results .append ("function added: {0}" .format (func .name ))
695
714
696
- def function_removed (self , view , func ):
697
- results .append ("function removed: {0}" .format (func .name ))
715
+ def function_removed (self , view , func ):
716
+ results .append ("function removed: {0}" .format (func .name ))
698
717
699
- def data_var_added (self , view , var ):
700
- results .append ("data var added: {0}" .format (hex (var .address )))
718
+ def data_var_added (self , view , var ):
719
+ results .append ("data var added: {0}" .format (hex (var .address )))
701
720
702
- def data_var_removed (self , view , var ):
703
- results .append ("data var removed: {0}" .format (hex (var .address )))
721
+ def data_var_removed (self , view , var ):
722
+ results .append ("data var removed: {0}" .format (hex (var .address )))
704
723
705
- def string_found (self , view , string_type , offset , length ):
706
- results .append ("string found: offset {0} length {1}" .format (hex (offset ), hex (length )))
724
+ def string_found (self , view , string_type , offset , length ):
725
+ results .append ("string found: offset {0} length {1}" .format (hex (offset ), hex (length )))
707
726
708
- def string_removed (self , view , string_type , offset , length ):
709
- results .append ("string removed: offset {0} length {1}" .format (hex (offset ), hex (length )))
727
+ def string_removed (self , view , string_type , offset , length ):
728
+ results .append ("string removed: offset {0} length {1}" .format (hex (offset ), hex (length )))
710
729
711
- def type_defined (self , view , name , type ):
712
- results .append ("type defined: {0}" .format (name ))
730
+ def type_defined (self , view , name , type ):
731
+ results .append ("type defined: {0}" .format (name ))
713
732
714
- def type_undefined (self , view , name , type ):
715
- results .append ("type undefined: {0}" .format (name ))
733
+ def type_undefined (self , view , name , type ):
734
+ results .append ("type undefined: {0}" .format (name ))
716
735
717
- test = NotifyTest ()
718
- bv .register_notification (test )
719
- sacrificial_addr = 0x84fc
736
+ test = NotifyTest ()
737
+ bv .register_notification (test )
738
+ sacrificial_addr = 0x84fc
720
739
721
- type , name = bv .parse_type_string ("int foo" )
722
- type_id = type .generate_auto_type_id ("source" , name )
740
+ type , name = bv .parse_type_string ("int foo" )
741
+ type_id = type .generate_auto_type_id ("source" , name )
723
742
724
- bv .define_type (type_id , name , type )
725
- bv .undefine_type (type_id )
743
+ bv .define_type (type_id , name , type )
744
+ bv .undefine_type (type_id )
726
745
727
- bv .update_analysis_and_wait ()
746
+ bv .update_analysis_and_wait ()
728
747
729
- bv .insert (sacrificial_addr , b"AAAA" )
730
- bv .update_analysis_and_wait ()
748
+ bv .insert (sacrificial_addr , b"AAAA" )
749
+ bv .update_analysis_and_wait ()
731
750
732
- bv .define_data_var (sacrificial_addr , binja .types .Type .int (4 ))
733
- bv .update_analysis_and_wait ()
751
+ bv .define_data_var (sacrificial_addr , binja .types .Type .int (4 ))
752
+ bv .update_analysis_and_wait ()
734
753
735
- bv .write (sacrificial_addr , b"BBBB" )
736
- bv .update_analysis_and_wait ()
754
+ bv .write (sacrificial_addr , b"BBBB" )
755
+ bv .update_analysis_and_wait ()
737
756
738
- bv .add_function (sacrificial_addr )
739
- bv .update_analysis_and_wait ()
757
+ bv .add_function (sacrificial_addr )
758
+ bv .update_analysis_and_wait ()
740
759
741
- bv .remove_function (bv .get_function_at (sacrificial_addr ))
742
- bv .update_analysis_and_wait ()
760
+ bv .remove_function (bv .get_function_at (sacrificial_addr ))
761
+ bv .update_analysis_and_wait ()
743
762
744
- bv .undefine_data_var (sacrificial_addr )
745
- bv .update_analysis_and_wait ()
763
+ bv .undefine_data_var (sacrificial_addr )
764
+ bv .update_analysis_and_wait ()
746
765
747
- bv .remove (sacrificial_addr , 4 )
748
- bv .update_analysis_and_wait ()
766
+ bv .remove (sacrificial_addr , 4 )
767
+ bv .update_analysis_and_wait ()
749
768
750
- bv .unregister_notification (test )
769
+ bv .unregister_notification (test )
751
770
752
- return fixOutput (sorted (results ))
771
+ return fixOutput (sorted (results ))
772
+ finally :
773
+ self .delete_package ("helloworld" )
753
774
754
775
755
776
class VerifyBuilder (Builder ):
@@ -805,6 +826,7 @@ def test_verify_BNDB_round_trip(self):
805
826
# force windows to close the handle to the bndb that we want to delete
806
827
bv .file .close ()
807
828
del bv
829
+ os .unlink (temp_name )
808
830
return [str (functions == bndb_functions and comments == bndb_comments )]
809
831
finally :
810
- os . unlink ( temp_name )
832
+ self . delete_package ( "helloworld" )
0 commit comments