fix edge case
[c11tester.git] / cmodelint.cc
index c052552ed5936779fba36e52538c0627e417b9c9..d3fcbb4656919157220ed5a4197ee3c58b723e75 100644 (file)
@@ -8,10 +8,11 @@
 #include "cmodelint.h"
 #include "snapshot-interface.h"
 #include "threads-model.h"
+#include "datarace.h"
 
-memory_order orders[6] = {
+memory_order orders[7] = {
        memory_order_relaxed, memory_order_consume, memory_order_acquire,
-       memory_order_release, memory_order_acq_rel, memory_order_seq_cst
+       memory_order_release, memory_order_acq_rel, memory_order_seq_cst,
 };
 
 static void ensureModel() {
@@ -92,12 +93,45 @@ void model_rmwc_action_helper(void *obj, int atomic_index, const char *position)
        model->switch_to_master(new ModelAction(ATOMIC_RMWC, position, orders[atomic_index], obj));
 }
 
+// cds volatile loads
+#define VOLATILELOAD(size) \
+       uint ## size ## _t cds_volatile_load ## size(void * obj, const char * position) { \
+               ensureModel();                                                      \
+               return (uint ## size ## _t)model->switch_to_master(new ModelAction(ATOMIC_READ, position, memory_order_volatile_load, obj)); \
+       }
+
+VOLATILELOAD(8)
+VOLATILELOAD(16)
+VOLATILELOAD(32)
+VOLATILELOAD(64)
+
+// cds volatile stores
+#define VOLATILESTORE(size) \
+       void cds_volatile_store ## size (void * obj, uint ## size ## _t val, const char * position) { \
+               ensureModel();                                                      \
+               model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, memory_order_volatile_store, obj, (uint64_t) val)); \
+               *((volatile uint ## size ## _t *)obj) = val;            \
+               thread_id_t tid = thread_current()->get_id();           \
+               for(int i=0;i < size / 8;i++) {                         \
+                       atomraceCheckWrite(tid, (void *)(((char *)obj)+i));          \
+               }                                                       \
+       }
+
+VOLATILESTORE(8)
+VOLATILESTORE(16)
+VOLATILESTORE(32)
+VOLATILESTORE(64)
+
 // cds atomic inits
 #define CDSATOMICINT(size)                                              \
        void cds_atomic_init ## size (void * obj, uint ## size ## _t val, const char * position) { \
                ensureModel();                                                      \
                model->switch_to_master(new ModelAction(ATOMIC_INIT, position, memory_order_relaxed, obj, (uint64_t) val)); \
-               *((uint ## size ## _t *)obj) = val;                                 \
+               *((volatile uint ## size ## _t *)obj) = val;                                 \
+               thread_id_t tid = thread_current()->get_id();           \
+               for(int i=0;i < size / 8;i++) {                       \
+                       atomraceCheckWrite(tid, (void *)(((char *)obj)+i));          \
+               }                                                       \
        }
 
 CDSATOMICINT(8)
@@ -109,8 +143,13 @@ CDSATOMICINT(64)
 #define CDSATOMICLOAD(size)                                             \
        uint ## size ## _t cds_atomic_load ## size(void * obj, int atomic_index, const char * position) { \
                ensureModel();                                                      \
-               return (uint ## size ## _t) model->switch_to_master( \
+               uint ## size ## _t val = (uint ## size ## _t)model->switch_to_master( \
                        new ModelAction(ATOMIC_READ, position, orders[atomic_index], obj)); \
+               thread_id_t tid = thread_current()->get_id();           \
+               for(int i=0;i < size / 8;i++) {                         \
+                       atomraceCheckRead(tid, (void *)(((char *)obj)+i));    \
+               }                                                       \
+               return val; \
        }
 
 CDSATOMICLOAD(8)
@@ -119,12 +158,15 @@ CDSATOMICLOAD(32)
 CDSATOMICLOAD(64)
 
 // cds atomic stores
-
 #define CDSATOMICSTORE(size)                                            \
        void cds_atomic_store ## size(void * obj, uint ## size ## _t val, int atomic_index, const char * position) { \
                ensureModel();                                                        \
                model->switch_to_master(new ModelAction(ATOMIC_WRITE, position, orders[atomic_index], obj, (uint64_t) val)); \
-               *((uint ## size ## _t *)obj) = val;                                   \
+               *((volatile uint ## size ## _t *)obj) = val;                     \
+               thread_id_t tid = thread_current()->get_id();           \
+               for(int i=0;i < size / 8;i++) {                       \
+                       atomraceCheckWrite(tid, (void *)(((char *)obj)+i));          \
+               }                                                       \
        }
 
 CDSATOMICSTORE(8)
@@ -140,7 +182,13 @@ CDSATOMICSTORE(64)
                uint ## size ## _t _val = val;                                            \
                _copy __op__ _val;                                                    \
                model_rmw_action_helper(addr, (uint64_t) _copy, atomic_index, position);        \
-               return _old;                                                          \
+               *((volatile uint ## size ## _t *)addr) = _copy;                  \
+               thread_id_t tid = thread_current()->get_id();           \
+               for(int i=0;i < size / 8;i++) {                       \
+                       atomraceCheckRead(tid,  (void *)(((char *)addr)+i));  \
+                       recordWrite(tid, (void *)(((char *)addr)+i));         \
+               }                                                       \
+               return _old;                                            \
        })
 
 // cds atomic exchange
@@ -219,7 +267,13 @@ CDSATOMICXOR(64)
                uint ## size ## _t _expected = expected;                                                          \
                uint ## size ## _t _old = model_rmwrcas_action_helper(addr, atomic_index, _expected, sizeof(_expected), position); \
                if (_old == _expected) {                                                                    \
-                       model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); return _expected; }      \
+                       model_rmw_action_helper(addr, (uint64_t) _desired, atomic_index, position); \
+                       *((volatile uint ## size ## _t *)addr) = desired;                        \
+                       thread_id_t tid = thread_current()->get_id();           \
+                       for(int i=0;i < size / 8;i++) {                       \
+                               recordWrite(tid, (void *)(((char *)addr)+i));         \
+                       }                                                       \
+                       return _expected; }                                     \
                else {                                                                                        \
                        model_rmwc_action_helper(addr, atomic_index, position); _expected = _old; return _old; }              \
        })
@@ -286,8 +340,8 @@ void cds_atomic_thread_fence(int atomic_index, const char * position) {
  */
 
 void cds_func_entry(const char * funcName) {
-       if (!model) return;
-
+#ifdef NEWFUZZER
+       ensureModel();
        Thread * th = thread_current();
        uint32_t func_id;
 
@@ -302,29 +356,34 @@ void cds_func_entry(const char * funcName) {
                ModelVector<const char *> * func_map_rev = history->getFuncMapRev();
                if ( func_map_rev->size() <= func_id )
                        func_map_rev->resize( func_id + 1 );
+
                func_map_rev->at(func_id) = funcName;
        } else {
                func_id = history->getFuncMap()->get(funcName);
        }
 
        history->enter_function(func_id, th->get_id());
+#endif
 }
 
 void cds_func_exit(const char * funcName) {
-       if (!model) return;
+#ifdef NEWFUZZER
+       ensureModel();
 
        Thread * th = thread_current();
        uint32_t func_id;
 
        ModelHistory *history = model->get_history();
        func_id = history->getFuncMap()->get(funcName);
-
-       /* func_id not found; this could happen in the case where a function calls cds_func_entry
-        * when the model has been defined yet, but then an atomic inside the function initializes
-        * the model. And then cds_func_exit is called upon the function exiting.
-        */
+/*
+ * func_id not found; this could happen in the case where a function calls cds_func_entry
+ * when the model has been defined yet, but then an atomic inside the function initializes
+ * the model. And then cds_func_exit is called upon the function exiting.
+ *
+ */
        if (func_id == 0)
                return;
 
        history->exit_function(func_id, th->get_id());
+#endif
 }