-
Notifications
You must be signed in to change notification settings - Fork 144
/
call_graph_test.cc
160 lines (133 loc) · 6.08 KB
/
call_graph_test.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
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
// Copyright 2011-2024 Google LLC
//
// 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
//
// https://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/bindiff/call_graph.h"
#include <boost/graph/compressed_sparse_row_graph.hpp> // NOLINT
#include <limits>
#include <memory>
#include <stdexcept>
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "third_party/absl/memory/memory.h"
#include "third_party/zynamics/bindiff/test_util.h"
namespace security::bindiff {
namespace {
using ::testing::DoubleNear;
using ::testing::Eq;
using ::testing::IsEmpty;
using ::testing::IsTrue;
using ::testing::Ne;
using ::testing::StrEq;
TEST(EmptyCallGraphTest, Construction) {
CallGraph call_graph; // Empty
// Verify metadata
EXPECT_THAT(call_graph.GetFilename(), IsEmpty());
EXPECT_THAT(call_graph.GetFilePath(), IsEmpty());
EXPECT_THAT(call_graph.GetExeFilename(), IsEmpty());
EXPECT_THAT(call_graph.GetExeHash(), IsEmpty());
// No vertices, no edges
EXPECT_THAT(boost::num_vertices(call_graph.GetGraph()), Eq(0));
EXPECT_THAT(boost::num_edges(call_graph.GetGraph()), Eq(0));
EXPECT_THAT(call_graph.GetVertex(0), Eq(CallGraph::kInvalidVertex));
EXPECT_THAT(call_graph.GetVertex(std::numeric_limits<Address>::max()),
Eq(CallGraph::kInvalidVertex));
// Empty graph's MD index should be exactly zero
call_graph.CalculateTopology();
EXPECT_THAT(call_graph.GetMdIndex(), Eq(0.0));
call_graph.SetMdIndex(47.0);
EXPECT_THAT(call_graph.GetMdIndex(), Eq(47.0));
EXPECT_THAT(call_graph.GetComments(), IsEmpty());
}
TEST(EmptyCallGraphTest, AddOrRemoveNullFlowGraphThrows) {
CallGraph call_graph; // Empty
EXPECT_THROW(call_graph.AttachFlowGraph(nullptr), std::runtime_error);
EXPECT_THROW(call_graph.DetachFlowGraph(nullptr), std::runtime_error);
}
TEST(EmptyCallGraphDeathTest, QueryingVerticesCrashes) {
CallGraph call_graph; // Empty
// These should fail in all builds
// TODO(cblichmann): Implement bound checks in debug mode.
EXPECT_DEATH_IF_SUPPORTED(call_graph.GetAddress(CallGraph::kInvalidVertex),
"");
EXPECT_DEATH_IF_SUPPORTED(call_graph.GetMdIndex(CallGraph::kInvalidVertex),
"");
EXPECT_DEATH_IF_SUPPORTED(
call_graph.GetMdIndexInverted(CallGraph::kInvalidVertex), "");
}
TEST(EmptyCallGraphTest, CrossPlatformFileBasenames) {
class CallGraphForTesting : public CallGraph {
public:
void set_filename(std::string value) { filename_ = std::move(value); }
} call_graph; // Empty
// Plain filename
call_graph.set_filename("primary.v1.test.exe");
EXPECT_THAT(call_graph.GetFilename(), StrEq("primary.v1.test"));
// Windows style
call_graph.set_filename(R"(C:\TEMP\RE.project\primary.v1.test.exe)");
EXPECT_THAT(call_graph.GetFilename(), StrEq("primary.v1.test"));
// Posix style
call_graph.set_filename(R"(/tmp/RE.project/primary.v1.test.exe)");
EXPECT_THAT(call_graph.GetFilename(), StrEq("primary.v1.test"));
}
class SimpleCallGraphTest : public ::testing::Test {
protected:
SimpleCallGraphTest()
: binary_(DiffBinaryBuilder()
.AddFunctions(
{FunctionBuilder(0x10000, "func_a")
.AddBasicBlocks(
{BasicBlockBuilder("entry")
.AddInstructions(
{InstructionBuilder("test eax, eax"),
InstructionBuilder("jz loc_10002")})
.SetFlowTrue("loc_10002")
.SetFlowFalse("loc_10004"),
BasicBlockBuilder("loc_10002")
.AddInstructions(
{InstructionBuilder("mov eax, 1"),
InstructionBuilder("jmp loc_10005")})
.SetFlow("loc_10005"),
BasicBlockBuilder("loc_10004")
.AddInstructions(
{InstructionBuilder("xor eax, eax")})
.SetFlow("loc_10005"),
BasicBlockBuilder("loc_10005")
.AddInstructions(
{InstructionBuilder("ret")})}),
FunctionBuilder(0x20000, "func_b")
.AddBasicBlocks(
{BasicBlockBuilder("entry").AddInstructions(
{InstructionBuilder("call func_a")
.SetCallsFunction("func_a"),
InstructionBuilder("ret")})})})
.Build(&cache_)),
call_graph_(binary_->call_graph) {}
Instruction::Cache cache_;
std::unique_ptr<DiffBinary> binary_;
CallGraph& call_graph_; // Convenient access to binary_.call_graph
};
TEST_F(SimpleCallGraphTest, Construction) {
EXPECT_THAT(boost::num_vertices(call_graph_.GetGraph()), Eq(2));
EXPECT_THAT(boost::num_edges(call_graph_.GetGraph()), Eq(1));
}
TEST_F(SimpleCallGraphTest, TopologyValid) {
call_graph_.CalculateTopology();
EXPECT_THAT(call_graph_.GetMdIndex(), DoubleNear(0.132036, 0.000001));
const auto func_a = call_graph_.GetVertex(0x10000);
ASSERT_THAT(func_a, Ne(CallGraph::kInvalidVertex));
const auto func_b = call_graph_.GetVertex(0x20000);
ASSERT_THAT(func_b, Ne(CallGraph::kInvalidVertex));
EXPECT_THAT(call_graph_.FindEdge(func_b, func_a).second, IsTrue());
}
} // namespace
} // namespace security::bindiff