klee
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
MemoryManager.cpp
Go to the documentation of this file.
1 //===-- MemoryManager.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 "Common.h"
11 
12 #include "CoreStats.h"
13 #include "Memory.h"
14 #include "MemoryManager.h"
15 
16 #include "klee/ExecutionState.h"
17 #include "klee/Expr.h"
18 #include "klee/Solver.h"
19 
20 #include "llvm/Support/CommandLine.h"
21 
22 using namespace klee;
23 
24 /***/
25 
27  while (!objects.empty()) {
28  MemoryObject *mo = *objects.begin();
29  if (!mo->isFixed)
30  free((void *)mo->address);
31  objects.erase(mo);
32  delete mo;
33  }
34 }
35 
36 MemoryObject *MemoryManager::allocate(uint64_t size, bool isLocal,
37  bool isGlobal,
38  const llvm::Value *allocSite) {
39  if (size>10*1024*1024)
40  klee_warning_once(0, "Large alloc: %u bytes. KLEE may run out of memory.", (unsigned) size);
41 
42  uint64_t address = (uint64_t) (unsigned long) malloc((unsigned) size);
43  if (!address)
44  return 0;
45 
47  MemoryObject *res = new MemoryObject(address, size, isLocal, isGlobal, false,
48  allocSite, this);
49  objects.insert(res);
50  return res;
51 }
52 
53 MemoryObject *MemoryManager::allocateFixed(uint64_t address, uint64_t size,
54  const llvm::Value *allocSite) {
55 #ifndef NDEBUG
56  for (objects_ty::iterator it = objects.begin(), ie = objects.end();
57  it != ie; ++it) {
58  MemoryObject *mo = *it;
59  if (address+size > mo->address && address < mo->address+mo->size)
60  klee_error("Trying to allocate an overlapping object");
61  }
62 #endif
63 
65  MemoryObject *res = new MemoryObject(address, size, false, true, true,
66  allocSite, this);
67  objects.insert(res);
68  return res;
69 }
70 
72  assert(0);
73 }
74 
76  if (objects.find(mo) != objects.end())
77  {
78  if (!mo->isFixed)
79  free((void *)mo->address);
80  objects.erase(mo);
81  }
82 }
void void void void klee_warning_once(const void *id, const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:90
void klee_error(const char *msg,...) __attribute__((format(printf
Definition: Common.cpp:73
void markFreed(MemoryObject *mo)
unsigned size
size in bytes
Definition: Memory.h:45
uint64_t address
Definition: Memory.h:42
MemoryObject * allocate(uint64_t size, bool isLocal, bool isGlobal, const llvm::Value *allocSite)
void deallocate(const MemoryObject *mo)
Statistic allocations
MemoryObject * allocateFixed(uint64_t address, uint64_t size, const llvm::Value *allocSite)