diff options
| -rw-r--r-- | rust/src/llil/lifting.rs | 44 | ||||
| -rw-r--r-- | rust/src/section.rs | 22 | ||||
| -rw-r--r-- | rust/src/segment.rs | 3 | ||||
| -rw-r--r-- | rust/src/symbol.rs | 18 | ||||
| -rw-r--r-- | rust/src/types.rs | 120 |
5 files changed, 63 insertions, 144 deletions
diff --git a/rust/src/llil/lifting.rs b/rust/src/llil/lifting.rs index 1f019c85..a4d3c560 100644 --- a/rust/src/llil/lifting.rs +++ b/rust/src/llil/lifting.rs @@ -619,33 +619,7 @@ where self } - pub fn into_expr(self) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R> { - self.into() - } - - pub fn with_source_operand( - self, - op: u32, - ) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R> { - let expr = self.into_expr(); - expr.with_source_operand(op) - } - - pub fn append(self) { - let expr = self.into_expr(); - let il = expr.function; - - il.instruction(expr); - } -} - -impl<'a, A, R> Into<Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R>> - for ExpressionBuilder<'a, A, R> -where - A: 'a + Architecture, - R: ExpressionResultType, -{ - fn into(self) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R> { + pub fn build(self) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R> { use binaryninjacore_sys::BNLowLevelILAddExpr; let expr_idx = unsafe { @@ -667,6 +641,20 @@ where _ty: PhantomData, } } + + pub fn with_source_operand( + self, + op: u32, + ) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, R> { + self.build().with_source_operand(op) + } + + pub fn append(self) { + let expr = self.build(); + let il = expr.function; + + il.instruction(expr); + } } impl<'a, A, R> Liftable<'a, A> for ExpressionBuilder<'a, A, R> @@ -682,7 +670,7 @@ where ) -> Expression<'a, A, Mutable, NonSSA<LiftedNonSSA>, Self::Result> { debug_assert!(expr.function.handle == il.handle); - expr.into() + expr.build() } } diff --git a/rust/src/section.rs b/rust/src/section.rs index 49642b4d..971204ba 100644 --- a/rust/src/section.rs +++ b/rust/src/section.rs @@ -70,13 +70,12 @@ impl Section { Self { handle: raw } } - #[allow(clippy::new_ret_no_self)] /// You need to create a section builder, customize that section, then add it to a binary view: /// /// ``` /// bv.add_section(Section::new().align(4).entry_size(4)) /// ``` - pub fn new<S: BnStrCompatible>(name: S, range: Range<u64>) -> SectionBuilder<S> { + pub fn builder<S: BnStrCompatible>(name: S, range: Range<u64>) -> SectionBuilder<S> { SectionBuilder::new(name, range) } @@ -204,7 +203,7 @@ pub struct SectionBuilder<S: BnStrCompatible> { impl<S: BnStrCompatible> SectionBuilder<S> { pub fn new(name: S, range: Range<u64>) -> Self { - SectionBuilder { + Self { is_auto: false, name, range, @@ -268,19 +267,12 @@ impl<S: BnStrCompatible> SectionBuilder<S> { let len = self.range.end.wrapping_sub(start); unsafe { - use std::ffi::CStr; - - let nul_str = CStr::from_bytes_with_nul_unchecked(b"\x00").as_ptr(); + let nul_str = std::ffi::CStr::from_bytes_with_nul_unchecked(b"\x00").as_ptr(); let name_ptr = name.as_ref().as_ptr() as *mut _; - let ty_ptr = ty - .as_ref() - .map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); - let linked_section_ptr = linked_section - .as_ref() - .map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); - let info_section_ptr = info_section - .as_ref() - .map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); + let ty_ptr = ty.map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); + let linked_section_ptr = + linked_section.map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); + let info_section_ptr = info_section.map_or(nul_str, |s| s.as_ref().as_ptr() as *mut _); if self.is_auto { BNAddAutoSection( diff --git a/rust/src/segment.rs b/rust/src/segment.rs index 285aa8a8..671c3569 100644 --- a/rust/src/segment.rs +++ b/rust/src/segment.rs @@ -115,13 +115,12 @@ impl Segment { Self { handle: raw } } - #[allow(clippy::new_ret_no_self)] /// You need to create a segment builder, customize that segment, then add it to a binary view: /// /// ``` /// bv.add_segment(Segment::new().align(4).entry_size(4)) /// ``` - pub fn new(ea_range: Range<u64>) -> SegmentBuilder { + pub fn builder(ea_range: Range<u64>) -> SegmentBuilder { SegmentBuilder::new(ea_range) } diff --git a/rust/src/symbol.rs b/rust/src/symbol.rs index 6b04582b..22c2b051 100644 --- a/rust/src/symbol.rs +++ b/rust/src/symbol.rs @@ -150,15 +150,11 @@ impl<S: BnStrCompatible> SymbolBuilder<S> { let short_name = self.short_name.map(|s| s.into_bytes_with_nul()); let full_name = self.full_name.map(|s| s.into_bytes_with_nul()); - unsafe { - let raw_name = raw_name.as_ref().as_ptr() as *mut _; - let short_name = short_name - .as_ref() - .map_or(raw_name, |s| s.as_ref().as_ptr() as *mut _); - let full_name = full_name - .as_ref() - .map_or(raw_name, |s| s.as_ref().as_ptr() as *mut _); + let raw_name = raw_name.as_ref().as_ptr() as *mut _; + let short_name = short_name.map_or(raw_name, |s| s.as_ref().as_ptr() as *mut _); + let full_name = full_name.map_or(raw_name, |s| s.as_ref().as_ptr() as *mut _); + unsafe { let res = BNCreateSymbol( self.ty.into(), short_name, @@ -175,6 +171,7 @@ impl<S: BnStrCompatible> SymbolBuilder<S> { } } +#[derive(Eq)] pub struct Symbol { pub(crate) handle: *mut BNSymbol, } @@ -184,13 +181,12 @@ impl Symbol { Self { handle: raw } } - #[allow(clippy::new_ret_no_self)] /// To create a new symbol, you need to create a symbol builder, customize that symbol, then add `SymbolBuilder::create` it into a `Ref<Symbol>`: /// /// ``` /// Symbol::new().short_name("hello").full_name("hello").create(); /// ``` - pub fn new<S: BnStrCompatible>(ty: SymbolType, raw_name: S, addr: u64) -> SymbolBuilder<S> { + pub fn builder<S: BnStrCompatible>(ty: SymbolType, raw_name: S, addr: u64) -> SymbolBuilder<S> { SymbolBuilder::new(ty, raw_name, addr) } @@ -298,5 +294,3 @@ impl PartialEq for Symbol { self.handle == other.handle } } - -impl Eq for Symbol {} diff --git a/rust/src/types.rs b/rust/src/types.rs index 75475b6e..5d524c76 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -295,10 +295,14 @@ impl TypeBuilder { pub(crate) unsafe fn from_raw(handle: *mut BNTypeBuilder) -> Self { debug_assert!(!handle.is_null()); - Self { handle } } + // Chainable terminal + pub fn finalize(&self) -> Ref<Type> { + unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(self.handle)) } + } + // Settable properties pub fn set_const<T: Into<Conf<bool>>>(&self, value: T) -> &Self { @@ -313,12 +317,6 @@ impl TypeBuilder { self } - // Chainable terminal - - pub fn finalize(&self) -> Ref<Type> { - unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(self.handle)) } - } - // Readable properties pub fn type_class(&self) -> TypeClass { @@ -424,7 +422,7 @@ impl TypeBuilder { if result.is_null() { Err(()) } else { - Ok(Enumeration::ref_from_raw(result)) + Ok(unsafe { Enumeration::ref_from_raw(result) }) } } @@ -670,13 +668,11 @@ pub struct Type { impl Type { unsafe fn from_raw(handle: *mut BNType) -> Self { debug_assert!(!handle.is_null()); - Self { handle } } pub(crate) unsafe fn ref_from_raw(handle: *mut BNType) -> Ref<Self> { debug_assert!(!handle.is_null()); - Ref::new(Self { handle }) } @@ -794,7 +790,7 @@ impl Type { if result.is_null() { Err(()) } else { - Ok(Enumeration::ref_from_raw(result)) + Ok(unsafe { Enumeration::ref_from_raw(result) }) } } @@ -1157,12 +1153,6 @@ impl Type { } } -impl From<&TypeBuilder> for Ref<Type> { - fn from(builder: &TypeBuilder) -> Self { - unsafe { Type::ref_from_raw(BNFinalizeTypeBuilder(builder.handle)) } - } -} - impl fmt::Display for Type { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", unsafe { @@ -1390,7 +1380,7 @@ impl EnumerationBuilder { } pub fn finalize(&self) -> Ref<Enumeration> { - Enumeration::new(self) + unsafe { Enumeration::ref_from_raw(BNFinalizeEnumerationBuilder(self.handle)) } } pub fn append<S: BnStrCompatible>(&self, name: S) -> &Self { @@ -1474,21 +1464,18 @@ pub struct Enumeration { } impl Enumeration { - pub fn new(builder: &EnumerationBuilder) -> Ref<Self> { - unsafe { - let handle = BNFinalizeEnumerationBuilder(builder.handle); - Ref::new(Self { handle }) - } - } - - fn from_raw(handle: *mut BNEnumeration) -> Self { + unsafe fn from_raw(handle: *mut BNEnumeration) -> Self { debug_assert!(!handle.is_null()); Self { handle } } - pub(crate) fn ref_from_raw(handle: *mut BNEnumeration) -> Ref<Self> { + pub(crate) unsafe fn ref_from_raw(handle: *mut BNEnumeration) -> Ref<Self> { debug_assert!(!handle.is_null()); - unsafe { Ref::new(Self { handle }) } + Ref::new(Self { handle }) + } + + pub fn builder() -> EnumerationBuilder { + EnumerationBuilder::new() } pub fn members(&self) -> Vec<EnumerationMember> { @@ -1509,15 +1496,9 @@ impl Enumeration { } } -impl From<&EnumerationBuilder> for Ref<Enumeration> { - fn from(builder: &EnumerationBuilder) -> Self { - Enumeration::new(builder) - } -} - unsafe impl RefCountable for Enumeration { unsafe fn inc_ref(handle: &Self) -> Ref<Self> { - Ref::new(Self::from_raw(BNNewEnumerationReference(handle.handle))) + Self::ref_from_raw(BNNewEnumerationReference(handle.handle)) } unsafe fn dec_ref(handle: &Self) { @@ -1543,15 +1524,15 @@ pub struct StructureBuilder { pub(crate) handle: *mut BNStructureBuilder, } -/// ``` +/// ```rust /// // Includes /// use binaryninja::types::{Structure, Type}; /// /// // Define struct, set size (in bytes) -/// let mut my_custom_struct = Structure::new();BnStr::from_raw(raw.name) -/// let field_1 = Self::named_int(5, false, "my_weird_int_type"); -/// let field_2 = Self::int(4, false); -/// let field_3 = Self::int(8, false); +/// let mut my_custom_struct = StructureBuilder::new(); +/// let field_1 = Type::named_int(5, false, "my_weird_int_type"); +/// let field_2 = Type::int(4, false); +/// let field_3 = Type::int(8, false); /// /// // Assign those fields /// my_custom_struct.append(&field_1, "field_4"); @@ -1580,7 +1561,7 @@ impl StructureBuilder { // Chainable terminal pub fn finalize(&self) -> Ref<Structure> { - Structure::new(self) + unsafe { Structure::ref_from_raw(BNFinalizeStructureBuilder(self.handle)) } } // Chainable builders/setters @@ -1609,13 +1590,18 @@ impl StructureBuilder { self } + pub fn set_structure_type(&self, t: StructureType) -> &Self { + unsafe { BNSetStructureBuilderType(self.handle, t) }; + self + } + pub fn append<'a, S: BnStrCompatible, T: Into<Conf<&'a Type>>>( - &'a self, + &self, t: T, name: S, access: MemberAccess, scope: MemberScope, - ) -> &'a Self { + ) -> &Self { let name = name.into_bytes_with_nul(); unsafe { BNAddStructureBuilderMember( @@ -1672,11 +1658,6 @@ impl StructureBuilder { self } - pub fn set_structure_type(&self, t: StructureType) -> &Self { - unsafe { BNSetStructureBuilderType(self.handle, t) }; - self - } - // Getters pub fn width(&self) -> u64 { @@ -1740,40 +1721,7 @@ pub struct Structure { pub(crate) handle: *mut BNStructure, } -/// ``` -/// // Includes -/// use binaryninja::types::{Structure, Type}; -/// -/// // Define struct, set size (in bytes) -/// let mut my_custom_struct = Structure::new(); -/// my_custom_struct.set_width(17); -/// -/// // Create some fields for the struct -/// let field_1 = Self::named_int(5, false, "my_weird_int_type"); -/// let field_2 = Self::int(4, false); -/// let field_3 = Self::int(8, false); -/// -/// // Assign those fields -/// my_custom_struct.append(&field_1, "field_4"); -/// my_custom_struct.insert(&field_1, "field_1", 0); -/// my_custom_struct.insert(&field_2, "field_2", 5); -/// my_custom_struct.insert(&field_3, "field_3", 9); -/// -/// // Convert structure to type -/// let my_custom_structure_type = Self::structure_type(&mut my_custom_struct); -/// -/// // Add the struct to the binary view to use in analysis -/// let bv = unsafe { BinaryView::from_raw(view) }; -/// bv.define_user_type("my_custom_struct", &my_custom_structure_type); -/// ``` impl Structure { - pub fn new(builder: &StructureBuilder) -> Ref<Self> { - unsafe { - let handle = BNFinalizeStructureBuilder(builder.handle); - Ref::new(Self { handle }) - } - } - unsafe fn from_raw(handle: *mut BNStructure) -> Self { debug_assert!(!handle.is_null()); Self { handle } @@ -1784,6 +1732,10 @@ impl Structure { Ref::new(Self { handle }) } + pub fn builder() -> StructureBuilder { + StructureBuilder::new() + } + pub fn width(&self) -> u64 { unsafe { BNGetStructureWidth(self.handle) } } @@ -1815,12 +1767,6 @@ impl Structure { // TODO : The other methods in the python version (alignment, packed, type, members, remove, replace, etc) } -impl From<&StructureBuilder> for Ref<Structure> { - fn from(builder: &StructureBuilder) -> Self { - Structure::new(builder) - } -} - impl Debug for Structure { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { write!(f, "Structure {{")?; |
