|
| 1 | +//! APIs for resolving argument types described by format strings. |
| 2 | +
|
| 3 | +use binaryninjacore_sys::*; |
| 4 | +use std::ffi::{c_char, c_void}; |
| 5 | +use std::fmt::Debug; |
| 6 | +use std::ptr::NonNull; |
| 7 | + |
| 8 | +use crate::confidence::Conf; |
| 9 | +use crate::platform::Platform; |
| 10 | +use crate::rc::{Array, CoreArrayProvider, CoreArrayProviderInner, Ref}; |
| 11 | +use crate::string::{raw_to_string, BnString, IntoCStr}; |
| 12 | +use crate::types::Type; |
| 13 | + |
| 14 | +/// A custom provider that validates format strings and resolves their argument types. |
| 15 | +/// |
| 16 | +/// Providers are registered for the lifetime of the process and may be invoked from multiple |
| 17 | +/// analysis threads. |
| 18 | +pub trait CustomFormatStringResolutionProvider: Send + Sync + 'static { |
| 19 | + /// Resolve the argument types described by `format` for `platform`. |
| 20 | + /// |
| 21 | + /// Return `None` when the string is not valid for this provider. A valid string with no |
| 22 | + /// arguments is represented by `Some(Vec::new())`. Each returned type retains its individual |
| 23 | + /// confidence value. |
| 24 | + fn is_valid(&self, format: &str, platform: Option<&Platform>) -> Option<Vec<Conf<Ref<Type>>>>; |
| 25 | +} |
| 26 | + |
| 27 | +/// A registered format string resolution provider. |
| 28 | +#[derive(Clone, Copy, Hash, PartialEq, Eq)] |
| 29 | +#[repr(transparent)] |
| 30 | +pub struct FormatStringResolutionProvider { |
| 31 | + handle: NonNull<BNFormatStringResolutionProvider>, |
| 32 | +} |
| 33 | + |
| 34 | +impl FormatStringResolutionProvider { |
| 35 | + pub(crate) unsafe fn from_raw(handle: NonNull<BNFormatStringResolutionProvider>) -> Self { |
| 36 | + Self { handle } |
| 37 | + } |
| 38 | + |
| 39 | + /// Register a custom format string resolution provider. |
| 40 | + /// |
| 41 | + /// The provider is retained for the lifetime of the process. |
| 42 | + pub fn register<P>(name: &str, provider: P) -> Self |
| 43 | + where |
| 44 | + P: CustomFormatStringResolutionProvider, |
| 45 | + { |
| 46 | + let name = name.to_cstr(); |
| 47 | + // The core registry has no unregister operation, so the callback context must remain valid |
| 48 | + // for the lifetime of the process. |
| 49 | + let provider = Box::leak(Box::new(provider)); |
| 50 | + let mut callbacks = BNFormatStringResolutionProviderCallbacks { |
| 51 | + context: provider as *mut P as *mut c_void, |
| 52 | + isValid: Some(cb_is_valid::<P>), |
| 53 | + freeTypeList: Some(cb_free_type_list), |
| 54 | + }; |
| 55 | + let result = |
| 56 | + unsafe { BNRegisterFormatStringResolutionProvider(name.as_ptr(), &mut callbacks) }; |
| 57 | + let handle = |
| 58 | + NonNull::new(result).expect("failed to register format string resolution provider"); |
| 59 | + unsafe { Self::from_raw(handle) } |
| 60 | + } |
| 61 | + |
| 62 | + /// Retrieve all registered format string resolution providers. |
| 63 | + pub fn all() -> Array<Self> { |
| 64 | + let mut count = 0; |
| 65 | + let result = unsafe { BNGetFormatStringResolutionProviderList(&mut count) }; |
| 66 | + assert!(!result.is_null()); |
| 67 | + unsafe { Array::new(result, count, ()) } |
| 68 | + } |
| 69 | + |
| 70 | + /// Retrieve a registered format string resolution provider by name. |
| 71 | + pub fn by_name(name: &str) -> Option<Self> { |
| 72 | + let name = name.to_cstr(); |
| 73 | + let result = unsafe { BNGetFormatStringResolutionProviderByName(name.as_ptr()) }; |
| 74 | + NonNull::new(result).map(|handle| unsafe { Self::from_raw(handle) }) |
| 75 | + } |
| 76 | + |
| 77 | + /// Return the provider's registration name. |
| 78 | + pub fn name(&self) -> String { |
| 79 | + let result = unsafe { BNGetFormatStringResolutionProviderName(self.handle.as_ptr()) }; |
| 80 | + assert!(!result.is_null()); |
| 81 | + unsafe { BnString::into_string(result) } |
| 82 | + } |
| 83 | + |
| 84 | + /// Resolve the argument types described by `format` for `platform`. |
| 85 | + /// |
| 86 | + /// Return `None` when the string is not valid for this provider. A valid string with no |
| 87 | + /// arguments is represented by `Some(Vec::new())`. |
| 88 | + pub fn is_valid( |
| 89 | + &self, |
| 90 | + format: &str, |
| 91 | + platform: Option<&Platform>, |
| 92 | + ) -> Option<Vec<Conf<Ref<Type>>>> { |
| 93 | + let format = format.to_cstr(); |
| 94 | + let mut types = std::ptr::null_mut(); |
| 95 | + let mut count = 0; |
| 96 | + let valid = unsafe { |
| 97 | + BNFormatStringResolutionProviderIsValid( |
| 98 | + self.handle.as_ptr(), |
| 99 | + format.as_ptr(), |
| 100 | + platform.map_or(std::ptr::null_mut(), |platform| platform.handle), |
| 101 | + &mut types, |
| 102 | + &mut count, |
| 103 | + ) |
| 104 | + }; |
| 105 | + |
| 106 | + if !valid { |
| 107 | + if !types.is_null() { |
| 108 | + unsafe { BNFreeTypeWithConfidenceList(types, count) }; |
| 109 | + } |
| 110 | + return None; |
| 111 | + } |
| 112 | + |
| 113 | + if count == 0 { |
| 114 | + if !types.is_null() { |
| 115 | + unsafe { BNFreeTypeWithConfidenceList(types, count) }; |
| 116 | + } |
| 117 | + return Some(Vec::new()); |
| 118 | + } |
| 119 | + |
| 120 | + if types.is_null() { |
| 121 | + return None; |
| 122 | + } |
| 123 | + |
| 124 | + let result = unsafe { std::slice::from_raw_parts(types, count) } |
| 125 | + .iter() |
| 126 | + .map(Conf::<Ref<Type>>::from_raw) |
| 127 | + .collect(); |
| 128 | + unsafe { BNFreeTypeWithConfidenceList(types, count) }; |
| 129 | + Some(result) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +impl Debug for FormatStringResolutionProvider { |
| 134 | + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 135 | + f.debug_struct("FormatStringResolutionProvider") |
| 136 | + .field("name", &self.name()) |
| 137 | + .finish() |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +unsafe impl Send for FormatStringResolutionProvider {} |
| 142 | +unsafe impl Sync for FormatStringResolutionProvider {} |
| 143 | + |
| 144 | +impl CoreArrayProvider for FormatStringResolutionProvider { |
| 145 | + type Raw = *mut BNFormatStringResolutionProvider; |
| 146 | + type Context = (); |
| 147 | + type Wrapped<'a> = Self; |
| 148 | +} |
| 149 | + |
| 150 | +unsafe impl CoreArrayProviderInner for FormatStringResolutionProvider { |
| 151 | + unsafe fn free(raw: *mut Self::Raw, _count: usize, _context: &Self::Context) { |
| 152 | + BNFreeFormatStringResolutionProviderList(raw); |
| 153 | + } |
| 154 | + |
| 155 | + unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> { |
| 156 | + let handle = |
| 157 | + NonNull::new(*raw).expect("format string resolution provider list contained null"); |
| 158 | + Self::from_raw(handle) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +unsafe extern "C" fn cb_is_valid<P>( |
| 163 | + ctxt: *mut c_void, |
| 164 | + format: *const c_char, |
| 165 | + platform: *mut BNPlatform, |
| 166 | + types: *mut *mut BNTypeWithConfidence, |
| 167 | + count: *mut usize, |
| 168 | +) -> bool |
| 169 | +where |
| 170 | + P: CustomFormatStringResolutionProvider, |
| 171 | +{ |
| 172 | + ffi_wrap!("CustomFormatStringResolutionProvider::is_valid", unsafe { |
| 173 | + if types.is_null() || count.is_null() { |
| 174 | + return false; |
| 175 | + } |
| 176 | + *types = std::ptr::null_mut(); |
| 177 | + *count = 0; |
| 178 | + |
| 179 | + let Some(format) = raw_to_string(format) else { |
| 180 | + return false; |
| 181 | + }; |
| 182 | + let provider = &*(ctxt as *const P); |
| 183 | + let platform = NonNull::new(platform).map(|handle| Platform::from_raw(handle.as_ptr())); |
| 184 | + let Some(result) = provider.is_valid(&format, platform.as_ref()) else { |
| 185 | + return false; |
| 186 | + }; |
| 187 | + |
| 188 | + let raw_types: Box<[BNTypeWithConfidence]> = result |
| 189 | + .into_iter() |
| 190 | + .map(Conf::<Ref<Type>>::into_raw) |
| 191 | + .collect(); |
| 192 | + *count = raw_types.len(); |
| 193 | + if raw_types.is_empty() { |
| 194 | + true |
| 195 | + } else { |
| 196 | + *types = Box::leak(raw_types).as_mut_ptr(); |
| 197 | + true |
| 198 | + } |
| 199 | + }) |
| 200 | +} |
| 201 | + |
| 202 | +unsafe extern "C" fn cb_free_type_list( |
| 203 | + _ctxt: *mut c_void, |
| 204 | + types: *mut BNTypeWithConfidence, |
| 205 | + count: usize, |
| 206 | +) { |
| 207 | + ffi_wrap!( |
| 208 | + "CustomFormatStringResolutionProvider::free_type_list", |
| 209 | + unsafe { |
| 210 | + if types.is_null() { |
| 211 | + return; |
| 212 | + } |
| 213 | + let types = Box::from_raw(std::ptr::slice_from_raw_parts_mut(types, count)); |
| 214 | + for ty in types { |
| 215 | + Conf::<Ref<Type>>::free_raw(ty); |
| 216 | + } |
| 217 | + } |
| 218 | + ) |
| 219 | +} |
0 commit comments