From bfa6fce83383e7be1458a917f8e6dbf71bdab28b Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 4 Jun 2018 14:12:26 -0400 Subject: Generic flow graph API and report collections --- flowgraph.cpp | 261 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 flowgraph.cpp (limited to 'flowgraph.cpp') diff --git a/flowgraph.cpp b/flowgraph.cpp new file mode 100644 index 00000000..e557f540 --- /dev/null +++ b/flowgraph.cpp @@ -0,0 +1,261 @@ +// Copyright (c) 2015-2017 Vector 35 LLC +// +// 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; + + +FlowGraph::FlowGraph(BNFlowGraph* graph): m_graph(graph) +{ +} + + +FlowGraph::~FlowGraph() +{ + // This object is going away, so ensure that any pending completion routines are + // no longer called + Abort(); + + BNFreeFlowGraph(m_graph); +} + + +void FlowGraph::CompleteCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->m_completeFunc(); +} + + +Ref FlowGraph::GetFunction() const +{ + BNFunction* func = BNGetFunctionForFlowGraph(m_graph); + if (!func) + return nullptr; + return new Function(BNNewFunctionReference(func)); +} + + +void FlowGraph::SetFunction(Function* func) +{ + BNSetFunctionForFlowGraph(m_graph, func ? func->GetObject() : nullptr); +} + + +int FlowGraph::GetHorizontalNodeMargin() const +{ + return BNGetHorizontalFlowGraphNodeMargin(m_graph); +} + + +int FlowGraph::GetVerticalNodeMargin() const +{ + return BNGetVerticalFlowGraphNodeMargin(m_graph); +} + + +void FlowGraph::SetNodeMargins(int horiz, int vert) +{ + BNSetFlowGraphNodeMargins(m_graph, horiz, vert); +} + + +void FlowGraph::StartLayout() +{ + BNStartFlowGraphLayout(m_graph); +} + + +bool FlowGraph::IsLayoutComplete() +{ + return BNIsFlowGraphLayoutComplete(m_graph); +} + + +void FlowGraph::OnComplete(const std::function& func) +{ + m_completeFunc = func; + BNSetFlowGraphCompleteCallback(m_graph, this, CompleteCallback); +} + + +void FlowGraph::Abort() +{ + // Must clear the callback with the core before clearing our own function object, as until it + // is cleared in the core it can be called at any time from a different thread. + BNAbortFlowGraph(m_graph); + m_completeFunc = []() {}; +} + + +vector> FlowGraph::GetNodes() +{ + size_t count; + BNFlowGraphNode** nodes = BNGetFlowGraphNodes(m_graph, &count); + + vector> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + auto node = m_cachedNodes.find(nodes[i]); + if (node == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(BNNewFlowGraphNodeReference(nodes[i])); + m_cachedNodes[nodes[i]] = newNode; + result.push_back(newNode); + } + else + { + result.push_back(node->second); + } + } + + BNFreeFlowGraphNodeList(nodes, count); + return result; +} + + +Ref FlowGraph::GetNode(size_t i) +{ + BNFlowGraphNode* node = BNGetFlowGraphNode(m_graph, i); + if (!node) + return nullptr; + + auto nodeIter = m_cachedNodes.find(node); + if (nodeIter == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(node); + m_cachedNodes[node] = newNode; + return newNode; + } + else + { + BNFreeFlowGraphNode(node); + return nodeIter->second; + } +} + + +bool FlowGraph::HasNodes() const +{ + return BNFlowGraphHasNodes(m_graph); +} + + +size_t FlowGraph::AddNode(FlowGraphNode* node) +{ + m_cachedNodes[node->GetObject()] = node; + return BNAddFlowGraphNode(m_graph, node->GetObject()); +} + + +int FlowGraph::GetWidth() const +{ + return BNGetFlowGraphWidth(m_graph); +} + + +int FlowGraph::GetHeight() const +{ + return BNGetFlowGraphHeight(m_graph); +} + + +vector> FlowGraph::GetNodesInRegion(int left, int top, int right, int bottom) +{ + size_t count; + BNFlowGraphNode** nodes = BNGetFlowGraphNodesInRegion(m_graph, left, top, right, bottom, &count); + + vector> result; + result.reserve(count); + for (size_t i = 0; i < count; i++) + { + auto node = m_cachedNodes.find(nodes[i]); + if (node == m_cachedNodes.end()) + { + FlowGraphNode* newNode = new FlowGraphNode(BNNewFlowGraphNodeReference(nodes[i])); + m_cachedNodes[nodes[i]] = newNode; + result.push_back(newNode); + } + else + { + result.push_back(node->second); + } + } + + BNFreeFlowGraphNodeList(nodes, count); + return result; +} + + +bool FlowGraph::IsILGraph() const +{ + return BNIsILFlowGraph(m_graph); +} + + +bool FlowGraph::IsLowLevelILGraph() const +{ + return BNIsLowLevelILFlowGraph(m_graph); +} + + +bool FlowGraph::IsMediumLevelILGraph() const +{ + return BNIsMediumLevelILFlowGraph(m_graph); +} + + +Ref FlowGraph::GetLowLevelILFunction() const +{ + BNLowLevelILFunction* func = BNGetFlowGraphLowLevelILFunction(m_graph); + if (!func) + return nullptr; + return new LowLevelILFunction(func); +} + + +Ref FlowGraph::GetMediumLevelILFunction() const +{ + BNMediumLevelILFunction* func = BNGetFlowGraphMediumLevelILFunction(m_graph); + if (!func) + return nullptr; + return new MediumLevelILFunction(func); +} + + +void FlowGraph::SetLowLevelILFunction(LowLevelILFunction* func) +{ + BNSetFlowGraphLowLevelILFunction(m_graph, func ? func->GetObject() : nullptr); +} + + +void FlowGraph::SetMediumLevelILFunction(MediumLevelILFunction* func) +{ + BNSetFlowGraphMediumLevelILFunction(m_graph, func ? func->GetObject() : nullptr); +} + + +void FlowGraph::Show(const string& title) +{ + ShowGraphReport(title, this); +} -- cgit v1.3.1 From dfe5d28f0aaee75ccad8c733e23879676ed77c37 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Wed, 11 Jul 2018 20:32:22 -0400 Subject: Add APIs for subclassing flow graphs --- binaryninjaapi.h | 38 +++++++++++++++++++++++++++++++------ binaryninjacore.h | 11 +++++++++++ flowgraph.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++ python/flowgraph.py | 40 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 134 insertions(+), 9 deletions(-) (limited to 'flowgraph.cpp') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 3582b265..1133625e 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -54,6 +54,7 @@ namespace BinaryNinja virtual ~RefCountObject() {} RefCountObject* GetObject() { return this; } + static RefCountObject* GetObject(RefCountObject* obj) { return obj; } void AddRef() { @@ -107,6 +108,13 @@ namespace BinaryNinja T* GetObject() const { return m_object; } + static T* GetObject(CoreRefCountObject* obj) + { + if (!obj) + return nullptr; + return obj->GetObject(); + } + void AddRef() { if (m_object && (m_refs != 0)) @@ -164,6 +172,13 @@ namespace BinaryNinja T* GetObject() const { return m_object; } + static T* GetObject(StaticCoreRefCountObject* obj) + { + if (!obj) + return nullptr; + return obj->GetObject(); + } + void AddRef() { AddRefInternal(); @@ -252,32 +267,32 @@ namespace BinaryNinja bool operator==(const T* obj) const { - return m_obj->GetObject() == obj->GetObject(); + return T::GetObject(m_obj) == T::GetObject(obj); } bool operator==(const Ref& obj) const { - return m_obj->GetObject() == obj.m_obj->GetObject(); + return T::GetObject(m_obj) == T::GetObject(obj.m_obj); } bool operator!=(const T* obj) const { - return m_obj->GetObject() != obj->GetObject(); + return T::GetObject(m_obj) != T::GetObject(obj); } bool operator!=(const Ref& obj) const { - return m_obj->GetObject() != obj.m_obj->GetObject(); + return T::GetObject(m_obj) != T::GetObject(obj.m_obj); } bool operator<(const T* obj) const { - return m_obj->GetObject() < obj->GetObject(); + return T::GetObject(m_obj) < T::GetObject(obj); } bool operator<(const Ref& obj) const { - return m_obj->GetObject() < obj.m_obj->GetObject(); + return T::GetObject(m_obj) < T::GetObject(obj.m_obj); } T* GetPtr() const @@ -2567,7 +2582,18 @@ namespace BinaryNinja static void CompleteCallback(void* ctxt); + static void PrepareForLayoutCallback(void* ctxt); + static void PopulateNodesCallback(void* ctxt); + static void CompleteLayoutCallback(void* ctxt); + + protected: + void FinishPrepareForLayout(); + virtual void PrepareForLayout(); + virtual void PopulateNodes(); + virtual void CompleteLayout(); + public: + FlowGraph(); FlowGraph(BNFlowGraph* graph); ~FlowGraph(); diff --git a/binaryninjacore.h b/binaryninjacore.h index c057b378..a32d8364 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1772,6 +1772,14 @@ extern "C" FlowGraphReportType }; + struct BNCustomFlowGraph + { + void* context; + void (*prepareForLayout)(void* ctxt); + void (*populateNodes)(void* ctxt); + void (*completeLayout)(void* ctxt); + }; + BINARYNINJACOREAPI char* BNAllocString(const char* contents); BINARYNINJACOREAPI void BNFreeString(char* str); BINARYNINJACOREAPI char** BNAllocStringList(const char** contents, size_t size); @@ -2566,6 +2574,7 @@ extern "C" BINARYNINJACOREAPI BNFlowGraph* BNCreateFlowGraph(); BINARYNINJACOREAPI BNFlowGraph* BNCreateFunctionGraph(BNFunction* func, BNFunctionGraphType type, BNDisassemblySettings* settings); + BINARYNINJACOREAPI BNFlowGraph* BNCreateCustomFlowGraph(BNCustomFlowGraph* callbacks); BINARYNINJACOREAPI BNFlowGraph* BNNewFlowGraphReference(BNFlowGraph* graph); BINARYNINJACOREAPI void BNFreeFlowGraph(BNFlowGraph* graph); BINARYNINJACOREAPI BNFunction* BNGetFunctionForFlowGraph(BNFlowGraph* graph); @@ -2618,6 +2627,8 @@ extern "C" BINARYNINJACOREAPI BNHighlightColor BNGetFlowGraphNodeHighlight(BNFlowGraphNode* node); BINARYNINJACOREAPI void BNSetFlowGraphNodeHighlight(BNFlowGraphNode* node, BNHighlightColor color); + BINARYNINJACOREAPI void BNFinishPrepareForLayout(BNFlowGraph* graph); + // Symbols BINARYNINJACOREAPI BNSymbol* BNCreateSymbol(BNSymbolType type, const char* shortName, const char* fullName, const char* rawName, uint64_t addr); diff --git a/flowgraph.cpp b/flowgraph.cpp index e557f540..4bacee81 100644 --- a/flowgraph.cpp +++ b/flowgraph.cpp @@ -24,6 +24,17 @@ using namespace BinaryNinja; using namespace std; +FlowGraph::FlowGraph() +{ + BNCustomFlowGraph callbacks; + callbacks.context = this; + callbacks.prepareForLayout = PrepareForLayoutCallback; + callbacks.populateNodes = PopulateNodesCallback; + callbacks.completeLayout = CompleteLayoutCallback; + m_graph = BNCreateCustomFlowGraph(&callbacks); +} + + FlowGraph::FlowGraph(BNFlowGraph* graph): m_graph(graph) { } @@ -46,6 +57,49 @@ void FlowGraph::CompleteCallback(void* ctxt) } +void FlowGraph::PrepareForLayoutCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->PrepareForLayout(); +} + + +void FlowGraph::PopulateNodesCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->PopulateNodes(); +} + + +void FlowGraph::CompleteLayoutCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + graph->CompleteLayout(); +} + + +void FlowGraph::FinishPrepareForLayout() +{ + BNFinishPrepareForLayout(m_graph); +} + + +void FlowGraph::PrepareForLayout() +{ + FinishPrepareForLayout(); +} + + +void FlowGraph::PopulateNodes() +{ +} + + +void FlowGraph::CompleteLayout() +{ +} + + Ref FlowGraph::GetFunction() const { BNFunction* func = BNGetFunctionForFlowGraph(m_graph); diff --git a/python/flowgraph.py b/python/flowgraph.py index 1568bc77..25fdc430 100644 --- a/python/flowgraph.py +++ b/python/flowgraph.py @@ -24,13 +24,12 @@ import traceback # Binary Ninja components import _binaryninjacore as core -from enums import (BranchType, InstructionTextTokenType, HighlightColorStyle, HighlightStandardColor) +from enums import (BranchType, InstructionTextTokenType, HighlightStandardColor) import function import binaryview import lowlevelil import mediumlevelil import basicblock -import architecture import log import interaction import highlight @@ -264,7 +263,12 @@ class FlowGraphNode(object): class FlowGraph(object): def __init__(self, handle = None): if handle is None: - handle = core.BNCreateFlowGraph() + self._ext_cb = core.BNCustomFlowGraph() + self._ext_cb.context = 0 + self._ext_cb.prepareForLayout = self._ext_cb.prepareForLayout.__class__(self._prepare_for_layout) + self._ext_cb.populateNodes = self._ext_cb.populateNodes.__class__(self._populate_nodes) + self._ext_cb.completeLayout = self._ext_cb.completeLayout.__class__(self._complete_layout) + handle = core.BNCreateCustomFlowGraph(self._ext_cb) self.handle = handle self._on_complete = None self._cb = ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self._complete) @@ -283,6 +287,36 @@ class FlowGraph(object): return True return ctypes.addressof(self.handle.contents) != ctypes.addressof(value.handle.contents) + def _prepare_for_layout(self, ctxt): + try: + self.prepare_for_layout() + except: + log.log_error(traceback.format_exc()) + + def _populate_nodes(self, ctxt): + try: + self.populate_nodes() + except: + log.log_error(traceback.format_exc()) + + def _complete_layout(self, ctxt): + try: + self.complete_layout() + except: + log.log_error(traceback.format_exc()) + + def finish_prepare_for_layout(self): + core.BNFinishPrepareForLayout(self.handle) + + def prepare_for_layout(self): + self.finish_prepare_for_layout() + + def populate_nodes(self): + pass + + def complete_layout(self): + pass + @property def function(self): """Function for a flow graph""" -- cgit v1.3.1 From c5c93fc82b8929d04f62d241ca50228de60fa5f4 Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Fri, 13 Jul 2018 18:43:34 -0400 Subject: Add ability to update custom flow graphs --- binaryninjaapi.h | 13 ++++++++++++- binaryninjacore.h | 3 +++ flowgraph.cpp | 30 ++++++++++++++++++++++++++++++ function.cpp | 4 ++-- interaction.cpp | 4 ++-- python/flowgraph.py | 25 +++++++++++++++++++++++++ python/function.py | 4 ++-- python/interaction.py | 4 ++-- 8 files changed, 78 insertions(+), 9 deletions(-) (limited to 'flowgraph.cpp') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 1133625e..dd685d2b 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2585,8 +2585,11 @@ namespace BinaryNinja static void PrepareForLayoutCallback(void* ctxt); static void PopulateNodesCallback(void* ctxt); static void CompleteLayoutCallback(void* ctxt); + static BNFlowGraph* UpdateCallback(void* ctxt); protected: + FlowGraph(BNFlowGraph* graph); + void FinishPrepareForLayout(); virtual void PrepareForLayout(); virtual void PopulateNodes(); @@ -2594,7 +2597,6 @@ namespace BinaryNinja public: FlowGraph(); - FlowGraph(BNFlowGraph* graph); ~FlowGraph(); BNFlowGraph* GetGraphObject() const { return m_graph; } @@ -2629,6 +2631,15 @@ namespace BinaryNinja void SetMediumLevelILFunction(MediumLevelILFunction* func); void Show(const std::string& title); + + virtual Ref Update(); + }; + + class CoreFlowGraph: public FlowGraph + { + public: + CoreFlowGraph(BNFlowGraph* graph); + virtual Ref Update() override; }; struct LowLevelILLabel: public BNLowLevelILLabel diff --git a/binaryninjacore.h b/binaryninjacore.h index a32d8364..9359ca0e 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -1778,6 +1778,7 @@ extern "C" void (*prepareForLayout)(void* ctxt); void (*populateNodes)(void* ctxt); void (*completeLayout)(void* ctxt); + BNFlowGraph* (*update)(void* ctxt); }; BINARYNINJACOREAPI char* BNAllocString(const char* contents); @@ -2629,6 +2630,8 @@ extern "C" BINARYNINJACOREAPI void BNFinishPrepareForLayout(BNFlowGraph* graph); + BINARYNINJACOREAPI BNFlowGraph* BNUpdateFlowGraph(BNFlowGraph* graph); + // Symbols BINARYNINJACOREAPI BNSymbol* BNCreateSymbol(BNSymbolType type, const char* shortName, const char* fullName, const char* rawName, uint64_t addr); diff --git a/flowgraph.cpp b/flowgraph.cpp index 4bacee81..c779db0c 100644 --- a/flowgraph.cpp +++ b/flowgraph.cpp @@ -78,6 +78,16 @@ void FlowGraph::CompleteLayoutCallback(void* ctxt) } +BNFlowGraph* FlowGraph::UpdateCallback(void* ctxt) +{ + FlowGraph* graph = (FlowGraph*)ctxt; + Ref result = graph->Update(); + if (!result) + return nullptr; + return BNNewFlowGraphReference(result->GetGraphObject()); +} + + void FlowGraph::FinishPrepareForLayout() { BNFinishPrepareForLayout(m_graph); @@ -313,3 +323,23 @@ void FlowGraph::Show(const string& title) { ShowGraphReport(title, this); } + + +Ref FlowGraph::Update() +{ + return nullptr; +} + + +CoreFlowGraph::CoreFlowGraph(BNFlowGraph* graph): FlowGraph(graph) +{ +} + + +Ref CoreFlowGraph::Update() +{ + BNFlowGraph* graph = BNUpdateFlowGraph(GetGraphObject()); + if (!graph) + return nullptr; + return new CoreFlowGraph(graph); +} diff --git a/function.cpp b/function.cpp index 62a8e1b0..202f6d77 100644 --- a/function.cpp +++ b/function.cpp @@ -811,7 +811,7 @@ void Function::ApplyAutoDiscoveredType(Type* type) Ref Function::CreateFunctionGraph(BNFunctionGraphType type, DisassemblySettings* settings) { BNFlowGraph* graph = BNCreateFunctionGraph(m_object, type, settings ? settings->GetObject() : nullptr); - return new FlowGraph(graph); + return new CoreFlowGraph(graph); } @@ -1396,7 +1396,7 @@ Ref Function::GetUnresolvedStackAdjustmentGraph() BNFlowGraph* graph = BNGetUnresolvedStackAdjustmentGraph(m_object); if (!graph) return nullptr; - return new FlowGraph(graph); + return new CoreFlowGraph(graph); } diff --git a/interaction.cpp b/interaction.cpp index 61c9dfb3..da3942bb 100644 --- a/interaction.cpp +++ b/interaction.cpp @@ -208,7 +208,7 @@ static void ShowGraphReportCallback(void* ctxt, BNBinaryView* view, const char* { InteractionHandler* handler = (InteractionHandler*)ctxt; handler->ShowGraphReport(view ? new BinaryView(BNNewViewReference(view)) : nullptr, title, - new FlowGraph(BNNewFlowGraphReference(graph))); + new CoreFlowGraph(BNNewFlowGraphReference(graph))); } @@ -663,7 +663,7 @@ Ref ReportCollection::GetFlowGraph(size_t i) const BNFlowGraph* graph = BNGetReportFlowGraph(m_object, i); if (!graph) return nullptr; - return new FlowGraph(graph); + return new CoreFlowGraph(graph); } diff --git a/python/flowgraph.py b/python/flowgraph.py index 25fdc430..e6c98597 100644 --- a/python/flowgraph.py +++ b/python/flowgraph.py @@ -268,6 +268,7 @@ class FlowGraph(object): self._ext_cb.prepareForLayout = self._ext_cb.prepareForLayout.__class__(self._prepare_for_layout) self._ext_cb.populateNodes = self._ext_cb.populateNodes.__class__(self._populate_nodes) self._ext_cb.completeLayout = self._ext_cb.completeLayout.__class__(self._complete_layout) + self._ext_cb.update = self._ext_cb.update.__class__(self._update) handle = core.BNCreateCustomFlowGraph(self._ext_cb) self.handle = handle self._on_complete = None @@ -305,6 +306,16 @@ class FlowGraph(object): except: log.log_error(traceback.format_exc()) + def _update(self, ctxt): + try: + graph = self.update() + if graph is None: + return None + return core.BNNewFlowGraphReference(graph.handle) + except: + log.log_error(traceback.format_exc()) + return None + def finish_prepare_for_layout(self): core.BNFinishPrepareForLayout(self.handle) @@ -497,3 +508,17 @@ class FlowGraph(object): def show(self, title): interaction.show_graph_report(title, self) + + def update(self): + return None + + +class CoreFlowGraph(FlowGraph): + def __init__(self, handle): + super(CoreFlowGraph, self).__init__(handle) + + def update(self): + graph = core.BNUpdateFlowGraph(self.handle) + if not graph: + return None + return CoreFlowGraph(graph) diff --git a/python/function.py b/python/function.py index dca9dda6..f64a9677 100644 --- a/python/function.py +++ b/python/function.py @@ -851,7 +851,7 @@ class Function(object): graph = core.BNGetUnresolvedStackAdjustmentGraph(self.handle) if not graph: return None - return flowgraph.FlowGraph(graph) + return flowgraph.CoreFlowGraph(graph) def __iter__(self): count = ctypes.c_ulonglong() @@ -1122,7 +1122,7 @@ class Function(object): settings_obj = settings.handle else: settings_obj = None - return flowgraph.FlowGraph(core.BNCreateFunctionGraph(self.handle, graph_type, settings_obj)) + return flowgraph.CoreFlowGraph(core.BNCreateFunctionGraph(self.handle, graph_type, settings_obj)) def apply_imported_types(self, sym): core.BNApplyImportedTypes(self.handle, sym.handle) diff --git a/python/interaction.py b/python/interaction.py index 81aeb04f..b9aa0c2a 100644 --- a/python/interaction.py +++ b/python/interaction.py @@ -302,7 +302,7 @@ class InteractionHandler(object): view = binaryview.BinaryView(handle = core.BNNewViewReference(view)) else: view = None - self.show_graph_report(view, title, flowgraph.FlowGraph(core.BNNewFlowGraphReference(graph))) + self.show_graph_report(view, title, flowgraph.CoreFlowGraph(core.BNNewFlowGraphReference(graph))) except: log.log_error(traceback.format_exc()) @@ -567,7 +567,7 @@ class ReportCollection(object): plaintext = core.BNGetReportPlainText(self.handle, i) return HTMLReport(title, contents, plaintext, view) elif report_type == ReportType.FlowGraphReportType: - graph = flowgraph.FlowGraph(core.BNGetReportFlowGraph(self.handle, i)) + graph = flowgraph.CoreFlowGraph(core.BNGetReportFlowGraph(self.handle, i)) return FlowGraphReport(title, graph, view) raise TypeError("invalid report type %s" % repr(report_type)) -- cgit v1.3.1 From 78ea5dced49f4576d71c4549001ecdb63a2da53e Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Mon, 30 Jul 2018 15:57:39 -0400 Subject: Don't abort flow graph unless completion routine was set --- flowgraph.cpp | 3 ++- python/flowgraph.py | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) (limited to 'flowgraph.cpp') diff --git a/flowgraph.cpp b/flowgraph.cpp index c779db0c..81950367 100644 --- a/flowgraph.cpp +++ b/flowgraph.cpp @@ -44,7 +44,8 @@ FlowGraph::~FlowGraph() { // This object is going away, so ensure that any pending completion routines are // no longer called - Abort(); + if (m_completeFunc) + Abort(); BNFreeFlowGraph(m_graph); } diff --git a/python/flowgraph.py b/python/flowgraph.py index cf2d8e02..884d6587 100644 --- a/python/flowgraph.py +++ b/python/flowgraph.py @@ -278,7 +278,8 @@ class FlowGraph(object): self._cb = ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self._complete) def __del__(self): - self.abort() + if self._on_complete is not None: + self.abort() core.BNFreeFlowGraph(self.handle) def __eq__(self, value): -- cgit v1.3.1 From 1df50c8093bf3b949055d2670836fa1bb742fc1b Mon Sep 17 00:00:00 2001 From: Rusty Wagner Date: Tue, 14 Aug 2018 19:59:58 -0400 Subject: Modify flow graph API to support multiple layout requests for a single graph --- binaryninjaapi.h | 27 +++++++---- binaryninjacore.h | 10 ++-- binaryview.cpp | 2 +- flowgraph.cpp | 128 +++++++++++++++++++++++++++----------------------- flowgraphnode.cpp | 2 +- interaction.cpp | 6 +-- python/flowgraph.py | 59 ++++++++++++++--------- python/interaction.py | 2 +- 8 files changed, 137 insertions(+), 99 deletions(-) (limited to 'flowgraph.cpp') diff --git a/binaryninjaapi.h b/binaryninjaapi.h index 2c467a35..67d2f7f8 100644 --- a/binaryninjaapi.h +++ b/binaryninjaapi.h @@ -2598,14 +2598,28 @@ namespace BinaryNinja void SetHighlight(const BNHighlightColor& color); }; - class FlowGraph: public RefCountObject + class FlowGraphLayoutRequest: public RefCountObject { - BNFlowGraph* m_graph; + BNFlowGraphLayoutRequest* m_object; std::function m_completeFunc; - std::map> m_cachedNodes; static void CompleteCallback(void* ctxt); + public: + FlowGraphLayoutRequest(FlowGraph* graph, const std::function& completeFunc); + virtual ~FlowGraphLayoutRequest(); + + BNFlowGraphLayoutRequest* GetObject() const { return m_object; } + + Ref GetGraph() const; + bool IsComplete() const; + void Abort(); + }; + + class FlowGraph: public CoreRefCountObject + { + std::map> m_cachedNodes; + static void PrepareForLayoutCallback(void* ctxt); static void PopulateNodesCallback(void* ctxt); static void CompleteLayoutCallback(void* ctxt); @@ -2621,9 +2635,6 @@ namespace BinaryNinja public: FlowGraph(); - ~FlowGraph(); - - BNFlowGraph* GetGraphObject() const { return m_graph; } Ref GetFunction() const; void SetFunction(Function* func); @@ -2632,10 +2643,8 @@ namespace BinaryNinja int GetVerticalNodeMargin() const; void SetNodeMargins(int horiz, int vert); - void StartLayout(); + Ref StartLayout(const std::function& func); bool IsLayoutComplete(); - void OnComplete(const std::function& func); - void Abort(); std::vector> GetNodes(); Ref GetNode(size_t i); diff --git a/binaryninjacore.h b/binaryninjacore.h index 3e060c61..7e353ff0 100644 --- a/binaryninjacore.h +++ b/binaryninjacore.h @@ -124,6 +124,7 @@ extern "C" struct BNDownloadInstance; struct BNFlowGraph; struct BNFlowGraphNode; + struct BNFlowGraphLayoutRequest; struct BNSymbol; struct BNTemporaryFile; struct BNLowLevelILFunction; @@ -2654,10 +2655,13 @@ extern "C" BINARYNINJACOREAPI int BNGetVerticalFlowGraphNodeMargin(BNFlowGraph* graph); BINARYNINJACOREAPI void BNSetFlowGraphNodeMargins(BNFlowGraph* graph, int horiz, int vert); - BINARYNINJACOREAPI void BNStartFlowGraphLayout(BNFlowGraph* graph); + BINARYNINJACOREAPI BNFlowGraphLayoutRequest* BNStartFlowGraphLayout(BNFlowGraph* graph, void* ctxt, void (*func)(void* ctxt)); BINARYNINJACOREAPI bool BNIsFlowGraphLayoutComplete(BNFlowGraph* graph); - BINARYNINJACOREAPI void BNSetFlowGraphCompleteCallback(BNFlowGraph* graph, void* ctxt, void (*func)(void* ctxt)); - BINARYNINJACOREAPI void BNAbortFlowGraph(BNFlowGraph* graph); + BINARYNINJACOREAPI BNFlowGraphLayoutRequest* BNNewFlowGraphLayoutRequestReference(BNFlowGraphLayoutRequest* layout); + BINARYNINJACOREAPI void BNFreeFlowGraphLayoutRequest(BNFlowGraphLayoutRequest* layout); + BINARYNINJACOREAPI bool BNIsFlowGraphLayoutRequestComplete(BNFlowGraphLayoutRequest* layout); + BINARYNINJACOREAPI BNFlowGraph* BNGetGraphForFlowGraphLayoutRequest(BNFlowGraphLayoutRequest* layout); + BINARYNINJACOREAPI void BNAbortFlowGraphLayoutRequest(BNFlowGraphLayoutRequest* graph); BINARYNINJACOREAPI bool BNIsILFlowGraph(BNFlowGraph* graph); BINARYNINJACOREAPI bool BNIsLowLevelILFlowGraph(BNFlowGraph* graph); BINARYNINJACOREAPI bool BNIsMediumLevelILFlowGraph(BNFlowGraph* graph); diff --git a/binaryview.cpp b/binaryview.cpp index da516184..258b0153 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -1720,7 +1720,7 @@ void BinaryView::ShowHTMLReport(const string& title, const string& contents, con void BinaryView::ShowGraphReport(const string& title, FlowGraph* graph) { - BNShowGraphReport(m_object, title.c_str(), graph->GetGraphObject()); + BNShowGraphReport(m_object, title.c_str(), graph->GetObject()); } diff --git a/flowgraph.cpp b/flowgraph.cpp index 81950367..dc7e628e 100644 --- a/flowgraph.cpp +++ b/flowgraph.cpp @@ -24,37 +24,65 @@ using namespace BinaryNinja; using namespace std; -FlowGraph::FlowGraph() +FlowGraphLayoutRequest::FlowGraphLayoutRequest(FlowGraph* graph, const std::function& completeFunc): + m_completeFunc(completeFunc) { - BNCustomFlowGraph callbacks; - callbacks.context = this; - callbacks.prepareForLayout = PrepareForLayoutCallback; - callbacks.populateNodes = PopulateNodesCallback; - callbacks.completeLayout = CompleteLayoutCallback; - m_graph = BNCreateCustomFlowGraph(&callbacks); + m_object = BNStartFlowGraphLayout(graph->GetObject(), this, CompleteCallback); } -FlowGraph::FlowGraph(BNFlowGraph* graph): m_graph(graph) +FlowGraphLayoutRequest::~FlowGraphLayoutRequest() { + // This object is going away, so ensure that any pending completion routines are + // no longer called + Abort(); + + BNFreeFlowGraphLayoutRequest(m_object); } -FlowGraph::~FlowGraph() +void FlowGraphLayoutRequest::CompleteCallback(void* ctxt) { - // This object is going away, so ensure that any pending completion routines are - // no longer called - if (m_completeFunc) - Abort(); + FlowGraphLayoutRequest* layout = (FlowGraphLayoutRequest*)ctxt; + layout->m_completeFunc(); +} - BNFreeFlowGraph(m_graph); + +Ref FlowGraphLayoutRequest::GetGraph() const +{ + return new CoreFlowGraph(BNGetGraphForFlowGraphLayoutRequest(m_object)); } -void FlowGraph::CompleteCallback(void* ctxt) +bool FlowGraphLayoutRequest::IsComplete() const { - FlowGraph* graph = (FlowGraph*)ctxt; - graph->m_completeFunc(); + return BNIsFlowGraphLayoutRequestComplete(m_object); +} + + +void FlowGraphLayoutRequest::Abort() +{ + // Must clear the callback with the core before clearing our own function object, as until it + // is cleared in the core it can be called at any time from a different thread. + BNAbortFlowGraphLayoutRequest(m_object); + m_completeFunc = []() {}; +} + + +FlowGraph::FlowGraph() +{ + BNCustomFlowGraph callbacks; + callbacks.context = this; + callbacks.prepareForLayout = PrepareForLayoutCallback; + callbacks.populateNodes = PopulateNodesCallback; + callbacks.completeLayout = CompleteLayoutCallback; + m_object = BNCreateCustomFlowGraph(&callbacks); +} + + +FlowGraph::FlowGraph(BNFlowGraph* graph) +{ + m_object = graph; } @@ -85,13 +113,13 @@ BNFlowGraph* FlowGraph::UpdateCallback(void* ctxt) Ref result = graph->Update(); if (!result) return nullptr; - return BNNewFlowGraphReference(result->GetGraphObject()); + return BNNewFlowGraphReference(result->GetObject()); } void FlowGraph::FinishPrepareForLayout() { - BNFinishPrepareForLayout(m_graph); + BNFinishPrepareForLayout(m_object); } @@ -113,7 +141,7 @@ void FlowGraph::CompleteLayout() Ref FlowGraph::GetFunction() const { - BNFunction* func = BNGetFunctionForFlowGraph(m_graph); + BNFunction* func = BNGetFunctionForFlowGraph(m_object); if (!func) return nullptr; return new Function(BNNewFunctionReference(func)); @@ -122,60 +150,44 @@ Ref FlowGraph::GetFunction() const void FlowGraph::SetFunction(Function* func) { - BNSetFunctionForFlowGraph(m_graph, func ? func->GetObject() : nullptr); + BNSetFunctionForFlowGraph(m_object, func ? func->GetObject() : nullptr); } int FlowGraph::GetHorizontalNodeMargin() const { - return BNGetHorizontalFlowGraphNodeMargin(m_graph); + return BNGetHorizontalFlowGraphNodeMargin(m_object); } int FlowGraph::GetVerticalNodeMargin() const { - return BNGetVerticalFlowGraphNodeMargin(m_graph); + return BNGetVerticalFlowGraphNodeMargin(m_object); } void FlowGraph::SetNodeMargins(int horiz, int vert) { - BNSetFlowGraphNodeMargins(m_graph, horiz, vert); + BNSetFlowGraphNodeMargins(m_object, horiz, vert); } -void FlowGraph::StartLayout() +Ref FlowGraph::StartLayout(const std::function& func) { - BNStartFlowGraphLayout(m_graph); + return new FlowGraphLayoutRequest(this, func); } bool FlowGraph::IsLayoutComplete() { - return BNIsFlowGraphLayoutComplete(m_graph); -} - - -void FlowGraph::OnComplete(const std::function& func) -{ - m_completeFunc = func; - BNSetFlowGraphCompleteCallback(m_graph, this, CompleteCallback); -} - - -void FlowGraph::Abort() -{ - // Must clear the callback with the core before clearing our own function object, as until it - // is cleared in the core it can be called at any time from a different thread. - BNAbortFlowGraph(m_graph); - m_completeFunc = []() {}; + return BNIsFlowGraphLayoutComplete(m_object); } vector> FlowGraph::GetNodes() { size_t count; - BNFlowGraphNode** nodes = BNGetFlowGraphNodes(m_graph, &count); + BNFlowGraphNode** nodes = BNGetFlowGraphNodes(m_object, &count); vector> result; result.reserve(count); @@ -201,7 +213,7 @@ vector> FlowGraph::GetNodes() Ref FlowGraph::GetNode(size_t i) { - BNFlowGraphNode* node = BNGetFlowGraphNode(m_graph, i); + BNFlowGraphNode* node = BNGetFlowGraphNode(m_object, i); if (!node) return nullptr; @@ -222,33 +234,33 @@ Ref FlowGraph::GetNode(size_t i) bool FlowGraph::HasNodes() const { - return BNFlowGraphHasNodes(m_graph); + return BNFlowGraphHasNodes(m_object); } size_t FlowGraph::AddNode(FlowGraphNode* node) { m_cachedNodes[node->GetObject()] = node; - return BNAddFlowGraphNode(m_graph, node->GetObject()); + return BNAddFlowGraphNode(m_object, node->GetObject()); } int FlowGraph::GetWidth() const { - return BNGetFlowGraphWidth(m_graph); + return BNGetFlowGraphWidth(m_object); } int FlowGraph::GetHeight() const { - return BNGetFlowGraphHeight(m_graph); + return BNGetFlowGraphHeight(m_object); } vector> FlowGraph::GetNodesInRegion(int left, int top, int right, int bottom) { size_t count; - BNFlowGraphNode** nodes = BNGetFlowGraphNodesInRegion(m_graph, left, top, right, bottom, &count); + BNFlowGraphNode** nodes = BNGetFlowGraphNodesInRegion(m_object, left, top, right, bottom, &count); vector> result; result.reserve(count); @@ -274,25 +286,25 @@ vector> FlowGraph::GetNodesInRegion(int left, int top, int ri bool FlowGraph::IsILGraph() const { - return BNIsILFlowGraph(m_graph); + return BNIsILFlowGraph(m_object); } bool FlowGraph::IsLowLevelILGraph() const { - return BNIsLowLevelILFlowGraph(m_graph); + return BNIsLowLevelILFlowGraph(m_object); } bool FlowGraph::IsMediumLevelILGraph() const { - return BNIsMediumLevelILFlowGraph(m_graph); + return BNIsMediumLevelILFlowGraph(m_object); } Ref FlowGraph::GetLowLevelILFunction() const { - BNLowLevelILFunction* func = BNGetFlowGraphLowLevelILFunction(m_graph); + BNLowLevelILFunction* func = BNGetFlowGraphLowLevelILFunction(m_object); if (!func) return nullptr; return new LowLevelILFunction(func); @@ -301,7 +313,7 @@ Ref FlowGraph::GetLowLevelILFunction() const Ref FlowGraph::GetMediumLevelILFunction() const { - BNMediumLevelILFunction* func = BNGetFlowGraphMediumLevelILFunction(m_graph); + BNMediumLevelILFunction* func = BNGetFlowGraphMediumLevelILFunction(m_object); if (!func) return nullptr; return new MediumLevelILFunction(func); @@ -310,13 +322,13 @@ Ref FlowGraph::GetMediumLevelILFunction() const void FlowGraph::SetLowLevelILFunction(LowLevelILFunction* func) { - BNSetFlowGraphLowLevelILFunction(m_graph, func ? func->GetObject() : nullptr); + BNSetFlowGraphLowLevelILFunction(m_object, func ? func->GetObject() : nullptr); } void FlowGraph::SetMediumLevelILFunction(MediumLevelILFunction* func) { - BNSetFlowGraphMediumLevelILFunction(m_graph, func ? func->GetObject() : nullptr); + BNSetFlowGraphMediumLevelILFunction(m_object, func ? func->GetObject() : nullptr); } @@ -339,7 +351,7 @@ CoreFlowGraph::CoreFlowGraph(BNFlowGraph* graph): FlowGraph(graph) Ref CoreFlowGraph::Update() { - BNFlowGraph* graph = BNUpdateFlowGraph(GetGraphObject()); + BNFlowGraph* graph = BNUpdateFlowGraph(GetObject()); if (!graph) return nullptr; return new CoreFlowGraph(graph); diff --git a/flowgraphnode.cpp b/flowgraphnode.cpp index eea033a8..60912df1 100644 --- a/flowgraphnode.cpp +++ b/flowgraphnode.cpp @@ -26,7 +26,7 @@ using namespace std; FlowGraphNode::FlowGraphNode(FlowGraph* graph) { - m_object = BNCreateFlowGraphNode(graph->GetGraphObject()); + m_object = BNCreateFlowGraphNode(graph->GetObject()); m_cachedLinesValid = false; m_cachedEdgesValid = false; } diff --git a/interaction.cpp b/interaction.cpp index da3942bb..b2e62482 100644 --- a/interaction.cpp +++ b/interaction.cpp @@ -431,9 +431,9 @@ void BinaryNinja::ShowGraphReport(const string& title, FlowGraph* graph) { Ref func = graph->GetFunction(); if (func) - BNShowGraphReport(func->GetView()->GetObject(), title.c_str(), graph->GetGraphObject()); + BNShowGraphReport(func->GetView()->GetObject(), title.c_str(), graph->GetObject()); else - BNShowGraphReport(nullptr, title.c_str(), graph->GetGraphObject()); + BNShowGraphReport(nullptr, title.c_str(), graph->GetObject()); } @@ -691,5 +691,5 @@ void ReportCollection::AddHTMLReport(Ref view, const string& title, void ReportCollection::AddGraphReport(Ref view, const string& title, Ref graph) { - BNAddGraphReportToCollection(m_object, view ? view->GetObject() : nullptr, title.c_str(), graph->GetGraphObject()); + BNAddGraphReportToCollection(m_object, view ? view->GetObject() : nullptr, title.c_str(), graph->GetObject()); } diff --git a/python/flowgraph.py b/python/flowgraph.py index 884d6587..6d1174d6 100644 --- a/python/flowgraph.py +++ b/python/flowgraph.py @@ -263,6 +263,38 @@ class FlowGraphNode(object): core.BNAddFlowGraphNodeOutgoingEdge(self.handle, edge_type, target.handle) +class FlowGraphLayoutRequest(object): + def __init__(self, graph, callback = None): + self.on_complete = callback + self._cb = ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self._complete) + self.handle = core.BNStartFlowGraphLayout(graph.handle, None, self._cb) + + def __del__(self): + self.abort() + core.BNFreeFlowGraphLayoutRequest(self.handle) + + def _complete(self, ctxt): + try: + if self._on_complete is not None: + self._on_complete() + except: + log.log_error(traceback.format_exc()) + + @property + def complete(self): + """Whether flow graph layout is complete (read-only)""" + return core.BNIsFlowGraphLayoutRequestComplete(self.handle) + + @property + def graph(self): + """Flow graph that is being processed (read-only)""" + return CoreFlowGraph(core.BNGetGraphForFlowGraphLayoutRequest(self.handle)) + + def abort(self): + core.BNAbortFlowGraphLayoutRequest(self.handle) + self.on_complete = None + + class FlowGraph(object): def __init__(self, handle = None): if handle is None: @@ -274,12 +306,8 @@ class FlowGraph(object): self._ext_cb.update = self._ext_cb.update.__class__(self._update) handle = core.BNCreateCustomFlowGraph(self._ext_cb) self.handle = handle - self._on_complete = None - self._cb = ctypes.CFUNCTYPE(None, ctypes.c_void_p)(self._complete) def __del__(self): - if self._on_complete is not None: - self.abort() core.BNFreeFlowGraph(self.handle) def __eq__(self, value): @@ -460,15 +488,8 @@ class FlowGraph(object): finally: core.BNFreeFlowGraphNodeList(nodes, count.value) - def _complete(self, ctxt): - try: - if self._on_complete is not None: - self._on_complete() - except: - log.log_error(traceback.format_exc()) - - def layout(self): - core.BNStartFlowGraphLayout(self.handle) + def layout(self, callback = None): + return FlowGraphLayoutRequest(self, callback) def _wait_complete(self): self._wait_cond.acquire() @@ -477,21 +498,13 @@ class FlowGraph(object): def layout_and_wait(self): self._wait_cond = threading.Condition() - self.on_complete(self._wait_complete) - self.layout() + request = self.layout(self._wait_complete) self._wait_cond.acquire() - while not self.complete: + while not request.complete: self._wait_cond.wait() self._wait_cond.release() - def on_complete(self, callback): - self._on_complete = callback - core.BNSetFlowGraphCompleteCallback(self.handle, None, self._cb) - - def abort(self): - core.BNAbortFlowGraph(self.handle) - def get_nodes_in_region(self, left, top, right, bottom): count = ctypes.c_ulonglong() nodes = core.BNGetFlowGraphNodesInRegion(self.handle, left, top, right, bottom, count) diff --git a/python/interaction.py b/python/interaction.py index 0170e6aa..e53312cf 100644 --- a/python/interaction.py +++ b/python/interaction.py @@ -23,7 +23,7 @@ import traceback # Binary Ninja components from binaryninja import _binaryninjacore as core -from binaryninja.enums import FormInputFieldType, MessageBoxIcon, MessageBoxButtonSet, MessageBoxButtonResult +from binaryninja.enums import FormInputFieldType, MessageBoxIcon, MessageBoxButtonSet, MessageBoxButtonResult, ReportType from binaryninja import binaryview from binaryninja import log from binaryninja import flowgraph -- cgit v1.3.1