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