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