From 66298e1c54fcf3f4643528c461f5cf376d27055b Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Thu, 10 Mar 2022 10:35:32 +0100 Subject: [PATCH] tsan: fix another false positive related to open/close The false positive fixed by commit f831d6fc80 ("tsan: fix false positive during fd close") still happens episodically on the added more stressful test which does just open/close. I don't have a coherent explanation as to what exactly happens but the fix fixes the false positive on this test as well. The issue may be related to lost writes during asynchronous MADV_DONTNEED. I've debugged similar unexplainable false positive related to freed and reused memory and at the time the only possible explanation I found is that an asynchronous MADV_DONTNEED may lead to lost writes. That's why commit 302ec7b9bc ("tsan: add memory_limit_mb flag") added StopTheWorld around the memory flush, but unfortunately the commit does not capture these findings. Reviewed By: melver Differential Revision: https://reviews.llvm.org/D121363 --- compiler-rt/lib/tsan/rtl/tsan_fd.cpp | 7 ++++++- compiler-rt/test/tsan/fd_close_norace3.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 compiler-rt/test/tsan/fd_close_norace3.cpp diff --git a/compiler-rt/lib/tsan/rtl/tsan_fd.cpp b/compiler-rt/lib/tsan/rtl/tsan_fd.cpp index 6c5835f..9f358ea 100644 --- a/compiler-rt/lib/tsan/rtl/tsan_fd.cpp +++ b/compiler-rt/lib/tsan/rtl/tsan_fd.cpp @@ -113,12 +113,17 @@ static void init(ThreadState *thr, uptr pc, int fd, FdSync *s, } d->creation_tid = thr->tid; d->creation_stack = CurrentStackId(thr, pc); + // This prevents false positives on fd_close_norace3.cpp test. + // The mechanics of the false positive are not completely clear, + // but it happens only if global reset is enabled (flush_memory_ms=1) + // and may be related to lost writes during asynchronous MADV_DONTNEED. + SlotLocker locker(thr); if (write) { // To catch races between fd usage and open. MemoryRangeImitateWrite(thr, pc, (uptr)d, 8); } else { // See the dup-related comment in FdClose. - MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead); + MemoryAccess(thr, pc, (uptr)d, 8, kAccessRead | kAccessSlotLocked); } } diff --git a/compiler-rt/test/tsan/fd_close_norace3.cpp b/compiler-rt/test/tsan/fd_close_norace3.cpp new file mode 100644 index 0000000..eab8cd5 --- /dev/null +++ b/compiler-rt/test/tsan/fd_close_norace3.cpp @@ -0,0 +1,28 @@ +// RUN: %clangxx_tsan -O1 %s -o %t && %env_tsan_opts=flush_memory_ms=1 %run %t 2>&1 | FileCheck %s +#include "test.h" +#include +#include +#include +#include + +void *Thread(void *stop) { + while (!__atomic_load_n((int *)stop, __ATOMIC_RELAXED)) + close(open("/dev/null", O_RDONLY)); + return 0; +} + +int main() { + int stop = 0; + const int kThreads = 10; + pthread_t th[kThreads]; + for (int i = 0; i < kThreads; i++) + pthread_create(&th[i], 0, Thread, &stop); + sleep(5); + __atomic_store_n(&stop, 1, __ATOMIC_RELAXED); + for (int i = 0; i < kThreads; i++) + pthread_join(th[i], 0); + fprintf(stderr, "DONE\n"); +} + +// CHECK-NOT: WARNING: ThreadSanitizer: data race +// CHECK: DONE -- 2.7.4