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 };
29 #if !USE_MPROTECT_SNAPSHOT
30 static mspace sStaticSpace = NULL;
33 /** Non-snapshotting calloc for our use. */
34 void *model_calloc(size_t count, size_t size)
36 #if USE_MPROTECT_SNAPSHOT
37 static void *(*callocp)(size_t count, size_t size) = NULL;
41 /* get address of libc malloc */
43 callocp = (void * (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
44 if ((error = dlerror()) != NULL) {
49 ptr = callocp(count, size);
53 sStaticSpace = create_shared_mspace();
54 return mspace_calloc(sStaticSpace, count, size);
58 /** Non-snapshotting malloc for our use. */
59 void *model_malloc(size_t size)
61 #if USE_MPROTECT_SNAPSHOT
62 static void *(*mallocp)(size_t size) = NULL;
66 /* get address of libc malloc */
68 mallocp = (void * (*)(size_t))dlsym(RTLD_NEXT, "malloc");
69 if ((error = dlerror()) != NULL) {
78 sStaticSpace = create_shared_mspace();
79 return mspace_malloc(sStaticSpace, size);
83 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
84 void *model_realloc(void *ptr, size_t size)
86 #if USE_MPROTECT_SNAPSHOT
87 static void *(*reallocp)(void *ptr, size_t size) = NULL;
91 /* get address of libc malloc */
93 reallocp = (void * (*)(void *,size_t))dlsym(RTLD_NEXT, "realloc");
94 if ((error = dlerror()) != NULL) {
99 tmpptr = reallocp(ptr, size);
103 sStaticSpace = create_shared_mspace();
104 return mspace_realloc(sStaticSpace, ptr, size);
108 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
109 void * snapshot_malloc(size_t size)
111 void *tmp = mspace_malloc(model_snapshot_space, size);
116 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
117 void * snapshot_calloc(size_t count, size_t size)
119 void *tmp = mspace_calloc(model_snapshot_space, count, size);
124 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
125 void *snapshot_realloc(void *ptr, size_t size)
127 void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
132 /** @brief Snapshotting free, for use by model-checker (not user progs) */
133 void snapshot_free(void *ptr)
135 mspace_free(model_snapshot_space, ptr);
138 /** Non-snapshotting free for our use. */
139 void model_free(void *ptr)
141 #if USE_MPROTECT_SNAPSHOT
142 static void (*freep)(void *);
145 /* get address of libc free */
147 freep = (void (*)(void *))dlsym(RTLD_NEXT, "free");
148 if ((error = dlerror()) != NULL) {
149 fputs(error, stderr);
155 mspace_free(sStaticSpace, ptr);
159 /** Bootstrap allocation. Problem is that the dynamic linker calls require
160 * calloc to work and calloc requires the dynamic linker to work. */
162 #define BOOTSTRAPBYTES 4096
163 char bootstrapmemory[BOOTSTRAPBYTES];
166 void * HandleEarlyAllocationRequest(size_t sz)
168 /* Align to 8 byte boundary */
171 if (sz > (BOOTSTRAPBYTES-offset)) {
172 model_print("OUT OF BOOTSTRAP MEMORY\n");
176 void *pointer = (void *)&bootstrapmemory[offset];
181 /** @brief Global mspace reference for the model-checker's snapshotting heap */
182 mspace model_snapshot_space = NULL;
184 #if USE_MPROTECT_SNAPSHOT
186 /** @brief Global mspace reference for the user's snapshotting heap */
187 void * user_snapshot_space = NULL;
188 mspace thread_snapshot_space = NULL;
190 struct snapshot_heap_data * snapshot_struct;
192 /** Check whether this is bootstrapped memory that we should not free */
193 static bool DontFree(void *ptr)
195 return (ptr >= (&bootstrapmemory[0]) && ptr < (&bootstrapmemory[BOOTSTRAPBYTES]));
199 static void * user_malloc(size_t size) {
200 return model->get_execution()->alloc(size);
204 * @brief The allocator function for "user" allocation
206 * Should only be used for allocations which will not disturb the allocation
207 * patterns of a user thread.
209 void * real_user_malloc(size_t size)
211 size=(size+7)&~((size_t)7);
212 void *tmp = snapshot_struct->allocation_ptr;
213 snapshot_struct->allocation_ptr = (void *)((char *) snapshot_struct->allocation_ptr +size);
215 ASSERT(snapshot_struct->allocation_ptr <= snapshot_struct->top_ptr);
220 * @brief Snapshotting malloc implementation for user programs
222 * Do NOT call this function from a model-checker context. Doing so may disrupt
223 * the allocation patterns of a user thread.
225 void *malloc(size_t size)
227 if (user_snapshot_space) {
228 /* Only perform user allocations from user context */
229 return user_malloc(size);
231 return HandleEarlyAllocationRequest(size);
234 /** @brief Snapshotting free implementation for user programs */
235 void free(void * ptr)
238 mspace_free(user_snapshot_space, ptr);
241 /** @brief Snapshotting realloc implementation for user programs */
242 void *realloc(void *ptr, size_t size)
248 /** @brief Snapshotting calloc implementation for user programs */
249 void * calloc(size_t num, size_t size)
251 if (user_snapshot_space) {
252 void *tmp = user_malloc(num * size);
253 bzero(tmp, num*size);
257 void *tmp = HandleEarlyAllocationRequest(size * num);
258 memset(tmp, 0, size * num);
263 /** @brief Snapshotting allocation function for use by the Thread class only */
264 void * Thread_malloc(size_t size)
266 void *tmp = mspace_malloc(thread_snapshot_space, size);
271 /** @brief Snapshotting free function for use by the Thread class only */
272 void Thread_free(void *ptr)
277 /** @brief Snapshotting new operator for user programs */
278 void * operator new(size_t size) throw(std::bad_alloc)
283 /** @brief Snapshotting delete operator for user programs */
284 void operator delete(void *p) throw()
289 /** @brief Snapshotting new[] operator for user programs */
290 void * operator new[](size_t size) throw(std::bad_alloc)
295 /** @brief Snapshotting delete[] operator for user programs */
296 void operator delete[](void *p, size_t size)
301 #else /* !USE_MPROTECT_SNAPSHOT */
303 /** @brief Snapshotting allocation function for use by the Thread class only */
304 void * Thread_malloc(size_t size)
309 /** @brief Snapshotting free function for use by the Thread class only */
310 void Thread_free(void *ptr)
315 #endif /* !USE_MPROTECT_SNAPSHOT */