Skip to content

Commit e273986

Browse files
committed
tsan: improve "destroy of a locked mutex" reports
1. Allow to suppress by current stack. We generally allow to suppress by all main stacks. Current is probably the stack one wants to use to suppress such reports. 2. Fix last lock stack restoration. We trimmed shadow value by storing it in u32. This magically worked for the test that provoked the report on the main thread. But this breaks for locks in any other threads. git-svn-id: https://llvm.org/svn/llvm-project/compiler-rt/trunk@331023 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent b820ab2 commit e273986

File tree

4 files changed

+52
-2
lines changed

4 files changed

+52
-2
lines changed

lib/tsan/rtl/tsan_rtl_mutex.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ void MutexDestroy(ThreadState *thr, uptr pc, uptr addr, u32 flagz) {
104104
unlock_locked = true;
105105
}
106106
u64 mid = s->GetId();
107-
u32 last_lock = s->last_lock;
107+
u64 last_lock = s->last_lock;
108108
if (!unlock_locked)
109109
s->Reset(thr->proc()); // must not reset it before the report is printed
110110
s->mtx.Unlock();
@@ -114,7 +114,7 @@ void MutexDestroy(ThreadState *thr, uptr pc, uptr addr, u32 flagz) {
114114
rep.AddMutex(mid);
115115
VarSizeStackTrace trace;
116116
ObtainCurrentStack(thr, pc, &trace);
117-
rep.AddStack(trace);
117+
rep.AddStack(trace, true);
118118
FastState last(last_lock);
119119
RestoreStack(last.tid(), last.epoch(), &trace, 0);
120120
rep.AddStack(trace, true);

test/tsan/mutex_destroy_locked2.cc

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
2+
#include <pthread.h>
3+
#include <unistd.h>
4+
5+
void *thread(void *arg) {
6+
pthread_mutex_t m;
7+
pthread_mutex_init(&m, 0);
8+
pthread_mutex_lock(&m);
9+
pthread_mutex_destroy(&m);
10+
return 0;
11+
}
12+
13+
int main() {
14+
pthread_t th;
15+
pthread_create(&th, 0, thread, 0);
16+
pthread_join(th, 0);
17+
return 0;
18+
}
19+
20+
// CHECK: WARNING: ThreadSanitizer: destroy of a locked mutex
21+
// CHECK: #0 pthread_mutex_destroy
22+
// CHECK: #1 thread
23+
// CHECK: and:
24+
// CHECK: #0 pthread_mutex_lock
25+
// CHECK: #1 thread
26+
// CHECK: Mutex {{.*}} created at:
27+
// CHECK: #0 pthread_mutex_init
28+
// CHECK: #1 thread
29+
// CHECK: SUMMARY: ThreadSanitizer: destroy of a locked mutex {{.*}} in thread

test/tsan/suppressions_mutex.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %clang_tsan -O1 %s -o %t && %env_tsan_opts=suppressions='%s.supp' %run %t 2>&1 | FileCheck %s
2+
#include "test.h"
3+
4+
void __attribute__((noinline)) suppress_this(pthread_mutex_t *mu) {
5+
pthread_mutex_destroy(mu);
6+
}
7+
8+
int main() {
9+
pthread_mutex_t mu;
10+
pthread_mutex_init(&mu, 0);
11+
pthread_mutex_lock(&mu);
12+
suppress_this(&mu);
13+
fprintf(stderr, "DONE\n");
14+
return 0;
15+
}
16+
17+
// CHECK-NOT: failed to open suppressions file
18+
// CHECK-NOT: WARNING: ThreadSanitizer:
19+
// CHECK: DONE

test/tsan/suppressions_mutex.cc.supp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
mutex:suppress_this
2+

0 commit comments

Comments
 (0)