unlink the extra tmp file generated
[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         // model_print("position: %s\n", position);
188 }
189
190
191 /** @brief ModelAction destructor */
192 ModelAction::~ModelAction()
193 {
194         /**
195          * We can't free the clock vector:
196          * Clock vectors are snapshotting state. When we delete model actions,
197          * they are at the end of the node list and have invalid old clock
198          * vectors which have already been rolled back to an unallocated state.
199          */
200
201         /*
202            if (cv)
203                 delete cv; */
204 }
205
206 int ModelAction::getSize() const {
207         return size;
208 }
209
210 void ModelAction::copy_from_new(ModelAction *newaction)
211 {
212         seq_number = newaction->seq_number;
213 }
214
215 void ModelAction::set_seq_number(modelclock_t num)
216 {
217         /* ATOMIC_UNINIT actions should never have non-zero clock */
218         ASSERT(!is_uninitialized());
219         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
220         seq_number = num;
221 }
222
223 bool ModelAction::is_thread_start() const
224 {
225         return type == THREAD_START;
226 }
227
228 bool ModelAction::is_thread_join() const
229 {
230         return type == THREAD_JOIN || type == PTHREAD_JOIN;
231 }
232
233 bool ModelAction::is_mutex_op() const
234 {
235         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK || type == ATOMIC_WAIT || type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
236 }
237
238 bool ModelAction::is_lock() const
239 {
240         return type == ATOMIC_LOCK;
241 }
242
243 bool ModelAction::is_sleep() const
244 {
245         return type == THREAD_SLEEP;
246 }
247
248 bool ModelAction::is_wait() const {
249         return type == ATOMIC_WAIT;
250 }
251
252 bool ModelAction::is_notify() const {
253         return type == ATOMIC_NOTIFY_ONE || type == ATOMIC_NOTIFY_ALL;
254 }
255
256 bool ModelAction::is_notify_one() const {
257         return type == ATOMIC_NOTIFY_ONE;
258 }
259
260 bool ModelAction::is_unlock() const
261 {
262         return type == ATOMIC_UNLOCK;
263 }
264
265 bool ModelAction::is_trylock() const
266 {
267         return type == ATOMIC_TRYLOCK;
268 }
269
270 bool ModelAction::is_success_lock() const
271 {
272         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
273 }
274
275 bool ModelAction::is_failed_trylock() const
276 {
277         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
278 }
279
280 /** @return True if this operation is performed on a C/C++ atomic variable */
281 bool ModelAction::is_atomic_var() const
282 {
283         return is_read() || could_be_write();
284 }
285
286 bool ModelAction::is_uninitialized() const
287 {
288         return type == ATOMIC_UNINIT;
289 }
290
291 bool ModelAction::is_read() const
292 {
293         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS || type == ATOMIC_RMW;
294 }
295
296 bool ModelAction::is_write() const
297 {
298         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT || type == ATOMIC_UNINIT || type == NONATOMIC_WRITE;
299 }
300
301 bool ModelAction::could_be_write() const
302 {
303         return is_write() || is_rmwr();
304 }
305
306 bool ModelAction::is_yield() const
307 {
308         return type == THREAD_YIELD;
309 }
310
311 bool ModelAction::is_rmwr() const
312 {
313         return type == ATOMIC_RMWR || type == ATOMIC_RMWRCAS;
314 }
315
316 bool ModelAction::is_rmwrcas() const
317 {
318         return type == ATOMIC_RMWRCAS;
319 }
320
321 bool ModelAction::is_rmw() const
322 {
323         return type == ATOMIC_RMW;
324 }
325
326 bool ModelAction::is_rmwc() const
327 {
328         return type == ATOMIC_RMWC;
329 }
330
331 bool ModelAction::is_fence() const
332 {
333         return type == ATOMIC_FENCE;
334 }
335
336 bool ModelAction::is_initialization() const
337 {
338         return type == ATOMIC_INIT;
339 }
340
341 bool ModelAction::is_annotation() const
342 {
343         return type == ATOMIC_ANNOTATION;
344 }
345
346 bool ModelAction::is_relaxed() const
347 {
348         return order == std::memory_order_relaxed;
349 }
350
351 bool ModelAction::is_acquire() const
352 {
353         switch (order) {
354         case std::memory_order_acquire:
355         case std::memory_order_acq_rel:
356         case std::memory_order_seq_cst:
357                 return true;
358         default:
359                 return false;
360         }
361 }
362
363 bool ModelAction::is_release() const
364 {
365         switch (order) {
366         case std::memory_order_release:
367         case std::memory_order_acq_rel:
368         case std::memory_order_seq_cst:
369                 return true;
370         default:
371                 return false;
372         }
373 }
374
375 bool ModelAction::is_seqcst() const
376 {
377         return order == std::memory_order_seq_cst;
378 }
379
380 bool ModelAction::same_var(const ModelAction *act) const
381 {
382         if (act->is_wait() || is_wait()) {
383                 if (act->is_wait() && is_wait()) {
384                         if (((void *)value) == ((void *)act->value))
385                                 return true;
386                 } else if (is_wait()) {
387                         if (((void *)value) == act->location)
388                                 return true;
389                 } else if (act->is_wait()) {
390                         if (location == ((void *)act->value))
391                                 return true;
392                 }
393         }
394
395         return location == act->location;
396 }
397
398 bool ModelAction::same_thread(const ModelAction *act) const
399 {
400         return tid == act->tid;
401 }
402
403 void ModelAction::copy_typeandorder(ModelAction * act)
404 {
405         this->type = act->type;
406         this->order = act->order;
407 }
408
409 /**
410  * Get the Thread which is the operand of this action. This is only valid for
411  * THREAD_* operations (currently only for THREAD_CREATE and THREAD_JOIN). Note
412  * that this provides a central place for determining the conventions of Thread
413  * storage in ModelAction, where we generally aren't very type-safe (e.g., we
414  * store object references in a (void *) address.
415  *
416  * For THREAD_CREATE, this yields the Thread which is created.
417  * For THREAD_JOIN, this yields the Thread we are joining with.
418  *
419  * @return The Thread which this action acts on, if exists; otherwise NULL
420  */
421 Thread * ModelAction::get_thread_operand() const
422 {
423         if (type == THREAD_CREATE) {
424                 /* thread_operand is set in execution.cc */
425                 return thread_operand;
426         } else if (type == PTHREAD_CREATE) {
427                 return thread_operand;
428         } else if (type == THREAD_JOIN) {
429                 /* THREAD_JOIN uses (Thread *) for location */
430                 return (Thread *)get_location();
431         } else if (type == PTHREAD_JOIN) {
432                 // return NULL;
433                 // thread_operand is stored in execution::pthread_map;
434                 return (Thread *)get_location();
435         } else
436                 return NULL;
437 }
438
439 /**
440  * @brief Convert the read portion of an RMW
441  *
442  * Changes an existing read part of an RMW action into either:
443  *  -# a full RMW action in case of the completed write or
444  *  -# a READ action in case a failed action.
445  *
446  * @todo  If the memory_order changes, we may potentially need to update our
447  * clock vector.
448  *
449  * @param act The second half of the RMW (either RMWC or RMW)
450  */
451 void ModelAction::process_rmw(ModelAction *act)
452 {
453         this->order = act->order;
454         if (act->is_rmwc())
455                 this->type = ATOMIC_READ;
456         else if (act->is_rmw()) {
457                 this->type = ATOMIC_RMW;
458                 this->value = act->value;
459         }
460 }
461
462 /**
463  * @brief Check if this action should be backtracked with another, due to
464  * potential synchronization
465  *
466  * The is_synchronizing method should only explore interleavings if:
467  *  -# the operations are seq_cst and don't commute or
468  *  -# the reordering may establish or break a synchronization relation.
469  *
470  * Other memory operations will be dealt with by using the reads_from relation.
471  *
472  * @param act The action to consider exploring a reordering
473  * @return True, if we have to explore a reordering; otherwise false
474  */
475 bool ModelAction::could_synchronize_with(const ModelAction *act) const
476 {
477         // Same thread can't be reordered
478         if (same_thread(act))
479                 return false;
480
481         // Different locations commute
482         if (!same_var(act) && !is_fence() && !act->is_fence())
483                 return false;
484
485         // Explore interleavings of seqcst writes/fences to guarantee total
486         // order of seq_cst operations that don't commute
487         if ((could_be_write() || act->could_be_write() || is_fence() || act->is_fence()) && is_seqcst() && act->is_seqcst())
488                 return true;
489
490         // Explore synchronizing read/write pairs
491         if (is_acquire() && act->is_release() && is_read() && act->could_be_write())
492                 return true;
493
494         // lock just released...we can grab lock
495         if ((is_lock() || is_trylock()) && (act->is_unlock() || act->is_wait()))
496                 return true;
497
498         // lock just acquired...we can fail to grab lock
499         if (is_trylock() && act->is_success_lock())
500                 return true;
501
502         // other thread stalling on lock...we can release lock
503         if (is_unlock() && (act->is_trylock() || act->is_lock()))
504                 return true;
505
506         if (is_trylock() && (act->is_unlock() || act->is_wait()))
507                 return true;
508
509         if (is_notify() && act->is_wait())
510                 return true;
511
512         if (is_wait() && act->is_notify())
513                 return true;
514
515         // Otherwise handle by reads_from relation
516         return false;
517 }
518
519 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
520 {
521         // Must be different threads to reorder
522         if (same_thread(act))
523                 return false;
524
525         // Try to reorder a lock past a successful lock
526         if (act->is_success_lock())
527                 return true;
528
529         // Try to push a successful trylock past an unlock
530         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
531                 return true;
532
533         // Try to push a successful trylock past a wait
534         if (act->is_wait() && is_trylock() && value == VALUE_TRYSUCCESS)
535                 return true;
536
537         return false;
538 }
539
540 /**
541  * Create a new clock vector for this action. Note that this function allows a
542  * user to clobber (and leak) a ModelAction's existing clock vector. A user
543  * should ensure that the vector has already either been rolled back
544  * (effectively "freed") or freed.
545  *
546  * @param parent A ModelAction from which to inherit a ClockVector
547  */
548 void ModelAction::create_cv(const ModelAction *parent)
549 {
550         if (parent)
551                 cv = new ClockVector(parent->cv, this);
552         else
553                 cv = new ClockVector(NULL, this);
554 }
555
556 void ModelAction::set_try_lock(bool obtainedlock)
557 {
558         value = obtainedlock ? VALUE_TRYSUCCESS : VALUE_TRYFAILED;
559 }
560
561 /**
562  * @brief Get the value read by this load
563  *
564  * We differentiate this function from ModelAction::get_write_value and
565  * ModelAction::get_value for the purpose of RMW's, which may have both a
566  * 'read' and a 'write' value.
567  *
568  * Note: 'this' must be a load.
569  *
570  * @return The value read by this load
571  */
572 uint64_t ModelAction::get_reads_from_value() const
573 {
574         ASSERT(is_read());
575         if (reads_from)
576                 return reads_from->get_write_value();
577
578         return VALUE_NONE;      // Only for new actions with no reads-from
579 }
580
581 /**
582  * @brief Get the value written by this store
583  *
584  * We differentiate this function from ModelAction::get_reads_from_value and
585  * ModelAction::get_value for the purpose of RMW's, which may have both a
586  * 'read' and a 'write' value.
587  *
588  * Note: 'this' must be a store.
589  *
590  * @return The value written by this store
591  */
592 uint64_t ModelAction::get_write_value() const
593 {
594         ASSERT(is_write());
595         return value;
596 }
597
598 /**
599  * @brief Get the value returned by this action
600  *
601  * For atomic reads (including RMW), an operation returns the value it read.
602  * For atomic writes, an operation returns the value it wrote. For other
603  * operations, the return value varies (sometimes is a "don't care"), but the
604  * value is simply stored in the "value" field.
605  *
606  * @return This action's return value
607  */
608 uint64_t ModelAction::get_return_value() const
609 {
610         if (is_read())
611                 return get_reads_from_value();
612         else if (is_write())
613                 return get_write_value();
614         else
615                 return value;
616 }
617
618 /**
619  * Update the model action's read_from action
620  * @param act The action to read from; should be a write
621  */
622 void ModelAction::set_read_from(ModelAction *act)
623 {
624         ASSERT(act);
625
626         reads_from = act;
627
628         if (act->is_uninitialized()) {  // WL
629                 uint64_t val = *((uint64_t *) location);
630                 ModelAction * act_uninitialized = (ModelAction *)act;
631                 act_uninitialized->set_value(val);
632                 reads_from = act_uninitialized;
633
634 // disabled by WL, because LLVM IR is unable to detect atomic init
635 /*              model->assert_bug("May read from uninitialized atomic:\n"
636                                 "    action %d, thread %d, location %p (%s, %s)",
637                                 seq_number, id_to_int(tid), location,
638                                 get_type_str(), get_mo_str());
639  */
640         }
641 }
642
643 /**
644  * Synchronize the current thread with the thread corresponding to the
645  * ModelAction parameter.
646  * @param act The ModelAction to synchronize with
647  * @return True if this is a valid synchronization; false otherwise
648  */
649 bool ModelAction::synchronize_with(const ModelAction *act)
650 {
651         if (*this < *act)
652                 return false;
653         cv->merge(act->cv);
654         return true;
655 }
656
657 bool ModelAction::has_synchronized_with(const ModelAction *act) const
658 {
659         return cv->synchronized_since(act);
660 }
661
662 /**
663  * Check whether 'this' happens before act, according to the memory-model's
664  * happens before relation. This is checked via the ClockVector constructs.
665  * @return true if this action's thread has synchronized with act's thread
666  * since the execution of act, false otherwise.
667  */
668 bool ModelAction::happens_before(const ModelAction *act) const
669 {
670         return act->cv->synchronized_since(this);
671 }
672
673 const char * ModelAction::get_type_str() const
674 {
675         switch (this->type) {
676         case THREAD_CREATE: return "thread create";
677         case THREAD_START: return "thread start";
678         case THREAD_YIELD: return "thread yield";
679         case THREAD_JOIN: return "thread join";
680         case THREAD_FINISH: return "thread finish";
681         case THREAD_SLEEP: return "thread sleep";
682         case THREADONLY_FINISH: return "pthread_exit finish";
683
684         case PTHREAD_CREATE: return "pthread create";
685         case PTHREAD_JOIN: return "pthread join";
686
687         case ATOMIC_UNINIT: return "uninitialized";
688         case NONATOMIC_WRITE: return "nonatomic write";
689         case ATOMIC_READ: return "atomic read";
690         case ATOMIC_WRITE: return "atomic write";
691         case ATOMIC_RMW: return "atomic rmw";
692         case ATOMIC_FENCE: return "fence";
693         case ATOMIC_RMWR: return "atomic rmwr";
694         case ATOMIC_RMWRCAS: return "atomic rmwrcas";
695         case ATOMIC_RMWC: return "atomic rmwc";
696         case ATOMIC_INIT: return "init atomic";
697         case ATOMIC_LOCK: return "lock";
698         case ATOMIC_UNLOCK: return "unlock";
699         case ATOMIC_TRYLOCK: return "trylock";
700         case ATOMIC_WAIT: return "wait";
701         case ATOMIC_NOTIFY_ONE: return "notify one";
702         case ATOMIC_NOTIFY_ALL: return "notify all";
703         case ATOMIC_ANNOTATION: return "annotation";
704         default: return "unknown type";
705         };
706 }
707
708 const char * ModelAction::get_mo_str() const
709 {
710         switch (this->order) {
711         case std::memory_order_relaxed: return "relaxed";
712         case std::memory_order_acquire: return "acquire";
713         case std::memory_order_release: return "release";
714         case std::memory_order_acq_rel: return "acq_rel";
715         case std::memory_order_seq_cst: return "seq_cst";
716         default: return "unknown";
717         }
718 }
719
720 /** @brief Print nicely-formatted info about this ModelAction */
721 void ModelAction::print() const
722 {
723         const char *type_str = get_type_str(), *mo_str = get_mo_str();
724
725         model_print("%-4d %-2d   %-14s  %7s  %14p   %-#18" PRIx64,
726                                                         seq_number, id_to_int(tid), type_str, mo_str, location, get_return_value());
727         if (is_read()) {
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 }