forked from Vector35/binaryninja-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flowgraphnode.cpp
221 lines (174 loc) · 5.93 KB
/
flowgraphnode.cpp
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
213
214
215
216
217
218
219
220
221
// Copyright (c) 2015-2022 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;
FlowGraphNode::FlowGraphNode(FlowGraph* graph)
{
m_object = BNCreateFlowGraphNode(graph->GetObject());
m_cachedLinesValid = false;
m_cachedEdgesValid = false;
m_cachedIncomingEdgesValid = false;
}
FlowGraphNode::FlowGraphNode(BNFlowGraphNode* node)
{
m_object = node;
m_cachedLinesValid = false;
m_cachedEdgesValid = false;
m_cachedIncomingEdgesValid = false;
}
Ref<FlowGraph> FlowGraphNode::GetGraph() const
{
BNFlowGraph* graph = BNGetFlowGraphNodeOwner(m_object);
if (!graph)
return nullptr;
return new CoreFlowGraph(graph);
}
Ref<BasicBlock> FlowGraphNode::GetBasicBlock() const
{
BNBasicBlock* block = BNGetFlowGraphBasicBlock(m_object);
if (!block)
return nullptr;
return new BasicBlock(block);
}
void FlowGraphNode::SetBasicBlock(BasicBlock* block)
{
BNSetFlowGraphBasicBlock(m_object, block ? block->GetObject() : nullptr);
}
int FlowGraphNode::GetX() const
{
return BNGetFlowGraphNodeX(m_object);
}
int FlowGraphNode::GetY() const
{
return BNGetFlowGraphNodeY(m_object);
}
int FlowGraphNode::GetWidth() const
{
return BNGetFlowGraphNodeWidth(m_object);
}
int FlowGraphNode::GetHeight() const
{
return BNGetFlowGraphNodeHeight(m_object);
}
const vector<DisassemblyTextLine>& FlowGraphNode::GetLines()
{
if (m_cachedLinesValid)
return m_cachedLines;
size_t count;
BNDisassemblyTextLine* lines = BNGetFlowGraphNodeLines(m_object, &count);
vector<DisassemblyTextLine> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
DisassemblyTextLine line;
line.addr = lines[i].addr;
line.instrIndex = lines[i].instrIndex;
line.highlight = lines[i].highlight;
line.tokens = InstructionTextToken::ConvertInstructionTextTokenList(lines[i].tokens, lines[i].count);
line.tags = Tag::ConvertTagList(lines[i].tags, lines[i].tagCount);
result.push_back(line);
}
BNFreeDisassemblyTextLines(lines, count);
m_cachedLines = result;
return m_cachedLines;
}
void FlowGraphNode::SetLines(const vector<DisassemblyTextLine>& lines)
{
BNDisassemblyTextLine* buf = new BNDisassemblyTextLine[lines.size()];
for (size_t i = 0; i < lines.size(); i++)
{
buf[i].addr = lines[i].addr;
buf[i].instrIndex = lines[i].instrIndex;
buf[i].highlight = lines[i].highlight;
buf[i].tokens = InstructionTextToken::CreateInstructionTextTokenList(lines[i].tokens);
buf[i].count = lines[i].tokens.size();
buf[i].tags = Tag::CreateTagList(lines[i].tags, &(buf[i].tagCount));
}
BNSetFlowGraphNodeLines(m_object, buf, lines.size());
BNFreeDisassemblyTextLines(buf, lines.size());
m_cachedLines = lines;
m_cachedLinesValid = true;
}
const vector<FlowGraphEdge>& FlowGraphNode::GetOutgoingEdges()
{
if (m_cachedEdgesValid)
return m_cachedEdges;
size_t count;
BNFlowGraphEdge* edges = BNGetFlowGraphNodeOutgoingEdges(m_object, &count);
vector<FlowGraphEdge> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
FlowGraphEdge edge;
edge.type = edges[i].type;
edge.target = edges[i].target ? new FlowGraphNode(BNNewFlowGraphNodeReference(edges[i].target)) : nullptr;
edge.points.insert(edge.points.begin(), &edges[i].points[0], &edges[i].points[edges[i].pointCount]);
edge.backEdge = edges[i].backEdge;
edge.style.color = edges[i].style.color;
edge.style.width = edges[i].style.width;
edge.style.style = edges[i].style.style;
result.push_back(edge);
}
BNFreeFlowGraphNodeEdgeList(edges, count);
m_cachedEdges = result;
m_cachedEdgesValid = true;
return m_cachedEdges;
}
const vector<FlowGraphEdge>& FlowGraphNode::GetIncomingEdges()
{
if (m_cachedIncomingEdgesValid)
return m_cachedIncomingEdges;
size_t count;
BNFlowGraphEdge* edges = BNGetFlowGraphNodeIncomingEdges(m_object, &count);
vector<FlowGraphEdge> result;
result.reserve(count);
for (size_t i = 0; i < count; i++)
{
FlowGraphEdge edge;
edge.type = edges[i].type;
edge.target = edges[i].target ? new FlowGraphNode(BNNewFlowGraphNodeReference(edges[i].target)) : nullptr;
edge.points.insert(edge.points.begin(), &edges[i].points[0], &edges[i].points[edges[i].pointCount]);
edge.backEdge = edges[i].backEdge;
result.push_back(edge);
}
BNFreeFlowGraphNodeEdgeList(edges, count);
m_cachedIncomingEdges = result;
m_cachedIncomingEdgesValid = true;
return m_cachedIncomingEdges;
}
void FlowGraphNode::AddOutgoingEdge(BNBranchType type, FlowGraphNode* target, BNEdgeStyle edgeStyle)
{
BNAddFlowGraphNodeOutgoingEdge(m_object, type, target->GetObject(), edgeStyle);
m_cachedEdges.clear();
m_cachedEdgesValid = false;
}
BNHighlightColor FlowGraphNode::GetHighlight() const
{
return BNGetFlowGraphNodeHighlight(m_object);
}
void FlowGraphNode::SetHighlight(const BNHighlightColor& color)
{
BNSetFlowGraphNodeHighlight(m_object, color);
}
bool FlowGraphNode::IsValidForGraph(FlowGraph* graph) const
{
return BNIsNodeValidForFlowGraph(graph->GetObject(), m_object);
}