|
| 1 | +use crate::convert::{comment_to_bn_comment, to_bn_symbol_at_address}; |
| 2 | +use binaryninja::binary_view::BinaryViewExt; |
1 | 3 | use binaryninja::function::{Function as BNFunction, FunctionUpdateType};
|
| 4 | +use binaryninja::symbol::SymbolType; |
2 | 5 | use warp::signature::function::Function;
|
3 | 6 |
|
4 |
| -/// Inserts a function match into the cache. |
| 7 | +// TODO: Rename this? |
| 8 | +/// Inserts a function match into the cache. This also has the side effect of setting persisted function |
| 9 | +/// information, such as the matched function symbol. |
5 | 10 | ///
|
6 | 11 | /// IMPORTANT: This will mark the function as needing updates, if you intend to fill in functions with
|
7 | 12 | /// no match (i.e. `None`), then you must change this function to prevent marking that as needing updates.
|
8 | 13 | /// However, it's perfectly valid to remove a match and need to update the function still, so be careful.
|
9 | 14 | pub fn insert_cached_function_match(function: &BNFunction, matched_function: Option<Function>) {
|
| 15 | + let view = function.view(); |
| 16 | + let function_start = function.start(); |
10 | 17 | // NOTE: If we expect to run match_function multiple times on a function, we should move this elsewhere.
|
11 | 18 | // Mark the function as needing updates so that reanalysis occurs on the function, and we apply the match.
|
12 | 19 | function.mark_updates_required(FunctionUpdateType::FullAutoFunctionUpdate);
|
| 20 | + if let Some(auto_sym) = view.symbol_by_address(function_start) { |
| 21 | + // TODO: If we ever create non library function symbols we will need to remove this check (see: `to_bn_symbol_at_address`). |
| 22 | + if auto_sym.sym_type() == SymbolType::LibraryFunction { |
| 23 | + // NOTE: This will also mark for full auto function update, one thing to note is that the |
| 24 | + // requirement to call this is that this function not be called in the associated function's analysis. |
| 25 | + view.undefine_auto_symbol(&auto_sym); |
| 26 | + } |
| 27 | + } |
13 | 28 | match matched_function {
|
14 | 29 | Some(matched_function) => {
|
| 30 | + // Define the new matched function symbol, this can safely be done here as the symbol itself |
| 31 | + // will be persisted, unlike function type information or variable information. |
| 32 | + let new_sym = to_bn_symbol_at_address(&view, &matched_function.symbol, function_start); |
| 33 | + view.define_auto_symbol(&new_sym); |
| 34 | + // TODO: How to clear the comments? They are just persisted. |
| 35 | + // TODO: Also they generate an undo action, i hate implicit undo actions so much. |
| 36 | + for comment in &matched_function.comments { |
| 37 | + let bn_comment = comment_to_bn_comment(&function, comment.clone()); |
| 38 | + function.set_comment_at(bn_comment.addr, &bn_comment.comment); |
| 39 | + } |
15 | 40 | function.store_metadata("warp_matched_function", &matched_function.to_bytes(), false);
|
16 | 41 | }
|
17 | 42 | None => {
|
|
0 commit comments