[compiler-rt] r190590 - [asan] Fix deadlock in stack unwinder on android/x86.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Thu Sep 12 01:16:28 PDT 2013


Author: eugenis
Date: Thu Sep 12 03:16:28 2013
New Revision: 190590

URL: http://llvm.org/viewvc/llvm-project?rev=190590&view=rev
Log:
[asan] Fix deadlock in stack unwinder on android/x86.

Fixes PR17116.
Patch by 林作健 (manjian2006 at gmail.com).

Modified:
    compiler-rt/trunk/lib/asan/asan_stack.h
    compiler-rt/trunk/lib/asan/asan_thread.h
    compiler-rt/trunk/lib/lsan/lsan_common_linux.cc

Modified: compiler-rt/trunk/lib/asan/asan_stack.h
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/asan/asan_stack.h?rev=190590&r1=190589&r2=190590&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_stack.h (original)
+++ compiler-rt/trunk/lib/asan/asan_stack.h Thu Sep 12 03:16:28 2013
@@ -29,21 +29,21 @@ void PrintStack(StackTrace *stack);
 // The pc will be in the position 0 of the resulting stack trace.
 // The bp may refer to the current frame or to the caller's frame.
 #if SANITIZER_WINDOWS
-#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast)     \
-  StackTrace stack;                                             \
+#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast) \
+  StackTrace stack;                                         \
   GetStackTrace(&stack, max_s, pc, bp, 0, 0, fast)
 #else
-#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast)     \
-  StackTrace stack;                                             \
-  {                                                             \
-    uptr stack_top = 0, stack_bottom = 0;                       \
-    AsanThread *t;                                              \
-    if (asan_inited && (t = GetCurrentThread())) {              \
-      stack_top = t->stack_top();                               \
-      stack_bottom = t->stack_bottom();                         \
-    }                                                           \
-    GetStackTrace(&stack, max_s, pc, bp,                        \
-                  stack_top, stack_bottom, fast);               \
+#define GET_STACK_TRACE_WITH_PC_AND_BP(max_s, pc, bp, fast)                \
+  StackTrace stack;                                                        \
+  {                                                                        \
+    AsanThread *t;                                                         \
+    stack.size = 0;                                                        \
+    if (asan_inited && (t = GetCurrentThread()) && !t->isUnwinding()) {    \
+      uptr stack_top = t->stack_top();                                     \
+      uptr stack_bottom = t->stack_bottom();                               \
+      ScopedUnwinding unwind_scope(t);                                     \
+      GetStackTrace(&stack, max_s, pc, bp, stack_top, stack_bottom, fast); \
+    }                                                                      \
   }
 #endif  // SANITIZER_WINDOWS
 

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=190590&r1=190589&r2=190590&view=diff
==============================================================================
--- compiler-rt/trunk/lib/asan/asan_thread.h (original)
+++ compiler-rt/trunk/lib/asan/asan_thread.h Thu Sep 12 03:16:28 2013
@@ -87,11 +87,17 @@ class AsanThread {
     return fake_stack_;
   }
 
+  // True is this thread is currently unwinding stack (i.e. collecting a stack
+  // trace). Used to prevent deadlocks on platforms where libc unwinder calls
+  // malloc internally. See PR17116 for more details.
+  bool isUnwinding() const { return unwinding; }
+  void setUnwinding(bool b) { unwinding = b; }
+
   AsanThreadLocalMallocStorage &malloc_storage() { return malloc_storage_; }
   AsanStats &stats() { return stats_; }
 
  private:
-  AsanThread() {}
+  AsanThread() : unwinding(false) {}
   void SetThreadStackAndTls();
   void ClearShadowForThreadStackAndTLS();
   AsanThreadContext *context_;
@@ -105,6 +111,19 @@ class AsanThread {
   FakeStack *fake_stack_;
   AsanThreadLocalMallocStorage malloc_storage_;
   AsanStats stats_;
+  bool unwinding;
+};
+
+// ScopedUnwinding is a scope for stacktracing member of a context
+class ScopedUnwinding {
+ public:
+  explicit ScopedUnwinding(AsanThread *t) : thread(t) {
+    t->setUnwinding(true);
+  }
+  ~ScopedUnwinding() { thread->setUnwinding(false); }
+
+ private:
+  AsanThread *thread;
 };
 
 struct CreateThreadContextArgs {

Modified: compiler-rt/trunk/lib/lsan/lsan_common_linux.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/lib/lsan/lsan_common_linux.cc?rev=190590&r1=190589&r2=190590&view=diff
==============================================================================
--- compiler-rt/trunk/lib/lsan/lsan_common_linux.cc (original)
+++ compiler-rt/trunk/lib/lsan/lsan_common_linux.cc Thu Sep 12 03:16:28 2013
@@ -114,8 +114,9 @@ static void ProcessPlatformSpecificAlloc
   chunk = GetUserBegin(chunk);
   LsanMetadata m(chunk);
   if (m.allocated() && m.tag() != kReachable) {
-    if (linker->containsAddress(
-            GetCallerPC(m.stack_trace_id(), param->stack_depot_reverse_map))) {
+    u32 stack_id = m.stack_trace_id();
+    if (!stack_id || linker->containsAddress(GetCallerPC(
+                         stack_id, param->stack_depot_reverse_map))) {
       m.set_tag(kReachable);
       param->frontier->push_back(chunk);
     }





More information about the llvm-commits mailing list