Merge branch 'new_fuzzer' of /home/git/random-fuzzer into new_fuzzer
[c11tester.git] / model.cc
index f5f20dd75dcc0147fd4f62ee200d7674bf2df6fe..c468d0deb3e8b6972673d3910670e99eff42052f 100644 (file)
--- a/model.cc
+++ b/model.cc
 #include "execution.h"
 #include "bugmessage.h"
 
-ModelChecker *model;
+ModelChecker *model = NULL;
+ModelChecker *model_init = NULL;
+
+/** Wrapper to run the user's main function, with appropriate arguments */
+void user_main_wrapper(void *)
+{
+       user_main(model->params.argc, model->params.argv);
+}
 
 /** @brief Constructor */
 ModelChecker::ModelChecker() :
@@ -33,6 +40,9 @@ ModelChecker::ModelChecker() :
        inspect_plugin(NULL)
 {
        memset(&stats,0,sizeof(struct execution_stats));
+       init_thread = new Thread(execution->get_next_id(), (thrd_t *) malloc(sizeof(thrd_t)), &user_main_wrapper, NULL, NULL);  // L: user_main_wrapper passes the user program
+       execution->add_thread(init_thread);
+       scheduler->set_current_thread(init_thread);
 }
 
 /** @brief Destructor */
@@ -44,8 +54,8 @@ ModelChecker::~ModelChecker()
 
 /** Method to set parameters */
 void ModelChecker::setParams(struct model_params params) {
-  this->params = params;
-  execution->setParams(&params);
+       this->params = params;
+       execution->setParams(&params);
 }
 
 /**
@@ -54,15 +64,13 @@ void ModelChecker::setParams(struct model_params params) {
  */
 void ModelChecker::reset_to_initial_state()
 {
-       DEBUG("+++ Resetting to initial state +++\n");
-       node_stack->reset_execution();
 
        /**
         * FIXME: if we utilize partial rollback, we will need to free only
         * those pending actions which were NOT pending before the rollback
         * point
         */
-       for (unsigned int i = 0; i < get_num_threads(); i++)
+       for (unsigned int i = 0;i < get_num_threads();i++)
                delete get_thread(int_to_id(i))->get_pending();
 
        snapshot_backtrack_before(0);
@@ -148,9 +156,9 @@ void ModelChecker::print_bugs() const
        SnapVector<bug_message *> *bugs = execution->get_bugs();
 
        model_print("Bug report: %zu bug%s detected\n",
-                       bugs->size(),
-                       bugs->size() > 1 ? "s" : "");
-       for (unsigned int i = 0; i < bugs->size(); i++)
+                                                       bugs->size(),
+                                                       bugs->size() > 1 ? "s" : "");
+       for (unsigned int i = 0;i < bugs->size();i++)
                (*bugs)[i]->print();
 }
 
@@ -198,7 +206,7 @@ void ModelChecker::print_stats() const
 void ModelChecker::print_execution(bool printbugs) const
 {
        model_print("Program output from execution %d:\n",
-                       get_execution_number());
+                                                       get_execution_number());
        print_program_output();
 
        if (params.verbose >= 3) {
@@ -227,8 +235,8 @@ bool ModelChecker::next_execution()
        DBG();
        /* Is this execution a feasible execution that's worth bug-checking? */
        bool complete = execution->isfeasibleprefix() &&
-               (execution->is_complete_execution() ||
-                execution->have_bug_reports());
+                                                                       (execution->is_complete_execution() ||
+                                                                        execution->have_bug_reports());
 
        /* End-of-execution bug checks */
        if (complete) {
@@ -237,7 +245,7 @@ bool ModelChecker::next_execution()
 
                checkDataRaces();
                run_trace_analyses();
-       } 
+       }
 
        record_stats();
        /* Output */
@@ -253,13 +261,12 @@ bool ModelChecker::next_execution()
 // test code
        execution_number++;
        reset_to_initial_state();
-       node_stack->full_reset();
        return false;
 }
 
 /** @brief Run trace analyses on complete trace */
 void ModelChecker::run_trace_analyses() {
-       for (unsigned int i = 0; i < trace_analyses.size(); i++)
+       for (unsigned int i = 0;i < trace_analyses.size();i++)
                trace_analyses[i]->analyze(execution->get_action_trace());
 }
 
@@ -313,10 +320,11 @@ uint64_t ModelChecker::switch_to_master(ModelAction *act)
        Thread *old = thread_current();
        scheduler->set_current_thread(NULL);
        ASSERT(!old->get_pending());
-/* W: No plugin
+
        if (inspect_plugin != NULL) {
-               inspect_plugin->inspectModelAction(act); 
-       }*/
+               inspect_plugin->inspectModelAction(act);
+       }
+
        old->set_pending(act);
        if (Thread::swap(old, &system_context) < 0) {
                perror("swap threads");
@@ -325,12 +333,6 @@ uint64_t ModelChecker::switch_to_master(ModelAction *act)
        return old->get_return_value();
 }
 
