Fixed stack red zone handling.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@7713 a5019735-40e9-0310-863c-91ae7b9d1cf9
diff --git a/exp-drd/drd_main.c b/exp-drd/drd_main.c
index db33579..6033dc4 100644
--- a/exp-drd/drd_main.c
+++ b/exp-drd/drd_main.c
@@ -162,7 +162,7 @@
                : access_type == eStart
                ? "start"
                : access_type == eEnd
-               ? "end"
+               ? "end  "
                : "????",
                addr,
                size,
@@ -464,7 +464,7 @@
 
   if (a1 <= drd_trace_address && drd_trace_address < a2)
   {
-    drd_trace_mem_access(a1, len, eStart);
+    drd_trace_mem_access(a1, len, eEnd);
   }
   thread_stop_using_mem(a1, a2);
   clientobj_stop_using_mem(a1, a2);
@@ -485,8 +485,8 @@
 /* Assumption: stacks grow downward.                                     */
 static void drd_start_using_mem_stack(const Addr a, const SizeT len)
 {
-  thread_set_stack_min(thread_get_running_tid(), a);
-  drd_start_using_mem(a, len);
+  thread_set_stack_min(thread_get_running_tid(), a - VG_STACK_REDZONE_SZB);
+  drd_start_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB);
 }
 
 /* Called by the core when the stack of a thread shrinks, to indicate that */
@@ -494,8 +494,9 @@
 /* Assumption: stacks grow downward.                                       */
 static void drd_stop_using_mem_stack(const Addr a, const SizeT len)
 {
-  thread_set_stack_min(thread_get_running_tid(), a + len);
-  drd_stop_using_mem(a, len);
+  thread_set_stack_min(thread_get_running_tid(),
+                       a + len - VG_STACK_REDZONE_SZB);
+  drd_stop_using_mem(a - VG_STACK_REDZONE_SZB, len + VG_STACK_REDZONE_SZB);
 }
 
 static void drd_start_using_mem_stack_signal(const Addr a, const SizeT len)
@@ -584,24 +585,28 @@
 }
 
 /* Called after a thread has performed its last memory access. */
-static void drd_thread_finished(ThreadId tid)
+static void drd_thread_finished(ThreadId vg_tid)
 {
   DrdThreadId drd_tid;
 
-  tl_assert(VG_(get_running_tid)() == tid);
+  tl_assert(VG_(get_running_tid)() == vg_tid);
 
-  drd_tid = VgThreadIdToDrdThreadId(tid);
+  drd_tid = VgThreadIdToDrdThreadId(vg_tid);
   if (drd_trace_fork_join)
   {
     VG_(message)(Vg_DebugMsg,
                  "drd_thread_finished tid = %d/%d%s",
-                 tid,
+                 vg_tid,
                  drd_tid,
                  thread_get_joinable(drd_tid)
                  ? ""
                  : " (which is a detached thread)");
 
   }
+  drd_stop_using_mem(thread_get_stack_min(drd_tid),
+                     thread_get_stack_max(drd_tid)
+                     - thread_get_stack_min(drd_tid));
+  thread_stop_recording(drd_tid);
   thread_finished(drd_tid);
 }
 
diff --git a/exp-drd/drd_suppression.c b/exp-drd/drd_suppression.c
index cad09c4..91fcbde 100644
--- a/exp-drd/drd_suppression.c
+++ b/exp-drd/drd_suppression.c
@@ -61,7 +61,7 @@
   }
 
   tl_assert(a1 < a2);
-  tl_assert(! drd_is_any_suppressed(a1, a2));
+  // tl_assert(! drd_is_any_suppressed(a1, a2));
   bm_access_range_store(s_suppressed, a1, a2);
 }
 
diff --git a/exp-drd/drd_thread.c b/exp-drd/drd_thread.c
index dc59818..45eb235 100644
--- a/exp-drd/drd_thread.c
+++ b/exp-drd/drd_thread.c
@@ -246,21 +246,11 @@
   return s_threadinfo[tid].stack_min;
 }
 
-DrdThreadId thread_lookup_stackaddr(const Addr a,
-                                    Addr* const stack_min,
-                                    Addr* const stack_max)
+Addr thread_get_stack_max(const DrdThreadId tid)
 {
-  unsigned i;
-  for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
-  {
-    if (s_threadinfo[i].stack_min <= a && a <= s_threadinfo[i].stack_max)
-    {
-      *stack_min = s_threadinfo[i].stack_min;
-      *stack_max = s_threadinfo[i].stack_max;
-      return i;
-    }
-  }
-  return DRD_INVALID_THREADID;
+  tl_assert(0 <= tid && tid < DRD_N_THREADS
+            && tid != DRD_INVALID_THREADID);
+  return s_threadinfo[tid].stack_max;
 }
 
 /**
@@ -295,9 +285,6 @@
   tl_assert(0 <= tid && tid < DRD_N_THREADS
             && tid != DRD_INVALID_THREADID);
 
-  thread_stop_using_mem(s_threadinfo[tid].stack_min,
-                        s_threadinfo[tid].stack_max);
-
   s_threadinfo[tid].vg_thread_exists = False;
 
   if (s_threadinfo[tid].detached_posix_thread)
@@ -606,11 +593,11 @@
  */
 void thread_stop_using_mem(const Addr a1, const Addr a2)
 {
-  DrdThreadId other_user = DRD_INVALID_THREADID;
+  DrdThreadId other_user;
+  unsigned i;
 
   /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
-
-  unsigned i;
+  other_user = DRD_INVALID_THREADID;
   for (i = 0; i < sizeof(s_threadinfo) / sizeof(s_threadinfo[0]); i++)
   {
     Segment* p;
@@ -681,7 +668,7 @@
 {
   const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid);
 
-  VG_(message)(Vg_UserMsg, "%s (thread %d)", msg, tid);
+  VG_(message)(Vg_UserMsg, "%s (thread %d)", msg, /*vg_tid,*/ tid);
 
   if (vg_tid != VG_INVALID_THREADID)
   {
diff --git a/exp-drd/drd_thread.h b/exp-drd/drd_thread.h
index 24e3506..290d4ea 100644
--- a/exp-drd/drd_thread.h
+++ b/exp-drd/drd_thread.h
@@ -102,9 +102,7 @@
 void thread_finished(const DrdThreadId tid);
 void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup);
 Addr thread_get_stack_min(const DrdThreadId tid);
-DrdThreadId thread_lookup_stackaddr(const Addr a,
-                                    Addr* const stack_min,
-                                    Addr* const stack_max);
+Addr thread_get_stack_max(const DrdThreadId tid);
 void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid);
 Bool thread_get_joinable(const DrdThreadId tid);
 void thread_set_joinable(const DrdThreadId tid, const Bool joinable);