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
|
//===-- SearcherTest.cpp ----------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define KLEE_UNITTEST
#include "gtest/gtest.h"
#include "Core/ExecutionState.h"
#include "Core/ExecutionTree.h"
#include "Core/Searcher.h"
#include "klee/ADT/RNG.h"
#include "llvm/Support/raw_ostream.h"
using namespace klee;
namespace {
TEST(SearcherTest, RandomPath) {
// First state
ExecutionState es;
InMemoryExecutionTree executionTree(es);
RNG rng;
RandomPathSearcher rp(&executionTree, rng);
EXPECT_TRUE(rp.empty());
rp.update(nullptr, {&es}, {});
EXPECT_FALSE(rp.empty());
EXPECT_EQ(&rp.selectState(), &es);
// Two states
ExecutionState es1(es);
executionTree.attach(es.executionTreeNode, &es1, &es,
BranchType::Conditional);
rp.update(&es, {&es1}, {});
// Random path seed dependant
EXPECT_EQ(&rp.selectState(), &es1);
EXPECT_EQ(&rp.selectState(), &es);
EXPECT_EQ(&rp.selectState(), &es1);
rp.update(&es, {}, {&es1});
for (int i = 0; i < 100; i++) {
EXPECT_EQ(&rp.selectState(), &es);
}
rp.update(&es, {&es1}, {&es});
executionTree.remove(es.executionTreeNode);
for (int i = 0; i < 100; i++) {
EXPECT_EQ(&rp.selectState(), &es1);
}
rp.update(&es1, {}, {&es1});
executionTree.remove(es1.executionTreeNode);
EXPECT_TRUE(rp.empty());
}
TEST(SearcherTest, TwoRandomPath) {
// Root state
ExecutionState root;
InMemoryExecutionTree executionTree(root);
ExecutionState es(root);
executionTree.attach(root.executionTreeNode, &es, &root,
BranchType::Conditional);
RNG rng, rng1;
RandomPathSearcher rp(&executionTree, rng);
RandomPathSearcher rp1(&executionTree, rng1);
EXPECT_TRUE(rp.empty());
EXPECT_TRUE(rp1.empty());
rp.update(nullptr, {&es}, {});
EXPECT_FALSE(rp.empty());
EXPECT_TRUE(rp1.empty());
EXPECT_EQ(&rp.selectState(), &es);
// Two states
ExecutionState es1(es);
executionTree.attach(es.executionTreeNode, &es1, &es,
BranchType::Conditional);
rp.update(&es, {}, {});
rp1.update(nullptr, {&es1}, {});
EXPECT_FALSE(rp1.empty());
for (int i = 0; i < 100; i++) {
EXPECT_EQ(&rp.selectState(), &es);
EXPECT_EQ(&rp1.selectState(), &es1);
}
rp.update(&es, {&es1}, {&es});
rp1.update(nullptr, {&es}, {&es1});
for (int i = 0; i < 100; i++) {
EXPECT_EQ(&rp1.selectState(), &es);
EXPECT_EQ(&rp.selectState(), &es1);
}
rp1.update(&es, {}, {&es});
executionTree.remove(es.executionTreeNode);
EXPECT_TRUE(rp1.empty());
EXPECT_EQ(&rp.selectState(), &es1);
rp.update(&es1, {}, {&es1});
executionTree.remove(es1.executionTreeNode);
EXPECT_TRUE(rp.empty());
EXPECT_TRUE(rp1.empty());
executionTree.remove(root.executionTreeNode);
}
TEST(SearcherTest, TwoRandomPathDot) {
std::stringstream modelExecutionTreeDot;
ExecutionTreeNode *rootExecutionTreeNode, *rightLeafExecutionTreeNode,
*esParentExecutionTreeNode, *es1LeafExecutionTreeNode,
*esLeafExecutionTreeNode;
// Root state
ExecutionState root;
InMemoryExecutionTree executionTree(root);
rootExecutionTreeNode = root.executionTreeNode;
ExecutionState es(root);
executionTree.attach(root.executionTreeNode, &es, &root, BranchType::NONE);
rightLeafExecutionTreeNode = root.executionTreeNode;
esParentExecutionTreeNode = es.executionTreeNode;
RNG rng;
RandomPathSearcher rp(&executionTree, rng);
RandomPathSearcher rp1(&executionTree, rng);
rp.update(nullptr, {&es}, {});
ExecutionState es1(es);
executionTree.attach(es.executionTreeNode, &es1, &es, BranchType::NONE);
esLeafExecutionTreeNode = es.executionTreeNode;
es1LeafExecutionTreeNode = es1.executionTreeNode;
rp.update(&es, {}, {});
rp1.update(nullptr, {&es1}, {});
// Compare ExecutionTree to model ExecutionTree
modelExecutionTreeDot
<< "digraph G {\n"
<< "\tsize=\"10,7.5\";\n"
<< "\tratio=fill;\n"
<< "\trotate=90;\n"
<< "\tcenter = \"true\";\n"
<< "\tnode [style=\"filled\",width=.1,height=.1,fontname=\"Terminus\"]\n"
<< "\tedge [arrowsize=.3]\n"
<< "\tn" << rootExecutionTreeNode << " [shape=diamond];\n"
<< "\tn" << rootExecutionTreeNode << " -> n" << esParentExecutionTreeNode << " [label=0b011];\n"
<< "\tn" << rootExecutionTreeNode << " -> n" << rightLeafExecutionTreeNode << " [label=0b000];\n"
<< "\tn" << rightLeafExecutionTreeNode << " [shape=diamond,fillcolor=green];\n"
<< "\tn" << esParentExecutionTreeNode << " [shape=diamond];\n"
<< "\tn" << esParentExecutionTreeNode << " -> n" << es1LeafExecutionTreeNode << " [label=0b010];\n"
<< "\tn" << esParentExecutionTreeNode << " -> n" << esLeafExecutionTreeNode << " [label=0b001];\n"
<< "\tn" << esLeafExecutionTreeNode << " [shape=diamond,fillcolor=green];\n"
<< "\tn" << es1LeafExecutionTreeNode << " [shape=diamond,fillcolor=green];\n"
<< "}\n";
std::string executionTreeDot;
llvm::raw_string_ostream executionTreeDotStream(executionTreeDot);
executionTree.dump(executionTreeDotStream);
EXPECT_EQ(modelExecutionTreeDot.str(), executionTreeDotStream.str());
rp.update(&es, {&es1}, {&es});
rp1.update(nullptr, {&es}, {&es1});
rp1.update(&es, {}, {&es});
executionTree.remove(es.executionTreeNode);
modelExecutionTreeDot.str("");
modelExecutionTreeDot
<< "digraph G {\n"
<< "\tsize=\"10,7.5\";\n"
<< "\tratio=fill;\n"
<< "\trotate=90;\n"
<< "\tcenter = \"true\";\n"
<< "\tnode [style=\"filled\",width=.1,height=.1,fontname=\"Terminus\"]\n"
<< "\tedge [arrowsize=.3]\n"
<< "\tn" << rootExecutionTreeNode << " [shape=diamond];\n"
<< "\tn" << rootExecutionTreeNode << " -> n" << esParentExecutionTreeNode << " [label=0b001];\n"
<< "\tn" << rootExecutionTreeNode << " -> n" << rightLeafExecutionTreeNode << " [label=0b000];\n"
<< "\tn" << rightLeafExecutionTreeNode << " [shape=diamond,fillcolor=green];\n"
<< "\tn" << esParentExecutionTreeNode << " [shape=diamond];\n"
<< "\tn" << esParentExecutionTreeNode << " -> n" << es1LeafExecutionTreeNode << " [label=0b001];\n"
<< "\tn" << es1LeafExecutionTreeNode << " [shape=diamond,fillcolor=green];\n"
<< "}\n";
executionTreeDot = "";
executionTree.dump(executionTreeDotStream);
EXPECT_EQ(modelExecutionTreeDot.str(), executionTreeDotStream.str());
executionTree.remove(es1.executionTreeNode);
executionTree.remove(root.executionTreeNode);
}
TEST(SearcherDeathTest, TooManyRandomPaths) {
// First state
ExecutionState es;
InMemoryExecutionTree executionTree(es);
executionTree.remove(es.executionTreeNode); // Need to remove to avoid leaks
RNG rng;
RandomPathSearcher rp(&executionTree, rng);
RandomPathSearcher rp1(&executionTree, rng);
RandomPathSearcher rp2(&executionTree, rng);
ASSERT_DEATH({ RandomPathSearcher rp3(&executionTree, rng); }, "");
}
}
|