forked from googleprojectzero/functionsimsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowgraph.cpp
244 lines (214 loc) · 7.9 KB
/
flowgraph.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
// Copyright 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 3.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <fstream>
#include <map>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#include "flowgraphutil.hpp"
#include "flowgraph.hpp"
Flowgraph::Flowgraph() {}
// Automatically default-constructs the target vector.
bool Flowgraph::AddNode(address node_address) {
return out_edges_[node_address].empty();
}
bool Flowgraph::AddEdge(address source_address, address target_address) {
AddNode(source_address);
AddNode(target_address);
out_edges_[source_address].push_back(target_address);
in_edges_[target_address].push_back(source_address);
bidirectional_edges_[source_address].push_back(target_address);
bidirectional_edges_[target_address].push_back(source_address);
return true;
}
void Flowgraph::GetNodes(std::vector<address>* nodes) const {
for (const auto& entry : out_edges_) {
nodes->push_back(entry.first);
}
}
void Flowgraph::WriteDot(const std::string& output_file) {
std::ofstream dotfile;
dotfile.open(output_file);
dotfile << "digraph G {\n";
for (const auto& edges : out_edges_) {
address source = edges.first;
for (const address& target : edges.second) {
dotfile << "\tblk_" << std::hex << source << " -> blk_" << target << ";\n";
}
}
dotfile << "}\n";
}
bool Flowgraph::HasNode(address node) {
return (out_edges_.find(node) != out_edges_.end());
}
void Flowgraph::GetTopologicalOrder(std::map<address, std::vector<address>>*
edges, address startnode, std::map<address, int32_t>* order) {
// Begin by calculating the topological order of each node.
std::queue<std::pair<address, uint32_t>> worklist;
worklist.push(std::make_pair(startnode, 0));
(*order)[startnode] = 0;
address current_node;
uint32_t current_order;
while (!worklist.empty()) {
std::tie(current_node, current_order) = worklist.front();
worklist.pop();
std::vector<address>& targets = (*edges)[current_node];
for (const address& target : targets) {
if (order->find(target) != order->end()) {
uint32_t target_order = (*order)[target];
if (target_order <= current_order + 1) {
continue;
}
}
// Found a shorter path to this node, reprocess it.
worklist.push(std::make_pair(target, current_order + 1));
(*order)[target] = current_order + 1;
}
}
for (const auto& entry : (*edges)) {
if (order->find(entry.first) == order->end()) {
(*order)[entry.first] = -1;
}
}
}
// The hash is calculated as follows:
// - Each edge in the graph is associated with a tuple of numbers:
// - Topological order forward of source
// - Topological order backward of source
// - Topological order bidirectional of source
// - indegree of source
// - outdegree of source
// - Same for target of the edge
// The result is a set of ten-tuples. Each element in the tuple is multiplied
// with a constant and added into the hash value, which is then rotated.
//
uint64_t Flowgraph::CalculateHash(address startnode,
uint64_t k0, uint64_t k1, uint64_t k2) {
std::map<address, int32_t> order_forward, order_backward, order_both;
GetTopologicalOrder(&out_edges_, startnode, &order_forward);
GetTopologicalOrder(&in_edges_, startnode, &order_backward);
GetTopologicalOrder(&bidirectional_edges_, startnode, &order_both);
std::map<address, uint32_t> indegrees, outdegrees;
for (const auto& element : out_edges_) {
outdegrees[element.first] = element.second.size();
for (const address target : element.second) {
indegrees[target]++;
}
}
// Arbitrarily chosen seed value;
uint64_t hash_result = 0x0BADDEED600DDEEDL;
for (const auto& element : out_edges_) {
address source = element.first;
uint64_t per_edge_hash = 0x600DDEED0BADDEEDL;
for (const address target : element.second) {
per_edge_hash += k0 * order_forward[source];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k1 * order_backward[source];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k2 * order_both[source];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k0 * indegrees[source];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k1 * outdegrees[source];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k2 * order_forward[target];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k0 * order_backward[target];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k1 * order_both[target];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k2 * indegrees[target];
per_edge_hash = rotl64( per_edge_hash, 7 );
per_edge_hash += k0 * outdegrees[target];
per_edge_hash = rotl64( per_edge_hash, 7 );
}
// Combine the per-edge-hashes with a commutative operation.
hash_result += per_edge_hash;
}
return hash_result;
}
Flowgraph* Flowgraph::GetSubgraph(address node, uint32_t distance, uint32_t
max_size = 0xFFFFFFFF) {
Flowgraph* subgraph = new Flowgraph();
// This code proceeds in two iterations: It first identifies all nodes within
// the specified distance and adds them o the new graph. It then performs a
// iteration where it adds all the edges that fall within the subgraph.
// FIFO queue so we get BFS iteration.
std::queue<std::pair<address, uint32_t>> worklist;
std::set<address> visited;
worklist.push(std::make_pair(node, 0));
visited.insert(node);
subgraph->AddNode(node);
address current_node;
uint32_t current_distance;
while (!worklist.empty()) {
std::tie(current_node, current_distance) = worklist.front();
worklist.pop();
if (current_distance < distance) {
std::vector<address>* in = &(in_edges_[current_node]);
std::vector<address>* out = &(out_edges_[current_node]);
std::vector<std::vector<address>*> in_and_out = { in, out };
for (std::vector<address>* edges : in_and_out) {
for (const address& target : *edges) {
if (visited.find(target) == visited.end()) {
visited.insert(target);
subgraph->AddNode(target);
worklist.push(std::make_pair(target, current_distance + 1));
if (subgraph->GetSize() > max_size) {
delete subgraph;
return nullptr;
}
}
}
}
}
}
// We should have all nodes within the right distance.
std::vector<address> nodes;
subgraph->GetNodes(&nodes);
for (const address& node : nodes) {
// Get the outgoing edges for this node.
for (const address& target : *(GetOutEdges(node))) {
if (subgraph->HasNode(target)) {
subgraph->AddEdge(node, target);
}
}
}
return subgraph;
}
const std::vector<address>* Flowgraph::GetOutEdges(address node) {
auto iter = out_edges_.find(node);
if (iter != out_edges_.end()) {
return &(iter->second);
}
return nullptr;
}
const std::vector<address>* Flowgraph::GetInEdges(address node) {
auto iter = in_edges_.find(node);
if (iter != in_edges_.end()) {
return &(iter->second);
}
}
// Returns the number of nodes that have more than one successor in the graph.
// Required to measure graph complexity to decide what to index.
uint64_t Flowgraph::GetNumberOfBranchingNodes() const {
uint64_t result = 0;
for (const auto& edge : out_edges_) {
if (edge.second.size() > 1) {
++result;
}
}
return result;
}