[llvm-commits] [compiler-rt] r147674 - in /compiler-rt/trunk/lib/asan: asan_mac.cc asan_rtl.cc asan_thread.cc asan_thread.h asan_thread_registry.cc

Kostya Serebryany kcc at google.com
Fri Jan 6 11:44:11 PST 2012


Author: kcc
Date: Fri Jan  6 13:44:11 2012
New Revision: 147674

URL: http://llvm.org/viewvc/llvm-project?rev=147674&view=rev
Log:
[asan] do not use new/delete for the internal thread structure

Modified:
    compiler-rt/trunk/lib/asan/asan_mac.cc
    compiler-rt/trunk/lib/asan/asan_rtl.cc
    compiler-rt/trunk/lib/asan/asan_thread.cc
    compiler-rt/trunk/lib/asan/asan_thread.h
    compiler-rt/trunk/lib/asan/asan_thread_registry.cc

Modified: compiler-rt/trunk/lib/asan/asan_mac.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_mac.cc?rev=147674&r1=147673&r2=147674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_mac.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_mac.cc Fri Jan  6 13:44:11 2012
@@ -28,8 +28,6 @@
 #include <fcntl.h>
 #include <unistd.h>
 
-#include <new>
-
 namespace __asan {
 
 extern dispatch_async_f_f real_dispatch_async_f;
@@ -190,9 +188,8 @@
     // It's incorrect to assert that the current thread is not dying: at least
     // the callbacks from dispatch_sync() are sometimes called after the TSD is
     // destroyed.
-    t = (AsanThread*)asan_malloc(sizeof(AsanThread), &stack);
-    new(t) AsanThread(context->parent_tid,
-                      /*start_routine*/NULL, /*arg*/NULL, &stack);
+    AsanThread *t = AsanThread::Create(context->parent_tid, NULL, NULL);
+    asanThreadRegistry().RegisterThread(t, context->parent_tid, &stack);
     t->Init();
     asanThreadRegistry().SetCurrent(t);
   }

Modified: compiler-rt/trunk/lib/asan/asan_rtl.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_rtl.cc?rev=147674&r1=147673&r2=147674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_rtl.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_rtl.cc Fri Jan  6 13:44:11 2012
@@ -396,11 +396,11 @@
 int WRAP(pthread_create)(pthread_t *thread, const pthread_attr_t *attr,
                          void *(*start_routine) (void *), void *arg) {
   GET_STACK_TRACE_HERE(kStackTraceMax, /*fast_unwind*/false);
-  AsanThread *t = (AsanThread*)asan_malloc(sizeof(AsanThread), &stack);
   AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
   CHECK(curr_thread || asanThreadRegistry().IsCurrentThreadDying());
-  new(t) AsanThread(asanThreadRegistry().GetCurrentTidOrMinusOne(),
-                    start_routine, arg, &stack);
+  int current_tid = asanThreadRegistry().GetCurrentTidOrMinusOne();
+  AsanThread *t = AsanThread::Create(current_tid, start_routine, arg);
+  asanThreadRegistry().RegisterThread(t, current_tid, &stack);
   return real_pthread_create(thread, attr, asan_thread_start, t);
 }
 

Modified: compiler-rt/trunk/lib/asan/asan_thread.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.cc?rev=147674&r1=147673&r2=147674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.cc Fri Jan  6 13:44:11 2012
@@ -15,7 +15,6 @@
 #include "asan_interceptors.h"
 #include "asan_procmaps.h"
 #include "asan_thread.h"
-#include "asan_thread_registry.h"
 #include "asan_mapping.h"
 
 #include <pthread.h>
@@ -29,20 +28,23 @@
       malloc_storage_(x),
       stats_(x) { }
 
-AsanThread::AsanThread(int parent_tid, void *(*start_routine) (void *),
-                       void *arg, AsanStackTrace *stack)
-    : start_routine_(start_routine),
-      arg_(arg) {
-  asanThreadRegistry().RegisterThread(this, parent_tid, stack);
+AsanThread *AsanThread::Create(int parent_tid, void *(*start_routine) (void *),
+                               void *arg) {
+  size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
+  AsanThread *res = (AsanThread*)AsanMmapSomewhereOrDie(size, __FUNCTION__);
+  res->start_routine_ = start_routine;
+  res->arg_ = arg;
+  return res;
 }
 
-AsanThread::~AsanThread() {
-  asanThreadRegistry().UnregisterThread(this);
+void AsanThread::Destroy() {
   fake_stack().Cleanup();
   // We also clear the shadow on thread destruction because
   // some code may still be executing in later TSD destructors
   // and we don't want it to have any poisoned stack.
   ClearShadowForThreadStack();
+  size_t size = RoundUpTo(sizeof(AsanThread), kPageSize);
+  AsanUnmapOrDie(this, size);
 }
 
 void AsanThread::ClearShadowForThreadStack() {

Modified: compiler-rt/trunk/lib/asan/asan_thread.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread.h?rev=147674&r1=147673&r2=147674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.h Fri Jan  6 13:44:11 2012
@@ -62,9 +62,9 @@
 class AsanThread {
  public:
   explicit AsanThread(LinkerInitialized);  // for T0.
-  AsanThread(int parent_tid, void *(*start_routine) (void *),
-             void *arg, AsanStackTrace *stack);
-  ~AsanThread();
+  static AsanThread *Create(int parent_tid, void *(*start_routine) (void *),
+                            void *arg);
+  void Destroy();
 
   void Init();  // Should be called from the thread itself.
   void *ThreadStart();

Modified: compiler-rt/trunk/lib/asan/asan_thread_registry.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_thread_registry.cc?rev=147674&r1=147673&r2=147674&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread_registry.cc (original)
+++ compiler-rt/trunk/lib/asan/asan_thread_registry.cc Fri Jan  6 13:44:11 2012
@@ -51,7 +51,8 @@
     // The pointer is valid.
     AsanThread *t = (AsanThread*)tsd;
     if (t != asanThreadRegistry().GetMain()) {
-      delete t;
+      asanThreadRegistry().UnregisterThread(t);
+      t->Destroy();
     }
     iter = 1;
   } else {





More information about the llvm-commits mailing list