diff options
| author | KyleMiles <krm504@nyu.edu> | 2022-08-31 15:39:41 -0400 |
|---|---|---|
| committer | KyleMiles <krm504@nyu.edu> | 2022-08-31 16:00:50 -0400 |
| commit | 8a66d88d33253bce6f1efc278abe805575e81c1d (patch) | |
| tree | b6b717cbe6f6600b7b8516724d6ac56e53e360af /rust | |
| parent | 568050e3cf5e96e134931181de14885e5b64d735 (diff) | |
Rust API : Major docs update (still need detailed module and function documentation)
Diffstat (limited to 'rust')
| -rw-r--r-- | rust/README.md | 12 | ||||
| -rw-r--r-- | rust/build.rs | 3 | ||||
| -rw-r--r-- | rust/examples/template/README.md | 8 | ||||
| -rw-r--r-- | rust/src/architecture.rs | 2 | ||||
| -rw-r--r-- | rust/src/backgroundtask.rs | 2 | ||||
| -rw-r--r-- | rust/src/basicblock.rs | 2 | ||||
| -rw-r--r-- | rust/src/binaryreader.rs | 2 | ||||
| -rw-r--r-- | rust/src/binaryview.rs | 5 | ||||
| -rw-r--r-- | rust/src/binarywriter.rs | 2 | ||||
| -rw-r--r-- | rust/src/callingconvention.rs | 2 | ||||
| -rw-r--r-- | rust/src/command.rs | 128 | ||||
| -rw-r--r-- | rust/src/custombinaryview.rs | 2 | ||||
| -rw-r--r-- | rust/src/databuffer.rs | 2 | ||||
| -rw-r--r-- | rust/src/demangle.rs | 2 | ||||
| -rw-r--r-- | rust/src/flowgraph.rs | 2 | ||||
| -rw-r--r-- | rust/src/headless.rs | 32 | ||||
| -rw-r--r-- | rust/src/interaction.rs | 2 | ||||
| -rw-r--r-- | rust/src/lib.rs | 106 | ||||
| -rw-r--r-- | rust/src/linearview.rs | 2 | ||||
| -rw-r--r-- | rust/src/platform.rs | 2 | ||||
| -rw-r--r-- | rust/src/rc.rs | 2 | ||||
| -rw-r--r-- | rust/src/section.rs | 2 | ||||
| -rw-r--r-- | rust/src/segment.rs | 2 | ||||
| -rw-r--r-- | rust/src/settings.rs | 2 | ||||
| -rw-r--r-- | rust/src/string.rs | 2 | ||||
| -rw-r--r-- | rust/src/symbol.rs | 2 | ||||
| -rw-r--r-- | rust/src/tags.rs | 2 | ||||
| -rw-r--r-- | rust/src/types.rs | 121 |
28 files changed, 357 insertions, 98 deletions
diff --git a/rust/README.md b/rust/README.md index 15f00e80..80cd287e 100644 --- a/rust/README.md +++ b/rust/README.md @@ -9,9 +9,9 @@ ## Dependencies -Having BinaryNinja installed (and your license registered) -Clang -Rust +Having BinaryNinja installed (and your license registered) +Clang +Rust ## How to use @@ -40,9 +40,9 @@ See the `./examples/`. Plugin registration commands are in `binaryninja::comman binaryninja = { git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"} ``` -All standalone binaries should call both `binaryninja::headless::init()` and `binaryninja::headless::shutdown()`. -All standalone binaries need to provide a `build.rs`. -See [`examples/template`](examples/template) for details. +All standalone binaries should call both `binaryninja::headless::init()` and `binaryninja::headless::shutdown()`. +All standalone binaries need to provide a `build.rs`. +See [`examples/template`](examples/template) for details. --- diff --git a/rust/build.rs b/rust/build.rs index a00a19cd..91f1021f 100644 --- a/rust/build.rs +++ b/rust/build.rs @@ -1,4 +1,7 @@ fn main() { + // TODO : Enable the following when https://github.com/rust-lang/rust/issues/43781 stabilizes + // #[cfg(doc)] + let _ = std::fs::create_dir("target"); let _ = std::fs::create_dir("target/doc"); let _ = std::fs::copy("../docs/img/favicon.ico", "target/doc/favicon.ico"); let _ = std::fs::copy( diff --git a/rust/examples/template/README.md b/rust/examples/template/README.md index 998a9cd2..1466ef24 100644 --- a/rust/examples/template/README.md +++ b/rust/examples/template/README.md @@ -1,8 +1,8 @@ # Template -[The only official method of providing linker arguments to a crate is through that crate's `build.rs`](https://github.com/rust-lang/cargo/issues/9554), thus this template. +[The only official method of providing linker arguments to a crate is through that crate's `build.rs`](https://github.com/rust-lang/cargo/issues/9554), thus this template. -Please see `Cargo.toml` for further configuration options. +Please see `Cargo.toml` for further configuration options. ## Plugins @@ -15,5 +15,5 @@ in `Cargo.toml`. ## Standalone executables -All standalone executables should call both `binaryninja::headless::init()` and `binaryninja::headless::shutdown()` (see [`src/main.rs`](src/main.rs)). -Standalone executables will fail to link if you do not provide a `build.rs`. The one provided here should work. +All standalone executables should call both `binaryninja::headless::init()` and `binaryninja::headless::shutdown()` (see [`src/main.rs`](src/main.rs)). +Standalone executables will fail to link if you do not provide a `build.rs`. The one provided here should work. diff --git a/rust/src/architecture.rs b/rust/src/architecture.rs index 95bfedd6..878b2c5e 100644 --- a/rust/src/architecture.rs +++ b/rust/src/architecture.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Architectures provide disassembly, lifting, and associated metadata about a CPU to inform analysis and decompilation. + // container abstraction to avoid Vec<> (want CoreArchFlagList, CoreArchRegList) // RegisterInfo purge use binaryninjacore_sys::*; diff --git a/rust/src/backgroundtask.rs b/rust/src/backgroundtask.rs index 0a893ccd..234bc623 100644 --- a/rust/src/backgroundtask.rs +++ b/rust/src/backgroundtask.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Background tasks provide plugins the ability to run tasks in the background so they don't hand the UI + use binaryninjacore_sys::*; use std::result; diff --git a/rust/src/basicblock.rs b/rust/src/basicblock.rs index abc57a7a..571786aa 100644 --- a/rust/src/basicblock.rs +++ b/rust/src/basicblock.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. + + use std::fmt; use crate::architecture::CoreArchitecture; diff --git a/rust/src/binaryreader.rs b/rust/src/binaryreader.rs index c84dfed5..3c881627 100644 --- a/rust/src/binaryreader.rs +++ b/rust/src/binaryreader.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! A convenience class for reading binary data + use binaryninjacore_sys::*; use crate::binaryview::BinaryView; diff --git a/rust/src/binaryview.rs b/rust/src/binaryview.rs index 311e867e..a372b778 100644 --- a/rust/src/binaryview.rs +++ b/rust/src/binaryview.rs @@ -12,6 +12,11 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! A view on binary data and queryable interface of a binary file. +//! +//! One key job of BinaryView is file format parsing which allows Binary Ninja to read, write, insert, remove portions of the file given a virtual address. For the purposes of this documentation we define a virtual address as the memory address that the various pieces of the physical file will be loaded at. +//! TODO : Mirror the Python docs for this + use binaryninjacore_sys::*; pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; diff --git a/rust/src/binarywriter.rs b/rust/src/binarywriter.rs index f56ed108..88d124e6 100644 --- a/rust/src/binarywriter.rs +++ b/rust/src/binarywriter.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! A convenience class for writing binary data + use binaryninjacore_sys::*; use crate::binaryview::BinaryView; diff --git a/rust/src/callingconvention.rs b/rust/src/callingconvention.rs index 627e3dfc..8e29a0b0 100644 --- a/rust/src/callingconvention.rs +++ b/rust/src/callingconvention.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Contains and provides information about different systems' calling conventions to analysis. + use std::borrow::Borrow; use std::marker::PhantomData; use std::mem; diff --git a/rust/src/command.rs b/rust/src/command.rs index 39bf329e..4763fee2 100644 --- a/rust/src/command.rs +++ b/rust/src/command.rs @@ -12,6 +12,22 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Provides commands for registering plugins and plugin actions. +//! +//! All plugins need to provide one of the following functions for Binary Ninja to call: +//! +//! ```rust +//! pub extern "C" fn CorePluginInit() -> bool {} +//! ``` +//! +//! ```rust +//! pub extern "C" fn UIPluginInit() -> bool {} +//! ``` +//! +//! Both of these functions can call any of the following registration functions, though `CorePluginInit` is called during Binary Ninja core initialization, and `UIPluginInit` is called during Binary Ninja UI initialization. +//! +//! The return value of these functions should indicate whether they successfully initialized themselves. + use binaryninjacore_sys::{ BNBinaryView, BNFunction, BNRegisterPluginCommand, BNRegisterPluginCommandForAddress, BNRegisterPluginCommandForFunction, BNRegisterPluginCommandForRange, @@ -24,6 +40,7 @@ use crate::binaryview::BinaryView; use crate::function::Function; use crate::string::BnStrCompatible; +/// The trait required for generic commands. See [register] for example usage. pub trait Command: 'static + Sync { fn action(&self, view: &BinaryView); fn valid(&self, view: &BinaryView) -> bool; @@ -42,6 +59,33 @@ where } } +/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar. +/// +/// # Example +/// ```rust +/// Struct MyCommand; +/// +/// impl Command for MyCommand { +/// fn action(&self, view: &BinaryView) { +/// // Your code here +/// } +/// +/// fn valid(&self, view: &BinaryView) -> bool { +/// // Your code here +/// true +/// } +/// } +/// +/// #[no_mangle] +/// pub extern "C" fn CorePluginInit() -> bool { +/// register( +/// "My Plugin Command", +/// "A description of my command", +/// MyCommand {}, +/// ); +/// true +/// } +/// ``` pub fn register<S, C>(name: S, desc: S, command: C) where S: BnStrCompatible, @@ -94,6 +138,7 @@ where } } +/// The trait required for address-associated commands. See [register_for_address] for example usage. pub trait AddressCommand: 'static + Sync { fn action(&self, view: &BinaryView, addr: u64); fn valid(&self, view: &BinaryView, addr: u64) -> bool; @@ -112,6 +157,33 @@ where } } +/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar. +/// +/// # Example +/// ```rust +/// Struct MyCommand; +/// +/// impl AddressCommand for MyCommand { +/// fn action(&self, view: &BinaryView, addr: u64) { +/// // Your code here +/// } +/// +/// fn valid(&self, view: &BinaryView, addr: u64) -> bool { +/// // Your code here +/// true +/// } +/// } +/// +/// #[no_mangle] +/// pub extern "C" fn CorePluginInit() -> bool { +/// register_for_address( +/// "My Plugin Command", +/// "A description of my command", +/// MyCommand {}, +/// ); +/// true +/// } +/// ``` pub fn register_for_address<S, C>(name: S, desc: S, command: C) where S: BnStrCompatible, @@ -164,6 +236,7 @@ where } } +/// The trait required for range-associated commands. See [register_for_range] for example usage. pub trait RangeCommand: 'static + Sync { fn action(&self, view: &BinaryView, range: Range<u64>); fn valid(&self, view: &BinaryView, range: Range<u64>) -> bool; @@ -182,6 +255,33 @@ where } } +/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar. +/// +/// # Example +/// ```rust +/// Struct MyCommand; +/// +/// impl AddressCommand for MyCommand { +/// fn action(&self, view: &BinaryView, range: Range<u64>) { +/// // Your code here +/// } +/// +/// fn valid(&self, view: &BinaryView, range: Range<u64>) -> bool { +/// // Your code here +/// true +/// } +/// } +/// +/// #[no_mangle] +/// pub extern "C" fn CorePluginInit() -> bool { +/// register_for_range( +/// "My Plugin Command", +/// "A description of my command", +/// MyCommand {}, +/// ); +/// true +/// } +/// ``` pub fn register_for_range<S, C>(name: S, desc: S, command: C) where S: BnStrCompatible, @@ -239,6 +339,7 @@ where } } +/// The trait required for function-associated commands. See [register_for_function] for example usage. pub trait FunctionCommand: 'static + Sync { fn action(&self, view: &BinaryView, func: &Function); fn valid(&self, view: &BinaryView, func: &Function) -> bool; @@ -257,6 +358,33 @@ where } } +/// The function call required for generic commands; commands added in this way will be in the `Plugins` submenu of the menu bar. +/// +/// # Example +/// ```rust +/// Struct MyCommand; +/// +/// impl AddressCommand for MyCommand { +/// fn action(&self, view: &BinaryView, func: &Function) { +/// // Your code here +/// } +/// +/// fn valid(&self, view: &BinaryView, func: &Function) -> bool { +/// // Your code here +/// true +/// } +/// } +/// +/// #[no_mangle] +/// pub extern "C" fn CorePluginInit() -> bool { +/// register_for_function( +/// "My Plugin Command", +/// "A description of my command", +/// MyCommand {}, +/// ); +/// true +/// } +/// ``` pub fn register_for_function<S, C>(name: S, desc: S, command: C) where S: BnStrCompatible, diff --git a/rust/src/custombinaryview.rs b/rust/src/custombinaryview.rs index 50938018..14944cd1 100644 --- a/rust/src/custombinaryview.rs +++ b/rust/src/custombinaryview.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! An interface for providing your own [BinaryView]s to Binary Ninja. + use binaryninjacore_sys::*; pub use binaryninjacore_sys::BNModificationStatus as ModificationStatus; diff --git a/rust/src/databuffer.rs b/rust/src/databuffer.rs index cda78e80..405084a6 100644 --- a/rust/src/databuffer.rs +++ b/rust/src/databuffer.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! A basic wrapper around an array of binary data + use binaryninjacore_sys::*; use std::ptr; diff --git a/rust/src/demangle.rs b/rust/src/demangle.rs index 11d54b39..2a1bff0d 100644 --- a/rust/src/demangle.rs +++ b/rust/src/demangle.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Interfaces for demangling and simplifying mangled names in binaries. + use binaryninjacore_sys::*; use std::{ffi::CStr, result}; diff --git a/rust/src/flowgraph.rs b/rust/src/flowgraph.rs index c55970e2..df4ee391 100644 --- a/rust/src/flowgraph.rs +++ b/rust/src/flowgraph.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Interfaces for creating and displaying pretty CFGs in Binary Ninja. + use binaryninjacore_sys::*; use crate::disassembly::DisassemblyTextLine; diff --git a/rust/src/headless.rs b/rust/src/headless.rs index 5f56a881..fb73535c 100644 --- a/rust/src/headless.rs +++ b/rust/src/headless.rs @@ -66,6 +66,9 @@ fn binja_path() -> PathBuf { use binaryninjacore_sys::{BNInitPlugins, BNInitRepoPlugins, BNSetBundledPluginDirectory}; +/// Loads plugins, core architecture, platform, etc. +/// +/// ⚠️ Important! Must be called at the beginning of scripts. Plugins do not need to call this. ⚠️ pub fn init() { unsafe { let path = binja_path().join("plugins").into_os_string(); @@ -77,26 +80,25 @@ pub fn init() { } } +/// Unloads plugins, stops all worker threads, and closes open logs +/// +/// ⚠️ Important! Must be called at the end of scripts. ⚠️ pub fn shutdown() { - //! Unloads plugins, stops all worker threads, and closes open logs - //! Important! : Must be called at the end of scripts - unsafe { binaryninjacore_sys::BNShutdown() }; } +/// Prelued-postlued helper function (calls [`init`] and [`shutdown`] for you) +/// ```rust +/// fn main() { +/// binaryninja::headless::script_helper(|| { +/// binaryninja::open_view("/bin/cat") +/// .expect("Couldn't open `/bin/cat`") +/// .iter() +/// .for_each(|func| println!(" `{}`", func.symbol().full_name())); +/// }); +/// } +/// ``` pub fn script_helper(func: fn()) { - //! Prelued-postlued helper function: - //! ``` - //! fn main() { - //! binaryninja::headless::script_helper(|| { - //! binaryninja::open_view("/bin/cat") - //! .expect("Couldn't open `/bin/cat`") - //! .iter() - //! .for_each(|func| println!(" `{}`", func.symbol().full_name())); - //! }); - //! } - //! ``` - init(); func(); shutdown(); diff --git a/rust/src/interaction.rs b/rust/src/interaction.rs index 74b6ebc6..96af3daa 100644 --- a/rust/src/interaction.rs +++ b/rust/src/interaction.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Interfaces for asking the user for information: forms, opening files, etc. + use binaryninjacore_sys::*; use std::ffi::CString; diff --git a/rust/src/lib.rs b/rust/src/lib.rs index f4ccea9f..d1181bb4 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -13,16 +13,95 @@ // limitations under the License. #![doc(html_no_source)] -#![doc(html_favicon_url = "../favicon.ico")] -#![doc(html_logo_url = "../logo.png")] +#![doc(html_favicon_url = "/favicon.ico")] +#![doc(html_logo_url = "/logo.png")] #![doc(issue_tracker_base_url = "https://github.com/Vector35/binaryninja-api/issues/")] -//! # Warning +//! This crate is the official [Binary Ninja] API wrapper for Rust. +//! +//! [Binary Ninja] is an interactive disassembler, decompiler, and binary analysis platform for reverse engineers, malware analysts, vulnerability researchers, and software developers that runs on Windows, macOS, and Linux. Our extensive API can be used to create and customize loaders, add or augment architectures, customize the UI, or automate any workflow (types, patches, decompilation...anything!). +//! +//! If you're just getting started with [Binary Ninja], you may wish to check out the [Getting Started Guide] +//! +//! If you have questions, we'd love to answer them in [our public Slack], and if you find any issues, please [file an issue] or [submit a PR]. +//! +//! --- +//! # Warning //! <img align="right" src="../under_construction.png" width="175" height="175"> //! //! > ⚠️ **These bindings are in a very early beta, only have partial support for the core APIs and are still actively under development. Compatibility _will_ break and conventions _will_ change! They are being used for core Binary Ninja features however, so we expect much of what is already there to be reliable enough to build on, just don't be surprised if your plugins/scripts need to hit a moving target.** //! //! > ⚠️ This project runs on Rust version `stable-2022-04-07` +//! +//! --- +//! +//! # Examples +//! +//! There are two distinct ways to use this crate: +//! 1. [Writing a Plugin](#writing-a-plugin) +//! 2. [Writing a Script](#writing-a-script) +//! +//! ## Writing a Plugin +//! +//! Create a new library (`cargo new --lib <plugin-name>`) and include the following in your `Cargo.toml`: +//! +//! ``` +//! [lib] +//! crate-type = ["cdylib"] +//! +//! [dependencies] +//! binaryninja = {git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"} +//! ``` +//! +//! In `lib.rs` you'll need to provide a `CorePluginInit` or `UIPluginInit` function for Binary Ninja to call. +//! +//! See [`command`] for the different actions you can provide and how to register your plugin with [Binary Ninja]. +//! +//! ## Writing a Script: +//! +//! "Scripts" are binaries (`cargo new --bin <script-name>`), and have some specific requirements: +//! +//! ### build.rs +//! +//! Because [the only official method of providing linker arguments to a crate is through that crate's `build.rs`], all scripts need to provide their own `build.rs` so they can probably link with Binary Ninja. +//! +//! The most up-to-date version of the suggested [`build.rs` is here]. +//! +//! ### `main.rs` +//! All standalone binaries need to call [`headless::init()`] at start and [`headless::shutdown()`] at shutdown. +//! ```rust +//! fn main() { +//! // This loads all the core architecture, platform, etc plugins +//! // Standalone executables need to call this, but plugins do not +//! binaryninja::headless::init(); +//! +//! println!("Loading binary..."); +//! let bv = binaryninja::open_view("/bin/cat").expect("Couldn't open `/bin/cat`"); +//! +//! // Your code here... +//! +//! // Important! Standalone executables need to call shutdown or they will hang forever +//! binaryninja::headless::shutdown(); +//! } +//! ``` +//! +//! ### `Cargo.toml` +//! ``` +//! [dependencies] +//! binaryninja = { git = "https://github.com/Vector35/binaryninja-api.git", branch = "dev"} +//! ``` +//! +//! See the [examples] on GitHub for more comprehensive examples. +//! +//! [Binary Ninja]: https://binary.ninja/ +//! [Getting Started Guide]: https://docs.binary.ninja/ +//! [our public Slack]: https://join.slack.com/t/binaryninja/shared_invite/zt-3u4vu3ja-IGUF4ZWNlD7ER2ulvICvuQ +//! [file an issue]: https://github.com/Vector35/binaryninja-api/issues +//! [submit a PR]: https://github.com/Vector35/binaryninja-api/pulls +//! [the only official method of providing linker arguments to a crate is through that crate's `build.rs`]: https://github.com/rust-lang/cargo/issues/9554 +//! [`build.rs` is here]: https://github.com/Vector35/binaryninja-api/blob/dev/rust/examples/template/build.rs +//! [examples]: https://github.com/Vector35/binaryninja-api/tree/dev/rust/examples +//! #[macro_use] extern crate log; @@ -318,22 +397,21 @@ pub fn open_view<F: AsRef<Path>>(filename: F) -> Result<rc::Ref<binaryview::Bina Ok(bv) } +/// This is incomplete, but should work in most cases: +/// ```rust +/// let settings = [("analysis.linearSweep.autorun", "false")] +/// .iter() +/// .cloned() +/// .collect(); +/// +/// let bv = binaryninja::open_view_with_options("/bin/cat", true, Some(settings)) +/// .expect("Couldn't open `/bin/cat`"); +/// ``` pub fn open_view_with_options<F: AsRef<Path>>( filename: F, update_analysis_and_wait: bool, options: Option<HashMap<&str, &str>>, ) -> Result<rc::Ref<binaryview::BinaryView>, String> { - //! This is incomplete, but should work in most cases: - //! ``` - //! let settings = [("analysis.linearSweep.autorun", "false")] - //! .iter() - //! .cloned() - //! .collect(); - //! - //! let bv = binaryninja::open_view_with_options("/bin/cat", true, Some(settings)) - //! .expect("Couldn't open `/bin/cat`"); - //! ``` - use crate::binaryview::BinaryViewExt; use crate::custombinaryview::{BinaryViewTypeBase, BinaryViewTypeExt}; diff --git a/rust/src/linearview.rs b/rust/src/linearview.rs index ff8cbfff..f320f666 100644 --- a/rust/src/linearview.rs +++ b/rust/src/linearview.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! APIs for accessing Binary Ninja's linear view + use binaryninjacore_sys::*; use crate::binaryview::BinaryView; diff --git a/rust/src/platform.rs b/rust/src/platform.rs index 1e84e7ec..d0cc9282 100644 --- a/rust/src/platform.rs +++ b/rust/src/platform.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Contains all information related to the execution environment of the binary, mainly the calling conventions used + use std::{borrow::Borrow, collections::HashMap, os::raw, path::Path, ptr, slice}; use binaryninjacore_sys::*; diff --git a/rust/src/rc.rs b/rust/src/rc.rs index b31c4a5a..f8507d0c 100644 --- a/rust/src/rc.rs +++ b/rust/src/rc.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Reference counting for core Binary Ninja objects. + use std::borrow::Borrow; use std::marker::PhantomData; use std::mem; diff --git a/rust/src/section.rs b/rust/src/section.rs index 547f1d34..9790801c 100644 --- a/rust/src/section.rs +++ b/rust/src/section.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Sections are [crate::segment::Segment]s that are loaded into memory at run time + use std::fmt; use std::ops::Range; diff --git a/rust/src/segment.rs b/rust/src/segment.rs index 38c1dd1f..2d774350 100644 --- a/rust/src/segment.rs +++ b/rust/src/segment.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Labeled segments in a binary file that aren't loaded in to memory + use binaryninjacore_sys::*; use std::ops::Range; diff --git a/rust/src/settings.rs b/rust/src/settings.rs index 027e8c38..8ad05d38 100644 --- a/rust/src/settings.rs +++ b/rust/src/settings.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! An interface for reading, writing, and creating new settings + pub use binaryninjacore_sys::BNSettingsScope as SettingsScope; use binaryninjacore_sys::*; diff --git a/rust/src/string.rs b/rust/src/string.rs index 86decf79..c1ca88d8 100644 --- a/rust/src/string.rs +++ b/rust/src/string.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! String wrappers for core-owned strings and strings being passed to the core + use std::borrow::Borrow; use std::ffi::{CStr, CString}; use std::fmt; diff --git a/rust/src/symbol.rs b/rust/src/symbol.rs index 74932051..27e701dc 100644 --- a/rust/src/symbol.rs +++ b/rust/src/symbol.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Interfaces for the various kinds of symbols in a binary. + use std::fmt; use std::hash::{Hash, Hasher}; use std::ptr; diff --git a/rust/src/tags.rs b/rust/src/tags.rs index 2db58892..d215ec1b 100644 --- a/rust/src/tags.rs +++ b/rust/src/tags.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. +//! Interfaces for creating and modifying tags in a BinaryView. + use binaryninjacore_sys::*; use crate::binaryview::BinaryView; diff --git a/rust/src/types.rs b/rust/src/types.rs index 22271bad..09b1b97d 100644 --- a/rust/src/types.rs +++ b/rust/src/types.rs @@ -395,14 +395,13 @@ impl TypeBuilder { unsafe { Self::from_raw(BNCreateArrayTypeBuilder(&t.into().into(), count)) } } + /// The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 + /// For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to pub fn enumeration<T: Into<Conf<bool>>>( enumeration: &Enumeration, width: usize, is_signed: T, ) -> Self { - //! The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 - //! For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to - unsafe { // TODO : This is _extremely fragile_, we should change the internals of BNCreateEnumerationTypeBuilder instead of doing this let mut fake_arch: BNArchitecture = mem::zeroed(); @@ -541,14 +540,15 @@ pub struct Type { pub(crate) handle: *mut BNType, } +/// ``` +/// use binaryninja::types::Type; +/// let bv = unsafe { BinaryView::from_raw(view) }; +/// let my_custom_type_1 = Self::named_int(5, false, "my_w"); +/// let my_custom_type_2 = Self::int(5, false); +/// bv.define_user_type("int_1", &my_custom_type_1); +/// bv.define_user_type("int_2", &my_custom_type_2); +/// ``` impl Type { - //! use binaryninja::types::Type; - //! let bv = unsafe { BinaryView::from_raw(view) }; - //! let my_custom_type_1 = Self::named_int(5, false, "my_w"); - //! let my_custom_type_2 = Self::int(5, false); - //! bv.define_user_type("int_1", &my_custom_type_1); - //! bv.define_user_type("int_2", &my_custom_type_2); - unsafe fn from_raw(handle: *mut BNType) -> Self { debug_assert!(!handle.is_null()); @@ -769,13 +769,14 @@ impl Type { unsafe { Self::ref_from_raw(BNCreateArrayType(&t.into().into(), count)) } } + /// The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 + /// + /// For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to pub fn enumeration<T: Into<Conf<bool>>>( enumeration: &Enumeration, width: usize, is_signed: T, ) -> Ref<Self> { - //! The C/C++ APIs require an associated architecture, but in the core we only query the default_int_size if the given width is 0 - //! For simplicity's sake, that convention isn't followed and you can query the default_int_size from an arch, if you have it, if you need to unsafe { // TODO : This is _extremely fragile_, we should change the internals of BNCreateEnumerationType instead of doing this let mut fake_arch: BNArchitecture = mem::zeroed(); @@ -1336,29 +1337,30 @@ pub struct StructureBuilder { pub(crate) handle: *mut BNStructureBuilder, } +/// ``` +/// // 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); +/// +/// // 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 StructureBuilder { - //! // 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); - - //! // 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); - pub fn new() -> Self { Self { handle: unsafe { BNCreateStructureBuilder() }, @@ -1469,32 +1471,33 @@ 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 { - //! // 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); - pub fn new(builder: &StructureBuilder) -> Ref<Self> { unsafe { let handle = BNFinalizeStructureBuilder(builder.handle); |
