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
|
//===-- AddressSpace.cpp --------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "AddressSpace.h"
#include "CoreStats.h"
#include "Memory.h"
#include "TimingSolver.h"
#include "klee/Expr.h"
#include "klee/TimerStatIncrementer.h"
using namespace klee;
///
void AddressSpace::bindObject(const MemoryObject *mo, ObjectState *os) {
assert(os->copyOnWriteOwner==0 && "object already has owner");
os->copyOnWriteOwner = cowKey;
objects = objects.replace(std::make_pair(mo, os));
}
void AddressSpace::unbindObject(const MemoryObject *mo) {
objects = objects.remove(mo);
}
const ObjectState *AddressSpace::findObject(const MemoryObject *mo) const {
const MemoryMap::value_type *res = objects.lookup(mo);
return res ? res->second : 0;
}
ObjectState *AddressSpace::getWriteable(const MemoryObject *mo,
const ObjectState *os) {
assert(!os->readOnly);
if (cowKey==os->copyOnWriteOwner) {
return const_cast<ObjectState*>(os);
} else {
ObjectState *n = new ObjectState(*os);
n->copyOnWriteOwner = cowKey;
objects = objects.replace(std::make_pair(mo, n));
return n;
}
}
///
bool AddressSpace::resolveOne(uint64_t addr64, ObjectPair &result) {
unsigned address = (unsigned) addr64;
MemoryObject hack(address);
if (const MemoryMap::value_type *res = objects.lookup_previous(&hack)) {
const MemoryObject *mo = res->first;
if ((mo->size==0 && address==mo->address) ||
(address - mo->address < mo->size)) {
result = *res;
return true;
}
}
return false;
}
bool AddressSpace::resolveOne(ExecutionState &state,
TimingSolver *solver,
ref<Expr> address,
ObjectPair &result,
bool &success) {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(address)) {
success = resolveOne(CE->getConstantValue(), result);
return true;
} else {
TimerStatIncrementer timer(stats::resolveTime);
// try cheap search, will succeed for any inbounds pointer
ref<Expr> cex(0);
if (!solver->getValue(state, address, cex))
return false;
unsigned example = (unsigned) cex->getConstantValue();
MemoryObject hack(example);
const MemoryMap::value_type *res = objects.lookup_previous(&hack);
if (res) {
const MemoryObject *mo = res->first;
if (example - mo->address < mo->size) {
result = *res;
success = true;
return true;
}
}
// didn't work, now we have to search
MemoryMap::iterator oi = objects.upper_bound(&hack);
MemoryMap::iterator begin = objects.begin();
MemoryMap::iterator end = objects.end();
MemoryMap::iterator start = oi;
while (oi!=begin) {
--oi;
const MemoryObject *mo = oi->first;
bool mayBeTrue;
if (!solver->mayBeTrue(state,
mo->getBoundsCheckPointer(address), mayBeTrue))
return false;
if (mayBeTrue) {
result = *oi;
success = true;
return true;
} else {
bool mustBeTrue;
if (!solver->mustBeTrue(state,
UgeExpr::create(address, mo->getBaseExpr()),
mustBeTrue))
return false;
if (mustBeTrue)
break;
}
}
// search forwards
for (oi=start; oi!=end; ++oi) {
const MemoryObject *mo = oi->first;
bool mustBeTrue;
if (!solver->mustBeTrue(state,
UltExpr::create(address, mo->getBaseExpr()),
mustBeTrue))
return false;
if (mustBeTrue) {
break;
} else {
bool mayBeTrue;
if (!solver->mayBeTrue(state,
mo->getBoundsCheckPointer(address),
mayBeTrue))
return false;
if (mayBeTrue) {
result = *oi;
success = true;
return true;
}
}
}
success = false;
return true;
}
}
bool AddressSpace::resolve(ExecutionState &state,
TimingSolver *solver,
ref<Expr> p,
ResolutionList &rl,
unsigned maxResolutions,
double timeout) {
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(p)) {
ObjectPair res;
if (resolveOne(CE->getConstantValue(), res))
rl.push_back(res);
return false;
} else {
TimerStatIncrementer timer(stats::resolveTime);
uint64_t timeout_us = (uint64_t) (timeout*1000000.);
// XXX in general this isn't exactly what we want... for
// a multiple resolution case (or for example, a \in {b,c,0})
// we want to find the first object, find a cex assuming
// not the first, find a cex assuming not the second...
// etc.
// XXX how do we smartly amortize the cost of checking to
// see if we need to keep searching up/down, in bad cases?
// maybe we don't care?
// XXX we really just need a smart place to start (although
// if its a known solution then the code below is guaranteed
// to hit the fast path with exactly 2 queries). we could also
// just get this by inspection of the expr.
ref<Expr> cex(0);
if (!solver->getValue(state, p, cex))
return true;
unsigned example = (unsigned) cex->getConstantValue();
MemoryObject hack(example);
MemoryMap::iterator oi = objects.upper_bound(&hack);
MemoryMap::iterator begin = objects.begin();
MemoryMap::iterator end = objects.end();
MemoryMap::iterator start = oi;
// XXX in the common case we can save one query if we ask
// mustBeTrue before mayBeTrue for the first result. easy
// to add I just want to have a nice symbolic test case first.
// search backwards, start with one minus because this
// is the object that p *should* be within, which means we
// get write off the end with 4 queries (XXX can be better,
// no?)
while (oi!=begin) {
--oi;
const MemoryObject *mo = oi->first;
if (timeout_us && timeout_us < timer.check())
return true;
// XXX I think there is some query wasteage here?
ref<Expr> inBounds = mo->getBoundsCheckPointer(p);
bool mayBeTrue;
if (!solver->mayBeTrue(state, inBounds, mayBeTrue))
return true;
if (mayBeTrue) {
rl.push_back(*oi);
// fast path check
unsigned size = rl.size();
if (size==1) {
bool mustBeTrue;
if (!solver->mustBeTrue(state, inBounds, mustBeTrue))
return true;
if (mustBeTrue)
return false;
} else if (size==maxResolutions) {
return true;
}
}
bool mustBeTrue;
if (!solver->mustBeTrue(state,
UgeExpr::create(p, mo->getBaseExpr()),
mustBeTrue))
return true;
if (mustBeTrue)
break;
}
// search forwards
for (oi=start; oi!=end; ++oi) {
const MemoryObject *mo = oi->first;
if (timeout_us && timeout_us < timer.check())
return true;
bool mustBeTrue;
if (!solver->mustBeTrue(state,
UltExpr::create(p, mo->getBaseExpr()),
mustBeTrue))
return true;
if (mustBeTrue)
break;
// XXX I think there is some query wasteage here?
ref<Expr> inBounds = mo->getBoundsCheckPointer(p);
bool mayBeTrue;
if (!solver->mayBeTrue(state, inBounds, mayBeTrue))
return true;
if (mayBeTrue) {
rl.push_back(*oi);
// fast path check
unsigned size = rl.size();
if (size==1) {
bool mustBeTrue;
if (!solver->mustBeTrue(state, inBounds, mustBeTrue))
return true;
if (mustBeTrue)
return false;
} else if (size==maxResolutions) {
return true;
}
}
}
}
return false;
}
// These two are pretty big hack so we can sort of pass memory back
// and forth to externals. They work by abusing the concrete cache
// store inside of the object states, which allows them to
// transparently avoid screwing up symbolics (if the byte is symbolic
// then its concrete cache byte isn't being used) but is just a hack.
void AddressSpace::copyOutConcretes() {
for (MemoryMap::iterator it = objects.begin(), ie = objects.end();
it != ie; ++it) {
const MemoryObject *mo = it->first;
if (!mo->isUserSpecified) {
ObjectState *os = it->second;
uint8_t *address = (uint8_t*) (unsigned long) mo->address;
if (!os->readOnly)
memcpy(address, os->concreteStore, mo->size);
}
}
}
bool AddressSpace::copyInConcretes() {
for (MemoryMap::iterator it = objects.begin(), ie = objects.end();
it != ie; ++it) {
const MemoryObject *mo = it->first;
if (!mo->isUserSpecified) {
const ObjectState *os = it->second;
uint8_t *address = (uint8_t*) (unsigned long) mo->address;
if (memcmp(address, os->concreteStore, mo->size)!=0) {
if (os->readOnly) {
return false;
} else {
ObjectState *wos = getWriteable(mo, os);
memcpy(wos->concreteStore, address, mo->size);
}
}
}
}
return true;
}
/***/
bool MemoryObjectLT::operator()(const MemoryObject *a, const MemoryObject *b) const {
return a->address < b->address;
}
|