Remove unused functions and optimize data race checking for mem* operations
[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 #include "datarace.h"
15
16 #define REQUESTS_BEFORE_ALLOC 1024
17
18 size_t allocatedReqs[REQUESTS_BEFORE_ALLOC] = { 0 };
19 int nextRequest = 0;
20 int howManyFreed = 0;
21 mspace sStaticSpace = NULL;
22
23 /** Non-snapshotting calloc for our use. */
24 void *model_calloc(size_t count, size_t size)
25 {
26         return mspace_calloc(sStaticSpace, count, size);
27 }
28
29 /** Non-snapshotting malloc for our use. */
30 void *model_malloc(size_t size)
31 {
32         return mspace_malloc(sStaticSpace, size);
33 }
34
35 /** Non-snapshotting malloc for our use. */
36 void *model_realloc(void *ptr, size_t size)
37 {
38         return mspace_realloc(sStaticSpace, ptr, size);
39 }
40
41 /** @brief Snapshotting malloc, for use by model-checker (not user progs) */
42 void * snapshot_malloc(size_t size)
43 {
44         void *tmp = mspace_malloc(model_snapshot_space, size);
45         ASSERT(tmp);
46         return tmp;
47 }
48
49 /** @brief Snapshotting calloc, for use by model-checker (not user progs) */
50 void * snapshot_calloc(size_t count, size_t size)
51 {
52         void *tmp = mspace_calloc(model_snapshot_space, count, size);
53         ASSERT(tmp);
54         return tmp;
55 }
56
57 /** @brief Snapshotting realloc, for use by model-checker (not user progs) */
58 void *snapshot_realloc(void *ptr, size_t size)
59 {
60         void *tmp = mspace_realloc(model_snapshot_space, ptr, size);
61         ASSERT(tmp);
62         return tmp;
63 }
64
65 /** @brief Snapshotting free, for use by model-checker (not user progs) */
66 void snapshot_free(void *ptr)
67 {
68         mspace_free(model_snapshot_space, ptr);
69 }
70
71 /** Non-snapshotting free for our use. */
72 void model_free(void *ptr)
73 {
74         mspace_free(sStaticSpace, ptr);
75 }
76
77 /** Bootstrap allocation. Problem is that the dynamic linker calls require
78  *  calloc to work and calloc requires the dynamic linker to work. */
79
80 #define BOOTSTRAPBYTES 131072
81 char bootstrapmemory[BOOTSTRAPBYTES];
82 size_t offset = 0;
83
84 void * HandleEarlyAllocationRequest(size_t sz)
85 {
86         /* Align to 8 byte boundary */
87         sz = (sz + 7) & ~7;
88
89         if (sz > (BOOTSTRAPBYTES-offset)) {
90                 model_print("OUT OF BOOTSTRAP MEMORY.  Increase the size of BOOTSTRAPBYTES in mymemory.cc\n");
91                 exit(EXIT_FAILURE);
92         }
93
94         void *pointer = (void *)&bootstrapmemory[offset];
95         offset += sz;
96         return pointer;
97 }
98
99 /** @brief Global mspace reference for the model-checker's snapshotting heap */
100 mspace model_snapshot_space = NULL;
101
102 /** @brief Snapshotting allocation function for use by the Thread class only */
103 void * Thread_malloc(size_t size)
104 {
105         return snapshot_malloc(size);
106 }
107
108 /** @brief Snapshotting free function for use by the Thread class only */
109 void Thread_free(void *ptr)
110 {
111         snapshot_free(ptr);
112 }
113
114 void * (*volatile real_memcpy)(void * dst, const void *src, size_t n) = NULL;
115 void * (*volatile real_memmove)(void * dst, const void *src, size_t len) = NULL;
116 void (*volatile real_bzero)(void * dst, size_t len) = NULL;
117 void * (*volatile real_memset)(void * dst, int c, size_t len) = NULL;
118
119 void init_memory_ops()
120 {
121         if (!real_memcpy) {
122                 real_memcpy = (void * (*)(void * dst, const void *src, size_t n)) 1;
123                 real_memcpy = (void * (*)(void * dst, const void *src, size_t n))dlsym(RTLD_NEXT, "memcpy");
124         }
125         if (!real_memmove) {
126                 real_memmove = (void * (*)(void * dst, const void *src, size_t n)) 1;
127                 real_memmove = (void * (*)(void * dst, const void *src, size_t n))dlsym(RTLD_NEXT, "memmove");
128         }
129         if (!real_memset) {
130                 real_memset = (void * (*)(void * dst, int c, size_t n)) 1;
131                 real_memset = (void * (*)(void * dst, int c, size_t n))dlsym(RTLD_NEXT, "memset");
132         }
133         if (!real_bzero) {
134                 real_bzero = (void (*)(void * dst, size_t len)) 1;
135                 real_bzero = (void (*)(void * dst, size_t len))dlsym(RTLD_NEXT, "bzero");
136         }
137 }
138
139 void * memcpy(void * dst, const void * src, size_t n) {
140         if (model && !inside_model) {
141                 //model_print("memcpy size: %d\n", n);
142                 thread_id_t tid = thread_current_id();
143                 raceCheckReadMemop(tid, (void *)src, n);
144                 raceCheckWriteMemop(tid, (void *)dst, n);
145         } else if (((uintptr_t)real_memcpy) < 2) {
146                 for(uint i=0;i<n;i++) {
147                         ((volatile char *)dst)[i] = ((char *)src)[i];
148                 }
149                 return dst;
150         }
151         return real_memcpy(dst, src, n);
152 }
153
154 void * memmove(void * dst, const void * src, size_t n) {
155         if (model && !inside_model) {
156                 thread_id_t tid = thread_current_id();
157                 raceCheckReadMemop(tid, (void *)src, n);
158                 raceCheckWriteMemop(tid, (void *)dst, n);
159         } else if (((uintptr_t)real_memmove) < 2) {
160                 if (((uintptr_t)dst) < ((uintptr_t)src))
161                         for(uint i=0;i<n;i++) {
162                                 ((volatile char *)dst)[i] = ((char *)src)[i];
163                         }
164                 else
165                         for(uint i=n;i!=0; ) {
166                                 i--;
167                                 ((volatile char *)dst)[i] = ((char *)src)[i];
168                         }
169                 return dst;
170         }
171         return real_memmove(dst, src, n);
172 }
173
174 void * memset(void *dst, int c, size_t n) {
175         if (model && !inside_model) {
176                 //model_print("memset size: %d\n", n);
177                 thread_id_t tid = thread_current_id();
178                 raceCheckWriteMemop(tid, (void *)dst, n);
179         } else if (((uintptr_t)real_memset) < 2) {
180                 //stuck in dynamic linker alloc cycle...
181                 for(size_t s=0;s<n;s++) {
182                         ((volatile char *)dst)[s] = (char) c;
183                 }
184                 return dst;
185         }
186         return real_memset(dst, c, n);
187 }
188
189 void bzero(void *dst, size_t n) {
190         if (model && !inside_model) {
191                 thread_id_t tid = thread_current_id();
192                 raceCheckWriteMemop(tid, (void *)dst, n);
193         } else if (((uintptr_t)real_bzero) < 2) {
194                 for(size_t s=0;s<n;s++) {
195                         ((volatile char *)dst)[s] = 0;
196                 }
197                 return;
198         }
199         real_bzero(dst, n);
200 }