[ASan] Move GetCurrentThread/SetCurrentThread from AsanThreadRegistry class into...
authorAlexey Samsonov <samsonov@google.com>
Wed, 20 Mar 2013 09:23:28 +0000 (09:23 +0000)
committerAlexey Samsonov <samsonov@google.com>
Wed, 20 Mar 2013 09:23:28 +0000 (09:23 +0000)
llvm-svn: 177501

12 files changed:
compiler-rt/lib/asan/asan_allocator.cc
compiler-rt/lib/asan/asan_allocator2.cc
compiler-rt/lib/asan/asan_fake_stack.cc
compiler-rt/lib/asan/asan_interceptors.cc
compiler-rt/lib/asan/asan_linux.cc
compiler-rt/lib/asan/asan_posix.cc
compiler-rt/lib/asan/asan_report.cc
compiler-rt/lib/asan/asan_rtl.cc
compiler-rt/lib/asan/asan_thread.cc
compiler-rt/lib/asan/asan_thread.h
compiler-rt/lib/asan/asan_thread_registry.cc
compiler-rt/lib/asan/asan_thread_registry.h

index 47b00bb..9df741f 100644 (file)
@@ -529,7 +529,7 @@ static u8 *Allocate(uptr alignment, uptr size, StackTrace *stack,
          alignment, size, size_class, size_to_allocate);
   }
 
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   AsanStats &thread_stats = asanThreadRegistry().GetCurrentThreadStats();
   // Statistics
   thread_stats.mallocs++;
