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