1 /* Copyright (c) 2015 Regents of the University of California
3 * Author: Brian Demsky <bdemsky@uci.edu>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
20 #include "threads-model.h"
22 #include "mcexecution.h"
24 #define REQUESTS_BEFORE_ALLOC 1024
26 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
30 #if !USE_MPROTECT_SNAPSHOT
31 static mspace sStaticSpace = NULL;
34 /** Non-snapshotting calloc for our use. */
35 void *model_calloc(size_t count, size_t size)
37 #if USE_MPROTECT_SNAPSHOT
38 static void *(*callocp)(size_t count, size_t size) = NULL;
42 /* get address of libc malloc */
44 callocp = (void * (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
45 if ((error = dlerror()) != NULL) {
50 ptr = callocp(count, size);
54 sStaticSpace = create_shared_mspace();
55 return mspace_calloc(sStaticSpace, count, size);
59 /** Non-snapshotting malloc for our use. */
60 void *model_malloc(size_t size)
62 #if USE_MPROTECT_SNAPSHOT
63 static void *(*mallocp)(size_t size) = NULL;
67 /* get address of libc malloc */
69 mallocp = (void * (*)(size_t))dlsym(RTLD_NEXT, "malloc");
70 if ((error = dlerror()) != NULL) {
79 sStaticSpace = create_shared_mspace();
80 return mspace_malloc(sStaticSpace, size);
84 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
85 void *model_realloc(void *ptr, size_t size)
87 #if USE_MPROTECT_SNAPSHOT
88 static void *(*reallocp)(void *ptr, size_t size) = NULL;
92 /* get address of libc malloc */
94 reallocp = (void * (*)(void *,size_t))dlsym(RTLD_NEXT, "realloc");
95 if ((error = dlerror()) != NULL) {
100 tmpptr = reallocp(ptr, size);
104 sStaticSpace = create_shared_mspace();
105 return mspace_realloc(sStaticSpace, ptr, size);
109 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
110 void * snapshot_malloc(size_t size)
112 void *tmp = mspace_malloc(model_snapshot_space, size);
117 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
118 void * snapshot_calloc(size_t count, size_t size)
120 void *tmp = mspace_calloc(model_snapshot_space, count, size);
125 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
126 void *snapshot_realloc(void *ptr, size_t size)
128 void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
133 /** @brief Snapshotting free, for use by model-checker (not user progs) */
134 void snapshot_free(void *ptr)
136 mspace_free(model_snapshot_space, ptr);
139 /** Non-snapshotting free for our use. */
140 void model_free(void *ptr)
142 #if USE_MPROTECT_SNAPSHOT
143 static void (*freep)(void *);
146 /* get address of libc free */
148 freep = (void (*)(void *))dlsym(RTLD_NEXT, "free");
149 if ((error = dlerror()) != NULL) {
150 fputs(error, stderr);
156 mspace_free(sStaticSpace, ptr);
160 /** Bootstrap allocation. Problem is that the dynamic linker calls require
161 * calloc to work and calloc requires the dynamic linker to work. */
163 #define BOOTSTRAPBYTES 131072
164 char bootstrapmemory[BOOTSTRAPBYTES];
167 void * HandleEarlyAllocationRequest(size_t sz)
169 /* Align to 8 byte boundary */
172 if (sz > (BOOTSTRAPBYTES-offset)) {
173 model_print("OUT OF BOOTSTRAP MEMORY. Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
177 void *pointer = (void *)&bootstrapmemory[offset];
182 /** @brief Global mspace reference for the model-checker's snapshotting heap */
183 mspace model_snapshot_space = NULL;
185 #if USE_MPROTECT_SNAPSHOT
187 /** @brief Global mspace reference for the user's snapshotting heap */
188 void * user_snapshot_space = NULL;
189 mspace thread_snapshot_space = NULL;
191 struct snapshot_heap_data * snapshot_struct;
193 /** Check whether this is bootstrapped memory that we should not free */
194 static bool DontFree(void *ptr)
196 return (ptr >= (&bootstrapmemory[0]) && ptr < (&bootstrapmemory[BOOTSTRAPBYTES]));
200 static void * user_malloc(size_t size) {
201 return model->get_execution()->alloc(size);
205 * @brief The allocator function for "user" allocation
207 * Should only be used for allocations which will not disturb the allocation
208 * patterns of a user thread.
210 void * real_user_malloc(size_t size)
212 size=(size+7)&~((size_t)7);
213 void *tmp = snapshot_struct->allocation_ptr;
214 snapshot_struct->allocation_ptr = (void *)((char *) snapshot_struct->allocation_ptr +size);
216 ASSERT(snapshot_struct->allocation_ptr <= snapshot_struct->top_ptr);
221 * @brief Snapshotting malloc implementation for user programs
223 * Do NOT call this function from a model-checker context. Doing so may disrupt
224 * the allocation patterns of a user thread.
226 void *malloc(size_t size)
228 if (user_snapshot_space) {
229 /* Only perform user allocations from user context */
231 return model_malloc(size);
233 return user_malloc(size);
236 return HandleEarlyAllocationRequest(size);
239 /** @brief Snapshotting free implementation for user programs */
240 void free(void * ptr)
246 mspace_free(user_snapshot_space, ptr);
250 /** @brief Snapshotting realloc implementation for user programs */
251 void *realloc(void *ptr, size_t size)
257 /** @brief Snapshotting calloc implementation for user programs */
258 void * calloc(size_t num, size_t size)
260 if (user_snapshot_space) {
261 void *tmp = user_malloc(num * size);
262 bzero(tmp, num*size);
266 void *tmp = HandleEarlyAllocationRequest(size * num);
267 memset(tmp, 0, size * num);
272 /** @brief Snapshotting allocation function for use by the Thread class only */
273 void * Thread_malloc(size_t size)
275 void *tmp = mspace_malloc(thread_snapshot_space, size);
280 /** @brief Snapshotting free function for use by the Thread class only */
281 void Thread_free(void *ptr)
286 /** @brief Snapshotting new operator for user programs */
287 void * operator new(size_t size) throw(std::bad_alloc)
292 /** @brief Snapshotting delete operator for user programs */
293 void operator delete(void *p) throw()
298 /** @brief Snapshotting new[] operator for user programs */
299 void * operator new[](size_t size) throw(std::bad_alloc)
304 /** @brief Snapshotting delete[] operator for user programs */
305 void operator delete[](void *p, size_t size)
310 #else /* !USE_MPROTECT_SNAPSHOT */
312 /** @brief Snapshotting allocation function for use by the Thread class only */
313 void * Thread_malloc(size_t size)
318 /** @brief Snapshotting free function for use by the Thread class only */
319 void Thread_free(void *ptr)
324 #endif /* !USE_MPROTECT_SNAPSHOT */