Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Rust: Better errors #2976

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 8 additions & 2 deletions rust/src/backgroundtask.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::result;

use crate::rc::*;
use crate::string::*;
use crate::errors::*;

pub type Result<R> = result::Result<R, ()>;

Expand All @@ -33,13 +34,18 @@ impl BackgroundTask {
Self { handle }
}

pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> Result<Ref<Self>> {
pub fn new<S: BnStrCompatible>(initial_text: S, can_cancel: bool) -> BNResult<Ref<Self>> {
let text = initial_text.as_bytes_with_nul();

let handle = unsafe { BNBeginBackgroundTask(text.as_ref().as_ptr() as *mut _, can_cancel) };

if handle.is_null() {
return Err(());
return Err(
bn_api_error!(
BNBeginBackgroundTask,
&format!("initial_text={:?}, can_cancel={:?}", Utf8Display(&text), can_cancel)
)
);
}

unsafe { Ok(Ref::new(Self { handle })) }
Expand Down
79 changes: 52 additions & 27 deletions rust/src/binaryview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus;

use std::ops;
use std::ptr;
use std::result;

use crate::architecture::Architecture;
use crate::architecture::CoreArchitecture;
use crate::basicblock::BasicBlock;
use crate::databuffer::DataBuffer;
use crate::debuginfo::DebugInfo;
use crate::errors::*;
use crate::fileaccessor::FileAccessor;
use crate::filemetadata::FileMetadata;
use crate::flowgraph::FlowGraph;
Expand All @@ -45,8 +45,6 @@ use crate::string::*;

// TODO : general reorg of modules related to bv

pub type Result<R> = result::Result<R, ()>;

pub trait BinaryViewBase: AsRef<BinaryView> {
fn read(&self, _buf: &mut [u8], _offset: u64) -> usize {
0
Expand Down Expand Up @@ -136,11 +134,11 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn parent_view(&self) -> Result<Ref<BinaryView>> {
fn parent_view(&self) -> BNResult<Ref<BinaryView>> {
let handle = unsafe { BNGetParentView(self.as_ref().handle) };

if handle.is_null() {
return Err(());
return Err(bn_api_error!(BNGetParentView));
}

unsafe { Ok(BinaryView::from_raw(handle)) }
Expand Down Expand Up @@ -273,19 +271,19 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn symbol_by_address(&self, addr: u64) -> Result<Ref<Symbol>> {
fn symbol_by_address(&self, addr: u64) -> BNResult<Ref<Symbol>> {
unsafe {
let raw_sym = BNGetSymbolByAddress(self.as_ref().handle, addr, ptr::null_mut());

if raw_sym.is_null() {
return Err(());
return Err(bn_api_error!(BNGetSymbolByAddress, &format!("addr=0x{addr:x}")));
}

Ok(Ref::new(Symbol::from_raw(raw_sym)))
}
}

fn symbol_by_raw_name<S: BnStrCompatible>(&self, raw_name: S) -> Result<Ref<Symbol>> {
fn symbol_by_raw_name<S: BnStrCompatible>(&self, raw_name: S) -> BNResult<Ref<Symbol>> {
let raw_name = raw_name.as_bytes_with_nul();

unsafe {
Expand All @@ -296,7 +294,7 @@ pub trait BinaryViewExt: BinaryViewBase {
);

if raw_sym.is_null() {
return Err(());
return Err(bn_api_error!(BNGetSymbolByRawName, &format!("raw_name={:?}", Utf8Display(&raw_name))));
}

Ok(Ref::new(Symbol::from_raw(raw_sym)))
Expand Down Expand Up @@ -382,7 +380,7 @@ pub trait BinaryViewExt: BinaryViewBase {
sym: &Symbol,
plat: &Platform,
ty: T,
) -> Result<Ref<Symbol>> {
) -> BNResult<Ref<Symbol>> {
let raw_type = if let Some(t) = ty.into() {
t.handle
} else {
Expand All @@ -398,7 +396,10 @@ pub trait BinaryViewExt: BinaryViewBase {
);

if raw_sym.is_null() {
return Err(());
return Err(bn_api_error!(
BNDefineAutoSymbolAndVariableOrFunction,
&format!("platform={:?}, sym={:?}, raw_type={:?}", plat, sym, raw_type)
));
}

Ok(Ref::new(Symbol::from_raw(raw_sym)))
Expand Down Expand Up @@ -485,14 +486,17 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn section_by_name<S: BnStrCompatible>(&self, name: S) -> Result<Section> {
fn section_by_name<S: BnStrCompatible>(&self, name: S) -> BNResult<Section> {
unsafe {
let raw_name = name.as_bytes_with_nul();
let name_ptr = raw_name.as_ref().as_ptr() as *mut _;
let raw_section = BNGetSectionByName(self.as_ref().handle, name_ptr);

if raw_section.is_null() {
return Err(());
return Err(bn_api_error!(
BNGetSectionByName,
&format!("name={:?}", Utf8Display(&raw_name))
));
}

Ok(Section::from_raw(raw_section))
Expand Down Expand Up @@ -539,12 +543,12 @@ pub trait BinaryViewExt: BinaryViewBase {
unsafe { BNHasFunctions(self.as_ref().handle) }
}

fn entry_point_function(&self) -> Result<Ref<Function>> {
fn entry_point_function(&self) -> BNResult<Ref<Function>> {
unsafe {
let func = BNGetAnalysisEntryPoint(self.as_ref().handle);

if func.is_null() {
return Err(());
return Err(bn_api_error!(BNGetAnalysisEntryPoint));
}

Ok(Function::from_raw(func))
Expand All @@ -571,12 +575,15 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn function_at(&self, platform: &Platform, addr: u64) -> Result<Ref<Function>> {
fn function_at(&self, platform: &Platform, addr: u64) -> BNResult<Ref<Function>> {
unsafe {
let handle = BNGetAnalysisFunction(self.as_ref().handle, platform.handle, addr);

if handle.is_null() {
return Err(());
return Err(bn_api_error!(
BNGetAnalysisFunction,
&format!("platform={:?}, addr={:x}", platform, addr)
));
}

Ok(Function::from_raw(handle))
Expand Down Expand Up @@ -611,10 +618,13 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn read_buffer(&self, offset: u64, len: usize) -> Result<DataBuffer> {
fn read_buffer(&self, offset: u64, len: usize) -> BNResult<DataBuffer> {
let read_buffer = unsafe { BNReadViewBuffer(self.as_ref().handle, offset, len) };
if read_buffer.is_null() {
Err(())
Err(bn_api_error!(
BNReadViewBuffer,
&format!("offset=0x{offset:x}, len=0x{len:x}")
))
} else {
Ok(DataBuffer::from_raw(read_buffer))
}
Expand Down Expand Up @@ -643,7 +653,7 @@ pub trait BinaryViewExt: BinaryViewBase {
}
}

fn load_settings<S: BnStrCompatible>(&self, view_type_name: S) -> Result<Ref<Settings>> {
fn load_settings<S: BnStrCompatible>(&self, view_type_name: S) -> BNResult<Ref<Settings>> {
let view_type_name = view_type_name.as_bytes_with_nul();
let settings_handle = unsafe {
BNBinaryViewGetLoadSettings(
Expand All @@ -653,7 +663,10 @@ pub trait BinaryViewExt: BinaryViewBase {
};

if settings_handle.is_null() {
Err(())
Err(bn_api_error!(
BNBinaryViewGetLoadSettings,
&format!("name={:?}", Utf8Display(&view_type_name))
))
} else {
Ok(unsafe { Settings::from_raw(settings_handle) })
}
Expand Down Expand Up @@ -843,38 +856,50 @@ impl BinaryView {
pub fn from_filename<S: BnStrCompatible>(
meta: &mut FileMetadata,
filename: S,
) -> Result<Ref<Self>> {
) -> BNResult<Ref<Self>> {
let file = filename.as_bytes_with_nul();

let handle = unsafe {
BNCreateBinaryDataViewFromFilename(meta.handle, file.as_ref().as_ptr() as *mut _)
};

if handle.is_null() {
return Err(());
return Err(bn_api_error!(
BNCreateBinaryDataViewFromFilename,
&format!("meta.filename={:?}, filename={:?}",
Utf8Display(&meta.filename()),
Utf8Display(&file)
)
));
}

unsafe { Ok(Ref::new(Self { handle })) }
}

pub fn from_accessor(meta: &FileMetadata, file: &mut FileAccessor) -> Result<Ref<Self>> {
pub fn from_accessor(meta: &FileMetadata, file: &mut FileAccessor) -> BNResult<Ref<Self>> {
let handle =
unsafe { BNCreateBinaryDataViewFromFile(meta.handle, &mut file.api_object as *mut _) };

if handle.is_null() {
return Err(());
return Err(bn_api_error!(
BNCreateBinaryDataViewFromFile,
&format!("meta.filename={:?}", Utf8Display(&meta.filename()))
));
}

unsafe { Ok(Ref::new(Self { handle })) }
}

pub fn from_data(meta: &FileMetadata, data: &[u8]) -> Result<Ref<Self>> {
pub fn from_data(meta: &FileMetadata, data: &[u8]) -> BNResult<Ref<Self>> {
let handle = unsafe {
BNCreateBinaryDataViewFromData(meta.handle, data.as_ptr() as *mut _, data.len())
};

if handle.is_null() {
return Err(());
return Err(bn_api_error!(
BNCreateBinaryDataViewFromData,
&format!("meta.filename={:?}", Utf8Display(&meta.filename()))
));
}

unsafe { Ok(Ref::new(Self { handle })) }
Expand Down