4db4a6
commit 9abfed23c0d430aafb85de6397d171316c982792
4db4a6
Author: Paul Floyd <pjfloyd@wanadoo.fr>
4db4a6
Date:   Fri Nov 19 08:34:53 2021 +0100
4db4a6
4db4a6
    Bug 445504 Using C++ condition_variable results in bogus "mutex is locked simultaneously by two threads" warning(edit)
4db4a6
    
4db4a6
    Add intercepts for pthread_cond_clockwait to DRD and Helgrind
4db4a6
    Also testcase from bugzilla done by Bart, with configure check
4db4a6
4db4a6
diff --git a/configure.ac b/configure.ac
4db4a6
index e7381f205..cb836dbff 100755
4db4a6
--- a/configure.ac
4db4a6
+++ b/configure.ac
4db4a6
@@ -1989,6 +1989,27 @@ AC_LANG(C)
4db4a6
 
4db4a6
 AM_CONDITIONAL(CXX_CAN_INCLUDE_THREAD_HEADER, test x$ac_cxx_can_include_thread_header = xyes)
4db4a6
 
4db4a6
+# Check whether compiler can process #include <condition_variable> without errors
4db4a6
+
4db4a6
+AC_MSG_CHECKING([that C++ compiler can include <condition_variable> header file])
4db4a6
+AC_LANG(C++)
4db4a6
+safe_CXXFLAGS=$CXXFLAGS
4db4a6
+CXXFLAGS=-std=c++0x
4db4a6
+
4db4a6
+AC_COMPILE_IFELSE([AC_LANG_SOURCE([
4db4a6
+#include <condition_variable> 
4db4a6
+])],
4db4a6
+[
4db4a6
+ac_cxx_can_include_condition_variable_header=yes
4db4a6
+AC_MSG_RESULT([yes])
4db4a6
+], [
4db4a6
+ac_cxx_can_include_condition_variable_header=no
4db4a6
+AC_MSG_RESULT([no])
4db4a6
+])
4db4a6
+CXXFLAGS=$safe_CXXFLAGS
4db4a6
+AC_LANG(C)
4db4a6
+
4db4a6
+AM_CONDITIONAL(CXX_CAN_INCLUDE_CONDITION_VARIABLE_HEADER, test x$ac_cxx_can_include_condition_variable_header = xyes)
4db4a6
 
4db4a6
 # On aarch64 before glibc 2.20 we would get the kernel user_pt_regs instead
4db4a6
 # of the user_regs_struct from sys/user.h. They are structurally the same
4db4a6
diff --git a/drd/drd_pthread_intercepts.c b/drd/drd_pthread_intercepts.c
4db4a6
index 8b4454364..95127b42c 100644
4db4a6
--- a/drd/drd_pthread_intercepts.c
4db4a6
+++ b/drd/drd_pthread_intercepts.c
4db4a6
@@ -1175,6 +1175,30 @@ PTH_FUNCS(int, condZureltimedwait, pthread_cond_timedwait_intercept,
4db4a6
           (cond, mutex, timeout));
4db4a6
 #endif /* VGO_solaris */
4db4a6
 
4db4a6
+
4db4a6
+static __always_inline
4db4a6
+int pthread_cond_clockwait_intercept(pthread_cond_t *cond,
4db4a6
+                                     pthread_mutex_t *mutex,
4db4a6
+                                     clockid_t clockid,
4db4a6
+                                     const struct timespec* abstime)
4db4a6
+{
4db4a6
+   int   ret;
4db4a6
+   OrigFn fn;
4db4a6
+   VALGRIND_GET_ORIG_FN(fn);
4db4a6
+   VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__PRE_COND_WAIT,
4db4a6
+                                   cond, mutex, DRD_(mutex_type)(mutex), 0, 0);
4db4a6
+   CALL_FN_W_WWWW(ret, fn, cond, mutex, clockid, abstime);
4db4a6
+   VALGRIND_DO_CLIENT_REQUEST_STMT(VG_USERREQ__POST_COND_WAIT,
4db4a6
+                                   cond, mutex, 1, 0, 0);
4db4a6
+   return ret;
4db4a6
+}
4db4a6
+
4db4a6
+PTH_FUNCS(int, pthreadZucondZuclockwait, pthread_cond_clockwait_intercept,
4db4a6
+          (pthread_cond_t *cond, pthread_mutex_t *mutex,
4db4a6
+            clockid_t clockid, const struct timespec* abstime),
4db4a6
+          (cond, mutex, clockid, abstime));
4db4a6
+
4db4a6
+
4db4a6
 // NOTE: be careful to intercept only pthread_cond_signal() and not Darwin's
