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

pub struct AddFunctionSignature;

impl FunctionCommand for AddFunctionSignature {
    fn action(&self, view: &BinaryView, func: &Function) {
        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;
            };

            let Some(save_file) = binaryninja::interaction::get_save_filename_input(
                "Use Signature File",
                "*.sbin",
                "user.sbin",
            ) 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);
            }

            if let Ok(mut file) = std::fs::File::create(&save_file) {
                match file.write_all(&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),
                }
            } else {
                log::error!("Could not create signature file: {:?}", save_file);
            }
        });
    }

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