klee
MergeHandler.cpp
Go to the documentation of this file.
1//===-- MergeHandler.cpp --------------------------------------------------===//
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#include "MergeHandler.h"
11
12#include "CoreStats.h"
13#include "ExecutionState.h"
14#include "Executor.h"
15#include "Searcher.h"
16
17namespace klee {
18
19/*** Test generation options ***/
20
21llvm::cl::OptionCategory MergeCat("Path merging options",
22 "These options control path merging.");
23
24llvm::cl::opt<bool> UseMerge(
25 "use-merge", llvm::cl::init(false),
26 llvm::cl::desc("Enable support for path merging via klee_open_merge() and "
27 "klee_close_merge() (default=false)"),
28 llvm::cl::cat(klee::MergeCat));
29
30llvm::cl::opt<bool> DebugLogMerge(
31 "debug-log-merge", llvm::cl::init(false),
32 llvm::cl::desc("Debug information for path merging (default=false)"),
33 llvm::cl::cat(klee::MergeCat));
34
35llvm::cl::opt<bool> UseIncompleteMerge(
36 "use-incomplete-merge", llvm::cl::init(false),
37 llvm::cl::desc("Heuristic-based path merging (default=false)"),
38 llvm::cl::cat(klee::MergeCat));
39
40llvm::cl::opt<bool> DebugLogIncompleteMerge(
41 "debug-log-incomplete-merge", llvm::cl::init(false),
42 llvm::cl::desc("Debug information for incomplete path merging (default=false)"),
43 llvm::cl::cat(klee::MergeCat));
44
46 if (closedStateCount == 0)
47 return 0;
48
49 return closedMean;
50}
51
54}
55
57 for (ExecutionState *cur_state : openStates) {
58 bool stateIsClosed =
59 (executor->mergingSearcher->inCloseMerge.find(cur_state) !=
61
62 if (!stateIsClosed && (getInstructionDistance(cur_state) < 2 * getMean())) {
63 return cur_state;
64 }
65 }
66 return 0;
67}
68
69
71 openStates.push_back(es);
72}
73
75 auto it = std::find(openStates.begin(), openStates.end(), es);
76 assert(it != openStates.end());
77 std::swap(*it, openStates.back());
78 openStates.pop_back();
79}
80
82 llvm::Instruction *mp) {
83 // Update stats
85
86 // Incremental update of mean (travelling mean)
87 // https://math.stackexchange.com/a/1836447
88 closedMean += (static_cast<double>(getInstructionDistance(es)) - closedMean) /
90
91 // Remove from openStates
93
94 auto closePoint = reachedCloseMerge.find(mp);
95
96 // If no other state has yet encountered this klee_close_merge instruction,
97 // add a new element to the map
98 if (closePoint == reachedCloseMerge.end()) {
99 reachedCloseMerge[mp].push_back(es);
101 } else {
102 // Otherwise try to merge with any state in the map element for this
103 // instruction
104 auto &cpv = closePoint->second;
105 bool mergedSuccessful = false;
106
107 for (auto& mState: cpv) {
108 if (mState->merge(*es)) {
109 executor->terminateStateEarly(*es, "merged state.", StateTerminationType::Merge);
111 mergedSuccessful = true;
112 break;
113 }
114 }
115 if (!mergedSuccessful) {
116 cpv.push_back(es);
118 }
119 }
120}
121
123 for (auto& curMergeGroup: reachedCloseMerge) {
124 for (auto curState: curMergeGroup.second) {
126 executor->mergingSearcher->inCloseMerge.erase(curState);
127 }
128 }
129 reachedCloseMerge.clear();
130}
131
133 return (!reachedCloseMerge.empty());
134}
135
137 : executor(_executor), openInstruction(es->steppedInstructions),
138 closedMean(0), closedStateCount(0) {
139 executor->mergingSearcher->mergeGroups.push_back(this);
140 addOpenState(es);
141}
142
144 auto it = std::find(executor->mergingSearcher->mergeGroups.begin(),
145 executor->mergingSearcher->mergeGroups.end(), this);
146 assert(it != executor->mergingSearcher->mergeGroups.end() &&
147 "All MergeHandlers should be registered in mergeGroups");
148 std::swap(*it, executor->mergingSearcher->mergeGroups.back());
150
152}
153}
Implementation of the region based merging.
ExecutionState representing a path under exploration.
std::uint64_t steppedInstructions
The numbers of times this state has run through Executor::stepInstruction.
void terminateStateEarly(ExecutionState &state, const llvm::Twine &message, StateTerminationType terminationType)
Definition: Executor.cpp:3648
MergingSearcher * mergingSearcher
Definition: Executor.h:206
void releaseStates()
Immediately release the merged states that have run into a 'klee_merge_close()'.
void addClosedState(ExecutionState *es, llvm::Instruction *mp)
Called when a state runs into a 'klee_close_merge()' call.
bool hasMergedStates()
True, if any states have run into 'klee_close_merge()' and have not been released yet.
double closedMean
The average number of instructions between the open and close merge of each state that has finished s...
Definition: MergeHandler.h:105
std::map< llvm::Instruction *, std::vector< ExecutionState * > > reachedCloseMerge
Mapping the different 'klee_close_merge' calls to the states that ran into them.
Definition: MergeHandler.h:121
unsigned closedStateCount
Number of states that are tracked by this MergeHandler, that ran into a relevant klee_close_merge.
Definition: MergeHandler.h:109
void addOpenState(ExecutionState *es)
Add state to the 'openStates' vector.
unsigned getInstructionDistance(ExecutionState *es)
Get distance of state from the openInstruction.
uint64_t openInstruction
The instruction count when the state ran into the klee_open_merge.
Definition: MergeHandler.h:101
Executor * executor
Definition: MergeHandler.h:98
ExecutionState * getPrioritizeState()
Return state that should be prioritized to complete this merge.
std::vector< ExecutionState * > openStates
States that ran through the klee_open_merge, but not yet into a corresponding klee_close_merge.
Definition: MergeHandler.h:116
MergeHandler(Executor *_executor, ExecutionState *es)
void removeOpenState(ExecutionState *es)
Remove state from the 'openStates' vector.
std::set< ExecutionState * > inCloseMerge
Definition: Searcher.h:215
std::vector< MergeHandler * > mergeGroups
Definition: Searcher.h:222
void continueState(ExecutionState &state)
Continue a paused state.
Definition: Searcher.cpp:354
void pauseState(ExecutionState &state)
Definition: Searcher.cpp:348
Definition: main.cpp:291
llvm::cl::OptionCategory MergeCat
llvm::cl::opt< bool > DebugLogIncompleteMerge
llvm::cl::opt< bool > UseMerge
llvm::cl::opt< bool > DebugLogMerge
llvm::cl::opt< bool > UseIncompleteMerge