Commit state of repository at time of OOPSLA 2015 submission.
[satcheck.git] / mymemory.cc
1 /*      Copyright (c) 2015 Regents of the University of California
2  *
3  *      Author: Brian Demsky <bdemsky@uci.edu>
4  *
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.
8  */
9
10 #include <stdlib.h>
11 #include <stdio.h>
12 #include <dlfcn.h>
13 #include <unistd.h>
14 #include <string.h>
15 #include <new>
16
17 #include "mymemory.h"
18 #include "snapshot.h"
19 #include "common.h"
20 #include "threads-model.h"
21 #include "model.h"
22 #include "mcexecution.h"
23
24 #define REQUESTS_BEFORE_ALLOC 1024
25
26 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
27 int nextRequest = 0;
28 int howManyFreed = 0;
29 #if !USE_MPROTECT_SNAPSHOT
30 static mspace sStaticSpace = NULL;
31 #endif
32
33 /** Non-snapshotting calloc for our use. */
34 void *model_calloc(size_t count, size_t size)
35 {
36 #if USE_MPROTECT_SNAPSHOT
37         static void *(*callocp)(size_t count, size_t size) = NULL;
38         char *error;
39         void *ptr;
40
41         /* get address of libc malloc */
42         if (!callocp) {
43                 callocp = (void * (*)(size_t, size_t))dlsym(RTLD_NEXT, "calloc");
44                 if ((error = dlerror()) != NULL) {
45                         fputs(error, stderr);
46                         exit(EXIT_FAILURE);
47                 }
48         }
49         ptr = callocp(count, size);
50         return ptr;
51 #else
52         if (!sStaticSpace)
53                 sStaticSpace = create_shared_mspace();
54         return mspace_calloc(sStaticSpace, count, size);
55 #endif
56 }
57
58 /** Non-snapshotting malloc for our use. */
59 void *model_malloc(size_t size)
60 {
61 #if USE_MPROTECT_SNAPSHOT
62         static void *(*mallocp)(size_t size) = NULL;
63         char *error;
64         void *ptr;
65
66         /* get address of libc malloc */
67         if (!mallocp) {
68                 mallocp = (void * (*)(size_t))dlsym(RTLD_NEXT, "malloc");
69                 if ((error = dlerror()) != NULL) {
70                         fputs(error, stderr);
71                         exit(EXIT_FAILURE);
72                 }
73         }
74         ptr = mallocp(size);
75         return ptr;
76 #else
77         if (!sStaticSpace)
78                 sStaticSpace = create_shared_mspace();
79         return mspace_malloc(sStaticSpace, size);
80 #endif
81 }
82
83 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
84 void *model_realloc(void *ptr, size_t size)
85 {
86 #if USE_MPROTECT_SNAPSHOT
87         static void *(*reallocp)(void *ptr, size_t size) = NULL;
88         char *error;
89         void *tmpptr;
90
91         /* get address of libc malloc */
92         if (!reallocp) {
93                 reallocp = (void * (*)(void *,size_t))dlsym(RTLD_NEXT, "realloc");
94                 if ((error = dlerror()) != NULL) {
95                         fputs(error, stderr);
96                         exit(EXIT_FAILURE);
97                 }
98         }
99         tmpptr = reallocp(ptr, size);
100         return tmpptr;
101 #else
102         if (!sStaticSpace)
103                 sStaticSpace = create_shared_mspace();
104         return mspace_realloc(sStaticSpace, ptr, size);
105 #endif
106 }
107
108 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
109 void * snapshot_malloc(size_t size)
110 {
111         void *tmp = mspace_malloc(model_snapshot_space, size);
112         ASSERT(tmp);
113         return tmp;
114 }
115
116 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
117 void * snapshot_calloc(size_t count, size_t size)
118 {
119         void *tmp = mspace_calloc(model_snapshot_space, count, size);
120         ASSERT(tmp);
121         return tmp;
122 }
123
124 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
125 void *snapshot_realloc(void *ptr, size_t size)
126 {
127         void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
128         ASSERT(tmp);
129         return tmp;
130 }
131
132 /** @brief Snapshotting free, for use by model-checker (not user progs) */
133 void snapshot_free(void *ptr)
134 {
135         mspace_free(model_snapshot_space, ptr);
136 }
137
138 /** Non-snapshotting free for our use. */
139 void model_free(void *ptr)
140 {
141 #if USE_MPROTECT_SNAPSHOT
142         static void (*freep)(void *);
143         char *error;
144
145         /* get address of libc free */
146         if (!freep) {
147                 freep = (void (*)(void *))dlsym(RTLD_NEXT, "free");
148                 if ((error = dlerror()) != NULL) {
149                         fputs(error, stderr);
150                         exit(EXIT_FAILURE);
151                 }
152         }
153         freep(ptr);
154 #else
155         mspace_free(sStaticSpace, ptr);
156 #endif
157 }
158
159 /** Bootstrap allocation. Problem is that the dynamic linker calls require
160  *  calloc to work and calloc requires the dynamic linker to work. */
161
162 #define BOOTSTRAPBYTES 4096
163 char bootstrapmemory[BOOTSTRAPBYTES];
164 size_t offset = 0;
165
166 void * HandleEarlyAllocationRequest(size_t sz)
167 {
168         /* Align to 8 byte boundary */
169         sz = (sz + 7) & ~7;
170
171         if (sz > (BOOTSTRAPBYTES-offset)) {
172                 model_print("OUT OF BOOTSTRAP MEMORY\n");
173                 exit(EXIT_FAILURE);
174         }
175
176         void *pointer = (void *)&bootstrapmemory[offset];
177         offset += sz;
178         return pointer;
179 }
180
181 /** @brief Global mspace reference for the model-checker's snapshotting heap */
182 mspace model_snapshot_space = NULL;
183
184 #if USE_MPROTECT_SNAPSHOT
185
186 /** @brief Global mspace reference for the user's snapshotting heap */
187 void * user_snapshot_space = NULL;
188 mspace thread_snapshot_space = NULL;
189
190 struct snapshot_heap_data * snapshot_struct;
191
192 /** Check whether this is bootstrapped memory that we should not free */
193 static bool DontFree(void *ptr)
194 {
195         return (ptr >= (&bootstrapmemory[0]) && ptr < (&bootstrapmemory[BOOTSTRAPBYTES]));
196 }
197
198
199 static void * user_malloc(size_t size) {
200         return model->get_execution()->alloc(size);
201 }
202
203 /**
204  * @brief The allocator function for "user" allocation
205  *
206  * Should only be used for allocations which will not disturb the allocation
207  * patterns of a user thread.
208  */
209 void * real_user_malloc(size_t size)
210 {
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);
214         
215         ASSERT(snapshot_struct->allocation_ptr <= snapshot_struct->top_ptr);
216         return tmp;
217 }
218
219 /**
220  * @brief Snapshotting malloc implementation for user programs
221  *
222  * Do NOT call this function from a model-checker context. Doing so may disrupt
223  * the allocation patterns of a user thread.
224  */
225 void *malloc(size_t size)
226 {
227         if (user_snapshot_space) {
228                 /* Only perform user allocations from user context */
229                 return user_malloc(size);
230         } else
231                 return HandleEarlyAllocationRequest(size);
232 }
233
234 /** @brief Snapshotting free implementation for user programs */
235 void free(void * ptr)
236 {
237         if (!DontFree(ptr))
238                 mspace_free(user_snapshot_space, ptr);
239 }
240
241 /** @brief Snapshotting realloc implementation for user programs */
242 void *realloc(void *ptr, size_t size)
243 {
244         ASSERT(false);
245         return NULL;
246 }
247
248 /** @brief Snapshotting calloc implementation for user programs */
249 void * calloc(size_t num, size_t size)
250 {
251         if (user_snapshot_space) {
252                 void *tmp = user_malloc(num * size);
253                 bzero(tmp, num*size);
254                 ASSERT(tmp);
255                 return tmp;
256         } else {
257                 void *tmp = HandleEarlyAllocationRequest(size * num);
258                 memset(tmp, 0, size * num);
259                 return tmp;
260         }
261 }
262
263 /** @brief Snapshotting allocation function for use by the Thread class only */
264 void * Thread_malloc(size_t size)
265 {
266         void *tmp = mspace_malloc(thread_snapshot_space, size);
267         ASSERT(tmp);
268         return tmp;
269 }
270
271 /** @brief Snapshotting free function for use by the Thread class only */
272 void Thread_free(void *ptr)
273 {
274         free(ptr);
275 }
276
277 /** @brief Snapshotting new operator for user programs */
278 void * operator new(size_t size) throw(std::bad_alloc)
279 {
280         return malloc(size);
281 }
282
283 /** @brief Snapshotting delete operator for user programs */
284 void operator delete(void *p) throw()
285 {
286         free(p);
287 }
288
289 /** @brief Snapshotting new[] operator for user programs */
290 void * operator new[](size_t size) throw(std::bad_alloc)
291 {
292         return malloc(size);
293 }
294
295 /** @brief Snapshotting delete[] operator for user programs */
296 void operator delete[](void *p, size_t size)
297 {
298         free(p);
299 }
300
301 #else /* !USE_MPROTECT_SNAPSHOT */
302
303 /** @brief Snapshotting allocation function for use by the Thread class only */
304 void * Thread_malloc(size_t size)
305 {
306         return malloc(size);
307 }
308
309 /** @brief Snapshotting free function for use by the Thread class only */
310 void Thread_free(void *ptr)
311 {
312         free(ptr);
313 }
314
315 #endif /* !USE_MPROTECT_SNAPSHOT */