[llvm-dev] [PATCH compiler-rt 2/5] asan: introduce AsanThread::StackDescriptor

Paweł Dziepak via llvm-dev
2016-02-02T14:42:40Z

StackDescriptor contains all stack information (bottom, top and size),
which will make code implementing better support for swapcontext()
simpler.
---
 lib/asan/asan_thread.cc | 17 +++++++++--------
 lib/asan/asan_thread.h  | 26 +++++++++++++++++++-------
 2 files changed, 28 insertions(+), 15 deletions(-)

diff --git a/lib/asan/asan_thread.cc b/lib/asan/asan_thread.cc
index 526ef3d..33fffec 100644
--- a/lib/asan/asan_thread.cc
+++ b/lib/asan/asan_thread.cc
@@ -154,13 +154,13 @@ void AsanThread::Init() {
   CHECK_EQ(this->stack_size(), 0U);
   SetThreadStackAndTls();
   CHECK_GT(this->stack_size(), 0U);
-  CHECK(AddrIsInMem(stack_bottom_));
-  CHECK(AddrIsInMem(stack_top_ - 1));
+  CHECK(AddrIsInMem(stack_bottom()));
+  CHECK(AddrIsInMem(stack_top() - 1));
   ClearShadowForThreadStackAndTLS();
   int local = 0;
   VReport(1, "T%d: stack [%p,%p) size 0x%zx; local=%p\n", tid(),
-          (void *)stack_bottom_, (void *)stack_top_, stack_top_ -
stack_bottom_,
-          &local);
+          (void *)stack_bottom(), (void *)stack_top(),
+          stack_top() - stack_bottom(), &local);
 }

 thread_return_t AsanThread::ThreadStart(
@@ -195,9 +195,10 @@ thread_return_t AsanThread::ThreadStart(

 void AsanThread::SetThreadStackAndTls() {
   uptr tls_size = 0;
-  GetThreadStackAndTls(tid() == 0, &stack_bottom_, &stack_size_,
&tls_begin_,
-                       &tls_size);
-  stack_top_ = stack_bottom_ + stack_size_;
+  GetThreadStackAndTls(tid() == 0, &current_stack_.stack_bottom,
+                       &current_stack_.stack_size, &tls_begin_,
&tls_size);
+  current_stack_.stack_top = current_stack_.stack_bottom
+                             + current_stack_.stack_size;
   tls_end_ = tls_begin_ + tls_size;
   dtls_ = DTLS_Get();

@@ -206,7 +207,7 @@ void AsanThread::SetThreadStackAndTls() {
 }

 void AsanThread::ClearShadowForThreadStackAndTLS() {
-  PoisonShadow(stack_bottom_, stack_top_ - stack_bottom_, 0);
+  PoisonShadow(stack_bottom(), stack_top() - stack_bottom(), 0);
   if (tls_begin_ != tls_end_)
     PoisonShadow(tls_begin_, tls_end_ - tls_begin_, 0);
 }
diff --git a/lib/asan/asan_thread.h b/lib/asan/asan_thread.h
index b05d720..c4670e1 100644
--- a/lib/asan/asan_thread.h
+++ b/lib/asan/asan_thread.h
@@ -66,9 +66,15 @@ class AsanThread {
   thread_return_t ThreadStart(uptr os_id,
                               atomic_uintptr_t *signal_thread_is_registered);

-  uptr stack_top() { return stack_top_; }
-  uptr stack_bottom() { return stack_bottom_; }
-  uptr stack_size() { return stack_size_; }
+  struct StackDescriptor {
+    uptr stack_top;
+    uptr stack_bottom;
+    uptr stack_size;
+  };
+
+  uptr stack_top() { return CurrentStack()->stack_top; }
+  uptr stack_bottom() { return CurrentStack()->stack_bottom; }
+  uptr stack_size() { return CurrentStack()->stack_size; }
   uptr tls_begin() { return tls_begin_; }
   uptr tls_end() { return tls_end_; }
   DTLS *dtls() { return dtls_; }
@@ -83,8 +89,16 @@ class AsanThread {
   };
   bool GetStackFrameAccessByAddr(uptr addr, StackFrameAccess *access);

+  bool AddrIsInStack(StackDescriptor *stack, uptr addr) {
+    return addr >= stack->stack_bottom && addr <
stack->stack_top;
+  }
+
+  StackDescriptor *CurrentStack() {
+    return &current_stack_;
+  }
+
   bool AddrIsInStack(uptr addr) {
-    return addr >= stack_bottom_ && addr < stack_top_;
+    return AddrIsInStack(CurrentStack(), addr);
   }

   void DeleteFakeStack(int tid) {
@@ -130,11 +144,9 @@ class AsanThread {
   AsanThreadContext *context_;
   thread_callback_t start_routine_;
   void *arg_;
-  uptr stack_top_;
-  uptr stack_bottom_;
   // stack_size_ == stack_top_ - stack_bottom_;
   // It needs to be set in a async-signal-safe manner.
-  uptr stack_size_;
+  StackDescriptor current_stack_;
   uptr tls_begin_;
   uptr tls_end_;
   DTLS *dtls_;
--
2.5.0