Lock model check when we terminate proxy thread
[c11tester.git] / execution.cc
index 8877fcdc676c4c807e5f7f94a9e61f3aefb29d4c..422b82a988255d4b7b1125ec75d7f6f8f21159f0 100644 (file)
@@ -13,6 +13,7 @@
 #include "datarace.h"
 #include "threads-model.h"
 #include "bugmessage.h"
+#include "history.h"
 #include "fuzzer.h"
 
 #define INITIAL_THREAD_ID       0
@@ -62,8 +63,10 @@ ModelExecution::ModelExecution(ModelChecker *m, Scheduler *scheduler) :
        thrd_last_action(1),
        thrd_last_fence_release(),
        priv(new struct model_snapshot_members ()),
-                        mo_graph(new CycleGraph()),
-       fuzzer(new Fuzzer())
+       mo_graph(new CycleGraph()),
+       fuzzer(new Fuzzer()),
+       thrd_func_list(),
+       thrd_func_inst_lists()
 {
        /* Initialize a model-checker thread, for special ModelActions */
        model_thread = new Thread(get_next_id());
@@ -188,6 +191,16 @@ bool ModelExecution::have_bug_reports() const
        return priv->bugs.size() != 0;
 }
 
+/** @return True, if any fatal bugs have been reported for this execution.
+ *  Any bug other than a data race is considered a fatal bug. Data races
+ *  are not considered fatal unless the number of races is exceeds
+ *  a threshold (temporarily set as 15).
+ */
+bool ModelExecution::have_fatal_bug_reports() const
+{
+       return priv->bugs.size() != 0;
+}
+
 SnapVector<bug_message *> * ModelExecution::get_bugs() const
 {
        return &priv->bugs;
@@ -250,6 +263,20 @@ bool ModelExecution::is_complete_execution() const
        return true;
 }
 
+ModelAction * ModelExecution::convertNonAtomicStore(void * location) {
+       uint64_t value = *((const uint64_t *) location);
+       modelclock_t storeclock;
+       thread_id_t storethread;
+       getStoreThreadAndClock(location, &storethread, &storeclock);
+       setAtomicStoreFlag(location);
+       ModelAction * act = new ModelAction(NONATOMIC_WRITE, memory_order_relaxed, location, value, get_thread(storethread));
+       act->set_seq_number(storeclock);
+       add_normal_write_to_lists(act);
+       add_write_to_lists(act);
+       w_modification_order(act);
+       return act;
+}
+
 
 /**
  * Processes a read model action.
@@ -260,8 +287,13 @@ bool ModelExecution::is_complete_execution() const
 void ModelExecution::process_read(ModelAction *curr, SnapVector<ModelAction *> * rf_set)
 {
        SnapVector<const ModelAction *> * priorset = new SnapVector<const ModelAction *>();
-       while(true) {
+       bool hasnonatomicstore = hasNonAtomicStore(curr->get_location());
+       if (hasnonatomicstore) {
+               ModelAction * nonatomicstore = convertNonAtomicStore(curr->get_location());
+               rf_set->push_back(nonatomicstore);
+       }
 
+       while(true) {
                int index = fuzzer->selectWrite(curr, rf_set);
                ModelAction *rf = (*rf_set)[index];
 
@@ -323,8 +355,9 @@ bool ModelExecution::process_mutex(ModelAction *curr)
        }
        //otherwise fall into the lock case
        case ATOMIC_LOCK: {
-               if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock)
-                       assert_bug("Lock access before initialization");
+               //TODO: FIND SOME BETTER WAY TO CHECK LOCK INITIALIZED OR NOT
+               //if (curr->get_cv()->getClock(state->alloc_tid) <= state->alloc_clock)
+               //      assert_bug("Lock access before initialization");
                state->locked = get_thread(curr);
                ModelAction *unlock = get_last_unlock(curr);
                //synchronize with the previous unlock statement
@@ -385,10 +418,7 @@ bool ModelExecution::process_mutex(ModelAction *curr)
  */
 void ModelExecution::process_write(ModelAction *curr)
 {
-
        w_modification_order(curr);
-
-
        get_thread(curr)->set_return_value(VALUE_NONE);
 }
 
@@ -431,8 +461,8 @@ bool ModelExecution::process_fence(ModelAction *curr)
                                continue;
 
                        /* Establish hypothetical release sequences */
-                       ClockVector *cv = get_hb_from_write(act);
-                       if (curr->get_cv()->merge(cv))
+                       ClockVector *cv = get_hb_from_write(act->get_reads_from());
+                       if (cv != NULL && curr->get_cv()->merge(cv))
                                updated = true;
                }
        }
