8 #include "snapshot-interface.h"
10 #include "clockvector.h"
11 #include "cyclegraph.h"
16 #define INITIAL_THREAD_ID 0
20 /** @brief Constructor */
21 ModelChecker::ModelChecker(struct model_params params) :
22 /* Initialize default scheduler */
24 scheduler(new Scheduler()),
26 num_feasible_executions(0),
28 action_trace(new action_list_t()),
29 thread_map(new HashTable<int, Thread *, int>()),
30 obj_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
31 lock_waiters_map(new HashTable<const void *, action_list_t, uintptr_t, 4>()),
32 obj_thrd_map(new HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 >()),
33 promises(new std::vector<Promise *>()),
34 futurevalues(new std::vector<struct PendingFutureValue>()),
35 pending_acq_rel_seq(new std::vector<ModelAction *>()),
36 thrd_last_action(new std::vector<ModelAction *>(1)),
37 node_stack(new NodeStack()),
38 mo_graph(new CycleGraph()),
39 failed_promise(false),
40 too_many_reads(false),
42 bad_synchronization(false)
44 /* Allocate this "size" on the snapshotting heap */
45 priv = (struct model_snapshot_members *)calloc(1, sizeof(*priv));
46 /* First thread created will have id INITIAL_THREAD_ID */
47 priv->next_thread_id = INITIAL_THREAD_ID;
50 /** @brief Destructor */
51 ModelChecker::~ModelChecker()
53 for (int i = 0; i < get_num_threads(); i++)
54 delete thread_map->get(i);
59 delete lock_waiters_map;
62 for (unsigned int i = 0; i < promises->size(); i++)
63 delete (*promises)[i];
66 delete pending_acq_rel_seq;
68 delete thrd_last_action;
75 * Restores user program to initial state and resets all model-checker data
78 void ModelChecker::reset_to_initial_state()
80 DEBUG("+++ Resetting to initial state +++\n");
81 node_stack->reset_execution();
82 failed_promise = false;
83 too_many_reads = false;
84 bad_synchronization = false;
86 snapshotObject->backTrackBeforeStep(0);
89 /** @return a thread ID for a new Thread */
90 thread_id_t ModelChecker::get_next_id()
92 return priv->next_thread_id++;
95 /** @return the number of user threads created during this execution */
96 int ModelChecker::get_num_threads()
98 return priv->next_thread_id;
101 /** @return a sequence number for a new ModelAction */
102 modelclock_t ModelChecker::get_next_seq_num()
104 return ++priv->used_sequence_numbers;
108 * @brief Choose the next thread to execute.
110 * This function chooses the next thread that should execute. It can force the
111 * adjacency of read/write portions of a RMW action, force THREAD_CREATE to be
112 * followed by a THREAD_START, or it can enforce execution replay/backtracking.
113 * The model-checker may have no preference regarding the next thread (i.e.,
114 * when exploring a new execution ordering), in which case this will return
116 * @param curr The current ModelAction. This action might guide the choice of
118 * @return The next thread to run. If the model-checker has no preference, NULL.
120 Thread * ModelChecker::get_next_thread(ModelAction *curr)
125 /* Do not split atomic actions. */
127 return thread_current();
128 /* The THREAD_CREATE action points to the created Thread */
129 else if (curr->get_type() == THREAD_CREATE)
130 return (Thread *)curr->get_location();
133 /* Have we completed exploring the preselected path? */
137 /* Else, we are trying to replay an execution */
138 ModelAction *next = node_stack->get_next()->get_action();
140 if (next == diverge) {
141 Node *nextnode = next->get_node();
142 /* Reached divergence point */
143 if (nextnode->increment_promise()) {
144 /* The next node will try to satisfy a different set of promises. */
145 tid = next->get_tid();
146 node_stack->pop_restofstack(2);
147 } else if (nextnode->increment_read_from()) {
148 /* The next node will read from a different value. */
149 tid = next->get_tid();
150 node_stack->pop_restofstack(2);
151 } else if (nextnode->increment_future_value()) {
152 /* The next node will try to read from a different future value. */
153 tid = next->get_tid();
154 node_stack->pop_restofstack(2);
156 /* Make a different thread execute for next step */
157 Node *node = nextnode->get_parent();
158 tid = node->get_next_backtrack();
159 node_stack->pop_restofstack(1);
161 DEBUG("*** Divergence point ***\n");
164 tid = next->get_tid();
166 DEBUG("*** ModelChecker chose next thread = %d ***\n", tid);
167 ASSERT(tid != THREAD_ID_T_NONE);
168 return thread_map->get(id_to_int(tid));
172 * Queries the model-checker for more executions to explore and, if one
173 * exists, resets the model-checker state to execute a new execution.
175 * @return If there are more executions to explore, return true. Otherwise,
178 bool ModelChecker::next_execution()
183 if (isfinalfeasible())
184 num_feasible_executions++;
186 if (isfinalfeasible() || DBG_ENABLED())
189 if ((diverge = get_next_backtrack()) == NULL)
193 printf("Next execution will diverge at:\n");
197 reset_to_initial_state();
201 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
203 switch (act->get_type()) {
207 /* linear search: from most recent to oldest */
208 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
209 action_list_t::reverse_iterator rit;
210 for (rit = list->rbegin(); rit != list->rend(); rit++) {
211 ModelAction *prev = *rit;
212 if (act->is_synchronizing(prev))
218 case ATOMIC_TRYLOCK: {
219 /* linear search: from most recent to oldest */
220 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
221 action_list_t::reverse_iterator rit;
222 for (rit = list->rbegin(); rit != list->rend(); rit++) {
223 ModelAction *prev = *rit;
224 if (act->is_conflicting_lock(prev))
229 case ATOMIC_UNLOCK: {
230 /* linear search: from most recent to oldest */
231 action_list_t *list = obj_map->get_safe_ptr(act->get_location());
232 action_list_t::reverse_iterator rit;
233 for (rit = list->rbegin(); rit != list->rend(); rit++) {
234 ModelAction *prev = *rit;
235 if (!act->same_thread(prev)&&prev->is_failed_trylock())
246 /** This method find backtracking points where we should try to
247 * reorder the parameter ModelAction against.
249 * @param the ModelAction to find backtracking points for.
251 void ModelChecker::set_backtracking(ModelAction *act)
253 Thread *t = get_thread(act);
254 ModelAction * prev = get_last_conflict(act);
258 Node * node = prev->get_node()->get_parent();
260 int low_tid, high_tid;
261 if (node->is_enabled(t)) {
262 low_tid = id_to_int(act->get_tid());
263 high_tid = low_tid+1;
266 high_tid = get_num_threads();
269 for(int i = low_tid; i < high_tid; i++) {
270 thread_id_t tid = int_to_id(i);
271 if (!node->is_enabled(tid))
274 /* Check if this has been explored already */
275 if (node->has_been_explored(tid))
278 /* See if fairness allows */
279 if (model->params.fairwindow != 0 && !node->has_priority(tid)) {
281 for(int t=0;t<node->get_num_threads();t++) {
282 thread_id_t tother=int_to_id(t);
283 if (node->is_enabled(tother) && node->has_priority(tother)) {
292 /* Cache the latest backtracking point */
293 if (!priv->next_backtrack || *prev > *priv->next_backtrack)
294 priv->next_backtrack = prev;
296 /* If this is a new backtracking point, mark the tree */
297 if (!node->set_backtrack(tid))
299 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
300 prev->get_tid(), t->get_id());
309 * Returns last backtracking point. The model checker will explore a different
310 * path for this point in the next execution.
311 * @return The ModelAction at which the next execution should diverge.
313 ModelAction * ModelChecker::get_next_backtrack()
315 ModelAction *next = priv->next_backtrack;
316 priv->next_backtrack = NULL;
321 * Processes a read or rmw model action.
322 * @param curr is the read model action to process.
323 * @param second_part_of_rmw is boolean that is true is this is the second action of a rmw.
324 * @return True if processing this read updates the mo_graph.
326 bool ModelChecker::process_read(ModelAction *curr, bool second_part_of_rmw)
329 bool updated = false;
331 const ModelAction *reads_from = curr->get_node()->get_read_from();
332 if (reads_from != NULL) {
333 mo_graph->startChanges();
335 value = reads_from->get_value();
336 bool r_status = false;
338 if (!second_part_of_rmw) {
339 check_recency(curr, reads_from);
340 r_status = r_modification_order(curr, reads_from);
344 if (!second_part_of_rmw&&!isfeasible()&&(curr->get_node()->increment_read_from()||curr->get_node()->increment_future_value())) {
345 mo_graph->rollbackChanges();
346 too_many_reads = false;
350 curr->read_from(reads_from);
351 mo_graph->commitChanges();
353 } else if (!second_part_of_rmw) {
354 /* Read from future value */
355 value = curr->get_node()->get_future_value();
356 modelclock_t expiration = curr->get_node()->get_future_value_expiration();
357 curr->read_from(NULL);
358 Promise *valuepromise = new Promise(curr, value, expiration);
359 promises->push_back(valuepromise);
361 get_thread(curr)->set_return_value(value);
367 * Processes a lock, trylock, or unlock model action. @param curr is
368 * the read model action to process.
370 * The try lock operation checks whether the lock is taken. If not,
371 * it falls to the normal lock operation case. If so, it returns
374 * The lock operation has already been checked that it is enabled, so
375 * it just grabs the lock and synchronizes with the previous unlock.
377 * The unlock operation has to re-enable all of the threads that are
378 * waiting on the lock.
380 * @return True if synchronization was updated; false otherwise
382 bool ModelChecker::process_mutex(ModelAction *curr) {
383 std::mutex *mutex = (std::mutex *)curr->get_location();
384 struct std::mutex_state *state = mutex->get_state();
385 switch (curr->get_type()) {
386 case ATOMIC_TRYLOCK: {
387 bool success = !state->islocked;
388 curr->set_try_lock(success);
390 get_thread(curr)->set_return_value(0);
393 get_thread(curr)->set_return_value(1);
395 //otherwise fall into the lock case
397 if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock) {
398 printf("Lock access before initialization\n");
401 state->islocked = true;
402 ModelAction *unlock = get_last_unlock(curr);
403 //synchronize with the previous unlock statement
404 if (unlock != NULL) {
405 curr->synchronize_with(unlock);
410 case ATOMIC_UNLOCK: {
412 state->islocked = false;
413 //wake up the other threads
414 action_list_t *waiters = lock_waiters_map->get_safe_ptr(curr->get_location());
415 //activate all the waiting threads
416 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
417 scheduler->add_thread(get_thread((*rit)->get_tid()));
429 * Process a write ModelAction
430 * @param curr The ModelAction to process
431 * @return True if the mo_graph was updated or promises were resolved
433 bool ModelChecker::process_write(ModelAction *curr)
435 bool updated_mod_order = w_modification_order(curr);
436 bool updated_promises = resolve_promises(curr);
438 if (promises->size() == 0) {
439 for (unsigned int i = 0; i < futurevalues->size(); i++) {
440 struct PendingFutureValue pfv = (*futurevalues)[i];
441 if (pfv.act->get_node()->add_future_value(pfv.value, pfv.expiration) &&
442 (!priv->next_backtrack || *pfv.act > *priv->next_backtrack))
443 priv->next_backtrack = pfv.act;
445 futurevalues->resize(0);
448 mo_graph->commitChanges();
449 get_thread(curr)->set_return_value(VALUE_NONE);
450 return updated_mod_order || updated_promises;
454 * @brief Process the current action for thread-related activity
456 * Performs current-action processing for a THREAD_* ModelAction. Proccesses
457 * may include setting Thread status, completing THREAD_FINISH/THREAD_JOIN
458 * synchronization, etc. This function is a no-op for non-THREAD actions
459 * (e.g., ATOMIC_{READ,WRITE,RMW,LOCK}, etc.)
461 * @param curr The current action
462 * @return True if synchronization was updated
464 bool ModelChecker::process_thread_action(ModelAction *curr)
466 bool synchronized = false;
468 switch (curr->get_type()) {
469 case THREAD_CREATE: {
470 Thread *th = (Thread *)curr->get_location();
471 th->set_creation(curr);
475 Thread *waiting, *blocking;
476 waiting = get_thread(curr);
477 blocking = (Thread *)curr->get_location();
478 if (!blocking->is_complete()) {
479 blocking->push_wait_list(curr);
480 scheduler->sleep(waiting);
482 do_complete_join(curr);
487 case THREAD_FINISH: {
488 Thread *th = get_thread(curr);
489 while (!th->wait_list_empty()) {
490 ModelAction *act = th->pop_wait_list();
491 Thread *wake = get_thread(act);
492 scheduler->wake(wake);
493 do_complete_join(act);
500 check_promises(NULL, curr->get_cv());
511 * Initialize the current action by performing one or more of the following
512 * actions, as appropriate: merging RMWR and RMWC/RMW actions, stepping forward
513 * in the NodeStack, manipulating backtracking sets, allocating and
514 * initializing clock vectors, and computing the promises to fulfill.
516 * @param curr The current action, as passed from the user context; may be
517 * freed/invalidated after the execution of this function
518 * @return The current action, as processed by the ModelChecker. Is only the
519 * same as the parameter @a curr if this is a newly-explored action.
521 ModelAction * ModelChecker::initialize_curr_action(ModelAction *curr)
523 ModelAction *newcurr;
525 if (curr->is_rmwc() || curr->is_rmw()) {
526 newcurr = process_rmw(curr);
529 if (newcurr->is_rmw())
530 compute_promises(newcurr);
534 newcurr = node_stack->explore_action(curr, scheduler->get_enabled());
536 /* First restore type and order in case of RMW operation */
538 newcurr->copy_typeandorder(curr);
540 ASSERT(curr->get_location() == newcurr->get_location());
541 newcurr->copy_from_new(curr);
543 /* Discard duplicate ModelAction; use action from NodeStack */
546 /* Always compute new clock vector */
547 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
551 /* Always compute new clock vector */
552 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
554 * Perform one-time actions when pushing new ModelAction onto
557 if (newcurr->is_write())
558 compute_promises(newcurr);
564 * This method checks whether a model action is enabled at the given point.
565 * At this point, it checks whether a lock operation would be successful at this point.
566 * If not, it puts the thread in a waiter list.
567 * @param curr is the ModelAction to check whether it is enabled.
568 * @return a bool that indicates whether the action is enabled.
570 bool ModelChecker::check_action_enabled(ModelAction *curr) {
571 if (curr->is_lock()) {
572 std::mutex * lock = (std::mutex *)curr->get_location();
573 struct std::mutex_state * state = lock->get_state();
574 if (state->islocked) {
575 //Stick the action in the appropriate waiting queue
576 lock_waiters_map->get_safe_ptr(curr->get_location())->push_back(curr);
585 * This is the heart of the model checker routine. It performs model-checking
586 * actions corresponding to a given "current action." Among other processes, it
587 * calculates reads-from relationships, updates synchronization clock vectors,
588 * forms a memory_order constraints graph, and handles replay/backtrack
589 * execution when running permutations of previously-observed executions.
591 * @param curr The current action to process
592 * @return The next Thread that must be executed. May be NULL if ModelChecker
593 * makes no choice (e.g., according to replay execution, combining RMW actions,
596 Thread * ModelChecker::check_current_action(ModelAction *curr)
600 bool second_part_of_rmw = curr->is_rmwc() || curr->is_rmw();
602 if (!check_action_enabled(curr)) {
603 /* Make the execution look like we chose to run this action
604 * much later, when a lock is actually available to release */
605 get_current_thread()->set_pending(curr);
606 remove_thread(get_current_thread());
607 return get_next_thread(NULL);
610 ModelAction *newcurr = initialize_curr_action(curr);
612 /* Add the action to lists before any other model-checking tasks */
613 if (!second_part_of_rmw)
614 add_action_to_lists(newcurr);
616 /* Build may_read_from set for newly-created actions */
617 if (curr == newcurr && curr->is_read())
618 build_reads_from_past(curr);
621 /* Initialize work_queue with the "current action" work */
622 work_queue_t work_queue(1, CheckCurrWorkEntry(curr));
624 while (!work_queue.empty()) {
625 WorkQueueEntry work = work_queue.front();
626 work_queue.pop_front();
629 case WORK_CHECK_CURR_ACTION: {
630 ModelAction *act = work.action;
631 bool update = false; /* update this location's release seq's */
632 bool update_all = false; /* update all release seq's */
634 if (process_thread_action(curr))
637 if (act->is_read() && process_read(act, second_part_of_rmw))
640 if (act->is_write() && process_write(act))
643 if (act->is_mutex_op() && process_mutex(act))
647 work_queue.push_back(CheckRelSeqWorkEntry(NULL));
649 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
652 case WORK_CHECK_RELEASE_SEQ:
653 resolve_release_sequences(work.location, &work_queue);
655 case WORK_CHECK_MO_EDGES: {
656 /** @todo Complete verification of work_queue */
657 ModelAction *act = work.action;
658 bool updated = false;
660 if (act->is_read()) {
661 const ModelAction *rf = act->get_reads_from();
662 if (rf != NULL && r_modification_order(act, rf))
665 if (act->is_write()) {
666 if (w_modification_order(act))
669 mo_graph->commitChanges();
672 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
681 check_curr_backtracking(curr);
683 set_backtracking(curr);
685 return get_next_thread(curr);
689 * Complete a THREAD_JOIN operation, by synchronizing with the THREAD_FINISH
690 * operation from the Thread it is joining with. Must be called after the
691 * completion of the Thread in question.
692 * @param join The THREAD_JOIN action
694 void ModelChecker::do_complete_join(ModelAction *join)
696 Thread *blocking = (Thread *)join->get_location();
697 ModelAction *act = get_last_action(blocking->get_id());
698 join->synchronize_with(act);
701 void ModelChecker::check_curr_backtracking(ModelAction * curr) {
702 Node *currnode = curr->get_node();
703 Node *parnode = currnode->get_parent();
705 if ((!parnode->backtrack_empty() ||
706 !currnode->read_from_empty() ||
707 !currnode->future_value_empty() ||
708 !currnode->promise_empty())
709 && (!priv->next_backtrack ||
710 *curr > *priv->next_backtrack)) {
711 priv->next_backtrack = curr;
715 bool ModelChecker::promises_expired() {
716 for (unsigned int promise_index = 0; promise_index < promises->size(); promise_index++) {
717 Promise *promise = (*promises)[promise_index];
718 if (promise->get_expiration()<priv->used_sequence_numbers) {
725 /** @return whether the current partial trace must be a prefix of a
727 bool ModelChecker::isfeasibleprefix() {
728 return promises->size() == 0 && pending_acq_rel_seq->size() == 0;
731 /** @return whether the current partial trace is feasible. */
732 bool ModelChecker::isfeasible() {
733 if (DBG_ENABLED() && mo_graph->checkForRMWViolation())
734 DEBUG("Infeasible: RMW violation\n");
736 return !mo_graph->checkForRMWViolation() && isfeasibleotherthanRMW();
739 /** @return whether the current partial trace is feasible other than
740 * multiple RMW reading from the same store. */
741 bool ModelChecker::isfeasibleotherthanRMW() {
743 if (mo_graph->checkForCycles())
744 DEBUG("Infeasible: modification order cycles\n");
746 DEBUG("Infeasible: failed promise\n");
748 DEBUG("Infeasible: too many reads\n");
749 if (bad_synchronization)
750 DEBUG("Infeasible: bad synchronization ordering\n");
751 if (promises_expired())
752 DEBUG("Infeasible: promises expired\n");
754 return !mo_graph->checkForCycles() && !failed_promise && !too_many_reads && !bad_synchronization && !promises_expired();
757 /** Returns whether the current completed trace is feasible. */
758 bool ModelChecker::isfinalfeasible() {
759 if (DBG_ENABLED() && promises->size() != 0)
760 DEBUG("Infeasible: unrevolved promises\n");
762 return isfeasible() && promises->size() == 0;
765 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
766 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
767 int tid = id_to_int(act->get_tid());
768 ModelAction *lastread = get_last_action(tid);
769 lastread->process_rmw(act);
770 if (act->is_rmw() && lastread->get_reads_from()!=NULL) {
771 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
772 mo_graph->commitChanges();
778 * Checks whether a thread has read from the same write for too many times
779 * without seeing the effects of a later write.
782 * 1) there must a different write that we could read from that would satisfy the modification order,
783 * 2) we must have read from the same value in excess of maxreads times, and
784 * 3) that other write must have been in the reads_from set for maxreads times.
786 * If so, we decide that the execution is no longer feasible.
788 void ModelChecker::check_recency(ModelAction *curr, const ModelAction *rf) {
789 if (params.maxreads != 0) {
791 if (curr->get_node()->get_read_from_size() <= 1)
793 //Must make sure that execution is currently feasible... We could
794 //accidentally clear by rolling back
797 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
798 int tid = id_to_int(curr->get_tid());
801 if ((int)thrd_lists->size() <= tid)
803 action_list_t *list = &(*thrd_lists)[tid];
805 action_list_t::reverse_iterator rit = list->rbegin();
807 for (; (*rit) != curr; rit++)
809 /* go past curr now */
812 action_list_t::reverse_iterator ritcopy = rit;
813 //See if we have enough reads from the same value
815 for (; count < params.maxreads; rit++,count++) {
816 if (rit==list->rend())
818 ModelAction *act = *rit;
822 if (act->get_reads_from() != rf)
824 if (act->get_node()->get_read_from_size() <= 1)
827 for (int i = 0; i<curr->get_node()->get_read_from_size(); i++) {
829 const ModelAction * write = curr->get_node()->get_read_from_at(i);
831 //Need a different write
835 /* Test to see whether this is a feasible write to read from*/
836 mo_graph->startChanges();
837 r_modification_order(curr, write);
838 bool feasiblereadfrom = isfeasible();
839 mo_graph->rollbackChanges();
841 if (!feasiblereadfrom)
845 bool feasiblewrite = true;
846 //new we need to see if this write works for everyone
848 for (int loop = count; loop>0; loop--,rit++) {
849 ModelAction *act=*rit;
850 bool foundvalue = false;
851 for (int j = 0; j<act->get_node()->get_read_from_size(); j++) {
852 if (act->get_node()->get_read_from_at(i)==write) {
858 feasiblewrite = false;
863 too_many_reads = true;
871 * Updates the mo_graph with the constraints imposed from the current
874 * Basic idea is the following: Go through each other thread and find
875 * the lastest action that happened before our read. Two cases:
877 * (1) The action is a write => that write must either occur before
878 * the write we read from or be the write we read from.
880 * (2) The action is a read => the write that that action read from
881 * must occur before the write we read from or be the same write.
883 * @param curr The current action. Must be a read.
884 * @param rf The action that curr reads from. Must be a write.
885 * @return True if modification order edges were added; false otherwise
887 bool ModelChecker::r_modification_order(ModelAction *curr, const ModelAction *rf)
889 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
892 ASSERT(curr->is_read());
894 /* Iterate over all threads */
895 for (i = 0; i < thrd_lists->size(); i++) {
896 /* Iterate over actions in thread, starting from most recent */
897 action_list_t *list = &(*thrd_lists)[i];
898 action_list_t::reverse_iterator rit;
899 for (rit = list->rbegin(); rit != list->rend(); rit++) {
900 ModelAction *act = *rit;
903 * Include at most one act per-thread that "happens
904 * before" curr. Don't consider reflexively.
906 if (act->happens_before(curr) && act != curr) {
907 if (act->is_write()) {
909 mo_graph->addEdge(act, rf);
913 const ModelAction *prevreadfrom = act->get_reads_from();
914 if (prevreadfrom != NULL && rf != prevreadfrom) {
915 mo_graph->addEdge(prevreadfrom, rf);
927 /** This method fixes up the modification order when we resolve a
928 * promises. The basic problem is that actions that occur after the
929 * read curr could not property add items to the modification order
932 * So for each thread, we find the earliest item that happens after
933 * the read curr. This is the item we have to fix up with additional
934 * constraints. If that action is write, we add a MO edge between
935 * the Action rf and that action. If the action is a read, we add a
936 * MO edge between the Action rf, and whatever the read accessed.
938 * @param curr is the read ModelAction that we are fixing up MO edges for.
939 * @param rf is the write ModelAction that curr reads from.
942 void ModelChecker::post_r_modification_order(ModelAction *curr, const ModelAction *rf)
944 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
946 ASSERT(curr->is_read());
948 /* Iterate over all threads */
949 for (i = 0; i < thrd_lists->size(); i++) {
950 /* Iterate over actions in thread, starting from most recent */
951 action_list_t *list = &(*thrd_lists)[i];
952 action_list_t::reverse_iterator rit;
953 ModelAction *lastact = NULL;
955 /* Find last action that happens after curr that is either not curr or a rmw */
956 for (rit = list->rbegin(); rit != list->rend(); rit++) {
957 ModelAction *act = *rit;
958 if (curr->happens_before(act) && (curr != act || curr->is_rmw())) {
964 /* Include at most one act per-thread that "happens before" curr */
965 if (lastact != NULL) {
967 //Case 1: The resolved read is a RMW, and we need to make sure
968 //that the write portion of the RMW mod order after rf
970 mo_graph->addEdge(rf, lastact);
971 } else if (lastact->is_read()) {
972 //Case 2: The resolved read is a normal read and the next
973 //operation is a read, and we need to make sure the value read
974 //is mod ordered after rf
976 const ModelAction *postreadfrom = lastact->get_reads_from();
977 if (postreadfrom != NULL&&rf != postreadfrom)
978 mo_graph->addEdge(rf, postreadfrom);
980 //Case 3: The resolved read is a normal read and the next
981 //operation is a write, and we need to make sure that the
982 //write is mod ordered after rf
984 mo_graph->addEdge(rf, lastact);
992 * Updates the mo_graph with the constraints imposed from the current write.
994 * Basic idea is the following: Go through each other thread and find
995 * the lastest action that happened before our write. Two cases:
997 * (1) The action is a write => that write must occur before
1000 * (2) The action is a read => the write that that action read from
1001 * must occur before the current write.
1003 * This method also handles two other issues:
1005 * (I) Sequential Consistency: Making sure that if the current write is
1006 * seq_cst, that it occurs after the previous seq_cst write.
1008 * (II) Sending the write back to non-synchronizing reads.
1010 * @param curr The current action. Must be a write.
1011 * @return True if modification order edges were added; false otherwise
1013 bool ModelChecker::w_modification_order(ModelAction *curr)
1015 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
1018 ASSERT(curr->is_write());
1020 if (curr->is_seqcst()) {
1021 /* We have to at least see the last sequentially consistent write,
1022 so we are initialized. */
1023 ModelAction *last_seq_cst = get_last_seq_cst(curr);
1024 if (last_seq_cst != NULL) {
1025 mo_graph->addEdge(last_seq_cst, curr);
1030 /* Iterate over all threads */
1031 for (i = 0; i < thrd_lists->size(); i++) {
1032 /* Iterate over actions in thread, starting from most recent */
1033 action_list_t *list = &(*thrd_lists)[i];
1034 action_list_t::reverse_iterator rit;
1035 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1036 ModelAction *act = *rit;
1039 * If RMW, we already have all relevant edges,
1040 * so just skip to next thread.
1041 * If normal write, we need to look at earlier
1042 * actions, so continue processing list.
1051 * Include at most one act per-thread that "happens
1054 if (act->happens_before(curr)) {
1056 * Note: if act is RMW, just add edge:
1058 * The following edge should be handled elsewhere:
1059 * readfrom(act) --mo--> act
1061 if (act->is_write())
1062 mo_graph->addEdge(act, curr);
1063 else if (act->is_read() && act->get_reads_from() != NULL)
1064 mo_graph->addEdge(act->get_reads_from(), curr);
1067 } else if (act->is_read() && !act->is_synchronizing(curr) &&
1068 !act->same_thread(curr)) {
1069 /* We have an action that:
1070 (1) did not happen before us
1071 (2) is a read and we are a write
1072 (3) cannot synchronize with us
1073 (4) is in a different thread
1075 that read could potentially read from our write.
1077 if (thin_air_constraint_may_allow(curr, act)) {
1079 (curr->is_rmw() && act->is_rmw() && curr->get_reads_from() == act->get_reads_from() && isfeasibleotherthanRMW())) {
1080 struct PendingFutureValue pfv = {curr->get_value(),curr->get_seq_number()+params.maxfuturedelay,act};
1081 futurevalues->push_back(pfv);
1091 /** Arbitrary reads from the future are not allowed. Section 29.3
1092 * part 9 places some constraints. This method checks one result of constraint
1093 * constraint. Others require compiler support. */
1094 bool ModelChecker::thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader) {
1095 if (!writer->is_rmw())
1098 if (!reader->is_rmw())
1101 for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
1102 if (search == reader)
1104 if (search->get_tid() == reader->get_tid() &&
1105 search->happens_before(reader))
1113 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
1114 * The ModelAction under consideration is expected to be taking part in
1115 * release/acquire synchronization as an object of the "reads from" relation.
1116 * Note that this can only provide release sequence support for RMW chains
1117 * which do not read from the future, as those actions cannot be traced until
1118 * their "promise" is fulfilled. Similarly, we may not even establish the
1119 * presence of a release sequence with certainty, as some modification order
1120 * constraints may be decided further in the future. Thus, this function
1121 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
1122 * and a boolean representing certainty.
1124 * @todo Finish lazy updating, when promises are fulfilled in the future
1125 * @param rf The action that might be part of a release sequence. Must be a
1127 * @param release_heads A pass-by-reference style return parameter. After
1128 * execution of this function, release_heads will contain the heads of all the
1129 * relevant release sequences, if any exists
1130 * @return true, if the ModelChecker is certain that release_heads is complete;
1133 bool ModelChecker::release_seq_head(const ModelAction *rf, rel_heads_list_t *release_heads) const
1135 /* Only check for release sequences if there are no cycles */
1136 if (mo_graph->checkForCycles())
1140 ASSERT(rf->is_write());
1142 if (rf->is_release())
1143 release_heads->push_back(rf);
1145 break; /* End of RMW chain */
1147 /** @todo Need to be smarter here... In the linux lock
1148 * example, this will run to the beginning of the program for
1150 /** @todo The way to be smarter here is to keep going until 1
1151 * thread has a release preceded by an acquire and you've seen
1154 /* acq_rel RMW is a sufficient stopping condition */
1155 if (rf->is_acquire() && rf->is_release())
1156 return true; /* complete */
1158 rf = rf->get_reads_from();
1161 /* read from future: need to settle this later */
1162 return false; /* incomplete */
1165 if (rf->is_release())
1166 return true; /* complete */
1168 /* else relaxed write; check modification order for contiguous subsequence
1169 * -> rf must be same thread as release */
1170 int tid = id_to_int(rf->get_tid());
1171 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(rf->get_location());
1172 action_list_t *list = &(*thrd_lists)[tid];
1173 action_list_t::const_reverse_iterator rit;
1175 /* Find rf in the thread list */
1176 rit = std::find(list->rbegin(), list->rend(), rf);
1177 ASSERT(rit != list->rend());
1179 /* Find the last write/release */
1180 for (; rit != list->rend(); rit++)
1181 if ((*rit)->is_release())
1183 if (rit == list->rend()) {
1184 /* No write-release in this thread */
1185 return true; /* complete */
1187 ModelAction *release = *rit;
1189 ASSERT(rf->same_thread(release));
1191 bool certain = true;
1192 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
1193 if (id_to_int(rf->get_tid()) == (int)i)
1195 list = &(*thrd_lists)[i];
1197 /* Can we ensure no future writes from this thread may break
1198 * the release seq? */
1199 bool future_ordered = false;
1201 ModelAction *last = get_last_action(int_to_id(i));
1202 if (last && (rf->happens_before(last) ||
1203 last->get_type() == THREAD_FINISH))
1204 future_ordered = true;
1206 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1207 const ModelAction *act = *rit;
1208 /* Reach synchronization -> this thread is complete */
1209 if (act->happens_before(release))
1211 if (rf->happens_before(act)) {
1212 future_ordered = true;
1216 /* Only writes can break release sequences */
1217 if (!act->is_write())
1220 /* Check modification order */
1221 if (mo_graph->checkReachable(rf, act)) {
1222 /* rf --mo--> act */
1223 future_ordered = true;
1226 if (mo_graph->checkReachable(act, release))
1227 /* act --mo--> release */
1229 if (mo_graph->checkReachable(release, act) &&
1230 mo_graph->checkReachable(act, rf)) {
1231 /* release --mo-> act --mo--> rf */
1232 return true; /* complete */
1236 if (!future_ordered)
1237 return false; /* This thread is uncertain */
1241 release_heads->push_back(release);
1246 * A public interface for getting the release sequence head(s) with which a
1247 * given ModelAction must synchronize. This function only returns a non-empty
1248 * result when it can locate a release sequence head with certainty. Otherwise,
1249 * it may mark the internal state of the ModelChecker so that it will handle
1250 * the release sequence at a later time, causing @a act to update its
1251 * synchronization at some later point in execution.
1252 * @param act The 'acquire' action that may read from a release sequence
1253 * @param release_heads A pass-by-reference return parameter. Will be filled
1254 * with the head(s) of the release sequence(s), if they exists with certainty.
1255 * @see ModelChecker::release_seq_head
1257 void ModelChecker::get_release_seq_heads(ModelAction *act, rel_heads_list_t *release_heads)
1259 const ModelAction *rf = act->get_reads_from();
1261 complete = release_seq_head(rf, release_heads);
1263 /* add act to 'lazy checking' list */
1264 pending_acq_rel_seq->push_back(act);
1269 * Attempt to resolve all stashed operations that might synchronize with a
1270 * release sequence for a given location. This implements the "lazy" portion of
1271 * determining whether or not a release sequence was contiguous, since not all
1272 * modification order information is present at the time an action occurs.
1274 * @param location The location/object that should be checked for release
1275 * sequence resolutions. A NULL value means to check all locations.
1276 * @param work_queue The work queue to which to add work items as they are
1278 * @return True if any updates occurred (new synchronization, new mo_graph
1281 bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
1283 bool updated = false;
1284 std::vector<ModelAction *>::iterator it = pending_acq_rel_seq->begin();
1285 while (it != pending_acq_rel_seq->end()) {
1286 ModelAction *act = *it;
1288 /* Only resolve sequences on the given location, if provided */
1289 if (location && act->get_location() != location) {
1294 const ModelAction *rf = act->get_reads_from();
1295 rel_heads_list_t release_heads;
1297 complete = release_seq_head(rf, &release_heads);
1298 for (unsigned int i = 0; i < release_heads.size(); i++) {
1299 if (!act->has_synchronized_with(release_heads[i])) {
1300 if (act->synchronize_with(release_heads[i]))
1303 set_bad_synchronization();
1308 /* Re-check act for mo_graph edges */
1309 work_queue->push_back(MOEdgeWorkEntry(act));
1311 /* propagate synchronization to later actions */
1312 action_list_t::reverse_iterator rit = action_trace->rbegin();
1313 for (; (*rit) != act; rit++) {
1314 ModelAction *propagate = *rit;
1315 if (act->happens_before(propagate)) {
1316 propagate->synchronize_with(act);
1317 /* Re-check 'propagate' for mo_graph edges */
1318 work_queue->push_back(MOEdgeWorkEntry(propagate));
1323 it = pending_acq_rel_seq->erase(it);
1328 // If we resolved promises or data races, see if we have realized a data race.
1329 if (checkDataRaces()) {
1337 * Performs various bookkeeping operations for the current ModelAction. For
1338 * instance, adds action to the per-object, per-thread action vector and to the
1339 * action trace list of all thread actions.
1341 * @param act is the ModelAction to add.
1343 void ModelChecker::add_action_to_lists(ModelAction *act)
1345 int tid = id_to_int(act->get_tid());
1346 action_trace->push_back(act);
1348 obj_map->get_safe_ptr(act->get_location())->push_back(act);
1350 std::vector<action_list_t> *vec = obj_thrd_map->get_safe_ptr(act->get_location());
1351 if (tid >= (int)vec->size())
1352 vec->resize(priv->next_thread_id);
1353 (*vec)[tid].push_back(act);
1355 if ((int)thrd_last_action->size() <= tid)
1356 thrd_last_action->resize(get_num_threads());
1357 (*thrd_last_action)[tid] = act;
1361 * @brief Get the last action performed by a particular Thread
1362 * @param tid The thread ID of the Thread in question
1363 * @return The last action in the thread
1365 ModelAction * ModelChecker::get_last_action(thread_id_t tid) const
1367 int threadid = id_to_int(tid);
1368 if (threadid < (int)thrd_last_action->size())
1369 return (*thrd_last_action)[id_to_int(tid)];
1375 * Gets the last memory_order_seq_cst write (in the total global sequence)
1376 * performed on a particular object (i.e., memory location), not including the
1378 * @param curr The current ModelAction; also denotes the object location to
1380 * @return The last seq_cst write
1382 ModelAction * ModelChecker::get_last_seq_cst(ModelAction *curr) const
1384 void *location = curr->get_location();
1385 action_list_t *list = obj_map->get_safe_ptr(location);
1386 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
1387 action_list_t::reverse_iterator rit;
1388 for (rit = list->rbegin(); rit != list->rend(); rit++)
1389 if ((*rit)->is_write() && (*rit)->is_seqcst() && (*rit) != curr)
1395 * Gets the last unlock operation performed on a particular mutex (i.e., memory
1396 * location). This function identifies the mutex according to the current
1397 * action, which is presumed to perform on the same mutex.
1398 * @param curr The current ModelAction; also denotes the object location to
1400 * @return The last unlock operation
1402 ModelAction * ModelChecker::get_last_unlock(ModelAction *curr) const
1404 void *location = curr->get_location();
1405 action_list_t *list = obj_map->get_safe_ptr(location);
1406 /* Find: max({i in dom(S) | isUnlock(t_i) && samevar(t_i, t)}) */
1407 action_list_t::reverse_iterator rit;
1408 for (rit = list->rbegin(); rit != list->rend(); rit++)
1409 if ((*rit)->is_unlock())
1414 ModelAction * ModelChecker::get_parent_action(thread_id_t tid)
1416 ModelAction *parent = get_last_action(tid);
1418 parent = get_thread(tid)->get_creation();
1423 * Returns the clock vector for a given thread.
1424 * @param tid The thread whose clock vector we want
1425 * @return Desired clock vector
1427 ClockVector * ModelChecker::get_cv(thread_id_t tid)
1429 return get_parent_action(tid)->get_cv();
1433 * Resolve a set of Promises with a current write. The set is provided in the
1434 * Node corresponding to @a write.
1435 * @param write The ModelAction that is fulfilling Promises
1436 * @return True if promises were resolved; false otherwise
1438 bool ModelChecker::resolve_promises(ModelAction *write)
1440 bool resolved = false;
1442 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
1443 Promise *promise = (*promises)[promise_index];
1444 if (write->get_node()->get_promise(i)) {
1445 ModelAction *read = promise->get_action();
1446 if (read->is_rmw()) {
1447 mo_graph->addRMWEdge(write, read);
1449 read->read_from(write);
1450 //First fix up the modification order for actions that happened
1452 r_modification_order(read, write);
1453 //Next fix up the modification order for actions that happened
1455 post_r_modification_order(read, write);
1456 //Make sure the promise's value matches the write's value
1457 ASSERT(promise->get_value() == write->get_value());
1459 promises->erase(promises->begin() + promise_index);
1468 * Compute the set of promises that could potentially be satisfied by this
1469 * action. Note that the set computation actually appears in the Node, not in
1471 * @param curr The ModelAction that may satisfy promises
1473 void ModelChecker::compute_promises(ModelAction *curr)
1475 for (unsigned int i = 0; i < promises->size(); i++) {
1476 Promise *promise = (*promises)[i];
1477 const ModelAction *act = promise->get_action();
1478 if (!act->happens_before(curr) &&
1480 !act->is_synchronizing(curr) &&
1481 !act->same_thread(curr) &&
1482 promise->get_value() == curr->get_value()) {
1483 curr->get_node()->set_promise(i);
1488 /** Checks promises in response to change in ClockVector Threads. */
1489 void ModelChecker::check_promises(ClockVector *old_cv, ClockVector *merge_cv)
1491 for (unsigned int i = 0; i < promises->size(); i++) {
1492 Promise *promise = (*promises)[i];
1493 const ModelAction *act = promise->get_action();
1494 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
1495 merge_cv->synchronized_since(act)) {
1496 //This thread is no longer able to send values back to satisfy the promise
1497 int num_synchronized_threads = promise->increment_threads();
1498 if (num_synchronized_threads == get_num_threads()) {
1499 //Promise has failed
1500 failed_promise = true;
1508 * Build up an initial set of all past writes that this 'read' action may read
1509 * from. This set is determined by the clock vector's "happens before"
1511 * @param curr is the current ModelAction that we are exploring; it must be a
1514 void ModelChecker::build_reads_from_past(ModelAction *curr)
1516 std::vector<action_list_t> *thrd_lists = obj_thrd_map->get_safe_ptr(curr->get_location());
1518 ASSERT(curr->is_read());
1520 ModelAction *last_seq_cst = NULL;
1522 /* Track whether this object has been initialized */
1523 bool initialized = false;
1525 if (curr->is_seqcst()) {
1526 last_seq_cst = get_last_seq_cst(curr);
1527 /* We have to at least see the last sequentially consistent write,
1528 so we are initialized. */
1529 if (last_seq_cst != NULL)
1533 /* Iterate over all threads */
1534 for (i = 0; i < thrd_lists->size(); i++) {
1535 /* Iterate over actions in thread, starting from most recent */
1536 action_list_t *list = &(*thrd_lists)[i];
1537 action_list_t::reverse_iterator rit;
1538 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1539 ModelAction *act = *rit;
1541 /* Only consider 'write' actions */
1542 if (!act->is_write() || act == curr)
1545 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
1546 if (!curr->is_seqcst() || (!act->is_seqcst() && (last_seq_cst == NULL || !act->happens_before(last_seq_cst))) || act == last_seq_cst) {
1547 DEBUG("Adding action to may_read_from:\n");
1548 if (DBG_ENABLED()) {
1552 curr->get_node()->add_read_from(act);
1555 /* Include at most one act per-thread that "happens before" curr */
1556 if (act->happens_before(curr)) {
1564 /** @todo Need a more informative way of reporting errors. */
1565 printf("ERROR: may read from uninitialized atomic\n");
1568 if (DBG_ENABLED() || !initialized) {
1569 printf("Reached read action:\n");
1571 printf("Printing may_read_from\n");
1572 curr->get_node()->print_may_read_from();
1573 printf("End printing may_read_from\n");
1576 ASSERT(initialized);
1579 static void print_list(action_list_t *list)
1581 action_list_t::iterator it;
1583 printf("---------------------------------------------------------------------\n");
1586 for (it = list->begin(); it != list->end(); it++) {
1589 printf("---------------------------------------------------------------------\n");
1592 void ModelChecker::print_summary()
1595 printf("Number of executions: %d\n", num_executions);
1596 printf("Number of feasible executions: %d\n", num_feasible_executions);
1597 printf("Total nodes created: %d\n", node_stack->get_total_nodes());
1599 #if SUPPORT_MOD_ORDER_DUMP
1601 char buffername[100];
1602 sprintf(buffername, "exec%04u", num_executions);
1603 mo_graph->dumpGraphToFile(buffername);
1606 if (!isfinalfeasible())
1607 printf("INFEASIBLE EXECUTION!\n");
1608 print_list(action_trace);
1613 * Add a Thread to the system for the first time. Should only be called once
1615 * @param t The Thread to add
1617 void ModelChecker::add_thread(Thread *t)
1619 thread_map->put(id_to_int(t->get_id()), t);
1620 scheduler->add_thread(t);
1624 * Removes a thread from the scheduler.
1625 * @param the thread to remove.
1627 void ModelChecker::remove_thread(Thread *t)
1629 scheduler->remove_thread(t);
1633 * Switch from a user-context to the "master thread" context (a.k.a. system
1634 * context). This switch is made with the intention of exploring a particular
1635 * model-checking action (described by a ModelAction object). Must be called
1636 * from a user-thread context.
1637 * @param act The current action that will be explored. Must not be NULL.
1638 * @return Return status from the 'swap' call (i.e., success/fail, 0/-1)
1640 int ModelChecker::switch_to_master(ModelAction *act)
1643 Thread *old = thread_current();
1644 set_current_action(act);
1645 old->set_state(THREAD_READY);
1646 return Thread::swap(old, &system_context);
1650 * Takes the next step in the execution, if possible.
1651 * @return Returns true (success) if a step was taken and false otherwise.
1653 bool ModelChecker::take_step() {
1657 Thread * curr = thread_current();
1659 if (curr->get_state() == THREAD_READY) {
1660 ASSERT(priv->current_action);
1662 priv->nextThread = check_current_action(priv->current_action);
1663 priv->current_action = NULL;
1665 if (curr->is_blocked() || curr->is_complete())
1666 scheduler->remove_thread(curr);
1671 Thread * next = scheduler->next_thread(priv->nextThread);
1673 /* Infeasible -> don't take any more steps */
1678 next->set_state(THREAD_RUNNING);
1679 DEBUG("(%d, %d)\n", curr ? curr->get_id() : -1, next ? next->get_id() : -1);
1681 /* next == NULL -> don't take any more steps */
1685 if ( next->get_pending() != NULL ) {
1686 //restart a pending action
1687 set_current_action(next->get_pending());
1688 next->set_pending(NULL);
1689 next->set_state(THREAD_READY);
1693 /* Return false only if swap fails with an error */
1694 return (Thread::swap(&system_context, next) == 0);
1697 /** Runs the current execution until threre are no more steps to take. */
1698 void ModelChecker::finish_execution() {
1701 while (take_step());