volatile support
[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 }
114
115 VOLATILESTORE(8)
116 VOLATILESTORE(16)
117 VOLATILESTORE(32)
118 VOLATILESTORE(64)
119
120 // cds atomic inits
121 #define CDSATOMICINT(size)                                              \
122         void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
123                 ensureModel();                                                      \
124                 model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
125                 *((uint ## size ## _t *)obj) = val;                                 \
126                 thread_id_t tid = thread_current()->get_id();           \
127                 for(int i=0;i < size / 8;i++) {                       \
128                         recordWrite(tid, (void *)(((char *)obj)+i));          \
129                 }                                                       \
130         }
131
132 CDSATOMICINT(8)
133 CDSATOMICINT(16)
134 CDSATOMICINT(32)
135 CDSATOMICINT(64)
136
137 // cds atomic loads
138 #define CDSATOMICLOAD(size)                                             \
139         uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
140                 ensureModel();                                                      \
141                 return (uint ## size ## _t)model->switch_to_master( \
142                         new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
143         }
144
145 CDSATOMICLOAD(8)
146 CDSATOMICLOAD(16)
147 CDSATOMICLOAD(32)
148 CDSATOMICLOAD(64)
149
150 // cds atomic stores
151 #define CDSATOMICSTORE(size)                                            \
152         void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
153                 ensureModel();                                                        \
154                 model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
155                 *((uint ## size ## _t *)obj) = val;                     \
156                 thread_id_t tid = thread_current()->get_id();           \
157                 for(int i=0;i < size / 8;i++) {                       \
158                         recordWrite(tid, (void *)(((char *)obj)+i));          \
159                 }                                                       \
160         }
161
162 CDSATOMICSTORE(8)
163 CDSATOMICSTORE(16)
164 CDSATOMICSTORE(32)
165 CDSATOMICSTORE(64)
166
167
168 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
169         ({                                                                      \
170                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
171                 uint ## size ## _t _copy = _old;                                          \
172                 uint ## size ## _t _val = val;                                            \
173                 _copy __op__ _val;                                                    \
174                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
175                 *((uint ## size ## _t *)addr) = _copy;                  \
176                 thread_id_t tid = thread_current()->get_id();           \
177                 for(int i=0;i < size / 8;i++) {                       \
178                         recordWrite(tid, (void *)(((char *)addr)+i));         \
179                 }                                                       \
180                 return _old;                                                          \
181         })
182
183 // cds atomic exchange
184 #define CDSATOMICEXCHANGE(size)                                         \
185         uint ## size ## _t cds_atomic_exchange ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
186                 _ATOMIC_RMW_( =, size, addr, val, atomic_index, position);          \
187         }
188
189 CDSATOMICEXCHANGE(8)
190 CDSATOMICEXCHANGE(16)
191 CDSATOMICEXCHANGE(32)
192 CDSATOMICEXCHANGE(64)
193
194 // cds atomic fetch add
195 #define CDSATOMICADD(size)                                              \
196         uint ## size ## _t cds_atomic_fetch_add ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
197                 _ATOMIC_RMW_( +=, size, addr, val, atomic_index, position);         \
198         }
199
200 CDSATOMICADD(8)
201 CDSATOMICADD(16)
202 CDSATOMICADD(32)
203 CDSATOMICADD(64)
204
205 // cds atomic fetch sub
206 #define CDSATOMICSUB(size)                                              \
207         uint ## size ## _t cds_atomic_fetch_sub ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
208                 _ATOMIC_RMW_( -=, size, addr, val, atomic_index, position);         \
209         }
210
211 CDSATOMICSUB(8)
212 CDSATOMICSUB(16)
213 CDSATOMICSUB(32)
214 CDSATOMICSUB(64)
215
216 // cds atomic fetch and
217 #define CDSATOMICAND(size)                                              \
218         uint ## size ## _t cds_atomic_fetch_and ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
219                 _ATOMIC_RMW_( &=, size, addr, val, atomic_index, position);         \
220         }
221
222 CDSATOMICAND(8)
223 CDSATOMICAND(16)
224 CDSATOMICAND(32)
225 CDSATOMICAND(64)
226
227 // cds atomic fetch or
228 #define CDSATOMICOR(size)                                               \
229         uint ## size ## _t cds_atomic_fetch_or ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
230                 _ATOMIC_RMW_( |=, size, addr, val, atomic_index, position);         \
231         }
232
233 CDSATOMICOR(8)
234 CDSATOMICOR(16)
235 CDSATOMICOR(32)
236 CDSATOMICOR(64)
237
238 // cds atomic fetch xor
239 #define CDSATOMICXOR(size)                                              \
240         uint ## size ## _t cds_atomic_fetch_xor ## size(void* addr, uint ## size ## _t val, int atomic_index, const char * position) { \
241                 _ATOMIC_RMW_( ^=, size, addr, val, atomic_index, position);         \
242         }
243
244 CDSATOMICXOR(8)
245 CDSATOMICXOR(16)
246 CDSATOMICXOR(32)
247 CDSATOMICXOR(64)
248
249 // cds atomic compare and exchange
250 // In order to accomodate the LLVM PASS, the return values are not true or false.
251
252 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
253 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position)                            \
254         ({                                                                                              \
255                 uint ## size ## _t _desired = desired;                                                            \
256                 uint ## size ## _t _expected = expected;                                                          \
257                 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
258                 if (_old == _expected) {                                                                    \
259                         model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
260                         *((uint ## size ## _t *)addr) = desired;                        \
261                         thread_id_t tid = thread_current()->get_id();           \
262                         for(int i=0;i < size / 8;i++) {                       \
263                                 recordWrite(tid, (void *)(((char *)addr)+i));         \
264                         }                                                       \
265                         return _expected; }                                     \
266                 else {                                                                                        \
267                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
268         })
269
270 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
271 // extracted from LLVM IR is an integer type.
272 #define CDSATOMICCASV1(size)                                            \
273         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) { \
274                 _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index_succ, position); \
275         }
276
277 CDSATOMICCASV1(8)
278 CDSATOMICCASV1(16)
279 CDSATOMICCASV1(32)
280 CDSATOMICCASV1(64)
281
282 // atomic_compare_exchange version 2
283 #define CDSATOMICCASV2(size)                                            \
284         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) { \
285                 uint ## size ## _t ret = cds_atomic_compare_exchange ## size ## _v1(addr, *expected, desired, atomic_index_succ, atomic_index_fail, position); \
286                 if (ret == *expected) {return true;} else {return false;}               \
287         }
288
289 CDSATOMICCASV2(8)
290 CDSATOMICCASV2(16)
291 CDSATOMICCASV2(32)
292 CDSATOMICCASV2(64)
293
294 // cds atomic thread fence
295
296 void cds_atomic_thread_fence(int atomic_index, const char * position) {
297         model->switch_to_master(
298                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
299                 );
300 }
301
302 /*
303  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
304         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
305                 __typeof__(__e__) __q__ = (__e__);                            \
306                 __typeof__(__m__) __v__ = (__m__);                            \
307                 bool __r__;                                                   \
308                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
309                 if (__t__ == * __q__ ) {                                      \
310                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
311                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
312                 __r__; })
313
314  #define _ATOMIC_FENCE_( __x__ ) \
315         ({ model_fence_action(__x__);})
316  */
317
318 /*
319
320  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
321         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
322         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
323         __typeof__(__m__) __v__ = (__m__);                                    \
324         __typeof__((__a__)->__f__) __copy__= __old__;                         \
325         __copy__ __o__ __v__;                                                 \
326         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
327         __old__ = __old__;  Silence clang (-Wunused-value)                    \
328          })
329  */
330
331 void cds_func_entry(const char * funcName) {
332         if (!model) return;
333
334         Thread * th = thread_current();
335         uint32_t func_id;
336
337         ModelHistory *history = model->get_history();
338         if ( !history->getFuncMap()->contains(funcName) ) {
339                 // add func id to func map
340                 func_id = history->get_func_counter();
341                 history->incr_func_counter();
342                 history->getFuncMap()->put(funcName, func_id);
343
344                 // add func id to reverse func map
345                 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
346                 if ( func_map_rev->size() <= func_id )
347                         func_map_rev->resize( func_id + 1 );
348                 func_map_rev->at(func_id) = funcName;
349         } else {
350                 func_id = history->getFuncMap()->get(funcName);
351         }
352
353         history->enter_function(func_id, th->get_id());
354 }
355
356 void cds_func_exit(const char * funcName) {
357         if (!model) return;
358
359         Thread * th = thread_current();
360         uint32_t func_id;
361
362         ModelHistory *history = model->get_history();
363         func_id = history->getFuncMap()->get(funcName);
364
365         /* func_id not found; this could happen in the case where a function calls cds_func_entry
366          * when the model has been defined yet, but then an atomic inside the function initializes
367          * the model. And then cds_func_exit is called upon the function exiting.
368          */
369         if (func_id == 0)
370                 return;
371
372         history->exit_function(func_id, th->get_id());
373 }