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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
//===-- CexCachingSolver.cpp ----------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "klee/Solver.h"
#include "klee/Constraints.h"
#include "klee/Expr.h"
#include "klee/SolverImpl.h"
#include "klee/TimerStatIncrementer.h"
#include "klee/util/Assignment.h"
#include "klee/util/ExprUtil.h"
#include "klee/util/ExprVisitor.h"
#include "klee/Internal/ADT/MapOfSets.h"
#include "SolverStats.h"
#include "llvm/Support/CommandLine.h"
using namespace klee;
using namespace llvm;
namespace {
cl::opt<bool>
DebugCexCacheCheckBinding("debug-cex-cache-check-binding");
cl::opt<bool>
CexCacheTryAll("cex-cache-try-all",
cl::desc("try substituting all counterexamples before asking STP"),
cl::init(false));
cl::opt<bool>
CexCacheExperimental("cex-cache-exp", cl::init(false));
}
///
typedef std::set< ref<Expr> > KeyType;
struct AssignmentLessThan {
bool operator()(const Assignment *a, const Assignment *b) {
return a->bindings < b->bindings;
}
};
class CexCachingSolver : public SolverImpl {
typedef std::set<Assignment*, AssignmentLessThan> assignmentsTable_ty;
Solver *solver;
MapOfSets<ref<Expr>, Assignment*> cache;
// memo table
assignmentsTable_ty assignmentsTable;
bool searchForAssignment(KeyType &key,
Assignment *&result);
bool lookupAssignment(const Query& query, KeyType &key, Assignment *&result);
bool lookupAssignment(const Query& query, Assignment *&result) {
KeyType key;
return lookupAssignment(query, key, result);
}
bool getAssignment(const Query& query, Assignment *&result);
public:
CexCachingSolver(Solver *_solver) : solver(_solver) {}
~CexCachingSolver();
bool computeTruth(const Query&, bool &isValid);
bool computeValidity(const Query&, Solver::Validity &result);
bool computeValue(const Query&, ref<Expr> &result);
bool computeInitialValues(const Query&,
const std::vector<const Array*> &objects,
std::vector< std::vector<unsigned char> > &values,
bool &hasSolution);
};
///
struct NullAssignment {
bool operator()(Assignment *a) const { return !a; }
};
struct NonNullAssignment {
bool operator()(Assignment *a) const { return a!=0; }
};
struct NullOrSatisfyingAssignment {
KeyType &key;
NullOrSatisfyingAssignment(KeyType &_key) : key(_key) {}
bool operator()(Assignment *a) const {
return !a || a->satisfies(key.begin(), key.end());
}
};
/// searchForAssignment - Look for a cached solution for a query.
///
/// \param key - The query to look up.
/// \param result [out] - The cached result, if the lookup is succesful. This is
/// either a satisfying assignment (for a satisfiable query), or 0 (for an
/// unsatisfiable query).
/// \return - True if a cached result was found.
bool CexCachingSolver::searchForAssignment(KeyType &key, Assignment *&result) {
Assignment * const *lookup = cache.lookup(key);
if (lookup) {
result = *lookup;
return true;
}
if (CexCacheTryAll) {
// Look for a satisfying assignment for a superset, which is trivially an
// assignment for any subset.
Assignment **lookup = cache.findSuperset(key, NonNullAssignment());
// Otherwise, look for a subset which is unsatisfiable, see below.
if (!lookup)
lookup = cache.findSubset(key, NullAssignment());
// If either lookup succeeded, then we have a cached solution.
if (lookup) {
result = *lookup;
return true;
}
// Otherwise, iterate through the set of current assignments to see if one
// of them satisfies the query.
for (assignmentsTable_ty::iterator it = assignmentsTable.begin(),
ie = assignmentsTable.end(); it != ie; ++it) {
Assignment *a = *it;
if (a->satisfies(key.begin(), key.end())) {
result = a;
return true;
}
}
} else {
// FIXME: Which order? one is sure to be better.
// Look for a satisfying assignment for a superset, which is trivially an
// assignment for any subset.
Assignment **lookup = cache.findSuperset(key, NonNullAssignment());
// Otherwise, look for a subset which is unsatisfiable -- if the subset is
// unsatisfiable then no additional constraints can produce a valid
// assignment. While searching subsets, we also explicitly the solutions for
// satisfiable subsets to see if they solve the current query and return
// them if so. This is cheap and frequently succeeds.
if (!lookup)
lookup = cache.findSubset(key, NullOrSatisfyingAssignment(key));
// If either lookup succeeded, then we have a cached solution.
if (lookup) {
result = *lookup;
return true;
}
}
return false;
}
/// lookupAssignment - Lookup a cached result for the given \arg query.
///
/// \param query - The query to lookup.
/// \param key [out] - On return, the key constructed for the query.
/// \param result [out] - The cached result, if the lookup is succesful. This is
/// either a satisfying assignment (for a satisfiable query), or 0 (for an
/// unsatisfiable query).
/// \return True if a cached result was found.
bool CexCachingSolver::lookupAssignment(const Query &query,
KeyType &key,
Assignment *&result) {
key = KeyType(query.constraints.begin(), query.constraints.end());
ref<Expr> neg = Expr::createIsZero(query.expr);
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(neg)) {
if (CE->isFalse()) {
result = (Assignment*) 0;
return true;
}
} else {
key.insert(neg);
}
return searchForAssignment(key, result);
}
bool CexCachingSolver::getAssignment(const Query& query, Assignment *&result) {
KeyType key;
if (lookupAssignment(query, key, result))
return true;
std::vector<const Array*> objects;
findSymbolicObjects(key.begin(), key.end(), objects);
std::vector< std::vector<unsigned char> > values;
bool hasSolution;
if (!solver->impl->computeInitialValues(query, objects, values,
hasSolution))
return false;
Assignment *binding;
if (hasSolution) {
binding = new Assignment(objects, values);
// Memoize the result.
std::pair<assignmentsTable_ty::iterator, bool>
res = assignmentsTable.insert(binding);
if (!res.second) {
delete binding;
binding = *res.first;
}
if (DebugCexCacheCheckBinding)
assert(binding->satisfies(key.begin(), key.end()));
} else {
binding = (Assignment*) 0;
}
result = binding;
cache.insert(key, binding);
return true;
}
///
CexCachingSolver::~CexCachingSolver() {
cache.clear();
delete solver;
for (assignmentsTable_ty::iterator it = assignmentsTable.begin(),
ie = assignmentsTable.end(); it != ie; ++it)
delete *it;
}
bool CexCachingSolver::computeValidity(const Query& query,
Solver::Validity &result) {
TimerStatIncrementer t(stats::cexCacheTime);
Assignment *a;
if (!getAssignment(query.withFalse(), a))
return false;
assert(a && "computeValidity() must have assignment");
ref<Expr> q = a->evaluate(query.expr);
assert(isa<ConstantExpr>(q) &&
"assignment evaluation did not result in constant");
if (cast<ConstantExpr>(q)->isTrue()) {
if (!getAssignment(query, a))
return false;
result = !a ? Solver::True : Solver::Unknown;
} else {
if (!getAssignment(query.negateExpr(), a))
return false;
result = !a ? Solver::False : Solver::Unknown;
}
return true;
}
bool CexCachingSolver::computeTruth(const Query& query,
bool &isValid) {
TimerStatIncrementer t(stats::cexCacheTime);
// There is a small amount of redundancy here. We only need to know
// truth and do not really need to compute an assignment. This means
// that we could check the cache to see if we already know that
// state ^ query has no assignment. In that case, by the validity of
// state, we know that state ^ !query must have an assignment, and
// so query cannot be true (valid). This does get hits, but doesn't
// really seem to be worth the overhead.
if (CexCacheExperimental) {
Assignment *a;
if (lookupAssignment(query.negateExpr(), a) && !a)
return false;
}
Assignment *a;
if (!getAssignment(query, a))
return false;
isValid = !a;
return true;
}
bool CexCachingSolver::computeValue(const Query& query,
ref<Expr> &result) {
TimerStatIncrementer t(stats::cexCacheTime);
Assignment *a;
if (!getAssignment(query.withFalse(), a))
return false;
assert(a && "computeValue() must have assignment");
result = a->evaluate(query.expr);
assert(isa<ConstantExpr>(result) &&
"assignment evaluation did not result in constant");
return true;
}
bool
CexCachingSolver::computeInitialValues(const Query& query,
const std::vector<const Array*>
&objects,
std::vector< std::vector<unsigned char> >
&values,
bool &hasSolution) {
TimerStatIncrementer t(stats::cexCacheTime);
Assignment *a;
if (!getAssignment(query, a))
return false;
hasSolution = !!a;
if (!a)
return true;
// FIXME: We should use smarter assignment for result so we don't
// need redundant copy.
values = std::vector< std::vector<unsigned char> >(objects.size());
for (unsigned i=0; i < objects.size(); ++i) {
const Array *os = objects[i];
Assignment::bindings_ty::iterator it = a->bindings.find(os);
if (it == a->bindings.end()) {
values[i] = std::vector<unsigned char>(os->size, 0);
} else {
values[i] = it->second;
}
}
return true;
}
///
Solver *klee::createCexCachingSolver(Solver *_solver) {
return new Solver(new CexCachingSolver(_solver));
}
|