-/** Wrapper to run the user's main function, with appropriate arguments */
-void user_main_wrapper(void *)
-{
-       user_main(model->params.argc, model->params.argv);
-}
-
 bool ModelChecker::should_terminate_execution()
 {
        /* Infeasible -> don't take any more steps */
@@ -354,7 +356,6 @@ void ModelChecker::do_restart()
 {
        restart_flag = false;
        reset_to_initial_state();
-       node_stack->full_reset();
        memset(&stats,0,sizeof(struct execution_stats));
        execution_number = 1;
 }
@@ -366,11 +367,9 @@ void ModelChecker::run()
        char random_state[256];
        initstate(423121, random_state, sizeof(random_state));
 
-       for(int exec = 0; exec < params.maxexecutions; exec++) {
-               thrd_t user_thread;
-               Thread *t = new Thread(execution->get_next_id(), &user_thread, &user_main_wrapper, NULL, NULL); // L: user_main_wrapper passes the user program
-               execution->add_thread(t);
-               //Need to seed random number generator, otherwise its state gets reset
+       for(int exec = 0;exec < params.maxexecutions;exec++) {
+               Thread * t = init_thread;
+
                do {
                        /*
                         * Stash next pending action(s) for thread(s). There
@@ -379,18 +378,18 @@ void ModelChecker::run()
                         * for any newly-created thread
                         */
 
-                       for (unsigned int i = 0; i < get_num_threads(); i++) {
+                       for (unsigned int i = 0;i < get_num_threads();i++) {
                                thread_id_t tid = int_to_id(i);
                                Thread *thr = get_thread(tid);
                                if (!thr->is_model_thread() && !thr->is_complete() && !thr->get_pending()) {
-                                       switch_from_master(thr);        // L: context swapped, and action type of thr changed. 
+                                       switch_from_master(thr);        // L: context swapped, and action type of thr changed.
                                        if (thr->is_waiting_on(thr))
                                                assert_bug("Deadlock detected (thread %u)", i);
                                }
                        }
 
                        /* Don't schedule threads which should be disabled */
-                       for (unsigned int i = 0; i < get_num_threads(); i++) {
+                       for (unsigned int i = 0;i < get_num_threads();i++) {
                                Thread *th = get_thread(int_to_id(i));
                                ModelAction *act = th->get_pending();
                                if (act && execution->is_enabled(th) && !execution->check_action_enabled(act)) {
@@ -398,33 +397,33 @@ void ModelChecker::run()
                                }
                        }
 
-                       for (unsigned int i = 1; i < get_num_threads(); i++) {
+                       for (unsigned int i = 1;i < get_num_threads();i++) {
                                Thread *th = get_thread(int_to_id(i));
                                ModelAction *act = th->get_pending();
-                               if (act && execution->is_enabled(th) && (th->get_state() != THREAD_BLOCKED) ){
-                                       if (act->is_write()){
-                                               std::memory_order order = act->get_mo(); 
-                                               if (order == std::memory_order_relaxed || \
-                                                       order == std::memory_order_release) {
+                               if (act && execution->is_enabled(th) && (th->get_state() != THREAD_BLOCKED) ) {
+                                       if (act->is_write()) {
+                                               std::memory_order order = act->get_mo();
+                                               if (order == std::memory_order_relaxed || \
+                                                               order == std::memory_order_release) {
                                                        t = th;
                                                        break;
                                                }
                                        } else if (act->get_type() == THREAD_CREATE || \
-                                                       act->get_type() == PTHREAD_CREATE || \
-                                                       act->get_type() == THREAD_START || \
-                                                       act->get_type() == THREAD_FINISH) {
+                                                                                act->get_type() == PTHREAD_CREATE || \
+                                                                                act->get_type() == THREAD_START || \
+                                                                                act->get_type() == THREAD_FINISH) {
                                                t = th;
                                                break;
-                                       }                               
+                                       }
                                }
                        }
 
                        /* Catch assertions from prior take_step or from
-                        * between-ModelAction bugs (e.g., data races) */
+                       * between-ModelAction bugs (e.g., data races) */
 
                        if (execution->has_asserted())
                                break;
-                       if (!t)                         
+                       if (!t)
                                t = get_next_thread();
                        if (!t || t->is_model_thread())
                                break;
@@ -443,6 +442,6 @@ void ModelChecker::run()
        print_stats();
 
        /* Have the trace analyses dump their output. */
-       for (unsigned int i = 0; i < trace_analyses.size(); i++)
+       for (unsigned int i = 0;i < trace_analyses.size();i++)
                trace_analyses[i]->finish();
 }