10 #include "snapshot-interface.h"
12 #include "clockvector.h"
13 #include "cyclegraph.h"
16 #include "threads-model.h"
19 #define INITIAL_THREAD_ID 0
24 bug_message(const char *str) {
25 const char *fmt = " [BUG] %s\n";
26 msg = (char *)snapshot_malloc(strlen(fmt) + strlen(str));
27 sprintf(msg, fmt, str);
29 ~bug_message() { if (msg) snapshot_free(msg); }
32 void print() { model_print("%s", msg); }
38 * Structure for holding small ModelChecker members that should be snapshotted
40 struct model_snapshot_members {
41 model_snapshot_members() :
42 /* First thread created will have id INITIAL_THREAD_ID */
43 next_thread_id(INITIAL_THREAD_ID),
44 used_sequence_numbers(0),
48 failed_promise(false),
49 too_many_reads(false),
50 no_valid_reads(false),
51 bad_synchronization(false),
55 ~model_snapshot_members() {
56 for (unsigned int i = 0; i < bugs.size(); i++)
61 unsigned int next_thread_id;
62 modelclock_t used_sequence_numbers;
63 ModelAction *next_backtrack;
64 std::vector< bug_message *, SnapshotAlloc<bug_message *> > bugs;
65 struct execution_stats stats;
69 /** @brief Incorrectly-ordered synchronization was made */
70 bool bad_synchronization;
76 /** @brief Constructor */
77 ModelChecker::ModelChecker(struct model_params params) :
78 /* Initialize default scheduler */
80 scheduler(new Scheduler()),
82 earliest_diverge(NULL),
83 action_trace(new action_list_t()),
84 thread_map(new HashTable<int, Thread *, int>()),
85 obj_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
86 lock_waiters_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
87 condvar_waiters_map(new HashTable<const void *, action_list_t *, uintptr_t, 4>()),
88 obj_thrd_map(new HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4 >()),
89 promises(new std::vector< Promise *, SnapshotAlloc<Promise *> >()),
90 futurevalues(new std::vector< struct PendingFutureValue, SnapshotAlloc<struct PendingFutureValue> >()),
91 pending_rel_seqs(new std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> >()),
92 thrd_last_action(new std::vector< ModelAction *, SnapshotAlloc<ModelAction *> >(1)),
93 thrd_last_fence_release(new std::vector< ModelAction *, SnapshotAlloc<ModelAction *> >()),
94 node_stack(new NodeStack()),
95 priv(new struct model_snapshot_members()),
96 mo_graph(new CycleGraph())
98 /* Initialize a model-checker thread, for special ModelActions */
99 model_thread = new Thread(get_next_id());
100 thread_map->put(id_to_int(model_thread->get_id()), model_thread);
103 /** @brief Destructor */
104 ModelChecker::~ModelChecker()
106 for (unsigned int i = 0; i < get_num_threads(); i++)
107 delete thread_map->get(i);
112 delete lock_waiters_map;
113 delete condvar_waiters_map;
116 for (unsigned int i = 0; i < promises->size(); i++)
117 delete (*promises)[i];
120 delete pending_rel_seqs;
122 delete thrd_last_action;
123 delete thrd_last_fence_release;
130 static action_list_t * get_safe_ptr_action(HashTable<const void *, action_list_t *, uintptr_t, 4> * hash, void * ptr)
132 action_list_t *tmp = hash->get(ptr);
134 tmp = new action_list_t();
140 static std::vector<action_list_t> * get_safe_ptr_vect_action(HashTable<void *, std::vector<action_list_t> *, uintptr_t, 4> * hash, void * ptr)
142 std::vector<action_list_t> *tmp = hash->get(ptr);
144 tmp = new std::vector<action_list_t>();
151 * Restores user program to initial state and resets all model-checker data
154 void ModelChecker::reset_to_initial_state()
156 DEBUG("+++ Resetting to initial state +++\n");
157 node_stack->reset_execution();
159 /* Print all model-checker output before rollback */
162 snapshot_backtrack_before(0);
165 /** @return a thread ID for a new Thread */
166 thread_id_t ModelChecker::get_next_id()
168 return priv->next_thread_id++;
171 /** @return the number of user threads created during this execution */
172 unsigned int ModelChecker::get_num_threads() const
174 return priv->next_thread_id;
178 * Must be called from user-thread context (e.g., through the global
179 * thread_current() interface)
181 * @return The currently executing Thread.
183 Thread * ModelChecker::get_current_thread() const
185 return scheduler->get_current_thread();
188 /** @return a sequence number for a new ModelAction */
189 modelclock_t ModelChecker::get_next_seq_num()
191 return ++priv->used_sequence_numbers;
194 Node * ModelChecker::get_curr_node() const
196 return node_stack->get_head();
200 * @brief Choose the next thread to execute.
202 * This function chooses the next thread that should execute. It can force the
203 * adjacency of read/write portions of a RMW action, force THREAD_CREATE to be
204 * followed by a THREAD_START, or it can enforce execution replay/backtracking.
205 * The model-checker may have no preference regarding the next thread (i.e.,
206 * when exploring a new execution ordering), in which case this will return
208 * @param curr The current ModelAction. This action might guide the choice of
210 * @return The next thread to run. If the model-checker has no preference, NULL.
212 Thread * ModelChecker::get_next_thread(ModelAction *curr)
217 /* Do not split atomic actions. */
219 return get_thread(curr);
220 else if (curr->get_type() == THREAD_CREATE)
221 return curr->get_thread_operand();
224 /* Have we completed exploring the preselected path? */
228 /* Else, we are trying to replay an execution */
229 ModelAction *next = node_stack->get_next()->get_action();
231 if (next == diverge) {
232 if (earliest_diverge == NULL || *diverge < *earliest_diverge)
233 earliest_diverge = diverge;
235 Node *nextnode = next->get_node();
236 Node *prevnode = nextnode->get_parent();
237 scheduler->update_sleep_set(prevnode);
239 /* Reached divergence point */
240 if (nextnode->increment_misc()) {
241 /* The next node will try to satisfy a different misc_index values. */
242 tid = next->get_tid();
243 node_stack->pop_restofstack(2);
244 } else if (nextnode->increment_promise()) {
245 /* The next node will try to satisfy a different set of promises. */
246 tid = next->get_tid();
247 node_stack->pop_restofstack(2);
248 } else if (nextnode->increment_read_from()) {
249 /* The next node will read from a different value. */
250 tid = next->get_tid();
251 node_stack->pop_restofstack(2);
252 } else if (nextnode->increment_future_value()) {
253 /* The next node will try to read from a different future value. */
254 tid = next->get_tid();
255 node_stack->pop_restofstack(2);
256 } else if (nextnode->increment_relseq_break()) {
257 /* The next node will try to resolve a release sequence differently */
258 tid = next->get_tid();
259 node_stack->pop_restofstack(2);
262 /* Make a different thread execute for next step */
263 scheduler->add_sleep(get_thread(next->get_tid()));
264 tid = prevnode->get_next_backtrack();
265 /* Make sure the backtracked thread isn't sleeping. */
266 node_stack->pop_restofstack(1);
267 if (diverge == earliest_diverge) {
268 earliest_diverge = prevnode->get_action();
271 /* The correct sleep set is in the parent node. */
274 DEBUG("*** Divergence point ***\n");
278 tid = next->get_tid();
280 DEBUG("*** ModelChecker chose next thread = %d ***\n", id_to_int(tid));
281 ASSERT(tid != THREAD_ID_T_NONE);
282 return thread_map->get(id_to_int(tid));
286 * We need to know what the next actions of all threads in the sleep
287 * set will be. This method computes them and stores the actions at
288 * the corresponding thread object's pending action.
291 void ModelChecker::execute_sleep_set()
293 for (unsigned int i = 0; i < get_num_threads(); i++) {
294 thread_id_t tid = int_to_id(i);
295 Thread *thr = get_thread(tid);
296 if (scheduler->is_sleep_set(thr) && thr->get_pending() == NULL) {
297 scheduler->next_thread(thr);
298 Thread::swap(&system_context, thr);
299 thr->get_pending()->set_sleep_flag();
304 void ModelChecker::wake_up_sleeping_actions(ModelAction *curr)
306 for (unsigned int i = 0; i < get_num_threads(); i++) {
307 Thread *thr = get_thread(int_to_id(i));
308 if (scheduler->is_sleep_set(thr)) {
309 ModelAction *pending_act = thr->get_pending();
310 if ((!curr->is_rmwr()) && pending_act->could_synchronize_with(curr))
311 //Remove this thread from sleep set
312 scheduler->remove_sleep(thr);
317 /** @brief Alert the model-checker that an incorrectly-ordered
318 * synchronization was made */
319 void ModelChecker::set_bad_synchronization()
321 priv->bad_synchronization = true;
324 bool ModelChecker::has_asserted() const
326 return priv->asserted;
329 void ModelChecker::set_assert()
331 priv->asserted = true;
335 * Check if we are in a deadlock. Should only be called at the end of an
336 * execution, although it should not give false positives in the middle of an
337 * execution (there should be some ENABLED thread).
339 * @return True if program is in a deadlock; false otherwise
341 bool ModelChecker::is_deadlocked() const
343 bool blocking_threads = false;
344 for (unsigned int i = 0; i < get_num_threads(); i++) {
345 thread_id_t tid = int_to_id(i);
348 Thread *t = get_thread(tid);
349 if (!t->is_model_thread() && t->get_pending())
350 blocking_threads = true;
352 return blocking_threads;
356 * Check if this is a complete execution. That is, have all thread completed
357 * execution (rather than exiting because sleep sets have forced a redundant
360 * @return True if the execution is complete.
362 bool ModelChecker::is_complete_execution() const
364 for (unsigned int i = 0; i < get_num_threads(); i++)
365 if (is_enabled(int_to_id(i)))
371 * @brief Assert a bug in the executing program.
373 * Use this function to assert any sort of bug in the user program. If the
374 * current trace is feasible (actually, a prefix of some feasible execution),
375 * then this execution will be aborted, printing the appropriate message. If
376 * the current trace is not yet feasible, the error message will be stashed and
377 * printed if the execution ever becomes feasible.
379 * @param msg Descriptive message for the bug (do not include newline char)
380 * @return True if bug is immediately-feasible
382 bool ModelChecker::assert_bug(const char *msg)
384 priv->bugs.push_back(new bug_message(msg));
386 if (isfeasibleprefix()) {
394 * @brief Assert a bug in the executing program, asserted by a user thread
395 * @see ModelChecker::assert_bug
396 * @param msg Descriptive message for the bug (do not include newline char)
398 void ModelChecker::assert_user_bug(const char *msg)
400 /* If feasible bug, bail out now */
402 switch_to_master(NULL);
405 /** @return True, if any bugs have been reported for this execution */
406 bool ModelChecker::have_bug_reports() const
408 return priv->bugs.size() != 0;
411 /** @brief Print bug report listing for this execution (if any bugs exist) */
412 void ModelChecker::print_bugs() const
414 if (have_bug_reports()) {
415 model_print("Bug report: %zu bug%s detected\n",
417 priv->bugs.size() > 1 ? "s" : "");
418 for (unsigned int i = 0; i < priv->bugs.size(); i++)
419 priv->bugs[i]->print();
424 * @brief Record end-of-execution stats
426 * Must be run when exiting an execution. Records various stats.
427 * @see struct execution_stats
429 void ModelChecker::record_stats()
432 if (!isfeasibleprefix())
433 stats.num_infeasible++;
434 else if (have_bug_reports())
435 stats.num_buggy_executions++;
436 else if (is_complete_execution())
437 stats.num_complete++;
439 stats.num_redundant++;
442 /** @brief Print execution stats */
443 void ModelChecker::print_stats() const
445 model_print("Number of complete, bug-free executions: %d\n", stats.num_complete);
446 model_print("Number of redundant executions: %d\n", stats.num_redundant);
447 model_print("Number of buggy executions: %d\n", stats.num_buggy_executions);
448 model_print("Number of infeasible executions: %d\n", stats.num_infeasible);
449 model_print("Total executions: %d\n", stats.num_total);
450 model_print("Total nodes created: %d\n", node_stack->get_total_nodes());
454 * @brief End-of-exeuction print
455 * @param printbugs Should any existing bugs be printed?
457 void ModelChecker::print_execution(bool printbugs) const
459 print_program_output();
461 if (DBG_ENABLED() || params.verbose) {
462 model_print("Earliest divergence point since last feasible execution:\n");
463 if (earliest_diverge)
464 earliest_diverge->print();
466 model_print("(Not set)\n");
472 /* Don't print invalid bugs */
481 * Queries the model-checker for more executions to explore and, if one
482 * exists, resets the model-checker state to execute a new execution.
484 * @return If there are more executions to explore, return true. Otherwise,
487 bool ModelChecker::next_execution()
490 /* Is this execution a feasible execution that's worth bug-checking? */
491 bool complete = isfeasibleprefix() && (is_complete_execution() ||
494 /* End-of-execution bug checks */
497 assert_bug("Deadlock detected");
505 if (DBG_ENABLED() || params.verbose || (complete && have_bug_reports()))
506 print_execution(complete);
508 clear_program_output();
511 earliest_diverge = NULL;
513 if ((diverge = get_next_backtrack()) == NULL)
517 model_print("Next execution will diverge at:\n");
521 reset_to_initial_state();
525 ModelAction * ModelChecker::get_last_conflict(ModelAction *act)
527 switch (act->get_type()) {
532 /* Optimization: relaxed operations don't need backtracking */
533 if (act->is_relaxed())
535 /* linear search: from most recent to oldest */
536 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
537 action_list_t::reverse_iterator rit;
538 for (rit = list->rbegin(); rit != list->rend(); rit++) {
539 ModelAction *prev = *rit;
540 if (prev->could_synchronize_with(act))
546 case ATOMIC_TRYLOCK: {
547 /* linear search: from most recent to oldest */
548 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
549 action_list_t::reverse_iterator rit;
550 for (rit = list->rbegin(); rit != list->rend(); rit++) {
551 ModelAction *prev = *rit;
552 if (act->is_conflicting_lock(prev))
557 case ATOMIC_UNLOCK: {
558 /* linear search: from most recent to oldest */
559 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
560 action_list_t::reverse_iterator rit;
561 for (rit = list->rbegin(); rit != list->rend(); rit++) {
562 ModelAction *prev = *rit;
563 if (!act->same_thread(prev) && prev->is_failed_trylock())
569 /* linear search: from most recent to oldest */
570 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
571 action_list_t::reverse_iterator rit;
572 for (rit = list->rbegin(); rit != list->rend(); rit++) {
573 ModelAction *prev = *rit;
574 if (!act->same_thread(prev) && prev->is_failed_trylock())
576 if (!act->same_thread(prev) && prev->is_notify())
582 case ATOMIC_NOTIFY_ALL:
583 case ATOMIC_NOTIFY_ONE: {
584 /* linear search: from most recent to oldest */
585 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
586 action_list_t::reverse_iterator rit;
587 for (rit = list->rbegin(); rit != list->rend(); rit++) {
588 ModelAction *prev = *rit;
589 if (!act->same_thread(prev) && prev->is_wait())
600 /** This method finds backtracking points where we should try to
601 * reorder the parameter ModelAction against.
603 * @param the ModelAction to find backtracking points for.
605 void ModelChecker::set_backtracking(ModelAction *act)
607 Thread *t = get_thread(act);
608 ModelAction *prev = get_last_conflict(act);
612 Node *node = prev->get_node()->get_parent();
614 int low_tid, high_tid;
615 if (node->enabled_status(t->get_id()) == THREAD_ENABLED) {
616 low_tid = id_to_int(act->get_tid());
617 high_tid = low_tid + 1;
620 high_tid = get_num_threads();
623 for (int i = low_tid; i < high_tid; i++) {
624 thread_id_t tid = int_to_id(i);
626 /* Make sure this thread can be enabled here. */
627 if (i >= node->get_num_threads())
630 /* Don't backtrack into a point where the thread is disabled or sleeping. */
631 if (node->enabled_status(tid) != THREAD_ENABLED)
634 /* Check if this has been explored already */
635 if (node->has_been_explored(tid))
638 /* See if fairness allows */
639 if (model->params.fairwindow != 0 && !node->has_priority(tid)) {
641 for (int t = 0; t < node->get_num_threads(); t++) {
642 thread_id_t tother = int_to_id(t);
643 if (node->is_enabled(tother) && node->has_priority(tother)) {
651 /* Cache the latest backtracking point */
652 set_latest_backtrack(prev);
654 /* If this is a new backtracking point, mark the tree */
655 if (!node->set_backtrack(tid))
657 DEBUG("Setting backtrack: conflict = %d, instead tid = %d\n",
658 id_to_int(prev->get_tid()),
659 id_to_int(t->get_id()));
668 * @brief Cache the a backtracking point as the "most recent", if eligible
670 * Note that this does not prepare the NodeStack for this backtracking
671 * operation, it only caches the action on a per-execution basis
673 * @param act The operation at which we should explore a different next action
674 * (i.e., backtracking point)
675 * @return True, if this action is now the most recent backtracking point;
678 bool ModelChecker::set_latest_backtrack(ModelAction *act)
680 if (!priv->next_backtrack || *act > *priv->next_backtrack) {
681 priv->next_backtrack = act;
688 * Returns last backtracking point. The model checker will explore a different
689 * path for this point in the next execution.
690 * @return The ModelAction at which the next execution should diverge.
692 ModelAction * ModelChecker::get_next_backtrack()
694 ModelAction *next = priv->next_backtrack;
695 priv->next_backtrack = NULL;
700 * Processes a read or rmw model action.
701 * @param curr is the read model action to process.
702 * @param second_part_of_rmw is boolean that is true is this is the second action of a rmw.
703 * @return True if processing this read updates the mo_graph.
705 bool ModelChecker::process_read(ModelAction *curr, bool second_part_of_rmw)
707 uint64_t value = VALUE_NONE;
708 bool updated = false;
710 const ModelAction *reads_from = curr->get_node()->get_read_from();
711 if (reads_from != NULL) {
712 mo_graph->startChanges();
714 value = reads_from->get_value();
715 bool r_status = false;
717 if (!second_part_of_rmw) {
718 check_recency(curr, reads_from);
719 r_status = r_modification_order(curr, reads_from);
722 if (!second_part_of_rmw && is_infeasible() && (curr->get_node()->increment_read_from() || curr->get_node()->increment_future_value())) {
723 mo_graph->rollbackChanges();
724 priv->too_many_reads = false;
728 read_from(curr, reads_from);
729 mo_graph->commitChanges();
730 mo_check_promises(curr, true);
733 } else if (!second_part_of_rmw) {
734 /* Read from future value */
735 struct future_value fv = curr->get_node()->get_future_value();
736 Promise *promise = new Promise(curr, fv);
738 curr->set_read_from_promise(promise);
739 promises->push_back(promise);
740 mo_graph->startChanges();
741 updated = r_modification_order(curr, promise);
742 mo_graph->commitChanges();
744 get_thread(curr)->set_return_value(value);
750 * Processes a lock, trylock, or unlock model action. @param curr is
751 * the read model action to process.
753 * The try lock operation checks whether the lock is taken. If not,
754 * it falls to the normal lock operation case. If so, it returns
757 * The lock operation has already been checked that it is enabled, so
758 * it just grabs the lock and synchronizes with the previous unlock.
760 * The unlock operation has to re-enable all of the threads that are
761 * waiting on the lock.
763 * @return True if synchronization was updated; false otherwise
765 bool ModelChecker::process_mutex(ModelAction *curr)
767 std::mutex *mutex = NULL;
768 struct std::mutex_state *state = NULL;
770 if (curr->is_trylock() || curr->is_lock() || curr->is_unlock()) {
771 mutex = (std::mutex *)curr->get_location();
772 state = mutex->get_state();
773 } else if (curr->is_wait()) {
774 mutex = (std::mutex *)curr->get_value();
775 state = mutex->get_state();
778 switch (curr->get_type()) {
779 case ATOMIC_TRYLOCK: {
780 bool success = !state->islocked;
781 curr->set_try_lock(success);
783 get_thread(curr)->set_return_value(0);
786 get_thread(curr)->set_return_value(1);
788 //otherwise fall into the lock case
790 if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock)
791 assert_bug("Lock access before initialization");
792 state->islocked = true;
793 ModelAction *unlock = get_last_unlock(curr);
794 //synchronize with the previous unlock statement
795 if (unlock != NULL) {
796 curr->synchronize_with(unlock);
801 case ATOMIC_UNLOCK: {
803 state->islocked = false;
804 //wake up the other threads
805 action_list_t *waiters = get_safe_ptr_action(lock_waiters_map, curr->get_location());
806 //activate all the waiting threads
807 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
808 scheduler->wake(get_thread(*rit));
815 state->islocked = false;
816 //wake up the other threads
817 action_list_t *waiters = get_safe_ptr_action(lock_waiters_map, (void *) curr->get_value());
818 //activate all the waiting threads
819 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
820 scheduler->wake(get_thread(*rit));
823 //check whether we should go to sleep or not...simulate spurious failures
824 if (curr->get_node()->get_misc() == 0) {
825 get_safe_ptr_action(condvar_waiters_map, curr->get_location())->push_back(curr);
827 scheduler->sleep(get_thread(curr));
831 case ATOMIC_NOTIFY_ALL: {
832 action_list_t *waiters = get_safe_ptr_action(condvar_waiters_map, curr->get_location());
833 //activate all the waiting threads
834 for (action_list_t::iterator rit = waiters->begin(); rit != waiters->end(); rit++) {
835 scheduler->wake(get_thread(*rit));
840 case ATOMIC_NOTIFY_ONE: {
841 action_list_t *waiters = get_safe_ptr_action(condvar_waiters_map, curr->get_location());
842 int wakeupthread = curr->get_node()->get_misc();
843 action_list_t::iterator it = waiters->begin();
844 advance(it, wakeupthread);
845 scheduler->wake(get_thread(*it));
856 void ModelChecker::add_future_value(const ModelAction *writer, ModelAction *reader)
858 /* Do more ambitious checks now that mo is more complete */
859 if (mo_may_allow(writer, reader)) {
860 Node *node = reader->get_node();
862 /* Find an ancestor thread which exists at the time of the reader */
863 Thread *write_thread = get_thread(writer);
864 while (id_to_int(write_thread->get_id()) >= node->get_num_threads())
865 write_thread = write_thread->get_parent();
867 struct future_value fv = {
869 writer->get_seq_number() + params.maxfuturedelay,
870 write_thread->get_id(),
872 if (node->add_future_value(fv))
873 set_latest_backtrack(reader);
878 * Process a write ModelAction
879 * @param curr The ModelAction to process
880 * @return True if the mo_graph was updated or promises were resolved
882 bool ModelChecker::process_write(ModelAction *curr)
884 bool updated_mod_order = w_modification_order(curr);
885 bool updated_promises = resolve_promises(curr);
887 if (promises->size() == 0) {
888 for (unsigned int i = 0; i < futurevalues->size(); i++) {
889 struct PendingFutureValue pfv = (*futurevalues)[i];
890 add_future_value(pfv.writer, pfv.act);
892 futurevalues->clear();
895 mo_graph->commitChanges();
896 mo_check_promises(curr, false);
898 get_thread(curr)->set_return_value(VALUE_NONE);
899 return updated_mod_order || updated_promises;
903 * Process a fence ModelAction
904 * @param curr The ModelAction to process
905 * @return True if synchronization was updated
907 bool ModelChecker::process_fence(ModelAction *curr)
910 * fence-relaxed: no-op
911 * fence-release: only log the occurence (not in this function), for
912 * use in later synchronization
913 * fence-acquire (this function): search for hypothetical release
916 bool updated = false;
917 if (curr->is_acquire()) {
918 action_list_t *list = action_trace;
919 action_list_t::reverse_iterator rit;
920 /* Find X : is_read(X) && X --sb-> curr */
921 for (rit = list->rbegin(); rit != list->rend(); rit++) {
922 ModelAction *act = *rit;
925 if (act->get_tid() != curr->get_tid())
927 /* Stop at the beginning of the thread */
928 if (act->is_thread_start())
930 /* Stop once we reach a prior fence-acquire */
931 if (act->is_fence() && act->is_acquire())
935 /* read-acquire will find its own release sequences */
936 if (act->is_acquire())
939 /* Establish hypothetical release sequences */
940 rel_heads_list_t release_heads;
941 get_release_seq_heads(curr, act, &release_heads);
942 for (unsigned int i = 0; i < release_heads.size(); i++)
943 if (!curr->synchronize_with(release_heads[i]))
944 set_bad_synchronization();
945 if (release_heads.size() != 0)
953 * @brief Process the current action for thread-related activity
955 * Performs current-action processing for a THREAD_* ModelAction. Proccesses
956 * may include setting Thread status, completing THREAD_FINISH/THREAD_JOIN
957 * synchronization, etc. This function is a no-op for non-THREAD actions
958 * (e.g., ATOMIC_{READ,WRITE,RMW,LOCK}, etc.)
960 * @param curr The current action
961 * @return True if synchronization was updated or a thread completed
963 bool ModelChecker::process_thread_action(ModelAction *curr)
965 bool updated = false;
967 switch (curr->get_type()) {
968 case THREAD_CREATE: {
969 thrd_t *thrd = (thrd_t *)curr->get_location();
970 struct thread_params *params = (struct thread_params *)curr->get_value();
971 Thread *th = new Thread(thrd, params->func, params->arg);
973 th->set_creation(curr);
974 /* Promises can be satisfied by children */
975 for (unsigned int i = 0; i < promises->size(); i++) {
976 Promise *promise = (*promises)[i];
977 if (promise->thread_is_available(curr->get_tid()))
978 promise->add_thread(th->get_id());
983 Thread *blocking = curr->get_thread_operand();
984 ModelAction *act = get_last_action(blocking->get_id());
985 curr->synchronize_with(act);
986 updated = true; /* trigger rel-seq checks */
989 case THREAD_FINISH: {
990 Thread *th = get_thread(curr);
991 while (!th->wait_list_empty()) {
992 ModelAction *act = th->pop_wait_list();
993 scheduler->wake(get_thread(act));
996 /* Completed thread can't satisfy promises */
997 for (unsigned int i = 0; i < promises->size(); i++) {
998 Promise *promise = (*promises)[i];
999 if (promise->thread_is_available(th->get_id()))
1000 if (promise->eliminate_thread(th->get_id()))
1001 priv->failed_promise = true;
1003 updated = true; /* trigger rel-seq checks */
1006 case THREAD_START: {
1007 check_promises(curr->get_tid(), NULL, curr->get_cv());
1018 * @brief Process the current action for release sequence fixup activity
1020 * Performs model-checker release sequence fixups for the current action,
1021 * forcing a single pending release sequence to break (with a given, potential
1022 * "loose" write) or to complete (i.e., synchronize). If a pending release
1023 * sequence forms a complete release sequence, then we must perform the fixup
1024 * synchronization, mo_graph additions, etc.
1026 * @param curr The current action; must be a release sequence fixup action
1027 * @param work_queue The work queue to which to add work items as they are
1030 void ModelChecker::process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue)
1032 const ModelAction *write = curr->get_node()->get_relseq_break();
1033 struct release_seq *sequence = pending_rel_seqs->back();
1034 pending_rel_seqs->pop_back();
1036 ModelAction *acquire = sequence->acquire;
1037 const ModelAction *rf = sequence->rf;
1038 const ModelAction *release = sequence->release;
1042 ASSERT(release->same_thread(rf));
1044 if (write == NULL) {
1046 * @todo Forcing a synchronization requires that we set
1047 * modification order constraints. For instance, we can't allow
1048 * a fixup sequence in which two separate read-acquire
1049 * operations read from the same sequence, where the first one
1050 * synchronizes and the other doesn't. Essentially, we can't
1051 * allow any writes to insert themselves between 'release' and
1055 /* Must synchronize */
1056 if (!acquire->synchronize_with(release)) {
1057 set_bad_synchronization();
1060 /* Re-check all pending release sequences */
1061 work_queue->push_back(CheckRelSeqWorkEntry(NULL));
1062 /* Re-check act for mo_graph edges */
1063 work_queue->push_back(MOEdgeWorkEntry(acquire));
1065 /* propagate synchronization to later actions */
1066 action_list_t::reverse_iterator rit = action_trace->rbegin();
1067 for (; (*rit) != acquire; rit++) {
1068 ModelAction *propagate = *rit;
1069 if (acquire->happens_before(propagate)) {
1070 propagate->synchronize_with(acquire);
1071 /* Re-check 'propagate' for mo_graph edges */
1072 work_queue->push_back(MOEdgeWorkEntry(propagate));
1076 /* Break release sequence with new edges:
1077 * release --mo--> write --mo--> rf */
1078 mo_graph->addEdge(release, write);
1079 mo_graph->addEdge(write, rf);
1082 /* See if we have realized a data race */
1087 * Initialize the current action by performing one or more of the following
1088 * actions, as appropriate: merging RMWR and RMWC/RMW actions, stepping forward
1089 * in the NodeStack, manipulating backtracking sets, allocating and
1090 * initializing clock vectors, and computing the promises to fulfill.
1092 * @param curr The current action, as passed from the user context; may be
1093 * freed/invalidated after the execution of this function, with a different
1094 * action "returned" its place (pass-by-reference)
1095 * @return True if curr is a newly-explored action; false otherwise
1097 bool ModelChecker::initialize_curr_action(ModelAction **curr)
1099 ModelAction *newcurr;
1101 if ((*curr)->is_rmwc() || (*curr)->is_rmw()) {
1102 newcurr = process_rmw(*curr);
1105 if (newcurr->is_rmw())
1106 compute_promises(newcurr);
1112 (*curr)->set_seq_number(get_next_seq_num());
1114 newcurr = node_stack->explore_action(*curr, scheduler->get_enabled_array());
1116 /* First restore type and order in case of RMW operation */
1117 if ((*curr)->is_rmwr())
1118 newcurr->copy_typeandorder(*curr);
1120 ASSERT((*curr)->get_location() == newcurr->get_location());
1121 newcurr->copy_from_new(*curr);
1123 /* Discard duplicate ModelAction; use action from NodeStack */
1126 /* Always compute new clock vector */
1127 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
1130 return false; /* Action was explored previously */
1134 /* Always compute new clock vector */
1135 newcurr->create_cv(get_parent_action(newcurr->get_tid()));
1137 /* Assign most recent release fence */
1138 newcurr->set_last_fence_release(get_last_fence_release(newcurr->get_tid()));
1141 * Perform one-time actions when pushing new ModelAction onto
1144 if (newcurr->is_write())
1145 compute_promises(newcurr);
1146 else if (newcurr->is_relseq_fixup())
1147 compute_relseq_breakwrites(newcurr);
1148 else if (newcurr->is_wait())
1149 newcurr->get_node()->set_misc_max(2);
1150 else if (newcurr->is_notify_one()) {
1151 newcurr->get_node()->set_misc_max(get_safe_ptr_action(condvar_waiters_map, newcurr->get_location())->size());
1153 return true; /* This was a new ModelAction */
1158 * @brief Establish reads-from relation between two actions
1160 * Perform basic operations involved with establishing a concrete rf relation,
1161 * including setting the ModelAction data and checking for release sequences.
1163 * @param act The action that is reading (must be a read)
1164 * @param rf The action from which we are reading (must be a write)
1166 * @return True if this read established synchronization
1168 bool ModelChecker::read_from(ModelAction *act, const ModelAction *rf)
1170 act->set_read_from(rf);
1171 if (rf != NULL && act->is_acquire()) {
1172 rel_heads_list_t release_heads;
1173 get_release_seq_heads(act, act, &release_heads);
1174 int num_heads = release_heads.size();
1175 for (unsigned int i = 0; i < release_heads.size(); i++)
1176 if (!act->synchronize_with(release_heads[i])) {
1177 set_bad_synchronization();
1180 return num_heads > 0;
1186 * @brief Check whether a model action is enabled.
1188 * Checks whether a lock or join operation would be successful (i.e., is the
1189 * lock already locked, or is the joined thread already complete). If not, put
1190 * the action in a waiter list.
1192 * @param curr is the ModelAction to check whether it is enabled.
1193 * @return a bool that indicates whether the action is enabled.
1195 bool ModelChecker::check_action_enabled(ModelAction *curr) {
1196 if (curr->is_lock()) {
1197 std::mutex *lock = (std::mutex *)curr->get_location();
1198 struct std::mutex_state *state = lock->get_state();
1199 if (state->islocked) {
1200 //Stick the action in the appropriate waiting queue
1201 get_safe_ptr_action(lock_waiters_map, curr->get_location())->push_back(curr);
1204 } else if (curr->get_type() == THREAD_JOIN) {
1205 Thread *blocking = (Thread *)curr->get_location();
1206 if (!blocking->is_complete()) {
1207 blocking->push_wait_list(curr);
1216 * This is the heart of the model checker routine. It performs model-checking
1217 * actions corresponding to a given "current action." Among other processes, it
1218 * calculates reads-from relationships, updates synchronization clock vectors,
1219 * forms a memory_order constraints graph, and handles replay/backtrack
1220 * execution when running permutations of previously-observed executions.
1222 * @param curr The current action to process
1223 * @return The ModelAction that is actually executed; may be different than
1224 * curr; may be NULL, if the current action is not enabled to run
1226 ModelAction * ModelChecker::check_current_action(ModelAction *curr)
1229 bool second_part_of_rmw = curr->is_rmwc() || curr->is_rmw();
1231 if (!check_action_enabled(curr)) {
1232 /* Make the execution look like we chose to run this action
1233 * much later, when a lock/join can succeed */
1234 get_thread(curr)->set_pending(curr);
1235 scheduler->sleep(get_thread(curr));
1239 bool newly_explored = initialize_curr_action(&curr);
1245 wake_up_sleeping_actions(curr);
1247 /* Add the action to lists before any other model-checking tasks */
1248 if (!second_part_of_rmw)
1249 add_action_to_lists(curr);
1251 /* Build may_read_from set for newly-created actions */
1252 if (newly_explored && curr->is_read())
1253 build_reads_from_past(curr);
1255 /* Initialize work_queue with the "current action" work */
1256 work_queue_t work_queue(1, CheckCurrWorkEntry(curr));
1257 while (!work_queue.empty() && !has_asserted()) {
1258 WorkQueueEntry work = work_queue.front();
1259 work_queue.pop_front();
1261 switch (work.type) {
1262 case WORK_CHECK_CURR_ACTION: {
1263 ModelAction *act = work.action;
1264 bool update = false; /* update this location's release seq's */
1265 bool update_all = false; /* update all release seq's */
1267 if (process_thread_action(curr))
1270 if (act->is_read() && process_read(act, second_part_of_rmw))
1273 if (act->is_write() && process_write(act))
1276 if (act->is_fence() && process_fence(act))
1279 if (act->is_mutex_op() && process_mutex(act))
1282 if (act->is_relseq_fixup())
1283 process_relseq_fixup(curr, &work_queue);
1286 work_queue.push_back(CheckRelSeqWorkEntry(NULL));
1288 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
1291 case WORK_CHECK_RELEASE_SEQ:
1292 resolve_release_sequences(work.location, &work_queue);
1294 case WORK_CHECK_MO_EDGES: {
1295 /** @todo Complete verification of work_queue */
1296 ModelAction *act = work.action;
1297 bool updated = false;
1299 if (act->is_read()) {
1300 const ModelAction *rf = act->get_reads_from();
1301 const Promise *promise = act->get_reads_from_promise();
1303 if (r_modification_order(act, rf))
1305 } else if (promise) {
1306 if (r_modification_order(act, promise))
1310 if (act->is_write()) {
1311 if (w_modification_order(act))
1314 mo_graph->commitChanges();
1317 work_queue.push_back(CheckRelSeqWorkEntry(act->get_location()));
1326 check_curr_backtracking(curr);
1327 set_backtracking(curr);
1331 void ModelChecker::check_curr_backtracking(ModelAction *curr)
1333 Node *currnode = curr->get_node();
1334 Node *parnode = currnode->get_parent();
1336 if ((parnode && !parnode->backtrack_empty()) ||
1337 !currnode->misc_empty() ||
1338 !currnode->read_from_empty() ||
1339 !currnode->future_value_empty() ||
1340 !currnode->promise_empty() ||
1341 !currnode->relseq_break_empty()) {
1342 set_latest_backtrack(curr);
1346 bool ModelChecker::promises_expired() const
1348 for (unsigned int i = 0; i < promises->size(); i++) {
1349 Promise *promise = (*promises)[i];
1350 if (promise->get_expiration() < priv->used_sequence_numbers)
1357 * This is the strongest feasibility check available.
1358 * @return whether the current trace (partial or complete) must be a prefix of
1361 bool ModelChecker::isfeasibleprefix() const
1363 return pending_rel_seqs->size() == 0 && is_feasible_prefix_ignore_relseq();
1367 * Print disagnostic information about an infeasible execution
1368 * @param prefix A string to prefix the output with; if NULL, then a default
1369 * message prefix will be provided
1371 void ModelChecker::print_infeasibility(const char *prefix) const
1375 if (mo_graph->checkForCycles())
1376 ptr += sprintf(ptr, "[mo cycle]");
1377 if (priv->failed_promise)
1378 ptr += sprintf(ptr, "[failed promise]");
1379 if (priv->too_many_reads)
1380 ptr += sprintf(ptr, "[too many reads]");
1381 if (priv->no_valid_reads)
1382 ptr += sprintf(ptr, "[no valid reads-from]");
1383 if (priv->bad_synchronization)
1384 ptr += sprintf(ptr, "[bad sw ordering]");
1385 if (promises_expired())
1386 ptr += sprintf(ptr, "[promise expired]");
1387 if (promises->size() != 0)
1388 ptr += sprintf(ptr, "[unresolved promise]");
1390 model_print("%s: %s\n", prefix ? prefix : "Infeasible", buf);
1394 * Returns whether the current completed trace is feasible, except for pending
1395 * release sequences.
1397 bool ModelChecker::is_feasible_prefix_ignore_relseq() const
1399 return !is_infeasible() && promises->size() == 0;
1403 * Check if the current partial trace is infeasible. Does not check any
1404 * end-of-execution flags, which might rule out the execution. Thus, this is
1405 * useful only for ruling an execution as infeasible.
1406 * @return whether the current partial trace is infeasible.
1408 bool ModelChecker::is_infeasible() const
1410 return mo_graph->checkForCycles() ||
1411 priv->no_valid_reads ||
1412 priv->failed_promise ||
1413 priv->too_many_reads ||
1414 priv->bad_synchronization ||
1418 /** Close out a RMWR by converting previous RMWR into a RMW or READ. */
1419 ModelAction * ModelChecker::process_rmw(ModelAction *act) {
1420 ModelAction *lastread = get_last_action(act->get_tid());
1421 lastread->process_rmw(act);
1422 if (act->is_rmw()) {
1423 if (lastread->get_reads_from())
1424 mo_graph->addRMWEdge(lastread->get_reads_from(), lastread);
1426 mo_graph->addRMWEdge(lastread->get_reads_from_promise(), lastread);
1427 mo_graph->commitChanges();
1433 * Checks whether a thread has read from the same write for too many times
1434 * without seeing the effects of a later write.
1437 * 1) there must a different write that we could read from that would satisfy the modification order,
1438 * 2) we must have read from the same value in excess of maxreads times, and
1439 * 3) that other write must have been in the reads_from set for maxreads times.
1441 * If so, we decide that the execution is no longer feasible.
1443 void ModelChecker::check_recency(ModelAction *curr, const ModelAction *rf)
1445 if (params.maxreads != 0) {
1446 if (curr->get_node()->get_read_from_size() <= 1)
1448 //Must make sure that execution is currently feasible... We could
1449 //accidentally clear by rolling back
1450 if (is_infeasible())
1452 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1453 int tid = id_to_int(curr->get_tid());
1456 if ((int)thrd_lists->size() <= tid)
1458 action_list_t *list = &(*thrd_lists)[tid];
1460 action_list_t::reverse_iterator rit = list->rbegin();
1461 /* Skip past curr */
1462 for (; (*rit) != curr; rit++)
1464 /* go past curr now */
1467 action_list_t::reverse_iterator ritcopy = rit;
1468 //See if we have enough reads from the same value
1470 for (; count < params.maxreads; rit++, count++) {
1471 if (rit == list->rend())
1473 ModelAction *act = *rit;
1474 if (!act->is_read())
1477 if (act->get_reads_from() != rf)
1479 if (act->get_node()->get_read_from_size() <= 1)
1482 for (int i = 0; i < curr->get_node()->get_read_from_size(); i++) {
1484 const ModelAction *write = curr->get_node()->get_read_from_at(i);
1486 /* Need a different write */
1490 /* Test to see whether this is a feasible write to read from */
1491 /** NOTE: all members of read-from set should be
1492 * feasible, so we no longer check it here **/
1496 bool feasiblewrite = true;
1497 //new we need to see if this write works for everyone
1499 for (int loop = count; loop > 0; loop--, rit++) {
1500 ModelAction *act = *rit;
1501 bool foundvalue = false;
1502 for (int j = 0; j < act->get_node()->get_read_from_size(); j++) {
1503 if (act->get_node()->get_read_from_at(j) == write) {
1509 feasiblewrite = false;
1513 if (feasiblewrite) {
1514 priv->too_many_reads = true;
1522 * Updates the mo_graph with the constraints imposed from the current
1525 * Basic idea is the following: Go through each other thread and find
1526 * the last action that happened before our read. Two cases:
1528 * (1) The action is a write => that write must either occur before
1529 * the write we read from or be the write we read from.
1531 * (2) The action is a read => the write that that action read from
1532 * must occur before the write we read from or be the same write.
1534 * @param curr The current action. Must be a read.
1535 * @param rf The ModelAction or Promise that curr reads from. Must be a write.
1536 * @return True if modification order edges were added; false otherwise
1538 template <typename rf_type>
1539 bool ModelChecker::r_modification_order(ModelAction *curr, const rf_type *rf)
1541 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1544 ASSERT(curr->is_read());
1546 /* Last SC fence in the current thread */
1547 ModelAction *last_sc_fence_local = get_last_seq_cst_fence(curr->get_tid(), NULL);
1549 /* Iterate over all threads */
1550 for (i = 0; i < thrd_lists->size(); i++) {
1551 /* Last SC fence in thread i */
1552 ModelAction *last_sc_fence_thread_local = NULL;
1553 if (int_to_id((int)i) != curr->get_tid())
1554 last_sc_fence_thread_local = get_last_seq_cst_fence(int_to_id(i), NULL);
1556 /* Last SC fence in thread i, before last SC fence in current thread */
1557 ModelAction *last_sc_fence_thread_before = NULL;
1558 if (last_sc_fence_local)
1559 last_sc_fence_thread_before = get_last_seq_cst_fence(int_to_id(i), last_sc_fence_local);
1561 /* Iterate over actions in thread, starting from most recent */
1562 action_list_t *list = &(*thrd_lists)[i];
1563 action_list_t::reverse_iterator rit;
1564 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1565 ModelAction *act = *rit;
1567 if (act->is_write() && !act->equals(rf) && act != curr) {
1568 /* C++, Section 29.3 statement 5 */
1569 if (curr->is_seqcst() && last_sc_fence_thread_local &&
1570 *act < *last_sc_fence_thread_local) {
1571 added = mo_graph->addEdge(act, rf) || added;
1574 /* C++, Section 29.3 statement 4 */
1575 else if (act->is_seqcst() && last_sc_fence_local &&
1576 *act < *last_sc_fence_local) {
1577 added = mo_graph->addEdge(act, rf) || added;
1580 /* C++, Section 29.3 statement 6 */
1581 else if (last_sc_fence_thread_before &&
1582 *act < *last_sc_fence_thread_before) {
1583 added = mo_graph->addEdge(act, rf) || added;
1589 * Include at most one act per-thread that "happens
1590 * before" curr. Don't consider reflexively.
1592 if (act->happens_before(curr) && act != curr) {
1593 if (act->is_write()) {
1594 if (!act->equals(rf)) {
1595 added = mo_graph->addEdge(act, rf) || added;
1598 const ModelAction *prevreadfrom = act->get_reads_from();
1599 //if the previous read is unresolved, keep going...
1600 if (prevreadfrom == NULL)
1603 if (!prevreadfrom->equals(rf)) {
1604 added = mo_graph->addEdge(prevreadfrom, rf) || added;
1613 * All compatible, thread-exclusive promises must be ordered after any
1614 * concrete loads from the same thread
1616 for (unsigned int i = 0; i < promises->size(); i++)
1617 if ((*promises)[i]->is_compatible_exclusive(curr))
1618 added = mo_graph->addEdge(rf, (*promises)[i]) || added;
1624 * Updates the mo_graph with the constraints imposed from the current write.
1626 * Basic idea is the following: Go through each other thread and find
1627 * the lastest action that happened before our write. Two cases:
1629 * (1) The action is a write => that write must occur before
1632 * (2) The action is a read => the write that that action read from
1633 * must occur before the current write.
1635 * This method also handles two other issues:
1637 * (I) Sequential Consistency: Making sure that if the current write is
1638 * seq_cst, that it occurs after the previous seq_cst write.
1640 * (II) Sending the write back to non-synchronizing reads.
1642 * @param curr The current action. Must be a write.
1643 * @return True if modification order edges were added; false otherwise
1645 bool ModelChecker::w_modification_order(ModelAction *curr)
1647 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
1650 ASSERT(curr->is_write());
1652 if (curr->is_seqcst()) {
1653 /* We have to at least see the last sequentially consistent write,
1654 so we are initialized. */
1655 ModelAction *last_seq_cst = get_last_seq_cst_write(curr);
1656 if (last_seq_cst != NULL) {
1657 added = mo_graph->addEdge(last_seq_cst, curr) || added;
1661 /* Last SC fence in the current thread */
1662 ModelAction *last_sc_fence_local = get_last_seq_cst_fence(curr->get_tid(), NULL);
1664 /* Iterate over all threads */
1665 for (i = 0; i < thrd_lists->size(); i++) {
1666 /* Last SC fence in thread i, before last SC fence in current thread */
1667 ModelAction *last_sc_fence_thread_before = NULL;
1668 if (last_sc_fence_local && int_to_id((int)i) != curr->get_tid())
1669 last_sc_fence_thread_before = get_last_seq_cst_fence(int_to_id(i), last_sc_fence_local);
1671 /* Iterate over actions in thread, starting from most recent */
1672 action_list_t *list = &(*thrd_lists)[i];
1673 action_list_t::reverse_iterator rit;
1674 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1675 ModelAction *act = *rit;
1678 * 1) If RMW and it actually read from something, then we
1679 * already have all relevant edges, so just skip to next
1682 * 2) If RMW and it didn't read from anything, we should
1683 * whatever edge we can get to speed up convergence.
1685 * 3) If normal write, we need to look at earlier actions, so
1686 * continue processing list.
1688 if (curr->is_rmw()) {
1689 if (curr->get_reads_from() != NULL)
1697 /* C++, Section 29.3 statement 7 */
1698 if (last_sc_fence_thread_before && act->is_write() &&
1699 *act < *last_sc_fence_thread_before) {
1700 added = mo_graph->addEdge(act, curr) || added;
1705 * Include at most one act per-thread that "happens
1708 if (act->happens_before(curr)) {
1710 * Note: if act is RMW, just add edge:
1712 * The following edge should be handled elsewhere:
1713 * readfrom(act) --mo--> act
1715 if (act->is_write())
1716 added = mo_graph->addEdge(act, curr) || added;
1717 else if (act->is_read()) {
1718 //if previous read accessed a null, just keep going
1719 if (act->get_reads_from() == NULL)
1721 added = mo_graph->addEdge(act->get_reads_from(), curr) || added;
1724 } else if (act->is_read() && !act->could_synchronize_with(curr) &&
1725 !act->same_thread(curr)) {
1726 /* We have an action that:
1727 (1) did not happen before us
1728 (2) is a read and we are a write
1729 (3) cannot synchronize with us
1730 (4) is in a different thread
1732 that read could potentially read from our write. Note that
1733 these checks are overly conservative at this point, we'll
1734 do more checks before actually removing the
1738 if (thin_air_constraint_may_allow(curr, act)) {
1739 if (!is_infeasible())
1740 futurevalues->push_back(PendingFutureValue(curr, act));
1741 else if (curr->is_rmw() && act->is_rmw() && curr->get_reads_from() && curr->get_reads_from() == act->get_reads_from())
1742 add_future_value(curr, act);
1749 * All compatible, thread-exclusive promises must be ordered after any
1750 * concrete stores to the same thread, or else they can be merged with
1753 for (unsigned int i = 0; i < promises->size(); i++)
1754 if ((*promises)[i]->is_compatible_exclusive(curr))
1755 added = mo_graph->addEdge(curr, (*promises)[i]) || added;
1760 /** Arbitrary reads from the future are not allowed. Section 29.3
1761 * part 9 places some constraints. This method checks one result of constraint
1762 * constraint. Others require compiler support. */
1763 bool ModelChecker::thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader)
1765 if (!writer->is_rmw())
1768 if (!reader->is_rmw())
1771 for (const ModelAction *search = writer->get_reads_from(); search != NULL; search = search->get_reads_from()) {
1772 if (search == reader)
1774 if (search->get_tid() == reader->get_tid() &&
1775 search->happens_before(reader))
1783 * Arbitrary reads from the future are not allowed. Section 29.3 part 9 places
1784 * some constraints. This method checks one the following constraint (others
1785 * require compiler support):
1787 * If X --hb-> Y --mo-> Z, then X should not read from Z.
1789 bool ModelChecker::mo_may_allow(const ModelAction *writer, const ModelAction *reader)
1791 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, reader->get_location());
1793 /* Iterate over all threads */
1794 for (i = 0; i < thrd_lists->size(); i++) {
1795 const ModelAction *write_after_read = NULL;
1797 /* Iterate over actions in thread, starting from most recent */
1798 action_list_t *list = &(*thrd_lists)[i];
1799 action_list_t::reverse_iterator rit;
1800 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1801 ModelAction *act = *rit;
1803 /* Don't disallow due to act == reader */
1804 if (!reader->happens_before(act) || reader == act)
1806 else if (act->is_write())
1807 write_after_read = act;
1808 else if (act->is_read() && act->get_reads_from() != NULL)
1809 write_after_read = act->get_reads_from();
1812 if (write_after_read && write_after_read != writer && mo_graph->checkReachable(write_after_read, writer))
1819 * Finds the head(s) of the release sequence(s) containing a given ModelAction.
1820 * The ModelAction under consideration is expected to be taking part in
1821 * release/acquire synchronization as an object of the "reads from" relation.
1822 * Note that this can only provide release sequence support for RMW chains
1823 * which do not read from the future, as those actions cannot be traced until
1824 * their "promise" is fulfilled. Similarly, we may not even establish the
1825 * presence of a release sequence with certainty, as some modification order
1826 * constraints may be decided further in the future. Thus, this function
1827 * "returns" two pieces of data: a pass-by-reference vector of @a release_heads
1828 * and a boolean representing certainty.
1830 * @param rf The action that might be part of a release sequence. Must be a
1832 * @param release_heads A pass-by-reference style return parameter. After
1833 * execution of this function, release_heads will contain the heads of all the
1834 * relevant release sequences, if any exists with certainty
1835 * @param pending A pass-by-reference style return parameter which is only used
1836 * when returning false (i.e., uncertain). Returns most information regarding
1837 * an uncertain release sequence, including any write operations that might
1838 * break the sequence.
1839 * @return true, if the ModelChecker is certain that release_heads is complete;
1842 bool ModelChecker::release_seq_heads(const ModelAction *rf,
1843 rel_heads_list_t *release_heads,
1844 struct release_seq *pending) const
1846 /* Only check for release sequences if there are no cycles */
1847 if (mo_graph->checkForCycles())
1851 ASSERT(rf->is_write());
1853 if (rf->is_release())
1854 release_heads->push_back(rf);
1855 else if (rf->get_last_fence_release())
1856 release_heads->push_back(rf->get_last_fence_release());
1858 break; /* End of RMW chain */
1860 /** @todo Need to be smarter here... In the linux lock
1861 * example, this will run to the beginning of the program for
1863 /** @todo The way to be smarter here is to keep going until 1
1864 * thread has a release preceded by an acquire and you've seen
1867 /* acq_rel RMW is a sufficient stopping condition */
1868 if (rf->is_acquire() && rf->is_release())
1869 return true; /* complete */
1871 rf = rf->get_reads_from();
1874 /* read from future: need to settle this later */
1876 return false; /* incomplete */
1879 if (rf->is_release())
1880 return true; /* complete */
1882 /* else relaxed write
1883 * - check for fence-release in the same thread (29.8, stmt. 3)
1884 * - check modification order for contiguous subsequence
1885 * -> rf must be same thread as release */
1887 const ModelAction *fence_release = rf->get_last_fence_release();
1888 /* Synchronize with a fence-release unconditionally; we don't need to
1889 * find any more "contiguous subsequence..." for it */
1891 release_heads->push_back(fence_release);
1893 int tid = id_to_int(rf->get_tid());
1894 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, rf->get_location());
1895 action_list_t *list = &(*thrd_lists)[tid];
1896 action_list_t::const_reverse_iterator rit;
1898 /* Find rf in the thread list */
1899 rit = std::find(list->rbegin(), list->rend(), rf);
1900 ASSERT(rit != list->rend());
1902 /* Find the last {write,fence}-release */
1903 for (; rit != list->rend(); rit++) {
1904 if (fence_release && *(*rit) < *fence_release)
1906 if ((*rit)->is_release())
1909 if (rit == list->rend()) {
1910 /* No write-release in this thread */
1911 return true; /* complete */
1912 } else if (fence_release && *(*rit) < *fence_release) {
1913 /* The fence-release is more recent (and so, "stronger") than
1914 * the most recent write-release */
1915 return true; /* complete */
1916 } /* else, need to establish contiguous release sequence */
1917 ModelAction *release = *rit;
1919 ASSERT(rf->same_thread(release));
1921 pending->writes.clear();
1923 bool certain = true;
1924 for (unsigned int i = 0; i < thrd_lists->size(); i++) {
1925 if (id_to_int(rf->get_tid()) == (int)i)
1927 list = &(*thrd_lists)[i];
1929 /* Can we ensure no future writes from this thread may break
1930 * the release seq? */
1931 bool future_ordered = false;
1933 ModelAction *last = get_last_action(int_to_id(i));
1934 Thread *th = get_thread(int_to_id(i));
1935 if ((last && rf->happens_before(last)) ||
1938 future_ordered = true;
1940 ASSERT(!th->is_model_thread() || future_ordered);
1942 for (rit = list->rbegin(); rit != list->rend(); rit++) {
1943 const ModelAction *act = *rit;
1944 /* Reach synchronization -> this thread is complete */
1945 if (act->happens_before(release))
1947 if (rf->happens_before(act)) {
1948 future_ordered = true;
1952 /* Only non-RMW writes can break release sequences */
1953 if (!act->is_write() || act->is_rmw())
1956 /* Check modification order */
1957 if (mo_graph->checkReachable(rf, act)) {
1958 /* rf --mo--> act */
1959 future_ordered = true;
1962 if (mo_graph->checkReachable(act, release))
1963 /* act --mo--> release */
1965 if (mo_graph->checkReachable(release, act) &&
1966 mo_graph->checkReachable(act, rf)) {
1967 /* release --mo-> act --mo--> rf */
1968 return true; /* complete */
1970 /* act may break release sequence */
1971 pending->writes.push_back(act);
1974 if (!future_ordered)
1975 certain = false; /* This thread is uncertain */
1979 release_heads->push_back(release);
1980 pending->writes.clear();
1982 pending->release = release;
1989 * An interface for getting the release sequence head(s) with which a
1990 * given ModelAction must synchronize. This function only returns a non-empty
1991 * result when it can locate a release sequence head with certainty. Otherwise,
1992 * it may mark the internal state of the ModelChecker so that it will handle
1993 * the release sequence at a later time, causing @a acquire to update its
1994 * synchronization at some later point in execution.
1996 * @param acquire The 'acquire' action that may synchronize with a release
1998 * @param read The read action that may read from a release sequence; this may
1999 * be the same as acquire, or else an earlier action in the same thread (i.e.,
2000 * when 'acquire' is a fence-acquire)
2001 * @param release_heads A pass-by-reference return parameter. Will be filled
2002 * with the head(s) of the release sequence(s), if they exists with certainty.
2003 * @see ModelChecker::release_seq_heads
2005 void ModelChecker::get_release_seq_heads(ModelAction *acquire,
2006 ModelAction *read, rel_heads_list_t *release_heads)
2008 const ModelAction *rf = read->get_reads_from();
2009 struct release_seq *sequence = (struct release_seq *)snapshot_calloc(1, sizeof(struct release_seq));
2010 sequence->acquire = acquire;
2011 sequence->read = read;
2013 if (!release_seq_heads(rf, release_heads, sequence)) {
2014 /* add act to 'lazy checking' list */
2015 pending_rel_seqs->push_back(sequence);
2017 snapshot_free(sequence);
2022 * Attempt to resolve all stashed operations that might synchronize with a
2023 * release sequence for a given location. This implements the "lazy" portion of
2024 * determining whether or not a release sequence was contiguous, since not all
2025 * modification order information is present at the time an action occurs.
2027 * @param location The location/object that should be checked for release
2028 * sequence resolutions. A NULL value means to check all locations.
2029 * @param work_queue The work queue to which to add work items as they are
2031 * @return True if any updates occurred (new synchronization, new mo_graph
2034 bool ModelChecker::resolve_release_sequences(void *location, work_queue_t *work_queue)
2036 bool updated = false;
2037 std::vector< struct release_seq *, SnapshotAlloc<struct release_seq *> >::iterator it = pending_rel_seqs->begin();
2038 while (it != pending_rel_seqs->end()) {
2039 struct release_seq *pending = *it;
2040 ModelAction *acquire = pending->acquire;
2041 const ModelAction *read = pending->read;
2043 /* Only resolve sequences on the given location, if provided */
2044 if (location && read->get_location() != location) {
2049 const ModelAction *rf = read->get_reads_from();
2050 rel_heads_list_t release_heads;
2052 complete = release_seq_heads(rf, &release_heads, pending);
2053 for (unsigned int i = 0; i < release_heads.size(); i++) {
2054 if (!acquire->has_synchronized_with(release_heads[i])) {
2055 if (acquire->synchronize_with(release_heads[i]))
2058 set_bad_synchronization();
2063 /* Re-check all pending release sequences */
2064 work_queue->push_back(CheckRelSeqWorkEntry(NULL));
2065 /* Re-check read-acquire for mo_graph edges */
2066 if (acquire->is_read())
2067 work_queue->push_back(MOEdgeWorkEntry(acquire));
2069 /* propagate synchronization to later actions */
2070 action_list_t::reverse_iterator rit = action_trace->rbegin();
2071 for (; (*rit) != acquire; rit++) {
2072 ModelAction *propagate = *rit;
2073 if (acquire->happens_before(propagate)) {
2074 propagate->synchronize_with(acquire);
2075 /* Re-check 'propagate' for mo_graph edges */
2076 work_queue->push_back(MOEdgeWorkEntry(propagate));
2081 it = pending_rel_seqs->erase(it);
2082 snapshot_free(pending);
2088 // If we resolved promises or data races, see if we have realized a data race.
2095 * Performs various bookkeeping operations for the current ModelAction. For
2096 * instance, adds action to the per-object, per-thread action vector and to the
2097 * action trace list of all thread actions.
2099 * @param act is the ModelAction to add.
2101 void ModelChecker::add_action_to_lists(ModelAction *act)
2103 int tid = id_to_int(act->get_tid());
2104 ModelAction *uninit = NULL;
2106 action_list_t *list = get_safe_ptr_action(obj_map, act->get_location());
2107 if (list->empty() && act->is_atomic_var()) {
2108 uninit = new_uninitialized_action(act->get_location());
2109 uninit_id = id_to_int(uninit->get_tid());
2110 list->push_back(uninit);
2112 list->push_back(act);
2114 action_trace->push_back(act);
2116 action_trace->push_front(uninit);
2118 std::vector<action_list_t> *vec = get_safe_ptr_vect_action(obj_thrd_map, act->get_location());
2119 if (tid >= (int)vec->size())
2120 vec->resize(priv->next_thread_id);
2121 (*vec)[tid].push_back(act);
2123 (*vec)[uninit_id].push_front(uninit);
2125 if ((int)thrd_last_action->size() <= tid)
2126 thrd_last_action->resize(get_num_threads());
2127 (*thrd_last_action)[tid] = act;
2129 (*thrd_last_action)[uninit_id] = uninit;
2131 if (act->is_fence() && act->is_release()) {
2132 if ((int)thrd_last_fence_release->size() <= tid)
2133 thrd_last_fence_release->resize(get_num_threads());
2134 (*thrd_last_fence_release)[tid] = act;
2137 if (act->is_wait()) {
2138 void *mutex_loc = (void *) act->get_value();
2139 get_safe_ptr_action(obj_map, mutex_loc)->push_back(act);
2141 std::vector<action_list_t> *vec = get_safe_ptr_vect_action(obj_thrd_map, mutex_loc);
2142 if (tid >= (int)vec->size())
2143 vec->resize(priv->next_thread_id);
2144 (*vec)[tid].push_back(act);
2149 * @brief Get the last action performed by a particular Thread
2150 * @param tid The thread ID of the Thread in question
2151 * @return The last action in the thread
2153 ModelAction * ModelChecker::get_last_action(thread_id_t tid) const
2155 int threadid = id_to_int(tid);
2156 if (threadid < (int)thrd_last_action->size())
2157 return (*thrd_last_action)[id_to_int(tid)];
2163 * @brief Get the last fence release performed by a particular Thread
2164 * @param tid The thread ID of the Thread in question
2165 * @return The last fence release in the thread, if one exists; NULL otherwise
2167 ModelAction * ModelChecker::get_last_fence_release(thread_id_t tid) const
2169 int threadid = id_to_int(tid);
2170 if (threadid < (int)thrd_last_fence_release->size())
2171 return (*thrd_last_fence_release)[id_to_int(tid)];
2177 * Gets the last memory_order_seq_cst write (in the total global sequence)
2178 * performed on a particular object (i.e., memory location), not including the
2180 * @param curr The current ModelAction; also denotes the object location to
2182 * @return The last seq_cst write
2184 ModelAction * ModelChecker::get_last_seq_cst_write(ModelAction *curr) const
2186 void *location = curr->get_location();
2187 action_list_t *list = get_safe_ptr_action(obj_map, location);
2188 /* Find: max({i in dom(S) | seq_cst(t_i) && isWrite(t_i) && samevar(t_i, t)}) */
2189 action_list_t::reverse_iterator rit;
2190 for (rit = list->rbegin(); rit != list->rend(); rit++)
2191 if ((*rit)->is_write() && (*rit)->is_seqcst() && (*rit) != curr)
2197 * Gets the last memory_order_seq_cst fence (in the total global sequence)
2198 * performed in a particular thread, prior to a particular fence.
2199 * @param tid The ID of the thread to check
2200 * @param before_fence The fence from which to begin the search; if NULL, then
2201 * search for the most recent fence in the thread.
2202 * @return The last prior seq_cst fence in the thread, if exists; otherwise, NULL
2204 ModelAction * ModelChecker::get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const
2206 /* All fences should have NULL location */
2207 action_list_t *list = get_safe_ptr_action(obj_map, NULL);
2208 action_list_t::reverse_iterator rit = list->rbegin();
2211 for (; rit != list->rend(); rit++)
2212 if (*rit == before_fence)
2215 ASSERT(*rit == before_fence);
2219 for (; rit != list->rend(); rit++)
2220 if ((*rit)->is_fence() && (tid == (*rit)->get_tid()) && (*rit)->is_seqcst())
2226 * Gets the last unlock operation performed on a particular mutex (i.e., memory
2227 * location). This function identifies the mutex according to the current
2228 * action, which is presumed to perform on the same mutex.
2229 * @param curr The current ModelAction; also denotes the object location to
2231 * @return The last unlock operation
2233 ModelAction * ModelChecker::get_last_unlock(ModelAction *curr) const
2235 void *location = curr->get_location();
2236 action_list_t *list = get_safe_ptr_action(obj_map, location);
2237 /* Find: max({i in dom(S) | isUnlock(t_i) && samevar(t_i, t)}) */
2238 action_list_t::reverse_iterator rit;
2239 for (rit = list->rbegin(); rit != list->rend(); rit++)
2240 if ((*rit)->is_unlock() || (*rit)->is_wait())
2245 ModelAction * ModelChecker::get_parent_action(thread_id_t tid) const
2247 ModelAction *parent = get_last_action(tid);
2249 parent = get_thread(tid)->get_creation();
2254 * Returns the clock vector for a given thread.
2255 * @param tid The thread whose clock vector we want
2256 * @return Desired clock vector
2258 ClockVector * ModelChecker::get_cv(thread_id_t tid) const
2260 return get_parent_action(tid)->get_cv();
2264 * Resolve a set of Promises with a current write. The set is provided in the
2265 * Node corresponding to @a write.
2266 * @param write The ModelAction that is fulfilling Promises
2267 * @return True if promises were resolved; false otherwise
2269 bool ModelChecker::resolve_promises(ModelAction *write)
2271 bool haveResolved = false;
2272 std::vector< ModelAction *, ModelAlloc<ModelAction *> > actions_to_check;
2273 promise_list_t mustResolve, resolved;
2275 for (unsigned int i = 0, promise_index = 0; promise_index < promises->size(); i++) {
2276 Promise *promise = (*promises)[promise_index];
2277 if (write->get_node()->get_promise(i)) {
2278 ModelAction *read = promise->get_action();
2279 read_from(read, write);
2280 //Make sure the promise's value matches the write's value
2281 ASSERT(promise->is_compatible(write));
2282 mo_graph->resolvePromise(read, write, &mustResolve);
2284 resolved.push_back(promise);
2285 promises->erase(promises->begin() + promise_index);
2286 actions_to_check.push_back(read);
2288 haveResolved = true;
2293 for (unsigned int i = 0; i < mustResolve.size(); i++) {
2294 if (std::find(resolved.begin(), resolved.end(), mustResolve[i])
2296 priv->failed_promise = true;
2298 for (unsigned int i = 0; i < resolved.size(); i++)
2300 //Check whether reading these writes has made threads unable to
2303 for (unsigned int i = 0; i < actions_to_check.size(); i++) {
2304 ModelAction *read = actions_to_check[i];
2305 mo_check_promises(read, true);
2308 return haveResolved;
2312 * Compute the set of promises that could potentially be satisfied by this
2313 * action. Note that the set computation actually appears in the Node, not in
2315 * @param curr The ModelAction that may satisfy promises
2317 void ModelChecker::compute_promises(ModelAction *curr)
2319 for (unsigned int i = 0; i < promises->size(); i++) {
2320 Promise *promise = (*promises)[i];
2321 const ModelAction *act = promise->get_action();
2322 if (!act->happens_before(curr) &&
2324 !act->could_synchronize_with(curr) &&
2325 !act->same_thread(curr) &&
2326 act->get_location() == curr->get_location() &&
2327 promise->get_value() == curr->get_value()) {
2328 curr->get_node()->set_promise(i, act->is_rmw());
2333 /** Checks promises in response to change in ClockVector Threads. */
2334 void ModelChecker::check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv)
2336 for (unsigned int i = 0; i < promises->size(); i++) {
2337 Promise *promise = (*promises)[i];
2338 const ModelAction *act = promise->get_action();
2339 if ((old_cv == NULL || !old_cv->synchronized_since(act)) &&
2340 merge_cv->synchronized_since(act)) {
2341 if (promise->eliminate_thread(tid)) {
2342 //Promise has failed
2343 priv->failed_promise = true;
2350 void ModelChecker::check_promises_thread_disabled()
2352 for (unsigned int i = 0; i < promises->size(); i++) {
2353 Promise *promise = (*promises)[i];
2354 if (promise->has_failed()) {
2355 priv->failed_promise = true;
2362 * @brief Checks promises in response to addition to modification order for
2365 * We test whether threads are still available for satisfying promises after an
2366 * addition to our modification order constraints. Those that are unavailable
2367 * are "eliminated". Once all threads are eliminated from satisfying a promise,
2368 * that promise has failed.
2370 * @param act The ModelAction which updated the modification order
2371 * @param is_read_check Should be true if act is a read and we must check for
2372 * updates to the store from which it read (there is a distinction here for
2373 * RMW's, which are both a load and a store)
2375 void ModelChecker::mo_check_promises(const ModelAction *act, bool is_read_check)
2377 const ModelAction *write = is_read_check ? act->get_reads_from() : act;
2379 for (unsigned int i = 0; i < promises->size(); i++) {
2380 Promise *promise = (*promises)[i];
2381 const ModelAction *pread = promise->get_action();
2383 // Is this promise on the same location?
2384 if (!pread->same_var(write))
2387 if (pread->happens_before(act) && mo_graph->checkPromise(write, promise)) {
2388 priv->failed_promise = true;
2392 // Don't do any lookups twice for the same thread
2393 if (!promise->thread_is_available(act->get_tid()))
2396 if (mo_graph->checkReachable(promise, write)) {
2397 if (mo_graph->checkPromise(write, promise)) {
2398 priv->failed_promise = true;
2406 * Compute the set of writes that may break the current pending release
2407 * sequence. This information is extracted from previou release sequence
2410 * @param curr The current ModelAction. Must be a release sequence fixup
2413 void ModelChecker::compute_relseq_breakwrites(ModelAction *curr)
2415 if (pending_rel_seqs->empty())
2418 struct release_seq *pending = pending_rel_seqs->back();
2419 for (unsigned int i = 0; i < pending->writes.size(); i++) {
2420 const ModelAction *write = pending->writes[i];
2421 curr->get_node()->add_relseq_break(write);
2424 /* NULL means don't break the sequence; just synchronize */
2425 curr->get_node()->add_relseq_break(NULL);
2429 * Build up an initial set of all past writes that this 'read' action may read
2430 * from. This set is determined by the clock vector's "happens before"
2432 * @param curr is the current ModelAction that we are exploring; it must be a
2435 void ModelChecker::build_reads_from_past(ModelAction *curr)
2437 std::vector<action_list_t> *thrd_lists = get_safe_ptr_vect_action(obj_thrd_map, curr->get_location());
2439 ASSERT(curr->is_read());
2441 ModelAction *last_sc_write = NULL;
2443 if (curr->is_seqcst())
2444 last_sc_write = get_last_seq_cst_write(curr);
2446 /* Iterate over all threads */
2447 for (i = 0; i < thrd_lists->size(); i++) {
2448 /* Iterate over actions in thread, starting from most recent */
2449 action_list_t *list = &(*thrd_lists)[i];
2450 action_list_t::reverse_iterator rit;
2451 for (rit = list->rbegin(); rit != list->rend(); rit++) {
2452 ModelAction *act = *rit;
2454 /* Only consider 'write' actions */
2455 if (!act->is_write() || act == curr)
2458 /* Don't consider more than one seq_cst write if we are a seq_cst read. */
2459 bool allow_read = true;
2461 if (curr->is_seqcst() && (act->is_seqcst() || (last_sc_write != NULL && act->happens_before(last_sc_write))) && act != last_sc_write)
2463 else if (curr->get_sleep_flag() && !curr->is_seqcst() && !sleep_can_read_from(curr, act))
2467 /* Only add feasible reads */
2468 mo_graph->startChanges();
2469 r_modification_order(curr, act);
2470 if (!is_infeasible())
2471 curr->get_node()->add_read_from(act);
2472 mo_graph->rollbackChanges();
2475 /* Include at most one act per-thread that "happens before" curr */
2476 if (act->happens_before(curr))
2480 /* We may find no valid may-read-from only if the execution is doomed */
2481 if (!curr->get_node()->get_read_from_size()) {
2482 priv->no_valid_reads = true;
2486 if (DBG_ENABLED()) {
2487 model_print("Reached read action:\n");
2489 model_print("Printing may_read_from\n");
2490 curr->get_node()->print_may_read_from();
2491 model_print("End printing may_read_from\n");
2495 bool ModelChecker::sleep_can_read_from(ModelAction *curr, const ModelAction *write)
2498 /* UNINIT actions don't have a Node, and they never sleep */
2499 if (write->is_uninitialized())
2501 Node *prevnode = write->get_node()->get_parent();
2503 bool thread_sleep = prevnode->enabled_status(curr->get_tid()) == THREAD_SLEEP_SET;
2504 if (write->is_release() && thread_sleep)
2506 if (!write->is_rmw()) {
2509 if (write->get_reads_from() == NULL)
2511 write = write->get_reads_from();
2516 * @brief Create a new action representing an uninitialized atomic
2517 * @param location The memory location of the atomic object
2518 * @return A pointer to a new ModelAction
2520 ModelAction * ModelChecker::new_uninitialized_action(void *location) const
2522 ModelAction *act = (ModelAction *)snapshot_malloc(sizeof(class ModelAction));
2523 act = new (act) ModelAction(ATOMIC_UNINIT, std::memory_order_relaxed, location, 0, model_thread);
2524 act->create_cv(NULL);
2528 static void print_list(action_list_t *list)
2530 action_list_t::iterator it;
2532 model_print("---------------------------------------------------------------------\n");
2534 unsigned int hash = 0;
2536 for (it = list->begin(); it != list->end(); it++) {
2538 hash = hash^(hash<<3)^((*it)->hash());
2540 model_print("HASH %u\n", hash);
2541 model_print("---------------------------------------------------------------------\n");
2544 #if SUPPORT_MOD_ORDER_DUMP
2545 void ModelChecker::dumpGraph(char *filename) const
2548 sprintf(buffer, "%s.dot", filename);
2549 FILE *file = fopen(buffer, "w");
2550 fprintf(file, "digraph %s {\n", filename);
2551 mo_graph->dumpNodes(file);
2552 ModelAction **thread_array = (ModelAction **)model_calloc(1, sizeof(ModelAction *) * get_num_threads());
2554 for (action_list_t::iterator it = action_trace->begin(); it != action_trace->end(); it++) {
2555 ModelAction *action = *it;
2556 if (action->is_read()) {
2557 fprintf(file, "N%u [label=\"N%u, T%u\"];\n", action->get_seq_number(), action->get_seq_number(), action->get_tid());
2558 if (action->get_reads_from() != NULL)
2559 fprintf(file, "N%u -> N%u[label=\"rf\", color=red];\n", action->get_seq_number(), action->get_reads_from()->get_seq_number());
2561 if (thread_array[action->get_tid()] != NULL) {
2562 fprintf(file, "N%u -> N%u[label=\"sb\", color=blue];\n", thread_array[action->get_tid()]->get_seq_number(), action->get_seq_number());
2565 thread_array[action->get_tid()] = action;
2567 fprintf(file, "}\n");
2568 model_free(thread_array);
2573 /** @brief Prints an execution trace summary. */
2574 void ModelChecker::print_summary() const
2576 #if SUPPORT_MOD_ORDER_DUMP
2577 char buffername[100];
2578 sprintf(buffername, "exec%04u", stats.num_total);
2579 mo_graph->dumpGraphToFile(buffername);
2580 sprintf(buffername, "graph%04u", stats.num_total);
2581 dumpGraph(buffername);
2584 model_print("Execution %d:", stats.num_total);
2585 if (isfeasibleprefix())
2588 print_infeasibility(" INFEASIBLE");
2589 print_list(action_trace);
2594 * Add a Thread to the system for the first time. Should only be called once
2596 * @param t The Thread to add
2598 void ModelChecker::add_thread(Thread *t)
2600 thread_map->put(id_to_int(t->get_id()), t);
2601 scheduler->add_thread(t);
2605 * Removes a thread from the scheduler.
2606 * @param the thread to remove.
2608 void ModelChecker::remove_thread(Thread *t)
2610 scheduler->remove_thread(t);
2614 * @brief Get a Thread reference by its ID
2615 * @param tid The Thread's ID
2616 * @return A Thread reference
2618 Thread * ModelChecker::get_thread(thread_id_t tid) const
2620 return thread_map->get(id_to_int(tid));
2624 * @brief Get a reference to the Thread in which a ModelAction was executed
2625 * @param act The ModelAction
2626 * @return A Thread reference
2628 Thread * ModelChecker::get_thread(const ModelAction *act) const
2630 return get_thread(act->get_tid());
2634 * @brief Check if a Thread is currently enabled
2635 * @param t The Thread to check
2636 * @return True if the Thread is currently enabled
2638 bool ModelChecker::is_enabled(Thread *t) const
2640 return scheduler->is_enabled(t);
2644 * @brief Check if a Thread is currently enabled
2645 * @param tid The ID of the Thread to check
2646 * @return True if the Thread is currently enabled
2648 bool ModelChecker::is_enabled(thread_id_t tid) const
2650 return scheduler->is_enabled(tid);
2654 * Switch from a user-context to the "master thread" context (a.k.a. system
2655 * context). This switch is made with the intention of exploring a particular
2656 * model-checking action (described by a ModelAction object). Must be called
2657 * from a user-thread context.
2659 * @param act The current action that will be explored. May be NULL only if
2660 * trace is exiting via an assertion (see ModelChecker::set_assert and
2661 * ModelChecker::has_asserted).
2662 * @return Return the value returned by the current action
2664 uint64_t ModelChecker::switch_to_master(ModelAction *act)
2667 Thread *old = thread_current();
2668 old->set_pending(act);
2669 if (Thread::swap(old, &system_context) < 0) {
2670 perror("swap threads");
2673 return old->get_return_value();
2677 * Takes the next step in the execution, if possible.
2678 * @param curr The current step to take
2679 * @return Returns the next Thread to run, if any; NULL if this execution
2682 Thread * ModelChecker::take_step(ModelAction *curr)
2687 Thread *curr_thrd = get_thread(curr);
2688 ASSERT(curr_thrd->get_state() == THREAD_READY);
2690 curr = check_current_action(curr);
2692 /* Infeasible -> don't take any more steps */
2693 if (is_infeasible())
2695 else if (isfeasibleprefix() && have_bug_reports()) {
2700 if (params.bound != 0 && priv->used_sequence_numbers > params.bound)
2703 if (curr_thrd->is_blocked() || curr_thrd->is_complete())
2704 scheduler->remove_thread(curr_thrd);
2706 Thread *next_thrd = get_next_thread(curr);
2707 next_thrd = scheduler->next_thread(next_thrd);
2709 DEBUG("(%d, %d)\n", curr_thrd ? id_to_int(curr_thrd->get_id()) : -1,
2710 next_thrd ? id_to_int(next_thrd->get_id()) : -1);
2713 * Launch end-of-execution release sequence fixups only when there are:
2715 * (1) no more user threads to run (or when execution replay chooses
2716 * the 'model_thread')
2717 * (2) pending release sequences
2718 * (3) pending assertions (i.e., data races)
2719 * (4) no pending promises
2721 if (!pending_rel_seqs->empty() && (!next_thrd || next_thrd->is_model_thread()) &&
2722 is_feasible_prefix_ignore_relseq() && !unrealizedraces.empty()) {
2723 model_print("*** WARNING: release sequence fixup action (%zu pending release seuqences) ***\n",
2724 pending_rel_seqs->size());
2725 ModelAction *fixup = new ModelAction(MODEL_FIXUP_RELSEQ,
2726 std::memory_order_seq_cst, NULL, VALUE_NONE,
2728 model_thread->set_pending(fixup);
2729 return model_thread;
2732 /* next_thrd == NULL -> don't take any more steps */
2739 /** Wrapper to run the user's main function, with appropriate arguments */
2740 void user_main_wrapper(void *)
2742 user_main(model->params.argc, model->params.argv);
2745 /** @brief Run ModelChecker for the user program */
2746 void ModelChecker::run()
2750 Thread *t = new Thread(&user_thread, &user_main_wrapper, NULL);
2754 scheduler->next_thread(t);
2755 Thread::swap(&system_context, t);
2757 /* Consume the next action for a Thread */
2758 ModelAction *curr = t->get_pending();
2759 t->set_pending(NULL);
2760 t = take_step(curr);
2761 } while (t && !t->is_model_thread());
2762 /** @TODO Re-write release sequence fixups here */
2763 } while (next_execution());