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