summaryrefslogtreecommitdiff
path: root/plugins/bntl_utils/src/command/validate.rs
blob: 8f0095ae2c292a4b2677be309e73eed4e4bc07db (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
use crate::helper::path_to_type_libraries;
use crate::validate::TypeLibValidater;
use binaryninja::binary_view::{BinaryView, BinaryViewExt};
use binaryninja::command::Command;
use binaryninja::interaction::get_open_filename_input;
use binaryninja::platform::Platform;
use binaryninja::types::TypeLibrary;

pub struct Validate;

impl Command for Validate {
    fn action(&self, _view: &BinaryView) {
        let Some(input_path) =
            get_open_filename_input("Select a type library to validate", "*.bntl")
        else {
            return;
        };

        let type_lib = match TypeLibrary::load_from_file(&input_path) {
            Some(type_lib) => type_lib,
            None => {
                tracing::error!("Failed to load type library from {}", input_path.display());
                return;
            }
        };

        // Type libraries should always have at least one platform associated with them.
        if type_lib.platform_names().is_empty() {
            tracing::error!("Type library {} has no platforms!", input_path.display());
            return;
        }

        // TODO: Currently we collect input path dependencies from the platform and the parent directory.
        let dependencies = path_to_type_libraries(input_path.parent().unwrap());

        let validator = TypeLibValidater::new().with_type_libraries(dependencies);
        // Validate for every platform so that we can find issues in lesser used platforms.
        for platform_name in &type_lib.platform_names() {
            let Some(platform) = Platform::by_name(platform_name) else {
                tracing::error!("Failed to find platform with name {}", platform_name);
                continue;
            };
            let results = validator
                .clone()
                .with_platform(&platform)
                .validate(&type_lib);
            if results.issues.is_empty() {
                tracing::info!(
                    "No issues found for type library {} on platform {}",
                    type_lib.name(),
                    platform_name
                );
                continue;
            }
            let rendered = match results.render_report() {
                Ok(rendered) => rendered,
                Err(err) => {
                    tracing::error!("Failed to render validation report: {}", err);
                    continue;
                }
            };
            let out_path = input_path.with_extension(format!("{}.html", platform_name));
            let out_name = format!("{} ({})", type_lib.name(), platform_name);
            _view.show_html_report(&out_name, &rendered, "");
            if let Err(e) = std::fs::write(out_path, rendered) {
                tracing::error!(
                    "Failed to write validation report to {}: {}",
                    input_path.display(),
                    e
                );
            }
        }
    }

    fn valid(&self, _view: &BinaryView) -> bool {
        true
    }
}