b1a8eb43d3258add2ff3d8af5f821ab566669105
[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[6] = {
13         memory_order_relaxed, memory_order_consume, memory_order_acquire,
14         memory_order_release, memory_order_acq_rel, memory_order_seq_cst
15 };
16
17 static void ensureModel() {
18         if (!model) {
19                 snapshot_system_init(10000, 1024, 1024, 40000);
20                 model = new ModelChecker();
21                 model->startChecker();
22         }
23 }
24
25 /** Performs a read action.*/
26 uint64_t model_read_action(void * obj, memory_order ord) {
27         return model->switch_to_master(new ModelAction(ATOMIC_READ, ord, obj));
28 }
29
30 /** Performs a write action.*/
31 void model_write_action(void * obj, memory_order ord, uint64_t val) {
32         model->switch_to_master(new ModelAction(ATOMIC_WRITE, ord, obj, val));
33 }
34
35 /** Performs an init action. */
36 void model_init_action(void * obj, uint64_t val) {
37         model->switch_to_master(new ModelAction(ATOMIC_INIT, memory_order_relaxed, obj, val));
38 }
39
40 /**
41  * Performs the read part of a RMW action. The next action must either be the
42  * write part of the RMW action or an explicit close out of the RMW action w/o
43  * a write.
44  */
45 uint64_t model_rmwr_action(void *obj, memory_order ord) {
46         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, ord, obj));
47 }
48
49 /**
50  * Performs the read part of a RMW CAS action. The next action must
51  * either be the write part of the RMW action or an explicit close out
52  * of the RMW action w/o a write.
53  */
54 uint64_t model_rmwrcas_action(void *obj, memory_order ord, uint64_t oldval, int size) {
55         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, ord, obj, oldval, size));
56 }
57
58
59 /** Performs the write part of a RMW action. */
60 void model_rmw_action(void *obj, memory_order ord, uint64_t val) {
61         model->switch_to_master(new ModelAction(ATOMIC_RMW, ord, obj, val));
62 }
63
64 /** Closes out a RMW action without doing a write. */
65 void model_rmwc_action(void *obj, memory_order ord) {
66         model->switch_to_master(new ModelAction(ATOMIC_RMWC, ord, obj));
67 }
68
69 /** Issues a fence operation. */
70 void model_fence_action(memory_order ord) {
71         model->switch_to_master(new ModelAction(ATOMIC_FENCE, ord, FENCE_LOCATION));
72 }
73
74 /* ---  helper functions --- */
75 uint64_t model_rmwrcas_action_helper(void *obj, int atomic_index, uint64_t oldval, int size, const char *position) {
76         ensureModel();
77         return model->switch_to_master(new ModelAction(ATOMIC_RMWRCAS, position, orders[atomic_index], obj, oldval, size));
78 }
79
80 uint64_t model_rmwr_action_helper(void *obj, int atomic_index, const char *position) {
81         ensureModel();
82         return model->switch_to_master(new ModelAction(ATOMIC_RMWR, position, orders[atomic_index], obj));
83 }
84
85 void model_rmw_action_helper(void *obj, uint64_t val, int atomic_index, const char * position) {
86         ensureModel();
87         model->switch_to_master(new ModelAction(ATOMIC_RMW, position, orders[atomic_index], obj, val));
88 }
89
90 void model_rmwc_action_helper(void *obj, int atomic_index, const char *position) {
91         ensureModel();
92         model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
93 }
94
95 // cds atomic inits
96 void cds_atomic_init8(void * obj, uint8_t val, const char * position) {
97         ensureModel();
98         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
99 }
100 void cds_atomic_init16(void * obj, uint16_t val, const char * position) {
101         ensureModel();
102         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
103 }
104 void cds_atomic_init32(void * obj, uint32_t val, const char * position) {
105         ensureModel();
106         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val));
107 }
108 void cds_atomic_init64(void * obj, uint64_t val, const char * position) {
109         ensureModel();
110         model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, val));
111 }
112
113
114 // cds atomic loads
115 uint8_t cds_atomic_load8(void * obj, int atomic_index, const char * position) {
116         ensureModel();
117         return (uint8_t) model->switch_to_master(
118                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
119 }
120 uint16_t cds_atomic_load16(void * obj, int atomic_index, const char * position) {
121         ensureModel();
122         return (uint16_t) model->switch_to_master(
123                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
124 }
125 uint32_t cds_atomic_load32(void * obj, int atomic_index, const char * position) {
126         ensureModel();
127         return (uint32_t) model->switch_to_master(
128                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)
129                 );
130 }
131 uint64_t cds_atomic_load64(void * obj, int atomic_index, const char * position) {
132         ensureModel();
133         return model->switch_to_master(
134                 new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj));
135 }
136
137 // cds atomic stores
138 void cds_atomic_store8(void * obj, uint8_t val, int atomic_index, const char * position) {
139         ensureModel();
140         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
141 }
142 void cds_atomic_store16(void * obj, uint16_t val, int atomic_index, const char * position) {
143         ensureModel();
144         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
145 }
146 void cds_atomic_store32(void * obj, uint32_t val, int atomic_index, const char * position) {
147         ensureModel();
148         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
149 }
150 void cds_atomic_store64(void * obj, uint64_t val, int atomic_index, const char * position) {
151         ensureModel();
152         model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val));
153 }
154
155 #define _ATOMIC_RMW_(__op__, size, addr, val, atomic_index, position)            \
156         ({                                                                      \
157                 uint ## size ## _t _old = model_rmwr_action_helper(addr, atomic_index, position);   \
158                 uint ## size ## _t _copy = _old;                                          \
159                 uint ## size ## _t _val = val;                                            \
160                 _copy __op__ _val;                                                    \
161                 model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
162                 return _old;                                                          \
163         })
164
165 // cds atomic exchange
166 uint8_t cds_atomic_exchange8(void* addr, uint8_t val, int atomic_index, const char * position) {
167         _ATOMIC_RMW_( =, 8, addr, val, atomic_index, position);
168 }
169 uint16_t cds_atomic_exchange16(void* addr, uint16_t val, int atomic_index, const char * position) {
170         _ATOMIC_RMW_( =, 16, addr, val, atomic_index, position);
171 }
172 uint32_t cds_atomic_exchange32(void* addr, uint32_t val, int atomic_index, const char * position) {
173         _ATOMIC_RMW_( =, 32, addr, val, atomic_index, position);
174 }
175 uint64_t cds_atomic_exchange64(void* addr, uint64_t val, int atomic_index, const char * position) {
176         _ATOMIC_RMW_( =, 64, addr, val, atomic_index, position);
177 }
178
179 // cds atomic fetch add
180 uint8_t cds_atomic_fetch_add8(void* addr, uint8_t val, int atomic_index, const char * position) {
181         _ATOMIC_RMW_( +=, 8, addr, val, atomic_index, position);
182 }
183 uint16_t cds_atomic_fetch_add16(void* addr, uint16_t val, int atomic_index, const char * position) {
184         _ATOMIC_RMW_( +=, 16, addr, val, atomic_index, position);
185 }
186 uint32_t cds_atomic_fetch_add32(void* addr, uint32_t val, int atomic_index, const char * position) {
187         _ATOMIC_RMW_( +=, 32, addr, val, atomic_index, position);
188 }
189 uint64_t cds_atomic_fetch_add64(void* addr, uint64_t val, int atomic_index, const char * position) {
190         _ATOMIC_RMW_( +=, 64, addr, val, atomic_index, position);
191 }
192
193 // cds atomic fetch sub
194 uint8_t cds_atomic_fetch_sub8(void* addr, uint8_t val, int atomic_index, const char * position) {
195         _ATOMIC_RMW_( -=, 8, addr, val, atomic_index, position);
196 }
197 uint16_t cds_atomic_fetch_sub16(void* addr, uint16_t val, int atomic_index, const char * position) {
198         _ATOMIC_RMW_( -=, 16, addr, val, atomic_index, position);
199 }
200 uint32_t cds_atomic_fetch_sub32(void* addr, uint32_t val, int atomic_index, const char * position) {
201         _ATOMIC_RMW_( -=, 32, addr, val, atomic_index, position);
202 }
203 uint64_t cds_atomic_fetch_sub64(void* addr, uint64_t val, int atomic_index, const char * position) {
204         _ATOMIC_RMW_( -=, 64, addr, val, atomic_index, position);
205 }
206
207 // cds atomic fetch and
208 uint8_t cds_atomic_fetch_and8(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_fetch_and16(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_fetch_and32(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_fetch_and64(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 or
222 uint8_t cds_atomic_fetch_or8(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_or16(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_or32(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_or64(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 xor
236 uint8_t cds_atomic_fetch_xor8(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_xor16(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_xor32(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_xor64(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 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); return _expected; }      \
260                 else {                                                                                        \
261                         model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
262         })
263
264 // atomic_compare_exchange version 1: the CmpOperand (corresponds to expected)
265 // extracted from LLVM IR is an integer type.
266
267 uint8_t cds_atomic_compare_exchange8_v1(void* addr, uint8_t expected, uint8_t desired,
268                                                                                                                                                                 int atomic_index_succ, int atomic_index_fail, const char *position )
269 {
270         _ATOMIC_CMPSWP_(8, addr, expected, desired,
271                                                                         atomic_index_succ, position);
272 }
273 uint16_t cds_atomic_compare_exchange16_v1(void* addr, uint16_t expected, uint16_t desired,
274                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
275 {
276         _ATOMIC_CMPSWP_(16, addr, expected, desired,
277                                                                         atomic_index_succ, position);
278 }
279 uint32_t cds_atomic_compare_exchange32_v1(void* addr, uint32_t expected, uint32_t desired,
280                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
281 {
282         _ATOMIC_CMPSWP_(32, addr, expected, desired,
283                                                                         atomic_index_succ, position);
284 }
285 uint64_t cds_atomic_compare_exchange64_v1(void* addr, uint64_t expected, uint64_t desired,
286                                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
287 {
288         _ATOMIC_CMPSWP_(64, addr, expected, desired,
289                                                                         atomic_index_succ, position);
290 }
291
292 // atomic_compare_exchange version 2
293 bool cds_atomic_compare_exchange8_v2(void* addr, uint8_t* expected, uint8_t desired,
294                                                                                                                                                  int atomic_index_succ, int atomic_index_fail, const char *position )
295 {
296         uint8_t ret = cds_atomic_compare_exchange8_v1(addr, *expected,
297                                                                                                                                                                                                 desired, atomic_index_succ, atomic_index_fail, position);
298         if (ret == *expected) return true;
299         else return false;
300 }
301 bool cds_atomic_compare_exchange16_v2(void* addr, uint16_t* expected, uint16_t desired,
302                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
303 {
304         uint16_t ret = cds_atomic_compare_exchange16_v1(addr, *expected,
305                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
306         if (ret == *expected) return true;
307         else return false;
308 }
309 bool cds_atomic_compare_exchange32_v2(void* addr, uint32_t* expected, uint32_t desired,
310                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
311 {
312         uint32_t ret = cds_atomic_compare_exchange32_v1(addr, *expected,
313                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
314         if (ret == *expected) return true;
315         else return false;
316 }
317 bool cds_atomic_compare_exchange64_v2(void* addr, uint64_t* expected, uint64_t desired,
318                                                                                                                                                         int atomic_index_succ, int atomic_index_fail, const char *position)
319 {
320         uint64_t ret = cds_atomic_compare_exchange64_v1(addr, *expected,
321                                                                                                                                                                                                         desired, atomic_index_succ, atomic_index_fail, position);
322         if (ret == *expected) return true;
323         else return false;
324 }
325
326
327 // cds atomic thread fence
328
329 void cds_atomic_thread_fence(int atomic_index, const char * position) {
330         model->switch_to_master(
331                 new ModelAction(ATOMIC_FENCE, position, orders[atomic_index], FENCE_LOCATION)
332                 );
333 }
334
335 /*
336  #define _ATOMIC_CMPSWP_( __a__, __e__, __m__, __x__ )                         \
337         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
338                 __typeof__(__e__) __q__ = (__e__);                            \
339                 __typeof__(__m__) __v__ = (__m__);                            \
340                 bool __r__;                                                   \
341                 __typeof__((__a__)->__f__) __t__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
342                 if (__t__ == * __q__ ) {                                      \
343                         model_rmw_action((void *)__p__, __x__, (uint64_t) __v__); __r__ = true; } \
344                 else {  model_rmwc_action((void *)__p__, __x__); *__q__ = __t__;  __r__ = false;} \
345                 __r__; })
346
347  #define _ATOMIC_FENCE_( __x__ ) \
348         ({ model_fence_action(__x__);})
349  */
350
351 /*
352
353  #define _ATOMIC_MODIFY_( __a__, __o__, __m__, __x__ )                         \
354         ({ volatile __typeof__((__a__)->__f__)* __p__ = & ((__a__)->__f__);   \
355         __typeof__((__a__)->__f__) __old__=(__typeof__((__a__)->__f__)) model_rmwr_action((void *)__p__, __x__); \
356         __typeof__(__m__) __v__ = (__m__);                                    \
357         __typeof__((__a__)->__f__) __copy__= __old__;                         \
358         __copy__ __o__ __v__;                                                 \
359         model_rmw_action((void *)__p__, __x__, (uint64_t) __copy__);          \
360         __old__ = __old__;  Silence clang (-Wunused-value)                    \
361          })
362  */
363
364 void cds_func_entry(const char * funcName) {
365         if (!model) return;
366
367         Thread * th = thread_current();
368         uint32_t func_id;
369
370         ModelHistory *history = model->get_history();
371         if ( !history->getFuncMap()->contains(funcName) ) {
372                 func_id = history->get_func_counter();
373                 history->incr_func_counter();
374
375                 history->getFuncMap()->put(funcName, func_id);
376         } else {
377                 func_id = history->getFuncMap()->get(funcName);
378         }
379
380         history->enter_function(func_id, th->get_id());
381 }
382
383 void cds_func_exit(const char * funcName) {
384         if (!model) return;
385
386         Thread * th = thread_current();
387         uint32_t func_id;
388
389         ModelHistory *history = model->get_history();
390         func_id = history->getFuncMap()->get(funcName);
391
392         history->exit_function(func_id, th->get_id());
393 }