Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 48 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ members = [
"arch/riscv",
"arch/msp430",
"rust/plugin_examples/data_renderer",
"rust/plugin_examples/flowgraph_layout",
"view/minidump",
"plugins/dwarf/dwarf_import",
"plugins/dwarf/dwarf_import/demo",
Expand Down
4 changes: 3 additions & 1 deletion rust/examples/flowgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use binaryninja::{
disassembly::{DisassemblyTextLine, InstructionTextToken, InstructionTextTokenKind},
flowgraph::{EdgePenStyle, FlowGraph, ThemeColor},
};
use std::time::Duration;

pub struct GraphPrinter;

Expand Down Expand Up @@ -131,8 +132,9 @@ fn main() {
test_graph();

for func in bv.functions().iter().take(5) {
// TODO: Why are the nodes empty? Python its empty until its shown...
let graph = func.create_graph(FunctionViewType::MediumLevelIL, None);
// It is important to call this, otherwise no nodes will be placed.
graph.request_layout_and_wait(Duration::from_secs(5));
let func_name = func.symbol().short_name();
let title = func_name.to_string_lossy();
bv.show_graph_report(&title, &graph);
Expand Down
13 changes: 13 additions & 0 deletions rust/plugin_examples/flowgraph_layout/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "example_flowgraph_layout"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
binaryninjacore-sys = { path = "../../binaryninjacore-sys" }
binaryninja = { path = "../.." }
rust-sugiyama = "0.4.0"
petgraph = "0.8"
16 changes: 16 additions & 0 deletions rust/plugin_examples/flowgraph_layout/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# FlowGraph Layout Example

This example implements a simple flow graph layout using the rust crate `rust-sugiyama`. After building and placing this
in the plugin directory, override the default flow graph layout setting `rendering.graph.defaultLayout`.

This example is complete _except_ for edge routing, we simply draw the edge from the outgoing nodes bottom to the
incoming nodes top, a real layout would route the edges around nodes to avoid overlaps and other rendering oddities.

## Building

```sh
# Build from the root directory (binaryninja-api)
cargo build -p example_flowgraph_layout
# Link binary on macOS
ln -sf $PWD/target/debug/libexample_data_renderer.dylib ~/Library/Application\ Support/Binary\ Ninja/plugins
```
25 changes: 25 additions & 0 deletions rust/plugin_examples/flowgraph_layout/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
fn main() {
let link_path = std::env::var_os("DEP_BINARYNINJACORE_PATH")
.expect("DEP_BINARYNINJACORE_PATH not specified");

println!("cargo::rustc-link-lib=dylib=binaryninjacore");
println!("cargo::rustc-link-search={}", link_path.to_str().unwrap());

#[cfg(target_os = "linux")]
{
println!(
"cargo::rustc-link-arg=-Wl,-rpath,{0},-L{0}",
link_path.to_string_lossy()
);
}

#[cfg(target_os = "macos")]
{
let crate_name = std::env::var("CARGO_PKG_NAME").expect("CARGO_PKG_NAME not set");
let lib_name = crate_name.replace('-', "_");
println!(
"cargo::rustc-link-arg=-Wl,-install_name,@rpath/lib{}.dylib",
lib_name
);
}
}
149 changes: 149 additions & 0 deletions rust/plugin_examples/flowgraph_layout/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
use binaryninja::flowgraph::edge::Point;
use binaryninja::flowgraph::layout::{register_flowgraph_layout, FlowGraphLayout};
use binaryninja::flowgraph::{FlowGraph, FlowGraphNode};
use binaryninja::rc::Ref;
use std::collections::HashMap;

pub struct StableGraphBuilder;

impl StableGraphBuilder {
pub fn new() -> Self {
Self {}
}

pub fn build(
self,
nodes: &[FlowGraphNode],
) -> petgraph::stable_graph::StableDiGraph<Ref<FlowGraphNode>, ()> {
let mut graph = petgraph::stable_graph::StableDiGraph::<Ref<FlowGraphNode>, ()>::new();
let mut node_idx_map = HashMap::<Ref<FlowGraphNode>, petgraph::graph::NodeIndex>::new();
for node in nodes {
let owned_node = node.to_owned();
node_idx_map.insert(owned_node.clone(), graph.add_node(owned_node));
}
for node in nodes {
let node_idx = node_idx_map.get(node).unwrap();
for edge in &node.outgoing_edges() {
let target_node_idx = node_idx_map.get(&edge.target).unwrap();
graph.add_edge(*node_idx, *target_node_idx, ());
}
}
graph
}
}

struct SugiyamaLayout;

impl FlowGraphLayout for SugiyamaLayout {
fn layout(&self, graph: &FlowGraph, nodes: &[FlowGraphNode]) -> bool {
let mut config = rust_sugiyama::configure::Config::default();
config.vertex_spacing = 5.0;

let vertex_size = |_, node: &Ref<FlowGraphNode>| {
let (width, height) = node.size();
(width as f64 * 1.2, height as f64)
};
let pet_graph = StableGraphBuilder::new().build(nodes);
let layouts = rust_sugiyama::from_graph(&pet_graph, &vertex_size, &config);

// Position graph nodes
for (nodes, _, _) in &layouts {
for (node_idx, (x, y)) in nodes {
let node = pet_graph.node_weight(*node_idx).unwrap();
node.set_position(*x as i32, *y as i32);
}
}

// Add edges to graph nodes
for (nodes, _, _) in &layouts {
for (node_idx, (x, y)) in nodes {
let node = pet_graph.node_weight(*node_idx).unwrap();
let (width, height) = node.size();
for (edge_idx, edge) in node.outgoing_edges().iter().enumerate() {
let from_point_x = x + (width as f64 / 2.0);
let from_point_y = y + height as f64;
let from_point = Point {
x: from_point_x as f32,
y: from_point_y as f32,
};
let (target_node_x, target_node_y) = edge.target.position();
let (target_node_width, _) = edge.target.size();
let to_point_x = target_node_x as f64 + (target_node_width as f64 / 2.0);
let to_point_y = target_node_y;
let to_point = Point {
x: to_point_x as f32,
y: to_point_y as f32,
};
// NOTE: This does not do proper routing, this will add edge points from the outgoing node
// to the target node, this will lead to lines overlapping nodes and other rendering oddities.
// The reason we do not do proper routing is because that is quite a bit more code with some
// dependence on a navigation algorithm like a-star.
node.set_outgoing_edge_points(edge_idx, &[from_point, to_point]);
}
}
}

// Calculate graph size and node visibility
let mut min_x = f32::MAX;
let mut min_y = f32::MAX;
let mut max_x = f32::MIN;
let mut max_y = f32::MIN;

for node in nodes {
let (node_x, node_y) = node.position();
let (node_width, node_height) = node.size();

// Initialize per-node bounds based on the node's current box
let mut min_node_x = node_x;
let mut max_node_x = node_x + node_width;
let mut min_node_y = node_y;
let mut max_node_y = node_y + node_height;

for edge in &node.outgoing_edges() {
for point in &edge.points {
let px = point.x;
let py = point.y;

// Update Global Graph Bounds
min_x = min_x.min(px);
min_y = min_y.min(py);
max_x = max_x.max(px + 1.0);
max_y = max_y.max(py + 1.0);

// Update Node Visibility Bounds
min_node_x = min_node_x.min(px as i32);
max_node_x = max_node_x.max(px as i32 + 1);
min_node_y = min_node_y.min(py as i32);
max_node_y = max_node_y.max(py as i32 + 1);
}
}

node.set_visibility_region(
min_node_x,
min_node_y,
(max_node_x - min_node_x),
(max_node_y - min_node_y),
);
}

// Set final graph dimensions
if min_x != f32::MAX {
let (horiz_node_margin, vert_node_margin) = graph.node_margins();
let final_graph_width = (max_x - min_x) as i32 + horiz_node_margin * 2;
let final_graph_height = (max_y - min_y) as i32 + vert_node_margin * 2;
graph.set_size(final_graph_width, final_graph_height);
}

true
}
}

/// # Safety
/// This function is called from Binary Ninja once to initialize the plugin.
#[allow(non_snake_case)]
#[unsafe(no_mangle)]
pub unsafe extern "C" fn CorePluginInit() -> bool {
// Register flow graph layout
register_flowgraph_layout("Sugiyama", SugiyamaLayout);
true
}
Loading
Loading