Skip to content

Commit 6b268f8

Browse files
committed
Merge branch 'name-and-type-transparent' of github.com:rbran/binaryninja-api into dev
2 parents e0fb875 + 562fbfa commit 6b268f8

File tree

6 files changed

+129
-59
lines changed

6 files changed

+129
-59
lines changed

rust/examples/dwarf/dwarf_export/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -551,23 +551,23 @@ fn export_data_vars(
551551
dwarf.unit.get_mut(var_die_uid).set(
552552
gimli::DW_AT_name,
553553
AttributeValue::String(
554-
format!("data_{:x}", data_variable.address)
554+
format!("data_{:x}", data_variable.address())
555555
.as_bytes()
556556
.to_vec(),
557557
),
558558
);
559559
}
560560

561561
let mut variable_location = Expression::new();
562-
variable_location.op_addr(Address::Constant(data_variable.address));
562+
variable_location.op_addr(Address::Constant(data_variable.address()));
563563
dwarf.unit.get_mut(var_die_uid).set(
564564
gimli::DW_AT_location,
565565
AttributeValue::Exprloc(variable_location),
566566
);
567567

568568
if let Some(target_die_uid) = export_type(
569-
format!("{}", data_variable.t.contents),
570-
data_variable.t.contents.as_ref(),
569+
format!("{}", data_variable.t()),
570+
data_variable.t(),
571571
bv,
572572
defined_types,
573573
dwarf,

rust/examples/dwarf/shared/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ pub fn create_section_reader<'a, Endian: 'a + Endianity>(
8888
if let Some(data_var) = view
8989
.data_variables()
9090
.iter()
91-
.find(|var| var.address == symbol.address())
91+
.find(|var| var.address() == symbol.address())
9292
{
9393
// TODO : This should eventually be wrapped by some DataView sorta thingy thing, like how python does it
94-
let data_type = data_var.type_with_confidence().contents;
95-
let data = view.read_vec(data_var.address, data_type.width() as usize);
94+
let data_type = data_var.t();
95+
let data = view.read_vec(data_var.address(), data_type.width() as usize);
9696
let element_type = data_type.element_type().unwrap().contents;
9797

9898
if let Some(current_section_header) = data

rust/src/architecture.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ pub trait Intrinsic: Sized + Clone + Copy {
313313
fn id(&self) -> u32;
314314

315315
/// Reeturns the list of the input names and types for this intrinsic.
316-
fn inputs(&self) -> Vec<NameAndType<String>>;
316+
fn inputs(&self) -> Vec<Ref<NameAndType>>;
317317

318318
/// Returns the list of the output types for this intrinsic.
319319
fn outputs(&self) -> Vec<Conf<Ref<Type>>>;
@@ -650,7 +650,7 @@ impl Intrinsic for UnusedIntrinsic {
650650
fn id(&self) -> u32 {
651651
unreachable!()
652652
}
653-
fn inputs(&self) -> Vec<NameAndType<String>> {
653+
fn inputs(&self) -> Vec<Ref<NameAndType>> {
654654
unreachable!()
655655
}
656656
fn outputs(&self) -> Vec<Conf<Ref<Type>>> {
@@ -992,7 +992,7 @@ impl Intrinsic for crate::architecture::CoreIntrinsic {
992992
self.1
993993
}
994994

995-
fn inputs(&self) -> Vec<NameAndType<String>> {
995+
fn inputs(&self) -> Vec<Ref<NameAndType>> {
996996
let mut count: usize = 0;
997997

998998
unsafe {
@@ -2423,7 +2423,7 @@ where
24232423
let inputs = intrinsic.inputs();
24242424
let mut res = Vec::with_capacity(inputs.len());
24252425
for input in inputs {
2426-
res.push(input.into_raw());
2426+
res.push(unsafe { Ref::into_raw(input) }.into_raw());
24272427
}
24282428

24292429
unsafe {

rust/src/binaryview.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,16 +574,24 @@ pub trait BinaryViewExt: BinaryViewBase {
574574
}
575575
}
576576

577-
fn define_auto_data_var(&self, dv: DataVariable) {
577+
fn define_auto_data_var(&self, dv: &DataVariable) {
578578
unsafe {
579-
BNDefineDataVariable(self.as_ref().handle, dv.address, &mut dv.t.into());
579+
BNDefineDataVariable(
580+
self.as_ref().handle,
581+
dv.address(),
582+
&mut dv.type_with_confidence().into(),
583+
);
580584
}
581585
}
582586

583587
/// You likely would also like to call [`Self::define_user_symbol`] to bind this data variable with a name
584-
fn define_user_data_var(&self, dv: DataVariable) {
588+
fn define_user_data_var(&self, dv: &DataVariable) {
585589
unsafe {
586-
BNDefineUserDataVariable(self.as_ref().handle, dv.address, &mut dv.t.into());
590+
BNDefineUserDataVariable(
591+
self.as_ref().handle,
592+
dv.address(),
593+
&mut dv.type_with_confidence().into(),
594+
);
587595
}
588596
}
589597

rust/src/debuginfo.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ impl DebugInfo {
376376
}
377377

378378
/// Returns a generator of all types provided by a named DebugInfoParser
379-
pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<NameAndType<String>> {
379+
pub fn types_by_name<S: BnStrCompatible>(&self, parser_name: S) -> Vec<Ref<NameAndType>> {
380380
let parser_name = parser_name.into_bytes_with_nul();
381381

382382
let mut count: usize = 0;
@@ -387,10 +387,10 @@ impl DebugInfo {
387387
&mut count,
388388
)
389389
};
390-
let result: Vec<NameAndType<String>> = unsafe {
390+
let result: Vec<Ref<NameAndType>> = unsafe {
391391
slice::from_raw_parts_mut(debug_types_ptr, count)
392392
.iter()
393-
.map(NameAndType::<String>::from_raw)
393+
.map(NameAndType::from_raw)
394394
.collect()
395395
};
396396

@@ -399,13 +399,13 @@ impl DebugInfo {
399399
}
400400

401401
/// A generator of all types provided by DebugInfoParsers
402-
pub fn types(&self) -> Vec<NameAndType<String>> {
402+
pub fn types(&self) -> Vec<Ref<NameAndType>> {
403403
let mut count: usize = 0;
404404
let debug_types_ptr = unsafe { BNGetDebugTypes(self.handle, ptr::null_mut(), &mut count) };
405-
let result: Vec<NameAndType<String>> = unsafe {
405+
let result: Vec<Ref<NameAndType>> = unsafe {
406406
slice::from_raw_parts_mut(debug_types_ptr, count)
407407
.iter()
408-
.map(NameAndType::<String>::from_raw)
408+
.map(NameAndType::from_raw)
409409
.collect()
410410
};
411411

rust/src/types.rs

Lines changed: 100 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ pub type MemberScope = BNMemberScope;
5656
////////////////
5757
// Confidence
5858

59+
/// Compatible with the `BNType*WithConfidence` types
5960
pub struct Conf<T> {
6061
pub contents: T,
6162
pub confidence: u8,
@@ -698,6 +699,7 @@ impl Drop for TypeBuilder {
698699
//////////
699700
// Type
700701

702+
#[repr(transparent)]
701703
pub struct Type {
702704
pub(crate) handle: *mut BNType,
703705
}
@@ -2447,13 +2449,11 @@ unsafe impl CoreArrayWrapper for QualifiedNameTypeAndId {
24472449
//////////////////////////
24482450
// NameAndType
24492451

2450-
pub struct NameAndType<S: BnStrCompatible> {
2451-
pub name: S,
2452-
pub t: Conf<Ref<Type>>,
2453-
}
2452+
#[repr(transparent)]
2453+
pub struct NameAndType(pub(crate) BNNameAndType);
24542454

2455-
impl NameAndType<String> {
2456-
pub(crate) fn from_raw(raw: &BNNameAndType) -> Self {
2455+
impl NameAndType {
2456+
pub(crate) fn from_raw(raw: &BNNameAndType) -> Ref<Self> {
24572457
Self::new(
24582458
raw_to_string(raw.name).unwrap(),
24592459
unsafe { &Type::ref_from_raw(raw.type_) },
@@ -2462,43 +2462,73 @@ impl NameAndType<String> {
24622462
}
24632463
}
24642464

2465-
impl<S: BnStrCompatible> NameAndType<S> {
2466-
pub fn new(name: S, t: &Ref<Type>, confidence: u8) -> Self {
2467-
Self {
2468-
name,
2469-
t: Conf::new(t.clone(), confidence),
2465+
impl NameAndType {
2466+
pub fn new<S: BnStrCompatible>(name: S, t: &Type, confidence: u8) -> Ref<Self> {
2467+
unsafe {
2468+
Ref::new(Self(BNNameAndType {
2469+
name: BNAllocString(name.into_bytes_with_nul().as_ref().as_ptr() as *mut _),
2470+
type_: Ref::into_raw(t.to_owned()).handle,
2471+
typeConfidence: confidence,
2472+
}))
24702473
}
24712474
}
24722475

24732476
pub(crate) fn into_raw(self) -> BNNameAndType {
2474-
let t = self.t.clone();
2475-
let res = BNNameAndType {
2476-
name: BnString::new(self.name).into_raw(),
2477-
type_: t.contents.handle,
2478-
typeConfidence: self.t.confidence,
2479-
};
2480-
mem::forget(t);
2481-
res
2477+
self.0
24822478
}
24832479

2484-
pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
2485-
self.t.clone()
2480+
pub fn name(&self) -> &str {
2481+
let c_str = unsafe { CStr::from_ptr(self.0.name) };
2482+
c_str.to_str().unwrap()
2483+
}
2484+
2485+
pub fn t(&self) -> &Type {
2486+
unsafe { mem::transmute::<_, &Type>(&self.0.type_) }
2487+
}
2488+
2489+
pub fn type_with_confidence(&self) -> Conf<&Type> {
2490+
Conf::new(self.t(), self.0.typeConfidence)
2491+
}
2492+
}
2493+
2494+
impl ToOwned for NameAndType {
2495+
type Owned = Ref<Self>;
2496+
2497+
fn to_owned(&self) -> Self::Owned {
2498+
unsafe { RefCountable::inc_ref(self) }
2499+
}
2500+
}
2501+
2502+
unsafe impl RefCountable for NameAndType {
2503+
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
2504+
Self::new(
2505+
CStr::from_ptr(handle.0.name),
2506+
handle.t(),
2507+
handle.type_with_confidence().confidence,
2508+
)
2509+
}
2510+
2511+
unsafe fn dec_ref(handle: &Self) {
2512+
unsafe {
2513+
BNFreeString(handle.0.name);
2514+
RefCountable::dec_ref(handle.t());
2515+
}
24862516
}
24872517
}
24882518

2489-
impl<S: BnStrCompatible> CoreArrayProvider for NameAndType<S> {
2519+
impl CoreArrayProvider for NameAndType {
24902520
type Raw = BNNameAndType;
24912521
type Context = ();
24922522
}
24932523

2494-
unsafe impl<S: BnStrCompatible> CoreOwnedArrayProvider for NameAndType<S> {
2524+
unsafe impl CoreOwnedArrayProvider for NameAndType {
24952525
unsafe fn free(raw: *mut Self::Raw, count: usize, _context: &Self::Context) {
24962526
BNFreeNameAndTypeList(raw, count);
24972527
}
24982528
}
24992529

2500-
unsafe impl<S: BnStrCompatible> CoreArrayWrapper for NameAndType<S> {
2501-
type Wrapped<'a> = &'a NameAndType<S> where S: 'a;
2530+
unsafe impl CoreArrayWrapper for NameAndType {
2531+
type Wrapped<'a> = &'a NameAndType;
25022532

25032533
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
25042534
mem::transmute(raw)
@@ -2508,29 +2538,61 @@ unsafe impl<S: BnStrCompatible> CoreArrayWrapper for NameAndType<S> {
25082538
//////////////////
25092539
// DataVariable
25102540

2511-
pub struct DataVariable {
2512-
pub address: u64,
2513-
pub t: Conf<Ref<Type>>,
2514-
pub auto_discovered: bool,
2515-
}
2541+
#[repr(transparent)]
2542+
pub struct DataVariable(pub(crate) BNDataVariable);
25162543

25172544
// impl DataVariable {
25182545
// pub(crate) fn from_raw(var: &BNDataVariable) -> Self {
2519-
// Self {
2520-
// address: var.address,
2521-
// t: Conf::new(unsafe { Type::ref_from_raw(var.type_) }, var.typeConfidence),
2522-
// auto_discovered: var.autoDiscovered,
2523-
// }
2546+
// let var = DataVariable(*var);
2547+
// Self(BNDataVariable {
2548+
// type_: unsafe { Ref::into_raw(var.t().to_owned()).handle },
2549+
// ..var.0
2550+
// })
25242551
// }
25252552
// }
25262553

25272554
impl DataVariable {
2528-
pub fn type_with_confidence(&self) -> Conf<Ref<Type>> {
2529-
Conf::new(self.t.contents.clone(), self.t.confidence)
2555+
pub fn address(&self) -> u64 {
2556+
self.0.address
2557+
}
2558+
2559+
pub fn auto_discovered(&self) -> bool {
2560+
self.0.autoDiscovered
2561+
}
2562+
2563+
pub fn t(&self) -> &Type {
2564+
unsafe { mem::transmute(&self.0.type_) }
2565+
}
2566+
2567+
pub fn type_with_confidence(&self) -> Conf<&Type> {
2568+
Conf::new(self.t(), self.0.typeConfidence)
25302569
}
25312570

25322571
pub fn symbol(&self, bv: &BinaryView) -> Option<Ref<Symbol>> {
2533-
bv.symbol_by_address(self.address).ok()
2572+
bv.symbol_by_address(self.0.address).ok()
2573+
}
2574+
}
2575+
2576+
impl ToOwned for DataVariable {
2577+
type Owned = Ref<Self>;
2578+
2579+
fn to_owned(&self) -> Self::Owned {
2580+
unsafe { RefCountable::inc_ref(self) }
2581+
}
2582+
}
2583+
2584+
unsafe impl RefCountable for DataVariable {
2585+
unsafe fn inc_ref(handle: &Self) -> Ref<Self> {
2586+
unsafe {
2587+
Ref::new(Self(BNDataVariable {
2588+
type_: Ref::into_raw(handle.t().to_owned()).handle,
2589+
..handle.0
2590+
}))
2591+
}
2592+
}
2593+
2594+
unsafe fn dec_ref(handle: &Self) {
2595+
unsafe { BNFreeType(handle.0.type_) }
25342596
}
25352597
}
25362598

0 commit comments

Comments
 (0)