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