[compiler-rt] r197156 - [msan] Clean stack and TLS shadow on thread exit.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Thu Dec 12 05:48:47 PST 2013


Author: eugenis
Date: Thu Dec 12 07:48:47 2013
New Revision: 197156

URL: http://llvm.org/viewvc/llvm-project?rev=197156&view=rev
Log:
[msan] Clean stack and TLS shadow on thread exit.

Added:
    compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc   (with props)
Modified:
    compiler-rt/trunk/lib/msan/msan.cc
    compiler-rt/trunk/lib/msan/msan.h
    compiler-rt/trunk/lib/msan/msan_interceptors.cc

Added: compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc?rev=197156&view=auto
==============================================================================
--- compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc (added)
+++ compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc Thu Dec 12 07:48:47 2013
@@ -0,0 +1,26 @@
+// RUN: %clangxx_msan -m64 -O0 %s -o %t && %t
+
+// Check that when TLS block is reused between threads, its shadow is cleaned.
+
+#include <pthread.h>
+#include <stdio.h>
+
+int __thread x;
+
+void *ThreadFn(void *) {
+  if (!x)
+    printf("zzz\n");
+  int y;
+  int * volatile p = &y;
+  x = *p;
+  return 0;
+}
+
+int main(void) {
+  pthread_t t;
+  for (int i = 0; i < 100; ++i) {
+    pthread_create(&t, 0, ThreadFn, 0);
+    pthread_join(t, 0);
+  }
+  return 0;
+}

Propchange: compiler-rt/trunk/lib/msan/lit_tests/tls_reuse.cc
------------------------------------------------------------------------------
    svn:eol-style = LF

Modified: compiler-rt/trunk/lib/msan/msan.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan.cc?rev=197156&r1=197155&r2=197156&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan.cc (original)
+++ compiler-rt/trunk/lib/msan/msan.cc Thu Dec 12 07:48:47 2013
@@ -173,8 +173,9 @@ void GetStackTrace(StackTrace *stack, up
     SymbolizerScope sym_scope;
     return stack->Unwind(max_s, pc, bp, 0, 0, request_fast_unwind);
   }
-  stack->Unwind(max_s, pc, bp, msan_stack_bounds.stack_top,
-                msan_stack_bounds.stack_bottom, request_fast_unwind);
+  uptr stack_bottom = msan_stack_bounds.stack_addr;
+  uptr stack_top = stack_bottom + msan_stack_bounds.stack_size;
+  stack->Unwind(max_s, pc, bp, stack_top, stack_bottom, request_fast_unwind);
 }
 
 void PrintWarning(uptr pc, uptr bp) {
@@ -324,9 +325,10 @@ void __msan_init() {
   }
   Symbolizer::Get()->AddHooks(EnterSymbolizer, ExitSymbolizer);
 
-  GetThreadStackTopAndBottom(/* at_initialization */ true,
-                             &msan_stack_bounds.stack_top,
-                             &msan_stack_bounds.stack_bottom);
+  GetThreadStackAndTls(/* main */ true, &msan_stack_bounds.stack_addr,
+                       &msan_stack_bounds.stack_size,
+                       &msan_stack_bounds.tls_addr,
+                       &msan_stack_bounds.tls_size);
   VPrintf(1, "MemorySanitizer init done\n");
   msan_init_is_running = 0;
   msan_inited = 1;

Modified: compiler-rt/trunk/lib/msan/msan.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan.h?rev=197156&r1=197155&r2=197156&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan.h (original)
+++ compiler-rt/trunk/lib/msan/msan.h Thu Dec 12 07:48:47 2013
@@ -107,7 +107,8 @@ class ScopedThreadLocalStateBackup {
   if (&__msan_free_hook) __msan_free_hook(ptr)
 
 struct MsanStackBounds {
-  uptr stack_top, stack_bottom;
+  uptr stack_addr, stack_size;
+  uptr tls_addr, tls_size;
 };
 
 extern THREADLOCAL MsanStackBounds msan_stack_bounds;

Modified: compiler-rt/trunk/lib/msan/msan_interceptors.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/msan/msan_interceptors.cc?rev=197156&r1=197155&r2=197156&view=diff
==============================================================================
--- compiler-rt/trunk/lib/msan/msan_interceptors.cc (original)
+++ compiler-rt/trunk/lib/msan/msan_interceptors.cc Thu Dec 12 07:48:47 2013
@@ -1070,6 +1070,11 @@ static void thread_finalize(void *v) {
     return;
   }
   MsanAllocatorThreadFinish();
+  __msan_unpoison((void *)msan_stack_bounds.stack_addr,
+                  msan_stack_bounds.stack_size);
+  if (msan_stack_bounds.tls_size)
+    __msan_unpoison((void *)msan_stack_bounds.tls_addr,
+                    msan_stack_bounds.tls_size);
 }
 
 struct ThreadParam {
@@ -1089,9 +1094,10 @@ static void *MsanThreadStartFunc(void *a
   }
   atomic_store(&p->done, 1, memory_order_release);
 
-  GetThreadStackTopAndBottom(/* at_initialization */ false,
-                             &msan_stack_bounds.stack_top,
-                             &msan_stack_bounds.stack_bottom);
+  GetThreadStackAndTls(/* main */ false, &msan_stack_bounds.stack_addr,
+                       &msan_stack_bounds.stack_size,
+                       &msan_stack_bounds.tls_addr,
+                       &msan_stack_bounds.tls_size);
   return callback(param);
 }
 





More information about the llvm-commits mailing list