700f25c017b1d44aa310eee2f4518c7e051abf1c
[c11tester.git] / model.cc
1 #include <stdio.h>
2 #include <algorithm>
3 #include <mutex>
4
5 #include "model.h"
6 #include "action.h"
7 #include "nodestack.h"
8 #include "schedule.h"
9 #include "snapshot-interface.h"
10 #include "common.h"
11 #include "clockvector.h"
12 #include "cyclegraph.h"
13 #include "promise.h"
14 #include "datarace.h"
15 #include "threads-model.h"
16 #include "output.h"
17
18 #define INITIAL_THREAD_ID       0
19
20 ModelChecker *model;
21
22 struct bug_message {
23         bug_message(const char *str) {
24                 const char *fmt = "  [BUG] %s\n";
25                 msg = (char *)snapshot_malloc(strlen(fmt) + strlen(str));
26                 sprintf(msg, fmt, str);
27         }
28         ~bug_message() { if (msg) snapshot_free(msg); }
29
30         char *msg;
31         void print() { model_print("%s", msg); }
32
33         SNAPSHOTALLOC
34 };
35
36 /**
37  * Structure for holding small ModelChecker members that should be snapshotted
38  */
39 struct model_snapshot_members {
40         model_snapshot_members() :
41                 current_action(NULL),
42                 /* First thread created will have id INITIAL_THREAD_ID */
43                 next_thread_id(INITIAL_THREAD_ID),
44                 used_sequence_numbers(0),
45                 nextThread(NULL),
46                 next_backtrack(NULL),
47                 bugs(),
48                 stats(),
49                 failed_promise(false),
50                 too_many_reads(false),
51                 bad_synchronization(false),
52                 asserted(false)
53         { }
54
55         ~model_snapshot_members() {
56                 for (unsigned int i = 0; i < bugs.size(); i++)
57                         delete bugs[i];
58                 bugs.clear();
59         }
60
61         ModelAction *current_action;
62         unsigned int next_thread_id;
63         modelclock_t used_sequence_numbers;
64         Thread *nextThread;
65         ModelAction *next_backtrack;
66         std::vector< bug_message *, SnapshotAlloc<bug_message *> > bugs;
67         struct execution_stats stats;
68         bool failed_promise;
69         bool too_many_reads;
70         /** @brief Incorrectly-ordered synchronization was made */
71         bool bad_synchronization;
72         bool asserted;
73
74         SNAPSHOTALLOC
75 };
76
77 /** @brief Constructor */
78 ModelChecker::ModelChecker(struct model_params params) :
79         /* Initialize default scheduler */
80         params(params),
81         scheduler(new Scheduler()),
82         diverge(NULL),
83         earliest_diverge(NULL),
84         action_trace(new action_list_t()),
85         thread_map(new HashTable<int, Thread *, int>()),
86         obj_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
87         lock_waiters_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
88         condvar_waiters_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
89         obj_thrd_map(new HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 >()),
90         promises(new std::vector< Promise *, SnapshotAlloc<Promise *> >()),
91         futurevalues(new std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> >()),
92         pending_rel_seqs(new std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> >()),
93         thrd_last_action(new std::vector< ModelAction *, SnapshotAlloc<ModelAction *> >(1)),
94         node_stack(new NodeStack()),
95         priv(new struct model_snapshot_members()),
96         mo_graph(new CycleGraph())
97 {
98         /* Initialize a model-checker thread, for special ModelActions */
99         model_thread = new Thread(get_next_id());
100         thread_map->put(id_to_int(model_thread->get_id()), model_thread);
101 }
102
103 /** @brief Destructor */
104 ModelChecker::~ModelChecker()
105 {
106         for (unsigned int i = 0; i < get_num_threads(); i++)
107                 delete thread_map->get(i);
108         delete thread_map;
109
110         delete obj_thrd_map;
111         delete obj_map;
112         delete lock_waiters_map;
113         delete condvar_waiters_map;
114         delete action_trace;
115
116         for (unsigned int i = 0; i < promises->size(); i++)
117                 delete (*promises)[i];
118         delete promises;
119
120         delete pending_rel_seqs;
121
122         delete thrd_last_action;
123         delete node_stack;
124         delete scheduler;
125         delete mo_graph;
126         delete priv;
127 }
128
129 static action_list_t * get_safe_ptr_action(HashTable<const void *, action_list_t *, uintptr_t, 4> * hash, void * ptr) {
130         action_list_t * tmp=hash->get(ptr);
131         if (tmp==NULL) {
132                 tmp=new action_list_t();
133                 hash->put(ptr, tmp);
134         }
135         return tmp;
136 }
137
138 static std::vector<action_list_t> * get_safe_ptr_vect_action(HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4> * hash, void * ptr) {
139         std::vector<action_list_t> * tmp=hash->get(ptr);
140         if (tmp==NULL) {
141                 tmp=new std::vector<action_list_t>();
142                 hash->put(ptr, tmp);
143         }
144         return tmp;
145 }
146
147 /**
148  * Restores user program to initial state and resets all model-checker data
149  * structures.
150  */
151 void ModelChecker::reset_to_initial_state()
152 {
153         DEBUG("+++ Resetting to initial state +++\n");
154         node_stack->reset_execution();
155
156         /* Print all model-checker output before rollback */
157         fflush(model_out);
158
159         snapshotObject->backTrackBeforeStep(0);
160 }
161
162 /** @return a thread ID for a new Thread */
163 thread_id_t ModelChecker::get_next_id()
164 {
165         return priv->next_thread_id++;
166 }
167
168 /** @return the number of user threads created during this execution */
169 unsigned int ModelChecker::get_num_threads() const
170 {
171         return priv->next_thread_id;
172 }
173
174 /** @return The currently executing Thread. */
175 Thread * ModelChecker::get_current_thread()
176 {
177         return scheduler->get_current_thread();
178 }
179
180 /** @return a sequence number for a new ModelAction */
181 modelclock_t ModelChecker::get_next_seq_num()
182 {
183         return ++priv->used_sequence_numbers;
184 }
185
186 Node * ModelChecker::get_curr_node() {
187         return node_stack->get_head();
188 }
189
190 /**
191  * @brief Choose the next thread to execute.
192  *
193  * This function chooses the next thread that should execute. It can force the
194  * adjacency of read/write portions of a RMW action, force THREAD_CREATE to be
195  * followed by a THREAD_START, or it can enforce execution replay/backtracking.
196  * The model-checker may have no preference regarding the next thread (i.e.,
197  * when exploring a new execution ordering), in which case this will return
198  * NULL.
199  * @param curr The current ModelAction. This action might guide the choice of
200  * next thread.
201  * @return The next thread to run. If the model-checker has no preference, NULL.
202  */
203 Thread * ModelChecker::get_next_thread(ModelAction *curr)
204 {
205         thread_id_t tid;
206
207         if (curr!=NULL) {
208                 /* Do not split atomic actions. */
209                 if (curr->is_rmwr())
210                         return thread_current();
211                 /* The THREAD_CREATE action points to the created Thread */
212                 else if (curr->get_type() == THREAD_CREATE)
213                         return (Thread *)curr->get_location();
214         }
215
216         /* Have we completed exploring the preselected path? */
217         if (diverge == NULL)
218                 return NULL;
219
220         /* Else, we are trying to replay an execution */
221         ModelAction *next = node_stack->get_next()->get_action();
222
223         if (next == diverge) {
224                 if (earliest_diverge == NULL || *diverge < *earliest_diverge)
225                         earliest_diverge=diverge;
226
227                 Node *nextnode = next->get_node();
228                 Node *prevnode = nextnode->get_parent();
229                 scheduler->update_sleep_set(prevnode);
230
231                 /* Reached divergence point */
232                 if (nextnode->increment_misc()) {
233                         /* The next node will try to satisfy a different misc_index values. */
234                         tid = next->get_tid();
235                         node_stack->pop_restofstack(2);
236                 } else if (nextnode->increment_promise()) {
237                         /* The next node will try to satisfy a different set of promises. */
238                         tid = next->get_tid();
239                         node_stack->pop_restofstack(2);
240                 } else if (nextnode->increment_read_from()) {
241                         /* The next node will read from a different value. */
242                         tid = next->get_tid();
243                         node_stack->pop_restofstack(2);
244                 } else if (nextnode->increment_future_value()) {
245                         /* The next node will try to read from a different future value. */
246                         tid = next->get_tid();
247                         node_stack->pop_restofstack(2);
248                 } else if (nextnode->increment_relseq_break()) {
249                         /* The next node will try to resolve a release sequence differently */
250                         tid = next->get_tid();
251                         node_stack->pop_restofstack(2);
252                 } else {
253                         /* Make a different thread execute for next step */
254                         scheduler->add_sleep(thread_map->get(id_to_int(next->get_tid())));
255                         tid = prevnode->get_next_backtrack();
256                         /* Make sure the backtracked thread isn't sleeping. */
257                         node_stack->pop_restofstack(1);
258                         if (diverge==earliest_diverge) {
259                                 earliest_diverge=prevnode->get_action();
260                         }
261                 }
262                 /* The correct sleep set is in the parent node. */
263                 execute_sleep_set();
264
265                 DEBUG("*** Divergence point ***\n");
266
267                 diverge = NULL;
268         } else {
269                 tid = next->get_tid();
270         }
271         DEBUG("*** ModelChecker chose next thread = %d ***\n", id_to_int(tid));
272         ASSERT(tid != THREAD_ID_T_NONE);
273         return thread_map->get(id_to_int(tid));
274 }
275
276 /**
277  * We need to know what the next actions of all threads in the sleep
278  * set will be.  This method computes them and stores the actions at
279  * the corresponding thread object's pending action.
280  */
281
282 void ModelChecker::execute_sleep_set() {
283         for(unsigned int i=0;i<get_num_threads();i++) {
284                 thread_id_t tid=int_to_id(i);
285                 Thread *thr=get_thread(tid);
286                 if ( scheduler->get_enabled(thr) == THREAD_SLEEP_SET &&
287                                  thr->get_pending() == NULL ) {
288                         thr->set_state(THREAD_RUNNING);
289                         scheduler->next_thread(thr);
290                         Thread::swap(&system_context, thr);
291                         priv->current_action->set_sleep_flag();
292                         thr->set_pending(priv->current_action);
293                 }
294         }
295         priv->current_action = NULL;
296 }
297
298 void ModelChecker::wake_up_sleeping_actions(ModelAction * curr) {
299         for(unsigned int i=0;i<get_num_threads();i++) {
300                 thread_id_t tid=int_to_id(i);
301                 Thread *thr=get_thread(tid);
302                 if ( scheduler->get_enabled(thr) == THREAD_SLEEP_SET ) {
303                         ModelAction *pending_act=thr->get_pending();
304                         if ((!curr->is_rmwr())&&pending_act->could_synchronize_with(curr)) {
305                                 //Remove this thread from sleep set
306                                 scheduler->remove_sleep(thr);
307                         }
308                 }
309         }
310 }
311
312 /** @brief Alert the model-checker that an incorrectly-ordered
313  * synchronization was made */
314 void ModelChecker::set_bad_synchronization()
315 {
316         priv->bad_synchronization = true;
317 }
318
319 bool ModelChecker::has_asserted() const
320 {
321         return priv->asserted;
322 }
323
324 void ModelChecker::set_assert()
325 {
326         priv->asserted = true;
327 }
328
329 /**
330  * Check if we are in a deadlock. Should only be called at the end of an
331  * execution, although it should not give false positives in the middle of an
332  * execution (there should be some ENABLED thread).
333  *
334  * @return True if program is in a deadlock; false otherwise
335  */
336 bool ModelChecker::is_deadlocked() const
337 {
338         bool blocking_threads = false;
339         for (unsigned int i = 0; i < get_num_threads(); i++) {
340                 thread_id_t tid = int_to_id(i);
341                 if (is_enabled(tid))
342                         return false;
343                 Thread *t = get_thread(tid);
344                 if (!t->is_model_thread() && t->get_pending())
345                         blocking_threads = true;
346         }
347         return blocking_threads;
348 }
349
350 /**
351  * Check if this is a complete execution. That is, have all thread completed
352  * execution (rather than exiting because sleep sets have forced a redundant
353  * execution).
354  *
355  * @return True if the execution is complete.
356  */
357 bool ModelChecker::is_complete_execution() const
358 {
359         for (unsigned int i = 0; i < get_num_threads(); i++)
360                 if (is_enabled(int_to_id(i)))
361                         return false;
362         return true;
363 }
364
365 /**
366  * @brief Assert a bug in the executing program.
367  *
368  * Use this function to assert any sort of bug in the user program. If the
369  * current trace is feasible (actually, a prefix of some feasible execution),
370  * then this execution will be aborted, printing the appropriate message. If
371  * the current trace is not yet feasible, the error message will be stashed and
372  * printed if the execution ever becomes feasible.
373  *
374  * @param msg Descriptive message for the bug (do not include newline char)
375  * @return True if bug is immediately-feasible
376  */
377 bool ModelChecker::assert_bug(const char *msg)
378 {
379         priv->bugs.push_back(new bug_message(msg));
380
381         if (isfeasibleprefix()) {
382                 set_assert();
383                 return true;
384         }
385         return false;
386 }
387
388 /**
389  * @brief Assert a bug in the executing program, asserted by a user thread
390  * @see ModelChecker::assert_bug
391  * @param msg Descriptive message for the bug (do not include newline char)
392  */
393 void ModelChecker::assert_user_bug(const char *msg)
394 {
395         /* If feasible bug, bail out now */
396         if (assert_bug(msg))
397                 switch_to_master(NULL);
398 }
399
400 /** @return True, if any bugs have been reported for this execution */
401 bool ModelChecker::have_bug_reports() const
402 {
403         return priv->bugs.size() != 0;
404 }
405
406 /** @brief Print bug report listing for this execution (if any bugs exist) */
407 void ModelChecker::print_bugs() const
408 {
409         if (have_bug_reports()) {
410                 model_print("Bug report: %zu bug%s detected\n",
411                                 priv->bugs.size(),
412                                 priv->bugs.size() > 1 ? "s" : "");
413                 for (unsigned int i = 0; i < priv->bugs.size(); i++)
414                         priv->bugs[i]->print();
415         }
416 }
417
418 /**
419  * @brief Record end-of-execution stats
420  *
421  * Must be run when exiting an execution. Records various stats.
422  * @see struct execution_stats
423  */
424 void ModelChecker::record_stats()
425 {
426         stats.num_total++;
427         if (!isfeasibleprefix())
428                 stats.num_infeasible++;
429         else if (have_bug_reports())
430                 stats.num_buggy_executions++;
431         else if (is_complete_execution())
432                 stats.num_complete++;
433         else
434                 stats.num_redundant++;
435 }
436
437 /** @brief Print execution stats */
438 void ModelChecker::print_stats() const
439 {
440         model_print("Number of complete, bug-free executions: %d\n", stats.num_complete);
441         model_print("Number of redundant executions: %d\n", stats.num_redundant);
442         model_print("Number of buggy executions: %d\n", stats.num_buggy_executions);
443         model_print("Number of infeasible executions: %d\n", stats.num_infeasible);
444         model_print("Total executions: %d\n", stats.num_total);
445         model_print("Total nodes created: %d\n", node_stack->get_total_nodes());
446 }
447
448 /**
449  * @brief End-of-exeuction print
450  * @param printbugs Should any existing bugs be printed?
451  */
452 void ModelChecker::print_execution(bool printbugs) const
453 {
454         print_program_output();
455
456         if (DBG_ENABLED() || params.verbose) {
457                 model_print("Earliest divergence point since last feasible execution:\n");
458                 if (earliest_diverge)
459                         earliest_diverge->print();
460                 else
461                         model_print("(Not set)\n");
462
463                 model_print("\n");
464                 print_stats();
465         }
466
467         /* Don't print invalid bugs */
468         if (printbugs)
469                 print_bugs();
470
471         model_print("\n");
472         print_summary();
473 }
474
475 /**
476  * Queries the model-checker for more executions to explore and, if one
477  * exists, resets the model-checker state to execute a new execution.
478  *
479  * @return If there are more executions to explore, return true. Otherwise,
480  * return false.
481  */
482 bool ModelChecker::next_execution()
483 {
484         DBG();
485         /* Is this execution a feasible execution that's worth bug-checking? */
486         bool complete = isfeasibleprefix() && (is_complete_execution() ||
487                         have_bug_reports());
488
489         /* End-of-execution bug checks */
490         if (complete) {
491                 if (is_deadlocked())
492                         assert_bug("Deadlock detected");
493
494                 checkDataRaces();
495         }
496
497         record_stats();
498
499         /* Output */
500         if (DBG_ENABLED() || params.verbose || have_bug_reports())
501                 print_execution(complete);
502         else
503                 clear_program_output();
504
505         if (complete)
506                 earliest_diverge = NULL;
507
508         if ((diverge = get_next_backtrack()) == NULL)
509                 return false;
510
511         if (DBG_ENABLED()) {
512                 model_print("Next execution will diverge at:\n");
513                 diverge->print();
514         }
515
516         reset_to_initial_state();
517         return true;
518 }
519
520 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
521 {
522         switch (act->get_type()) {
523         case ATOMIC_READ:
524         case ATOMIC_WRITE:
525         case ATOMIC_RMW: {
526                 /* Optimization: relaxed operations don't need backtracking */
527                 if (act->is_relaxed())
528                         return NULL;
529                 /* linear search: from most recent to oldest */
530                 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
531                 action_list_t::reverse_iterator rit;
532                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
533                         ModelAction *prev = *rit;
534                         if (prev->could_synchronize_with(act))
535                                 return prev;
536                 }
537                 break;
538         }
539         case ATOMIC_LOCK:
540         case ATOMIC_TRYLOCK: {
541                 /* linear search: from most recent to oldest */
542                 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
543                 action_list_t::reverse_iterator rit;
544                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
545                         ModelAction *prev = *rit;
546                         if (act->is_conflicting_lock(prev))
547                                 return prev;
548                 }
549                 break;
550         }
551         case ATOMIC_UNLOCK: {
552                 /* linear search: from most recent to oldest */
553                 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
554                 action_list_t::reverse_iterator rit;
555                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
556                         ModelAction *prev = *rit;
557                         if (!act->same_thread(prev)&&prev->is_failed_trylock())
558                                 return prev;
559                 }
560                 break;
561         }
562         case ATOMIC_WAIT: {
563                 /* linear search: from most recent to oldest */
564                 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
565                 action_list_t::reverse_iterator rit;
566                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
567                         ModelAction *prev = *rit;
568                         if (!act->same_thread(prev)&&prev->is_failed_trylock())
569                                 return prev;
570                         if (!act->same_thread(prev)&&prev->is_notify())
571                                 return prev;
572                 }
573                 break;
574         }
575
576         case ATOMIC_NOTIFY_ALL:
577         case ATOMIC_NOTIFY_ONE: {
578                 /* linear search: from most recent to oldest */
579                 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
580                 action_list_t::reverse_iterator rit;
581                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
582                         ModelAction *prev = *rit;
583                         if (!act->same_thread(prev)&&prev->is_wait())
584                                 return prev;
585                 }
586                 break;
587         }
588         default:
589                 break;
590         }
591         return NULL;
592 }
593
594 /** This method finds backtracking points where we should try to
595  * reorder the parameter ModelAction against.
596  *
597  * @param the ModelAction to find backtracking points for.
598  */
599 void ModelChecker::set_backtracking(ModelAction *act)
600 {
601         Thread *t = get_thread(act);
602         ModelAction * prev = get_last_conflict(act);
603         if (prev == NULL)
604                 return;
605
606         Node * node = prev->get_node()->get_parent();
607
608         int low_tid, high_tid;
609         if (node->is_enabled(t)) {
610                 low_tid = id_to_int(act->get_tid());
611                 high_tid = low_tid+1;
612         } else {
613                 low_tid = 0;
614                 high_tid = get_num_threads();
615         }
616
617         for(int i = low_tid; i < high_tid; i++) {
618                 thread_id_t tid = int_to_id(i);
619
620                 /* Make sure this thread can be enabled here. */
621                 if (i >= node->get_num_threads())
622                         break;
623
624                 /* Don't backtrack into a point where the thread is disabled or sleeping. */
625                 if (node->enabled_status(tid)!=THREAD_ENABLED)
626                         continue;
627
628                 /* Check if this has been explored already */
629                 if (node->has_been_explored(tid))
630                         continue;
631
632                 /* See if fairness allows */
633                 if (model->params.fairwindow != 0 && !node->has_priority(tid)) {
634                         bool unfair=false;
635                         for(int t=0;t<node->get_num_threads();t++) {
636                                 thread_id_t tother=int_to_id(t);
637                                 if (node->is_enabled(tother) && node->has_priority(tother)) {
638                                         unfair=true;
639                                         break;
640                                 }
641                         }
642                         if (unfair)
643                                 continue;
644                 }
645                 /* Cache the latest backtracking point */
646                 if (!priv->next_backtrack || *prev > *priv->next_backtrack)
647                         priv->next_backtrack = prev;
648
649                 /* If this is a new backtracking point, mark the tree */
650                 if (!node->set_backtrack(tid))
651                         continue;
652                 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
653                                         id_to_int(prev->get_tid()),
654                                         id_to_int(t->get_id()));
655                 if (DBG_ENABLED()) {
656                         prev->print();
657                         act->print();
658                 }
659         }
660 }
661
662 /**
663  * Returns last backtracking point. The model checker will explore a different
664  * path for this point in the next execution.
665  * @return The ModelAction at which the next execution should diverge.
666  */
667 ModelAction * ModelChecker::get_next_backtrack()
668 {
669         ModelAction *next = priv->next_backtrack;
670         priv->next_backtrack = NULL;
671         return next;
672 }
673
674 /**
675  * Processes a read or rmw model action.
676  * @param curr is the read model action to process.
677  * @param second_part_of_rmw is boolean that is true is this is the second action of a rmw.
678  * @return True if processing this read updates the mo_graph.
679  */
680 bool ModelChecker::process_read(ModelAction *curr, bool second_part_of_rmw)
681 {
682         uint64_t value = VALUE_NONE;
683         bool updated = false;
684         while (true) {
685                 const ModelAction *reads_from = curr->get_node()->get_read_from();
686                 if (reads_from != NULL) {
687                         mo_graph->startChanges();
688
689                         value = reads_from->get_value();
690                         bool r_status = false;
691
692                         if (!second_part_of_rmw) {
693                                 check_recency(curr, reads_from);
694                                 r_status = r_modification_order(curr, reads_from);
695                         }
696
697
698                         if (!second_part_of_rmw&&is_infeasible()&&(curr->get_node()->increment_read_from()||curr->get_node()->increment_future_value())) {
699                                 mo_graph->rollbackChanges();
700                                 priv->too_many_reads = false;
701                                 continue;
702                         }
703
704                         curr->read_from(reads_from);
705                         mo_graph->commitChanges();
706                         mo_check_promises(curr->get_tid(), reads_from);
707
708                         updated |= r_status;
709                 } else if (!second_part_of_rmw) {
710                         /* Read from future value */
711                         value = curr->get_node()->get_future_value();
712                         modelclock_t expiration = curr->get_node()->get_future_value_expiration();
713                         curr->read_from(NULL);
714                         Promise *valuepromise = new Promise(curr, value, expiration);
715                         promises->push_back(valuepromise);
716                 }
717                 get_thread(curr)->set_return_value(value);
718                 return updated;
719         }
720 }
721
722 /**
723  * Processes a lock, trylock, or unlock model action.  @param curr is
724  * the read model action to process.
725  *
726  * The try lock operation checks whether the lock is taken.  If not,
727  * it falls to the normal lock operation case.  If so, it returns
728  * fail.
729  *
730  * The lock operation has already been checked that it is enabled, so
731  * it just grabs the lock and synchronizes with the previous unlock.
732  *
733  * The unlock operation has to re-enable all of the threads that are
734  * waiting on the lock.
735  *
736  * @return True if synchronization was updated; false otherwise
737  */
738 bool ModelChecker::process_mutex(ModelAction *curr) {
739         std::mutex *mutex=NULL;
740         struct std::mutex_state *state=NULL;
741
742         if (curr->is_trylock() || curr->is_lock() || curr->is_unlock()) {
743                 mutex = (std::mutex *)curr->get_location();
744                 state = mutex->get_state();
745         } else if(curr->is_wait()) {
746                 mutex = (std::mutex *)curr->get_value();
747                 state = mutex->get_state();
748         }
749
750         switch (curr->get_type()) {
751         case ATOMIC_TRYLOCK: {
752                 bool success = !state->islocked;
753                 curr->set_try_lock(success);
754                 if (!success) {
755                         get_thread(curr)->set_return_value(0);
756                         break;
757                 }
758                 get_thread(curr)->set_return_value(1);
759         }
760                 //otherwise fall into the lock case
761         case ATOMIC_LOCK: {
762                 if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock)
763                         assert_bug("Lock access before initialization");
764                 state->islocked = true;
765                 ModelAction *unlock = get_last_unlock(curr);
766                 //synchronize with the previous unlock statement
767                 if (unlock != NULL) {
768                         curr->synchronize_with(unlock);
769                         return true;
770                 }
771                 break;
772         }
773         case ATOMIC_UNLOCK: {
774                 //unlock the lock
775                 state->islocked = false;
776                 //wake up the other threads
777                 action_list_t *waiters = get_safe_ptr_action(lock_waiters_map, curr->get_location());
778                 //activate all the waiting threads
779                 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
780                         scheduler->wake(get_thread(*rit));
781                 }
782                 waiters->clear();
783                 break;
784         }
785         case ATOMIC_WAIT: {
786                 //unlock the lock
787                 state->islocked = false;
788                 //wake up the other threads
789                 action_list_t *waiters = get_safe_ptr_action(lock_waiters_map, (void *) curr->get_value());
790                 //activate all the waiting threads
791                 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
792                         scheduler->wake(get_thread(*rit));
793                 }
794                 waiters->clear();
795                 //check whether we should go to sleep or not...simulate spurious failures
796                 if (curr->get_node()->get_misc()==0) {
797                         get_safe_ptr_action(condvar_waiters_map, curr->get_location())->push_back(curr);
798                         //disable us
799                         scheduler->sleep(get_current_thread());
800                 }
801                 break;
802         }
803         case ATOMIC_NOTIFY_ALL: {
804                 action_list_t *waiters = get_safe_ptr_action(condvar_waiters_map, curr->get_location());
805                 //activate all the waiting threads
806                 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
807                         scheduler->wake(get_thread(*rit));
808                 }
809                 waiters->clear();
810                 break;
811         }
812         case ATOMIC_NOTIFY_ONE: {
813                 action_list_t *waiters = get_safe_ptr_action(condvar_waiters_map, curr->get_location());
814                 int wakeupthread=curr->get_node()->get_misc();
815                 action_list_t::iterator it = waiters->begin();
816                 advance(it, wakeupthread);
817                 scheduler->wake(get_thread(*it));
818                 waiters->erase(it);
819                 break;
820         }
821
822         default:
823                 ASSERT(0);
824         }
825         return false;
826 }
827
828 /**
829  * Process a write ModelAction
830  * @param curr The ModelAction to process
831  * @return True if the mo_graph was updated or promises were resolved
832  */
833 bool ModelChecker::process_write(ModelAction *curr)
834 {
835         bool updated_mod_order = w_modification_order(curr);
836         bool updated_promises = resolve_promises(curr);
837
838         if (promises->size() == 0) {
839                 for (unsigned int i = 0; i < futurevalues->size(); i++) {
840                         struct PendingFutureValue pfv = (*futurevalues)[i];
841                         //Do more ambitious checks now that mo is more complete
842                         if (mo_may_allow(pfv.writer, pfv.act)&&
843                                         pfv.act->get_node()->add_future_value(pfv.writer->get_value(), pfv.writer->get_seq_number()+params.maxfuturedelay) &&
844                                         (!priv->next_backtrack || *pfv.act > *priv->next_backtrack))
845                                 priv->next_backtrack = pfv.act;
846                 }
847                 futurevalues->resize(0);
848         }
849
850         mo_graph->commitChanges();
851         mo_check_promises(curr->get_tid(), curr);
852
853         get_thread(curr)->set_return_value(VALUE_NONE);
854         return updated_mod_order || updated_promises;
855 }
856
857 /**
858  * @brief Process the current action for thread-related activity
859  *
860  * Performs current-action processing for a THREAD_* ModelAction. Proccesses
861  * may include setting Thread status, completing THREAD_FINISH/THREAD_JOIN
862  * synchronization, etc.  This function is a no-op for non-THREAD actions
863  * (e.g., ATOMIC_{READ,WRITE,RMW,LOCK}, etc.)
864  *
865  * @param curr The current action
866  * @return True if synchronization was updated or a thread completed
867  */
868 bool ModelChecker::process_thread_action(ModelAction *curr)
869 {
870         bool updated = false;
871
872         switch (curr->get_type()) {
873         case THREAD_CREATE: {
874                 Thread *th = (Thread *)curr->get_location();
875                 th->set_creation(curr);
876                 break;
877         }
878         case THREAD_JOIN: {
879                 Thread *blocking = (Thread *)curr->get_location();
880                 ModelAction *act = get_last_action(blocking->get_id());
881                 curr->synchronize_with(act);
882                 updated = true; /* trigger rel-seq checks */
883                 break;
884         }
885         case THREAD_FINISH: {
886                 Thread *th = get_thread(curr);
887                 while (!th->wait_list_empty()) {
888                         ModelAction *act = th->pop_wait_list();
889                         scheduler->wake(get_thread(act));
890                 }
891                 th->complete();
892                 updated = true; /* trigger rel-seq checks */
893                 break;
894         }
895         case THREAD_START: {
896                 check_promises(curr->get_tid(), NULL, curr->get_cv());
897                 break;
898         }
899         default:
900                 break;
901         }
902
903         return updated;
904 }
905
906 /**
907  * @brief Process the current action for release sequence fixup activity
908  *
909  * Performs model-checker release sequence fixups for the current action,
910  * forcing a single pending release sequence to break (with a given, potential
911  * "loose" write) or to complete (i.e., synchronize). If a pending release
912  * sequence forms a complete release sequence, then we must perform the fixup
913  * synchronization, mo_graph additions, etc.
914  *
915  * @param curr The current action; must be a release sequence fixup action
916  * @param work_queue The work queue to which to add work items as they are
917  * generated
918  */
919 void ModelChecker::process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue)
920 {
921         const ModelAction *write = curr->get_node()->get_relseq_break();
922         struct release_seq *sequence = pending_rel_seqs->back();
923         pending_rel_seqs->pop_back();
924         ASSERT(sequence);
925         ModelAction *acquire = sequence->acquire;
926         const ModelAction *rf = sequence->rf;
927         const ModelAction *release = sequence->release;
928         ASSERT(acquire);
929         ASSERT(release);
930         ASSERT(rf);
931         ASSERT(release->same_thread(rf));
932
933         if (write == NULL) {
934                 /**
935                  * @todo Forcing a synchronization requires that we set
936                  * modification order constraints. For instance, we can't allow
937                  * a fixup sequence in which two separate read-acquire
938                  * operations read from the same sequence, where the first one
939                  * synchronizes and the other doesn't. Essentially, we can't
940                  * allow any writes to insert themselves between 'release' and
941                  * 'rf'
942                  */
943
944                 /* Must synchronize */
945                 if (!acquire->synchronize_with(release)) {
946                         set_bad_synchronization();
947                         return;
948                 }
949                 /* Re-check all pending release sequences */
950                 work_queue->push_back(CheckRelSeqWorkEntry(NULL));
951                 /* Re-check act for mo_graph edges */
952                 work_queue->push_back(MOEdgeWorkEntry(acquire));
953
954                 /* propagate synchronization to later actions */
955                 action_list_t::reverse_iterator rit = action_trace->rbegin();
956                 for (; (*rit) != acquire; rit++) {
957                         ModelAction *propagate = *rit;
958                         if (acquire->happens_before(propagate)) {
959                                 propagate->synchronize_with(acquire);
960                                 /* Re-check 'propagate' for mo_graph edges */
961                                 work_queue->push_back(MOEdgeWorkEntry(propagate));
962                         }
963                 }
964         } else {
965                 /* Break release sequence with new edges:
966                  *   release --mo--> write --mo--> rf */
967                 mo_graph->addEdge(release, write);
968                 mo_graph->addEdge(write, rf);
969         }
970
971         /* See if we have realized a data race */
972         checkDataRaces();
973 }
974
975 /**
976  * Initialize the current action by performing one or more of the following
977  * actions, as appropriate: merging RMWR and RMWC/RMW actions, stepping forward
978  * in the NodeStack, manipulating backtracking sets, allocating and
979  * initializing clock vectors, and computing the promises to fulfill.
980  *
981  * @param curr The current action, as passed from the user context; may be
982  * freed/invalidated after the execution of this function, with a different
983  * action "returned" its place (pass-by-reference)
984  * @return True if curr is a newly-explored action; false otherwise
985  */
986 bool ModelChecker::initialize_curr_action(ModelAction **curr)
987 {
988         ModelAction *newcurr;
989
990         if ((*curr)->is_rmwc() || (*curr)->is_rmw()) {
991                 newcurr = process_rmw(*curr);
992                 delete *curr;
993
994                 if (newcurr->is_rmw())
995                         compute_promises(newcurr);
996
997                 *curr = newcurr;
998                 return false;
999         }
1000
1001         (*curr)->set_seq_number(get_next_seq_num());
1002
1003         newcurr = node_stack->explore_action(*curr, scheduler->get_enabled_array());
1004         if (newcurr) {
1005                 /* First restore type and order in case of RMW operation */
1006                 if ((*curr)->is_rmwr())
1007                         newcurr->copy_typeandorder(*curr);
1008
1009                 ASSERT((*curr)->get_location() == newcurr->get_location());
1010                 newcurr->copy_from_new(*curr);
1011
1012                 /* Discard duplicate ModelAction; use action from NodeStack */
1013                 delete *curr;
1014
1015                 /* Always compute new clock vector */
1016                 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
1017
1018                 *curr = newcurr;
1019                 return false; /* Action was explored previously */
1020         } else {
1021                 newcurr = *curr;
1022
1023                 /* Always compute new clock vector */
1024                 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
1025                 /*
1026                  * Perform one-time actions when pushing new ModelAction onto
1027                  * NodeStack
1028                  */
1029                 if (newcurr->is_write())
1030                         compute_promises(newcurr);
1031                 else if (newcurr->is_relseq_fixup())
1032                         compute_relseq_breakwrites(newcurr);
1033                 else if (newcurr->is_wait())
1034                         newcurr->get_node()->set_misc_max(2);
1035                 else if (newcurr->is_notify_one()) {
1036                         newcurr->get_node()->set_misc_max(get_safe_ptr_action(condvar_waiters_map, newcurr->get_location())->size());
1037                 }
1038                 return true; /* This was a new ModelAction */
1039         }
1040 }
1041
1042 /**
1043  * @brief Check whether a model action is enabled.
1044  *
1045  * Checks whether a lock or join operation would be successful (i.e., is the
1046  * lock already locked, or is the joined thread already complete). If not, put
1047  * the action in a waiter list.
1048  *
1049  * @param curr is the ModelAction to check whether it is enabled.
1050  * @return a bool that indicates whether the action is enabled.
1051  */
1052 bool ModelChecker::check_action_enabled(ModelAction *curr) {
1053         if (curr->is_lock()) {
1054                 std::mutex * lock = (std::mutex *)curr->get_location();
1055                 struct std::mutex_state * state = lock->get_state();
1056                 if (state->islocked) {
1057                         //Stick the action in the appropriate waiting queue
1058                         get_safe_ptr_action(lock_waiters_map, curr->get_location())->push_back(curr);
1059                         return false;
1060                 }
1061         } else if (curr->get_type() == THREAD_JOIN) {
1062                 Thread *blocking = (Thread *)curr->get_location();
1063                 if (!blocking->is_complete()) {
1064                         blocking->push_wait_list(curr);
1065                         return false;
1066                 }
1067         }
1068
1069         return true;
1070 }
1071
1072 /**
1073  * Stores the ModelAction for the current thread action.  Call this
1074  * immediately before switching from user- to system-context to pass
1075  * data between them.
1076  * @param act The ModelAction created by the user-thread action
1077  */
1078 void ModelChecker::set_current_action(ModelAction *act) {
1079         priv->current_action = act;
1080 }
1081
1082 /**
1083  * This is the heart of the model checker routine. It performs model-checking
1084  * actions corresponding to a given "current action." Among other processes, it
1085  * calculates reads-from relationships, updates synchronization clock vectors,
1086  * forms a memory_order constraints graph, and handles replay/backtrack
1087  * execution when running permutations of previously-observed executions.
1088  *
1089  * @param curr The current action to process
1090  * @return The next Thread that must be executed. May be NULL if ModelChecker
1091  * makes no choice (e.g., according to replay execution, combining RMW actions,
1092  * etc.)
1093  */
1094 Thread * ModelChecker::check_current_action(ModelAction *curr)
1095 {
1096         ASSERT(curr);
1097         bool second_part_of_rmw = curr->is_rmwc() || curr->is_rmw();
1098
1099         if (!check_action_enabled(curr)) {
1100                 /* Make the execution look like we chose to run this action
1101                  * much later, when a lock/join can succeed */
1102                 get_current_thread()->set_pending(curr);
1103                 scheduler->sleep(get_current_thread());
1104                 return get_next_thread(NULL);
1105         }
1106
1107         bool newly_explored = initialize_curr_action(&curr);
1108
1109         wake_up_sleeping_actions(curr);
1110
1111         /* Add the action to lists before any other model-checking tasks */
1112         if (!second_part_of_rmw)
1113                 add_action_to_lists(curr);
1114
1115         /* Build may_read_from set for newly-created actions */
1116         if (newly_explored && curr->is_read())
1117                 build_reads_from_past(curr);
1118
1119         /* Initialize work_queue with the "current action" work */
1120         work_queue_t work_queue(1, CheckCurrWorkEntry(curr));
1121         while (!work_queue.empty() && !has_asserted()) {
1122                 WorkQueueEntry work = work_queue.front();
1123                 work_queue.pop_front();
1124
1125                 switch (work.type) {
1126                 case WORK_CHECK_CURR_ACTION: {
1127                         ModelAction *act = work.action;
1128                         bool update = false; /* update this location's release seq's */
1129                         bool update_all = false; /* update all release seq's */
1130
1131                         if (process_thread_action(curr))
1132                                 update_all = true;
1133
1134                         if (act->is_read() && process_read(act, second_part_of_rmw))
1135                                 update = true;
1136
1137                         if (act->is_write() && process_write(act))
1138                                 update = true;
1139
1140                         if (act->is_mutex_op() && process_mutex(act))
1141                                 update_all = true;
1142
1143                         if (act->is_relseq_fixup())
1144                                 process_relseq_fixup(curr, &work_queue);
1145
1146                         if (update_all)
1147                                 work_queue.push_back(CheckRelSeqWorkEntry(NULL));
1148                         else if (update)
1149                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
1150                         break;
1151                 }
1152                 case WORK_CHECK_RELEASE_SEQ:
1153                         resolve_release_sequences(work.location, &work_queue);
1154                         break;
1155                 case WORK_CHECK_MO_EDGES: {
1156                         /** @todo Complete verification of work_queue */
1157                         ModelAction *act = work.action;
1158                         bool updated = false;
1159
1160                         if (act->is_read()) {
1161                                 const ModelAction *rf = act->get_reads_from();
1162                                 if (rf != NULL && r_modification_order(act, rf))
1163                                         updated = true;
1164                         }
1165                         if (act->is_write()) {
1166                                 if (w_modification_order(act))
1167                                         updated = true;
1168                         }
1169                         mo_graph->commitChanges();
1170
1171                         if (updated)
1172                                 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
1173                         break;
1174                 }
1175                 default:
1176                         ASSERT(false);
1177                         break;
1178                 }
1179         }
1180
1181         check_curr_backtracking(curr);
1182         set_backtracking(curr);
1183         return get_next_thread(curr);
1184 }
1185
1186 void ModelChecker::check_curr_backtracking(ModelAction * curr) {
1187         Node *currnode = curr->get_node();
1188         Node *parnode = currnode->get_parent();
1189
1190         if ((!parnode->backtrack_empty() ||
1191                          !currnode->misc_empty() ||
1192                          !currnode->read_from_empty() ||
1193                          !currnode->future_value_empty() ||
1194                          !currnode->promise_empty() ||
1195                          !currnode->relseq_break_empty())
1196                         && (!priv->next_backtrack ||
1197                                         *curr > *priv->next_backtrack)) {
1198                 priv->next_backtrack = curr;
1199         }
1200 }
1201
1202 bool ModelChecker::promises_expired() const
1203 {
1204         for (unsigned int promise_index = 0; promise_index < promises->size(); promise_index++) {
1205                 Promise *promise = (*promises)[promise_index];
1206                 if (promise->get_expiration()<priv->used_sequence_numbers) {
1207                         return true;
1208                 }
1209         }
1210         return false;
1211 }
1212
1213 /**
1214  * This is the strongest feasibility check available.
1215  * @return whether the current trace (partial or complete) must be a prefix of
1216  * a feasible trace.
1217  */
1218 bool ModelChecker::isfeasibleprefix() const
1219 {
1220         return pending_rel_seqs->size() == 0 && is_feasible_prefix_ignore_relseq();
1221 }
1222
1223 /**
1224  * Returns whether the current completed trace is feasible, except for pending
1225  * release sequences.
1226  */
1227 bool ModelChecker::is_feasible_prefix_ignore_relseq() const
1228 {
1229         if (DBG_ENABLED() && promises->size() != 0)
1230                 DEBUG("Infeasible: unrevolved promises\n");
1231
1232         return !is_infeasible() && promises->size() == 0;
1233 }
1234
1235 /**
1236  * Check if the current partial trace is infeasible. Does not check any
1237  * end-of-execution flags, which might rule out the execution. Thus, this is
1238  * useful only for ruling an execution as infeasible.
1239  * @return whether the current partial trace is infeasible.
1240  */
1241 bool ModelChecker::is_infeasible() const
1242 {
1243         if (DBG_ENABLED() && mo_graph->checkForRMWViolation())
1244                 DEBUG("Infeasible: RMW violation\n");
1245
1246         return mo_graph->checkForRMWViolation() || is_infeasible_ignoreRMW();
1247 }
1248
1249 /**
1250  * Check If the current partial trace is infeasible, while ignoring
1251  * infeasibility related to 2 RMW's reading from the same store. It does not
1252  * check end-of-execution feasibility.
1253  * @see ModelChecker::is_infeasible
1254  * @return whether the current partial trace is infeasible, ignoring multiple
1255  * RMWs reading from the same store.
1256  * */
1257 bool ModelChecker::is_infeasible_ignoreRMW() const
1258 {
1259         if (DBG_ENABLED()) {
1260                 if (mo_graph->checkForCycles())
1261                         DEBUG("Infeasible: modification order cycles\n");
1262                 if (priv->failed_promise)
1263                         DEBUG("Infeasible: failed promise\n");
1264                 if (priv->too_many_reads)
1265                         DEBUG("Infeasible: too many reads\n");
1266                 if (priv->bad_synchronization)
1267                         DEBUG("Infeasible: bad synchronization ordering\n");
1268                 if (promises_expired())
1269                         DEBUG("Infeasible: promises expired\n");
1270         }
1271         return mo_graph->checkForCycles() || priv->failed_promise ||
1272                 priv->too_many_reads || priv->bad_synchronization ||
1273                 promises_expired();
1274 }
1275
1276 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
1277 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
1278         ModelAction *lastread = get_last_action(act->get_tid());
1279         lastread->process_rmw(act);
1280         if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
1281                 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
1282                 mo_graph->commitChanges();
1283         }
1284         return lastread;
1285 }
1286
1287 /**
1288  * Checks whether a thread has read from the same write for too many times
1289  * without seeing the effects of a later write.
1290  *
1291  * Basic idea:
1292  * 1) there must a different write that we could read from that would satisfy the modification order,
1293  * 2) we must have read from the same value in excess of maxreads times, and
1294  * 3) that other write must have been in the reads_from set for maxreads times.
1295  *
1296  * If so, we decide that the execution is no longer feasible.
1297  */
1298 void ModelChecker::check_recency(ModelAction *curr, const ModelAction *rf) {
1299         if (params.maxreads != 0) {
1300
1301                 if (curr->get_node()->get_read_from_size() <= 1)
1302                         return;
1303                 //Must make sure that execution is currently feasible...  We could
1304                 //accidentally clear by rolling back
1305                 if (is_infeasible())
1306                         return;
1307                 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1308                 int tid = id_to_int(curr->get_tid());
1309
1310                 /* Skip checks */
1311                 if ((int)thrd_lists->size() <= tid)
1312                         return;
1313                 action_list_t *list = &(*thrd_lists)[tid];
1314
1315                 action_list_t::reverse_iterator rit = list->rbegin();
1316                 /* Skip past curr */
1317                 for (; (*rit) != curr; rit++)
1318                         ;
1319                 /* go past curr now */
1320                 rit++;
1321
1322                 action_list_t::reverse_iterator ritcopy = rit;
1323                 //See if we have enough reads from the same value
1324                 int count = 0;
1325                 for (; count < params.maxreads; rit++,count++) {
1326                         if (rit==list->rend())
1327                                 return;
1328                         ModelAction *act = *rit;
1329                         if (!act->is_read())
1330                                 return;
1331
1332                         if (act->get_reads_from() != rf)
1333                                 return;
1334                         if (act->get_node()->get_read_from_size() <= 1)
1335                                 return;
1336                 }
1337                 for (int i = 0; i<curr->get_node()->get_read_from_size(); i++) {
1338                         //Get write
1339                         const ModelAction * write = curr->get_node()->get_read_from_at(i);
1340
1341                         //Need a different write
1342                         if (write==rf)
1343                                 continue;
1344
1345                         /* Test to see whether this is a feasible write to read from*/
1346                         mo_graph->startChanges();
1347                         r_modification_order(curr, write);
1348                         bool feasiblereadfrom = !is_infeasible();
1349                         mo_graph->rollbackChanges();
1350
1351                         if (!feasiblereadfrom)
1352                                 continue;
1353                         rit = ritcopy;
1354
1355                         bool feasiblewrite = true;
1356                         //new we need to see if this write works for everyone
1357
1358                         for (int loop = count; loop>0; loop--,rit++) {
1359                                 ModelAction *act=*rit;
1360                                 bool foundvalue = false;
1361                                 for (int j = 0; j<act->get_node()->get_read_from_size(); j++) {
1362                                         if (act->get_node()->get_read_from_at(j)==write) {
1363                                                 foundvalue = true;
1364                                                 break;
1365                                         }
1366                                 }
1367                                 if (!foundvalue) {
1368                                         feasiblewrite = false;
1369                                         break;
1370                                 }
1371                         }
1372                         if (feasiblewrite) {
1373                                 priv->too_many_reads = true;
1374                                 return;
1375                         }
1376                 }
1377         }
1378 }
1379
1380 /**
1381  * Updates the mo_graph with the constraints imposed from the current
1382  * read.
1383  *
1384  * Basic idea is the following: Go through each other thread and find
1385  * the lastest action that happened before our read.  Two cases:
1386  *
1387  * (1) The action is a write => that write must either occur before
1388  * the write we read from or be the write we read from.
1389  *
1390  * (2) The action is a read => the write that that action read from
1391  * must occur before the write we read from or be the same write.
1392  *
1393  * @param curr The current action. Must be a read.
1394  * @param rf The action that curr reads from. Must be a write.
1395  * @return True if modification order edges were added; false otherwise
1396  */
1397 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
1398 {
1399         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1400         unsigned int i;
1401         bool added = false;
1402         ASSERT(curr->is_read());
1403
1404         /* Iterate over all threads */
1405         for (i = 0; i < thrd_lists->size(); i++) {
1406                 /* Iterate over actions in thread, starting from most recent */
1407                 action_list_t *list = &(*thrd_lists)[i];
1408                 action_list_t::reverse_iterator rit;
1409                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1410                         ModelAction *act = *rit;
1411
1412                         /*
1413                          * Include at most one act per-thread that "happens
1414                          * before" curr. Don't consider reflexively.
1415                          */
1416                         if (act->happens_before(curr) && act != curr) {
1417                                 if (act->is_write()) {
1418                                         if (rf != act) {
1419                                                 mo_graph->addEdge(act, rf);
1420                                                 added = true;
1421                                         }
1422                                 } else {
1423                                         const ModelAction *prevreadfrom = act->get_reads_from();
1424                                         //if the previous read is unresolved, keep going...
1425                                         if (prevreadfrom == NULL)
1426                                                 continue;
1427
1428                                         if (rf != prevreadfrom) {
1429                                                 mo_graph->addEdge(prevreadfrom, rf);
1430                                                 added = true;
1431                                         }
1432                                 }
1433                                 break;
1434                         }
1435                 }
1436         }
1437
1438         return added;
1439 }
1440
1441 /** This method fixes up the modification order when we resolve a
1442  *  promises.  The basic problem is that actions that occur after the
1443  *  read curr could not property add items to the modification order
1444  *  for our read.
1445  *
1446  *  So for each thread, we find the earliest item that happens after
1447  *  the read curr.  This is the item we have to fix up with additional
1448  *  constraints.  If that action is write, we add a MO edge between
1449  *  the Action rf and that action.  If the action is a read, we add a
1450  *  MO edge between the Action rf, and whatever the read accessed.
1451  *
1452  * @param curr is the read ModelAction that we are fixing up MO edges for.
1453  * @param rf is the write ModelAction that curr reads from.
1454  *
1455  */
1456 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
1457 {
1458         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1459         unsigned int i;
1460         ASSERT(curr->is_read());
1461
1462         /* Iterate over all threads */
1463         for (i = 0; i < thrd_lists->size(); i++) {
1464                 /* Iterate over actions in thread, starting from most recent */
1465                 action_list_t *list = &(*thrd_lists)[i];
1466                 action_list_t::reverse_iterator rit;
1467                 ModelAction *lastact = NULL;
1468
1469                 /* Find last action that happens after curr that is either not curr or a rmw */
1470                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1471                         ModelAction *act = *rit;
1472                         if (curr->happens_before(act) && (curr != act || curr->is_rmw())) {
1473                                 lastact = act;
1474                         } else
1475                                 break;
1476                 }
1477
1478                         /* Include at most one act per-thread that "happens before" curr */
1479                 if (lastact != NULL) {
1480                         if (lastact==curr) {
1481                                 //Case 1: The resolved read is a RMW, and we need to make sure
1482                                 //that the write portion of the RMW mod order after rf
1483
1484                                 mo_graph->addEdge(rf, lastact);
1485                         } else if (lastact->is_read()) {
1486                                 //Case 2: The resolved read is a normal read and the next
1487                                 //operation is a read, and we need to make sure the value read
1488                                 //is mod ordered after rf
1489
1490                                 const ModelAction *postreadfrom = lastact->get_reads_from();
1491                                 if (postreadfrom != NULL&&rf != postreadfrom)
1492                                         mo_graph->addEdge(rf, postreadfrom);
1493                         } else {
1494                                 //Case 3: The resolved read is a normal read and the next
1495                                 //operation is a write, and we need to make sure that the
1496                                 //write is mod ordered after rf
1497                                 if (lastact!=rf)
1498                                         mo_graph->addEdge(rf, lastact);
1499                         }
1500                         break;
1501                 }
1502         }
1503 }
1504
1505 /**
1506  * Updates the mo_graph with the constraints imposed from the current write.
1507  *
1508  * Basic idea is the following: Go through each other thread and find
1509  * the lastest action that happened before our write.  Two cases:
1510  *
1511  * (1) The action is a write => that write must occur before
1512  * the current write
1513  *
1514  * (2) The action is a read => the write that that action read from
1515  * must occur before the current write.
1516  *
1517  * This method also handles two other issues:
1518  *
1519  * (I) Sequential Consistency: Making sure that if the current write is
1520  * seq_cst, that it occurs after the previous seq_cst write.
1521  *
1522  * (II) Sending the write back to non-synchronizing reads.
1523  *
1524  * @param curr The current action. Must be a write.
1525  * @return True if modification order edges were added; false otherwise
1526  */
1527 bool ModelChecker::w_modification_order(ModelAction *curr)
1528 {
1529         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1530         unsigned int i;
1531         bool added = false;
1532         ASSERT(curr->is_write());
1533
1534         if (curr->is_seqcst()) {
1535                 /* We have to at least see the last sequentially consistent write,
1536                          so we are initialized. */
1537                 ModelAction *last_seq_cst = get_last_seq_cst(curr);
1538                 if (last_seq_cst != NULL) {
1539                         mo_graph->addEdge(last_seq_cst, curr);
1540                         added = true;
1541                 }
1542         }
1543
1544         /* Iterate over all threads */
1545         for (i = 0; i < thrd_lists->size(); i++) {
1546                 /* Iterate over actions in thread, starting from most recent */
1547                 action_list_t *list = &(*thrd_lists)[i];
1548                 action_list_t::reverse_iterator rit;
1549                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1550                         ModelAction *act = *rit;
1551                         if (act == curr) {
1552                                 /*
1553                                  * 1) If RMW and it actually read from something, then we
1554                                  * already have all relevant edges, so just skip to next
1555                                  * thread.
1556                                  *
1557                                  * 2) If RMW and it didn't read from anything, we should
1558                                  * whatever edge we can get to speed up convergence.
1559                                  *
1560                                  * 3) If normal write, we need to look at earlier actions, so
1561                                  * continue processing list.
1562                                  */
1563                                 if (curr->is_rmw()) {
1564                                         if (curr->get_reads_from()!=NULL)
1565                                                 break;
1566                                         else
1567                                                 continue;
1568                                 } else
1569                                         continue;
1570                         }
1571
1572                         /*
1573                          * Include at most one act per-thread that "happens
1574                          * before" curr
1575                          */
1576                         if (act->happens_before(curr)) {
1577                                 /*
1578                                  * Note: if act is RMW, just add edge:
1579                                  *   act --mo--> curr
1580                                  * The following edge should be handled elsewhere:
1581                                  *   readfrom(act) --mo--> act
1582                                  */
1583                                 if (act->is_write())
1584                                         mo_graph->addEdge(act, curr);
1585                                 else if (act->is_read()) {
1586                                         //if previous read accessed a null, just keep going
1587                                         if (act->get_reads_from() == NULL)
1588                                                 continue;
1589                                         mo_graph->addEdge(act->get_reads_from(), curr);
1590                                 }
1591                                 added = true;
1592                                 break;
1593                         } else if (act->is_read() && !act->could_synchronize_with(curr) &&
1594                                                      !act->same_thread(curr)) {
1595                                 /* We have an action that:
1596                                    (1) did not happen before us
1597                                    (2) is a read and we are a write
1598                                    (3) cannot synchronize with us
1599                                    (4) is in a different thread
1600                                    =>
1601                                    that read could potentially read from our write.  Note that
1602                                    these checks are overly conservative at this point, we'll
1603                                    do more checks before actually removing the
1604                                    pendingfuturevalue.
1605
1606                                  */
1607                                 if (thin_air_constraint_may_allow(curr, act)) {
1608                                         if (!is_infeasible() ||
1609                                                         (curr->is_rmw() && act->is_rmw() && curr->get_reads_from() == act->get_reads_from() && !is_infeasible_ignoreRMW())) {
1610                                                 struct PendingFutureValue pfv = {curr,act};
1611                                                 futurevalues->push_back(pfv);
1612                                         }
1613                                 }
1614                         }
1615                 }
1616         }
1617
1618         return added;
1619 }
1620
1621 /** Arbitrary reads from the future are not allowed.  Section 29.3
1622  * part 9 places some constraints.  This method checks one result of constraint
1623  * constraint.  Others require compiler support. */
1624 bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader) {
1625         if (!writer->is_rmw())
1626                 return true;
1627
1628         if (!reader->is_rmw())
1629                 return true;
1630
1631         for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
1632                 if (search == reader)
1633                         return false;
1634                 if (search->get_tid() == reader->get_tid() &&
1635                                 search->happens_before(reader))
1636                         break;
1637         }
1638
1639         return true;
1640 }
1641
1642 /**
1643  * Arbitrary reads from the future are not allowed. Section 29.3 part 9 places
1644  * some constraints. This method checks one the following constraint (others
1645  * require compiler support):
1646  *
1647  *   If X --hb-> Y --mo-> Z, then X should not read from Z.
1648  */
1649 bool ModelChecker::mo_may_allow(const ModelAction *writer, const ModelAction *reader)
1650 {
1651         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, reader->get_location());
1652         unsigned int i;
1653         /* Iterate over all threads */
1654         for (i = 0; i < thrd_lists->size(); i++) {
1655                 const ModelAction *write_after_read = NULL;
1656
1657                 /* Iterate over actions in thread, starting from most recent */
1658                 action_list_t *list = &(*thrd_lists)[i];
1659                 action_list_t::reverse_iterator rit;
1660                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1661                         ModelAction *act = *rit;
1662
1663                         if (!reader->happens_before(act))
1664                                 break;
1665                         else if (act->is_write())
1666                                 write_after_read = act;
1667                         else if (act->is_read() && act->get_reads_from() != NULL && act != reader) {
1668                                 write_after_read = act->get_reads_from();
1669                         }
1670                 }
1671
1672                 if (write_after_read && write_after_read!=writer && mo_graph->checkReachable(write_after_read, writer))
1673                         return false;
1674         }
1675         return true;
1676 }
1677
1678 /**
1679  * Finds the head(s) of the release sequence(s) containing a given ModelAction.
1680  * The ModelAction under consideration is expected to be taking part in
1681  * release/acquire synchronization as an object of the "reads from" relation.
1682  * Note that this can only provide release sequence support for RMW chains
1683  * which do not read from the future, as those actions cannot be traced until
1684  * their "promise" is fulfilled. Similarly, we may not even establish the
1685  * presence of a release sequence with certainty, as some modification order
1686  * constraints may be decided further in the future. Thus, this function
1687  * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
1688  * and a boolean representing certainty.
1689  *
1690  * @param rf The action that might be part of a release sequence. Must be a
1691  * write.
1692  * @param release_heads A pass-by-reference style return parameter. After
1693  * execution of this function, release_heads will contain the heads of all the
1694  * relevant release sequences, if any exists with certainty
1695  * @param pending A pass-by-reference style return parameter which is only used
1696  * when returning false (i.e., uncertain). Returns most information regarding
1697  * an uncertain release sequence, including any write operations that might
1698  * break the sequence.
1699  * @return true, if the ModelChecker is certain that release_heads is complete;
1700  * false otherwise
1701  */
1702 bool ModelChecker::release_seq_heads(const ModelAction *rf,
1703                 rel_heads_list_t *release_heads,
1704                 struct release_seq *pending) const
1705 {
1706         /* Only check for release sequences if there are no cycles */
1707         if (mo_graph->checkForCycles())
1708                 return false;
1709
1710         while (rf) {
1711                 ASSERT(rf->is_write());
1712
1713                 if (rf->is_release())
1714                         release_heads->push_back(rf);
1715                 if (!rf->is_rmw())
1716                         break; /* End of RMW chain */
1717
1718                 /** @todo Need to be smarter here...  In the linux lock
1719                  * example, this will run to the beginning of the program for
1720                  * every acquire. */
1721                 /** @todo The way to be smarter here is to keep going until 1
1722                  * thread has a release preceded by an acquire and you've seen
1723                  *       both. */
1724
1725                 /* acq_rel RMW is a sufficient stopping condition */
1726                 if (rf->is_acquire() && rf->is_release())
1727                         return true; /* complete */
1728
1729                 rf = rf->get_reads_from();
1730         };
1731         if (!rf) {
1732                 /* read from future: need to settle this later */
1733                 pending->rf = NULL;
1734                 return false; /* incomplete */
1735         }
1736
1737         if (rf->is_release())
1738                 return true; /* complete */
1739
1740         /* else relaxed write; check modification order for contiguous subsequence
1741          * -> rf must be same thread as release */
1742         int tid = id_to_int(rf->get_tid());
1743         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, rf->get_location());
1744         action_list_t *list = &(*thrd_lists)[tid];
1745         action_list_t::const_reverse_iterator rit;
1746
1747         /* Find rf in the thread list */
1748         rit = std::find(list->rbegin(), list->rend(), rf);
1749         ASSERT(rit != list->rend());
1750
1751         /* Find the last write/release */
1752         for (; rit != list->rend(); rit++)
1753                 if ((*rit)->is_release())
1754                         break;
1755         if (rit == list->rend()) {
1756                 /* No write-release in this thread */
1757                 return true; /* complete */
1758         }
1759         ModelAction *release = *rit;
1760
1761         ASSERT(rf->same_thread(release));
1762
1763         pending->writes.clear();
1764
1765         bool certain = true;
1766         for (unsigned int i = 0; i < thrd_lists->size(); i++) {
1767                 if (id_to_int(rf->get_tid()) == (int)i)
1768                         continue;
1769                 list = &(*thrd_lists)[i];
1770
1771                 /* Can we ensure no future writes from this thread may break
1772                  * the release seq? */
1773                 bool future_ordered = false;
1774
1775                 ModelAction *last = get_last_action(int_to_id(i));
1776                 Thread *th = get_thread(int_to_id(i));
1777                 if ((last && rf->happens_before(last)) ||
1778                                 !is_enabled(th) ||
1779                                 th->is_complete())
1780                         future_ordered = true;
1781
1782                 ASSERT(!th->is_model_thread() || future_ordered);
1783
1784                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1785                         const ModelAction *act = *rit;
1786                         /* Reach synchronization -> this thread is complete */
1787                         if (act->happens_before(release))
1788                                 break;
1789                         if (rf->happens_before(act)) {
1790                                 future_ordered = true;
1791                                 continue;
1792                         }
1793
1794                         /* Only non-RMW writes can break release sequences */
1795                         if (!act->is_write() || act->is_rmw())
1796                                 continue;
1797
1798                         /* Check modification order */
1799                         if (mo_graph->checkReachable(rf, act)) {
1800                                 /* rf --mo--> act */
1801                                 future_ordered = true;
1802                                 continue;
1803                         }
1804                         if (mo_graph->checkReachable(act, release))
1805                                 /* act --mo--> release */
1806                                 break;
1807                         if (mo_graph->checkReachable(release, act) &&
1808                                       mo_graph->checkReachable(act, rf)) {
1809                                 /* release --mo-> act --mo--> rf */
1810                                 return true; /* complete */
1811                         }
1812                         /* act may break release sequence */
1813                         pending->writes.push_back(act);
1814                         certain = false;
1815                 }
1816                 if (!future_ordered)
1817                         certain = false; /* This thread is uncertain */
1818         }
1819
1820         if (certain) {
1821                 release_heads->push_back(release);
1822                 pending->writes.clear();
1823         } else {
1824                 pending->release = release;
1825                 pending->rf = rf;
1826         }
1827         return certain;
1828 }
1829
1830 /**
1831  * A public interface for getting the release sequence head(s) with which a
1832  * given ModelAction must synchronize. This function only returns a non-empty
1833  * result when it can locate a release sequence head with certainty. Otherwise,
1834  * it may mark the internal state of the ModelChecker so that it will handle
1835  * the release sequence at a later time, causing @a act to update its
1836  * synchronization at some later point in execution.
1837  * @param act The 'acquire' action that may read from a release sequence
1838  * @param release_heads A pass-by-reference return parameter. Will be filled
1839  * with the head(s) of the release sequence(s), if they exists with certainty.
1840  * @see ModelChecker::release_seq_heads
1841  */
1842 void ModelChecker::get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads)
1843 {
1844         const ModelAction *rf = act->get_reads_from();
1845         struct release_seq *sequence = (struct release_seq *)snapshot_calloc(1, sizeof(struct release_seq));
1846         sequence->acquire = act;
1847
1848         if (!release_seq_heads(rf, release_heads, sequence)) {
1849                 /* add act to 'lazy checking' list */
1850                 pending_rel_seqs->push_back(sequence);
1851         } else {
1852                 snapshot_free(sequence);
1853         }
1854 }
1855
1856 /**
1857  * Attempt to resolve all stashed operations that might synchronize with a
1858  * release sequence for a given location. This implements the "lazy" portion of
1859  * determining whether or not a release sequence was contiguous, since not all
1860  * modification order information is present at the time an action occurs.
1861  *
1862  * @param location The location/object that should be checked for release
1863  * sequence resolutions. A NULL value means to check all locations.
1864  * @param work_queue The work queue to which to add work items as they are
1865  * generated
1866  * @return True if any updates occurred (new synchronization, new mo_graph
1867  * edges)
1868  */
1869 bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
1870 {
1871         bool updated = false;
1872         std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> >::iterator it = pending_rel_seqs->begin();
1873         while (it != pending_rel_seqs->end()) {
1874                 struct release_seq *pending = *it;
1875                 ModelAction *act = pending->acquire;
1876
1877                 /* Only resolve sequences on the given location, if provided */
1878                 if (location && act->get_location() != location) {
1879                         it++;
1880                         continue;
1881                 }
1882
1883                 const ModelAction *rf = act->get_reads_from();
1884                 rel_heads_list_t release_heads;
1885                 bool complete;
1886                 complete = release_seq_heads(rf, &release_heads, pending);
1887                 for (unsigned int i = 0; i < release_heads.size(); i++) {
1888                         if (!act->has_synchronized_with(release_heads[i])) {
1889                                 if (act->synchronize_with(release_heads[i]))
1890                                         updated = true;
1891                                 else
1892                                         set_bad_synchronization();
1893                         }
1894                 }
1895
1896                 if (updated) {
1897                         /* Re-check all pending release sequences */
1898                         work_queue->push_back(CheckRelSeqWorkEntry(NULL));
1899                         /* Re-check act for mo_graph edges */
1900                         work_queue->push_back(MOEdgeWorkEntry(act));
1901
1902                         /* propagate synchronization to later actions */
1903                         action_list_t::reverse_iterator rit = action_trace->rbegin();
1904                         for (; (*rit) != act; rit++) {
1905                                 ModelAction *propagate = *rit;
1906                                 if (act->happens_before(propagate)) {
1907                                         propagate->synchronize_with(act);
1908                                         /* Re-check 'propagate' for mo_graph edges */
1909                                         work_queue->push_back(MOEdgeWorkEntry(propagate));
1910                                 }
1911                         }
1912                 }
1913                 if (complete) {
1914                         it = pending_rel_seqs->erase(it);
1915                         snapshot_free(pending);
1916                 } else {
1917                         it++;
1918                 }
1919         }
1920
1921         // If we resolved promises or data races, see if we have realized a data race.
1922         checkDataRaces();
1923
1924         return updated;
1925 }
1926
1927 /**
1928  * Performs various bookkeeping operations for the current ModelAction. For
1929  * instance, adds action to the per-object, per-thread action vector and to the
1930  * action trace list of all thread actions.
1931  *
1932  * @param act is the ModelAction to add.
1933  */
1934 void ModelChecker::add_action_to_lists(ModelAction *act)
1935 {
1936         int tid = id_to_int(act->get_tid());
1937         action_trace->push_back(act);
1938
1939         get_safe_ptr_action(obj_map, act->get_location())->push_back(act);
1940
1941         std::vector<action_list_t> *vec = get_safe_ptr_vect_action(obj_thrd_map, act->get_location());
1942         if (tid >= (int)vec->size())
1943                 vec->resize(priv->next_thread_id);
1944         (*vec)[tid].push_back(act);
1945
1946         if ((int)thrd_last_action->size() <= tid)
1947                 thrd_last_action->resize(get_num_threads());
1948         (*thrd_last_action)[tid] = act;
1949
1950         if (act->is_wait()) {
1951                 void *mutex_loc=(void *) act->get_value();
1952                 get_safe_ptr_action(obj_map, mutex_loc)->push_back(act);
1953
1954                 std::vector<action_list_t> *vec = get_safe_ptr_vect_action(obj_thrd_map, mutex_loc);
1955                 if (tid >= (int)vec->size())
1956                         vec->resize(priv->next_thread_id);
1957                 (*vec)[tid].push_back(act);
1958
1959                 if ((int)thrd_last_action->size() <= tid)
1960                         thrd_last_action->resize(get_num_threads());
1961                 (*thrd_last_action)[tid] = act;
1962         }
1963 }
1964
1965 /**
1966  * @brief Get the last action performed by a particular Thread
1967  * @param tid The thread ID of the Thread in question
1968  * @return The last action in the thread
1969  */
1970 ModelAction * ModelChecker::get_last_action(thread_id_t tid) const
1971 {
1972         int threadid = id_to_int(tid);
1973         if (threadid < (int)thrd_last_action->size())
1974                 return (*thrd_last_action)[id_to_int(tid)];
1975         else
1976                 return NULL;
1977 }
1978
1979 /**
1980  * Gets the last memory_order_seq_cst write (in the total global sequence)
1981  * performed on a particular object (i.e., memory location), not including the
1982  * current action.
1983  * @param curr The current ModelAction; also denotes the object location to
1984  * check
1985  * @return The last seq_cst write
1986  */
1987 ModelAction * ModelChecker::get_last_seq_cst(ModelAction *curr) const
1988 {
1989         void *location = curr->get_location();
1990         action_list_t *list = get_safe_ptr_action(obj_map, location);
1991         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
1992         action_list_t::reverse_iterator rit;
1993         for (rit = list->rbegin(); rit != list->rend(); rit++)
1994                 if ((*rit)->is_write() && (*rit)->is_seqcst() && (*rit) != curr)
1995                         return *rit;
1996         return NULL;
1997 }
1998
1999 /**
2000  * Gets the last unlock operation performed on a particular mutex (i.e., memory
2001  * location). This function identifies the mutex according to the current
2002  * action, which is presumed to perform on the same mutex.
2003  * @param curr The current ModelAction; also denotes the object location to
2004  * check
2005  * @return The last unlock operation
2006  */
2007 ModelAction * ModelChecker::get_last_unlock(ModelAction *curr) const
2008 {
2009         void *location = curr->get_location();
2010         action_list_t *list = get_safe_ptr_action(obj_map, location);
2011         /* Find: max({i in dom(S) | isUnlock(t_i) && samevar(t_i, t)}) */
2012         action_list_t::reverse_iterator rit;
2013         for (rit = list->rbegin(); rit != list->rend(); rit++)
2014                 if ((*rit)->is_unlock() || (*rit)->is_wait())
2015                         return *rit;
2016         return NULL;
2017 }
2018
2019 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
2020 {
2021         ModelAction *parent = get_last_action(tid);
2022         if (!parent)
2023                 parent = get_thread(tid)->get_creation();
2024         return parent;
2025 }
2026
2027 /**
2028  * Returns the clock vector for a given thread.
2029  * @param tid The thread whose clock vector we want
2030  * @return Desired clock vector
2031  */
2032 ClockVector * ModelChecker::get_cv(thread_id_t tid)
2033 {
2034         return get_parent_action(tid)->get_cv();
2035 }
2036
2037 /**
2038  * Resolve a set of Promises with a current write. The set is provided in the
2039  * Node corresponding to @a write.
2040  * @param write The ModelAction that is fulfilling Promises
2041  * @return True if promises were resolved; false otherwise
2042  */
2043 bool ModelChecker::resolve_promises(ModelAction *write)
2044 {
2045         bool resolved = false;
2046         std::vector< thread_id_t, ModelAlloc<thread_id_t> > threads_to_check;
2047
2048         for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
2049                 Promise *promise = (*promises)[promise_index];
2050                 if (write->get_node()->get_promise(i)) {
2051                         ModelAction *read = promise->get_action();
2052                         if (read->is_rmw()) {
2053                                 mo_graph->addRMWEdge(write, read);
2054                         }
2055                         read->read_from(write);
2056                         //First fix up the modification order for actions that happened
2057                         //before the read
2058                         r_modification_order(read, write);
2059                         //Next fix up the modification order for actions that happened
2060                         //after the read.
2061                         post_r_modification_order(read, write);
2062                         //Make sure the promise's value matches the write's value
2063                         ASSERT(promise->get_value() == write->get_value());
2064                         delete(promise);
2065
2066                         promises->erase(promises->begin() + promise_index);
2067                         threads_to_check.push_back(read->get_tid());
2068
2069                         resolved = true;
2070                 } else
2071                         promise_index++;
2072         }
2073
2074         //Check whether reading these writes has made threads unable to
2075         //resolve promises
2076
2077         for(unsigned int i=0;i<threads_to_check.size();i++)
2078                 mo_check_promises(threads_to_check[i], write);
2079
2080         return resolved;
2081 }
2082
2083 /**
2084  * Compute the set of promises that could potentially be satisfied by this
2085  * action. Note that the set computation actually appears in the Node, not in
2086  * ModelChecker.
2087  * @param curr The ModelAction that may satisfy promises
2088  */
2089 void ModelChecker::compute_promises(ModelAction *curr)
2090 {
2091         for (unsigned int i = 0; i < promises->size(); i++) {
2092                 Promise *promise = (*promises)[i];
2093                 const ModelAction *act = promise->get_action();
2094                 if (!act->happens_before(curr) &&
2095                                 act->is_read() &&
2096                                 !act->could_synchronize_with(curr) &&
2097                                 !act->same_thread(curr) &&
2098                                 act->get_location() == curr->get_location() &&
2099                                 promise->get_value() == curr->get_value()) {
2100                         curr->get_node()->set_promise(i, act->is_rmw());
2101                 }
2102         }
2103 }
2104
2105 /** Checks promises in response to change in ClockVector Threads. */
2106 void ModelChecker::check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv)
2107 {
2108         for (unsigned int i = 0; i < promises->size(); i++) {
2109                 Promise *promise = (*promises)[i];
2110                 const ModelAction *act = promise->get_action();
2111                 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
2112                                 merge_cv->synchronized_since(act)) {
2113                         if (promise->increment_threads(tid)) {
2114                                 //Promise has failed
2115                                 priv->failed_promise = true;
2116                                 return;
2117                         }
2118                 }
2119         }
2120 }
2121
2122 void ModelChecker::check_promises_thread_disabled() {
2123         for (unsigned int i = 0; i < promises->size(); i++) {
2124                 Promise *promise = (*promises)[i];
2125                 if (promise->check_promise()) {
2126                         priv->failed_promise = true;
2127                         return;
2128                 }
2129         }
2130 }
2131
2132 /** Checks promises in response to addition to modification order for threads.
2133  * Definitions:
2134  * pthread is the thread that performed the read that created the promise
2135  *
2136  * pread is the read that created the promise
2137  *
2138  * pwrite is either the first write to same location as pread by
2139  * pthread that is sequenced after pread or the value read by the
2140  * first read to the same lcoation as pread by pthread that is
2141  * sequenced after pread..
2142  *
2143  *      1. If tid=pthread, then we check what other threads are reachable
2144  * through the mode order starting with pwrite.  Those threads cannot
2145  * perform a write that will resolve the promise due to modification
2146  * order constraints.
2147  *
2148  * 2. If the tid is not pthread, we check whether pwrite can reach the
2149  * action write through the modification order.  If so, that thread
2150  * cannot perform a future write that will resolve the promise due to
2151  * modificatin order constraints.
2152  *
2153  *      @parem tid The thread that either read from the model action
2154  *      write, or actually did the model action write.
2155  *
2156  *      @parem write The ModelAction representing the relevant write.
2157  */
2158
2159 void ModelChecker::mo_check_promises(thread_id_t tid, const ModelAction *write) {
2160         void * location = write->get_location();
2161         for (unsigned int i = 0; i < promises->size(); i++) {
2162                 Promise *promise = (*promises)[i];
2163                 const ModelAction *act = promise->get_action();
2164
2165                 //Is this promise on the same location?
2166                 if ( act->get_location() != location )
2167                         continue;
2168
2169                 //same thread as the promise
2170                 if ( act->get_tid()==tid ) {
2171
2172                         //do we have a pwrite for the promise, if not, set it
2173                         if (promise->get_write() == NULL ) {
2174                                 promise->set_write(write);
2175                                 //The pwrite cannot happen before the promise
2176                                 if (write->happens_before(act) && (write != act)) {
2177                                         priv->failed_promise = true;
2178                                         return;
2179                                 }
2180                         }
2181                         if (mo_graph->checkPromise(write, promise)) {
2182                                 priv->failed_promise = true;
2183                                 return;
2184                         }
2185                 }
2186
2187                 //Don't do any lookups twice for the same thread
2188                 if (promise->has_sync_thread(tid))
2189                         continue;
2190
2191                 if (promise->get_write()&&mo_graph->checkReachable(promise->get_write(), write)) {
2192                         if (promise->increment_threads(tid)) {
2193                                 priv->failed_promise = true;
2194                                 return;
2195                         }
2196                 }
2197         }
2198 }
2199
2200 /**
2201  * Compute the set of writes that may break the current pending release
2202  * sequence. This information is extracted from previou release sequence
2203  * calculations.
2204  *
2205  * @param curr The current ModelAction. Must be a release sequence fixup
2206  * action.
2207  */
2208 void ModelChecker::compute_relseq_breakwrites(ModelAction *curr)
2209 {
2210         if (pending_rel_seqs->empty())
2211                 return;
2212
2213         struct release_seq *pending = pending_rel_seqs->back();
2214         for (unsigned int i = 0; i < pending->writes.size(); i++) {
2215                 const ModelAction *write = pending->writes[i];
2216                 curr->get_node()->add_relseq_break(write);
2217         }
2218
2219         /* NULL means don't break the sequence; just synchronize */
2220         curr->get_node()->add_relseq_break(NULL);
2221 }
2222
2223 /**
2224  * Build up an initial set of all past writes that this 'read' action may read
2225  * from. This set is determined by the clock vector's "happens before"
2226  * relationship.
2227  * @param curr is the current ModelAction that we are exploring; it must be a
2228  * 'read' operation.
2229  */
2230 void ModelChecker::build_reads_from_past(ModelAction *curr)
2231 {
2232         std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
2233         unsigned int i;
2234         ASSERT(curr->is_read());
2235
2236         ModelAction *last_seq_cst = NULL;
2237
2238         /* Track whether this object has been initialized */
2239         bool initialized = false;
2240
2241         if (curr->is_seqcst()) {
2242                 last_seq_cst = get_last_seq_cst(curr);
2243                 /* We have to at least see the last sequentially consistent write,
2244                          so we are initialized. */
2245                 if (last_seq_cst != NULL)
2246                         initialized = true;
2247         }
2248
2249         /* Iterate over all threads */
2250         for (i = 0; i < thrd_lists->size(); i++) {
2251                 /* Iterate over actions in thread, starting from most recent */
2252                 action_list_t *list = &(*thrd_lists)[i];
2253                 action_list_t::reverse_iterator rit;
2254                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
2255                         ModelAction *act = *rit;
2256
2257                         /* Only consider 'write' actions */
2258                         if (!act->is_write() || act == curr)
2259                                 continue;
2260
2261                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
2262                         if (!curr->is_seqcst() || (!act->is_seqcst() && (last_seq_cst == NULL || !act->happens_before(last_seq_cst))) || act == last_seq_cst) {
2263                                 if (!curr->get_sleep_flag() || curr->is_seqcst() || sleep_can_read_from(curr, act)) {
2264                                         DEBUG("Adding action to may_read_from:\n");
2265                                         if (DBG_ENABLED()) {
2266                                                 act->print();
2267                                                 curr->print();
2268                                         }
2269                                         curr->get_node()->add_read_from(act);
2270                                 }
2271                         }
2272
2273                         /* Include at most one act per-thread that "happens before" curr */
2274                         if (act->happens_before(curr)) {
2275                                 initialized = true;
2276                                 break;
2277                         }
2278                 }
2279         }
2280
2281         if (!initialized)
2282                 assert_bug("May read from uninitialized atomic");
2283
2284         if (DBG_ENABLED() || !initialized) {
2285                 model_print("Reached read action:\n");
2286                 curr->print();
2287                 model_print("Printing may_read_from\n");
2288                 curr->get_node()->print_may_read_from();
2289                 model_print("End printing may_read_from\n");
2290         }
2291 }
2292
2293 bool ModelChecker::sleep_can_read_from(ModelAction * curr, const ModelAction *write) {
2294         while(true) {
2295                 Node *prevnode=write->get_node()->get_parent();
2296
2297                 bool thread_sleep=prevnode->enabled_status(curr->get_tid())==THREAD_SLEEP_SET;
2298                 if (write->is_release()&&thread_sleep)
2299                         return true;
2300                 if (!write->is_rmw()) {
2301                         return false;
2302                 }
2303                 if (write->get_reads_from()==NULL)
2304                         return true;
2305                 write=write->get_reads_from();
2306         }
2307 }
2308
2309 static void print_list(action_list_t *list, int exec_num = -1)
2310 {
2311         action_list_t::iterator it;
2312
2313         model_print("---------------------------------------------------------------------\n");
2314         if (exec_num >= 0)
2315                 model_print("Execution %d:\n", exec_num);
2316
2317         unsigned int hash=0;
2318
2319         for (it = list->begin(); it != list->end(); it++) {
2320                 (*it)->print();
2321                 hash=hash^(hash<<3)^((*it)->hash());
2322         }
2323         model_print("HASH %u\n", hash);
2324         model_print("---------------------------------------------------------------------\n");
2325 }
2326
2327 #if SUPPORT_MOD_ORDER_DUMP
2328 void ModelChecker::dumpGraph(char *filename) {
2329         char buffer[200];
2330         sprintf(buffer, "%s.dot",filename);
2331         FILE *file=fopen(buffer, "w");
2332         fprintf(file, "digraph %s {\n",filename);
2333         mo_graph->dumpNodes(file);
2334         ModelAction ** thread_array=(ModelAction **)model_calloc(1, sizeof(ModelAction *)*get_num_threads());
2335
2336         for (action_list_t::iterator it = action_trace->begin(); it != action_trace->end(); it++) {
2337                 ModelAction *action=*it;
2338                 if (action->is_read()) {
2339                         fprintf(file, "N%u [label=\"%u, T%u\"];\n", action->get_seq_number(),action->get_seq_number(), action->get_tid());
2340                         if (action->get_reads_from()!=NULL)
2341                                 fprintf(file, "N%u -> N%u[label=\"rf\", color=red];\n", action->get_seq_number(), action->get_reads_from()->get_seq_number());
2342                 }
2343                 if (thread_array[action->get_tid()] != NULL) {
2344                         fprintf(file, "N%u -> N%u[label=\"sb\", color=blue];\n", thread_array[action->get_tid()]->get_seq_number(), action->get_seq_number());
2345                 }
2346
2347                 thread_array[action->get_tid()]=action;
2348         }
2349         fprintf(file,"}\n");
2350         model_free(thread_array);
2351         fclose(file);
2352 }
2353 #endif
2354
2355 /** @brief Prints an execution trace summary. */
2356 void ModelChecker::print_summary() const
2357 {
2358 #if SUPPORT_MOD_ORDER_DUMP
2359         scheduler->print();
2360         char buffername[100];
2361         sprintf(buffername, "exec%04u", stats.num_total);
2362         mo_graph->dumpGraphToFile(buffername);
2363         sprintf(buffername, "graph%04u", stats.num_total);
2364         dumpGraph(buffername);
2365 #endif
2366
2367         if (!isfeasibleprefix())
2368                 model_print("INFEASIBLE EXECUTION!\n");
2369         print_list(action_trace, stats.num_total);
2370         model_print("\n");
2371 }
2372
2373 /**
2374  * Add a Thread to the system for the first time. Should only be called once
2375  * per thread.
2376  * @param t The Thread to add
2377  */
2378 void ModelChecker::add_thread(Thread *t)
2379 {
2380         thread_map->put(id_to_int(t->get_id()), t);
2381         scheduler->add_thread(t);
2382 }
2383
2384 /**
2385  * Removes a thread from the scheduler.
2386  * @param the thread to remove.
2387  */
2388 void ModelChecker::remove_thread(Thread *t)
2389 {
2390         scheduler->remove_thread(t);
2391 }
2392
2393 /**
2394  * @brief Get a Thread reference by its ID
2395  * @param tid The Thread's ID
2396  * @return A Thread reference
2397  */
2398 Thread * ModelChecker::get_thread(thread_id_t tid) const
2399 {
2400         return thread_map->get(id_to_int(tid));
2401 }
2402
2403 /**
2404  * @brief Get a reference to the Thread in which a ModelAction was executed
2405  * @param act The ModelAction
2406  * @return A Thread reference
2407  */
2408 Thread * ModelChecker::get_thread(ModelAction *act) const
2409 {
2410         return get_thread(act->get_tid());
2411 }
2412
2413 /**
2414  * @brief Check if a Thread is currently enabled
2415  * @param t The Thread to check
2416  * @return True if the Thread is currently enabled
2417  */
2418 bool ModelChecker::is_enabled(Thread *t) const
2419 {
2420         return scheduler->is_enabled(t);
2421 }
2422
2423 /**
2424  * @brief Check if a Thread is currently enabled
2425  * @param tid The ID of the Thread to check
2426  * @return True if the Thread is currently enabled
2427  */
2428 bool ModelChecker::is_enabled(thread_id_t tid) const
2429 {
2430         return scheduler->is_enabled(tid);
2431 }
2432
2433 /**
2434  * Switch from a user-context to the "master thread" context (a.k.a. system
2435  * context). This switch is made with the intention of exploring a particular
2436  * model-checking action (described by a ModelAction object). Must be called
2437  * from a user-thread context.
2438  *
2439  * @param act The current action that will be explored. May be NULL only if
2440  * trace is exiting via an assertion (see ModelChecker::set_assert and
2441  * ModelChecker::has_asserted).
2442  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
2443  */
2444 int ModelChecker::switch_to_master(ModelAction *act)
2445 {
2446         DBG();
2447         Thread *old = thread_current();
2448         set_current_action(act);
2449         old->set_state(THREAD_READY);
2450         return Thread::swap(old, &system_context);
2451 }
2452
2453 /**
2454  * Takes the next step in the execution, if possible.
2455  * @return Returns true (success) if a step was taken and false otherwise.
2456  */
2457 bool ModelChecker::take_step() {
2458         if (has_asserted())
2459                 return false;
2460
2461         Thread *curr = priv->current_action ? get_thread(priv->current_action) : NULL;
2462         if (curr) {
2463                 if (curr->get_state() == THREAD_READY) {
2464                         ASSERT(priv->current_action);
2465
2466                         priv->nextThread = check_current_action(priv->current_action);
2467                         priv->current_action = NULL;
2468
2469                         if (curr->is_blocked() || curr->is_complete())
2470                                 scheduler->remove_thread(curr);
2471                 } else {
2472                         ASSERT(false);
2473                 }
2474         }
2475         Thread *next = scheduler->next_thread(priv->nextThread);
2476
2477         /* Infeasible -> don't take any more steps */
2478         if (is_infeasible())
2479                 return false;
2480         else if (isfeasibleprefix() && have_bug_reports()) {
2481                 set_assert();
2482                 return false;
2483         }
2484
2485         if (params.bound != 0) {
2486                 if (priv->used_sequence_numbers > params.bound) {
2487                         return false;
2488                 }
2489         }
2490
2491         DEBUG("(%d, %d)\n", curr ? id_to_int(curr->get_id()) : -1,
2492                         next ? id_to_int(next->get_id()) : -1);
2493
2494         /*
2495          * Launch end-of-execution release sequence fixups only when there are:
2496          *
2497          * (1) no more user threads to run (or when execution replay chooses
2498          *     the 'model_thread')
2499          * (2) pending release sequences
2500          * (3) pending assertions (i.e., data races)
2501          * (4) no pending promises
2502          */
2503         if (!pending_rel_seqs->empty() && (!next || next->is_model_thread()) &&
2504                         is_feasible_prefix_ignore_relseq() && !unrealizedraces.empty()) {
2505                 model_print("*** WARNING: release sequence fixup action (%zu pending release seuqences) ***\n",
2506                                 pending_rel_seqs->size());
2507                 ModelAction *fixup = new ModelAction(MODEL_FIXUP_RELSEQ,
2508                                 std::memory_order_seq_cst, NULL, VALUE_NONE,
2509                                 model_thread);
2510                 set_current_action(fixup);
2511                 return true;
2512         }
2513
2514         /* next == NULL -> don't take any more steps */
2515         if (!next)
2516                 return false;
2517
2518         next->set_state(THREAD_RUNNING);
2519
2520         if (next->get_pending() != NULL) {
2521                 /* restart a pending action */
2522                 set_current_action(next->get_pending());
2523                 next->set_pending(NULL);
2524                 next->set_state(THREAD_READY);
2525                 return true;
2526         }
2527
2528         /* Return false only if swap fails with an error */
2529         return (Thread::swap(&system_context, next) == 0);
2530 }
2531
2532 /** Wrapper to run the user's main function, with appropriate arguments */
2533 void user_main_wrapper(void *)
2534 {
2535         user_main(model->params.argc, model->params.argv);
2536 }
2537
2538 /** @brief Run ModelChecker for the user program */
2539 void ModelChecker::run()
2540 {
2541         do {
2542                 thrd_t user_thread;
2543
2544                 /* Start user program */
2545                 add_thread(new Thread(&user_thread, &user_main_wrapper, NULL));
2546
2547                 /* Wait for all threads to complete */
2548                 while (take_step());
2549         } while (next_execution());
2550
2551         print_stats();
2552 }