summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/src/validate.rs
blob: b7bd4d38ef8f79d4fe12bd866d68d2deaec3cba2 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::schema::BntlSchema;
use binaryninja::platform::Platform;
use binaryninja::qualified_name::QualifiedName;
use binaryninja::rc::Ref;
use binaryninja::types::TypeLibrary;
use minijinja::{context, Environment};
use serde::Serialize;
use std::collections::{HashMap, HashSet};
use std::env::temp_dir;
use std::fmt::Display;

#[derive(Debug, PartialEq, PartialOrd, Clone, Eq, Hash, Serialize)]
pub enum ValidateIssue {
    DuplicateGUID {
        guid: String,
        existing_library: String,
    },
    DuplicateDependencyName {
        name: String,
        existing_library: String,
    },
    InvalidMetadata {
        key: String,
        issue: String,
    },
    DuplicateOrdinal {
        ordinal: u64,
        existing_name: String,
        duplicate_name: String,
    },
    NoPlatform,
    UnresolvedExternalReference {
        name: String,
        container: String,
    },
    UnresolvedSourceReference {
        name: String,
        source: String,
    },
    UnresolvedTypeLibrary {
        name: String,
    }, // TODO: Overlapping type name of platform?
       // TODO: E.g. a type is found in the type library, and also in the platform.
}

impl Display for ValidateIssue {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidateIssue::DuplicateGUID {
                guid,
                existing_library,
            } => {
                write!(
                    f,
                    "Duplicate GUID: '{}' is already used by library '{}'",
                    guid, existing_library
                )
            }
            ValidateIssue::DuplicateDependencyName {
                name,
                existing_library,
            } => {
                write!(
                    f,
                    "Duplicate Dependency Name: '{}' is already provided by '{}'",
                    name, existing_library
                )
            }
            ValidateIssue::InvalidMetadata { key, issue } => {
                write!(f, "Invalid Metadata: Key '{}' - {}", key, issue)
            }
            ValidateIssue::DuplicateOrdinal {
                ordinal,
                existing_name,
                duplicate_name,
            } => {
                write!(
                    f,
                    "Duplicate Ordinal: #{} is assigned to both '{}' and '{}'",
                    ordinal, existing_name, duplicate_name
                )
            }
            ValidateIssue::NoPlatform => {
                write!(
                    f,
                    "Missing Platform: The type library has no target platform associated with it"
                )
            }
            ValidateIssue::UnresolvedExternalReference { name, container } => {
                write!(
                    f,
                    "Unresolved External Reference: Type '{}' referenced inside '{}' is marked as external but has no source",
                    name, container
                )
            }
            ValidateIssue::UnresolvedSourceReference { name, source } => {
                write!(
                    f,
                    "Unresolved Source Reference: Type '{}' expects source '{}', but it wasn't found there",
                    name, source
                )
            }
            ValidateIssue::UnresolvedTypeLibrary { name } => {
                write!(
                    f,
                    "Unresolved Type Library: Could not find dependency library file for '{}'",
                    name
                )
            }
        }
    }
}

#[derive(Debug, Default)]
pub struct ValidateResult {
    pub issues: Vec<ValidateIssue>,
}

impl ValidateResult {
    /// Render the validation report as HTML.
    pub fn render_report(&self) -> Result<String, minijinja::Error> {
        let mut environment = Environment::new();
        // Remove trailing lines for blocks, this is required for Markdown tables.
        environment.set_trim_blocks(true);
        minijinja_embed::load_templates!(&mut environment);
        let tmpl = environment.get_template("validate.html")?;
        tmpl.render(context!(issues => self.issues))
    }
}

#[derive(Debug, Default, Clone)]
pub struct TypeLibValidater {
    pub seen_guids: HashMap<String, String>,
    // TODO: This needs to be by platform as well.
    pub seen_dependency_names: HashMap<String, String>,
    /// These are the type libraries that are accessible to the type library under validation.
    ///
    /// Used to validate external references.
    pub type_libraries: Vec<Ref<TypeLibrary>>,
    /// Built from the available type libraries.
    pub valid_external_references: HashSet<QualifiedName>,
}

impl TypeLibValidater {
    pub fn new() -> Self {
        Self {
            seen_guids: HashMap::new(),
            seen_dependency_names: HashMap::new(),
            type_libraries: Vec::new(),
            valid_external_references: HashSet::new(),
        }
    }

    /// These are the type libraries that are accessible to the type library under validation.
    ///
    /// Used to validate external references.
    pub fn with_type_libraries(mut self, type_libraries: Vec<Ref<TypeLibrary>>) -> Self {
        self.type_libraries = type_libraries;
        for type_lib in &self.type_libraries {
            for ty in &type_lib.named_types() {
                self.valid_external_references.insert(ty.name);
            }
            for obj in &type_lib.named_objects() {
                self.valid_external_references.insert(obj.name);
            }
        }
        self
    }

    /// The platform that is accessible to the type library under validation.
    ///
    /// Used to validate external references.
    pub fn with_platform(mut self, platform: &Platform) -> Self {
        for ty in &platform.types() {
            self.valid_external_references.insert(ty.name);
        }
        self
    }

