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