567cbd7a3de983078cfd54677628b34ab60995f3
[model-checker.git] / model.cc
1 #include <stdio.h>
2 #include <algorithm>
3
4 #include "model.h"
5 #include "action.h"
6 #include "nodestack.h"
7 #include "schedule.h"
8 #include "snapshot-interface.h"
9 #include "common.h"
10 #include "clockvector.h"
11 #include "cyclegraph.h"
12 #include "promise.h"
13 #include "datarace.h"
14
15 #define INITIAL_THREAD_ID       0
16
17 ModelChecker *model;
18
19 /** @brief Constructor */
20 ModelChecker::ModelChecker(struct model_params params) :
21         /* Initialize default scheduler */
22         scheduler(new Scheduler()),
23         num_executions(0),
24         params(params),
25         diverge(NULL),
26         action_trace(new action_list_t()),
27         thread_map(new HashTable<int, Thread *, int>()),
28         obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
29         obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
30         promises(new std::vector<Promise *>()),
31         lazy_sync_with_release(new HashTable<void *, std::list<ModelAction *>, uintptr_t, 4>()),
32         thrd_last_action(new std::vector<ModelAction *>(1)),
33         node_stack(new NodeStack()),
34         mo_graph(new CycleGraph()),
35         failed_promise(false),
36         too_many_reads(false),
37         asserted(false)
38 {
39         /* Allocate this "size" on the snapshotting heap */
40         priv = (struct model_snapshot_members *)calloc(1, sizeof(*priv));
41         /* First thread created will have id INITIAL_THREAD_ID */
42         priv->next_thread_id = INITIAL_THREAD_ID;
43
44         lazy_sync_size = &priv->lazy_sync_size;
45 }
46
47 /** @brief Destructor */
48 ModelChecker::~ModelChecker()
49 {
50         for (int i = 0; i < get_num_threads(); i++)
51                 delete thread_map->get(i);
52         delete thread_map;
53
54         delete obj_thrd_map;
55         delete obj_map;
56         delete action_trace;
57
58         for (unsigned int i = 0; i < promises->size(); i++)
59                 delete (*promises)[i];
60         delete promises;
61
62         delete lazy_sync_with_release;
63
64         delete thrd_last_action;
65         delete node_stack;
66         delete scheduler;
67         delete mo_graph;
68 }
69
70 /**
71  * Restores user program to initial state and resets all model-checker data
72  * structures.
73  */
74 void ModelChecker::reset_to_initial_state()
75 {
76         DEBUG("+++ Resetting to initial state +++\n");
77         node_stack->reset_execution();
78         failed_promise = false;
79         too_many_reads = false;
80         reset_asserted();
81         snapshotObject->backTrackBeforeStep(0);
82 }
83
84 /** @returns a thread ID for a new Thread */
85 thread_id_t ModelChecker::get_next_id()
86 {
87         return priv->next_thread_id++;
88 }
89
90 /** @returns the number of user threads created during this execution */
91 int ModelChecker::get_num_threads()
92 {
93         return priv->next_thread_id;
94 }
95
96 /** @returns a sequence number for a new ModelAction */
97 modelclock_t ModelChecker::get_next_seq_num()
98 {
99         return ++priv->used_sequence_numbers;
100 }
101
102 /**
103  * Choose the next thread in the replay sequence.
104  *
105  * If the replay sequence has reached the 'diverge' point, returns a thread
106  * from the backtracking set. Otherwise, simply returns the next thread in the
107  * sequence that is being replayed.
108  */
109 Thread * ModelChecker::get_next_replay_thread()
110 {
111         thread_id_t tid;
112
113         /* Have we completed exploring the preselected path? */
114         if (diverge == NULL)
115                 return NULL;
116
117         /* Else, we are trying to replay an execution */
118         ModelAction *next = node_stack->get_next()->get_action();
119
120         if (next == diverge) {
121                 Node *nextnode = next->get_node();
122                 /* Reached divergence point */
123                 if (nextnode->increment_promise()) {
124                         /* The next node will try to satisfy a different set of promises. */
125                         tid = next->get_tid();
126                         node_stack->pop_restofstack(2);
127                 } else if (nextnode->increment_read_from()) {
128                         /* The next node will read from a different value. */
129                         tid = next->get_tid();
130                         node_stack->pop_restofstack(2);
131                 } else if (nextnode->increment_future_value()) {
132                         /* The next node will try to read from a different future value. */
133                         tid = next->get_tid();
134                         node_stack->pop_restofstack(2);
135                 } else {
136                         /* Make a different thread execute for next step */
137                         Node *node = nextnode->get_parent();
138                         tid = node->get_next_backtrack();
139                         node_stack->pop_restofstack(1);
140                 }
141                 DEBUG("*** Divergence point ***\n");
142                 diverge = NULL;
143         } else {
144                 tid = next->get_tid();
145         }
146         DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
147         ASSERT(tid != THREAD_ID_T_NONE);
148         return thread_map->get(id_to_int(tid));
149 }
150
151 /**
152  * Queries the model-checker for more executions to explore and, if one
153  * exists, resets the model-checker state to execute a new execution.
154  *
155  * @return If there are more executions to explore, return true. Otherwise,
156  * return false.
157  */
158 bool ModelChecker::next_execution()
159 {
160         DBG();
161
162         num_executions++;
163
164         if (isfinalfeasible() || DBG_ENABLED())
165                 print_summary();
166
167         if ((diverge = get_next_backtrack()) == NULL)
168                 return false;
169
170         if (DBG_ENABLED()) {
171                 printf("Next execution will diverge at:\n");
172                 diverge->print();
173         }
174
175         reset_to_initial_state();
176         return true;
177 }
178
179 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
180 {
181         action_type type = act->get_type();
182
183         switch (type) {
184                 case ATOMIC_READ:
185                 case ATOMIC_WRITE:
186                 case ATOMIC_RMW:
187                         break;
188                 default:
189                         return NULL;
190         }
191         /* linear search: from most recent to oldest */
192         action_list_t *list = obj_map->get_safe_ptr(act->get_location());
193         action_list_t::reverse_iterator rit;
194         for (rit = list->rbegin(); rit != list->rend(); rit++) {
195                 ModelAction *prev = *rit;
196                 if (act->is_synchronizing(prev))
197                         return prev;
198         }
199         return NULL;
200 }
201
202 void ModelChecker::set_backtracking(ModelAction *act)
203 {
204         ModelAction *prev;
205         Node *node;
206         Thread *t = get_thread(act->get_tid());
207
208         prev = get_last_conflict(act);
209         if (prev == NULL)
210                 return;
211
212         node = prev->get_node()->get_parent();
213
214         while (!node->is_enabled(t))
215                 t = t->get_parent();
216
217         /* Check if this has been explored already */
218         if (node->has_been_explored(t->get_id()))
219                 return;
220
221         /* Cache the latest backtracking point */
222         if (!priv->next_backtrack || *prev > *priv->next_backtrack)
223                 priv->next_backtrack = prev;
224
225         /* If this is a new backtracking point, mark the tree */
226         if (!node->set_backtrack(t->get_id()))
227                 return;
228         DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
229                         prev->get_tid(), t->get_id());
230         if (DBG_ENABLED()) {
231                 prev->print();
232                 act->print();
233         }
234 }
235
236 /**
237  * Returns last backtracking point. The model checker will explore a different
238  * path for this point in the next execution.
239  * @return The ModelAction at which the next execution should diverge.
240  */
241 ModelAction * ModelChecker::get_next_backtrack()
242 {
243         ModelAction *next = priv->next_backtrack;
244         priv->next_backtrack = NULL;
245         return next;
246 }
247
248 /**
249  * This is the heart of the model checker routine. It performs model-checking
250  * actions corresponding to a given "current action." Among other processes, it
251  * calculates reads-from relationships, updates synchronization clock vectors,
252  * forms a memory_order constraints graph, and handles replay/backtrack
253  * execution when running permutations of previously-observed executions.
254  *
255  * @param curr The current action to process
256  * @return The next Thread that must be executed. May be NULL if ModelChecker
257  * makes no choice (e.g., according to replay execution, combining RMW actions,
258  * etc.)
259  */
260 Thread * ModelChecker::check_current_action(ModelAction *curr)
261 {
262         bool second_part_of_rmw = false;
263
264         ASSERT(curr);
265
266         if (curr->is_rmwc() || curr->is_rmw()) {
267                 ModelAction *tmp = process_rmw(curr);
268                 second_part_of_rmw = true;
269                 delete curr;
270                 curr = tmp;
271         } else {
272                 ModelAction *tmp = node_stack->explore_action(curr);
273                 if (tmp) {
274                         /* Discard duplicate ModelAction; use action from NodeStack */
275                         /* First restore type and order in case of RMW operation */
276                         if (curr->is_rmwr())
277                                 tmp->copy_typeandorder(curr);
278
279                         /* If we have diverged, we need to reset the clock vector. */
280                         if (diverge == NULL)
281                                 tmp->create_cv(get_parent_action(tmp->get_tid()));
282
283                         delete curr;
284                         curr = tmp;
285                 } else {
286                         /*
287                          * Perform one-time actions when pushing new ModelAction onto
288                          * NodeStack
289                          */
290                         curr->create_cv(get_parent_action(curr->get_tid()));
291                         /* Build may_read_from set */
292                         if (curr->is_read())
293                                 build_reads_from_past(curr);
294                         if (curr->is_write())
295                                 compute_promises(curr);
296                 }
297         }
298
299         /* Assign 'creation' parent */
300         if (curr->get_type() == THREAD_CREATE) {
301                 Thread *th = (Thread *)curr->get_location();
302                 th->set_creation(curr);
303         } else if (curr->get_type() == THREAD_JOIN) {
304                 Thread *wait, *join;
305                 wait = get_thread(curr->get_tid());
306                 join = (Thread *)curr->get_location();
307                 if (!join->is_complete())
308                         scheduler->wait(wait, join);
309         } else if (curr->get_type() == THREAD_FINISH) {
310                 Thread *th = get_thread(curr->get_tid());
311                 while (!th->wait_list_empty()) {
312                         Thread *wake = th->pop_wait_list();
313                         scheduler->wake(wake);
314                 }
315                 th->complete();
316         }
317
318         /* Deal with new thread */
319         if (curr->get_type() == THREAD_START)
320                 check_promises(NULL, curr->get_cv());
321
322         /* Assign reads_from values */
323         Thread *th = get_thread(curr->get_tid());
324         uint64_t value = VALUE_NONE;
325         bool updated = false;
326         if (curr->is_read()) {
327                 while(true) {
328                         const ModelAction *reads_from = curr->get_node()->get_read_from();
329                         if (reads_from != NULL) {
330                                 value = reads_from->get_value();
331                                 /* Assign reads_from, perform release/acquire synchronization */
332                                 curr->read_from(reads_from);
333                                 if (!second_part_of_rmw)
334                                         check_recency(curr,false);
335
336                                 bool r_status=r_modification_order(curr,reads_from);
337
338                                 if (!second_part_of_rmw&&!isfeasible()&&(curr->get_node()->increment_read_from()||!curr->get_node()->future_value_empty())) {
339                                         mo_graph->rollbackChanges();
340                                         too_many_reads=false;
341                                         continue;
342                                 }
343
344                                 mo_graph->commitChanges();
345                                 updated |= r_status;
346                         } else {
347                                 /* Read from future value */
348                                 value = curr->get_node()->get_future_value();
349                                 curr->read_from(NULL);
350                                 Promise *valuepromise = new Promise(curr, value);
351                                 promises->push_back(valuepromise);
352                         }
353                         break;
354                 }
355         } else if (curr->is_write()) {
356                 if (w_modification_order(curr))
357                         updated = true;
358                 if (resolve_promises(curr))
359                         updated = true;
360                 mo_graph->commitChanges();
361         }
362
363         if (updated)
364                 resolve_release_sequences(curr->get_location());
365
366         th->set_return_value(value);
367
368         /* Add action to list.  */
369         if (!second_part_of_rmw)
370                 add_action_to_lists(curr);
371
372         Node *currnode = curr->get_node();
373         Node *parnode = currnode->get_parent();
374
375         if (!parnode->backtrack_empty() || !currnode->read_from_empty() ||
376                   !currnode->future_value_empty() || !currnode->promise_empty())
377                 if (!priv->next_backtrack || *curr > *priv->next_backtrack)
378                         priv->next_backtrack = curr;
379
380         set_backtracking(curr);
381
382         /* Do not split atomic actions. */
383         if (curr->is_rmwr())
384                 return thread_current();
385         /* The THREAD_CREATE action points to the created Thread */
386         else if (curr->get_type() == THREAD_CREATE)
387                 return (Thread *)curr->get_location();
388         else
389                 return get_next_replay_thread();
390 }
391
392 /** @returns whether the current partial trace must be a prefix of a
393  * feasible trace. */
394 bool ModelChecker::isfeasibleprefix() {
395         return promises->size() == 0 && *lazy_sync_size == 0;
396 }
397
398 /** @returns whether the current partial trace is feasible. */
399 bool ModelChecker::isfeasible() {
400         return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads;
401 }
402
403 /** Returns whether the current completed trace is feasible. */
404 bool ModelChecker::isfinalfeasible() {
405         return isfeasible() && promises->size() == 0;
406 }
407
408 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
409 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
410         int tid = id_to_int(act->get_tid());
411         ModelAction *lastread = get_last_action(tid);
412         lastread->process_rmw(act);
413         if (act->is_rmw())
414                 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
415         return lastread;
416 }
417
418 /**
419  * Checks whether a thread has read from the same write for too many times
420  * without seeing the effects of a later write.
421  *
422  * Basic idea:
423  * 1) there must a different write that we could read from that would satisfy the modification order,
424  * 2) we must have read from the same value in excess of maxreads times, and
425  * 3) that other write must have been in the reads_from set for maxreads times.
426  *
427  * If so, we decide that the execution is no longer feasible.
428  */
429 void ModelChecker::check_recency(ModelAction *curr, bool already_added) {
430         if (params.maxreads != 0) {
431                 if (curr->get_node()->get_read_from_size() <= 1)
432                         return;
433
434                 //Must make sure that execution is currently feasible...  We could
435                 //accidentally clear by rolling back
436                 if (!isfeasible())
437                         return;
438
439                 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
440                 int tid = id_to_int(curr->get_tid());
441
442                 /* Skip checks */
443                 if ((int)thrd_lists->size() <= tid)
444                         return;
445
446                 action_list_t *list = &(*thrd_lists)[tid];
447
448                 action_list_t::reverse_iterator rit = list->rbegin();
449                 /* Skip past curr */
450                 if (!already_added) {
451                         for (; (*rit) != curr; rit++)
452                                 ;
453                         /* go past curr now */
454                         rit++;
455                 }
456
457                 action_list_t::reverse_iterator ritcopy=rit;
458                 //See if we have enough reads from the same value
459                 int count=0;
460                 for (; count < params.maxreads; rit++,count++) {
461                         if (rit==list->rend())
462                                 return;
463                         ModelAction *act = *rit;
464                         if (!act->is_read())
465                                 return;
466                         if (act->get_reads_from() != curr->get_reads_from())
467                                 return;
468                         if (act->get_node()->get_read_from_size() <= 1)
469                                 return;
470                 }
471
472                 for (int i=0;i<curr->get_node()->get_read_from_size();i++) {
473                         //Get write
474                         const ModelAction * write=curr->get_node()->get_read_from_at(i);
475                         //Need a different write
476                         if (write==curr->get_reads_from())
477                                 continue;
478
479                         /* Test to see whether this is a feasible write to read from*/
480                         r_modification_order(curr, write);
481                         bool feasiblereadfrom=isfeasible();
482                         mo_graph->rollbackChanges();
483
484                         if (!feasiblereadfrom)
485                                 continue;
486                         rit=ritcopy;
487
488                         bool feasiblewrite=true;
489                         //new we need to see if this write works for everyone
490
491                         for (int loop=count;loop>0;loop--,rit++) {
492                                 ModelAction *act=*rit;
493                                 bool foundvalue=false;
494                                 for(int j=0;j<act->get_node()->get_read_from_size();j++) {
495                                         if (act->get_node()->get_read_from_at(i)==write) {
496                                                 foundvalue=true;
497                                                 break;
498                                         }
499                                 }
500                                 if (!foundvalue) {
501                                         feasiblewrite=false;
502                                         break;
503                                 }
504                         }
505                         if (feasiblewrite) {
506                                 too_many_reads = true;
507                                 return;
508                         }
509                 }
510         }
511 }
512
513 /**
514  * Updates the mo_graph with the constraints imposed from the current read.
515  * @param curr The current action. Must be a read.
516  * @param rf The action that curr reads from. Must be a write.
517  * @return True if modification order edges were added; false otherwise
518  */
519 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
520 {
521         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
522         unsigned int i;
523         bool added = false;
524         ASSERT(curr->is_read());
525
526         /* Iterate over all threads */
527         for (i = 0; i < thrd_lists->size(); i++) {
528                 /* Iterate over actions in thread, starting from most recent */
529                 action_list_t *list = &(*thrd_lists)[i];
530                 action_list_t::reverse_iterator rit;
531                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
532                         ModelAction *act = *rit;
533
534                         /* Include at most one act per-thread that "happens before" curr */
535                         if (act->happens_before(curr)) {
536                                 if (act->is_read()) {
537                                         const ModelAction *prevreadfrom = act->get_reads_from();
538                                         if (prevreadfrom != NULL && rf != prevreadfrom) {
539                                                 mo_graph->addEdge(prevreadfrom, rf);
540                                                 added = true;
541                                         }
542                                 } else if (rf != act) {
543                                         mo_graph->addEdge(act, rf);
544                                         added = true;
545                                 }
546                                 break;
547                         }
548                 }
549         }
550
551         return added;
552 }
553
554 /** Updates the mo_graph with the constraints imposed from the current read. */
555 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
556 {
557         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
558         unsigned int i;
559         ASSERT(curr->is_read());
560
561         /* Iterate over all threads */
562         for (i = 0; i < thrd_lists->size(); i++) {
563                 /* Iterate over actions in thread, starting from most recent */
564                 action_list_t *list = &(*thrd_lists)[i];
565                 action_list_t::reverse_iterator rit;
566                 ModelAction *lastact = NULL;
567
568                 /* Find last action that happens after curr */
569                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
570                         ModelAction *act = *rit;
571                         if (curr->happens_before(act)) {
572                                 lastact = act;
573                         } else
574                                 break;
575                 }
576
577                         /* Include at most one act per-thread that "happens before" curr */
578                 if (lastact != NULL) {
579                         if (lastact->is_read()) {
580                                 const ModelAction *postreadfrom = lastact->get_reads_from();
581                                 if (postreadfrom != NULL&&rf != postreadfrom)
582                                         mo_graph->addEdge(rf, postreadfrom);
583                         } else if (rf != lastact) {
584                                 mo_graph->addEdge(rf, lastact);
585                         }
586                         break;
587                 }
588         }
589 }
590
591 /**
592  * Updates the mo_graph with the constraints imposed from the current write.
593  * @param curr The current action. Must be a write.
594  * @return True if modification order edges were added; false otherwise
595  */
596 bool ModelChecker::w_modification_order(ModelAction *curr)
597 {
598         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
599         unsigned int i;
600         bool added = false;
601         ASSERT(curr->is_write());
602
603         if (curr->is_seqcst()) {
604                 /* We have to at least see the last sequentially consistent write,
605                          so we are initialized. */
606                 ModelAction *last_seq_cst = get_last_seq_cst(curr->get_location());
607                 if (last_seq_cst != NULL) {
608                         mo_graph->addEdge(last_seq_cst, curr);
609                         added = true;
610                 }
611         }
612
613         /* Iterate over all threads */
614         for (i = 0; i < thrd_lists->size(); i++) {
615                 /* Iterate over actions in thread, starting from most recent */
616                 action_list_t *list = &(*thrd_lists)[i];
617                 action_list_t::reverse_iterator rit;
618                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
619                         ModelAction *act = *rit;
620
621                         /* Include at most one act per-thread that "happens before" curr */
622                         if (act->happens_before(curr)) {
623                                 /*
624                                  * Note: if act is RMW, just add edge:
625                                  *   act --mo--> curr
626                                  * The following edge should be handled elsewhere:
627                                  *   readfrom(act) --mo--> act
628                                  */
629                                 if (act->is_write())
630                                         mo_graph->addEdge(act, curr);
631                                 else if (act->is_read() && act->get_reads_from() != NULL)
632                                         mo_graph->addEdge(act->get_reads_from(), curr);
633                                 added = true;
634                                 break;
635                         } else if (act->is_read() && !act->is_synchronizing(curr) &&
636                                                      !act->same_thread(curr)) {
637                                 /* We have an action that:
638                                    (1) did not happen before us
639                                    (2) is a read and we are a write
640                                    (3) cannot synchronize with us
641                                    (4) is in a different thread
642                                    =>
643                                    that read could potentially read from our write.
644                                  */
645                                 if (act->get_node()->add_future_value(curr->get_value()) &&
646                                                 (!priv->next_backtrack || *act > *priv->next_backtrack))
647                                         priv->next_backtrack = act;
648                         }
649                 }
650         }
651
652         return added;
653 }
654
655 /**
656  * Finds the head(s) of the release sequence(s) containing a given ModelAction.
657  * The ModelAction under consideration is expected to be taking part in
658  * release/acquire synchronization as an object of the "reads from" relation.
659  * Note that this can only provide release sequence support for RMW chains
660  * which do not read from the future, as those actions cannot be traced until
661  * their "promise" is fulfilled. Similarly, we may not even establish the
662  * presence of a release sequence with certainty, as some modification order
663  * constraints may be decided further in the future. Thus, this function
664  * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
665  * and a boolean representing certainty.
666  *
667  * @todo Finish lazy updating, when promises are fulfilled in the future
668  * @param rf The action that might be part of a release sequence. Must be a
669  * write.
670  * @param release_heads A pass-by-reference style return parameter.  After
671  * execution of this function, release_heads will contain the heads of all the
672  * relevant release sequences, if any exists
673  * @return true, if the ModelChecker is certain that release_heads is complete;
674  * false otherwise
675  */
676 bool ModelChecker::release_seq_head(const ModelAction *rf,
677                 std::vector<const ModelAction *> *release_heads) const
678 {
679         ASSERT(rf->is_write());
680         if (!rf) {
681                 /* read from future: need to settle this later */
682                 return false; /* incomplete */
683         }
684         if (rf->is_release())
685                 release_heads->push_back(rf);
686         if (rf->is_rmw()) {
687                 /* We need a RMW action that is both an acquire and release to stop */
688                 /** @todo Need to be smarter here...  In the linux lock
689                  * example, this will run to the beginning of the program for
690                  * every acquire. */
691                 if (rf->is_acquire() && rf->is_release())
692                         return true; /* complete */
693                 return release_seq_head(rf->get_reads_from(), release_heads);
694         }
695         if (rf->is_release())
696                 return true; /* complete */
697
698         /* else relaxed write; check modification order for contiguous subsequence
699          * -> rf must be same thread as release */
700         int tid = id_to_int(rf->get_tid());
701         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
702         action_list_t *list = &(*thrd_lists)[tid];
703         action_list_t::const_reverse_iterator rit;
704
705         /* Find rf in the thread list */
706         rit = std::find(list->rbegin(), list->rend(), rf);
707         ASSERT(rit != list->rend());
708
709         /* Find the last write/release */
710         for (; rit != list->rend(); rit++)
711                 if ((*rit)->is_release())
712                         break;
713         if (rit == list->rend()) {
714                 /* No write-release in this thread */
715                 return true; /* complete */
716         }
717         ModelAction *release = *rit;
718
719         ASSERT(rf->same_thread(release));
720
721         bool certain = true;
722         for (unsigned int i = 0; i < thrd_lists->size(); i++) {
723                 if (id_to_int(rf->get_tid()) == (int)i)
724                         continue;
725                 list = &(*thrd_lists)[i];
726
727                 /* Can we ensure no future writes from this thread may break
728                  * the release seq? */
729                 bool future_ordered = false;
730
731                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
732                         const ModelAction *act = *rit;
733                         if (!act->is_write())
734                                 continue;
735                         /* Reach synchronization -> this thread is complete */
736                         if (act->happens_before(release))
737                                 break;
738                         if (rf->happens_before(act)) {
739                                 future_ordered = true;
740                                 continue;
741                         }
742
743                         /* Check modification order */
744                         if (mo_graph->checkReachable(rf, act)) {
745                                 /* rf --mo--> act */
746                                 future_ordered = true;
747                                 continue;
748                         }
749                         if (mo_graph->checkReachable(act, release))
750                                 /* act --mo--> release */
751                                 break;
752                         if (mo_graph->checkReachable(release, act) &&
753                                       mo_graph->checkReachable(act, rf)) {
754                                 /* release --mo-> act --mo--> rf */
755                                 return true; /* complete */
756                         }
757                         certain = false;
758                 }
759                 if (!future_ordered)
760                         return false; /* This thread is uncertain */
761         }
762
763         if (certain)
764                 release_heads->push_back(release);
765         return certain;
766 }
767
768 /**
769  * A public interface for getting the release sequence head(s) with which a
770  * given ModelAction must synchronize. This function only returns a non-empty
771  * result when it can locate a release sequence head with certainty. Otherwise,
772  * it may mark the internal state of the ModelChecker so that it will handle
773  * the release sequence at a later time, causing @a act to update its
774  * synchronization at some later point in execution.
775  * @param act The 'acquire' action that may read from a release sequence
776  * @param release_heads A pass-by-reference return parameter. Will be filled
777  * with the head(s) of the release sequence(s), if they exists with certainty.
778  * @see ModelChecker::release_seq_head
779  */
780 void ModelChecker::get_release_seq_heads(ModelAction *act,
781                 std::vector<const ModelAction *> *release_heads)
782 {
783         const ModelAction *rf = act->get_reads_from();
784         bool complete;
785         complete = release_seq_head(rf, release_heads);
786         if (!complete) {
787                 /* add act to 'lazy checking' list */
788                 std::list<ModelAction *> *list;
789                 list = lazy_sync_with_release->get_safe_ptr(act->get_location());
790                 list->push_back(act);
791                 (*lazy_sync_size)++;
792         }
793 }
794
795 /**
796  * Attempt to resolve all stashed operations that might synchronize with a
797  * release sequence for a given location. This implements the "lazy" portion of
798  * determining whether or not a release sequence was contiguous, since not all
799  * modification order information is present at the time an action occurs.
800  *
801  * @param location The location/object that should be checked for release
802  * sequence resolutions
803  * @return True if any updates occurred (new synchronization, new mo_graph edges)
804  */
805 bool ModelChecker::resolve_release_sequences(void *location)
806 {
807         std::list<ModelAction *> *list;
808         list = lazy_sync_with_release->getptr(location);
809         if (!list)
810                 return false;
811
812         bool updated = false;
813         std::list<ModelAction *>::iterator it = list->begin();
814         while (it != list->end()) {
815                 ModelAction *act = *it;
816                 const ModelAction *rf = act->get_reads_from();
817                 std::vector<const ModelAction *> release_heads;
818                 bool complete;
819                 complete = release_seq_head(rf, &release_heads);
820                 for (unsigned int i = 0; i < release_heads.size(); i++) {
821                         if (!act->has_synchronized_with(release_heads[i])) {
822                                 updated = true;
823                                 act->synchronize_with(release_heads[i]);
824                         }
825                 }
826
827                 if (updated) {
828                         /* propagate synchronization to later actions */
829                         action_list_t::reverse_iterator it = action_trace->rbegin();
830                         while ((*it) != act) {
831                                 ModelAction *propagate = *it;
832                                 if (act->happens_before(propagate))
833                                         /** @todo new mo_graph edges along with
834                                          * this synchronization? */
835                                         propagate->synchronize_with(act);
836                         }
837                 }
838                 if (complete) {
839                         it = list->erase(it);
840                         (*lazy_sync_size)--;
841                 } else
842                         it++;
843         }
844
845         // If we resolved promises or data races, see if we have realized a data race.
846         if (checkDataRaces()) {
847                 set_assert();
848         }
849
850         return updated;
851 }
852
853 /**
854  * Performs various bookkeeping operations for the current ModelAction. For
855  * instance, adds action to the per-object, per-thread action vector and to the
856  * action trace list of all thread actions.
857  *
858  * @param act is the ModelAction to add.
859  */
860 void ModelChecker::add_action_to_lists(ModelAction *act)
861 {
862         int tid = id_to_int(act->get_tid());
863         action_trace->push_back(act);
864
865         obj_map->get_safe_ptr(act->get_location())->push_back(act);
866
867         std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
868         if (tid >= (int)vec->size())
869                 vec->resize(priv->next_thread_id);
870         (*vec)[tid].push_back(act);
871
872         if ((int)thrd_last_action->size() <= tid)
873                 thrd_last_action->resize(get_num_threads());
874         (*thrd_last_action)[tid] = act;
875 }
876
877 ModelAction * ModelChecker::get_last_action(thread_id_t tid)
878 {
879         int nthreads = get_num_threads();
880         if ((int)thrd_last_action->size() < nthreads)
881                 thrd_last_action->resize(nthreads);
882         return (*thrd_last_action)[id_to_int(tid)];
883 }
884
885 /**
886  * Gets the last memory_order_seq_cst action (in the total global sequence)
887  * performed on a particular object (i.e., memory location).
888  * @param location The object location to check
889  * @return The last seq_cst action performed
890  */
891 ModelAction * ModelChecker::get_last_seq_cst(const void *location)
892 {
893         action_list_t *list = obj_map->get_safe_ptr(location);
894         /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
895         action_list_t::reverse_iterator rit;
896         for (rit = list->rbegin(); rit != list->rend(); rit++)
897                 if ((*rit)->is_write() && (*rit)->is_seqcst())
898                         return *rit;
899         return NULL;
900 }
901
902 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
903 {
904         ModelAction *parent = get_last_action(tid);
905         if (!parent)
906                 parent = get_thread(tid)->get_creation();
907         return parent;
908 }
909
910 /**
911  * Returns the clock vector for a given thread.
912  * @param tid The thread whose clock vector we want
913  * @return Desired clock vector
914  */
915 ClockVector * ModelChecker::get_cv(thread_id_t tid)
916 {
917         return get_parent_action(tid)->get_cv();
918 }
919
920 /**
921  * Resolve a set of Promises with a current write. The set is provided in the
922  * Node corresponding to @a write.
923  * @param write The ModelAction that is fulfilling Promises
924  * @return True if promises were resolved; false otherwise
925  */
926 bool ModelChecker::resolve_promises(ModelAction *write)
927 {
928         bool resolved = false;
929         for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
930                 Promise *promise = (*promises)[promise_index];
931                 if (write->get_node()->get_promise(i)) {
932                         ModelAction *read = promise->get_action();
933                         read->read_from(write);
934                         r_modification_order(read, write);
935                         post_r_modification_order(read, write);
936                         promises->erase(promises->begin() + promise_index);
937                         resolved = true;
938                 } else
939                         promise_index++;
940         }
941
942         return resolved;
943 }
944
945 /**
946  * Compute the set of promises that could potentially be satisfied by this
947  * action. Note that the set computation actually appears in the Node, not in
948  * ModelChecker.
949  * @param curr The ModelAction that may satisfy promises
950  */
951 void ModelChecker::compute_promises(ModelAction *curr)
952 {
953         for (unsigned int i = 0; i < promises->size(); i++) {
954                 Promise *promise = (*promises)[i];
955                 const ModelAction *act = promise->get_action();
956                 if (!act->happens_before(curr) &&
957                                 act->is_read() &&
958                                 !act->is_synchronizing(curr) &&
959                                 !act->same_thread(curr) &&
960                                 promise->get_value() == curr->get_value()) {
961                         curr->get_node()->set_promise(i);
962                 }
963         }
964 }
965
966 /** Checks promises in response to change in ClockVector Threads. */
967 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
968 {
969         for (unsigned int i = 0; i < promises->size(); i++) {
970                 Promise *promise = (*promises)[i];
971                 const ModelAction *act = promise->get_action();
972                 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
973                                 merge_cv->synchronized_since(act)) {
974                         //This thread is no longer able to send values back to satisfy the promise
975                         int num_synchronized_threads = promise->increment_threads();
976                         if (num_synchronized_threads == get_num_threads()) {
977                                 //Promise has failed
978                                 failed_promise = true;
979                                 return;
980                         }
981                 }
982         }
983 }
984
985 /**
986  * Build up an initial set of all past writes that this 'read' action may read
987  * from. This set is determined by the clock vector's "happens before"
988  * relationship.
989  * @param curr is the current ModelAction that we are exploring; it must be a
990  * 'read' operation.
991  */
992 void ModelChecker::build_reads_from_past(ModelAction *curr)
993 {
994         std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
995         unsigned int i;
996         ASSERT(curr->is_read());
997
998         ModelAction *last_seq_cst = NULL;
999
1000         /* Track whether this object has been initialized */
1001         bool initialized = false;
1002
1003         if (curr->is_seqcst()) {
1004                 last_seq_cst = get_last_seq_cst(curr->get_location());
1005                 /* We have to at least see the last sequentially consistent write,
1006                          so we are initialized. */
1007                 if (last_seq_cst != NULL)
1008                         initialized = true;
1009         }
1010
1011         /* Iterate over all threads */
1012         for (i = 0; i < thrd_lists->size(); i++) {
1013                 /* Iterate over actions in thread, starting from most recent */
1014                 action_list_t *list = &(*thrd_lists)[i];
1015                 action_list_t::reverse_iterator rit;
1016                 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1017                         ModelAction *act = *rit;
1018
1019                         /* Only consider 'write' actions */
1020                         if (!act->is_write())
1021                                 continue;
1022
1023                         /* Don't consider more than one seq_cst write if we are a seq_cst read. */
1024                         if (!act->is_seqcst() || !curr->is_seqcst() || act == last_seq_cst) {
1025                                 DEBUG("Adding action to may_read_from:\n");
1026                                 if (DBG_ENABLED()) {
1027                                         act->print();
1028                                         curr->print();
1029                                 }
1030                                 curr->get_node()->add_read_from(act);
1031                         }
1032
1033                         /* Include at most one act per-thread that "happens before" curr */
1034                         if (act->happens_before(curr)) {
1035                                 initialized = true;
1036                                 break;
1037                         }
1038                 }
1039         }
1040
1041         if (!initialized) {
1042                 /** @todo Need a more informative way of reporting errors. */
1043                 printf("ERROR: may read from uninitialized atomic\n");
1044         }
1045
1046         if (DBG_ENABLED() || !initialized) {
1047                 printf("Reached read action:\n");
1048                 curr->print();
1049                 printf("Printing may_read_from\n");
1050                 curr->get_node()->print_may_read_from();
1051                 printf("End printing may_read_from\n");
1052         }
1053
1054         ASSERT(initialized);
1055 }
1056
1057 static void print_list(action_list_t *list)
1058 {
1059         action_list_t::iterator it;
1060
1061         printf("---------------------------------------------------------------------\n");
1062         printf("Trace:\n");
1063
1064         for (it = list->begin(); it != list->end(); it++) {
1065                 (*it)->print();
1066         }
1067         printf("---------------------------------------------------------------------\n");
1068 }
1069
1070 void ModelChecker::print_summary()
1071 {
1072         printf("\n");
1073         printf("Number of executions: %d\n", num_executions);
1074         printf("Total nodes created: %d\n", node_stack->get_total_nodes());
1075
1076         scheduler->print();
1077
1078         if (!isfinalfeasible())
1079                 printf("INFEASIBLE EXECUTION!\n");
1080         print_list(action_trace);
1081         printf("\n");
1082 }
1083
1084 /**
1085  * Add a Thread to the system for the first time. Should only be called once
1086  * per thread.
1087  * @param t The Thread to add
1088  */
1089 void ModelChecker::add_thread(Thread *t)
1090 {
1091         thread_map->put(id_to_int(t->get_id()), t);
1092         scheduler->add_thread(t);
1093 }
1094
1095 void ModelChecker::remove_thread(Thread *t)
1096 {
1097         scheduler->remove_thread(t);
1098 }
1099
1100 /**
1101  * Switch from a user-context to the "master thread" context (a.k.a. system
1102  * context). This switch is made with the intention of exploring a particular
1103  * model-checking action (described by a ModelAction object). Must be called
1104  * from a user-thread context.
1105  * @param act The current action that will be explored. Must not be NULL.
1106  * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
1107  */
1108 int ModelChecker::switch_to_master(ModelAction *act)
1109 {
1110         DBG();
1111         Thread *old = thread_current();
1112         set_current_action(act);
1113         old->set_state(THREAD_READY);
1114         return Thread::swap(old, &system_context);
1115 }
1116
1117 /**
1118  * Takes the next step in the execution, if possible.
1119  * @return Returns true (success) if a step was taken and false otherwise.
1120  */
1121 bool ModelChecker::take_step() {
1122         Thread *curr, *next;
1123
1124         if (has_asserted())
1125                 return false;
1126
1127         curr = thread_current();
1128         if (curr) {
1129                 if (curr->get_state() == THREAD_READY) {
1130                         ASSERT(priv->current_action);
1131                         priv->nextThread = check_current_action(priv->current_action);
1132                         priv->current_action = NULL;
1133                         if (!curr->is_blocked() && !curr->is_complete())
1134                                 scheduler->add_thread(curr);
1135                 } else {
1136                         ASSERT(false);
1137                 }
1138         }
1139         next = scheduler->next_thread(priv->nextThread);
1140
1141         /* Infeasible -> don't take any more steps */
1142         if (!isfeasible())
1143                 return false;
1144
1145         if (next)
1146                 next->set_state(THREAD_RUNNING);
1147         DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1148
1149         /* next == NULL -> don't take any more steps */
1150         if (!next)
1151                 return false;
1152         /* Return false only if swap fails with an error */
1153         return (Thread::swap(&system_context, next) == 0);
1154 }
1155
1156 /** Runs the current execution until threre are no more steps to take. */
1157 void ModelChecker::finish_execution() {
1158         DBG();
1159
1160         while (take_step());
1161 }