18#include "llvm/Support/CommandLine.h"
19#include "llvm/Support/MathExtras.h"
28llvm::cl::OptionCategory MemoryCat(
"Memory management options",
29 "These options control memory management.");
31llvm::cl::opt<bool> DeterministicAllocation(
33 llvm::cl::desc(
"Allocate memory deterministically (default=false)"),
34 llvm::cl::init(
false), llvm::cl::cat(MemoryCat));
36llvm::cl::opt<unsigned> DeterministicAllocationSize(
37 "allocate-determ-size",
39 "Preallocated memory for deterministic allocation in MB (default=100)"),
40 llvm::cl::init(100), llvm::cl::cat(MemoryCat));
42llvm::cl::opt<bool> NullOnZeroMalloc(
43 "return-null-on-zero-malloc",
44 llvm::cl::desc(
"Returns NULL if malloc(0) is called (default=false)"),
45 llvm::cl::init(
false), llvm::cl::cat(MemoryCat));
47llvm::cl::opt<unsigned> RedzoneSize(
49 llvm::cl::desc(
"Set the size of the redzones to be added after each "
50 "allocation (in bytes). This is important to detect "
51 "out-of-bounds accesses (default=10)"),
52 llvm::cl::init(10), llvm::cl::cat(MemoryCat));
54llvm::cl::opt<unsigned long long> DeterministicStartAddress(
55 "allocate-determ-start-address",
56 llvm::cl::desc(
"Start address for deterministic allocation. Has to be page "
57 "aligned (default=0x7ff30000000)"),
58 llvm::cl::init(0x7ff30000000), llvm::cl::cat(MemoryCat));
63 : arrayCache(_arrayCache), deterministicSpace(0), nextFreeSlot(0),
64 spaceSize(DeterministicAllocationSize.getValue() * 1024 * 1024) {
65 if (DeterministicAllocation) {
67 void *expectedAddress = (
void *)DeterministicStartAddress.getValue();
70 (
char *)mmap(expectedAddress,
spaceSize, PROT_READ | PROT_WRITE,
71 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
73 if (newSpace == MAP_FAILED) {
74 klee_error(
"Couldn't mmap() memory for deterministic allocations");
76 if (expectedAddress != newSpace && expectedAddress != 0) {
77 klee_error(
"Could not allocate memory deterministically");
80 klee_message(
"Deterministic memory allocation starting from %p", newSpace);
89 if (!mo->
isFixed && !DeterministicAllocation)
95 if (DeterministicAllocation)
101 const llvm::Value *allocSite,
103 if (size > 10 * 1024 * 1024)
105 " bytes. KLEE may run out of memory.",
109 if (NullOnZeroMalloc && size == 0)
112 if (!llvm::isPowerOf2_64(alignment)) {
113 klee_warning(
"Only alignment of power of two is supported");
117 uint64_t address = 0;
118 if (DeterministicAllocation) {
119 address = llvm::alignTo((uint64_t)
nextFreeSlot + alignment - 1, alignment);
123 size_t alloc_size = std::max(size, (uint64_t)1);
125 nextFreeSlot = (
char *)address + alloc_size + RedzoneSize;
128 " bytes. Not enough deterministic space left.",
135 address = (uint64_t)malloc(size);
137 int res = posix_memalign((
void **)&address, alignment, size);
156 const llvm::Value *allocSite) {
158 for (objects_ty::iterator it =
objects.begin(), ie =
objects.end(); it != ie;
161 if (address + size > mo->
address && address < mo->address + mo->
size)
162 klee_error(
"Trying to allocate an overlapping object");
168 new MemoryObject(address, size,
false,
true,
true, allocSite,
this);
177 if (!mo->
isFixed && !DeterministicAllocation)
Provides an interface for creating and destroying Array objects.
MemoryObject * allocateFixed(uint64_t address, uint64_t size, const llvm::Value *allocSite)
size_t getUsedDeterministicSize()
void deallocate(const MemoryObject *mo)
void markFreed(MemoryObject *mo)
MemoryObject * allocate(uint64_t size, bool isLocal, bool isGlobal, const llvm::Value *allocSite, size_t alignment)
char * deterministicSpace
unsigned size
size in bytes
void klee_message(const char *msg,...) __attribute__((format(printf
void klee_error(const char *msg,...) __attribute__((format(printf
void void void void klee_warning_once(const void *id, const char *msg,...) __attribute__((format(printf
void void void klee_warning(const char *msg,...) __attribute__((format(printf