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

pub struct Validate;

impl Validate {
    pub fn execute() {
        let mut form = Form::new("Validate Type Libraries");
        form.add_field(InputDirectoryField::field());
        form.add_field(OutputDirectoryField::field());
        if !form.prompt() {
            return;
        }
        let output_dir = OutputDirectoryField::from_form(&form).unwrap();
        let input_path = InputDirectoryField::from_form(&form).unwrap();

        let type_libraries = path_to_type_libraries(&input_path);
        // TODO: Run this in parallel.
        for type_lib in &type_libraries {
            // 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());
                continue;
            }

            // TODO: Currently we collect input path dependencies from the platform and the parent directory.
            let validator = TypeLibValidater::new().with_type_libraries(type_libraries.clone());
            // 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 = output_dir.with_extension(format!("{}.html", platform_name));
                if let Err(e) = std::fs::write(out_path, rendered) {
                    tracing::error!(
                        "Failed to write validation report to {}: {}",
                        output_dir.display(),
                        e
                    );
                }
            }
        }
    }
}

impl Command for Validate {
    fn action(&self, _view: &BinaryView) {
        std::thread::spawn(move || {
            Validate::execute();
        });
    }

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