-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBNFlowGraphCustom.cs
More file actions
212 lines (186 loc) · 4.91 KB
/
Copy pathBNFlowGraphCustom.cs
File metadata and controls
212 lines (186 loc) · 4.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
namespace BinaryNinja
{
public partial class FlowGraph
{
private static readonly object externalReferenceLock = new object();
private static readonly List<FlowGraph> externallyReferencedGraphs =
new List<FlowGraph>();
private NativeDelegates.BNCustomFlowGraphEvent? prepareForLayoutCallback;
private NativeDelegates.BNCustomFlowGraphEvent? populateNodesCallback;
private NativeDelegates.BNCustomFlowGraphEvent? completeLayoutCallback;
private NativeDelegates.BNCustomFlowGraphUpdate? updateCallback;
private NativeDelegates.BNCustomFlowGraphEvent? externalReferenceTakenCallback;
private NativeDelegates.BNCustomFlowGraphEvent? externalReferenceReleasedCallback;
/// <summary>Prepares this graph before the layout worker requests nodes.</summary>
protected virtual void PrepareForLayout()
{
this.FinishPrepareForLayout();
}
/// <summary>Populates this graph on the layout worker thread.</summary>
protected virtual void PopulateNodes()
{
}
/// <summary>Runs after the core completes graph layout.</summary>
protected virtual void CompleteLayout()
{
}
private void InitializeCustomGraph()
{
this.prepareForLayoutCallback = new NativeDelegates.BNCustomFlowGraphEvent(
this.InvokePrepareForLayout
);
this.populateNodesCallback = new NativeDelegates.BNCustomFlowGraphEvent(
this.InvokePopulateNodes
);
this.completeLayoutCallback = new NativeDelegates.BNCustomFlowGraphEvent(
this.InvokeCompleteLayout
);
this.updateCallback = new NativeDelegates.BNCustomFlowGraphUpdate(
this.InvokeUpdate
);
this.externalReferenceTakenCallback =
new NativeDelegates.BNCustomFlowGraphEvent(
this.InvokeExternalReferenceTaken
);
this.externalReferenceReleasedCallback =
new NativeDelegates.BNCustomFlowGraphEvent(
this.InvokeExternalReferenceReleased
);
BNCustomFlowGraph callbacks = new BNCustomFlowGraph();
callbacks.context = IntPtr.Zero;
callbacks.prepareForLayout = Marshal.GetFunctionPointerForDelegate(
this.prepareForLayoutCallback
);
callbacks.populateNodes = Marshal.GetFunctionPointerForDelegate(
this.populateNodesCallback
);
callbacks.completeLayout = Marshal.GetFunctionPointerForDelegate(
this.completeLayoutCallback
);
callbacks.update = Marshal.GetFunctionPointerForDelegate(
this.updateCallback
);
callbacks.freeObject = IntPtr.Zero;
callbacks.externalRefTaken = Marshal.GetFunctionPointerForDelegate(
this.externalReferenceTakenCallback
);
callbacks.externalRefReleased = Marshal.GetFunctionPointerForDelegate(
this.externalReferenceReleasedCallback
);
IntPtr handle = NativeMethods.BNCreateCustomFlowGraph(in callbacks);
if (IntPtr.Zero == handle)
{
throw new InvalidOperationException(
"The core rejected the custom flow graph."
);
}
this.SetHandle(handle);
}
private void InvokePrepareForLayout(IntPtr context)
{
try
{
this.PrepareForLayout();
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in FlowGraph.PrepareForLayout: {0}",
exception
);
this.FinishPrepareForLayout();
}
}
private void InvokePopulateNodes(IntPtr context)
{
try
{
this.PopulateNodes();
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in FlowGraph.PopulateNodes: {0}",
exception
);
}
}
private void InvokeCompleteLayout(IntPtr context)
{
try
{
this.CompleteLayout();
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in FlowGraph.CompleteLayout: {0}",
exception
);
}
}
private IntPtr InvokeUpdate(IntPtr context)
{
try
{
FlowGraph? graph = this.Update();
if (null == graph)
{
return IntPtr.Zero;
}
return NativeMethods.BNNewFlowGraphReference(
graph.DangerousGetHandle()
);
}
catch (Exception exception)
{
Core.LogError(
"Unhandled exception in FlowGraph.Update: {0}",
exception
);
return IntPtr.Zero;
}
}
private void InvokeExternalReferenceTaken(IntPtr context)
{
lock (FlowGraph.externalReferenceLock)
{
FlowGraph.externallyReferencedGraphs.Add(this);
}
}
private void InvokeExternalReferenceReleased(IntPtr context)
{
lock (FlowGraph.externalReferenceLock)
{
FlowGraph.externallyReferencedGraphs.Remove(this);
}
}
private sealed class CoreFlowGraph : FlowGraph
{
internal CoreFlowGraph(IntPtr handle, bool owner)
: base(handle, owner)
{
}
public override bool HasUpdates
{
get
{
if (!this.UpdateQueryMode)
{
return false;
}
return NativeMethods.BNFlowGraphHasUpdates(this.handle);
}
}
public override FlowGraph? Update()
{
return FlowGraph.TakeHandle(
NativeMethods.BNUpdateFlowGraph(this.handle)
);
}
}
}
}