model: fix bugs in fence-acquire synchronization
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <vector>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /**
16  * @brief Construct a new ModelAction
17  *
18  * @param type The type of action
19  * @param order The memory order of this action. A "don't care" for non-ATOMIC
20  * actions (e.g., THREAD_* or MODEL_* actions).
21  * @param loc The location that this action acts upon
22  * @param value (optional) A value associated with the action (e.g., the value
23  * read or written). Defaults to a given macro constant, for debugging purposes.
24  * @param thread (optional) The Thread in which this action occurred. If NULL
25  * (default), then a Thread is assigned according to the scheduler.
26  */
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28                 uint64_t value, Thread *thread) :
29         type(type),
30         order(order),
31         location(loc),
32         value(value),
33         reads_from(NULL),
34         last_fence_release(NULL),
35         node(NULL),
36         seq_number(ACTION_INITIAL_CLOCK),
37         cv(NULL),
38         sleep_flag(false)
39 {
40         /* References to NULL atomic variables can end up here */
41         ASSERT(loc || type == ATOMIC_FENCE || type == MODEL_FIXUP_RELSEQ);
42
43         Thread *t = thread ? thread : thread_current();
44         this->tid = t->get_id();
45 }
46
47 /** @brief ModelAction destructor */
48 ModelAction::~ModelAction()
49 {
50         /**
51          * We can't free the clock vector:
52          * Clock vectors are snapshotting state. When we delete model actions,
53          * they are at the end of the node list and have invalid old clock
54          * vectors which have already been rolled back to an unallocated state.
55          */
56
57         /*
58          if (cv)
59                 delete cv; */
60 }
61
62 void ModelAction::copy_from_new(ModelAction *newaction)
63 {
64         seq_number = newaction->seq_number;
65 }
66
67 void ModelAction::set_seq_number(modelclock_t num)
68 {
69         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
70         seq_number = num;
71 }
72
73 bool ModelAction::is_thread_start() const
74 {
75         return type == THREAD_START;
76 }
77
78 bool ModelAction::is_relseq_fixup() const
79 {
80         return type == MODEL_FIXUP_RELSEQ;
81 }
82
83 bool ModelAction::is_mutex_op() const
84 {
85         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
86 }
87
88 bool ModelAction::is_lock() const
89 {
90         return type == ATOMIC_LOCK;
91 }
92
93 bool ModelAction::is_wait() const {
94         return type == ATOMIC_WAIT;
95 }
96
97 bool ModelAction::is_notify() const {
98         return type==ATOMIC_NOTIFY_ONE || type==ATOMIC_NOTIFY_ALL;
99 }
100
101 bool ModelAction::is_notify_one() const {
102         return type==ATOMIC_NOTIFY_ONE;
103 }
104
105 bool ModelAction::is_unlock() const
106 {
107         return type == ATOMIC_UNLOCK;
108 }
109
110 bool ModelAction::is_trylock() const
111 {
112         return type == ATOMIC_TRYLOCK;
113 }
114
115 bool ModelAction::is_success_lock() const
116 {
117         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
118 }
119
120 bool ModelAction::is_failed_trylock() const
121 {
122         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
123 }
124
125 bool ModelAction::is_read() const
126 {
127         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
128 }
129
130 bool ModelAction::is_write() const
131 {
132         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
133 }
134
135 bool ModelAction::could_be_write() const
136 {
137         return is_write() || is_rmwr();
138 }
139
140 bool ModelAction::is_rmwr() const
141 {
142         return type == ATOMIC_RMWR;
143 }
144
145 bool ModelAction::is_rmw() const
146 {
147         return type == ATOMIC_RMW;
148 }
149
150 bool ModelAction::is_rmwc() const
151 {
152         return type == ATOMIC_RMWC;
153 }
154
155 bool ModelAction::is_fence() const 
156 {
157         return type == ATOMIC_FENCE;
158 }
159
160 bool ModelAction::is_initialization() const
161 {
162         return type == ATOMIC_INIT;
163 }
164
165 bool ModelAction::is_relaxed() const
166 {
167         return order == std::memory_order_relaxed;
168 }
169
170 bool ModelAction::is_acquire() const
171 {
172         switch (order) {
173         case std::memory_order_acquire:
174         case std::memory_order_acq_rel:
175         case std::memory_order_seq_cst:
176                 return true;
177         default:
178                 return false;
179         }
180 }
181
182 bool ModelAction::is_release() const
183 {
184         switch (order) {
185         case std::memory_order_release:
186         case std::memory_order_acq_rel:
187         case std::memory_order_seq_cst:
188                 return true;
189         default:
190                 return false;
191         }
192 }
193
194 bool ModelAction::is_seqcst() const
195 {
196         return order==std::memory_order_seq_cst;
197 }
198
199 bool ModelAction::same_var(const ModelAction *act) const
200 {
201         if ( act->is_wait() || is_wait() ) {
202                 if ( act->is_wait() && is_wait() ) {
203                         if ( ((void *)value) == ((void *)act->value) )
204                                 return true;
205                 } else if ( is_wait() ) {
206                         if ( ((void *)value) == act->location )
207                                 return true;
208                 } else if ( act->is_wait() ) {
209                         if ( location == ((void *)act->value) )
210                                 return true;
211                 }
212         }
213
214         return location == act->location;
215 }
216
217 bool ModelAction::same_thread(const ModelAction *act) const
218 {
219         return tid == act->tid;
220 }
221
222 void ModelAction::copy_typeandorder(ModelAction * act) {
223         this->type = act->type;
224         this->order = act->order;
225 }
226
227 /** This method changes an existing read part of an RMW action into either:
228  *  (1) a full RMW action in case of the completed write or
229  *  (2) a READ action in case a failed action.
230  * @todo  If the memory_order changes, we may potentially need to update our
231  * clock vector.
232  */
233 void ModelAction::process_rmw(ModelAction * act) {
234         this->order=act->order;
235         if (act->is_rmwc())
236                 this->type=ATOMIC_READ;
237         else if (act->is_rmw()) {
238                 this->type=ATOMIC_RMW;
239                 this->value=act->value;
240         }
241 }
242
243 /** The is_synchronizing method should only explore interleavings if:
244  *  (1) the operations are seq_cst and don't commute or
245  *  (2) the reordering may establish or break a synchronization relation.
246  *  Other memory operations will be dealt with by using the reads_from
247  *  relation.
248  *
249  *  @param act is the action to consider exploring a reordering.
250  *  @return tells whether we have to explore a reordering.
251  */
252 bool ModelAction::could_synchronize_with(const ModelAction *act) const
253 {
254         //Same thread can't be reordered
255         if (same_thread(act))
256                 return false;
257
258         // Different locations commute
259         if (!same_var(act))
260                 return false;
261
262         // Explore interleavings of seqcst writes/fences to guarantee total
263         // order of seq_cst operations that don't commute
264         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence())
265                         && is_seqcst() && act->is_seqcst())
266                 return true;
267
268         // Explore synchronizing read/write/fence pairs
269         if (is_acquire() && act->is_release() && (is_read() || is_fence()) &&
270                         (act->could_be_write() || act->is_fence()))
271                 return true;
272
273         //lock just released...we can grab lock
274         if ((is_lock() ||is_trylock()) && (act->is_unlock()||act->is_wait()))
275                 return true;
276
277         //lock just acquired...we can fail to grab lock
278         if (is_trylock() && act->is_success_lock())
279                 return true;
280
281         //other thread stalling on lock...we can release lock
282         if (is_unlock() && (act->is_trylock()||act->is_lock()))
283                 return true;
284
285         if (is_trylock() && (act->is_unlock()||act->is_wait()))
286                 return true;
287
288         if ( is_notify() && act->is_wait() )
289                 return true;
290
291         if ( is_wait() && act->is_notify() )
292                 return true;
293
294         // Otherwise handle by reads_from relation
295         return false;
296 }
297
298 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
299 {
300         //Must be different threads to reorder
301         if (same_thread(act))
302                 return false;
303         
304         //Try to reorder a lock past a successful lock
305         if (act->is_success_lock())
306                 return true;
307         
308         //Try to push a successful trylock past an unlock
309         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
310                 return true;
311
312         //Try to push a successful trylock past a wait
313         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
314                 return true;
315
316         return false;
317 }
318
319 /**
320  * Create a new clock vector for this action. Note that this function allows a
321  * user to clobber (and leak) a ModelAction's existing clock vector. A user
322  * should ensure that the vector has already either been rolled back
323  * (effectively "freed") or freed.
324  *
325  * @param parent A ModelAction from which to inherit a ClockVector
326  */
327 void ModelAction::create_cv(const ModelAction *parent)
328 {
329         if (parent)
330                 cv = new ClockVector(parent->cv, this);
331         else
332                 cv = new ClockVector(NULL, this);
333 }
334
335 void ModelAction::set_try_lock(bool obtainedlock) {
336         if (obtainedlock)
337                 value=VALUE_TRYSUCCESS;
338         else
339                 value=VALUE_TRYFAILED;
340 }
341
342 /**
343  * Update the model action's read_from action
344  * @param act The action to read from; should be a write
345  */
346 void ModelAction::set_read_from(const ModelAction *act)
347 {
348         reads_from = act;
349 }
350
351 /**
352  * Synchronize the current thread with the thread corresponding to the
353  * ModelAction parameter.
354  * @param act The ModelAction to synchronize with
355  * @return True if this is a valid synchronization; false otherwise
356  */
357 bool ModelAction::synchronize_with(const ModelAction *act) {
358         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
359                 return false;
360         model->check_promises(act->get_tid(), cv, act->cv);
361         cv->merge(act->cv);
362         return true;
363 }
364
365 bool ModelAction::has_synchronized_with(const ModelAction *act) const
366 {
367         return cv->synchronized_since(act);
368 }
369
370 /**
371  * Check whether 'this' happens before act, according to the memory-model's
372  * happens before relation. This is checked via the ClockVector constructs.
373  * @return true if this action's thread has synchronized with act's thread
374  * since the execution of act, false otherwise.
375  */
376 bool ModelAction::happens_before(const ModelAction *act) const
377 {
378         return act->cv->synchronized_since(this);
379 }
380
381 /** @brief Print nicely-formatted info about this ModelAction */
382 void ModelAction::print() const
383 {
384         const char *type_str, *mo_str;
385         switch (this->type) {
386         case MODEL_FIXUP_RELSEQ:
387                 type_str = "relseq fixup";
388                 break;
389         case THREAD_CREATE:
390                 type_str = "thread create";
391                 break;
392         case THREAD_START:
393                 type_str = "thread start";
394                 break;
395         case THREAD_YIELD:
396                 type_str = "thread yield";
397                 break;
398         case THREAD_JOIN:
399                 type_str = "thread join";
400                 break;
401         case THREAD_FINISH:
402                 type_str = "thread finish";
403                 break;
404         case ATOMIC_READ:
405                 type_str = "atomic read";
406                 break;
407         case ATOMIC_WRITE:
408                 type_str = "atomic write";
409                 break;
410         case ATOMIC_RMW:
411                 type_str = "atomic rmw";
412                 break;
413         case ATOMIC_FENCE:
414                 type_str = "fence";
415                 break;
416         case ATOMIC_RMWR:
417                 type_str = "atomic rmwr";
418                 break;
419         case ATOMIC_RMWC:
420                 type_str = "atomic rmwc";
421                 break;
422         case ATOMIC_INIT:
423                 type_str = "init atomic";
424                 break;
425         case ATOMIC_LOCK:
426                 type_str = "lock";
427                 break;
428         case ATOMIC_UNLOCK:
429                 type_str = "unlock";
430                 break;
431         case ATOMIC_TRYLOCK:
432                 type_str = "trylock";
433                 break;
434         case ATOMIC_WAIT:
435                 type_str = "wait";
436                 break;
437         case ATOMIC_NOTIFY_ONE:
438                 type_str = "notify one";
439                 break;
440         case ATOMIC_NOTIFY_ALL:
441                 type_str = "notify all";
442                 break;
443         default:
444                 type_str = "unknown type";
445         }
446
447         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
448
449         switch (this->order) {
450         case std::memory_order_relaxed:
451                 mo_str = "relaxed";
452                 break;
453         case std::memory_order_acquire:
454                 mo_str = "acquire";
455                 break;
456         case std::memory_order_release:
457                 mo_str = "release";
458                 break;
459         case std::memory_order_acq_rel:
460                 mo_str = "acq_rel";
461                 break;
462         case std::memory_order_seq_cst:
463                 mo_str = "seq_cst";
464                 break;
465         default:
466                 mo_str = "unknown";
467                 break;
468         }
469
470         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
471                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
472         if (is_read()) {
473                 if (reads_from)
474                         model_print("  Rf: %-3d", reads_from->get_seq_number());
475                 else
476                         model_print("  Rf: ?  ");
477         }
478         if (cv) {
479                 if (is_read())
480                         model_print(" ");
481                 else
482                         model_print("          ");
483                 cv->print();
484         } else
485                 model_print("\n");
486 }
487
488 /** @brief Print nicely-formatted info about this ModelAction */
489 unsigned int ModelAction::hash() const
490 {
491         unsigned int hash=(unsigned int) this->type;
492         hash^=((unsigned int)this->order)<<3;
493         hash^=seq_number<<5;
494         hash ^= id_to_int(tid) << 6;
495
496         if (is_read()) {
497                 if (reads_from)
498                         hash^=reads_from->get_seq_number();
499         }
500         return hash;
501 }