]> git.ipfire.org Git - thirdparty/valgrind.git/commitdiff
Defensive hacks to detect cases where V corrupts its own heap and/or
authorJulian Seward <jseward@acm.org>
Sun, 4 Dec 2005 23:27:14 +0000 (23:27 +0000)
committerJulian Seward <jseward@acm.org>
Sun, 4 Dec 2005 23:27:14 +0000 (23:27 +0000)
uses memory after freeing.  Check the redzones for all non-client
frees, and fill all non-client freed areas with garbage.  Unroll
VG_(memset) as a precautionary measure against performance lossage.

git-svn-id: svn://svn.valgrind.org/valgrind/trunk@5283

coregrind/m_libcbase.c
coregrind/m_mallocfree.c

index b420b687133bdad61117e1bc95b44e6d7df2d468..4a530952278b4e909de571aecbf33fe56c10d355 100644 (file)
@@ -372,10 +372,19 @@ void* VG_(memcpy) ( void *dest, const void *src, SizeT sz )
 void* VG_(memset) ( void *dest, Int c, SizeT sz )
 {
    Char *d = (Char *)dest;
-
-   while (sz--)
-      *d++ = c;
-
+   while (sz >= 4) {
+      d[0] = c;
+      d[1] = c;
+      d[2] = c;
+      d[3] = c;
+      d += 4;
+      sz -= 4;
+   }
+   while (sz > 0) {
+      d[0] = c;
+      d++;
+      sz--;
+   }
    return dest;
 }
 
index a5916ae4c61510134d1a855b85ea68d977e614e4..d975f0879ddb92d4ecc95dda20c15cde76431ca5 100644 (file)
@@ -1058,9 +1058,10 @@ void VG_(arena_free) ( ArenaId aid, void* ptr )
       
    b = get_payload_block(a, ptr);
 
-#  ifdef DEBUG_MALLOC
-   vg_assert(blockSane(a, b));
-#  endif
+   /* If this is one of V's areas, check carefully the block we're
+      getting back.  This picks up simple block-end overruns. */
+   if (aid != VG_AR_CLIENT)
+      vg_assert(blockSane(a, b));
 
    b_bszB   = get_bszB(b);
    b_pszB   = bszB_to_pszB(a, b_bszB);
@@ -1070,6 +1071,15 @@ void VG_(arena_free) ( ArenaId aid, void* ptr )
 
    a->bytes_on_loan -= b_pszB;
 
+   /* If this is one of V's areas, fill it up with junk to enhance the
+      chances of catching any later reads of it.  Note, 0xDD is
+      carefully chosen junk :-), in that: (1) 0xDDDDDDDD is an invalid
+      and non-word-aligned address on most systems, and (2) 0xDD is a
+      value which is unlikely to be generated by the new compressed
+      Vbits representation for memcheck. */
+   if (aid != VG_AR_CLIENT)
+      VG_(memset)(ptr, 0xDD, (SizeT)b_pszB);
+
    // Put this chunk back on a list somewhere.
    b_listno = pszB_to_listNo(b_pszB);
    mkFreeBlock( a, b, b_bszB, b_listno );