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