[compiler-rt] r224702 - tsan: reset stack0 in the first partition of thread trace
Dmitry Vyukov
dvyukov at google.com
Mon Dec 22 04:32:53 PST 2014
Author: dvyukov
Date: Mon Dec 22 06:32:52 2014
New Revision: 224702
URL: http://llvm.org/viewvc/llvm-project?rev=224702&view=rev
Log:
tsan: reset stack0 in the first partition of thread trace
stack0/mset0 contained bogus values from the previous thread
that used the same id
Added:
compiler-rt/trunk/test/tsan/restore_stack.cc
Modified:
compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc
Modified: compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc?rev=224702&r1=224701&r2=224702&view=diff
==============================================================================
--- compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc (original)
+++ compiler-rt/trunk/lib/tsan/rtl/tsan_rtl_thread.cc Mon Dec 22 06:32:52 2014
@@ -116,7 +116,10 @@ void ThreadContext::OnStarted(void *arg)
thr->fast_state.SetHistorySize(flags()->history_size);
const uptr trace = (epoch0 / kTracePartSize) % TraceParts();
Trace *thr_trace = ThreadTrace(thr->tid);
- thr_trace->headers[trace].epoch0 = epoch0;
+ TraceHeader *hdr = &thr_trace->headers[trace];
+ hdr->epoch0 = epoch0;
+ ObtainCurrentStack(thr, 0, &hdr->stack0);
+ hdr->mset0 = thr->mset;
StatInc(thr, StatSyncAcquire);
sync.Reset(&thr->clock_cache);
DPrintf("#%d: ThreadStart epoch=%zu stk_addr=%zx stk_size=%zx "
Added: compiler-rt/trunk/test/tsan/restore_stack.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/tsan/restore_stack.cc?rev=224702&view=auto
==============================================================================
--- compiler-rt/trunk/test/tsan/restore_stack.cc (added)
+++ compiler-rt/trunk/test/tsan/restore_stack.cc Mon Dec 22 06:32:52 2014
@@ -0,0 +1,49 @@
+// RUN: %clangxx_tsan -O1 %s -o %t && not %run %t 2>&1 | FileCheck %s
+#include <pthread.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int Global;
+volatile int x;
+const int kSize = 64 << 10;
+volatile long data[kSize];
+
+void __attribute__((noinline)) foo() {
+ for (int i = 0; i < kSize; i++)
+ data[i]++;
+}
+
+void *Thread(void *a) {
+ __atomic_store_n(&x, 1, __ATOMIC_RELEASE);
+ foo();
+ data[0]++;
+ return 0;
+}
+
+int main() {
+ for (int i = 0; i < 50; i++) {
+ pthread_t t;
+ pthread_create(&t, 0, Thread, 0);
+ pthread_join(t, 0);
+ }
+ pthread_t t;
+ pthread_create(&t, 0, Thread, 0);
+ sleep(5);
+ for (int i = 0; i < kSize; i++)
+ data[i]++;
+ pthread_join(t, 0);
+ fprintf(stderr, "DONE\n");
+ return 0;
+}
+
+// Previously this test produced bogus stack traces like:
+// Previous write of size 8 at 0x0000006a8ff8 by thread T17:
+// #0 foo() restore_stack.cc:13:5 (restore_stack.cc.exe+0x00000040622c)
+// #1 Thread(void*) restore_stack.cc:18:3 (restore_stack.cc.exe+0x000000406283)
+// #2 __tsan_thread_start_func rtl/tsan_interceptors.cc:886 (restore_stack.cc.exe+0x00000040a749)
+// #3 Thread(void*) restore_stack.cc:18:3 (restore_stack.cc.exe+0x000000406283)
+
+// CHECK: WARNING: ThreadSanitizer: data race
+// CHECK-NOT: __tsan_thread_start_func
+// CHECK-NOT: #3 Thread
+// CHECK: DONE
More information about the llvm-commits
mailing list