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