|
| 1 | +use binaryninja::binary_view::BinaryView; |
| 2 | +use binaryninja::disassembly::InstructionTextToken; |
| 3 | +use binaryninja::headless::Session; |
| 4 | +use binaryninja::platform::Platform; |
| 5 | +use binaryninja::rc::Ref; |
| 6 | +use binaryninja::type_container::TypeContainer; |
| 7 | +use binaryninja::type_printer::{ |
| 8 | + register_type_printer, CoreTypePrinter, TokenEscapingType, TypeDefinitionLine, TypePrinter, |
| 9 | +}; |
| 10 | +use binaryninja::types::{ |
| 11 | + MemberAccess, MemberScope, QualifiedName, Structure, StructureMember, Type, |
| 12 | +}; |
| 13 | +use std::path::PathBuf; |
| 14 | + |
| 15 | +#[test] |
| 16 | +fn test_type_printer() { |
| 17 | + let _session = Session::new().expect("Failed to initialize session"); |
| 18 | + let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); |
| 19 | + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); |
| 20 | + |
| 21 | + let type_printer = CoreTypePrinter::default(); |
| 22 | + let my_structure = Type::structure( |
| 23 | + &Structure::builder() |
| 24 | + .insert_member( |
| 25 | + StructureMember::new( |
| 26 | + Type::int(4, false).into(), |
| 27 | + "my_field".to_string(), |
| 28 | + 0, |
| 29 | + MemberAccess::PublicAccess, |
| 30 | + MemberScope::NoScope, |
| 31 | + ), |
| 32 | + false, |
| 33 | + ) |
| 34 | + .finalize(), |
| 35 | + ); |
| 36 | + |
| 37 | + let printed_types = type_printer |
| 38 | + .print_all_types( |
| 39 | + [("my_struct", my_structure)], |
| 40 | + &view, |
| 41 | + 4, |
| 42 | + TokenEscapingType::NoTokenEscapingType, |
| 43 | + ) |
| 44 | + .expect("Failed to print types"); |
| 45 | + |
| 46 | + // TODO: Assert this |
| 47 | + /* |
| 48 | + // "my_struct" |
| 49 | + struct my_struct |
| 50 | + { |
| 51 | + uint32_t my_field; |
| 52 | + }; |
| 53 | + */ |
| 54 | + |
| 55 | + println!("{:#?}", printed_types); |
| 56 | +} |
| 57 | + |
| 58 | +struct MyTypePrinter; |
| 59 | + |
| 60 | +impl TypePrinter for MyTypePrinter { |
| 61 | + fn get_type_tokens<T: Into<QualifiedName>>( |
| 62 | + &self, |
| 63 | + _type_: Ref<Type>, |
| 64 | + _platform: Option<Ref<Platform>>, |
| 65 | + _name: T, |
| 66 | + _base_confidence: u8, |
| 67 | + _escaping: TokenEscapingType, |
| 68 | + ) -> Option<Vec<InstructionTextToken>> { |
| 69 | + todo!() |
| 70 | + } |
| 71 | + |
| 72 | + fn get_type_tokens_before_name( |
| 73 | + &self, |
| 74 | + _type_: Ref<Type>, |
| 75 | + _platform: Option<Ref<Platform>>, |
| 76 | + _base_confidence: u8, |
| 77 | + _parent_type: Option<Ref<Type>>, |
| 78 | + _escaping: TokenEscapingType, |
| 79 | + ) -> Option<Vec<InstructionTextToken>> { |
| 80 | + todo!() |
| 81 | + } |
| 82 | + |
| 83 | + fn get_type_tokens_after_name( |
| 84 | + &self, |
| 85 | + _type_: Ref<Type>, |
| 86 | + _platform: Option<Ref<Platform>>, |
| 87 | + _base_confidence: u8, |
| 88 | + _parent_type: Option<Ref<Type>>, |
| 89 | + _escaping: TokenEscapingType, |
| 90 | + ) -> Option<Vec<InstructionTextToken>> { |
| 91 | + todo!() |
| 92 | + } |
| 93 | + |
| 94 | + fn get_type_string<T: Into<QualifiedName>>( |
| 95 | + &self, |
| 96 | + _type_: Ref<Type>, |
| 97 | + _platform: Option<Ref<Platform>>, |
| 98 | + _name: T, |
| 99 | + _escaping: TokenEscapingType, |
| 100 | + ) -> Option<String> { |
| 101 | + todo!() |
| 102 | + } |
| 103 | + |
| 104 | + fn get_type_string_before_name( |
| 105 | + &self, |
| 106 | + _type_: Ref<Type>, |
| 107 | + _platform: Option<Ref<Platform>>, |
| 108 | + _escaping: TokenEscapingType, |
| 109 | + ) -> Option<String> { |
| 110 | + todo!() |
| 111 | + } |
| 112 | + |
| 113 | + fn get_type_string_after_name( |
| 114 | + &self, |
| 115 | + _type_: Ref<Type>, |
| 116 | + _platform: Option<Ref<Platform>>, |
| 117 | + _escaping: TokenEscapingType, |
| 118 | + ) -> Option<String> { |
| 119 | + todo!() |
| 120 | + } |
| 121 | + |
| 122 | + fn get_type_lines<T: Into<QualifiedName>>( |
| 123 | + &self, |
| 124 | + _type_: Ref<Type>, |
| 125 | + _types: &TypeContainer, |
| 126 | + _name: T, |
| 127 | + _padding_cols: isize, |
| 128 | + _collapsed: bool, |
| 129 | + _escaping: TokenEscapingType, |
| 130 | + ) -> Option<Vec<TypeDefinitionLine>> { |
| 131 | + todo!() |
| 132 | + } |
| 133 | + |
| 134 | + fn print_all_types( |
| 135 | + &self, |
| 136 | + names: Vec<QualifiedName>, |
| 137 | + types: Vec<Ref<Type>>, |
| 138 | + _data: Ref<BinaryView>, |
| 139 | + padding_cols: isize, |
| 140 | + escaping: TokenEscapingType, |
| 141 | + ) -> Option<String> { |
| 142 | + let printed = format!("{:?}, {:?}, {}, {:?}", names, types, padding_cols, escaping); |
| 143 | + Some(printed) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +#[test] |
| 148 | +fn test_custom_type_printer() { |
| 149 | + let _session = Session::new().expect("Failed to initialize session"); |
| 150 | + let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap(); |
| 151 | + let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view"); |
| 152 | + |
| 153 | + let type_printer = MyTypePrinter; |
| 154 | + register_type_printer("my_type_printer", type_printer); |
| 155 | + |
| 156 | + let core_type_printer = CoreTypePrinter::printer_by_name("my_type_printer") |
| 157 | + .expect("Failed to get core type printer"); |
| 158 | + let printed_types = core_type_printer |
| 159 | + .print_all_types( |
| 160 | + vec![("test", Type::int(4, false))], |
| 161 | + &view, |
| 162 | + 0, |
| 163 | + TokenEscapingType::NoTokenEscapingType, |
| 164 | + ) |
| 165 | + .expect("Failed to print types"); |
| 166 | + |
| 167 | + // TODO: Assert this |
| 168 | + println!("{:#?}", printed_types); |
| 169 | +} |
0 commit comments