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