summaryrefslogtreecommitdiff
path: root/plugins/warp/src/plugin/add.rs
blob: 2f76c8b5b8733cec19b930ebe6cd302df753b145 (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
use crate::cache::{cached_function, cached_type_references};
use crate::matcher::invalidate_function_matcher_cache;
use crate::user_signature_dir;
use binaryninja::binary_view::BinaryView;
use binaryninja::command::FunctionCommand;
use binaryninja::function::Function;
use std::thread;

pub struct AddFunctionSignature;

impl FunctionCommand for AddFunctionSignature {
    fn action(&self, view: &BinaryView, func: &Function) {
        let func_plat_name = func.platform().name().to_string();
        let signature_dir = user_signature_dir().join(func_plat_name);
        let view = view.to_owned();
        let func = func.to_owned();
        thread::spawn(move || {
            let Ok(llil) = func.low_level_il() else {
                log::error!("Could not get low level IL for function.");
                return;
            };

            // NOTE: Because we only can consume signatures from a specific directory, we don't need to use the interaction API.
            // If we did need to save signature files to a project than this would need to change.
            let Some(save_file) = rfd::FileDialog::new()
                .add_filter("Signature Files", &["sbin"])
                .set_file_name("user.sbin")
                .set_directory(signature_dir)
                .save_file()
            else {
                return;
            };

            let mut data = warp::signature::Data::default();
            if let Ok(file_bytes) = std::fs::read(&save_file) {
                // If the file we are adding the function to already has data we should preserve it!
                log::info!("Signature file already exists, preserving data...");
                let Some(file_data) = warp::signature::Data::from_bytes(&file_bytes) else {
                    log::error!("Could not get data from signature file: {:?}", save_file);
                    return;
                };
                data = file_data;
            };

            // Now add our function to the data.
            data.functions.push(cached_function(&func, &llil));

            if let Some(ref_ty_cache) = cached_type_references(&view) {
                let referenced_types = ref_ty_cache
                    .cache
                    .iter()
                    .filter_map(|t| t.to_owned())
                    .collect::<Vec<_>>();

                data.types.extend(referenced_types);
            }

            match std::fs::write(&save_file, data.to_bytes()) {
                Ok(_) => {
                    log::info!("Signature file saved successfully.");
                    // Force rebuild platform matcher.
                    invalidate_function_matcher_cache();
                }
                Err(e) => log::error!("Failed to write data to signature file: {:?}", e),
            }
        });
    }

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