-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathCFG.cpp
280 lines (258 loc) · 8.96 KB
/
CFG.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//===- CFG.cpp --------------------------------------------------*- C++ -*-===//
//
// Copyright (C) 2021 GrammaTech, Inc.
//
// This code is licensed under the MIT license. See the LICENSE file in the
// project root for license terms.
//
// This project is sponsored by the Office of Naval Research, One Liberty
// Center, 875 N. Randolph Street, Arlington, VA 22203 under contract #
// N68335-17-C-0700. The content of the information does not necessarily
// reflect the position or policy of the Government and no official
// endorsement should be inferred.
//
//===----------------------------------------------------------------------===//
#include "CFG.hpp"
#include "Serialization.hpp"
#include <gtirb/CodeBlock.hpp>
#include <gtirb/proto/CFG.pb.h>
#include <map>
#include <tuple>
namespace gtirb {
GTIRB_EXPORT_API std::ostream& operator<<(std::ostream& OS,
const ConditionalEdge& CE) {
switch (CE) {
case ConditionalEdge::OnFalse:
OS << "OnFalse";
break;
case ConditionalEdge::OnTrue:
OS << "OnTrue";
break;
}
return OS;
}
GTIRB_EXPORT_API std::ostream& operator<<(std::ostream& OS,
const EdgeType& ET) {
switch (ET) {
case EdgeType::Branch:
OS << "Branch";
break;
case EdgeType::Call:
OS << "Call";
break;
case EdgeType::Fallthrough:
OS << "Fallthrough";
break;
case EdgeType::Return:
OS << "Return";
break;
case EdgeType::Syscall:
OS << "Syscall";
break;
case EdgeType::Sysret:
OS << "Sysret";
break;
}
return OS;
}
GTIRB_EXPORT_API std::ostream& operator<<(std::ostream& OS,
const DirectEdge& DE) {
switch (DE) {
case DirectEdge::IsIndirect:
OS << "IsIndirect";
break;
case DirectEdge::IsDirect:
OS << "IsDirect";
break;
}
return OS;
}
GTIRB_EXPORT_API std::ostream& operator<<(std::ostream& OS,
const EdgeLabel& Label) {
if (!Label) {
OS << "<No EdgeLabel>";
} else {
auto ET = std::get<EdgeType>(*Label);
auto DE = std::get<DirectEdge>(*Label);
auto CE = std::get<ConditionalEdge>(*Label);
OS << "(" << CE << ", " << DE << ", " << ET << ")";
}
return OS;
}
std::pair<CFG::vertex_descriptor, bool> addVertex(CfgNode* B, CFG& Cfg) {
auto& IdTable = Cfg[boost::graph_bundle];
if (auto it = IdTable.find(B); it != IdTable.end()) {
return std::make_pair(it->second, false);
}
auto Vertex = add_vertex(Cfg);
Cfg[Vertex] = B;
IdTable[B] = Vertex;
return std::make_pair(Vertex, true);
}
bool removeVertex(CfgNode* N, CFG& Cfg) {
auto& IdTable = Cfg[boost::graph_bundle];
if (auto it = IdTable.find(N); it != IdTable.end()) {
clear_vertex(it->second, Cfg);
remove_vertex(it->second, Cfg);
IdTable.erase(it);
return true;
}
return false;
}
std::optional<CFG::vertex_descriptor> getVertex(const CfgNode* N,
const CFG& Cfg) {
auto& IdTable = Cfg[boost::graph_bundle];
if (auto it = IdTable.find(N); it != IdTable.end()) {
return it->second;
}
return std::nullopt;
}
std::optional<CFG::edge_descriptor> addEdge(const CfgNode* From,
const CfgNode* To, CFG& Cfg) {
const auto& IdTable = Cfg[boost::graph_bundle];
if (auto it = IdTable.find(From); it != IdTable.end()) {
auto FromVertex = it->second;
if (it = IdTable.find(To); it != IdTable.end()) {
auto ToVertex = it->second;
return add_edge(FromVertex, ToVertex, Cfg).first;
}
}
return std::nullopt;
}
bool removeEdge(const CfgNode* From, const CfgNode* To, CFG& Cfg) {
const auto& IdTable = Cfg[boost::graph_bundle];
if (auto it = IdTable.find(From); it != IdTable.end()) {
auto FromVertex = it->second;
if (it = IdTable.find(To); it != IdTable.end()) {
auto ToVertex = it->second;
remove_edge(FromVertex, ToVertex, Cfg);
return true;
}
}
return false;
}
bool removeEdge(const CfgNode* From, const CfgNode* To, const EdgeLabel Label,
CFG& Cfg) {
bool remove_called = true, deleted = false;
const auto& IdTable = Cfg[boost::graph_bundle];
boost::graph_traits<CFG>::out_edge_iterator ei, edge_end;
if (auto it = IdTable.find(From); it != IdTable.end()) {
auto FromVertex = it->second;
if (it = IdTable.find(To); it != IdTable.end()) {
while (remove_called) {
remove_called = false;
for (boost::tie(ei, edge_end) = out_edges(FromVertex, Cfg);
ei != edge_end; ++ei) {
if (Cfg[*ei] == Label) {
remove_edge(ei, Cfg);
// As remove_edge invalidate all iterators
// the iteration process should be restarted
remove_called = true;
deleted = true;
break;
}
}
}
}
}
return deleted;
}
boost::iterator_range<const_cfg_iterator> nodes(const CFG& Cfg) {
auto Vs = vertices(Cfg);
return boost::make_iterator_range(
const_cfg_iterator(cfg_node_iter_base(Cfg, Vs.first)),
const_cfg_iterator(cfg_node_iter_base(Cfg, Vs.second)));
}
boost::iterator_range<cfg_iterator> nodes(CFG& Cfg) {
auto Vs = vertices(Cfg);
return boost::make_iterator_range(
cfg_iterator(cfg_node_iter_base(Cfg, Vs.first)),
cfg_iterator(cfg_node_iter_base(Cfg, Vs.second)));
}
boost::iterator_range<const_block_iterator> blocks(const CFG& Cfg) {
auto Vs = vertices(Cfg);
return boost::make_iterator_range(
const_block_iterator(Cfg, Vs.first, Vs.second),
const_block_iterator(Cfg, Vs.second, Vs.second));
}
boost::iterator_range<block_iterator> blocks(CFG& Cfg) {
auto Vs = vertices(Cfg);
return boost::make_iterator_range(block_iterator(Cfg, Vs.first, Vs.second),
block_iterator(Cfg, Vs.second, Vs.second));
}
proto::CFG toProtobuf(const CFG& Cfg) {
proto::CFG Message;
auto MessageVertices = Message.mutable_vertices();
for (const Node& N : nodes(Cfg)) {
auto* M = MessageVertices->Add();
nodeUUIDToBytes(&N, *M);
}
auto MessageEdges = Message.mutable_edges();
for (const auto& E : boost::make_iterator_range(edges(Cfg))) {
auto M = MessageEdges->Add();
nodeUUIDToBytes(Cfg[source(E, Cfg)], *M->mutable_source_uuid());
nodeUUIDToBytes(Cfg[target(E, Cfg)], *M->mutable_target_uuid());
if (auto Label = Cfg[E]) {
auto* L = M->mutable_label();
L->set_conditional(std::get<ConditionalEdge>(*Label) ==
ConditionalEdge::OnTrue);
L->set_direct(std::get<DirectEdge>(*Label) == DirectEdge::IsDirect);
L->set_type(static_cast<proto::EdgeType>(std::get<EdgeType>(*Label)));
}
}
return Message;
}
bool fromProtobuf(Context& C, CFG& Result, const proto::CFG& Message) {
// Because we're deserializing, we have to assume the data is attacker-
// controlled and may be malicious. We cannot use cast<> because an attacker
// could specify the UUID to a node of the incorrect type. Instead, we use
// dyn_cast<> and assert as needed.
for (const auto& M : Message.vertices()) {
UUID Id;
if (!uuidFromBytes(M, Id))
return false;
auto* N = dyn_cast_or_null<CfgNode>(Node::getByUUID(C, Id));
assert(N && "CFG message contains vertex that is not a CfgNode!");
if (!N)
return false;
addVertex(N, Result);
}
for (const auto& M : Message.edges()) {
UUID Id;
if (!uuidFromBytes(M.source_uuid(), Id))
return false;
CfgNode* Source = dyn_cast_or_null<CfgNode>(Node::getByUUID(C, Id));
if (!uuidFromBytes(M.target_uuid(), Id))
return false;
CfgNode* Target = dyn_cast_or_null<CfgNode>(Node::getByUUID(C, Id));
if (Source && Target) {
if (auto E = addEdge(Source, Target, Result); E && M.has_label()) {
auto& L = M.label();
Result[*E] = std::make_tuple(L.conditional() ? ConditionalEdge::OnTrue
: ConditionalEdge::OnFalse,
L.direct() ? DirectEdge::IsDirect
: DirectEdge::IsIndirect,
static_cast<EdgeType>(L.type()));
}
}
}
return true;
}
// This function is defined here w/ GTIRB_EXPORT_API to provide a
// means for test code to directly invoke serialization routines on a
// CFG. This is a capability not supported for GTIRB clients, but must
// be made available to the testing system.
void GTIRB_EXPORT_API cfgSave(const CFG& Cfg, std::ostream& Out) {
proto::CFG Message = toProtobuf(Cfg);
Message.SerializeToOstream(&Out);
}
// This function is defined here w/ GTIRB_EXPORT_API to provide a
// means for test code to directly invoke serialization routines on a
// CFG. This is a capability not supported for GTIRB clients, but must
// be made available to the testing system.
void GTIRB_EXPORT_API cfgLoad(Context& C, CFG& Result, std::istream& In) {
proto::CFG Message;
Message.ParseFromIstream(&In);
(void)fromProtobuf(C, Result, Message);
}
} // namespace gtirb