Mark Wielaard 1e2b8b
commit 13747d21fe69dd730fc4bbe21fc9320d349b41db
Mark Wielaard 1e2b8b
Author: philippe <philippe@a5019735-40e9-0310-863c-91ae7b9d1cf9>
Mark Wielaard 1e2b8b
Date:   Sun Jul 10 21:17:58 2016 +0000
Mark Wielaard 1e2b8b
Mark Wielaard 1e2b8b
    Fix 365273 - Invalid write to stack location reported after signal handler runs
Mark Wielaard 1e2b8b
    
Mark Wielaard 1e2b8b
    Analysis and patch by Earl Chew
Mark Wielaard 1e2b8b
    
Mark Wielaard 1e2b8b
    Tested on x86/amd64/ppc64
Mark Wielaard 1e2b8b
    
Mark Wielaard 1e2b8b
    
Mark Wielaard 1e2b8b
    
Mark Wielaard 1e2b8b
    git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15902 a5019735-40e9-0310-863c-91ae7b9d1cf9
Mark Wielaard 1e2b8b
Mark Wielaard 1e2b8b
diff --git a/coregrind/m_signals.c b/coregrind/m_signals.c
Mark Wielaard 1e2b8b
index 3e8326f..9146d0c 100644
Mark Wielaard 1e2b8b
--- a/coregrind/m_signals.c
Mark Wielaard 1e2b8b
+++ b/coregrind/m_signals.c
Mark Wielaard 1e2b8b
@@ -2519,6 +2519,7 @@ void async_signalhandler ( Int sigNo,
Mark Wielaard 1e2b8b
 Bool VG_(extend_stack)(ThreadId tid, Addr addr)
Mark Wielaard 1e2b8b
 {
Mark Wielaard 1e2b8b
    SizeT udelta;
Mark Wielaard 1e2b8b
+   Addr new_stack_base;
Mark Wielaard 1e2b8b
 
Mark Wielaard 1e2b8b
    /* Get the segment containing addr. */
Mark Wielaard 1e2b8b
    const NSegment* seg = VG_(am_find_nsegment)(addr);
Mark Wielaard 1e2b8b
@@ -2536,14 +2537,15 @@ Bool VG_(extend_stack)(ThreadId tid, Addr addr)
Mark Wielaard 1e2b8b
    vg_assert(seg_next != NULL);
Mark Wielaard 1e2b8b
 
Mark Wielaard 1e2b8b
    udelta = VG_PGROUNDUP(seg_next->start - addr);
Mark Wielaard 1e2b8b
+   new_stack_base = seg_next->start - udelta;
Mark Wielaard 1e2b8b
 
Mark Wielaard 1e2b8b
    VG_(debugLog)(1, "signals", 
Mark Wielaard 1e2b8b
-                    "extending a stack base 0x%lx down by %lu\n",
Mark Wielaard 1e2b8b
-                    seg_next->start, udelta);
Mark Wielaard 1e2b8b
+                 "extending a stack base 0x%lx down by %lu"
Mark Wielaard 1e2b8b
+                 " new base 0x%lx to cover 0x%lx\n",
Mark Wielaard 1e2b8b
+                 seg_next->start, udelta, new_stack_base, addr);
Mark Wielaard 1e2b8b
    Bool overflow;
Mark Wielaard 1e2b8b
    if (! VG_(am_extend_into_adjacent_reservation_client)
Mark Wielaard 1e2b8b
        ( seg_next->start, -(SSizeT)udelta, &overflow )) {
Mark Wielaard 1e2b8b
-      Addr new_stack_base = seg_next->start - udelta;
Mark Wielaard 1e2b8b
       if (overflow)
Mark Wielaard 1e2b8b
          VG_(umsg)("Stack overflow in thread #%u: can't grow stack to %#lx\n",
Mark Wielaard 1e2b8b
                    tid, new_stack_base);
Mark Wielaard 1e2b8b
@@ -2555,7 +2557,7 @@ Bool VG_(extend_stack)(ThreadId tid, Addr addr)
Mark Wielaard 1e2b8b
 
Mark Wielaard 1e2b8b
    /* When we change the main stack, we have to let the stack handling
Mark Wielaard 1e2b8b
       code know about it. */
Mark Wielaard 1e2b8b
-   VG_(change_stack)(VG_(clstk_id), addr, VG_(clstk_end));
Mark Wielaard 1e2b8b
+   VG_(change_stack)(VG_(clstk_id), new_stack_base, VG_(clstk_end));
Mark Wielaard 1e2b8b
 
Mark Wielaard 1e2b8b
    if (VG_(clo_sanity_level) > 2)
Mark Wielaard 1e2b8b
       VG_(sanity_check_general)(False);