4db4a6
 // pthread_cond_signal_thread_np(). The former accepts one argument; the latter
4db4a6
 // two. Intercepting all pthread_cond_signal* functions will cause only one
4db4a6
diff --git a/drd/tests/Makefile.am b/drd/tests/Makefile.am
4db4a6
index 4cb2f7f84..c804391e8 100755
4db4a6
--- a/drd/tests/Makefile.am
4db4a6
+++ b/drd/tests/Makefile.am
4db4a6
@@ -105,6 +105,8 @@ EXTRA_DIST =                                        \
4db4a6
 	circular_buffer.vgtest			    \
4db4a6
 	concurrent_close.stderr.exp		    \
4db4a6
 	concurrent_close.vgtest			    \
4db4a6
+	condvar.stderr.exp			    \
4db4a6
+	condvar.vgtest				    \
4db4a6
 	custom_alloc.stderr.exp			    \
4db4a6
 	custom_alloc.vgtest			    \
4db4a6
 	custom_alloc_fiw.stderr.exp		    \
4db4a6
@@ -458,6 +460,11 @@ check_PROGRAMS += \
4db4a6
 endif
4db4a6
 endif
4db4a6
 
4db4a6
+if CXX_CAN_INCLUDE_CONDITION_VARIABLE_HEADER
4db4a6
+check_PROGRAMS += \
4db4a6
+    condvar
4db4a6
+endif
4db4a6
+
4db4a6
 if HAVE_OPENMP
4db4a6
 check_PROGRAMS += omp_matinv omp_prime omp_printf
4db4a6
 endif
4db4a6
@@ -502,6 +509,8 @@ LDADD = -lpthread
4db4a6
 
4db4a6
 
4db4a6
 bug322621_SOURCES           = bug322621.cpp
4db4a6
+condvar_SOURCES		    = condvar.cpp
4db4a6
+condvar_CXXFLAGS            = $(AM_CXXFLAGS) -std=c++0x
4db4a6
 concurrent_close_SOURCES    = concurrent_close.cpp
4db4a6
 if !VGCONF_OS_IS_FREEBSD
4db4a6
 dlopen_main_LDADD           = -ldl
