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