add function calls for volatile loads and stores
[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
12 memory_order orders[8] = {
13         memory_order_relaxed, memory_order_consume, memory_order_acquire,
14         memory_order_release, memory_order_acq_rel, memory_order_seq_cst,
15         volatile_order
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 uint8_t cds_volatile_load8(void * obj, int atomic_index, const char * position) {
98         ensureModel();
99         return (uint8_t) model->switch_to_master(
100                 new ModelAction(VOLATILE_READ, position, orders[atomic_index], obj));
101 }
102 uint16_t cds_volatile_load16(void * obj, int atomic_index, const char * position) {
103         ensureModel();
104         return (uint16_t) model->switch_to_master(
105                 new ModelAction(VOLATILE_READ, position, orders[atomic_index], obj));
106 }
107 uint32_t cds_volatile_load32(void * obj, int atomic_index, const char * position) {
108         ensureModel();
109         return (uint32_t) model->switch_to_master(
110                 new ModelAction(VOLATILE_READ, position, orders[atomic_index], obj)
111                 );
112 }
113 uint64_t cds_volatile_load64(void * obj, int atomic_index, const char * position) {
114         ensureModel();
115         return model->switch_to_master(
116                 new ModelAction(VOLATILE_READ, position, orders[atomic_index], obj));
117 }
118
119 // cds volatile stores
120 void cds_volatile_store8(void * obj, uint8_t val, int atomic_index, const char * position) {
121         ensureModel();
122         model->switch_to_master(new ModelAction(VOLATILE_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
123 }
124 void cds_volatile_store16(void * obj, uint16_t val, int atomic_index, const char * position) {
125         ensureModel();
126         model->switch_to_master(new ModelAction(VOLATILE_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
127 }
128 void cds_volatile_store32(void * obj, uint32_t val, int atomic_index, const char * position) {
129         ensureModel();
130         model->switch_to_master(new ModelAction(VOLATILE_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
131 }
132 void cds_volatile_store64(void * obj, uint64_t val, int atomic_index, const char * position) {
133         ensureModel();
134         model->switch_to_master(new ModelAction(VOLATILE_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
135 }
136
137 // cds atomic inits
138 void cds_atomic_init8(void * obj, uint8_t val, const char * position) {
139         ensureModel();
140         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
141 }
142 void cds_atomic_init16(void * obj, uint16_t val, const char * position) {
143         ensureModel();
144         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
145 }
146 void cds_atomic_init32(void * obj, uint32_t val, const char * position) {
147         ensureModel();
148         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
149 }
150 void cds_atomic_init64(void * obj, uint64_t val, const char * position) {
151         ensureModel();
152         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, val));
153 }
154
155
156 // cds atomic loads
157 uint8_t cds_atomic_load8(void * obj, int atomic_index, const char * position) {
158         ensureModel();
159         return (uint8_t) model->switch_to_master(
160                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
161 }
162 uint16_t cds_atomic_load16(void * obj, int atomic_index, const char * position) {
163         ensureModel();
164         return (uint16_t) model->switch_to_master(
165                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
166 }
167 uint32_t cds_atomic_load32(void * obj, int atomic_index, const char * position) {
168         ensureModel();
169         return (uint32_t) model->switch_to_master(
170                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)
171                 );
172 }
173 uint64_t cds_atomic_load64(void * obj, int atomic_index, const char * position) {
174         ensureModel();
175         return model->switch_to_master(
176                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
177 }
178
179 // cds atomic stores
180 void cds_atomic_store8(void * obj, uint8_t val, int atomic_index, const char * position) {
181         ensureModel();
182         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
183 }
184 void cds_atomic_store16(void * obj, uint16_t val, int atomic_index, const char * position) {
185         ensureModel();
186         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
187 }
188 void cds_atomic_store32(void * obj, uint32_t val, int atomic_index, const char * position) {
189         ensureModel();
190         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
191 }
192 void cds_atomic_store64(void * obj, uint64_t val, int atomic_index, const char * position) {
193         ensureModel();
194         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
195 }
196
197 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
198         ({                                                                      \
199                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
200                 uint ## size ## _t _copy = _old;                                          \
201                 uint ## size ## _t _val = val;                                            \
202                 _copy __op__ _val;                                                    \
203                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
204                 return _old;                                                          \
205         })
206
207 // cds atomic exchange
208 uint8_t cds_atomic_exchange8(void* addr, uint8_t val, int atomic_index, const char * position) {
209         _ATOMIC_RMW_( =, 8, addr, val, atomic_index, position);
210 }
211 uint16_t cds_atomic_exchange16(void* addr, uint16_t val, int atomic_index, const char * position) {
212         _ATOMIC_RMW_( =, 16, addr, val, atomic_index, position);
213 }
214 uint32_t cds_atomic_exchange32(void* addr, uint32_t val, int atomic_index, const char * position) {
215         _ATOMIC_RMW_( =, 32, addr, val, atomic_index, position);
216 }
217 uint64_t cds_atomic_exchange64(void* addr, uint64_t val, int atomic_index, const char * position) {
218         _ATOMIC_RMW_( =, 64, addr, val, atomic_index, position);
219 }
220
221 // cds atomic fetch add
222 uint8_t cds_atomic_fetch_add8(void* addr, uint8_t val, int atomic_index, const char * position) {
223         _ATOMIC_RMW_( +=, 8, addr, val, atomic_index, position);
224 }
225 uint16_t cds_atomic_fetch_add16(void* addr, uint16_t val, int atomic_index, const char * position) {
226         _ATOMIC_RMW_( +=, 16, addr, val, atomic_index, position);
227 }
228 uint32_t cds_atomic_fetch_add32(void* addr, uint32_t val, int atomic_index, const char * position) {
229         _ATOMIC_RMW_( +=, 32, addr, val, atomic_index, position);
230 }
231 uint64_t cds_atomic_fetch_add64(void* addr, uint64_t val, int atomic_index, const char * position) {
232         _ATOMIC_RMW_( +=, 64, addr, val, atomic_index, position);
233 }
234
235 // cds atomic fetch sub
236 uint8_t cds_atomic_fetch_sub8(void* addr, uint8_t val, int atomic_index, const char * position) {
237         _ATOMIC_RMW_( -=, 8, addr, val, atomic_index, position);
238 }
239 uint16_t cds_atomic_fetch_sub16(void* addr, uint16_t val, int atomic_index, const char * position) {
240         _ATOMIC_RMW_( -=, 16, addr, val, atomic_index, position);
241 }
242 uint32_t cds_atomic_fetch_sub32(void* addr, uint32_t val, int atomic_index, const char * position) {
243         _ATOMIC_RMW_( -=, 32, addr, val, atomic_index, position);
244 }
245 uint64_t cds_atomic_fetch_sub64(void* addr, uint64_t val, int atomic_index, const char * position) {
246         _ATOMIC_RMW_( -=, 64, addr, val, atomic_index, position);
247 }
248
249 // cds atomic fetch and
250 uint8_t cds_atomic_fetch_and8(void* addr, uint8_t val, int atomic_index, const char * position) {
251         _ATOMIC_RMW_( &=, 8, addr, val, atomic_index, position);
252 }
253 uint16_t cds_atomic_fetch_and16(void* addr, uint16_t val, int atomic_index, const char * position) {
254         _ATOMIC_RMW_( &=, 16, addr, val, atomic_index, position);
255 }
256 uint32_t cds_atomic_fetch_and32(void* addr, uint32_t val, int atomic_index, const char * position) {
257         _ATOMIC_RMW_( &=, 32, addr, val, atomic_index, position);
258 }
259 uint64_t cds_atomic_fetch_and64(void* addr, uint64_t val, int atomic_index, const char * position) {
260         _ATOMIC_RMW_( &=, 64, addr, val, atomic_index, position);
261 }
262
263 // cds atomic fetch or
264 uint8_t cds_atomic_fetch_or8(void* addr, uint8_t val, int atomic_index, const char * position) {
265         _ATOMIC_RMW_( |=, 8, addr, val, atomic_index, position);
266 }
267 uint16_t cds_atomic_fetch_or16(void* addr, uint16_t val, int atomic_index, const char * position) {
268         _ATOMIC_RMW_( |=, 16, addr, val, atomic_index, position);
269 }
270 uint32_t cds_atomic_fetch_or32(void* addr, uint32_t val, int atomic_index, const char * position) {
271         _ATOMIC_RMW_( |=, 32, addr, val, atomic_index, position);
272 }
273 uint64_t cds_atomic_fetch_or64(void* addr, uint64_t val, int atomic_index, const char * position) {
274         _ATOMIC_RMW_( |=, 64, addr, val, atomic_index, position);
275 }
276
277 // cds atomic fetch xor
278 uint8_t cds_atomic_fetch_xor8(void* addr, uint8_t val, int atomic_index, const char * position) {
279         _ATOMIC_RMW_( ^=, 8, addr, val, atomic_index, position);
280 }
281 uint16_t cds_atomic_fetch_xor16(void* addr, uint16_t val, int atomic_index, const char * position) {
282         _ATOMIC_RMW_( ^=, 16, addr, val, atomic_index, position);
283 }
284 uint32_t cds_atomic_fetch_xor32(void* addr, uint32_t val, int atomic_index, const char * position) {
285         _ATOMIC_RMW_( ^=, 32, addr, val, atomic_index, position);
286 }
287 uint64_t cds_atomic_fetch_xor64(void* addr, uint64_t val, int atomic_index, const char * position) {
288         _ATOMIC_RMW_( ^=, 64, addr, val, atomic_index, position);
289 }
290
291 // cds atomic compare and exchange
292 // In order to accomodate the LLVM PASS, the return values are not true or false.
293
294 #define _ATOMIC_CMPSWP_WEAK_ _ATOMIC_CMPSWP_
295 #define _ATOMIC_CMPSWP_(size, addr, expected, desired, atomic_index, position)                            \
296         ({                                                                                              \
297                 uint ## size ## _t _desired = desired;                                                            \
298                 uint ## size ## _t _expected = expected;                                                          \
299                 uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
300                 if (_old == _expected) {                                                                    \
301                         model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; }      \
302                 else {                                                                                        \
303                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
304         })
305
306 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
307 // extracted from LLVM IR is an integer type.
308
309 uint8_t cds_atomic_compare_exchange8_v1(void* addr, uint8_t expected, uint8_t desired,
310                                                                                                                                                                 int atomic_index_succ, int atomic_index_fail, const char *position )
311 {
312         _ATOMIC_CMPSWP_(8, addr, expected, desired,
313                                                                         atomic_index_succ, position);
314 }
315 uint16_t cds_atomic_compare_exchange16_v1(void* addr, uint16_t expected, uint16_t desired,
316                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
317 {
318         _ATOMIC_CMPSWP_(16, addr, expected, desired,
319                                                                         atomic_index_succ, position);
320 }
321 uint32_t cds_atomic_compare_exchange32_v1(void* addr, uint32_t expected, uint32_t desired,
322                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
323 {
324         _ATOMIC_CMPSWP_(32, addr, expected, desired,
325                                                                         atomic_index_succ, position);
326 }
327 uint64_t cds_atomic_compare_exchange64_v1(void* addr, uint64_t expected, uint64_t desired,
328                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
329 {
330         _ATOMIC_CMPSWP_(64, addr, expected, desired,
331                                                                         atomic_index_succ, position);
332 }
333
334 // atomic_compare_exchange version 2
335 bool cds_atomic_compare_exchange8_v2(void* addr, uint8_t* expected, uint8_t desired,
336                                                                                                                                                  int atomic_index_succ, int atomic_index_fail, const char *position )
337 {
338         uint8_t ret = cds_atomic_compare_exchange8_v1(addr, *expected,
339                                                                                                                                                                                                 desired, atomic_index_succ, atomic_index_fail, position);
340         if (ret == *expected) return true;
341         else return false;
342 }
343 bool cds_atomic_compare_exchange16_v2(void* addr, uint16_t* expected, uint16_t desired,
344                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
345 {
346         uint16_t ret = cds_atomic_compare_exchange16_v1(addr, *expected,
347                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
348         if (ret == *expected) return true;
349         else return false;
350 }
351 bool cds_atomic_compare_exchange32_v2(void* addr, uint32_t* expected, uint32_t desired,
352                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
353 {
354         uint32_t ret = cds_atomic_compare_exchange32_v1(addr, *expected,
355                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
356         if (ret == *expected) return true;
357         else return false;
358 }
359 bool cds_atomic_compare_exchange64_v2(void* addr, uint64_t* expected, uint64_t desired,
360                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
361 {
362         uint64_t ret = cds_atomic_compare_exchange64_v1(addr, *expected,
363                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
364         if (ret == *expected) return true;
365         else return false;
366 }
367
368
369 // cds atomic thread fence
370
371 void cds_atomic_thread_fence(int atomic_index, const char * position) {
372         model->switch_to_master(
373                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
374                 );
375 }
376
377 /*
378  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
379         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
380                 __typeof__(__e__) __q__ = (__e__);                            \
381                 __typeof__(__m__) __v__ = (__m__);                            \
382                 bool __r__;                                                   \
383                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
384                 if (__t__ == * __q__ ) {                                      \
385                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
386                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
387                 __r__; })
388
389  #define _ATOMIC_FENCE_( __x__ ) \
390         ({ model_fence_action(__x__);})
391  */
392
393 /*
394
395  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
396         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
397         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
398         __typeof__(__m__) __v__ = (__m__);                                    \
399         __typeof__((__a__)->__f__) __copy__= __old__;                         \
400         __copy__ __o__ __v__;                                                 \
401         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
402         __old__ = __old__;  Silence clang (-Wunused-value)                    \
403          })
404  */
405
406 void cds_func_entry(const char * funcName) {
407         if (!model) return;
408
409         Thread * th = thread_current();
410         uint32_t func_id;
411
412         ModelHistory *history = model->get_history();
413         if ( !history->getFuncMap()->contains(funcName) ) {
414                 // add func id to func map
415                 func_id = history->get_func_counter();
416                 history->incr_func_counter();
417                 history->getFuncMap()->put(funcName, func_id);
418
419                 // add func id to reverse func map
420                 ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
421                 if ( func_map_rev->size() <= func_id )
422                         func_map_rev->resize( func_id + 1 );
423                 func_map_rev->at(func_id) = funcName;
424         } else {
425                 func_id = history->getFuncMap()->get(funcName);
426         }
427
428         history->enter_function(func_id, th->get_id());
429 }
430
431 void cds_func_exit(const char * funcName) {
432         if (!model) return;
433
434         Thread * th = thread_current();
435         uint32_t func_id;
436
437         ModelHistory *history = model->get_history();
438         func_id = history->getFuncMap()->get(funcName);
439
440         /* func_id not found; this could happen in the case where a function calls cds_func_entry
441         * when the model has been defined yet, but then an atomic inside the function initializes 
442         * the model. And then cds_func_exit is called upon the function exiting. 
443         */
444         if (func_id == 0)
445                 return;
446
447         history->exit_function(func_id, th->get_id());
448 }