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