model: get_mutex() for locating the lock map
[model-checker.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4
5 #include "model.h"
6 #include "action.h"
7 #include "clockvector.h"
8 #include "common.h"
9 #include "threads-model.h"
10 #include "nodestack.h"
11
12 #define ACTION_INITIAL_CLOCK 0
13
14 /**
15  * @brief Construct a new ModelAction
16  *
17  * @param type The type of action
18  * @param order The memory order of this action. A "don't care" for non-ATOMIC
19  * actions (e.g., THREAD_* or MODEL_* actions).
20  * @param loc The location that this action acts upon
21  * @param value (optional) A value associated with the action (e.g., the value
22  * read or written). Defaults to a given macro constant, for debugging purposes.
23  * @param thread (optional) The Thread in which this action occurred. If NULL
24  * (default), then a Thread is assigned according to the scheduler.
25  */
26 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
27                 uint64_t value, Thread *thread) :
28         type(type),
29         order(order),
30         location(loc),
31         value(value),
32         reads_from(NULL),
33         reads_from_promise(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         /* ATOMIC_UNINIT actions should never have non-zero clock */
70         ASSERT(!is_uninitialized());
71         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
72         seq_number = num;
73 }
74
75 bool ModelAction::is_thread_start() const
76 {
77         return type == THREAD_START;
78 }
79
80 bool ModelAction::is_thread_join() const
81 {
82         return type == THREAD_JOIN;
83 }
84
85 bool ModelAction::is_relseq_fixup() const
86 {
87         return type == MODEL_FIXUP_RELSEQ;
88 }
89
90 bool ModelAction::is_mutex_op() const
91 {
92         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
93 }
94
95 bool ModelAction::is_lock() const
96 {
97         return type == ATOMIC_LOCK;
98 }
99
100 bool ModelAction::is_wait() const {
101         return type == ATOMIC_WAIT;
102 }
103
104 bool ModelAction::is_notify() const {
105         return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
106 }
107
108 bool ModelAction::is_notify_one() const {
109         return type == ATOMIC_NOTIFY_ONE;
110 }
111
112 bool ModelAction::is_unlock() const
113 {
114         return type == ATOMIC_UNLOCK;
115 }
116
117 bool ModelAction::is_trylock() const
118 {
119         return type == ATOMIC_TRYLOCK;
120 }
121
122 bool ModelAction::is_success_lock() const
123 {
124         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
125 }
126
127 bool ModelAction::is_failed_trylock() const
128 {
129         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
130 }
131
132 /** @return True if this operation is performed on a C/C++ atomic variable */
133 bool ModelAction::is_atomic_var() const
134 {
135         return is_read() || could_be_write();
136 }
137
138 bool ModelAction::is_uninitialized() const
139 {
140         return type == ATOMIC_UNINIT;
141 }
142
143 bool ModelAction::is_read() const
144 {
145         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
146 }
147
148 bool ModelAction::is_write() const
149 {
150         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT;
151 }
152
153 bool ModelAction::could_be_write() const
154 {
155         return is_write() || is_rmwr();
156 }
157
158 bool ModelAction::is_yield() const
159 {
160         return type == THREAD_YIELD;
161 }
162
163 bool ModelAction::is_rmwr() const
164 {
165         return type == ATOMIC_RMWR;
166 }
167
168 bool ModelAction::is_rmw() const
169 {
170         return type == ATOMIC_RMW;
171 }
172
173 bool ModelAction::is_rmwc() const
174 {
175         return type == ATOMIC_RMWC;
176 }
177
178 bool ModelAction::is_fence() const
179 {
180         return type == ATOMIC_FENCE;
181 }
182
183 bool ModelAction::is_initialization() const
184 {
185         return type == ATOMIC_INIT;
186 }
187
188 bool ModelAction::is_relaxed() const
189 {
190         return order == std::memory_order_relaxed;
191 }
192
193 bool ModelAction::is_acquire() const
194 {
195         switch (order) {
196         case std::memory_order_acquire:
197         case std::memory_order_acq_rel:
198         case std::memory_order_seq_cst:
199                 return true;
200         default:
201                 return false;
202         }
203 }
204
205 bool ModelAction::is_release() const
206 {
207         switch (order) {
208         case std::memory_order_release:
209         case std::memory_order_acq_rel:
210         case std::memory_order_seq_cst:
211                 return true;
212         default:
213                 return false;
214         }
215 }
216
217 bool ModelAction::is_seqcst() const
218 {
219         return order == std::memory_order_seq_cst;
220 }
221
222 bool ModelAction::same_var(const ModelAction *act) const
223 {
224         if (act->is_wait() || is_wait()) {
225                 if (act->is_wait() && is_wait()) {
226                         if (((void *)value) == ((void *)act->value))
227                                 return true;
228                 } else if (is_wait()) {
229                         if (((void *)value) == act->location)
230                                 return true;
231                 } else if (act->is_wait()) {
232                         if (location == ((void *)act->value))
233                                 return true;
234                 }
235         }
236
237         return location == act->location;
238 }
239
240 bool ModelAction::same_thread(const ModelAction *act) const
241 {
242         return tid == act->tid;
243 }
244
245 void ModelAction::copy_typeandorder(ModelAction * act)
246 {
247         this->type = act->type;
248         this->order = act->order;
249 }
250
251 /**
252  * Get the Thread which is the operand of this action. This is only valid for
253  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
254  * that this provides a central place for determining the conventions of Thread
255  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
256  * store object references in a (void *) address.
257  *
258  * For THREAD_CREATE, this yields the Thread which is created.
259  * For THREAD_JOIN, this yields the Thread we are joining with.
260  *
261  * @return The Thread which this action acts on, if exists; otherwise NULL
262  */
263 Thread * ModelAction::get_thread_operand() const
264 {
265         if (type == THREAD_CREATE) {
266                 /* THREAD_CREATE stores its (Thread *) in a thrd_t::priv */
267                 thrd_t *thrd = (thrd_t *)get_location();
268                 return thrd->priv;
269         } else if (type == THREAD_JOIN)
270                 /* THREAD_JOIN uses (Thread *) for location */
271                 return (Thread *)get_location();
272         else
273                 return NULL;
274 }
275
276 /** This method changes an existing read part of an RMW action into either:
277  *  (1) a full RMW action in case of the completed write or
278  *  (2) a READ action in case a failed action.
279  * @todo  If the memory_order changes, we may potentially need to update our
280  * clock vector.
281  */
282 void ModelAction::process_rmw(ModelAction *act)
283 {
284         this->order = act->order;
285         if (act->is_rmwc())
286                 this->type = ATOMIC_READ;
287         else if (act->is_rmw()) {
288                 this->type = ATOMIC_RMW;
289                 this->value = act->value;
290         }
291 }
292
293 /** The is_synchronizing method should only explore interleavings if:
294  *  (1) the operations are seq_cst and don't commute or
295  *  (2) the reordering may establish or break a synchronization relation.
296  *  Other memory operations will be dealt with by using the reads_from
297  *  relation.
298  *
299  *  @param act is the action to consider exploring a reordering.
300  *  @return tells whether we have to explore a reordering.
301  */
302 bool ModelAction::could_synchronize_with(const ModelAction *act) const
303 {
304         // Same thread can't be reordered
305         if (same_thread(act))
306                 return false;
307
308         // Different locations commute
309         if (!same_var(act))
310                 return false;
311
312         // Explore interleavings of seqcst writes/fences to guarantee total
313         // order of seq_cst operations that don't commute
314         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
315                 return true;
316
317         // Explore synchronizing read/write pairs
318         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
319                 return true;
320
321         // lock just released...we can grab lock
322         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
323                 return true;
324
325         // lock just acquired...we can fail to grab lock
326         if (is_trylock() && act->is_success_lock())
327                 return true;
328
329         // other thread stalling on lock...we can release lock
330         if (is_unlock() && (act->is_trylock() || act->is_lock()))
331                 return true;
332
333         if (is_trylock() && (act->is_unlock() || act->is_wait()))
334                 return true;
335
336         if (is_notify() && act->is_wait())
337                 return true;
338
339         if (is_wait() && act->is_notify())
340                 return true;
341
342         // Otherwise handle by reads_from relation
343         return false;
344 }
345
346 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
347 {
348         // Must be different threads to reorder
349         if (same_thread(act))
350                 return false;
351
352         // Try to reorder a lock past a successful lock
353         if (act->is_success_lock())
354                 return true;
355
356         // Try to push a successful trylock past an unlock
357         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
358                 return true;
359
360         // Try to push a successful trylock past a wait
361         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
362                 return true;
363
364         return false;
365 }
366
367 /**
368  * Create a new clock vector for this action. Note that this function allows a
369  * user to clobber (and leak) a ModelAction's existing clock vector. A user
370  * should ensure that the vector has already either been rolled back
371  * (effectively "freed") or freed.
372  *
373  * @param parent A ModelAction from which to inherit a ClockVector
374  */
375 void ModelAction::create_cv(const ModelAction *parent)
376 {
377         if (parent)
378                 cv = new ClockVector(parent->cv, this);
379         else
380                 cv = new ClockVector(NULL, this);
381 }
382
383 void ModelAction::set_try_lock(bool obtainedlock) {
384         if (obtainedlock)
385                 value = VALUE_TRYSUCCESS;
386         else
387                 value = VALUE_TRYFAILED;
388 }
389
390 /**
391  * @brief Get the value read by this load
392  *
393  * We differentiate this function from ModelAction::get_write_value and
394  * ModelAction::get_value for the purpose of RMW's, which may have both a
395  * 'read' and a 'write' value.
396  *
397  * Note: 'this' must be a load.
398  *
399  * @return The value read by this load
400  */
401 uint64_t ModelAction::get_reads_from_value() const
402 {
403         ASSERT(is_read());
404         if (reads_from)
405                 return reads_from->get_write_value();
406         else if (reads_from_promise)
407                 return reads_from_promise->get_value();
408         return VALUE_NONE; /* Only for new actions with no reads-from */
409 }
410
411 /**
412  * @brief Get the value written by this store
413  *
414  * We differentiate this function from ModelAction::get_reads_from_value and
415  * ModelAction::get_value for the purpose of RMW's, which may have both a
416  * 'read' and a 'write' value.
417  *
418  * Note: 'this' must be a store.
419  *
420  * @return The value written by this store
421  */
422 uint64_t ModelAction::get_write_value() const
423 {
424         ASSERT(is_write());
425         return value;
426 }
427
428 /**
429  * @brief Get the value returned by this action
430  *
431  * For atomic reads (including RMW), an operation returns the value it read.
432  * For atomic writes, an operation returns the value it wrote. For other
433  * operations, the return value varies (sometimes is a "don't care"), but the
434  * value is simply stored in the "value" field.
435  *
436  * @return This action's return value
437  */
438 uint64_t ModelAction::get_return_value() const
439 {
440         if (is_read())
441                 return get_reads_from_value();
442         else if (is_write())
443                 return get_write_value();
444         else
445                 return value;
446 }
447
448 /** @return The Node associated with this ModelAction */
449 Node * ModelAction::get_node() const
450 {
451         /* UNINIT actions do not have a Node */
452         ASSERT(!is_uninitialized());
453         return node;
454 }
455
456 /**
457  * Update the model action's read_from action
458  * @param act The action to read from; should be a write
459  */
460 void ModelAction::set_read_from(const ModelAction *act)
461 {
462         ASSERT(act);
463         reads_from = act;
464         reads_from_promise = NULL;
465         if (act->is_uninitialized())
466                 model->assert_bug("May read from uninitialized atomic\n");
467 }
468
469 /**
470  * Set this action's read-from promise
471  * @param promise The promise to read from
472  */
473 void ModelAction::set_read_from_promise(Promise *promise)
474 {
475         ASSERT(is_read());
476         reads_from_promise = promise;
477         reads_from = NULL;
478 }
479
480 /**
481  * Synchronize the current thread with the thread corresponding to the
482  * ModelAction parameter.
483  * @param act The ModelAction to synchronize with
484  * @return True if this is a valid synchronization; false otherwise
485  */
486 bool ModelAction::synchronize_with(const ModelAction *act)
487 {
488         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
489                 return false;
490         model->check_promises(act->get_tid(), cv, act->cv);
491         cv->merge(act->cv);
492         return true;
493 }
494
495 bool ModelAction::has_synchronized_with(const ModelAction *act) const
496 {
497         return cv->synchronized_since(act);
498 }
499
500 /**
501  * Check whether 'this' happens before act, according to the memory-model's
502  * happens before relation. This is checked via the ClockVector constructs.
503  * @return true if this action's thread has synchronized with act's thread
504  * since the execution of act, false otherwise.
505  */
506 bool ModelAction::happens_before(const ModelAction *act) const
507 {
508         return act->cv->synchronized_since(this);
509 }
510
511 /** @brief Print nicely-formatted info about this ModelAction */
512 void ModelAction::print() const
513 {
514         const char *type_str, *mo_str;
515         switch (this->type) {
516         case MODEL_FIXUP_RELSEQ:
517                 type_str = "relseq fixup";
518                 break;
519         case THREAD_CREATE:
520                 type_str = "thread create";
521                 break;
522         case THREAD_START:
523                 type_str = "thread start";
524                 break;
525         case THREAD_YIELD:
526                 type_str = "thread yield";
527                 break;
528         case THREAD_JOIN:
529                 type_str = "thread join";
530                 break;
531         case THREAD_FINISH:
532                 type_str = "thread finish";
533                 break;
534         case ATOMIC_UNINIT:
535                 type_str = "uninitialized";
536                 break;
537         case ATOMIC_READ:
538                 type_str = "atomic read";
539                 break;
540         case ATOMIC_WRITE:
541                 type_str = "atomic write";
542                 break;
543         case ATOMIC_RMW:
544                 type_str = "atomic rmw";
545                 break;
546         case ATOMIC_FENCE:
547                 type_str = "fence";
548                 break;
549         case ATOMIC_RMWR:
550                 type_str = "atomic rmwr";
551                 break;
552         case ATOMIC_RMWC:
553                 type_str = "atomic rmwc";
554                 break;
555         case ATOMIC_INIT:
556                 type_str = "init atomic";
557                 break;
558         case ATOMIC_LOCK:
559                 type_str = "lock";
560                 break;
561         case ATOMIC_UNLOCK:
562                 type_str = "unlock";
563                 break;
564         case ATOMIC_TRYLOCK:
565                 type_str = "trylock";
566                 break;
567         case ATOMIC_WAIT:
568                 type_str = "wait";
569                 break;
570         case ATOMIC_NOTIFY_ONE:
571                 type_str = "notify one";
572                 break;
573         case ATOMIC_NOTIFY_ALL:
574                 type_str = "notify all";
575                 break;
576         default:
577                 type_str = "unknown type";
578         }
579
580         switch (this->order) {
581         case std::memory_order_relaxed:
582                 mo_str = "relaxed";
583                 break;
584         case std::memory_order_acquire:
585                 mo_str = "acquire";
586                 break;
587         case std::memory_order_release:
588                 mo_str = "release";
589                 break;
590         case std::memory_order_acq_rel:
591                 mo_str = "acq_rel";
592                 break;
593         case std::memory_order_seq_cst:
594                 mo_str = "seq_cst";
595                 break;
596         default:
597                 mo_str = "unknown";
598                 break;
599         }
600
601         model_print("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
602                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
603         if (is_read()) {
604                 if (reads_from)
605                         model_print("  Rf: %-3d", reads_from->get_seq_number());
606                 else if (reads_from_promise) {
607                         int idx = model->get_promise_number(reads_from_promise);
608                         if (idx >= 0)
609                                 model_print("  Rf: P%-2d", idx);
610                         else
611                                 model_print("  RF: P? ");
612                 } else
613                         model_print("  Rf: ?  ");
614         }
615         if (cv) {
616                 if (is_read())
617                         model_print(" ");
618                 else
619                         model_print("          ");
620                 cv->print();
621         } else
622                 model_print("\n");
623 }
624
625 /** @brief Get a (likely) unique hash for this ModelAction */
626 unsigned int ModelAction::hash() const
627 {
628         unsigned int hash = (unsigned int)this->type;
629         hash ^= ((unsigned int)this->order) << 3;
630         hash ^= seq_number << 5;
631         hash ^= id_to_int(tid) << 6;
632
633         if (is_read() && reads_from)
634                 hash ^= reads_from->get_seq_number();
635         return hash;
636 }
637
638 /**
639  * @brief Checks the NodeStack to see if a ModelAction is in our may-read-from set
640  * @param write The ModelAction to check for
641  * @return True if the ModelAction is found; false otherwise
642  */
643 bool ModelAction::may_read_from(const ModelAction *write) const
644 {
645         for (int i = 0; i < node->get_read_from_past_size(); i++)
646                 if (node->get_read_from_past(i) == write)
647                         return true;
648         return false;
649 }
650
651 /**
652  * @brief Checks the NodeStack to see if a Promise is in our may-read-from set
653  * @param promise The Promise to check for
654  * @return True if the Promise is found; false otherwise
655  */
656 bool ModelAction::may_read_from(const Promise *promise) const
657 {
658         for (int i = 0; i < node->get_read_from_promise_size(); i++)
659                 if (node->get_read_from_promise(i) == promise)
660                         return true;
661         return false;
662 }
663
664 /**
665  * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
666  * @return The mutex operated on by this action, if any; otherwise NULL
667  */
668 std::mutex * ModelAction::get_mutex() const
669 {
670         if (is_trylock() || is_lock() || is_unlock())
671                 return (std::mutex *)get_location();
672         else if (is_wait())
673                 return (std::mutex *)get_value();
674         else
675                 return NULL;
676 }