Merge branch 'master' of /home/git/random-fuzzer into thread-switch
authorweiyu <weiyuluo1232@gmail.com>
Tue, 25 Aug 2020 22:14:48 +0000 (15:14 -0700)
committerweiyu <weiyuluo1232@gmail.com>
Tue, 25 Aug 2020 22:14:48 +0000 (15:14 -0700)
1  2 
model.cc
model.h
mutex.cc
pthread.cc
threads-model.h
threads.cc

diff --combined model.cc
index d9671a30b7b31bac2b182135edf8c7f26bd1e247,f1df6ae8bfbbdacd806f56976de6b1c2f2bc8099..a37e00521ea7761608db62b32cec81faadbcf5c4
+++ b/model.cc
@@@ -26,6 -26,37 +26,37 @@@ void placeholder(void *) 
        ASSERT(0);
  }
  
+ #include <signal.h>
+ #define SIGSTACKSIZE 65536
+ static void mprot_handle_pf(int sig, siginfo_t *si, void *unused)
+ {
+       model_print("Segmentation fault at %p\n", si->si_addr);
+       model_print("For debugging, place breakpoint at: %s:%d\n",
+                                                       __FILE__, __LINE__);
+       print_trace();  // Trace printing may cause dynamic memory allocation
+       while(1)
+               ;
+ }
+ void install_handler() {
+       stack_t ss;
+       ss.ss_sp = model_malloc(SIGSTACKSIZE);
+       ss.ss_size = SIGSTACKSIZE;
+       ss.ss_flags = 0;
+       sigaltstack(&ss, NULL);
+       struct sigaction sa;
+       sa.sa_flags = SA_SIGINFO | SA_NODEFER | SA_RESTART | SA_ONSTACK;
+       sigemptyset(&sa.sa_mask);
+       sa.sa_sigaction = mprot_handle_pf;
+       if (sigaction(SIGSEGV, &sa, NULL) == -1) {
+               perror("sigaction(SIGSEGV)");
+               exit(EXIT_FAILURE);
+       }
+ }
  /** @brief Constructor */
  ModelChecker::ModelChecker() :
        /* Initialize default scheduler */
@@@ -54,8 -85,7 +85,7 @@@
        parse_options(&params);
        initRaceDetector();
        /* Configure output redirection for the model-checker */
-       redirect_output();
-       install_trace_analyses(get_execution());
+       install_handler();
  }
  
  /** @brief Destructor */
@@@ -262,6 -292,7 +292,7 @@@ void ModelChecker::finish_execution(boo
        execution_number ++;
        if (more_executions)
                reset_to_initial_state();
        history->set_new_exec_flag();
  }
  
