Skip to content

Commit 1b4fd27

Browse files
committed
[WARP] Move symbol and comment application to when matched functions are inserted
Instead of applying symbols and comments in the applier step, we will do it when the matched function is identified. This has the side effect that if you turn off the apply activity names and comments will still be applied, more work to be done later.
1 parent 534627a commit 1b4fd27

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

plugins/warp/src/cache/function.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
1+
use crate::convert::{comment_to_bn_comment, to_bn_symbol_at_address};
2+
use binaryninja::binary_view::BinaryViewExt;
13
use binaryninja::function::{Function as BNFunction, FunctionUpdateType};
4+
use binaryninja::symbol::SymbolType;
25
use warp::signature::function::Function;
36

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.
510
///
611
/// IMPORTANT: This will mark the function as needing updates, if you intend to fill in functions with
712
/// no match (i.e. `None`), then you must change this function to prevent marking that as needing updates.
813
/// However, it's perfectly valid to remove a match and need to update the function still, so be careful.
914
pub fn insert_cached_function_match(function: &BNFunction, matched_function: Option<Function>) {
15+
let view = function.view();
16+
let function_start = function.start();
1017
// NOTE: If we expect to run match_function multiple times on a function, we should move this elsewhere.
1118
// Mark the function as needing updates so that reanalysis occurs on the function, and we apply the match.
1219
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+
}
1328
match matched_function {
1429
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+
}
1540
function.store_metadata("warp_matched_function", &matched_function.to_bytes(), false);
1641
}
1742
None => {

plugins/warp/src/plugin/ffi/function.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,15 @@ pub unsafe extern "C" fn BNWARPFunctionApply(
6767
let analysis_function = Function::from_raw(analysis_function);
6868
match function.is_null() {
6969
false => {
70-
// Set the matched function to `function` and return previous.
70+
// Set the matched function to `function`.
7171
let matched_function = ManuallyDrop::new(Arc::from_raw(function));
7272
insert_cached_function_match(
7373
&analysis_function,
7474
Some(matched_function.as_ref().clone()),
7575
)
7676
}
7777
true => {
78-
// We are removing the previous match and returning it.
78+
// We are removing the previous match.
7979
insert_cached_function_match(&analysis_function, None)
8080
}
8181
};

plugins/warp/src/plugin/workflow.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@ use crate::cache::{
33
cached_function_guid, insert_cached_function_match, try_cached_function_guid,
44
try_cached_function_match,
55
};
6-
use crate::convert::{
7-
comment_to_bn_comment, platform_to_target, to_bn_symbol_at_address, to_bn_type,
8-
};
6+
use crate::convert::{platform_to_target, to_bn_type};
97
use crate::matcher::{Matcher, MatcherSettings};
108
use crate::{get_warp_tag_type, relocatable_regions};
119
use binaryninja::architecture::RegisterId;
@@ -192,29 +190,20 @@ pub fn run_matcher(view: &BinaryView) {
192190
}
193191

194192
pub fn insert_workflow() {
193+
// TODO: Note: because of symbol persistence function symbol is applied in `insert_cached_function_match`.
194+
// TODO: Comments are also applied there, they are "user" like, persisted and make undo actions.
195195
// "Hey look, it's a plier" ~ Josh 2025
196196
let apply_activity = |ctx: &AnalysisContext| {
197197
let view = ctx.view();
198198
let function = ctx.function();
199199
if let Some(matched_function) = try_cached_function_match(&function) {
200-
view.define_auto_symbol(&to_bn_symbol_at_address(
201-
&view,
202-
&matched_function.symbol,
203-
function.symbol().address(),
204-
));
205200
// core.function.propagateAnalysis will assign user type info to auto, so we must not apply
206201
// otherwise we will wipe over user type info.
207202
if !function.has_user_type() {
208203
if let Some(func_ty) = &matched_function.ty {
209204
function.set_auto_type(&to_bn_type(&function.arch(), func_ty));
210205
}
211206
}
212-
// TODO: How to clear the comments? They are just persisted.
213-
// TODO: Also they generate an undo action, i hate implicit undo actions so much.
214-
for comment in matched_function.comments {
215-
let bn_comment = comment_to_bn_comment(&function, comment);
216-
function.set_comment_at(bn_comment.addr, &bn_comment.comment);
217-
}
218207
if let Some(mlil) = ctx.mlil_function() {
219208
for variable in matched_function.variables {
220209
let decl_addr = ((function.start() as i64) + variable.offset) as u64;

0 commit comments

Comments
 (0)