From 94b6801839deca5671df8cf8793b61e912b71c95 Mon Sep 17 00:00:00 2001 From: Kostya Serebryany Date: Mon, 14 Jul 2014 14:27:21 +0000 Subject: [PATCH] [tsan] add a currently-failing test with a must-deadlock llvm-svn: 212944 --- compiler-rt/test/tsan/must_deadlock.cc | 49 ++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 compiler-rt/test/tsan/must_deadlock.cc diff --git a/compiler-rt/test/tsan/must_deadlock.cc b/compiler-rt/test/tsan/must_deadlock.cc new file mode 100644 index 000000000000..f6283f822dfe --- /dev/null +++ b/compiler-rt/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 +#include +#include + +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"); +}