@@@ -344,191 -375,6 +375,191 @@@ uint64_t ModelChecker::switch_to_master
        return old->get_return_value();
  }
  
 +void ModelChecker::startRunExecution(Thread *old) 
 +{
 +      if (params.traceminsize != 0 &&
 +                      execution->get_curr_seq_num() > checkfree) {
 +              checkfree += params.checkthreshold;
 +              execution->collectActions();
 +      }
 +
 +      thread_chosen = false;
 +      curr_thread_num = 1;
 +      Thread *thr = getNextThread();
 +      if (thr != nullptr) {
 +              scheduler->set_current_thread(thr);
 +              if (old) {
 +                      if (Thread::swap(old, thr) < 0) {
 +                              perror("swap threads");
 +                              exit(EXIT_FAILURE);
 +                      }
 +              } else {
 +                      if (Thread::swap(&system_context, thr) < 0) {
 +                              perror("swap threads");
 +                              exit(EXIT_FAILURE);
 +                      }       
 +              }
 +      } else
 +              handleChosenThread(old);
 +}
 +
 +Thread* ModelChecker::getNextThread()
 +{
 +      Thread *nextThread = nullptr;
 +      for (unsigned int i = curr_thread_num; i < get_num_threads(); i++) {
 +              thread_id_t tid = int_to_id(i);
 +              Thread *thr = get_thread(tid);
 +              
 +              if (!thr->is_complete() && !thr->get_pending()) {
 +                      curr_thread_num = i;
 +                      nextThread = thr;
 +                      break;
 +              }
 +              ModelAction *act = thr->get_pending();
 +              
 +              if (act && execution->is_enabled(thr) && !execution->check_action_enabled(act)) {
 +                      scheduler->sleep(thr);
 +              }
 +              chooseThread(act, thr);
 +      }
 +      return nextThread;
 +}
 +
 +/* Swap back to system_context and terminate this execution */
 +void ModelChecker::finishRunExecution(Thread *old) 
 +{
 +      scheduler->set_current_thread(NULL);
 +      if (old != NULL) {
 +              if (Thread::swap(old, &system_context) < 0) {
 +                      perror("swap threads");
 +                      exit(EXIT_FAILURE);
 +              }
 +      }
 +      break_execution = true;
 +}
 +
 +void ModelChecker::consumeAction()
 +{
 +      ModelAction *curr = chosen_thread->get_pending();
 +      Thread * th = thread_current();
 +      if (curr->get_type() == THREAD_FINISH && th != NULL)  {
 +              // Thread finish must be consumed in the master context
 +              scheduler->set_current_thread(NULL);
 +              if (Thread::swap(th, &system_context) < 0) {
 +                      perror("swap threads");
 +                      exit(EXIT_FAILURE);
 +              }
 +      } else {
 +              chosen_thread->set_pending(NULL);
 +              chosen_thread = execution->take_step(curr);
 +      }
 +}
 +
 +void ModelChecker::chooseThread(ModelAction *act, Thread *thr)
 +{
 +      if (!thread_chosen && act && execution->is_enabled(thr) && (thr->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) {
 +                              chosen_thread = thr;
 +                              thread_chosen = true;
 +                      }
 +              } else if (act->get_type() == THREAD_CREATE || \
 +                                                      act->get_type() == PTHREAD_CREATE || \
 +                                                      act->get_type() == THREAD_START || \
 +                                                      act->get_type() == THREAD_FINISH) {
 +                      chosen_thread = thr;
 +                      thread_chosen = true;
 +              }
 +      }       
 +}
 +
 +uint64_t ModelChecker::switch_thread(ModelAction *act)
 +{
 +      if (modellock) {
 +              static bool fork_message_printed = false;
 +
 +              if (!fork_message_printed) {
 +                      model_print("Fork handler or dead thread trying to call into model checker...\n");
 +                      fork_message_printed = true;
 +              }
 +              delete act;
 +              return 0;
 +      }
 +      DBG();
 +      Thread *old = thread_current();
 +      ASSERT(!old->get_pending());
 +
 +      if (inspect_plugin != NULL) {
 +              inspect_plugin->inspectModelAction(act);
 +      }
 +
 +      old->set_pending(act);
 +      
 +      if (old->is_waiting_on(old))
 +              assert_bug("Deadlock detected (thread %u)", curr_thread_num);
 +
 +      if (act && execution->is_enabled(old) && !execution->check_action_enabled(act)) {
 +              scheduler->sleep(old);
 +      }
 +      chooseThread(act, old);
 +
 +      curr_thread_num++;
 +      Thread* next = getNextThread();
 +      if (next != nullptr) 
 +              handleNewValidThread(old, next);
 +      else {
 +              old->set_state(THREAD_READY);   // Just to avoid the first ASSERT in ModelExecution::take_step 
 +              handleChosenThread(old);
 +      }
 +
 +      return old->get_return_value();
 +}
 +
 +void ModelChecker::handleNewValidThread(Thread *old, Thread *next)
 +{
 +      scheduler->set_current_thread(next);
 +
 +      if (Thread::swap(old, next) < 0) {
 +              perror("swap threads");
 +              exit(EXIT_FAILURE);
 +      }
 +}
 +
 +void ModelChecker::handleChosenThread(Thread *old)
 +{
 +      Thread * th = old ? old : thread_current();
 +      if (execution->has_asserted()) {
 +              finishRunExecution(th);
 +              return;
 +      }
 +      if (!chosen_thread)
 +              chosen_thread = get_next_thread();
 +      if (!chosen_thread || chosen_thread->is_model_thread()) {
 +              finishRunExecution(th);
 +              return;
 +      }
 +      if (chosen_thread->just_woken_up()) {
 +              chosen_thread->set_wakeup_state(false);
 +              chosen_thread->set_pending(NULL);
 +              chosen_thread = NULL;
 +              // Allow this thread to stash the next pending action
 +//            if (should_terminate_execution())
 +//                    finishRunExecution(th);
 +//            else
 +                      startRunExecution(th);
 +      } else {
 +              /* Consume the next action for a Thread */
 +              consumeAction();
 +
 +              if (should_terminate_execution())
 +                      finishRunExecution(th);
 +              else
 +                      startRunExecution(th);
 +      }
 +}
 +
  static void runChecker() {
        model->run();
        delete model;
  void ModelChecker::startChecker() {
        startExecution(get_system_context(), runChecker);
        snapshot = take_snapshot();
+       install_trace_analyses(get_execution());
+       redirect_output();
        initMainThread();
  }
  
@@@ -557,17 -406,86 +591,17 @@@ void ModelChecker::run(
        //Need to initial random number generator state to avoid resets on rollback
        char random_state[256];
        initstate(423121, random_state, sizeof(random_state));
 -      modelclock_t checkfree = params.checkthreshold;
 +      checkfree = params.checkthreshold;
        for(int exec = 0;exec < params.maxexecutions;exec++) {
 -              Thread * t = init_thread;
 -
 +              chosen_thread = init_thread;
 +              break_execution = false;
                do {
 -                      /* Check whether we need to free model actions. */
 -
 -                      if (params.traceminsize != 0 &&
 -                                      execution->get_curr_seq_num() > checkfree) {
 -                              checkfree += params.checkthreshold;
 -                              execution->collectActions();
 -                      }
 -
 -                      /*
 -                       * Stash next pending action(s) for thread(s). There
 -                       * should only need to stash one thread's action--the
 -                       * thread which just took a step--plus the first step
 -                       * for any newly-created thread
 -                       */
 -                      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);
 -                                      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++) {
 -                              Thread *th = get_thread(int_to_id(i));
 -                              ModelAction *act = th->get_pending();
 -                              if (act && execution->is_enabled(th) && !execution->check_action_enabled(act)) {
 -                                      scheduler->sleep(th);
 -                              }
 -                      }
 -
 -                      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) {
 -                                                      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) {
 -                                              t = th;
 -                                              break;
 -                                      }
 -                              }
 -                      }
 -
 -                      /* Catch assertions from prior take_step or from
 -                      * between-ModelAction bugs (e.g., data races) */
 -
 -                      if (execution->has_asserted())
 -                              break;
 -                      if (!t)
 -                              t = get_next_thread();
 -                      if (!t || t->is_model_thread())
 +                      if (break_execution)
                                break;
 -                      if (t->just_woken_up()) {
 -                              t->set_wakeup_state(false);
 -                              t->set_pending(NULL);
 -                              t = NULL;
 -                              continue;       // Allow this thread to stash the next pending action
 -                      }
  
 -                      /* Consume the next action for a Thread */
 -                      ModelAction *curr = t->get_pending();
 -                      t->set_pending(NULL);
 -                      t = execution->take_step(curr);
 +                      startRunExecution(NULL);
                } while (!should_terminate_execution());
 +
                finish_execution((exec+1) < params.maxexecutions);
                //restore random number generator state after rollback
                setstate(random_state);