4db4a6
diff --git a/drd/tests/condvar.cpp b/drd/tests/condvar.cpp
4db4a6
new file mode 100644
4db4a6
index 000000000..18ecb3f8a
4db4a6
--- /dev/null
4db4a6
+++ b/drd/tests/condvar.cpp
4db4a6
@@ -0,0 +1,55 @@
4db4a6
+/* See also https://bugs.kde.org/show_bug.cgi?id=445504 */
4db4a6
+
4db4a6
+#include <condition_variable>
4db4a6
+#include <future>
4db4a6
+#include <iostream>
4db4a6
+#include <mutex>
4db4a6
+#include <thread>
4db4a6
+#include <vector>
4db4a6
+
4db4a6
+using lock_guard = std::lock_guard<std::mutex>;
4db4a6
+using unique_lock = std::unique_lock<std::mutex>;
4db4a6
+
4db4a6
+struct state {
4db4a6
+  std::mutex m;
4db4a6
+  std::vector<int> v;
4db4a6
+  std::condition_variable cv;
4db4a6
+
4db4a6
+  state() {
4db4a6
+    // Call pthread_cond_init() explicitly to let DRD know about 'cv'.
4db4a6
+    pthread_cond_init(cv.native_handle(), NULL);
4db4a6
+  }
4db4a6
+};
4db4a6
+
4db4a6
+void other_thread(state *sp) {
4db4a6
+  state &s = *sp;
4db4a6
+  std::cerr << "Other thread: waiting for notify\n";
4db4a6
+  unique_lock l{s.m};
4db4a6
+  while (true) {
4db4a6
+    if (s.cv.wait_for(l, std::chrono::seconds(3)) !=
4db4a6
+	std::cv_status::timeout) {
4db4a6
+      std::cerr << "Other thread: notified\n";
4db4a6
+      break;
4db4a6
+    }
4db4a6
+  }
4db4a6
+  return;
4db4a6
+}
4db4a6
+
4db4a6
+
4db4a6
+int main() {
4db4a6
+  state s;
4db4a6
+  auto future = std::async(std::launch::async, other_thread, &s);
4db4a6
+
4db4a6
+  if (future.wait_for(std::chrono::seconds(1)) != std::future_status::timeout) {
4db4a6
+    std::cerr << "Main: other thread returned too early!\n";
4db4a6
+    return 2;
4db4a6
+  }
4db4a6
+
4db4a6
+  {
4db4a6
+    std::lock_guard<std::mutex> g{s.m};
4db4a6
+    s.v.push_back(1);
4db4a6
+    s.v.push_back(2);
4db4a6
+    s.cv.notify_all();
4db4a6
+  }
4db4a6
+  return 0;
4db4a6
+}
4db4a6
diff --git a/drd/tests/condvar.stderr.exp b/drd/tests/condvar.stderr.exp
4db4a6
new file mode 100644
4db4a6
index 000000000..be1de9f97
4db4a6
--- /dev/null
4db4a6
+++ b/drd/tests/condvar.stderr.exp
4db4a6
@@ -0,0 +1,5 @@
4db4a6
+
4db4a6
+Other thread: waiting for notify
4db4a6
+Other thread: notified
4db4a6
+
4db4a6
+ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
4db4a6
diff --git a/drd/tests/condvar.vgtest b/drd/tests/condvar.vgtest
4db4a6
new file mode 100644
4db4a6
index 000000000..2e7d49f5a
4db4a6
--- /dev/null
4db4a6
+++ b/drd/tests/condvar.vgtest
4db4a6
@@ -0,0 +1,3 @@
4db4a6
+prereq: ./supported_libpthread && [ -e condvar ]
4db4a6
+vgopts: --check-stack-var=yes --read-var-info=yes
4db4a6
+prog: condvar
4db4a6
diff --git a/helgrind/hg_intercepts.c b/helgrind/hg_intercepts.c
4db4a6
index 866efdbaa..49c3ddcd9 100644
4db4a6
--- a/helgrind/hg_intercepts.c
4db4a6
+++ b/helgrind/hg_intercepts.c
4db4a6
@@ -1409,6 +1409,88 @@ static int pthread_cond_timedwait_WRK(pthread_cond_t* cond,
4db4a6
 #  error "Unsupported OS"
4db4a6
 #endif
4db4a6
 
4db4a6
+//-----------------------------------------------------------
4db4a6
+// glibc:   pthread_cond_clockwait
4db4a6
+//
4db4a6
+__attribute__((noinline))
4db4a6
+static int pthread_cond_clockwait_WRK(pthread_cond_t* cond,
4db4a6
+                                      pthread_mutex_t* mutex,
4db4a6
+                                      clockid_t clockid,
4db4a6
+                                      struct timespec* abstime,
4db4a6
+                                      int timeout_error)
4db4a6
+{
4db4a6
+   int ret;
4db4a6
+   OrigFn fn;
4db4a6
+   unsigned long mutex_is_valid;
4db4a6
+   Bool abstime_is_valid;
4db4a6
+   VALGRIND_GET_ORIG_FN(fn);
4db4a6
+
4db4a6
+   if (TRACE_PTH_FNS) {
4db4a6
+      fprintf(stderr, "<< pthread_cond_clockwait %p %p %p",
4db4a6
+                      cond, mutex, abstime);
4db4a6
+      fflush(stderr);
4db4a6
+   }
4db4a6
+
4db4a6
+   /* Tell the tool a cond-wait is about to happen, so it can check
4db4a6
+      for bogus argument values.  In return it tells us whether it
4db4a6
+      thinks the mutex is valid or not. */
4db4a6
+   DO_CREQ_W_WW(mutex_is_valid,
4db4a6
+                _VG_USERREQ__HG_PTHREAD_COND_WAIT_PRE,
4db4a6
+                pthread_cond_t*,cond, pthread_mutex_t*,mutex);
4db4a6
+   assert(mutex_is_valid == 1 || mutex_is_valid == 0);
4db4a6
+
4db4a6
+   abstime_is_valid = abstime->tv_nsec >= 0 && abstime->tv_nsec < 1000000000;
4db4a6
+
4db4a6
+   /* Tell the tool we're about to drop the mutex.  This reflects the
4db4a6
+      fact that in a cond_wait, we show up holding the mutex, and the
4db4a6
+      call atomically drops the mutex and waits for the cv to be
4db4a6
+      signalled. */
4db4a6
+   if (mutex_is_valid && abstime_is_valid) {
4db4a6
+      DO_CREQ_v_W(_VG_USERREQ__HG_PTHREAD_MUTEX_UNLOCK_PRE,
4db4a6
+                  pthread_mutex_t*,mutex);
4db4a6
+   }
4db4a6
+
4db4a6
+   CALL_FN_W_WWWW(ret, fn, cond,mutex,clockid,abstime);
4db4a6
+
4db4a6
+   if (mutex_is_valid && !abstime_is_valid && ret != EINVAL) {
4db4a6
+      DO_PthAPIerror("Bug in libpthread: pthread_cond_clockwait "
4db4a6
+                     "invalid abstime did not cause"
4db4a6
+                     " EINVAL", ret);
4db4a6
+   }
4db4a6
+
4db4a6
+   if (mutex_is_valid && abstime_is_valid) {
4db4a6
+      /* and now we have the mutex again if (ret == 0 || ret == timeout) */
4db4a6
+      DO_CREQ_v_WW(_VG_USERREQ__HG_PTHREAD_MUTEX_LOCK_POST,
4db4a6
+                   pthread_mutex_t *, mutex,
4db4a6
+                   long, (ret == 0 || ret == timeout_error) ? True : False);
4db4a6
+   }
4db4a6
+
4db4a6
+   DO_CREQ_v_WWWW(_VG_USERREQ__HG_PTHREAD_COND_WAIT_POST,
4db4a6
+                  pthread_cond_t*,cond, pthread_mutex_t*,mutex,
4db4a6
+                  long,ret == timeout_error,
4db4a6
+                  long, (ret == 0 || ret == timeout_error) && mutex_is_valid
4db4a6
+                        ? True : False);
4db4a6
+
4db4a6
+   if (ret != 0 && ret != timeout_error) {
4db4a6
+      DO_PthAPIerror( "pthread_cond_clockwait", ret );
4db4a6
+   }
4db4a6
+
4db4a6
+   if (TRACE_PTH_FNS) {
4db4a6
+      fprintf(stderr, " cotimedwait -> %d >>\n", ret);
4db4a6
+   }
4db4a6
+
4db4a6
+   return ret;
4db4a6
+}
4db4a6
+
4db4a6
+#if defined(VGO_linux)
4db4a6
+   PTH_FUNC(int, pthreadZucondZuclockwait, // pthread_cond_clockwait
4db4a6
+                 pthread_cond_t* cond, pthread_mutex_t* mutex,
4db4a6
+                 clockid_t clockid,
4db4a6
+                 struct timespec* abstime) {
4db4a6
+      return pthread_cond_clockwait_WRK(cond, mutex, clockid, abstime, ETIMEDOUT);
4db4a6
+   }
4db4a6
+#endif
4db4a6
+
4db4a6
 
4db4a6
 //-----------------------------------------------------------
4db4a6
 // glibc:   pthread_cond_signal@GLIBC_2.0