Mark Wielaard 79b01d
commit b28a423827a1c7917c6f3f3eba23b9432077dfbd
Mark Wielaard 79b01d
Author: florian <florian@a5019735-40e9-0310-863c-91ae7b9d1cf9>
Mark Wielaard 79b01d
Date:   Sat Jan 16 21:44:31 2016 +0000
Mark Wielaard 79b01d
Mark Wielaard 79b01d
    In ML_(am_allocate_segname) do not set the reference count of the
Mark Wielaard 79b01d
    slot to 1. Rather do that in add_segment which is where the segment
Mark Wielaard 79b01d
    refering to that name actually comes into existence.
Mark Wielaard 79b01d
    Properly handle the case in add_segment where the to-be-added segment
Mark Wielaard 79b01d
    and one (or more) of the segments it replaces have the same name
Mark Wielaard 79b01d
    This may occur when doing a mremap.
Mark Wielaard 79b01d
    
Mark Wielaard 79b01d
    
Mark Wielaard 79b01d
    git-svn-id: svn://svn.valgrind.org/valgrind/trunk@15761 a5019735-40e9-0310-863c-91ae7b9d1cf9
Mark Wielaard 79b01d
Mark Wielaard 79b01d
diff --git a/coregrind/m_aspacemgr/aspacemgr-linux.c b/coregrind/m_aspacemgr/aspacemgr-linux.c
Mark Wielaard 79b01d
index 0a8f675..f6c1a41 100644
Mark Wielaard 79b01d
--- a/coregrind/m_aspacemgr/aspacemgr-linux.c
Mark Wielaard 79b01d
+++ b/coregrind/m_aspacemgr/aspacemgr-linux.c
Mark Wielaard 79b01d
@@ -1445,6 +1445,15 @@ static void add_segment ( const NSegment* seg )
Mark Wielaard 79b01d
 
Mark Wielaard 79b01d
    split_nsegments_lo_and_hi( sStart, sEnd, &iLo, &iHi );
Mark Wielaard 79b01d
 
Mark Wielaard 79b01d
+   /* Increase the reference count of SEG's name. We need to do this
Mark Wielaard 79b01d
+      *before* decreasing the reference count of the names of the replaced
Mark Wielaard 79b01d
+      segments. Consider the case where the segment name of SEG and one of
Mark Wielaard 79b01d
+      the replaced segments are the same. If the refcount of that name is 1,
Mark Wielaard 79b01d
+      then decrementing first would put the slot for that name on the free
Mark Wielaard 79b01d
+      list. Attempting to increment the refcount later would then fail
Mark Wielaard 79b01d
+      because the slot is no longer allocated. */
Mark Wielaard 79b01d
+   ML_(am_inc_refcount)(seg->fnIdx);
Mark Wielaard 79b01d
+
Mark Wielaard 79b01d
    /* Now iLo .. iHi inclusive is the range of segment indices which
Mark Wielaard 79b01d
       seg will replace.  If we're replacing more than one segment,
Mark Wielaard 79b01d
       slide those above the range down to fill the hole. Before doing
Mark Wielaard 79b01d
diff --git a/coregrind/m_aspacemgr/aspacemgr-segnames.c b/coregrind/m_aspacemgr/aspacemgr-segnames.c
Mark Wielaard 79b01d
index ef3d3ef..8e74356 100644
Mark Wielaard 79b01d
--- a/coregrind/m_aspacemgr/aspacemgr-segnames.c
Mark Wielaard 79b01d
+++ b/coregrind/m_aspacemgr/aspacemgr-segnames.c
Mark Wielaard 79b01d
@@ -309,7 +309,7 @@ ML_(am_allocate_segname)(const HChar *name)
Mark Wielaard 79b01d
             freeslot_chain = next_freeslot;
Mark Wielaard 79b01d
          else
Mark Wielaard 79b01d
             put_slotindex(prev, next_freeslot);
Mark Wielaard 79b01d
-         put_refcount(ix, 1);
Mark Wielaard 79b01d
+         put_refcount(ix, 0);
Mark Wielaard 79b01d
          put_slotsize(ix, size);
Mark Wielaard 79b01d
          VG_(strcpy)(segnames + ix, name);
Mark Wielaard 79b01d
          ++num_segnames;
Mark Wielaard 79b01d
@@ -336,7 +336,7 @@ ML_(am_allocate_segname)(const HChar *name)
Mark Wielaard 79b01d
 
Mark Wielaard 79b01d
    /* copy it in */
Mark Wielaard 79b01d
    ix = segnames_used;
Mark Wielaard 79b01d
-   put_refcount(ix, 1);
Mark Wielaard 79b01d
+   put_refcount(ix, 0);
Mark Wielaard 79b01d
    put_slotsize(ix, len + 1);
Mark Wielaard 79b01d
    VG_(strcpy)(segnames + ix, name);
Mark Wielaard 79b01d
    segnames_used += need;