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