Remove the uses of inst_act_maps
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <stdlib.h>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "wildcard.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /** @brief A special value to represent a successful trylock */
16 #define VALUE_TRYSUCCESS 1
17
18 /** @brief A special value to represent a failed trylock */
19 #define VALUE_TRYFAILED 0
20
21 /**
22  * @brief Construct a new ModelAction
23  *
24  * @param type The type of action
25  * @param order The memory order of this action. A "don't care" for non-ATOMIC
26  * actions (e.g., THREAD_* or MODEL_* actions).
27  * @param loc The location that this action acts upon
28  * @param value (optional) A value associated with the action (e.g., the value
29  * read or written). Defaults to a given macro constant, for debugging purposes.
30  * @param thread (optional) The Thread in which this action occurred. If NULL
31  * (default), then a Thread is assigned according to the scheduler.
32  */
33 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
34                                                                                                  uint64_t value, Thread *thread) :
35         location(loc),
36         position(NULL),
37         reads_from(NULL),
38         last_fence_release(NULL),
39         cv(NULL),
40         rf_cv(NULL),
41         trace_ref(NULL),
42         thrdmap_ref(NULL),
43         action_ref(NULL),
44         value(value),
45         type(type),
46         order(order),
47         original_order(order),
48         seq_number(ACTION_INITIAL_CLOCK)
49 {
50         /* References to NULL atomic variables can end up here */
51         ASSERT(loc || type == ATOMIC_FENCE || type == ATOMIC_NOP);
52
53         Thread *t = thread ? thread : thread_current();
54         this->tid = t!= NULL ? t->get_id() : -1;
55 }
56
57
58 /**
59  * @brief Construct a new ModelAction for sleep actions
60  *
61  * @param type The type of action: THREAD_SLEEP
62  * @param order The memory order of this action. A "don't care" for non-ATOMIC
63  * actions (e.g., THREAD_* or MODEL_* actions).
64  * @param loc The location that this action acts upon
65  * @param value The time duration a thread is scheduled to sleep.
66  * @param _time The this sleep action is constructed
67  */
68 ModelAction::ModelAction(action_type_t type, memory_order order, uint64_t value, uint64_t _time) :
69         location(NULL),
70         position(NULL),
71         time(_time),
72         last_fence_release(NULL),
73         cv(NULL),
74         rf_cv(NULL),
75         trace_ref(NULL),
76         thrdmap_ref(NULL),
77         action_ref(NULL),
78         value(value),
79         type(type),
80         order(order),
81         original_order(order),
82         seq_number(ACTION_INITIAL_CLOCK)
83 {
84         Thread *t = thread_current();
85         this->tid = t!= NULL ? t->get_id() : -1;
86 }
87
88 /**
89  * @brief Construct a new ModelAction
90  *
91  * @param type The type of action
92  * @param order The memory order of this action. A "don't care" for non-ATOMIC
93  * actions (e.g., THREAD_* or MODEL_* actions).
94  * @param loc The location that this action acts upon
95  * @param value (optional) A value associated with the action (e.g., the value
96  * read or written). Defaults to a given macro constant, for debugging purposes.
97  * @param size (optional) The Thread in which this action occurred. If NULL
98  * (default), then a Thread is assigned according to the scheduler.
99  */
100 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
101                                                                                                  uint64_t value, int size) :
102         location(loc),
103         position(NULL),
104         reads_from(NULL),
105         last_fence_release(NULL),
106         cv(NULL),
107         rf_cv(NULL),
108         trace_ref(NULL),
109         thrdmap_ref(NULL),
110         action_ref(NULL),
111         value(value),
112         type(type),
113         order(order),
114         original_order(order),
115         seq_number(ACTION_INITIAL_CLOCK)
116 {
117         /* References to NULL atomic variables can end up here */
118         ASSERT(loc);
119         this->size = size;
120         Thread *t = thread_current();
121         this->tid = t->get_id();
122 }
123
124
125 /**
126  * @brief Construct a new ModelAction with source line number (requires llvm support)
127  *
128  * @param type The type of action
129  * @param order The memory order of this action. A "don't care" for non-ATOMIC
130  * actions (e.g., THREAD_* or MODEL_* actions).
131  * @param loc The location that this action acts upon
132  * @param value (optional) A value associated with the action (e.g., the value
133  * read or written). Defaults to a given macro constant, for debugging purposes.
134  * @param size (optional) The Thread in which this action occurred. If NULL
135  * (default), then a Thread is assigned according to the scheduler.
136  */
137 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order, void *loc,
138                                                                                                  uint64_t value, int size) :
139         location(loc),
140         position(position),
141         reads_from(NULL),
142         last_fence_release(NULL),
143         cv(NULL),
144         rf_cv(NULL),
145         trace_ref(NULL),
146         thrdmap_ref(NULL),
147         action_ref(NULL),
148         value(value),
149         type(type),
150         order(order),
151         original_order(order),
152         seq_number(ACTION_INITIAL_CLOCK)
153 {
154         /* References to NULL atomic variables can end up here */
155         ASSERT(loc);
156         this->size = size;
157         Thread *t = thread_current();
158         this->tid = t->get_id();
159 }
160
161
162 /**
163  * @brief Construct a new ModelAction with source line number (requires llvm support)
164  *
165  * @param type The type of action
166  * @param position The source line number of this atomic operation
167  * @param order The memory order of this action. A "don't care" for non-ATOMIC
168  * actions (e.g., THREAD_* or MODEL_* actions).
169  * @param loc The location that this action acts upon
170  * @param value (optional) A value associated with the action (e.g., the value
171  * read or written). Defaults to a given macro constant, for debugging purposes.
172  * @param thread (optional) The Thread in which this action occurred. If NULL
173  * (default), then a Thread is assigned according to the scheduler.
174  */
175 ModelAction::ModelAction(action_type_t type, const char * position, memory_order order,
176                                                                                                  void *loc, uint64_t value, Thread *thread) :
177         location(loc),
178         position(position),
179         reads_from(NULL),
180         last_fence_release(NULL),
181         cv(NULL),
182         rf_cv(NULL),
183         trace_ref(NULL),
184         thrdmap_ref(NULL),
185         action_ref(NULL),
186         value(value),
187         type(type),
188         order(order),
189         original_order(order),
190         seq_number(ACTION_INITIAL_CLOCK)
191 {
192         /* References to NULL atomic variables can end up here */
193         ASSERT(loc || type == ATOMIC_FENCE);
194
195         Thread *t = thread ? thread : thread_current();
196         this->tid = t->get_id();
197 }
198
199
200 /** @brief ModelAction destructor */
201 ModelAction::~ModelAction()
202 {
203         /**
204          * We can't free the clock vector:
205          * Clock vectors are snapshotting state. When we delete model actions,
206          * they are at the end of the node list and have invalid old clock
207          * vectors which have already been rolled back to an unallocated state.
208          */
209
210         /*
211            if (cv)
212                 delete cv; */
213 }
214
215 int ModelAction::getSize() const {
216         return size;
217 }
218
219 void ModelAction::copy_from_new(ModelAction *newaction)
220 {
221         seq_number = newaction->seq_number;
222 }
223
224 void ModelAction::set_seq_number(modelclock_t num)
225 {
226         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
227         seq_number = num;
228 }
229
230 void ModelAction::reset_seq_number()
231 {
232         seq_number = 0;
233 }
234
235 bool ModelAction::is_thread_start() const
236 {
237         return type == THREAD_START;
238 }
239
240 bool ModelAction::is_thread_join() const
241 {
242         return type == THREAD_JOIN || type == PTHREAD_JOIN;
243 }
244
245 bool ModelAction::is_mutex_op() const
246 {
247         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
248 }
249
250 bool ModelAction::is_lock() const
251 {
252         return type == ATOMIC_LOCK;
253 }
254
255 bool ModelAction::is_sleep() const
256 {
257         return type == THREAD_SLEEP;
258 }
259
260 bool ModelAction::is_wait() const {
261         return type == ATOMIC_WAIT || type == ATOMIC_TIMEDWAIT;
262 }
263
264 bool ModelAction::is_notify() const {
265         return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
266 }
267
268 bool ModelAction::is_notify_one() const {
269         return type == ATOMIC_NOTIFY_ONE;
270 }
271
272 bool ModelAction::is_unlock() const
273 {
274         return type == ATOMIC_UNLOCK;
275 }
276
277 bool ModelAction::is_trylock() const
278 {
279         return type == ATOMIC_TRYLOCK;
280 }
281
282 bool ModelAction::is_success_lock() const
283 {
284         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
285 }
286
287 bool ModelAction::is_failed_trylock() const
288 {
289         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
290 }
291
292 /** @return True if this operation is performed on a C/C++ atomic variable */
293 bool ModelAction::is_atomic_var() const
294 {
295         return is_read() || could_be_write();
296 }
297
298 bool ModelAction::is_read() const
299 {
300         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
301 }
302
303 bool ModelAction::is_write() const
304 {
305         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == NONATOMIC_WRITE;
306 }
307
308 bool ModelAction::is_create() const
309 {
310         return type == THREAD_CREATE || type == PTHREAD_CREATE;
311 }
312
313 bool ModelAction::is_free() const
314 {
315         return type == READY_FREE;
316 }
317
318 bool ModelAction::could_be_write() const
319 {
320         return is_write() || is_rmwr();
321 }
322
323 bool ModelAction::is_yield() const
324 {
325         return type == THREAD_YIELD;
326 }
327
328 bool ModelAction::is_rmwr() const
329 {
330         return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
331 }
332
333 bool ModelAction::is_rmwrcas() const
334 {
335         return type == ATOMIC_RMWRCAS;
336 }
337
338 bool ModelAction::is_rmw() const
339 {
340         return type == ATOMIC_RMW;
341 }
342
343 bool ModelAction::is_rmwc() const
344 {
345         return type == ATOMIC_RMWC;
346 }
347
348 bool ModelAction::is_fence() const
349 {
350         return type == ATOMIC_FENCE;
351 }
352
353 bool ModelAction::is_initialization() const
354 {
355         return type == ATOMIC_INIT;
356 }
357
358 bool ModelAction::is_annotation() const
359 {
360         return type == ATOMIC_ANNOTATION;
361 }
362
363 bool ModelAction::is_relaxed() const
364 {
365         return order == std::memory_order_relaxed;
366 }
367
368 bool ModelAction::is_acquire() const
369 {
370         switch (order) {
371         case std::memory_order_acquire:
372         case std::memory_order_acq_rel:
373         case std::memory_order_seq_cst:
374                 return true;
375         default:
376                 return false;
377         }
378 }
379
380 bool ModelAction::is_release() const
381 {
382         switch (order) {
383         case std::memory_order_release:
384         case std::memory_order_acq_rel:
385         case std::memory_order_seq_cst:
386                 return true;
387         default:
388                 return false;
389         }
390 }
391
392 bool ModelAction::is_seqcst() const
393 {
394         return order == std::memory_order_seq_cst;
395 }
396
397 bool ModelAction::same_var(const ModelAction *act) const
398 {
399         if (act->is_wait() || is_wait()) {
400                 if (act->is_wait() && is_wait()) {
401                         if (((void *)value) == ((void *)act->value))
402                                 return true;
403                 } else if (is_wait()) {
404                         if (((void *)value) == act->location)
405                                 return true;
406                 } else if (act->is_wait()) {
407                         if (location == ((void *)act->value))
408                                 return true;
409                 }
410         }
411
412         return location == act->location;
413 }
414
415 bool ModelAction::same_thread(const ModelAction *act) const
416 {
417         return tid == act->tid;
418 }
419
420 void ModelAction::copy_typeandorder(ModelAction * act)
421 {
422         this->type = act->type;
423         this->order = act->order;
424 }
425
426 /**
427  * Get the Thread which is the operand of this action. This is only valid for
428  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
429  * that this provides a central place for determining the conventions of Thread
430  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
431  * store object references in a (void *) address.
432  *
433  * For THREAD_CREATE, this yields the Thread which is created.
434  * For THREAD_JOIN, this yields the Thread we are joining with.
435  *
436  * @return The Thread which this action acts on, if exists; otherwise NULL
437  */
438 Thread * ModelAction::get_thread_operand() const
439 {
440         if (type == THREAD_CREATE) {
441                 /* thread_operand is set in execution.cc */
442                 return thread_operand;
443         } else if (type == PTHREAD_CREATE) {
444                 return thread_operand;
445         } else if (type == THREAD_JOIN) {
446                 /* THREAD_JOIN uses (Thread *) for location */
447                 return (Thread *)get_location();
448         } else if (type == PTHREAD_JOIN) {
449                 // return NULL;
450                 // thread_operand is stored in execution::pthread_map;
451                 return (Thread *)get_location();
452         } else
453                 return NULL;
454 }
455
456 /**
457  * @brief Convert the read portion of an RMW
458  *
459  * Changes an existing read part of an RMW action into either:
460  *  -# a full RMW action in case of the completed write or
461  *  -# a READ action in case a failed action.
462  *
463  * @todo  If the memory_order changes, we may potentially need to update our
464  * clock vector.
465  *
466  * @param act The second half of the RMW (either RMWC or RMW)
467  */
468 void ModelAction::process_rmw(ModelAction *act)
469 {
470         this->order = act->order;
471         if (act->is_rmwc())
472                 this->type = ATOMIC_READ;
473         else if (act->is_rmw()) {
474                 this->type = ATOMIC_RMW;
475                 this->value = act->value;
476         }
477 }
478
479 /**
480  * @brief Check if this action should be backtracked with another, due to
481  * potential synchronization
482  *
483  * The is_synchronizing method should only explore interleavings if:
484  *  -# the operations are seq_cst and don't commute or
485  *  -# the reordering may establish or break a synchronization relation.
486  *
487  * Other memory operations will be dealt with by using the reads_from relation.
488  *
489  * @param act The action to consider exploring a reordering
490  * @return True, if we have to explore a reordering; otherwise false
491  */
492 bool ModelAction::could_synchronize_with(const ModelAction *act) const
493 {
494         // Same thread can't be reordered
495         if (same_thread(act))
496                 return false;
497
498         // Different locations commute
499         if (!same_var(act) && !is_fence() && !act->is_fence())
500                 return false;
501
502         // Explore interleavings of seqcst writes/fences to guarantee total
503         // order of seq_cst operations that don't commute
504         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
505                 return true;
506
507         // Explore synchronizing read/write pairs
508         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
509                 return true;
510
511         // lock just released...we can grab lock
512         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
513                 return true;
514
515         // lock just acquired...we can fail to grab lock
516         if (is_trylock() && act->is_success_lock())
517                 return true;
518
519         // other thread stalling on lock...we can release lock
520         if (is_unlock() && (act->is_trylock() || act->is_lock()))
521                 return true;
522
523         if (is_trylock() && (act->is_unlock() || act->is_wait()))
524                 return true;
525
526         if (is_notify() && act->is_wait())
527                 return true;
528
529         if (is_wait() && act->is_notify())
530                 return true;
531
532         // Otherwise handle by reads_from relation
533         return false;
534 }
535
536 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
537 {
538         // Must be different threads to reorder
539         if (same_thread(act))
540                 return false;
541
542         // Try to reorder a lock past a successful lock
543         if (act->is_success_lock())
544                 return true;
545
546         // Try to push a successful trylock past an unlock
547         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
548                 return true;
549
550         // Try to push a successful trylock past a wait
551         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
552                 return true;
553
554         return false;
555 }
556
557 /**
558  * Create a new clock vector for this action. Note that this function allows a
559  * user to clobber (and leak) a ModelAction's existing clock vector. A user
560  * should ensure that the vector has already either been rolled back
561  * (effectively "freed") or freed.
562  *
563  * @param parent A ModelAction from which to inherit a ClockVector
564  */
565 void ModelAction::create_cv(const ModelAction *parent)
566 {
567         if (parent)
568                 cv = new ClockVector(parent->cv, this);
569         else
570                 cv = new ClockVector(NULL, this);
571 }
572
573 void ModelAction::set_try_lock(bool obtainedlock)
574 {
575         value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
576 }
577
578 /**
579  * @brief Get the value read by this load
580  *
581  * We differentiate this function from ModelAction::get_write_value and
582  * ModelAction::get_value for the purpose of RMW's, which may have both a
583  * 'read' and a 'write' value.
584  *
585  * Note: 'this' must be a load.
586  *
587  * @return The value read by this load
588  */
589 uint64_t ModelAction::get_reads_from_value() const
590 {
591         ASSERT(is_read());
592         if (reads_from)
593                 return reads_from->get_write_value();
594
595         return VALUE_NONE;      // Only for new actions with no reads-from
596 }
597
598 /**
599  * @brief Get the value written by this store
600  *
601  * We differentiate this function from ModelAction::get_reads_from_value and
602  * ModelAction::get_value for the purpose of RMW's, which may have both a
603  * 'read' and a 'write' value.
604  *
605  * Note: 'this' must be a store.
606  *
607  * @return The value written by this store
608  */
609 uint64_t ModelAction::get_write_value() const
610 {
611         ASSERT(is_write());
612         return value;
613 }
614
615 /**
616  * @brief Get the value returned by this action
617  *
618  * For atomic reads (including RMW), an operation returns the value it read.
619  * For atomic writes, an operation returns the value it wrote. For other
620  * operations, the return value varies (sometimes is a "don't care"), but the
621  * value is simply stored in the "value" field.
622  *
623  * @return This action's return value
624  */
625 uint64_t ModelAction::get_return_value() const
626 {
627         if (is_read())
628                 return get_reads_from_value();
629         else if (is_write())
630                 return get_write_value();
631         else
632                 return value;
633 }
634
635 /**
636  * Update the model action's read_from action
637  * @param act The action to read from; should be a write
638  */
639 void ModelAction::set_read_from(ModelAction *act)
640 {
641         ASSERT(act);
642
643         reads_from = act;
644 }
645
646 /**
647  * Synchronize the current thread with the thread corresponding to the
648  * ModelAction parameter.
649  * @param act The ModelAction to synchronize with
650  * @return True if this is a valid synchronization; false otherwise
651  */
652 bool ModelAction::synchronize_with(const ModelAction *act)
653 {
654         if (*this < *act)
655                 return false;
656         cv->merge(act->cv);
657         return true;
658 }
659
660 bool ModelAction::has_synchronized_with(const ModelAction *act) const
661 {
662         return cv->synchronized_since(act);
663 }
664
665 /**
666  * Check whether 'this' happens before act, according to the memory-model's
667  * happens before relation. This is checked via the ClockVector constructs.
668  * @return true if this action's thread has synchronized with act's thread
669  * since the execution of act, false otherwise.
670  */
671 bool ModelAction::happens_before(const ModelAction *act) const
672 {
673         return act->cv->synchronized_since(this);
674 }
675
676 const char * ModelAction::get_type_str() const
677 {
678         switch (this->type) {
679         case THREAD_CREATE: return "thread create";
680         case THREAD_START: return "thread start";
681         case THREAD_YIELD: return "thread yield";
682         case THREAD_JOIN: return "thread join";
683         case THREAD_FINISH: return "thread finish";
684         case THREAD_SLEEP: return "thread sleep";
685         case THREADONLY_FINISH: return "pthread_exit finish";
686
687         case PTHREAD_CREATE: return "pthread create";
688         case PTHREAD_JOIN: return "pthread join";
689
690         case NONATOMIC_WRITE: return "nonatomic write";
691         case ATOMIC_READ: return "atomic read";
692         case ATOMIC_WRITE: return "atomic write";
693         case ATOMIC_RMW: return "atomic rmw";
694         case ATOMIC_FENCE: return "fence";
695         case ATOMIC_RMWR: return "atomic rmwr";
696         case ATOMIC_RMWRCAS: return "atomic rmwrcas";
697         case ATOMIC_RMWC: return "atomic rmwc";
698         case ATOMIC_INIT: return "init atomic";
699         case ATOMIC_LOCK: return "lock";
700         case ATOMIC_UNLOCK: return "unlock";
701         case ATOMIC_TRYLOCK: return "trylock";
702         case ATOMIC_WAIT: return "wait";
703         case ATOMIC_TIMEDWAIT: return "timed wait";
704         case ATOMIC_NOTIFY_ONE: return "notify one";
705         case ATOMIC_NOTIFY_ALL: return "notify all";
706         case ATOMIC_ANNOTATION: return "annotation";
707         default: return "unknown type";
708         };
709 }
710
711 const char * ModelAction::get_mo_str() const
712 {
713         switch (this->order) {
714         case std::memory_order_relaxed: return "relaxed";
715         case std::memory_order_acquire: return "acquire";
716         case std::memory_order_release: return "release";
717         case std::memory_order_acq_rel: return "acq_rel";
718         case std::memory_order_seq_cst: return "seq_cst";
719         default: return "unknown";
720         }
721 }
722
723 /** @brief Print nicely-formatted info about this ModelAction */
724 void ModelAction::print() const
725 {
726         const char *type_str = get_type_str(), *mo_str = get_mo_str();
727
728         model_print("%-4d %-2d   %-14s  %7s  %14p   %-#18" PRIx64,
729                                                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
730         if (is_read()) {
731                 if (reads_from)
732                         model_print("  %-3d", reads_from->get_seq_number());
733                 else
734                         model_print("  ?  ");
735         }
736         if (cv) {
737                 if (is_read())
738                         model_print(" ");
739                 else
740                         model_print("      ");
741                 cv->print();
742         } else
743                 model_print("\n");
744 }
745
746 /** @brief Get a (likely) unique hash for this ModelAction */
747 unsigned int ModelAction::hash() const
748 {
749         unsigned int hash = (unsigned int)this->type;
750         hash ^= ((unsigned int)this->order) << 3;
751         hash ^= seq_number << 5;
752         hash ^= id_to_int(tid) << 6;
753
754         if (is_read()) {
755                 if (reads_from)
756                         hash ^= reads_from->get_seq_number();
757                 hash ^= get_reads_from_value();
758         }
759         return hash;
760 }
761
762 /**
763  * Only valid for LOCK, TRY_LOCK, UNLOCK, and WAIT operations.
764  * @return The mutex operated on by this action, if any; otherwise NULL
765  */
766 cdsc::mutex * ModelAction::get_mutex() const
767 {
768         if (is_trylock() || is_lock() || is_unlock())
769                 return (cdsc::mutex *)get_location();
770         else if (is_wait())
771                 return (cdsc::mutex *)get_value();
772         else
773                 return NULL;
774 }