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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
|
//===-- Memory.cpp --------------------------------------------------------===//
//
// The KLEE Symbolic Virtual Machine
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "Common.h"
#include "Memory.h"
#include "Context.h"
#include "klee/Expr.h"
#include "klee/Solver.h"
#include "klee/util/BitArray.h"
#include "ObjectHolder.h"
#include "MemoryManager.h"
#include <llvm/Function.h>
#include <llvm/Instruction.h>
#include <llvm/Value.h>
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include <iostream>
#include <cassert>
#include <sstream>
using namespace llvm;
using namespace klee;
namespace {
cl::opt<bool>
UseConstantArrays("use-constant-arrays",
cl::init(true));
}
/***/
ObjectHolder::ObjectHolder(const ObjectHolder &b) : os(b.os) {
if (os) ++os->refCount;
}
ObjectHolder::ObjectHolder(ObjectState *_os) : os(_os) {
if (os) ++os->refCount;
}
ObjectHolder::~ObjectHolder() {
if (os && --os->refCount==0) delete os;
}
ObjectHolder &ObjectHolder::operator=(const ObjectHolder &b) {
if (b.os) ++b.os->refCount;
if (os && --os->refCount==0) delete os;
os = b.os;
return *this;
}
/***/
int MemoryObject::counter = 0;
MemoryObject::~MemoryObject() {
if (parent)
parent->markFreed(this);
}
void MemoryObject::getAllocInfo(std::string &result) const {
llvm::raw_string_ostream info(result);
info << "MO" << id << "[" << size << "]";
if (allocSite) {
info << " allocated at ";
if (const Instruction *i = dyn_cast<Instruction>(allocSite)) {
info << i->getParent()->getParent()->getName() << "():";
info << *i;
} else if (const GlobalValue *gv = dyn_cast<GlobalValue>(allocSite)) {
info << "global:" << gv->getName();
} else {
info << "value:" << *allocSite;
}
} else {
info << " (no allocation info)";
}
info.flush();
}
/***/
ObjectState::ObjectState(const MemoryObject *mo)
: copyOnWriteOwner(0),
refCount(0),
object(mo),
concreteStore(new uint8_t[mo->size]),
concreteMask(0),
flushMask(0),
knownSymbolics(0),
updates(0, 0),
size(mo->size),
readOnly(false) {
mo->refCount++;
if (!UseConstantArrays) {
// FIXME: Leaked.
static unsigned id = 0;
const Array *array = new Array("tmp_arr" + llvm::utostr(++id), size);
updates = UpdateList(array, 0);
}
memset(concreteStore, 0, size);
}
ObjectState::ObjectState(const MemoryObject *mo, const Array *array)
: copyOnWriteOwner(0),
refCount(0),
object(mo),
concreteStore(new uint8_t[mo->size]),
concreteMask(0),
flushMask(0),
knownSymbolics(0),
updates(array, 0),
size(mo->size),
readOnly(false) {
mo->refCount++;
makeSymbolic();
memset(concreteStore, 0, size);
}
ObjectState::ObjectState(const ObjectState &os)
: copyOnWriteOwner(0),
refCount(0),
object(os.object),
concreteStore(new uint8_t[os.size]),
concreteMask(os.concreteMask ? new BitArray(*os.concreteMask, os.size) : 0),
flushMask(os.flushMask ? new BitArray(*os.flushMask, os.size) : 0),
knownSymbolics(0),
updates(os.updates),
size(os.size),
readOnly(false) {
assert(!os.readOnly && "no need to copy read only object?");
if (object)
object->refCount++;
if (os.knownSymbolics) {
knownSymbolics = new ref<Expr>[size];
for (unsigned i=0; i<size; i++)
knownSymbolics[i] = os.knownSymbolics[i];
}
memcpy(concreteStore, os.concreteStore, size*sizeof(*concreteStore));
}
ObjectState::~ObjectState() {
if (concreteMask) delete concreteMask;
if (flushMask) delete flushMask;
if (knownSymbolics) delete[] knownSymbolics;
delete[] concreteStore;
if (object)
{
assert(object->refCount > 0);
object->refCount--;
if (object->refCount == 0)
{
delete object;
}
}
}
/***/
const UpdateList &ObjectState::getUpdates() const {
// Constant arrays are created lazily.
if (!updates.root) {
// Collect the list of writes, with the oldest writes first.
// FIXME: We should be able to do this more efficiently, we just need to be
// careful to get the interaction with the cache right. In particular we
// should avoid creating UpdateNode instances we never use.
unsigned NumWrites = updates.head ? updates.head->getSize() : 0;
std::vector< std::pair< ref<Expr>, ref<Expr> > > Writes(NumWrites);
const UpdateNode *un = updates.head;
for (unsigned i = NumWrites; i != 0; un = un->next) {
--i;
Writes[i] = std::make_pair(un->index, un->value);
}
std::vector< ref<ConstantExpr> > Contents(size);
// Initialize to zeros.
for (unsigned i = 0, e = size; i != e; ++i)
Contents[i] = ConstantExpr::create(0, Expr::Int8);
// Pull off as many concrete writes as we can.
unsigned Begin = 0, End = Writes.size();
for (; Begin != End; ++Begin) {
// Push concrete writes into the constant array.
ConstantExpr *Index = dyn_cast<ConstantExpr>(Writes[Begin].first);
if (!Index)
break;
ConstantExpr *Value = dyn_cast<ConstantExpr>(Writes[Begin].second);
if (!Value)
break;
Contents[Index->getZExtValue()] = Value;
}
// FIXME: We should unique these, there is no good reason to create multiple
// ones.
// Start a new update list.
// FIXME: Leaked.
static unsigned id = 0;
const Array *array = new Array("const_arr" + llvm::utostr(++id), size,
&Contents[0],
&Contents[0] + Contents.size());
updates = UpdateList(array, 0);
// Apply the remaining (non-constant) writes.
for (; Begin != End; ++Begin)
updates.extend(Writes[Begin].first, Writes[Begin].second);
}
return updates;
}
void ObjectState::makeConcrete() {
if (concreteMask) delete concreteMask;
if (flushMask) delete flushMask;
if (knownSymbolics) delete[] knownSymbolics;
concreteMask = 0;
flushMask = 0;
knownSymbolics = 0;
}
void ObjectState::makeSymbolic() {
assert(!updates.head &&
"XXX makeSymbolic of objects with symbolic values is unsupported");
// XXX simplify this, can just delete various arrays I guess
for (unsigned i=0; i<size; i++) {
markByteSymbolic(i);
setKnownSymbolic(i, 0);
markByteFlushed(i);
}
}
void ObjectState::initializeToZero() {
makeConcrete();
memset(concreteStore, 0, size);
}
void ObjectState::initializeToRandom() {
makeConcrete();
for (unsigned i=0; i<size; i++) {
// randomly selected by 256 sided die
concreteStore[i] = 0xAB;
}
}
/*
Cache Invariants
--
isByteKnownSymbolic(i) => !isByteConcrete(i)
isByteConcrete(i) => !isByteKnownSymbolic(i)
!isByteFlushed(i) => (isByteConcrete(i) || isByteKnownSymbolic(i))
*/
void ObjectState::fastRangeCheckOffset(ref<Expr> offset,
unsigned *base_r,
unsigned *size_r) const {
*base_r = 0;
*size_r = size;
}
void ObjectState::flushRangeForRead(unsigned rangeBase,
unsigned rangeSize) const {
if (!flushMask) flushMask = new BitArray(size, true);
for (unsigned offset=rangeBase; offset<rangeBase+rangeSize; offset++) {
if (!isByteFlushed(offset)) {
if (isByteConcrete(offset)) {
updates.extend(ConstantExpr::create(offset, Expr::Int32),
ConstantExpr::create(concreteStore[offset], Expr::Int8));
} else {
assert(isByteKnownSymbolic(offset) && "invalid bit set in flushMask");
updates.extend(ConstantExpr::create(offset, Expr::Int32),
knownSymbolics[offset]);
}
flushMask->unset(offset);
}
}
}
void ObjectState::flushRangeForWrite(unsigned rangeBase,
unsigned rangeSize) {
if (!flushMask) flushMask = new BitArray(size, true);
for (unsigned offset=rangeBase; offset<rangeBase+rangeSize; offset++) {
if (!isByteFlushed(offset)) {
if (isByteConcrete(offset)) {
updates.extend(ConstantExpr::create(offset, Expr::Int32),
ConstantExpr::create(concreteStore[offset], Expr::Int8));
markByteSymbolic(offset);
} else {
assert(isByteKnownSymbolic(offset) && "invalid bit set in flushMask");
updates.extend(ConstantExpr::create(offset, Expr::Int32),
knownSymbolics[offset]);
setKnownSymbolic(offset, 0);
}
flushMask->unset(offset);
} else {
// flushed bytes that are written over still need
// to be marked out
if (isByteConcrete(offset)) {
markByteSymbolic(offset);
} else if (isByteKnownSymbolic(offset)) {
setKnownSymbolic(offset, 0);
}
}
}
}
bool ObjectState::isByteConcrete(unsigned offset) const {
return !concreteMask || concreteMask->get(offset);
}
bool ObjectState::isByteFlushed(unsigned offset) const {
return flushMask && !flushMask->get(offset);
}
bool ObjectState::isByteKnownSymbolic(unsigned offset) const {
return knownSymbolics && knownSymbolics[offset].get();
}
void ObjectState::markByteConcrete(unsigned offset) {
if (concreteMask)
concreteMask->set(offset);
}
void ObjectState::markByteSymbolic(unsigned offset) {
if (!concreteMask)
concreteMask = new BitArray(size, true);
concreteMask->unset(offset);
}
void ObjectState::markByteUnflushed(unsigned offset) {
if (flushMask)
flushMask->set(offset);
}
void ObjectState::markByteFlushed(unsigned offset) {
if (!flushMask) {
flushMask = new BitArray(size, false);
} else {
flushMask->unset(offset);
}
}
void ObjectState::setKnownSymbolic(unsigned offset,
Expr *value /* can be null */) {
if (knownSymbolics) {
knownSymbolics[offset] = value;
} else {
if (value) {
knownSymbolics = new ref<Expr>[size];
knownSymbolics[offset] = value;
}
}
}
/***/
ref<Expr> ObjectState::read8(unsigned offset) const {
if (isByteConcrete(offset)) {
return ConstantExpr::create(concreteStore[offset], Expr::Int8);
} else if (isByteKnownSymbolic(offset)) {
return knownSymbolics[offset];
} else {
assert(isByteFlushed(offset) && "unflushed byte without cache value");
return ReadExpr::create(getUpdates(),
ConstantExpr::create(offset, Expr::Int32));
}
}
ref<Expr> ObjectState::read8(ref<Expr> offset) const {
assert(!isa<ConstantExpr>(offset) && "constant offset passed to symbolic read8");
unsigned base, size;
fastRangeCheckOffset(offset, &base, &size);
flushRangeForRead(base, size);
if (size>4096) {
std::string allocInfo;
object->getAllocInfo(allocInfo);
klee_warning_once(0, "flushing %d bytes on read, may be slow and/or crash: %s",
size,
allocInfo.c_str());
}
return ReadExpr::create(getUpdates(), ZExtExpr::create(offset, Expr::Int32));
}
void ObjectState::write8(unsigned offset, uint8_t value) {
//assert(read_only == false && "writing to read-only object!");
concreteStore[offset] = value;
setKnownSymbolic(offset, 0);
markByteConcrete(offset);
markByteUnflushed(offset);
}
void ObjectState::write8(unsigned offset, ref<Expr> value) {
// can happen when ExtractExpr special cases
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(value)) {
write8(offset, (uint8_t) CE->getZExtValue(8));
} else {
setKnownSymbolic(offset, value.get());
markByteSymbolic(offset);
markByteUnflushed(offset);
}
}
void ObjectState::write8(ref<Expr> offset, ref<Expr> value) {
assert(!isa<ConstantExpr>(offset) && "constant offset passed to symbolic write8");
unsigned base, size;
fastRangeCheckOffset(offset, &base, &size);
flushRangeForWrite(base, size);
if (size>4096) {
std::string allocInfo;
object->getAllocInfo(allocInfo);
klee_warning_once(0, "flushing %d bytes on read, may be slow and/or crash: %s",
size,
allocInfo.c_str());
}
updates.extend(ZExtExpr::create(offset, Expr::Int32), value);
}
/***/
ref<Expr> ObjectState::read(ref<Expr> offset, Expr::Width width) const {
// Truncate offset to 32-bits.
offset = ZExtExpr::create(offset, Expr::Int32);
// Check for reads at constant offsets.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(offset))
return read(CE->getZExtValue(32), width);
// Treat bool specially, it is the only non-byte sized write we allow.
if (width == Expr::Bool)
return ExtractExpr::create(read8(offset), 0, Expr::Bool);
// Otherwise, follow the slow general case.
unsigned NumBytes = width / 8;
assert(width == NumBytes * 8 && "Invalid write size!");
ref<Expr> Res(0);
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
ref<Expr> Byte = read8(AddExpr::create(offset,
ConstantExpr::create(idx,
Expr::Int32)));
Res = i ? ConcatExpr::create(Byte, Res) : Byte;
}
return Res;
}
ref<Expr> ObjectState::read(unsigned offset, Expr::Width width) const {
// Treat bool specially, it is the only non-byte sized write we allow.
if (width == Expr::Bool)
return ExtractExpr::create(read8(offset), 0, Expr::Bool);
// Otherwise, follow the slow general case.
unsigned NumBytes = width / 8;
assert(width == NumBytes * 8 && "Invalid write size!");
ref<Expr> Res(0);
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
ref<Expr> Byte = read8(offset + idx);
Res = i ? ConcatExpr::create(Byte, Res) : Byte;
}
return Res;
}
void ObjectState::write(ref<Expr> offset, ref<Expr> value) {
// Truncate offset to 32-bits.
offset = ZExtExpr::create(offset, Expr::Int32);
// Check for writes at constant offsets.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(offset)) {
write(CE->getZExtValue(32), value);
return;
}
// Treat bool specially, it is the only non-byte sized write we allow.
Expr::Width w = value->getWidth();
if (w == Expr::Bool) {
write8(offset, ZExtExpr::create(value, Expr::Int8));
return;
}
// Otherwise, follow the slow general case.
unsigned NumBytes = w / 8;
assert(w == NumBytes * 8 && "Invalid write size!");
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
write8(AddExpr::create(offset, ConstantExpr::create(idx, Expr::Int32)),
ExtractExpr::create(value, 8 * i, Expr::Int8));
}
}
void ObjectState::write(unsigned offset, ref<Expr> value) {
// Check for writes of constant values.
if (ConstantExpr *CE = dyn_cast<ConstantExpr>(value)) {
Expr::Width w = CE->getWidth();
if (w <= 64) {
uint64_t val = CE->getZExtValue();
switch (w) {
default: assert(0 && "Invalid write size!");
case Expr::Bool:
case Expr::Int8: write8(offset, val); return;
case Expr::Int16: write16(offset, val); return;
case Expr::Int32: write32(offset, val); return;
case Expr::Int64: write64(offset, val); return;
}
}
}
// Treat bool specially, it is the only non-byte sized write we allow.
Expr::Width w = value->getWidth();
if (w == Expr::Bool) {
write8(offset, ZExtExpr::create(value, Expr::Int8));
return;
}
// Otherwise, follow the slow general case.
unsigned NumBytes = w / 8;
assert(w == NumBytes * 8 && "Invalid write size!");
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
write8(offset + idx, ExtractExpr::create(value, 8 * i, Expr::Int8));
}
}
void ObjectState::write16(unsigned offset, uint16_t value) {
unsigned NumBytes = 2;
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
write8(offset + idx, (uint8_t) (value >> (8 * i)));
}
}
void ObjectState::write32(unsigned offset, uint32_t value) {
unsigned NumBytes = 4;
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
write8(offset + idx, (uint8_t) (value >> (8 * i)));
}
}
void ObjectState::write64(unsigned offset, uint64_t value) {
unsigned NumBytes = 8;
for (unsigned i = 0; i != NumBytes; ++i) {
unsigned idx = Context::get().isLittleEndian() ? i : (NumBytes - i - 1);
write8(offset + idx, (uint8_t) (value >> (8 * i)));
}
}
void ObjectState::print() {
std::cerr << "-- ObjectState --\n";
std::cerr << "\tMemoryObject ID: " << object->id << "\n";
std::cerr << "\tRoot Object: " << updates.root << "\n";
std::cerr << "\tSize: " << size << "\n";
std::cerr << "\tBytes:\n";
for (unsigned i=0; i<size; i++) {
std::cerr << "\t\t["<<i<<"]"
<< " concrete? " << isByteConcrete(i)
<< " known-sym? " << isByteKnownSymbolic(i)
<< " flushed? " << isByteFlushed(i) << " = ";
ref<Expr> e = read8(i);
std::cerr << e << "\n";
}
std::cerr << "\tUpdates:\n";
for (const UpdateNode *un=updates.head; un; un=un->next) {
std::cerr << "\t\t[" << un->index << "] = " << un->value << "\n";
}
}
|