diff --combined model.h
index 60e19f634de146247d59386345fa35da2d9450cb,634748130ce51713d014c07903f10edf4d3a8fe1..ffc1a0c64b3175785440806f1be8c758b8ee62c1
+++ b/model.h
@@@ -18,8 -18,6 +18,6 @@@
  #include "classlist.h"
  #include "snapshot-interface.h"
  
- typedef SnapList<ModelAction *> action_list_t;
  /** @brief Model checker execution stats */
  struct execution_stats {
        int num_total;  /**< @brief Total number of executions */
@@@ -53,15 -51,6 +51,15 @@@ public
  
        void switch_from_master(Thread *thread);
        uint64_t switch_to_master(ModelAction *act);
 +      uint64_t switch_thread(ModelAction *act);
 +
 +      void startRunExecution(Thread *old);
 +      void finishRunExecution(Thread *old);
 +      void consumeAction();
 +      void chooseThread(ModelAction *act, Thread *thr);
 +      Thread * getNextThread();
 +      void handleChosenThread(Thread *old);
 +      void handleNewValidThread(Thread *old, Thread *next);
  
        void assert_bug(const char *msg, ...);
  
@@@ -86,15 -75,6 +84,15 @@@ private
  
        int execution_number;
  
 +      unsigned int curr_thread_num;
 +
 +      Thread * chosen_thread;
 +
 +      bool thread_chosen;
 +      bool break_execution;
 +
 +      modelclock_t checkfree;
 +
        unsigned int get_num_threads() const;
  
        void finish_execution(bool moreexecutions);
diff --combined mutex.cc
index 27e4a080a69de852f57295e46ad34f9e59e84464,10fb5e3248b0550dd8b18afea34164380306ff23..8b5d33b23e64f0c090633a498b330a9fd208eca4
+++ b/mutex.cc
@@@ -8,28 -8,32 +8,32 @@@
  
  namespace cdsc {
  
- mutex::mutex()
+ mutex::mutex(int type)
  {
        state.locked = NULL;
        thread_id_t tid = thread_current()->get_id();
        state.alloc_tid = tid;
        ClockVector *cv = model->get_execution()->get_cv(tid);
        state.alloc_clock = cv  == NULL ? 0 : cv->getClock(tid);
+       // For recursive mutex
+       state.type = type;
+       state.lock_count = 0;
  }
  
  void mutex::lock()
  {
 -      model->switch_to_master(new ModelAction(ATOMIC_LOCK, std::memory_order_seq_cst, this));
 +      model->switch_thread(new ModelAction(ATOMIC_LOCK, std::memory_order_seq_cst, this));
  }
  
  bool mutex::try_lock()
  {
 -      return model->switch_to_master(new ModelAction(ATOMIC_TRYLOCK, std::memory_order_seq_cst, this));
 +      return model->switch_thread(new ModelAction(ATOMIC_TRYLOCK, std::memory_order_seq_cst, this));
  }
  
  void mutex::unlock()
  {
 -      model->switch_to_master(new ModelAction(ATOMIC_UNLOCK, std::memory_order_seq_cst, this));
 +      model->switch_thread(new ModelAction(ATOMIC_UNLOCK, std::memory_order_seq_cst, this));
  }
  
  }
diff --combined pthread.cc
index e453f1b27e45b2188c73231fbac3d436cd15ac6a,50214c34b31d6d578e4d732c11865eab9b3a3a89..f5ce1bcf514e23c7a3e3b4e3f8facd82fa8b6ce2
@@@ -24,8 -24,10 +24,8 @@@ int pthread_create(pthread_t *t, const 
  
        struct pthread_params params = { start_routine, arg };
  
 -      ModelAction *act = new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params);
 -
        /* seq_cst is just a 'don't care' parameter */
 -      model->switch_to_master(act);
 +      model->switch_to_master(new ModelAction(PTHREAD_CREATE, std::memory_order_seq_cst, t, (uint64_t)&params));
  
        return 0;
  }
