Make everything do stores + add more buffer for printing
[c11tester.git] / cmodelint.cc
1 #include <stdio.h>
2 #include <string>
3
4 #include "model.h"
5 #include "execution.h"
6 #include "action.h"
7 #include "history.h"
8 #include "cmodelint.h"
9 #include "snapshot-interface.h"
10 #include "threads-model.h"
11 #include "datarace.h"
12
13 memory_order orders[8] = {
14         memory_order_relaxed, memory_order_consume, memory_order_acquire,
15         memory_order_release, memory_order_acq_rel, memory_order_seq_cst,
16 };
17
18 static void ensureModel() {
19         if (!model) {
20                 snapshot_system_init(10000, 1024, 1024, 40000);
21                 model = new ModelChecker();
22                 model->startChecker();
23         }
24 }
25
26 /** Performs a read action.*/
27 uint64_t model_read_action(void * obj, memory_order ord) {
28         return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
29 }
30
31 /** Performs a write action.*/
32 void model_write_action(void * obj, memory_order ord, uint64_t val) {
33         model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
34 }
35
36 /** Performs an init action. */
37 void model_init_action(void * obj, uint64_t val) {
38         model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
39 }
40
41 /**
42  * Performs the read part of a RMW action. The next action must either be the
43  * write part of the RMW action or an explicit close out of the RMW action w/o
44  * a write.
45  */
46 uint64_t model_rmwr_action(void *obj, memory_order ord) {
47         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
48 }
49
50 /**
51  * Performs the read part of a RMW CAS action. The next action must
52  * either be the write part of the RMW action or an explicit close out
53  * of the RMW action w/o a write.
54  */
55 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
56         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
57 }
58
59
60 /** Performs the write part of a RMW action. */
61 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
62         model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
63 }
64
65 /** Closes out a RMW action without doing a write. */
66 void model_rmwc_action(void *obj, memory_order ord) {
67         model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
68 }
69
70 /** Issues a fence operation. */
71 void model_fence_action(memory_order ord) {
72         model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
73 }
74
75 /* ---  helper functions --- */
76 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
77         ensureModel();
78         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size));
79 }
80
81 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
82         ensureModel();
83         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj));
84 }
85
86 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
87         ensureModel();
88         model->switch_to_master(new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val));
89 }
90
91 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
92         ensureModel();
93         model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
94 }
95
96 // cds volatile loads
97 #define VOLATILELOAD(size) \
98         uint ## size ## _t cds_volatile_load ## size(void * obj, const char * position) { \
99                 ensureModel();                                                      \
100                 return (uint ## size ## _t)model->switch_to_master(new ModelAction(ATOMIC_READ, position, memory_order_relaxed, obj)); \
101         }
102
103 VOLATILELOAD(8)
104 VOLATILELOAD(16)
105 VOLATILELOAD(32)
106 VOLATILELOAD(64)
107
108 // cds volatile stores
109 #define VOLATILESTORE(size) \
110         void cds_volatile_store ## size (void * obj, uint ## size ## _t val, const char * position) { \
111                 ensureModel();                                                      \
112                 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, memory_order_relaxed, obj, (uint64_t) val)); \
113                 *((volatile uint ## size ## _t *)obj) = val;            \
114         }
115
116 VOLATILESTORE(8)
117 VOLATILESTORE(16)
118 VOLATILESTORE(32)
119 VOLATILESTORE(64)
120
121 // cds atomic inits
122 #define CDSATOMICINT(size)                                              \
123         void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
124                 ensureModel();                                                      \
125                 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
126                 *((volatile uint ## size ## _t *)obj) = val;                                 \
127                 thread_id_t tid = thread_current()->get_id();           \
128                 for(int i=0;i < size / 8;i++) {                       \
129                         recordWrite(tid, (void *)(((char *)obj)+i));          \
130                 }                                                       \
131         }
132
133 CDSATOMICINT(8)
134 CDSATOMICINT(16)
135 CDSATOMICINT(32)
136 CDSATOMICINT(64)
137
138 // cds atomic loads
139 #define CDSATOMICLOAD(size)                                             \
140         uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
141                 ensureModel();                                                      \
142                 return (uint ## size ## _t)model->switch_to_master( \
143                         new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
144         }
145
146 CDSATOMICLOAD(8)
147 CDSATOMICLOAD(16)
148 CDSATOMICLOAD(32)
149 CDSATOMICLOAD(64)
150
151 // cds atomic stores
152 #define CDSATOMICSTORE(size)                                            \
153         void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
154                 ensureModel();                                                        \
155                 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
156                 *((volatile uint ## size ## _t *)obj) = val;                     \
157                 thread_id_t tid = thread_current()->get_id();           \
158                 for(int i=0;i < size / 8;i++) {                       \
159                         recordWrite(tid, (void *)(((char *)obj)+i));          \
160                 }                                                       \
161         }
162
163 CDSATOMICSTORE(8)
164 CDSATOMICSTORE(16)
165 CDSATOMICSTORE(32)
166 CDSATOMICSTORE(64)
167
168
169 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
170         ({                                                                      \
171                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
172                 uint ## size ## _t _copy = _old;                                          \
173                 uint ## size ## _t _val = val;                                            \
174                 _copy __op__ _val;                                                    \
175                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
176                 *((volatile uint ## size ## _t *)addr) = _copy;                  \
177                 thread_id_t tid = thread_current()->get_id();           \
178                 for(int i=0;i < size / 8;i++) {                       \
179                         recordWrite(tid, (void *)(((char *)addr)+i));         \
180                 }                                                       \
181                 return _old;                                                          \
182         })
183
184 // cds atomic exchange
185 #define CDSATOMICEXCHANGE(size)                                         \
186         uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
187                 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position);          \
188         }
189
190 CDSATOMICEXCHANGE(8)
191 CDSATOMICEXCHANGE(16)
192 CDSATOMICEXCHANGE(32)
193 CDSATOMICEXCHANGE(64)
194
195 // cds atomic fetch add
196 #define CDSATOMICADD(size)                                              \
197         uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
198                 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position);         \
199         }
200
201 CDSATOMICADD(8)
202 CDSATOMICADD(16)
203 CDSATOMICADD(32)
204 CDSATOMICADD(64)
205
206 // cds atomic fetch sub
207 #define CDSATOMICSUB(size)                                              \
208         uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
209                 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position);         \
210         }
211
212 CDSATOMICSUB(8)
213 CDSATOMICSUB(16)
214 CDSATOMICSUB(32)
215 CDSATOMICSUB(64)
216
217 // cds atomic fetch and
218 #define CDSATOMICAND(size)                                              \
219         uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
220                 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position);         \
221         }
222
223 CDSATOMICAND(8)
224 CDSATOMICAND(16)
225 CDSATOMICAND(32)
226 CDSATOMICAND(64)
227
228 // cds atomic fetch or
229 #define CDSATOMICOR(size)                                               \
230         uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
231                 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position);         \
232         }
233
234 CDSATOMICOR(8)
235 CDSATOMICOR(16)
236 CDSATOMICOR(32)
237 CDSATOMICOR(64)
238
239 // cds atomic fetch xor
240 #define CDSATOMICXOR(size)                                              \
241         uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
242                 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position);         \
243         }
244
245 CDSATOMICXOR(8)
246 CDSATOMICXOR(16)
247 CDSATOMICXOR(32)
248 CDSATOMICXOR(64)
249
250 // cds atomic compare and exchange
251 // In order to accomodate the LLVM PASS, the return values are not true or false.
252
253 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
254 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position)                            \
255         ({                                                                                              \
256                 uint ## size ## _t _desired = desired;                                                            \
257                 uint ## size ## _t _expected = expected;                                                          \
258                 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
259                 if (_old == _expected) {                                                                    \
260                         model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
261                         *((volatile uint ## size ## _t *)addr) = desired;                        \
262                         thread_id_t tid = thread_current()->get_id();           \
263                         for(int i=0;i < size / 8;i++) {                       \
264                                 recordWrite(tid, (void *)(((char *)addr)+i));         \
265                         }                                                       \
266                         return _expected; }                                     \
267                 else {                                                                                        \
268                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
269         })
270
271 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
272 // extracted from LLVM IR is an integer type.
273 #define CDSATOMICCASV1(size)                                            \
274         uint ## size ## _t cds_atomic_compare_exchange ## size ## _v1(void* addr, uint ## size ## _t expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
275                 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
276         }
277
278 CDSATOMICCASV1(8)
279 CDSATOMICCASV1(16)
280 CDSATOMICCASV1(32)
281 CDSATOMICCASV1(64)
282
283 // atomic_compare_exchange version 2
284 #define CDSATOMICCASV2(size)                                            \
285         bool cds_atomic_compare_exchange ## size ## _v2(void* addr, uint ## size ## _t* expected, uint ## size ## _t desired, int atomic_index_succ, int atomic_index_fail, const char *position) { \
286                 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
287                 if (ret == *expected) {return true;} else {return false;}               \
288         }
289
290 CDSATOMICCASV2(8)
291 CDSATOMICCASV2(16)
292 CDSATOMICCASV2(32)
293 CDSATOMICCASV2(64)
294
295 // cds atomic thread fence
296
297 void cds_atomic_thread_fence(int atomic_index, const char * position) {
298         model->switch_to_master(
299                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
300                 );
301 }
302
303 /*
304  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
305         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
306                 __typeof__(__e__) __q__ = (__e__);                            \
307                 __typeof__(__m__) __v__ = (__m__);                            \
308                 bool __r__;                                                   \
309                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
310                 if (__t__ == * __q__ ) {                                      \
311                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
312                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
313                 __r__; })
314
315  #define _ATOMIC_FENCE_( __x__ ) \
316         ({ model_fence_action(__x__);})
317  */
318
319 /*
320
321  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
322         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
323         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
324         __typeof__(__m__) __v__ = (__m__);                                    \
325         __typeof__((__a__)->__f__) __copy__= __old__;                         \
326         __copy__ __o__ __v__;                                                 \
327         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
328         __old__ = __old__;  Silence clang (-Wunused-value)                    \
329          })
330  */
331
332 void cds_func_entry(const char * funcName) {
333         if (!model) return;
334
335         Thread * th = thread_current();
336         uint32_t func_id;
337
338         ModelHistory *history = model->get_history();
339         if ( !history->getFuncMap()->contains(funcName) ) {
340                 // add func id to func map
341                 func_id = history->get_func_counter();
342                 history->incr_func_counter();
343                 history->getFuncMap()->put(funcName, func_id);
344
345                 // add func id to reverse func map
346                 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
347                 if ( func_map_rev->size() <= func_id )
348                         func_map_rev->resize( func_id + 1 );
349                 func_map_rev->at(func_id) = funcName;
350         } else {
351                 func_id = history->getFuncMap()->get(funcName);
352         }
353
354         history->enter_function(func_id, th->get_id());
355 }
356
357 void cds_func_exit(const char * funcName) {
358         if (!model) return;
359
360         Thread * th = thread_current();
361         uint32_t func_id;
362
363         ModelHistory *history = model->get_history();
364         func_id = history->getFuncMap()->get(funcName);
365
366         /* func_id not found; this could happen in the case where a function calls cds_func_entry
367          * when the model has been defined yet, but then an atomic inside the function initializes
368          * the model. And then cds_func_exit is called upon the function exiting.
369          */
370         if (func_id == 0)
371                 return;
372
373         history->exit_function(func_id, th->get_id());
374 }