Skip to content

Commit

Permalink
fix the array implementation using GAT
Browse files Browse the repository at this point in the history
  • Loading branch information
rbran committed Apr 10, 2024
1 parent 16fc310 commit b4abfb8
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 113 deletions.
8 changes: 4 additions & 4 deletions rust/src/backgroundtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ unsafe impl CoreOwnedArrayProvider for BackgroundTask {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for BackgroundTask {
type Wrapped = Guard<'a, BackgroundTask>;
unsafe impl CoreArrayWrapper for BackgroundTask {
type Wrapped<'a> = Guard<'a, BackgroundTask>;

unsafe fn wrap_raw(
unsafe fn wrap_raw<'a>(
raw: &'a *mut BNBackgroundTask,
context: &'a (),
) -> Guard<'a, BackgroundTask> {
) -> Self::Wrapped<'a> {
Guard::new(BackgroundTask::from_raw(*raw), context)
}
}
Expand Down
12 changes: 6 additions & 6 deletions rust/src/basicblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ unsafe impl<'a, C: 'a + BlockContext> CoreOwnedArrayProvider for Edge<'a, C> {
}
}

unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for Edge<'a, C> {
type Wrapped = Edge<'a, C>;
unsafe impl<'a, C: BlockContext> CoreArrayWrapper for Edge<'a, C> {
type Wrapped<'b> = Edge<'b, C> where 'a: 'b;

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Edge<'a, C> {
unsafe fn wrap_raw<'b>(raw: &'b Self::Raw, context: &'b Self::Context) -> Self::Wrapped<'b> {
let edge_target = Guard::new(
BasicBlock::from_raw(raw.target, context.orig_block.context.clone()),
raw,
Expand Down Expand Up @@ -309,10 +309,10 @@ unsafe impl<C: BlockContext> CoreOwnedArrayProvider for BasicBlock<C> {
}
}

unsafe impl<'a, C: 'a + BlockContext> CoreArrayWrapper<'a> for BasicBlock<C> {
type Wrapped = Guard<'a, BasicBlock<C>>;
unsafe impl<C: BlockContext> CoreArrayWrapper for BasicBlock<C> {
type Wrapped<'a> = Guard<'a, BasicBlock<C>> where C: 'a;

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(BasicBlock::from_raw(*raw, context.clone()), context)
}
}
6 changes: 3 additions & 3 deletions rust/src/callingconvention.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,10 +662,10 @@ unsafe impl<A: Architecture> CoreOwnedArrayProvider for CallingConvention<A> {
}
}