@@@ -52,7 -54,7 +52,7 @@@ int pthread_detach(pthread_t t) 
  
  /* Take care of both pthread_yield and c++ thread yield */
  int sched_yield() {
 -      model->switch_to_master(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, thread_current(), VALUE_NONE));
 +      model->switch_thread(new ModelAction(THREAD_YIELD, std::memory_order_seq_cst, thread_current(), VALUE_NONE));
        return 0;
  }
  
@@@ -64,13 -66,18 +64,18 @@@ void pthread_exit(void *value_ptr) 
        real_pthread_exit(NULL);
  }
  
- int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t *) {
+ int pthread_mutex_init(pthread_mutex_t *p_mutex, const pthread_mutexattr_t * attr) {
        if (!model) {
                snapshot_system_init(10000, 1024, 1024, 40000);
                model = new ModelChecker();
                model->startChecker();
        }
-       cdsc::snapmutex *m = new cdsc::snapmutex();
+       int mutex_type = PTHREAD_MUTEX_DEFAULT;
+       if (attr != NULL)
+               pthread_mutexattr_gettype(attr, &mutex_type);
+       cdsc::snapmutex *m = new cdsc::snapmutex(mutex_type);
  
        ModelExecution *execution = model->get_execution();
        execution->getMutexMap()->put(p_mutex, m);
@@@ -160,6 -167,12 +165,12 @@@ int pthread_mutex_timedlock (pthread_mu
  }
  
  pthread_t pthread_self() {
+       if (!model) {
+               snapshot_system_init(10000, 1024, 1024, 40000);
+               model = new ModelChecker();
+               model->startChecker();
+       }
        Thread* th = model->get_current_thread();
        return (pthread_t)th->get_id();
  }
@@@ -203,7 -216,7 +214,7 @@@ int pthread_cond_timedwait(pthread_cond
        cdsc::snapcondition_variable *v = execution->getCondMap()->get(p_cond);
        cdsc::snapmutex *m = execution->getMutexMap()->get(p_mutex);
  
 -      model->switch_to_master(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
 +      model->switch_thread(new ModelAction(ATOMIC_TIMEDWAIT, std::memory_order_seq_cst, v, (uint64_t) m));
        m->lock();
  
        // model_print("Timed_wait is called\n");
@@@ -244,3 -257,37 +255,37 @@@ int pthread_cond_destroy(pthread_cond_
        }
        return 0;
  }
+ /* https://github.com/lattera/glibc/blob/master/nptl/pthread_getattr_np.c */
+ int pthread_getattr_np(pthread_t t, pthread_attr_t *attr)
+ {
+       ModelExecution *execution = model->get_execution();
+       Thread *th = execution->get_pthread(t);
+       struct pthread_attr *iattr = (struct pthread_attr *) attr;
+       /* The sizes are subject to alignment.  */
+       if (th != NULL) {
+ #if _STACK_GROWS_DOWN
+               ASSERT(false);
+ #else
+               iattr->stackaddr = (char *) th->get_stack_addr();
+ #endif
+       } else {
+               ASSERT(false);
+       }
+       return 0;
+ }
+ int pthread_setname_np(pthread_t t, const char *name)
+ {
+       ModelExecution *execution = model->get_execution();
+       Thread *th = execution->get_pthread(t);
+       if (th != NULL)
+               return 0;
+       return 1;
+ }
diff --combined threads-model.h
index 5756c1029688fb6fd9d29616291d496129592f7a,f0b88bb25fea3abd10f8048ac55251460b99dca5..8047e1f33d963d001fab179feab210c405ed7f34
@@@ -49,7 -49,6 +49,7 @@@ public
  
        static int swap(ucontext_t *ctxt, Thread *t);
        static int swap(Thread *t, ucontext_t *ctxt);
 +      static int swap(Thread *t, Thread *t2);
  
        thread_state get_state() const { return state; }
        void set_state(thread_state s);
  
        bool is_model_thread() const { return model_thread; }
  
+       void * get_stack_addr() { return stack; }
+       ClockVector * get_acq_fence_cv() { return acq_fence_cv; }
        friend void thread_startup();
  #ifdef TLS
        friend void setup_context();
@@@ -136,6 -138,9 +139,9 @@@ private
        /** @brief The parent Thread which created this Thread */
        Thread * const parent;
  
+       /** @brief Acquire fence cv */
+       ClockVector *acq_fence_cv;
        /** @brief The THREAD_CREATE ModelAction which created this Thread */
        ModelAction *creation;
  
        void *arg;
        ucontext_t context;
        void *stack;
+       uint32_t stack_size;
  #ifdef TLS
        void * helper_stack;
  public:
diff --combined threads.cc
index 8af66bde53a6946a153d2009c04e554495d4b8eb,a576697f561087fe2c09c607e44ef02bb0c6fa8c..23041b28b55db34d617b86b25e8f3c527810b918
@@@ -14,6 -14,7 +14,7 @@@
  #include "model.h"
  #include "execution.h"
  #include "schedule.h"
+ #include "clockvector.h"
  
  #ifdef TLS
  #include <dlfcn.h>
@@@ -242,7 -243,7 +243,7 @@@ void setup_context() 
        Thread * curr_thread = thread_current();
  
        /* Add dummy "start" action, just to create a first clock vector */
 -      model->switch_to_master(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
 +      model->switch_thread(new ModelAction(THREAD_START, std::memory_order_seq_cst, curr_thread));
  
        real_init_all();
  
@@@ -330,16 -331,6 +331,16 @@@ int Thread::swap(ucontext_t *ctxt, Thre
        return model_swapcontext(ctxt, &t->context);
  }
  
 +int Thread::swap(Thread *t, Thread *t2)
 +{
 +      t->set_state(THREAD_READY);
 +      t2->set_state(THREAD_RUNNING);
 +#ifdef TLS
 +      if (t2->tls != NULL)
 +              set_tls_addr((uintptr_t)t2->tls);
 +#endif
 +      return model_swapcontext(&t->context, &t2->context);
 +}
  
  /** Terminate a thread and free its stack. */
  void Thread::complete()
   */
  Thread::Thread(thread_id_t tid) :
        parent(NULL),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(NULL),
   */
  Thread::Thread(thread_id_t tid, thrd_t *t, void (*func)(void *), void *a, Thread *parent) :
        parent(parent),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(func),
   */
  Thread::Thread(thread_id_t tid, thrd_t *t, void *(*func)(void *), void *a, Thread *parent) :
        parent(parent),
+       acq_fence_cv(new ClockVector()),
        creation(NULL),
        pending(NULL),
        start_routine(NULL),
@@@ -454,6 -448,8 +458,8 @@@ Thread::~Thread(
  {
        if (!is_complete())
                complete();
+       delete acq_fence_cv;
  }
  
  /** @return The thread_id_t corresponding to this Thread object. */
@@@ -500,6 -496,14 +506,14 @@@ Thread * Thread::waiting_on() cons
  bool Thread::is_waiting_on(const Thread *t) const
  {
        Thread *wait;
+       // One thread relocks a recursive mutex
+       if (waiting_on() == t && pending->is_lock()) {
+               int mutex_type = pending->get_mutex()->get_state()->type;
+               if (mutex_type == PTHREAD_MUTEX_RECURSIVE)
+                       return false;
+       }
        for (wait = waiting_on();wait != NULL;wait = wait->waiting_on())
                if (wait == t)
                        return true;