klee
Searcher.h
Go to the documentation of this file.
1//===-- Searcher.h ----------------------------------------------*- C++ -*-===//
2//
3// The KLEE Symbolic Virtual Machine
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#ifndef KLEE_SEARCHER_H
11#define KLEE_SEARCHER_H
12
13#include "ExecutionState.h"
14#include "PTree.h"
15#include "klee/ADT/RNG.h"
16#include "klee/System/Time.h"
17
18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/raw_ostream.h"
20
21#include <map>
22#include <queue>
23#include <set>
24#include <vector>
25
26namespace llvm {
27 class BasicBlock;
28 class Function;
29 class Instruction;
30 class raw_ostream;
31}
32
33namespace klee {
34 template<class T, class Comparator> class DiscretePDF;
35 class ExecutionState;
36 class Executor;
37
40 class Searcher {
41 public:
42 virtual ~Searcher() = default;
43
47
52 virtual void update(ExecutionState *current,
53 const std::vector<ExecutionState *> &addedStates,
54 const std::vector<ExecutionState *> &removedStates) = 0;
55
57 virtual bool empty() = 0;
58
60 // TODO: could probably made prettier or more flexible
61 virtual void printName(llvm::raw_ostream &os) = 0;
62
63 enum CoreSearchType : std::uint8_t {
75 };
76 };
77
80 class DFSSearcher final : public Searcher {
81 std::vector<ExecutionState*> states;
82
83 public:
84 ExecutionState &selectState() override;
85 void update(ExecutionState *current,
86 const std::vector<ExecutionState *> &addedStates,
87 const std::vector<ExecutionState *> &removedStates) override;
88 bool empty() override;
89 void printName(llvm::raw_ostream &os) override;
90 };
91
96 class BFSSearcher final : public Searcher {
97 std::deque<ExecutionState*> states;
98
99 public:
100 ExecutionState &selectState() override;
101 void update(ExecutionState *current,
102 const std::vector<ExecutionState *> &addedStates,
103 const std::vector<ExecutionState *> &removedStates) override;
104 bool empty() override;
105 void printName(llvm::raw_ostream &os) override;
106 };
107
109 class RandomSearcher final : public Searcher {
110 std::vector<ExecutionState*> states;
112
113 public:
114 explicit RandomSearcher(RNG &rng);
115 ExecutionState &selectState() override;
116 void update(ExecutionState *current,
117 const std::vector<ExecutionState *> &addedStates,
118 const std::vector<ExecutionState *> &removedStates) override;
119 bool empty() override;
120 void printName(llvm::raw_ostream &os) override;
121 };
122
125 class WeightedRandomSearcher final : public Searcher {
126 public:
127 enum WeightType : std::uint8_t {
135 };
136
137 private:
138 std::unique_ptr<DiscretePDF<ExecutionState*, ExecutionStateIDCompare>> states;
142
143 double getWeight(ExecutionState*);
144
145 public:
149 ~WeightedRandomSearcher() override = default;
150
151 ExecutionState &selectState() override;
152 void update(ExecutionState *current,
153 const std::vector<ExecutionState *> &addedStates,
154 const std::vector<ExecutionState *> &removedStates) override;
155 bool empty() override;
156 void printName(llvm::raw_ostream &os) override;
157 };
158
174 class RandomPathSearcher final : public Searcher {
177
178 // Unique bitmask of this searcher
179 const uint8_t idBitMask;
180
181 public:
185 ~RandomPathSearcher() override = default;
186
187 ExecutionState &selectState() override;
188 void update(ExecutionState *current,
189 const std::vector<ExecutionState *> &addedStates,
190 const std::vector<ExecutionState *> &removedStates) override;
191 bool empty() override;
192 void printName(llvm::raw_ostream &os) override;
193 };
194
195
196 extern llvm::cl::opt<bool> UseIncompleteMerge;
197 class MergeHandler;
198 class MergingSearcher final : public Searcher {
199 friend class MergeHandler;
200
201 private:
202
203 std::unique_ptr<Searcher> baseSearcher;
204
206 std::vector<ExecutionState*> pausedStates;
207
208 public:
211 ~MergingSearcher() override = default;
212
215 std::set<ExecutionState *> inCloseMerge;
216
222 std::vector<MergeHandler *> mergeGroups;
223
227 void pauseState(ExecutionState &state);
228
230 void continueState(ExecutionState &state);
231
232 ExecutionState &selectState() override;
233 void update(ExecutionState *current,
234 const std::vector<ExecutionState *> &addedStates,
235 const std::vector<ExecutionState *> &removedStates) override;
236
237 bool empty() override;
238 void printName(llvm::raw_ostream &os) override;
239 };
240
244 class BatchingSearcher final : public Searcher {
245 std::unique_ptr<Searcher> baseSearcher;
248
252
253 public:
258 ~BatchingSearcher() override = default;
259
260 ExecutionState &selectState() override;
261 void update(ExecutionState *current,
262 const std::vector<ExecutionState *> &addedStates,
263 const std::vector<ExecutionState *> &removedStates) override;
264 bool empty() override;
265 void printName(llvm::raw_ostream &os) override;
266 };
267
274 std::unique_ptr<Searcher> baseSearcher;
277 std::set<ExecutionState*> pausedStates;
278
279 public:
283
284 ExecutionState &selectState() override;
285 void update(ExecutionState *current,
286 const std::vector<ExecutionState *> &addedStates,
287 const std::vector<ExecutionState *> &removedStates) override;
288 bool empty() override;
289 void printName(llvm::raw_ostream &os) override;
290 };
291
295 class InterleavedSearcher final : public Searcher {
296 std::vector<std::unique_ptr<Searcher>> searchers;
297 unsigned index {1};
298
299 public:
301 explicit InterleavedSearcher(const std::vector<Searcher *> &searchers);
302 ~InterleavedSearcher() override = default;
303
304 ExecutionState &selectState() override;
305 void update(ExecutionState *current,
306 const std::vector<ExecutionState *> &addedStates,
307 const std::vector<ExecutionState *> &removedStates) override;
308 bool empty() override;
309 void printName(llvm::raw_ostream &os) override;
310 };
311
312} // klee namespace
313
314#endif /* KLEE_SEARCHER_H */
bool empty() override
Definition: Searcher.cpp:108
std::deque< ExecutionState * > states
Definition: Searcher.h:97
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:112
ExecutionState & selectState() override
Definition: Searcher.cpp:75
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:79
ExecutionState & selectState() override
Definition: Searcher.cpp:416
std::unique_ptr< Searcher > baseSearcher
Definition: Searcher.h:245
unsigned lastStartInstructions
Definition: Searcher.h:251
BatchingSearcher(Searcher *baseSearcher, time::Span timeBudget, unsigned instructionBudget)
Definition: Searcher.cpp:411
ExecutionState * lastState
Definition: Searcher.h:249
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:440
bool empty() override
Definition: Searcher.cpp:450
time::Span timeBudget
Definition: Searcher.h:246
unsigned instructionBudget
Definition: Searcher.h:247
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:454
time::Point lastStartTime
Definition: Searcher.h:250
~BatchingSearcher() override=default
std::vector< ExecutionState * > states
Definition: Searcher.h:81
bool empty() override
Definition: Searcher.cpp:64
ExecutionState & selectState() override
Definition: Searcher.cpp:42
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:68
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:46
ExecutionState representing a path under exploration.
ExecutionState & selectState() override
Definition: Searcher.cpp:530
std::vector< std::unique_ptr< Searcher > > searchers
Definition: Searcher.h:296
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:549
~InterleavedSearcher() override=default
bool empty() override
Definition: Searcher.cpp:545
InterleavedSearcher(const std::vector< Searcher * > &searchers)
Definition: Searcher.cpp:524
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:536
std::unique_ptr< Searcher > baseSearcher
Definition: Searcher.h:274
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:517
std::set< ExecutionState * > pausedStates
Definition: Searcher.h:277
ExecutionState & selectState() override
Definition: Searcher.cpp:468
IterativeDeepeningTimeSearcher(Searcher *baseSearcher)
Definition: Searcher.cpp:465
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:474
~IterativeDeepeningTimeSearcher() override=default
Represents one klee_open_merge() call. Handles merging of states that branched from it.
Definition: MergeHandler.h:96
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:390
std::unique_ptr< Searcher > baseSearcher
Definition: Searcher.h:203
ExecutionState & selectState() override
Definition: Searcher.cpp:361
std::set< ExecutionState * > inCloseMerge
Definition: Searcher.h:215
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:404
MergingSearcher(Searcher *baseSearcher)
Definition: Searcher.cpp:345
std::vector< MergeHandler * > mergeGroups
Definition: Searcher.h:222
void continueState(ExecutionState &state)
Continue a paused state.
Definition: Searcher.cpp:354
std::vector< ExecutionState * > pausedStates
States that have been paused by the 'pauseState' function.
Definition: Searcher.h:206
bool empty() override
Definition: Searcher.cpp:400
~MergingSearcher() override=default
void pauseState(ExecutionState &state)
Definition: Searcher.cpp:348
Definition: RNG.h:14
ExecutionState & selectState() override
Definition: Searcher.cpp:265
~RandomPathSearcher() override=default
RandomPathSearcher(PTree &processTree, RNG &rng)
Definition: Searcher.cpp:260
const uint8_t idBitMask
Definition: Searcher.h:179
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:338
bool empty() override
Definition: Searcher.cpp:334
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:291
RandomSearcher picks a state randomly.
Definition: Searcher.h:109
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:125
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:143
bool empty() override
Definition: Searcher.cpp:139
RandomSearcher(RNG &rng)
Definition: Searcher.cpp:119
std::vector< ExecutionState * > states
Definition: Searcher.h:110
ExecutionState & selectState() override
Definition: Searcher.cpp:121
virtual void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates)=0
virtual ~Searcher()=default
virtual void printName(llvm::raw_ostream &os)=0
Prints name of searcher as a klee_message().
virtual ExecutionState & selectState()=0
virtual bool empty()=0
void update(ExecutionState *current, const std::vector< ExecutionState * > &addedStates, const std::vector< ExecutionState * > &removedStates) override
Definition: Searcher.cpp:217
ExecutionState & selectState() override
Definition: Searcher.cpp:172
~WeightedRandomSearcher() override=default
WeightedRandomSearcher(WeightType type, RNG &rng)
Definition: Searcher.cpp:150
void printName(llvm::raw_ostream &os) override
Prints name of searcher as a klee_message().
Definition: Searcher.cpp:239
std::unique_ptr< DiscretePDF< ExecutionState *, ExecutionStateIDCompare > > states
Definition: Searcher.h:138
double getWeight(ExecutionState *)
Definition: Searcher.cpp:176
Span seconds(std::uint64_t)
Definition: Time.cpp:139
Definition: main.cpp:291
llvm::cl::opt< bool > UseIncompleteMerge