diff options
| author | Rusty Wagner <rusty.wagner@gmail.com> | 2024-08-22 12:55:49 -0600 |
|---|---|---|
| committer | Rusty Wagner <rusty.wagner@gmail.com> | 2024-10-21 13:56:55 -0400 |
| commit | d8e3001e535fad178c621ff07418f81f25123dc4 (patch) | |
| tree | 54c03cef2f0bdfd9a3b6df4334c81becb63e4993 /rust/src | |
| parent | 0e281a30d73c0f31ef9442fef0346779164231ad (diff) | |
Allow multiple high level representations for display, add Pseudo Rust and a Pseudo Python example plugin
Diffstat (limited to 'rust/src')
| -rw-r--r-- | rust/src/databuffer.rs | 4 | ||||
| -rw-r--r-- | rust/src/function.rs | 83 | ||||
| -rw-r--r-- | rust/src/linearview.rs | 11 |
3 files changed, 91 insertions, 7 deletions
diff --git a/rust/src/databuffer.rs b/rust/src/databuffer.rs index c3d268cd..29d7636b 100644 --- a/rust/src/databuffer.rs +++ b/rust/src/databuffer.rs @@ -109,8 +109,8 @@ impl DataBuffer { } } - pub fn to_escaped_string(&self, null_terminates: bool) -> BnString { - unsafe { BnString::from_raw(BNDataBufferToEscapedString(self.0, null_terminates)) } + pub fn to_escaped_string(&self, null_terminates: bool, escape_printable: bool) -> BnString { + unsafe { BnString::from_raw(BNDataBufferToEscapedString(self.0, null_terminates, escape_printable)) } } pub fn from_escaped_string(value: &BnString) -> Self { diff --git a/rust/src/function.rs b/rust/src/function.rs index 01b086f2..0508ca4d 100644 --- a/rust/src/function.rs +++ b/rust/src/function.rs @@ -39,6 +39,7 @@ use crate::{databuffer::DataBuffer, disassembly::InstructionTextToken, rc::*}; pub use binaryninjacore_sys::BNAnalysisSkipReason as AnalysisSkipReason; pub use binaryninjacore_sys::BNFunctionAnalysisSkipOverride as FunctionAnalysisSkipOverride; pub use binaryninjacore_sys::BNFunctionUpdateType as FunctionUpdateType; +pub use binaryninjacore_sys::BNBuiltinType as BuiltinType; use std::{fmt, mem}; use std::{ffi::c_char, hash::Hash, ops::Range}; @@ -124,6 +125,78 @@ impl BlockContext for NativeBlock { } } +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum FunctionViewType { + Normal, + LowLevelIL, + LiftedIL, + LowLevelILSSAForm, + MediumLevelIL, + MediumLevelILSSAForm, + MappedMediumLevelIL, + MappedMediumLevelILSSAForm, + HighLevelIL, + HighLevelILSSAForm, + HighLevelLanguageRepresentation(String), +} + +pub(crate) struct RawFunctionViewType(pub BNFunctionViewType); + +impl FunctionViewType { + pub(crate) fn as_raw(&self) -> RawFunctionViewType { + let view_type = match self { + FunctionViewType::Normal => BNFunctionGraphType::NormalFunctionGraph, + FunctionViewType::LowLevelIL => BNFunctionGraphType::LowLevelILFunctionGraph, + FunctionViewType::LiftedIL => BNFunctionGraphType::LiftedILFunctionGraph, + FunctionViewType::LowLevelILSSAForm => BNFunctionGraphType::LowLevelILSSAFormFunctionGraph, + FunctionViewType::MediumLevelIL => BNFunctionGraphType::MediumLevelILFunctionGraph, + FunctionViewType::MediumLevelILSSAForm => BNFunctionGraphType::MediumLevelILSSAFormFunctionGraph, + FunctionViewType::MappedMediumLevelIL => BNFunctionGraphType::MappedMediumLevelILFunctionGraph, + FunctionViewType::MappedMediumLevelILSSAForm => BNFunctionGraphType::MappedMediumLevelILSSAFormFunctionGraph, + FunctionViewType::HighLevelIL => BNFunctionGraphType::HighLevelILFunctionGraph, + FunctionViewType::HighLevelILSSAForm => BNFunctionGraphType::HighLevelILSSAFormFunctionGraph, + FunctionViewType::HighLevelLanguageRepresentation(_) => BNFunctionGraphType::HighLevelLanguageRepresentationFunctionGraph, + }; + RawFunctionViewType(BNFunctionViewType { + type_: view_type, + name: if let FunctionViewType::HighLevelLanguageRepresentation(ref name) = self { + std::ffi::CString::new(name.to_string()).unwrap().into_raw() + } else { + std::ptr::null() + }, + }) + } +} + +impl Into<FunctionViewType> for FunctionGraphType { + fn into(self) -> FunctionViewType { + match self { + BNFunctionGraphType::LowLevelILFunctionGraph => FunctionViewType::LowLevelIL, + BNFunctionGraphType::LiftedILFunctionGraph => FunctionViewType::LiftedIL, + BNFunctionGraphType::LowLevelILSSAFormFunctionGraph => FunctionViewType::LowLevelILSSAForm, + BNFunctionGraphType::MediumLevelILFunctionGraph => FunctionViewType::MediumLevelIL, + BNFunctionGraphType::MediumLevelILSSAFormFunctionGraph => FunctionViewType::MediumLevelILSSAForm, + BNFunctionGraphType::MappedMediumLevelILFunctionGraph => FunctionViewType::MappedMediumLevelIL, + BNFunctionGraphType::MappedMediumLevelILSSAFormFunctionGraph => FunctionViewType::MappedMediumLevelILSSAForm, + BNFunctionGraphType::HighLevelILFunctionGraph => FunctionViewType::HighLevelIL, + BNFunctionGraphType::HighLevelILSSAFormFunctionGraph => FunctionViewType::HighLevelILSSAForm, + BNFunctionGraphType::HighLevelLanguageRepresentationFunctionGraph => { + FunctionViewType::HighLevelLanguageRepresentation("Pseudo C".into() + ) + } + _ => FunctionViewType::Normal, + } + } +} + +impl Drop for RawFunctionViewType { + fn drop(&mut self) { + if !self.0.name.is_null() { + unsafe { let _ = std::ffi::CString::from_raw(self.0.name as *mut _); } + } + } +} + #[derive(Eq)] pub struct Function { pub(crate) handle: *mut BNFunction, @@ -1205,10 +1278,12 @@ impl Function { state: RegisterValueType, value: u64, size: Option<usize>, - ) -> DataBuffer { + ) -> (DataBuffer, BuiltinType) { let size = size.unwrap_or(0); let state_raw = state.into_raw_value(); - DataBuffer::from_raw(unsafe { BNGetConstantData(self.handle, state_raw, value, size) }) + let mut builtin_type = BuiltinType::BuiltinNone; + let buffer = DataBuffer::from_raw(unsafe { BNGetConstantData(self.handle, state_raw, value, size, &mut builtin_type) }); + (buffer, builtin_type) } pub fn constants_referenced_by( @@ -2141,11 +2216,11 @@ impl Function { pub fn create_graph( &self, - graph_type: FunctionGraphType, + view_type: FunctionViewType, settings: Option<DisassemblySettings>, ) -> Ref<FlowGraph> { let settings_raw = settings.map(|s| s.handle).unwrap_or(core::ptr::null_mut()); - let result = unsafe { BNCreateFunctionGraph(self.handle, graph_type, settings_raw) }; + let result = unsafe { BNCreateFunctionGraph(self.handle, view_type.as_raw().0, settings_raw) }; unsafe { Ref::new(FlowGraph::from_raw(result)) } } diff --git a/rust/src/linearview.rs b/rust/src/linearview.rs index 84b4323e..b90d2784 100644 --- a/rust/src/linearview.rs +++ b/rust/src/linearview.rs @@ -103,11 +103,17 @@ impl LinearViewObject { } } - pub fn language_representation(view: &BinaryView, settings: &DisassemblySettings) -> Ref<Self> { + pub fn language_representation( + view: &BinaryView, + settings: &DisassemblySettings, + language: &str, + ) -> Ref<Self> { unsafe { + let language = std::ffi::CString::new(language).unwrap(); let handle = binaryninjacore_sys::BNCreateLinearViewLanguageRepresentation( view.handle, settings.handle, + language.as_ptr(), ); Self::from_raw(handle) @@ -195,12 +201,15 @@ impl LinearViewObject { pub fn single_function_language_representation( function: &Function, settings: &DisassemblySettings, + language: &str, ) -> Ref<Self> { unsafe { + let language = std::ffi::CString::new(language).unwrap(); let handle = binaryninjacore_sys::BNCreateLinearViewSingleFunctionLanguageRepresentation( function.handle, settings.handle, + language.as_ptr(), ); Self::from_raw(handle) |
