summaryrefslogtreecommitdiff
path: root/rust/tests/type_printer.rs
blob: 6703537613b76892ad2e120b8a54ccc061489b70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use binaryninja::binary_view::BinaryView;
use binaryninja::disassembly::InstructionTextToken;
use binaryninja::headless::Session;
use binaryninja::platform::Platform;
use binaryninja::rc::Ref;
use binaryninja::types::printer::{register_type_printer, TokenEscapingType, TypeDefinitionLine};
use binaryninja::types::{
    CoreTypePrinter, MemberAccess, MemberScope, QualifiedName, Structure, StructureMember, Type,
    TypeContainer, TypePrinter,
};
use std::path::PathBuf;

#[test]
fn test_type_printer() {
    let _session = Session::new().expect("Failed to initialize session");
    let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
    let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");

    let type_printer = CoreTypePrinter::default();
    let my_structure = Type::structure(
        &Structure::builder()
            .insert_member(
                StructureMember::new(
                    Type::int(4, false).into(),
                    "my_field".to_string(),
                    0,
                    MemberAccess::PublicAccess,
                    MemberScope::NoScope,
                ),
                false,
            )
            .finalize(),
    );

    let printed_types = type_printer
        .print_all_types(
            [("my_struct", my_structure)],
            &view,
            4,
            TokenEscapingType::NoTokenEscapingType,
        )
        .expect("Failed to print types");

    // TODO: Assert this
    /*
    // "my_struct"
    struct my_struct
    {
        uint32_t my_field;
    };
    */

    println!("{:#?}", printed_types);
}

struct MyTypePrinter;

impl TypePrinter for MyTypePrinter {
    fn get_type_tokens<T: Into<QualifiedName>>(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _name: T,
        _base_confidence: u8,
        _escaping: TokenEscapingType,
    ) -> Option<Vec<InstructionTextToken>> {
        todo!()
    }

    fn get_type_tokens_before_name(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _base_confidence: u8,
        _parent_type: Option<Ref<Type>>,
        _escaping: TokenEscapingType,
    ) -> Option<Vec<InstructionTextToken>> {
        todo!()
    }

    fn get_type_tokens_after_name(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _base_confidence: u8,
        _parent_type: Option<Ref<Type>>,
        _escaping: TokenEscapingType,
    ) -> Option<Vec<InstructionTextToken>> {
        todo!()
    }

    fn get_type_string<T: Into<QualifiedName>>(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _name: T,
        _escaping: TokenEscapingType,
    ) -> Option<String> {
        todo!()
    }

    fn get_type_string_before_name(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _escaping: TokenEscapingType,
    ) -> Option<String> {
        todo!()
    }

    fn get_type_string_after_name(
        &self,
        _type_: Ref<Type>,
        _platform: Option<Ref<Platform>>,
        _escaping: TokenEscapingType,
    ) -> Option<String> {
        todo!()
    }

    fn get_type_lines<T: Into<QualifiedName>>(
        &self,
        _type_: Ref<Type>,
        _types: &TypeContainer,
        _name: T,
        _padding_cols: isize,
        _collapsed: bool,
        _escaping: TokenEscapingType,
    ) -> Option<Vec<TypeDefinitionLine>> {
        todo!()
    }

    fn print_all_types(
        &self,
        names: Vec<QualifiedName>,
        types: Vec<Ref<Type>>,
        _data: Ref<BinaryView>,
        padding_cols: isize,
        escaping: TokenEscapingType,
    ) -> Option<String> {
        let printed = format!("{:?}, {:?}, {}, {:?}", names, types, padding_cols, escaping);
        Some(printed)
    }
}

#[test]
fn test_custom_type_printer() {
    let _session = Session::new().expect("Failed to initialize session");
    let out_dir = env!("OUT_DIR").parse::<PathBuf>().unwrap();
    let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");

    let type_printer = MyTypePrinter;
    register_type_printer("my_type_printer", type_printer);

    let core_type_printer = CoreTypePrinter::printer_by_name("my_type_printer")
        .expect("Failed to get core type printer");
    let printed_types = core_type_printer
        .print_all_types(
            vec![("test", Type::int(4, false))],
            &view,
            0,
            TokenEscapingType::NoTokenEscapingType,
        )
        .expect("Failed to print types");

    // TODO: Assert this
    println!("{:#?}", printed_types);
}