unsafe impl<'a, A: Architecture> CoreArrayWrapper<'a> for CallingConvention<A> {
type Wrapped = Guard<'a, CallingConvention<A>>;
unsafe impl<A: Architecture> CoreArrayWrapper for CallingConvention<A> {
type Wrapped<'a> = Guard<'a, CallingConvention<A>>;

unsafe fn wrap_raw(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(
CallingConvention {
handle: *raw,
Expand Down
8 changes: 5 additions & 3 deletions rust/src/custombinaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,12 @@ unsafe impl CoreOwnedArrayProvider for BinaryViewType {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for BinaryViewType {
type Wrapped = BinaryViewType;
unsafe impl CoreArrayWrapper for BinaryViewType {
// TODO there is nothing blocking the returned value from out-living the
// array, change it to &_ or Guard?
type Wrapped<'a> = BinaryViewType;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
BinaryViewType(*raw)
}
}
Expand Down
104 changes: 104 additions & 0 deletions rust/src/datarenderer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
use binaryninjacore_sys::*;

use crate::{
binaryview::BinaryView,
disassembly::{DisassemblyTextLine, InstructionTextToken},
rc::Array,
types::Type,
};

//pub struct BNCustomDataRenderer {
// pub context: *mut ::std::os::raw::c_void,
// pub freeObject: ::std::option::Option<unsafe extern "C" fn(ctxt: *mut ::std::os::raw::c_void)>,
// pub isValidForData: ::std::option::Option<
// unsafe extern "C" fn(
// ctxt: *mut ::std::os::raw::c_void,
// view: *mut BNBinaryView,
// addr: u64,
// type_: *mut BNType,
// typeCtx: *mut BNTypeContext,
// ctxCount: usize,
// ) -> bool,
// >,
// pub getLinesForData: ::std::option::Option<
// unsafe extern "C" fn(
// ctxt: *mut ::std::os::raw::c_void,
// view: *mut BNBinaryView,
// addr: u64,
// type_: *mut BNType,
// prefix: *const BNInstructionTextToken,
// prefixCount: usize,
// width: usize,
// count: *mut usize,
// typeCtx: *mut BNTypeContext,
// ctxCount: usize,
// ) -> *mut BNDisassemblyTextLine,
// >,
// pub freeLines: ::std::option::Option<
// unsafe extern "C" fn(
// ctx: *mut ::std::os::raw::c_void,
// lines: *mut BNDisassemblyTextLine,
// count: usize,
// ),
// >,
//}

pub struct DataRenderer(*mut BNDataRenderer);

pub fn new_data_renderer() -> DataRenderer {
let dr = Box::new(BNCustomDataRenderer {
context: std::ptr::null_mut(),
freeObject: Some(default_free_object),
isValidForData: None,
getLinesForData: None,
freeLines: None,
});
let handle = unsafe { BNCreateDataRenderer(Box::leak(dr)) };
assert!(!handle.is_null());
DataRenderer(handle)
}

unsafe extern "C" fn default_free_object(ctxt: *mut ::std::os::raw::c_void) {
drop(Box::from_raw(ctxt))
}

unsafe extern "C" fn default_free_lines(
ctx: *mut ::std::os::raw::c_void,
lines: *mut BNDisassemblyTextLine,
count: usize,
) {
let lines: *mut [BNDisassemblyTextLine] = core::ptr::slice_from_raw_parts_mut(lines, count);
let lines: Box<[BNDisassemblyTextLine]> = Box::from_raw(lines);
// TODO: make DisassemblyTextLine transparent
let lines: Box<[DisassemblyTextLine]> = core::mem::transmute(lines);
for _ in 0..count {
drop(Box::from_raw(ctx))
}
}

pub trait CustomDataRenderer {
fn is_valid_for_data(
// TODO mutable???
&mut self,
view: &BinaryView,
addr: u64,
type_: *mut BNType,
typeCtx: *mut BNTypeContext,
ctxCount: usize,
) -> bool;
fn get_lines_for_data(
&mut self,
view: &BNBinaryView,
addr: u64,
type_: *mut BNType,
prefix: Box<[InstructionTextToken]>,
width: usize,
count: *mut usize,
typeCtx: *mut BNTypeContext,
ctxCount: usize,
) -> Vec<DisassemblyTextLine>;
}

pub struct MyDataRenderer {}

impl CustomDataRenderer for MyDataRenderer {}
8 changes: 5 additions & 3 deletions rust/src/downloadprovider.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ unsafe impl CoreOwnedArrayProvider for DownloadProvider {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for DownloadProvider {
type Wrapped = DownloadProvider;
unsafe impl CoreArrayWrapper for DownloadProvider {
// TODO there is nothing blocking the returned value from out-living the
// array, change it to &_ or Guard?
type Wrapped<'a> = DownloadProvider;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
DownloadProvider::from_raw(*raw)
}
}
Expand Down
13 changes: 6 additions & 7 deletions rust/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ pub use binaryninjacore_sys::BNAnalysisSkipReason as AnalysisSkipReason;
pub use binaryninjacore_sys::BNFunctionAnalysisSkipOverride as FunctionAnalysisSkipOverride;
pub use binaryninjacore_sys::BNFunctionUpdateType as FunctionUpdateType;


use std::hash::Hash;
use std::{fmt, mem};

Expand Down Expand Up @@ -407,10 +406,10 @@ unsafe impl CoreOwnedArrayProvider for Function {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for Function {
type Wrapped = Guard<'a, Function>;
unsafe impl CoreArrayWrapper for Function {
type Wrapped<'a> = Guard<'a, Function>;

unsafe fn wrap_raw(raw: &'a *mut BNFunction, context: &'a ()) -> Guard<'a, Function> {
unsafe fn wrap_raw<'a>(raw: &'a *mut BNFunction, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Function { handle: *raw }, context)
}
}
Expand Down Expand Up @@ -461,10 +460,10 @@ unsafe impl CoreOwnedArrayProvider for AddressRange {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for AddressRange {
type Wrapped = &'a AddressRange;
unsafe impl CoreArrayWrapper for AddressRange {
type Wrapped<'a> = &'a AddressRange;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
mem::transmute(raw)
}
}
6 changes: 3 additions & 3 deletions rust/src/linearview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,10 +423,10 @@ unsafe impl CoreOwnedArrayProvider for LinearDisassemblyLine {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for LinearDisassemblyLine {
type Wrapped = Guard<'a, LinearDisassemblyLine>;
unsafe impl CoreArrayWrapper for LinearDisassemblyLine {
type Wrapped<'a> = Guard<'a, LinearDisassemblyLine>;

unsafe fn wrap_raw(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped {
unsafe fn wrap_raw<'a>(raw: &'a Self::Raw, _context: &'a Self::Context) -> Self::Wrapped<'a> {
Guard::new(LinearDisassemblyLine::from_raw(raw), _context)
}
}
6 changes: 3 additions & 3 deletions rust/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ unsafe impl CoreOwnedArrayProvider for Metadata {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for Metadata {
type Wrapped = Guard<'a, Metadata>;
unsafe impl CoreArrayWrapper for Metadata {
type Wrapped<'a> = Guard<'a, Metadata>;

unsafe fn wrap_raw(raw: &'a *mut BNMetadata, context: &'a ()) -> Guard<'a, Metadata> {
unsafe fn wrap_raw<'a>(raw: &'a *mut BNMetadata, context: &'a ()) -> Self::Wrapped<'a> {
Guard::new(Metadata::from_raw(*raw), context)
}
}
Expand Down
6 changes: 3 additions & 3 deletions rust/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -373,10 +373,10 @@ unsafe impl CoreOwnedArrayProvider for Platform {
}
}

unsafe impl<'a> CoreArrayWrapper<'a> for Platform {
type Wrapped = Guard<'a, Platform>;
unsafe impl CoreArrayWrapper for Platform {
type Wrapped<'a> = Guard<'a, Platform>;

unsafe fn wrap_raw(raw: &'a *mut BNPlatform, context: &'a ()) -> Guard<'a, Platform> {
unsafe fn wrap_raw<'a>(raw: &'a *mut BNPlatform, context: &'a ()) -> Self::Wrapped<'a> {
debug_assert!(!raw.is_null());
Guard::new(Platform { handle: *raw }, context)
}
Expand Down
Loading

0 comments on commit b4abfb8

Please sign in to comment.