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