    pub fn validate(&mut self, type_lib: &TypeLibrary) -> ValidateResult {
        let mut result = ValidateResult::default();

        if type_lib.platform_names().is_empty() {
            result.issues.push(ValidateIssue::NoPlatform);
        }

        if let Some(issue) = self.validate_guid(type_lib) {
            result.issues.push(issue);
        }

        if let Some(issue) = self.validate_dependency_name(type_lib) {
            result.issues.push(issue);
        }

        result.issues.extend(self.validate_ordinals(type_lib));
        result
            .issues
            .extend(self.validate_external_references(type_lib));

        // TODO: This is currently disabled because it's too slow.
        result.issues.extend(self.validate_source_files(type_lib));

        result
    }

    pub fn validate_guid(&mut self, type_lib: &TypeLibrary) -> Option<ValidateIssue> {
        match self.seen_guids.insert(type_lib.guid(), type_lib.name()) {
            None => None,
            Some(existing_library) => Some(ValidateIssue::DuplicateGUID {
                guid: type_lib.guid(),
                existing_library,
            }),
        }
    }

    pub fn validate_dependency_name(&mut self, type_lib: &TypeLibrary) -> Option<ValidateIssue> {
        match self
            .seen_dependency_names
            .insert(type_lib.dependency_name(), type_lib.name())
        {
            None => None,
            Some(existing_library) => Some(ValidateIssue::DuplicateDependencyName {
                name: type_lib.dependency_name(),
                existing_library,
            }),
        }
    }

    pub fn validate_source_files(&self, type_lib: &TypeLibrary) -> Vec<ValidateIssue> {
        let mut issues = Vec::new();
        let tmp_type_lib_path = temp_dir().join(type_lib.name());
        if !type_lib.decompress_to_file(&tmp_type_lib_path) {
            tracing::error!(
                "Failed to decompress type library to temporary file: {}",
                type_lib.name()
            );
            return issues;
        }
        let schema = BntlSchema::from_path(&tmp_type_lib_path);
        for (src, types) in schema.to_source_map() {
            let Some(dep_type_lib) = self.type_libraries.iter().find(|tl| tl.name() == src) else {
                issues.push(ValidateIssue::UnresolvedTypeLibrary {
                    name: src.to_string(),
                });
                continue;
            };

            for ty in &types {
                let qualified_name = QualifiedName::from(ty);
                let is_named_ty = dep_type_lib
                    .get_named_type(qualified_name.clone())
                    .is_none();
                let is_named_obj = dep_type_lib.get_named_object(qualified_name).is_none();
                if !is_named_ty && !is_named_obj {
                    issues.push(ValidateIssue::UnresolvedSourceReference {
                        name: ty.to_string(),
                        source: src.to_string(),
                    });
                }
            }
        }
        issues
    }

    pub fn validate_external_references(&self, type_lib: &TypeLibrary) -> Vec<ValidateIssue> {
        let mut issues = Vec::new();
        for ty in &type_lib.named_types() {
            crate::helper::visit_type_reference(&ty.ty, &mut |ntr| {
                if !self.valid_external_references.contains(&ntr.name()) {
                    issues.push(ValidateIssue::UnresolvedExternalReference {
                        name: ntr.name().to_string(),
                        container: ty.name.to_string(),
                    });
                }
            })
        }
        for obj in &type_lib.named_objects() {
            crate::helper::visit_type_reference(&obj.ty, &mut |ntr| {
                if !self.valid_external_references.contains(&ntr.name()) {
                    issues.push(ValidateIssue::UnresolvedExternalReference {
                        name: ntr.name().to_string(),
                        container: obj.name.to_string(),
                    });
                }
            })
        }
        issues
    }

    pub fn validate_ordinals(&self, type_lib: &TypeLibrary) -> Vec<ValidateIssue> {
        let Some(metadata_key_md) = type_lib.query_metadata("metadata") else {
            return vec![];
        };
        let Some(metadata_key_str) = metadata_key_md.get_string() else {
            return vec![ValidateIssue::InvalidMetadata {
                key: "metadata".to_owned(),
                issue: "Expected string".to_owned(),
            }];
        };

        let Some(metadata_map_md) = type_lib.query_metadata(&metadata_key_str.to_string_lossy())
        else {
            return vec![ValidateIssue::InvalidMetadata {
                key: metadata_key_str.to_string_lossy().to_string(),
                issue: "Missing metadata map key".to_owned(),
            }];
        };

        let Some(metadata_map) = metadata_map_md.get_value_store() else {
            return vec![ValidateIssue::InvalidMetadata {
                key: metadata_key_str.to_string_lossy().to_string(),
                issue: "Expected value store".to_owned(),
            }];
        };

        let mut discovered_ordinals = HashMap::new();
        let mut issues = Vec::new();
        for (key, value) in metadata_map.iter() {
            let Ok(ordinal_num) = key.parse::<u64>() else {
                issues.push(ValidateIssue::InvalidMetadata {
                    key: key.to_string(),
                    issue: "Expected ordinal number".to_owned(),
                });
                continue;
            };

            let Some(value_bn_str) = value.get_string() else {
                issues.push(ValidateIssue::InvalidMetadata {
                    key: key.to_string(),
                    issue: "Expected string".to_owned(),
                });
                continue;
            };
            let value_str = value_bn_str.to_string_lossy().to_string();

            match discovered_ordinals.insert(ordinal_num, value_str.clone()) {
                None => (),
                Some(existing_ordinal) => issues.push(ValidateIssue::DuplicateOrdinal {
                    ordinal: ordinal_num,
                    existing_name: existing_ordinal,
                    duplicate_name: value_str,
                }),
            }
        }
        issues
    }
}