[PATCH] D54835: tsan: Support calling ThreadCreate()/ThreadStart()/ThreadFinish() for non-current thread

Yuri Per via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 22 09:50:23 PST 2018


yuri created this revision.
yuri added reviewers: dvyukov, blastrock.
yuri added a project: Sanitizers.
Herald added subscribers: Sanitizers, llvm-commits, kubamracek.

Currently it is not possible to call any of ThreadCreate()/ThreadStart()/ThreadFinish() with //thr// argument that is not current thread because of HACKY_CALL() implementation. This removes this limitation by calling TraceSwitch() directly on corresponding code paths.

Only generated code for ThreadContext::OnCreated()/ThreadContext::OnStarted()/ThreadContext::OnFinished() has changed (they became few bytes shorter). There is no any changes in performance-critical functions.


Repository:
  rCRT Compiler Runtime

https://reviews.llvm.org/D54835

Files:
  lib/tsan/rtl/tsan_rtl.cc
  lib/tsan/rtl/tsan_rtl.h
  lib/tsan/rtl/tsan_rtl_thread.cc


Index: lib/tsan/rtl/tsan_rtl_thread.cc
===================================================================
--- lib/tsan/rtl/tsan_rtl_thread.cc
+++ lib/tsan/rtl/tsan_rtl_thread.cc
@@ -59,7 +59,7 @@
     return;
   args->thr->fast_state.IncrementEpoch();
   // Can't increment epoch w/o writing to the trace as well.
-  TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0);
+  TraceAddEvent(args->thr, args->thr->fast_state, EventTypeMop, 0, false);
   ReleaseImpl(args->thr, 0, &sync);
   creation_stack_id = CurrentStackId(args->thr, args->pc);
   if (reuse_count == 0)
@@ -112,7 +112,7 @@
   thr->fast_state.SetHistorySize(flags()->history_size);
   // Commit switch to the new part of the trace.
   // TraceAddEvent will reset stack0/mset0 in the new part for us.
-  TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
+  TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0, false);
 
   thr->fast_synch_epoch = epoch0;
   AcquireImpl(thr, 0, &sync);
@@ -135,7 +135,7 @@
   if (!detached) {
     thr->fast_state.IncrementEpoch();
     // Can't increment epoch w/o writing to the trace as well.
-    TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0);
+    TraceAddEvent(thr, thr->fast_state, EventTypeMop, 0, false);
     ReleaseImpl(thr, 0, &sync);
   }
   epoch1 = thr->fast_state.epoch();
@@ -246,7 +246,8 @@
   uptr tls_addr = 0;
   uptr tls_size = 0;
 #if !SANITIZER_GO
-  GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
+  if (os_id)
+    GetThreadStackAndTls(tid == 0, &stk_addr, &stk_size, &tls_addr, &tls_size);
 
   if (tid) {
     if (stk_addr && stk_size)
Index: lib/tsan/rtl/tsan_rtl.h
===================================================================
--- lib/tsan/rtl/tsan_rtl.h
+++ lib/tsan/rtl/tsan_rtl.h
@@ -839,7 +839,8 @@
 
 extern "C" void __tsan_trace_switch();
 void ALWAYS_INLINE TraceAddEvent(ThreadState *thr, FastState fs,
-                                        EventType typ, u64 addr) {
+                                        EventType typ, u64 addr,
+                                        bool hacky_call = true) {
   if (!kCollectHistory)
     return;
   DCHECK_GE((int)typ, 0);
@@ -849,10 +850,12 @@
   u64 pos = fs.GetTracePos();
   if (UNLIKELY((pos % kTracePartSize) == 0)) {
 #if !SANITIZER_GO
-    HACKY_CALL(__tsan_trace_switch);
-#else
-    TraceSwitch(thr);
+    if (hacky_call) {
+      DCHECK_EQ(thr, cur_thread());
+      HACKY_CALL(__tsan_trace_switch);
+    } else
 #endif
+      TraceSwitch(thr);
   }
   Event *trace = (Event*)GetThreadTrace(fs.tid());
   Event *evp = &trace[pos];
Index: lib/tsan/rtl/tsan_rtl.cc
===================================================================
--- lib/tsan/rtl/tsan_rtl.cc
+++ lib/tsan/rtl/tsan_rtl.cc
@@ -619,6 +619,7 @@
   thr->racy_state[1] = old.raw();
   thr->racy_shadow_addr = shadow_mem;
 #if !SANITIZER_GO
+  DCHECK_EQ(thr, cur_thread());
   HACKY_CALL(__tsan_report_race);
 #else
   ReportRace(thr);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D54835.175060.patch
Type: text/x-patch
Size: 2948 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181122/9be65d9b/attachment.bin>


More information about the llvm-commits mailing list