@@ -619,7 +619,7 @@ static void Deallocate(u8 *ptr, StackTrace *stack, AllocType alloc_type) {
   CHECK(REDZONE <= 16 || !m->next);
   CHECK(m->free_tid == kInvalidTid);
   CHECK(m->alloc_tid >= 0);
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   m->free_tid = t ? t->tid() : 0;
   StackTrace::CompressStack(stack, m->compressed_free_stack(),
                                 m->compressed_free_stack_size());
index 3288f28..77cc87b 100644 (file)
@@ -336,7 +336,7 @@ static void *Allocate(uptr size, uptr alignment, StackTrace *stack,
     return 0;
   }
 
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   void *allocated;
   if (t) {
     AllocatorCache *cache = GetAllocatorCache(&t->malloc_storage());
@@ -438,7 +438,7 @@ static void Deallocate(void *ptr, StackTrace *stack, AllocType alloc_type) {
   CHECK_GE(m->alloc_tid, 0);
   if (SANITIZER_WORDSIZE == 64)  // On 32-bits this resides in user area.
     CHECK_EQ(m->free_tid, kInvalidTid);
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   m->free_tid = t ? t->tid() : 0;
   if (flags()->use_stack_depot) {
     m->free_context_id = StackDepotPut(stack->trace, stack->size);
index 2cede36..9ba54c7 100644 (file)
@@ -103,7 +103,7 @@ void FakeStack::AllocateOneSizeClass(uptr size_class) {
   uptr new_mem = (uptr)MmapOrDie(
       ClassMmapSize(size_class), __FUNCTION__);
   // Printf("T%d new_mem[%zu]: %p-%p mmap %zu\n",
-  //       asanThreadRegistry().GetCurrent()->tid(),
+  //       GetCurrentThread()->tid(),
   //       size_class, new_mem, new_mem + ClassMmapSize(size_class),
   //       ClassMmapSize(size_class));
   uptr i;
@@ -163,7 +163,7 @@ using namespace __asan;  // NOLINT
 
 uptr __asan_stack_malloc(uptr size, uptr real_stack) {
   if (!flags()->use_fake_stack) return real_stack;
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   if (!t) {
     // TSD is gone, use the real stack.
     return real_stack;
index 833a2f5..353c940 100644 (file)
@@ -89,7 +89,7 @@ static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
 }
 
 void SetThreadName(const char *name) {
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   if (t)
     t->summary()->set_name(name);
 }
@@ -115,7 +115,7 @@ using namespace __asan;  // NOLINT
 
 static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
   AsanThread *t = (AsanThread*)arg;
-  asanThreadRegistry().SetCurrent(t);
+  SetCurrentThread(t);
   return t->ThreadStart();
 }
 
@@ -123,7 +123,7 @@ static thread_return_t THREAD_CALLING_CONV asan_thread_start(void *arg) {
 INTERCEPTOR(int, pthread_create, void *thread,
     void *attr, void *(*start_routine)(void*), void *arg) {
   GET_STACK_TRACE_THREAD;
-  u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
+  u32 current_tid = GetCurrentTidOrInvalid();
   AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
   asanThreadRegistry().RegisterThread(t);
   return REAL(pthread_create)(thread, attr, asan_thread_start, t);
@@ -661,7 +661,7 @@ INTERCEPTOR_WINAPI(DWORD, CreateThread,
                    DWORD (__stdcall *start_routine)(void*), void* arg,
                    DWORD flags, void* tid) {
   GET_STACK_TRACE_THREAD;
-  u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
+  u32 current_tid = GetCurrentTidOrInvalid();
   AsanThread *t = AsanThread::Create(current_tid, start_routine, arg, &stack);
   asanThreadRegistry().RegisterThread(t);
   return REAL(CreateThread)(security, stack_size,
index b2df5a9..58b96a6 100644 (file)
@@ -116,7 +116,7 @@ void GetStackTrace(StackTrace *stack, uptr max_s, uptr pc, uptr bp, bool fast) {
   if (max_s > 1) {
     stack->max_size = max_s;
     if (!asan_inited) return;
-    if (AsanThread *t = asanThreadRegistry().GetCurrent())
+    if (AsanThread *t = GetCurrentThread())
       stack->FastUnwindStack(pc, bp, t->stack_top(), t->stack_bottom());
   }
 }
index a0cd8e0..10b671b 100644 (file)
@@ -74,7 +74,7 @@ void SetAlternateSignalStack() {
   CHECK(0 == sigaltstack(&altstack, 0));
   if (flags()->verbosity > 0) {
     Report("Alternative stack for T%d set: [%p,%p)\n",
-           asanThreadRegistry().GetCurrentTidOrInvalid(),
+           GetCurrentTidOrInvalid(),
            altstack.ss_sp, (char*)altstack.ss_sp + altstack.ss_size);
   }
 }
index 6359b26..d2315dc 100644 (file)
@@ -342,7 +342,7 @@ void DescribeHeapAddress(uptr addr, uptr access_size) {
       asanThreadRegistry().FindByTid(chunk.AllocTid());
   StackTrace alloc_stack;
   chunk.GetAllocStack(&alloc_stack);
-  AsanThread *t = asanThreadRegistry().GetCurrent();
+  AsanThread *t = GetCurrentThread();
   CHECK(t);
   char tname[128];
   Decorator d;
@@ -428,7 +428,7 @@ class ScopedInErrorReport {
       // they are defined as no-return.
       Report("AddressSanitizer: while reporting a bug found another one."
                  "Ignoring.\n");
-      u32 current_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
+      u32 current_tid = GetCurrentTidOrInvalid();
       if (current_tid != reporting_thread_tid) {
         // ASan found two bugs in different threads simultaneously. Sleep
         // long enough to make sure that the thread which started to print
@@ -440,13 +440,13 @@ class ScopedInErrorReport {
       internal__exit(flags()->exitcode);
     }
     ASAN_ON_ERROR();
-    reporting_thread_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
+    reporting_thread_tid = GetCurrentTidOrInvalid();
     Printf("===================================================="
            "=============\n");
     if (reporting_thread_tid != kInvalidTid) {
       // We started reporting an error message. Stop using the fake stack
       // in case we call an instrumented function from a symbolizer.
-      AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
+      AsanThread *curr_thread = GetCurrentThread();
       CHECK(curr_thread);
       curr_thread->fake_stack().StopUsingFakeStack();
     }
@@ -454,7 +454,7 @@ class ScopedInErrorReport {
   // Destructor is NORETURN, as functions that report errors are.
   NORETURN ~ScopedInErrorReport() {
     // Make sure the current thread is announced.
-    AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
+    AsanThread *curr_thread = GetCurrentThread();
     if (curr_thread) {
       DescribeThread(curr_thread->summary());
     }
@@ -490,7 +490,7 @@ void ReportSIGSEGV(uptr pc, uptr sp, uptr bp, uptr addr) {
   Report("ERROR: AddressSanitizer: SEGV on unknown address %p"
              " (pc %p sp %p bp %p T%d)\n",
              (void*)addr, (void*)pc, (void*)sp, (void*)bp,
-             asanThreadRegistry().GetCurrentTidOrInvalid());
+             GetCurrentTidOrInvalid());
   Printf("%s", d.EndWarning());
   Printf("AddressSanitizer can not provide additional info.\n");
   GET_STACK_TRACE_FATAL(pc, bp);
@@ -680,7 +680,7 @@ void __asan_report_error(uptr pc, uptr bp, uptr sp,
              bug_descr, (void*)addr, pc, bp, sp);
   Printf("%s", d.EndWarning());
 
-  u32 curr_tid = asanThreadRegistry().GetCurrentTidOrInvalid();
+  u32 curr_tid = GetCurrentTidOrInvalid();
   char tname[128];
   Printf("%s%s of size %zu at %p thread T%d%s%s\n",
          d.Access(),
index 1d6a024..bad564c 100644 (file)
@@ -401,7 +401,7 @@ int NOINLINE __asan_set_error_exit_code(int exit_code) {
 
 void NOINLINE __asan_handle_no_return() {
   int local_stack;
-  AsanThread *curr_thread = asanThreadRegistry().GetCurrent();
+  AsanThread *curr_thread = GetCurrentThread();
   CHECK(curr_thread);
   uptr PageSize = GetPageSizeCached();
   uptr top = curr_thread->stack_top();
index 778e919..8f5a8df 100644 (file)
@@ -152,4 +152,42 @@ const char *AsanThread::GetFrameNameByAddr(uptr addr, uptr *offset) {
   return (const char*)ptr[1];
 }
 
+AsanThread *GetCurrentThread() {
+  AsanThreadSummary *summary = (AsanThreadSummary *)AsanTSDGet();
+  if (!summary) {
+#if SANITIZER_ANDROID
+    // On Android, libc constructor is called _after_ asan_init, and cleans up
+    // TSD. Try to figure out if this is still the main thread by the stack
+    // address. We are not entirely sure that we have correct main thread
+    // limits, so only do this magic on Android, and only if the found thread is
+    // the main thread.
+    AsanThread *thread =
+        asanThreadRegistry().FindThreadByStackAddress((uptr)&summary);
+    if (thread && thread->tid() == 0) {
+      SetCurrentThread(thread);
+      return thread;
+    }
+#endif
+    return 0;
+  }
+  return summary->thread();
+}
+
+void SetCurrentThread(AsanThread *t) {
+  CHECK(t->summary());
+  if (flags()->verbosity >= 2) {
+    Report("SetCurrentThread: %p for thread %p\n",
+           t->summary(), (void*)GetThreadSelf());
+  }
+  // Make sure we do not reset the current AsanThread.
+  CHECK(AsanTSDGet() == 0);
+  AsanTSDSet(t->summary());
+  CHECK(AsanTSDGet() == t->summary());
+}
+
+u32 GetCurrentTidOrInvalid() {
+  AsanThread *t = GetCurrentThread();
+  return t ? t->tid() : kInvalidTid;
+}
+
 }  // namespace __asan
index acc27e5..bfdccfb 100644 (file)
@@ -109,6 +109,11 @@ class AsanThread {
   AsanStats stats_;
 };
 
+// Get the current thread. May return 0.
+AsanThread *GetCurrentThread();
+void SetCurrentThread(AsanThread *t);
+u32 GetCurrentTidOrInvalid();
+
 }  // namespace __asan
 
 #endif  // ASAN_THREAD_H
index 2fd1746..5e875a4 100644 (file)
@@ -38,7 +38,7 @@ void AsanThreadRegistry::Init() {
   main_thread_.set_summary(&main_thread_summary_);
   main_thread_summary_.set_thread(&main_thread_);
   RegisterThread(&main_thread_);
-  SetCurrent(&main_thread_);
+  SetCurrentThread(&main_thread_);
   // At this point only one thread exists.
   inited_ = true;
 }
@@ -67,40 +67,8 @@ AsanThread *AsanThreadRegistry::GetMain() {
   return &main_thread_;
 }
 
-AsanThread *AsanThreadRegistry::GetCurrent() {
-  AsanThreadSummary *summary = (AsanThreadSummary *)AsanTSDGet();
-  if (!summary) {
-#if SANITIZER_ANDROID
-    // On Android, libc constructor is called _after_ asan_init, and cleans up
-    // TSD. Try to figure out if this is still the main thread by the stack
-    // address. We are not entirely sure that we have correct main thread
-    // limits, so only do this magic on Android, and only if the found thread is
-    // the main thread.
-    AsanThread* thread = FindThreadByStackAddress((uptr)&summary);
-    if (thread && thread->tid() == 0) {
-      SetCurrent(thread);
-      return thread;
-    }
-#endif
-    return 0;
-  }
-  return summary->thread();
-}
-
-void AsanThreadRegistry::SetCurrent(AsanThread *t) {
-  CHECK(t->summary());
-  if (flags()->verbosity >= 2) {
-    Report("SetCurrent: %p for thread %p\n",
-           t->summary(), (void*)GetThreadSelf());
-  }
-  // Make sure we do not reset the current AsanThread.
-  CHECK(AsanTSDGet() == 0);
-  AsanTSDSet(t->summary());
-  CHECK(AsanTSDGet() == t->summary());
-}
-
 AsanStats &AsanThreadRegistry::GetCurrentThreadStats() {
-  AsanThread *t = GetCurrent();
+  AsanThread *t = GetCurrentThread();
   return (t) ? t->stats() : main_thread_.stats();
 }
 
index adb1a6d..112f412 100644 (file)
@@ -34,18 +34,9 @@ class AsanThreadRegistry {
   void UnregisterThread(AsanThread *thread);
 
   AsanThread *GetMain();
-  // Get the current thread. May return 0.
-  AsanThread *GetCurrent();
-  void SetCurrent(AsanThread *t);
 
-  u32 GetCurrentTidOrInvalid() {
-    if (!inited_) return 0;
-    AsanThread *t = GetCurrent();
-    return t ? t->tid() : kInvalidTid;
-  }
-
-  // Returns stats for GetCurrent(), or stats for
-  // T0 if GetCurrent() returns 0.
+  // Returns stats for GetCurrentThread(), or stats for
+  // T0 if GetCurrentThread() returns 0.
   AsanStats &GetCurrentThreadStats();
   // Flushes all thread-local stats to accumulated stats, and makes
   // a copy of accumulated stats.