Try to catch data races for memcpy
authorweiyu <weiyuluo1232@gmail.com>
Tue, 15 Sep 2020 01:17:04 +0000 (18:17 -0700)
committerweiyu <weiyuluo1232@gmail.com>
Tue, 15 Sep 2020 01:17:04 +0000 (18:17 -0700)
clockvector.cc
datarace.cc
main.cc
model.cc
model.h
mymemory.cc
mymemory.h
schedule.cc
stl-model.h

index 9820b8f986b8ba93c465674212d2967e1104fd96..ba5d1f4b2e627a76e41a8e40589b833fc00dd584 100644 (file)
@@ -24,7 +24,7 @@ ClockVector::ClockVector(ClockVector *parent, const ModelAction *act)
 
        clock = (modelclock_t *)snapshot_calloc(num_threads, sizeof(int));
        if (parent)
-               std::memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
+               real_memcpy(clock, parent->clock, parent->num_threads * sizeof(modelclock_t));
 
        if (act != NULL)
                clock[id_to_int(act->get_tid())] = act->get_seq_number();
index 9c403668e90d7c0fbe6ff76cda3cc3f1a2c37d81..13aa4655ce05bf254d66604b3f359622a5c00941 100644 (file)
@@ -575,8 +575,8 @@ struct DataRace * fullRaceCheckRead(thread_id_t thread, const void *location, ui
                        int newCapacity = copytoindex * 2;
                        thread_id_t *newthread = (thread_id_t *)snapshot_malloc(sizeof(thread_id_t) * newCapacity);
                        modelclock_t *newreadClock = (modelclock_t *)snapshot_malloc(sizeof(modelclock_t) * newCapacity);
-                       std::memcpy(newthread, record->thread, copytoindex * sizeof(thread_id_t));
-                       std::memcpy(newreadClock, record->readClock, copytoindex * sizeof(modelclock_t));
+                       real_memcpy(newthread, record->thread, copytoindex * sizeof(thread_id_t));
+                       real_memcpy(newreadClock, record->readClock, copytoindex * sizeof(modelclock_t));
                        snapshot_free(record->readClock);
                        snapshot_free(record->thread);
                        record->readClock = newreadClock;
diff --git a/main.cc b/main.cc
index b0931f757e860a778c46e347dde08f87e978abe0..36055107c696b85003702159646450bbe0db3e04 100644 (file)
--- a/main.cc
+++ b/main.cc
@@ -114,7 +114,7 @@ void parse_options(struct model_params *params) {
        }
        argc++; //first parameter is executable name
        char optcpy[index + 1];
-       memcpy(optcpy, options, index+1);
+       real_memcpy(optcpy, options, index+1);
        char * argv[argc + 1];
        argv[0] = NULL;
        argv[1] = optcpy;
index 4b2143dc36ab4e303840059dd9ed1f7f22cd2c43..450aac5a51f2201c62d4743d94339c0cab0c2469 100644 (file)
--- a/model.cc
+++ b/model.cc
@@ -21,6 +21,7 @@
 #include "plugins.h"
 
 ModelChecker *model = NULL;
+int inside_model = 0;
 
 void placeholder(void *) {
        ASSERT(0);
@@ -80,6 +81,7 @@ ModelChecker::ModelChecker() :
                                                        "Copyright (c) 2013 and 2019 Regents of the University of California. All rights reserved.\n"
                                                        "Distributed under the GPLv2\n"
                                                        "Written by Weiyu Luo, Brian Norris, and Brian Demsky\n\n");
+       init_memory_ops();
        memset(&stats,0,sizeof(struct execution_stats));
        init_thread = new Thread(execution->get_next_id(), (thrd_t *) model_malloc(sizeof(thrd_t)), &placeholder, NULL, NULL);
 #ifdef TLS
@@ -353,7 +355,7 @@ void ModelChecker::startRunExecution(Thread *old) {
                Thread *thr = getNextThread(old);
                if (thr != nullptr) {
                        scheduler->set_current_thread(thr);
-
+                       inside_model = 0;
                        if (Thread::swap(old, thr) < 0) {
                                perror("swap threads");
                                exit(EXIT_FAILURE);
@@ -452,6 +454,8 @@ uint64_t ModelChecker::switch_thread(ModelAction *act)
                delete act;
                return 0;
        }
+       inside_model = 1;
+
        DBG();
        Thread *old = thread_current();
        old->set_state(THREAD_READY);
diff --git a/model.h b/model.h
index e35457e1eea77c9dd653db444303d6284caa7279..331ea18adf96b6e43bcfd8f14551209e3b6ecc6e 100644 (file)
--- a/model.h
+++ b/model.h
@@ -104,6 +104,7 @@ private:
        void print_stats() const;
 };
 
+extern int inside_model;
 extern ModelChecker *model;
 void parse_options(struct model_params *params);
 void install_trace_analyses(ModelExecution *execution);
index dd22909c9bb31a32405070cb2e6873ac5d584e95..e82f8d2ef997f31f0d2d38871c74dd6cf17a8cbd 100644 (file)
@@ -110,3 +110,69 @@ void Thread_free(void *ptr)
 {
        snapshot_free(ptr);
 }
+
+void * (*volatile real_memcpy)(void * dst, const void *src, size_t n) = NULL;
+void * (*volatile real_memmove)(void * dst, const void *src, size_t len) = NULL;
+void (*volatile real_bzero)(void * dst, size_t len) = NULL;
+void * (*volatile real_memset)(void * dst, int c, size_t len) = NULL;
+
+void init_memory_ops()
+{
+       if (!real_memcpy) {
+               real_memcpy = (void * (*)(void * dst, const void *src, size_t n)) 1;
+               real_memcpy = (void * (*)(void * dst, const void *src, size_t n))dlsym(RTLD_NEXT, "memcpy");
+       }
+       if (!real_memmove) {
+               real_memmove = (void * (*)(void * dst, const void *src, size_t n)) 1;
+               real_memmove = (void * (*)(void * dst, const void *src, size_t n))dlsym(RTLD_NEXT, "memmove");
+       }
+       if (!real_memset) {
+               real_memset = (void * (*)(void * dst, int c, size_t n)) 1;
+               real_memset = (void * (*)(void * dst, int c, size_t n))dlsym(RTLD_NEXT, "memset");
+       }
+       if (!real_bzero) {
+               real_bzero = (void (*)(void * dst, size_t len)) 1;
+               real_bzero = (void (*)(void * dst, size_t len))dlsym(RTLD_NEXT, "bzero");
+       }
+}
+
+void * memcpy(void * dst, const void * src, size_t n) {
+       if (false && model && !inside_model) {
+               thread_id_t tid = thread_current_id();
+               if (((uintptr_t)src&7) == 0 && ((uintptr_t)dst&7) == 0 && (n&7) == 0) {
+                       for (uint i = 0; i < (n>>3); i++) {
+                               raceCheckRead64(tid, (void *)(((char *)src) + i));
+                               ((volatile uint64_t *)dst)[i] = ((uint64_t *)src)[i];
+                               raceCheckWrite64(tid, (void *)(((char *)src) + i));
+                       }
+               } else if (((uintptr_t)src&3) == 0 && ((uintptr_t)dst&3) == 0 && (n&3) == 0) {
+                       for (uint i = 0; i < (n>>2); i++) {
+                               raceCheckRead32(tid, (void *)(((char *)src) + i));
+                               ((volatile uint32_t *)dst)[i] = ((uint32_t *)src)[i];
+                               raceCheckWrite32(tid, (void *)(((char *)src) + i));
+                       }
+               } else if (((uintptr_t)src&1) == 0 && ((uintptr_t)dst&1) == 0 && (n&1) == 0) {
+                       for (uint i = 0; i < (n>>1); i++) {
+                               raceCheckRead16(tid, (void *)(((char *)src) + i));
+                               ((volatile uint16_t *)dst)[i] = ((uint16_t *)src)[i];
+                               raceCheckWrite16(tid, (void *)(((char *)src) + i));
+                       }
+               } else {
+                       for(uint i=0;i<n;i++) {
+                               raceCheckRead8(tid, (void *)(((char *)src) + i));
+                               ((volatile char *)dst)[i] = ((char *)src)[i];
+                               raceCheckWrite8(tid, (void *)(((char *)src) + i));
+                       }
+               }
+       } else {
+               if (((uintptr_t)real_memcpy) < 2) {
+                       for(uint i=0;i<n;i++) {
+                               ((volatile char *)dst)[i] = ((char *)src)[i];
+                       }
+                       return dst;
+               }
+
+               return real_memcpy(dst, src, n);
+       }
+       return dst;
+}
index 6fb2992bd4e4e93abce31d6002957830575c7081..88d92dca1dfef0b97111943f8d6f9c7549272c84 100644 (file)
@@ -63,6 +63,8 @@ extern mspace sStaticSpace;
 void * Thread_malloc(size_t size);
 void Thread_free(void *ptr);
 
+void init_memory_ops();
+
 /** @brief Provides a non-snapshotting allocator for use in STL classes.
  *
  * The code was adapted from a code example from the book The C++
@@ -260,6 +262,11 @@ extern mspace create_mspace(size_t capacity, int locked);
 
 extern mspace model_snapshot_space;
 
+extern void * (*volatile real_memcpy)(void * dst, const void *src, size_t n);
+extern void * (*volatile real_memmove)(void * dst, const void *src, size_t len);
+extern void (*volatile real_bzero)(void * dst, size_t len);
+extern void * (*volatile real_memset)(void * dst, int c, size_t len);
+
 #ifdef __cplusplus
 };     /* end of extern "C" */
 #endif
index f55c7b0deed28d98cc89530fb8c88a32fc99c1e4..74ecf406e3cff544ef195d7a48178931ba7cb8cb 100644 (file)
@@ -59,7 +59,7 @@ void Scheduler::set_enabled(Thread *t, enabled_type_t enabled_status) {
                enabled_type_t *new_enabled = (enabled_type_t *)snapshot_malloc(sizeof(enabled_type_t) * (threadid + 1));
                memset(&new_enabled[enabled_len], 0, (threadid + 1 - enabled_len) * sizeof(enabled_type_t));
                if (enabled != NULL) {
-                       memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
+                       real_memcpy(new_enabled, enabled, enabled_len * sizeof(enabled_type_t));
                        snapshot_free(enabled);
                }
                enabled = new_enabled;
index 611520fc70515fa745239df32b4248d1db8068d6..53e412061d2178fc3f839882fa883400c57b4944 100644 (file)
@@ -369,7 +369,7 @@ public:
                _size(_capacity),
                capacity(_capacity),
                array((type *) model_malloc(sizeof(type) * _capacity)) {
-               memcpy(array, _array, capacity * sizeof(type));
+               real_memcpy(array, _array, capacity * sizeof(type));
        }
        void pop_back() {
                _size--;
@@ -474,7 +474,7 @@ public:
                _size(_capacity),
                capacity(_capacity),
                array((type *) snapshot_malloc(sizeof(type) * _capacity)) {
-               memcpy(array, _array, capacity * sizeof(type));
+               real_memcpy(array, _array, capacity * sizeof(type));
        }
        void pop_back() {
                _size--;