summaryrefslogtreecommitdiff
path: root/test/tsan/must_deadlock.cc
diff options
context:
space:
mode:
authorKostya Serebryany <kcc@google.com>2014-07-14 14:27:21 +0000
committerKostya Serebryany <kcc@google.com>2014-07-14 14:27:21 +0000
commit5690d487c7419b5f90ca1a9313177ff0ad2ea197 (patch)
tree82816b10d3f17e47c32aae8ba7de7bf9054c60a5 /test/tsan/must_deadlock.cc
parent5c92560e050309321cbfc2d1825116a2ac91c516 (diff)
[tsan] add a currently-failing test with a must-deadlock
git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@212944 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'test/tsan/must_deadlock.cc')
-rw-r--r--test/tsan/must_deadlock.cc49
1 files changed, 49 insertions, 0 deletions
diff --git a/test/tsan/must_deadlock.cc b/test/tsan/must_deadlock.cc
new file mode 100644
index 000000000..f6283f822
--- /dev/null
+++ b/test/tsan/must_deadlock.cc
@@ -0,0 +1,49 @@
+// Test that the deadlock detector can find a deadlock that actually happened.
+// Currently we will fail to report such a deadlock because we check for
+// cycles in lock-order graph after pthread_mutex_lock.
+
+// RUN: %clangxx_tsan %s -o %t
+// RUN: not %run %t 2>&1 | FileCheck %s
+// XFAIL: *
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+
+pthread_mutex_t mu1, mu2;
+pthread_barrier_t barrier;
+
+void *Thread(void *p) {
+ // mu2 => mu1
+ pthread_mutex_lock(&mu2);
+ pthread_barrier_wait(&barrier);
+ pthread_mutex_lock(&mu1);
+ // CHECK: ThreadSanitizer: lock-order-inversion (potential deadlock)
+ pthread_mutex_unlock(&mu1);
+ pthread_mutex_unlock(&mu2);
+ return p;
+}
+
+int main() {
+ pthread_mutex_init(&mu1, NULL);
+ pthread_mutex_init(&mu2, NULL);
+ pthread_barrier_init(&barrier, 0, 2);
+
+ alarm(3);
+
+ pthread_t t;
+ pthread_create(&t, 0, Thread, 0);
+
+ // mu1 => mu2
+ pthread_mutex_lock(&mu1);
+ pthread_barrier_wait(&barrier);
+ pthread_mutex_lock(&mu2);
+ pthread_mutex_unlock(&mu2);
+ pthread_mutex_unlock(&mu1);
+
+ pthread_join(t, 0);
+
+ pthread_mutex_destroy(&mu1);
+ pthread_mutex_destroy(&mu2);
+ pthread_barrier_destroy(&barrier);
+ fprintf(stderr, "FAILED\n");
+}