tsan: fix reading of mutex flags

SyncVar::IsFlagSet returns true if any flag is set.
This is wrong. Check the actual requested flag.

llvm-svn: 305281
This commit is contained in:
Dmitry Vyukov 2017-06-13 09:37:51 +00:00
parent b23ff10c67
commit dc2a38cdf2
6 changed files with 53 additions and 7 deletions

View File

@ -83,7 +83,7 @@ struct SyncVar {
}
bool IsFlagSet(u32 f) const {
return atomic_load_relaxed(&flags);
return atomic_load_relaxed(&flags) & f;
}
void SetFlags(u32 f) {

View File

@ -6,11 +6,11 @@
// A very primitive mutex annotated with tsan annotations.
class Mutex {
public:
Mutex(bool prof = true)
Mutex(bool prof, unsigned flags)
: prof_(prof)
, locked_(false)
, seq_(0) {
__tsan_mutex_create(this, 0);
__tsan_mutex_create(this, flags);
}
~Mutex() {
@ -87,5 +87,5 @@ class Mutex {
}
};
Mutex Mutex::prof_mu_(false);
Mutex Mutex::prof_mu_(false, __tsan_mutex_linker_init);
int Mutex::prof_data_;

View File

@ -4,7 +4,7 @@
// Test that custom annoations provide normal mutex synchronization
// (no race reports for properly protected critical sections).
Mutex mu;
Mutex mu(true, 0);
long data;
void *thr(void *arg) {

View File

@ -3,7 +3,7 @@
// Test that failed TryLock does not induce parasitic synchronization.
Mutex mu;
Mutex mu(true, 0);
long data;
void *thr(void *arg) {

View File

@ -3,7 +3,7 @@
// Test that Broadcast does not induce parasitic synchronization.
Mutex mu;
Mutex mu(true, 0);
long data;
void *thr(void *arg) {

View File

@ -0,0 +1,46 @@
// RUN: %clangxx_tsan -O1 --std=c++11 %s -o %t
// RUN: %env_tsan_opts=report_destroy_locked=0 %run %t 2>&1 | FileCheck %s
#include "custom_mutex.h"
// Regression test for a bug.
// Thr1 destroys a locked mutex, previously such mutex was not removed from
// sync map and as the result subsequent uses of a mutex located at the same
// address caused false race reports.
Mutex mu(false, __tsan_mutex_write_reentrant);
long data;
void *thr1(void *arg) {
mu.Lock();
mu.~Mutex();
new(&mu) Mutex(true, __tsan_mutex_write_reentrant);
return 0;
}
void *thr2(void *arg) {
barrier_wait(&barrier);
mu.Lock();
data++;
mu.Unlock();
return 0;
}
int main() {
barrier_init(&barrier, 2);
pthread_t th;
pthread_create(&th, 0, thr1, 0);
pthread_join(th, 0);
barrier_init(&barrier, 2);
pthread_create(&th, 0, thr2, 0);
mu.Lock();
data++;
mu.Unlock();
barrier_wait(&barrier);
pthread_join(th, 0);
fprintf(stderr, "DONE\n");
return 0;
}
// CHECK-NOT: WARNING: ThreadSanitizer: data race
// CHECK: DONE