forked from google/binexport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flow_analyzer.cc
70 lines (61 loc) · 2.45 KB
/
flow_analyzer.cc
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
// Copyright 2011-2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.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 "third_party/zynamics/binexport/flow_analyzer.h"
#include <exception>
#include <iostream>
#include <map>
#include <set>
#include <stdexcept>
#include <string>
#include "base/logging.h"
#include "third_party/zynamics/binexport/call_graph.h"
#include "third_party/zynamics/binexport/flow_graph.h"
#include "third_party/zynamics/binexport/virtual_memory.h"
namespace {
void UpdateInstructionInDegree(Instructions* instructions,
const FlowGraph& flow_graph) {
// Increase in-degree count of normal flow instructions.
for (auto i = instructions->begin(); i != instructions->end(); ++i) {
auto j = GetNextInstruction(instructions, i);
// If the instruction flows then add an edge to the following instruction.
if (j != instructions->end()) {
j->AddInEdge();
}
}
// Increase in-degree count of jmp targets.
for (const auto& edge : flow_graph.GetEdges()) {
auto j = GetInstruction(instructions, edge.target);
if (j != instructions->end()) {
j->AddInEdge();
} else {
// TODO(user) When does this happen? If we have hit a stop block
// instruction? Otherwise all target instructions should be valid and in
// here...
}
}
}
} // namespace
void ReconstructFlowGraph(Instructions* instructions,
const FlowGraph& flow_graph,
CallGraph* call_graph) {
UpdateInstructionInDegree(instructions, flow_graph);
// Add all instructions with an in-degree count of 0 as function entry points.
for (const auto& instruction : *instructions) {
// The IsPossibleFunction check should only be necessary for IDA
// and shouldn't be required anyways if we have a valid instruction...
if (!instruction.HasFlag(FLAG_INVALID) && instruction.GetInDegree() == 0) {
call_graph->AddFunction(instruction.GetAddress());
}
}
}