binaryninja/
function_recognizer.rs1use crate::low_level_il::function::LowLevelILFunction;
2use crate::low_level_il::LowLevelILRegularFunction;
3use crate::medium_level_il::MediumLevelILFunction;
4use crate::{architecture::CoreArchitecture, binary_view::BinaryView, function::Function};
5use binaryninjacore_sys::*;
6use std::os::raw::c_void;
7
8pub trait FunctionRecognizer {
9 fn recognize_low_level_il(
10 &self,
11 _bv: &BinaryView,
12 _func: &Function,
13 _llil: &LowLevelILRegularFunction,
14 ) -> bool {
15 false
16 }
17
18 fn recognize_medium_level_il(
19 &self,
20 _bv: &BinaryView,
21 _func: &Function,
22 _mlil: &MediumLevelILFunction,
23 ) -> bool {
24 false
25 }
26}
27
28fn create_function_recognizer_registration<R>(recognizer: R) -> BNFunctionRecognizer
29where
30 R: 'static + FunctionRecognizer + Send + Sync + Sized,
31{
32 #[repr(C)]
33 struct FunctionRecognizerHandlerContext<R>
34 where
35 R: 'static + FunctionRecognizer + Send + Sync,
36 {
37 recognizer: R,
38 }
39
40 extern "C" fn cb_recognize_low_level_il<R>(
41 ctxt: *mut c_void,
42 bv: *mut BNBinaryView,
43 func: *mut BNFunction,
44 llil: *mut BNLowLevelILFunction,
45 ) -> bool
46 where
47 R: 'static + FunctionRecognizer + Send + Sync,
48 {
49 let context = unsafe { &*(ctxt as *mut FunctionRecognizerHandlerContext<R>) };
50 let bv = unsafe { BinaryView::from_raw(bv).to_owned() };
51 let func = unsafe { Function::from_raw(func).to_owned() };
52 let llil = unsafe { LowLevelILFunction::from_raw(llil).to_owned() };
53 context.recognizer.recognize_low_level_il(&bv, &func, &llil)
54 }
55
56 extern "C" fn cb_recognize_medium_level_il<R>(
57 ctxt: *mut c_void,
58 bv: *mut BNBinaryView,
59 func: *mut BNFunction,
60 mlil: *mut BNMediumLevelILFunction,
61 ) -> bool
62 where
63 R: 'static + FunctionRecognizer + Send + Sync,
64 {
65 let context = unsafe { &*(ctxt as *mut FunctionRecognizerHandlerContext<R>) };
66 let bv = unsafe { BinaryView::from_raw(bv).to_owned() };
67 let func = unsafe { Function::from_raw(func).to_owned() };
68 let mlil = unsafe { MediumLevelILFunction::from_raw(mlil).to_owned() };
69 context
70 .recognizer
71 .recognize_medium_level_il(&bv, &func, &mlil)
72 }
73
74 let recognizer = FunctionRecognizerHandlerContext { recognizer };
75 let raw = Box::into_raw(Box::new(recognizer));
77 BNFunctionRecognizer {
78 context: raw as *mut _,
79 recognizeLowLevelIL: Some(cb_recognize_low_level_il::<R>),
80 recognizeMediumLevelIL: Some(cb_recognize_medium_level_il::<R>),
81 }
82}
83
84pub fn register_global_function_recognizer<R>(recognizer: R)
85where
86 R: 'static + FunctionRecognizer + Send + Sync + Sized,
87{
88 let mut recognizer = create_function_recognizer_registration::<R>(recognizer);
89 unsafe {
90 BNRegisterGlobalFunctionRecognizer(&mut recognizer);
91 }
92}
93
94pub(crate) fn register_arch_function_recognizer<R>(arch: &CoreArchitecture, recognizer: R)
95where
96 R: 'static + FunctionRecognizer + Send + Sync + Sized,
97{
98 let mut recognizer = create_function_recognizer_registration::<R>(recognizer);
99 unsafe {
100 BNRegisterArchitectureFunctionRecognizer(arch.handle, &mut recognizer);
101 }
102}