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