@@ -822,7 +852,7 @@ bool ModelExecution::r_modification_order(ModelAction *curr, const ModelAction *
                        if (act->happens_before(curr)) {
                                if (i==0) {
                                        if (last_sc_fence_local == NULL ||
-                                                       (*last_sc_fence_local < *prev_same_thread)) {
+                                                       (*last_sc_fence_local < *act)) {
                                                prev_same_thread = act;
                                        }
                                }
@@ -1065,6 +1095,10 @@ void ModelExecution::add_action_to_lists(ModelAction *act)
                uninit = get_uninitialized_action(act);
                uninit_id = id_to_int(uninit->get_tid());
                list->push_front(uninit);
+               SnapVector<action_list_t> *vec = get_safe_ptr_vect_action(&obj_wr_thrd_map, act->get_location());
+               if (uninit_id >= (int)vec->size())
+                       vec->resize(uninit_id + 1);
+               (*vec)[uninit_id].push_front(uninit);
        }
        list->push_back(act);
 
@@ -1106,10 +1140,80 @@ void ModelExecution::add_action_to_lists(ModelAction *act)
        }
 }
 
+void insertIntoActionList(action_list_t *list, ModelAction *act) {
+       action_list_t::reverse_iterator rit = list->rbegin();
+       modelclock_t next_seq = act->get_seq_number();
+       if (rit == list->rend() || (*rit)->get_seq_number() == next_seq)
+               list->push_back(act);
+       else {
+               for(;rit != list->rend();rit++) {
+                       if ((*rit)->get_seq_number() == next_seq) {
+                               action_list_t::iterator it = rit.base();
+                               list->insert(it, act);
+                               break;
+                       }
+               }
+       }
+}
+
+void insertIntoActionListAndSetCV(action_list_t *list, ModelAction *act) {
+       action_list_t::reverse_iterator rit = list->rbegin();
+       modelclock_t next_seq = act->get_seq_number();
+       if (rit == list->rend()) {
+               act->create_cv(NULL);
+       } else if ((*rit)->get_seq_number() == next_seq) {
+               act->create_cv((*rit));
+               list->push_back(act);
+       } else {
+               for(;rit != list->rend();rit++) {
+                       if ((*rit)->get_seq_number() == next_seq) {
+                               act->create_cv((*rit));
+                               action_list_t::iterator it = rit.base();
+                               list->insert(it, act);
+                               break;
+                       }
+               }
+       }
+}
+
+/**
+ * Performs various bookkeeping operations for a normal write.  The
+ * complication is that we are typically inserting a normal write
+ * lazily, so we need to insert it into the middle of lists.
+ *
+ * @param act is the ModelAction to add.
+ */
+
+void ModelExecution::add_normal_write_to_lists(ModelAction *act)
+{
+       int tid = id_to_int(act->get_tid());
+       insertIntoActionListAndSetCV(&action_trace, act);
+
+       action_list_t *list = get_safe_ptr_action(&obj_map, act->get_location());
+       insertIntoActionList(list, act);
+
+       // Update obj_thrd_map, a per location, per thread, order of actions
+       SnapVector<action_list_t> *vec = get_safe_ptr_vect_action(&obj_thrd_map, act->get_location());
+       if (tid >= (int)vec->size())
+               vec->resize(priv->next_thread_id);
+       insertIntoActionList(&(*vec)[tid],act);
+
+       // Update thrd_last_action, the last action taken by each thrad
+       if (thrd_last_action[tid]->get_seq_number() == act->get_seq_number())
+               thrd_last_action[tid] = act;
+}
+
+
 void ModelExecution::add_write_to_lists(ModelAction *write) {
        // Update seq_cst map
        if (write->is_seqcst())
                obj_last_sc_map.put(write->get_location(), write);
+
+       SnapVector<action_list_t> *vec = get_safe_ptr_vect_action(&obj_wr_thrd_map, write->get_location());
+       int tid = id_to_int(write->get_tid());
+       if (tid >= (int)vec->size())
+               vec->resize(priv->next_thread_id);
+       (*vec)[tid].push_back(write);
 }
 
 /**
@@ -1252,7 +1356,7 @@ bool valequals(uint64_t val1, uint64_t val2, int size) {
  */
 SnapVector<ModelAction *> *  ModelExecution::build_may_read_from(ModelAction *curr)
 {
-       SnapVector<action_list_t> *thrd_lists = obj_thrd_map.get(curr->get_location());
+       SnapVector<action_list_t> *thrd_lists = obj_wr_thrd_map.get(curr->get_location());
        unsigned int i;
        ASSERT(curr->is_read());
 
@@ -1271,18 +1375,6 @@ SnapVector<ModelAction *> *  ModelExecution::build_may_read_from(ModelAction *cu
                for (rit = list->rbegin();rit != list->rend();rit++) {
                        ModelAction *act = *rit;
 
-                       /* Only consider 'write' actions */
-                       if (!act->is_write()) {
-                               if (act != curr && act->is_read() && act->happens_before(curr)) {
-                                       ModelAction *tmp = act->get_reads_from();
-                                       if (((unsigned int) id_to_int(tmp->get_tid()))==i)
-                                               act = tmp;
-                                       else
-                                               break;
-                               } else
-                                       continue;
-                       }
-
                        if (act == curr)
                                continue;
 
@@ -1539,6 +1631,9 @@ Thread * ModelExecution::take_step(ModelAction *curr)
        curr = check_current_action(curr);
        ASSERT(curr);
 
+       /* Process this action in ModelHistory for records*/
+       model->get_history()->process_action( curr, curr_thrd->get_id() );
+
        if (curr_thrd->is_blocked() || curr_thrd->is_complete())
                scheduler->remove_thread(curr_thrd);