Skip to content

Commit

Permalink
Rust API : Major docs update (still need detailed module and function…
Browse files Browse the repository at this point in the history
… documentation)
  • Loading branch information
KyleMiles committed Aug 31, 2022
1 parent 568050e commit 8a66d88
Show file tree
Hide file tree
Showing 29 changed files with 359 additions and 98 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -11,6 +11,8 @@ Online documentation is available for the following APIs:
- [C++ API, Stable Branch](https://api.binary.ninja/cpp/)
- [Python API, Stable Branch](https://api.binary.ninja/)
- [Python API, Dev Branch](https://dev-api.binary.ninja/)
- [Rust API, Stable Branch](https://rust.binary.ninja/)
- [Rust API, Dev Branch](https://rust-dev.binary.ninja/)

## Branches

Expand Down
12 changes: 6 additions & 6 deletions rust/README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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.

---

Expand Down
3 changes: 3 additions & 0 deletions 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(
Expand Down
8 changes: 4 additions & 4 deletions 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

Expand All @@ -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.
2 changes: 2 additions & 0 deletions rust/src/architecture.rs
Expand Up @@ -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::*;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/backgroundtask.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/basicblock.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/binaryreader.rs
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions rust/src/binaryview.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/binarywriter.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/callingconvention.rs
Expand Up @@ -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;
Expand Down
128 changes: 128 additions & 0 deletions rust/src/command.rs
Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions rust/src/custombinaryview.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/databuffer.rs
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions rust/src/demangle.rs
Expand Up @@ -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};

Expand Down
2 changes: 2 additions & 0 deletions rust/src/flowgraph.rs
Expand Up @@ -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;
Expand Down

0 comments on commit 8a66d88

Please sign in to comment.