diff options
| -rw-r--r-- | binaryninjaapi.h | 47 | ||||
| -rw-r--r-- | binaryninjacore.h | 23 | ||||
| -rw-r--r-- | flowgraph.cpp | 12 | ||||
| -rw-r--r-- | flowgraphlayout.cpp | 117 | ||||
| -rw-r--r-- | flowgraphnode.cpp | 18 | ||||
| -rw-r--r-- | python/examples/custom_graph_layout.py | 89 | ||||
| -rw-r--r-- | python/flowgraph.py | 71 |
7 files changed, 369 insertions, 8 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 3c6bc72a..4126c8cd 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -11005,6 +11005,18 @@ namespace BinaryNinja { */ void SetBasicBlock(BasicBlock* block); + /*! Set flow graph block X position + + \param x Flow graph block X position + */ + void SetX(int x); + + /*! Set flow graph block Y position + + \param y Flow graph block Y position + */ + void SetY(int y); + /*! Flow graph block X position \return Flow graph block X position @@ -11079,6 +11091,8 @@ namespace BinaryNinja { void SetHighlight(const BNHighlightColor& color); bool IsValidForGraph(FlowGraph* graph) const; + + void SetVisibilityRegion(int x, int y, int w, int h); }; /*! @@ -11200,17 +11214,22 @@ namespace BinaryNinja { */ size_t AddNode(FlowGraphNode* node); + + /*! Flow graph width \return Flow graph width */ int GetWidth() const; + void SetWidth(int width); /*! Flow graph height \return Flow graph height */ int GetHeight() const; + void SetHeight(int height); + std::vector<Ref<FlowGraphNode>> GetNodesInRegion(int left, int top, int right, int bottom); /*! Whether this graph is representing IL. @@ -11298,6 +11317,34 @@ namespace BinaryNinja { virtual Ref<FlowGraph> Update() override; }; + class FlowGraphLayout : public StaticCoreRefCountObject<BNFlowGraphLayout> + { + protected: + FlowGraphLayout(BNFlowGraphLayout* layout); + + static bool LayoutCallback(void* ctxt, BNFlowGraph* graph, BNFlowGraphNode** nodes, size_t nodeCount); + + std::string m_nameForRegister; + + public: + FlowGraphLayout(const std::string& name); + + static void Register(FlowGraphLayout* layout); + static Ref<FlowGraphLayout> GetByName(const std::string& name); + static std::vector<Ref<FlowGraphLayout>> GetFlowGraphLayouts(); + + std::string GetName() const; + virtual bool Layout(Ref<FlowGraph> graph, std::vector<Ref<FlowGraphNode>>& nodes); + }; + + class CoreFlowGraphLayout : public FlowGraphLayout + { + public: + CoreFlowGraphLayout(BNFlowGraphLayout* layout); + + virtual bool Layout(Ref<FlowGraph> graph, std::vector<Ref<FlowGraphNode>>& nodes) override; + }; + /*! \ingroup lowlevelil */ diff --git a/binaryninjacore.h b/binaryninjacore.h index ce74885c..2d3fbca3 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -216,6 +216,7 @@ extern "C" typedef struct BNTypePrinter BNTypePrinter; typedef struct BNFlowGraph BNFlowGraph; typedef struct BNFlowGraphNode BNFlowGraphNode; + typedef struct BNFlowGraphLayout BNFlowGraphLayout; typedef struct BNFlowGraphLayoutRequest BNFlowGraphLayoutRequest; typedef struct BNSymbol BNSymbol; typedef struct BNTemporaryFile BNTemporaryFile; @@ -3102,6 +3103,12 @@ extern "C" void (*externalRefReleased)(void* ctxt); } BNCustomFlowGraph; + typedef struct BNCustomFlowGraphLayout + { + void* context; + bool (*layout)(void* ctxt, BNFlowGraph* graph, BNFlowGraphNode** nodes, size_t nodeCount); + } BNCustomFlowGraphLayout; + typedef struct BNRange { uint64_t start; @@ -5271,6 +5278,8 @@ extern "C" BINARYNINJACOREAPI int BNGetFlowGraphWidth(BNFlowGraph* graph); BINARYNINJACOREAPI int BNGetFlowGraphHeight(BNFlowGraph* graph); + BINARYNINJACOREAPI void BNFlowGraphSetWidth(BNFlowGraph* graph, int width); + BINARYNINJACOREAPI void BNFlowGraphSetHeight(BNFlowGraph* graph, int height); BINARYNINJACOREAPI BNFlowGraphNode* BNCreateFlowGraphNode(BNFlowGraph* graph); BINARYNINJACOREAPI BNFlowGraphNode* BNNewFlowGraphNodeReference(BNFlowGraphNode* node); @@ -5279,6 +5288,10 @@ extern "C" BINARYNINJACOREAPI BNBasicBlock* BNGetFlowGraphBasicBlock(BNFlowGraphNode* node); BINARYNINJACOREAPI void BNSetFlowGraphBasicBlock(BNFlowGraphNode* node, BNBasicBlock* block); + + BINARYNINJACOREAPI void BNFlowGraphNodeSetX(BNFlowGraphNode* node, int x); + BINARYNINJACOREAPI void BNFlowGraphNodeSetY(BNFlowGraphNode* node, int y); + BINARYNINJACOREAPI int BNGetFlowGraphNodeX(BNFlowGraphNode* node); BINARYNINJACOREAPI int BNGetFlowGraphNodeY(BNFlowGraphNode* node); BINARYNINJACOREAPI int BNGetFlowGraphNodeWidth(BNFlowGraphNode* node); @@ -5307,6 +5320,16 @@ extern "C" BINARYNINJACOREAPI bool BNIsNodeValidForFlowGraph(BNFlowGraph* graph, BNFlowGraphNode* node); + BINARYNINJACOREAPI void BNFlowGraphNodeSetVisibilityRegion(BNFlowGraphNode* node, int x, int y, int w, int h); + BINARYNINJACOREAPI void BNFlowGraphNodeSetOutgoingEdgePoints(BNFlowGraphNode* node, size_t edgeNum, BNPoint* points, size_t pointCount); + + BINARYNINJACOREAPI BNFlowGraphLayout** BNGetFlowGraphLayouts(size_t* count); + BINARYNINJACOREAPI void BNFreeFlowGraphLayoutList(BNFlowGraphLayout** layouts); + BINARYNINJACOREAPI BNFlowGraphLayout* BNRegisterFlowGraphLayout(const char* name, BNCustomFlowGraphLayout* callbacks); + BINARYNINJACOREAPI BNFlowGraphLayout* BNGetFlowGraphLayoutByName(const char* name); + BINARYNINJACOREAPI char* BNGetFlowGraphLayoutName(BNFlowGraphLayout* layout); + BINARYNINJACOREAPI bool BNFlowGraphLayoutLayout(BNFlowGraphLayout* layout, BNFlowGraph* graph, BNFlowGraphNode** nodes, size_t nodeCount); + // Symbols BINARYNINJACOREAPI BNSymbol* BNCreateSymbol(BNSymbolType type, const char* shortName, const char* fullName, const char* rawName, uint64_t addr, BNSymbolBinding binding, const BNNameSpace* nameSpace, uint64_t ordinal); diff --git a/flowgraph.cpp b/flowgraph.cpp index 1bbf7fc3..257764c9 100644 --- a/flowgraph.cpp +++ b/flowgraph.cpp @@ -275,12 +275,24 @@ int FlowGraph::GetWidth() const } +void FlowGraph::SetWidth(int width) +{ + BNFlowGraphSetWidth(m_object, width); +} + + int FlowGraph::GetHeight() const { return BNGetFlowGraphHeight(m_object); } +void FlowGraph::SetHeight(int height) +{ + BNFlowGraphSetHeight(m_object, height); +} + + vector<Ref<FlowGraphNode>> FlowGraph::GetNodesInRegion(int left, int top, int right, int bottom) { size_t count; diff --git a/flowgraphlayout.cpp b/flowgraphlayout.cpp new file mode 100644 index 00000000..e8f31346 --- /dev/null +++ b/flowgraphlayout.cpp @@ -0,0 +1,117 @@ +// Copyright (c) 2015-2024 Vector 35 Inc +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to +// deal in the Software without restriction, including without limitation the +// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +// sell copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +// IN THE SOFTWARE. + +#include "binaryninjaapi.h" + +using namespace BinaryNinja; +using namespace std; + + +FlowGraphLayout::FlowGraphLayout(BNFlowGraphLayout* layout) +{ + m_object = layout; +} + + +FlowGraphLayout::FlowGraphLayout(const string& name) : m_nameForRegister(name) +{ + m_object = nullptr; +} + + +bool FlowGraphLayout::LayoutCallback(void* ctxt, BNFlowGraph* graph, BNFlowGraphNode** nodes, size_t nodeCount) +{ + CallbackRef<FlowGraphLayout> layout(ctxt); + std::vector<Ref<FlowGraphNode>> nodeVec; + nodeVec.reserve(nodeCount); + + for (size_t i = 0; i < nodeCount; i++) + { + nodeVec.push_back(new FlowGraphNode(nodes[i])); + } + + bool result = layout->Layout(new CoreFlowGraph(graph), nodeVec); + return result; +} + + +void FlowGraphLayout::Register(FlowGraphLayout* layout) +{ + BNCustomFlowGraphLayout callbacks; + callbacks.context = layout; + callbacks.layout = LayoutCallback; + layout->AddRefForRegistration(); + layout->m_object = BNRegisterFlowGraphLayout(layout->m_nameForRegister.c_str(), &callbacks); +} + + +Ref<FlowGraphLayout> FlowGraphLayout::GetByName(const string& name) +{ + BNFlowGraphLayout* result = BNGetFlowGraphLayoutByName(name.c_str()); + if (!result) + return nullptr; + return new CoreFlowGraphLayout(result); +} + + +vector<Ref<FlowGraphLayout>> FlowGraphLayout::GetFlowGraphLayouts() +{ + size_t count; + BNFlowGraphLayout** list = BNGetFlowGraphLayouts(&count); + + vector<Ref<FlowGraphLayout>> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + result.push_back(new CoreFlowGraphLayout(list[i])); + + BNFreeFlowGraphLayoutList(list); + return result; +} + + +string FlowGraphLayout::GetName() const +{ + char* name = BNGetFlowGraphLayoutName(m_object); + string result = name; + BNFreeString(name); + return result; +} + + +bool FlowGraphLayout::Layout(Ref<FlowGraph> graph, std::vector<Ref<FlowGraphNode>>& nodes) +{ + return false; +} + + +CoreFlowGraphLayout::CoreFlowGraphLayout(BNFlowGraphLayout* layout) : FlowGraphLayout(layout) {} + + +bool CoreFlowGraphLayout::Layout(Ref<FlowGraph> graph, std::vector<Ref<FlowGraphNode>>& nodes) +{ + BNFlowGraphNode** nodeList = new BNFlowGraphNode*[nodes.size()]; + for (size_t i = 0; i < nodes.size(); i++) + { + nodeList[i] = nodes[i]->m_object; + } + bool result = BNFlowGraphLayoutLayout(m_object, graph->m_object, nodeList, nodes.size()); + delete[] nodeList; + return result; +} diff --git a/flowgraphnode.cpp b/flowgraphnode.cpp index 8a7270d2..e1e2351a 100644 --- a/flowgraphnode.cpp +++ b/flowgraphnode.cpp @@ -66,6 +66,18 @@ void FlowGraphNode::SetBasicBlock(BasicBlock* block) } +void FlowGraphNode::SetX(int x) +{ + BNFlowGraphNodeSetX(m_object, x); +} + + +void FlowGraphNode::SetY(int y) +{ + BNFlowGraphNodeSetY(m_object, y); +} + + int FlowGraphNode::GetX() const { return BNGetFlowGraphNodeX(m_object); @@ -225,3 +237,9 @@ bool FlowGraphNode::IsValidForGraph(FlowGraph* graph) const { return BNIsNodeValidForFlowGraph(graph->GetObject(), m_object); } + + +void FlowGraphNode::SetVisibilityRegion(int x, int y, int w, int h) +{ + BNFlowGraphNodeSetVisibilityRegion(m_object, x, y, w, h); +} diff --git a/python/examples/custom_graph_layout.py b/python/examples/custom_graph_layout.py new file mode 100644 index 00000000..b76353de --- /dev/null +++ b/python/examples/custom_graph_layout.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# Copyright (c) 2015-2024 Vector 35 Inc +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. + +import random + +from binaryninja import FlowGraph, FlowGraphLayout, FlowGraphNode + + +class RandomLayout(FlowGraphLayout): + def layout(self, graph: FlowGraph, nodes: list[FlowGraphNode]): + min_x = 0 + max_x = 0 + min_y = 0 + max_y = 0 + + max_extent = len(nodes) * 50 + + # Place nodes + for node in nodes: + x = random.randint(0, max_extent) + y = random.randint(0, max_extent) + + min_x = min(x, min_x) + max_x = max(x + node.width, max_x) + + min_y = min(y, min_y) + max_y = max(y + node.height, max_y) + + node.x = x + node.y = y + + # Place edges + for node in nodes: + for edge_num,edge in enumerate(node.outgoing_edges): + points = [ + (node.x + node.width/2, node.y + node.height), + (edge.target.x + edge.target.width/2, edge.target.y + edge.target.height) + ] + node.set_outgoing_edge_points(edge_num, points) + + # Calculate graph size and node visibility + for node in nodes: + min_node_x = node.x + max_node_x = node.x + node.width + + min_node_y = node.y + max_node_y = node.y + node.height + for edge in node.outgoing_edges: + for point in edge.points: + px, py = point + min_x = min(min_x, px) + min_y = min(min_y, py) + + max_x = max(max_x, px + 1) + max_y = max(max_y, py + 1) + + min_node_x = min(min_node_x, px) + max_node_x = max(max_node_x, px+1) + + min_node_y = min(min_node_y, py) + max_node_y = max(max_node_y, py+1) + + node.set_visibility_region(int(min_node_x), int(min_node_y), int(max_node_x - min_node_x), int(max_node_y - min_node_y)) + + graph.width = int(max_x - min_x) + graph.horizontal_block_margin*2 + graph.height = int(max_y - min_y) + graph.vertical_block_margin*2 + + return True + +layout = RandomLayout() +layout.register("Random Layout") diff --git a/python/flowgraph.py b/python/flowgraph.py index 431b8657..c26b20a7 100644 --- a/python/flowgraph.py +++ b/python/flowgraph.py @@ -21,7 +21,7 @@ import ctypes import threading import traceback -from typing import Optional +from typing import List, Optional, Tuple # Binary Ninja components import binaryninja @@ -61,14 +61,14 @@ class FlowGraphEdge: class EdgeStyle: - def __init__(self, style=None, width=None, theme_color=None): + def __init__(self, style: Optional[EdgePenStyle] = None, width: Optional[int] = None, theme_color: Optional[ThemeColor] = None): self.style = style if style is not None else EdgePenStyle.SolidLine self.width = width if width is not None else 0 self.color = theme_color if theme_color is not None else ThemeColor.AddressColor def _to_core_struct(self) -> core.BNEdgeStyle: result = core.BNEdgeStyle() - result.style = self.style + result.style = int(self.style) result.width = self.width result.color = self.color return result @@ -190,14 +190,22 @@ class FlowGraphNode: @property def x(self): - """Flow graph block X (read-only)""" + """Flow graph block X""" return core.BNGetFlowGraphNodeX(self.handle) + @x.setter + def x(self, x: int): + return core.BNFlowGraphNodeSetX(self.handle, x) + @property def y(self): - """Flow graph block Y (read-only)""" + """Flow graph block Y""" return core.BNGetFlowGraphNodeY(self.handle) + @y.setter + def y(self, y: int): + return core.BNFlowGraphNodeSetY(self.handle, y) + @property def width(self): """Flow graph block width (read-only)""" @@ -263,7 +271,7 @@ class FlowGraphNode: core.BNSetFlowGraphNodeLines(self.handle, line_buf, len(lines)) @property - def outgoing_edges(self): + def outgoing_edges(self) -> List[FlowGraphEdge]: """Flow graph block list of outgoing edges (read-only)""" count = ctypes.c_ulonglong() edges = core.BNGetFlowGraphNodeOutgoingEdges(self.handle, count) @@ -346,6 +354,16 @@ class FlowGraphNode: def is_valid_for_graph(self, graph): return core.BNIsNodeValidForFlowGraph(graph.handle, self.handle) + def set_visibility_region(self, x: int, y: int, w: int, h: int): + core.BNFlowGraphNodeSetVisibilityRegion(self.handle, x, y, w, h) + + def set_outgoing_edge_points(self, edge_num: int, points: List[Tuple[float, float]]): + point_buf = (core.BNPoint * len(points))() + for i in range(0, len(points)): + point_buf[i].x = points[i][0] + point_buf[i].y = points[i][1] + core.BNFlowGraphNodeSetOutgoingEdgePoints(self.handle, edge_num, point_buf, len(points)) + class FlowGraphLayoutRequest: def __init__(self, graph, callback=None): @@ -601,14 +619,22 @@ class FlowGraph: @property def width(self): - """Flow graph width (read-only)""" + """Flow graph width""" return core.BNGetFlowGraphWidth(self.handle) + @width.setter + def width(self, width: int): + return core.BNFlowGraphSetWidth(self.handle, width) + @property def height(self): - """Flow graph height (read-only)""" + """Flow graph height""" return core.BNGetFlowGraphHeight(self.handle) + @height.setter + def height(self, height: int): + return core.BNFlowGraphSetHeight(self.handle, height) + @property def horizontal_block_margin(self): return core.BNGetHorizontalFlowGraphNodeMargin(self.handle) @@ -835,3 +861,32 @@ class CoreFlowGraph(FlowGraph): if not graph: return None return CoreFlowGraph(graph) + + +class FlowGraphLayout: + def __init__(self, handle: Optional[core.BNCustomFlowGraphLayout] = None): + if handle is not None: + self.handle = core.handle_of_type(handle, core.BNFlowGraphLayout) + + def register(self, name: str): + """ + Register a custom layout with the API + """ + self._cb = core.BNCustomFlowGraphLayout() + self._cb.context = 0 + self._cb.layout = self._cb.layout.__class__(self._layout) + self.handle = core.BNRegisterFlowGraphLayout(name, self._cb) + + def _layout(self, ctxt, graph_handle: core.BNFlowGraphHandle, node_handles: 'ctypes.pointer[core.BNFlowGraphNodeHandle]', node_handle_count: int) -> bool: + try: + graph = FlowGraph(handle=graph_handle) + nodes = [] + for i in range(node_handle_count): + nodes.append(FlowGraphNode(graph=graph, handle=node_handles[i])) + return self.layout(graph, nodes) + except: + log_error(traceback.format_exc()) + return False + + def layout(self, graph: FlowGraph, nodes: List[FlowGraphNode]) -> bool: + return False |
