malloc: perturb mchunk returned from tcache like fast, small, large bin

Message ID 1589527677-38586-1-git-send-email-wangxu72@huawei.com
State New, archived
Headers
Series malloc: perturb mchunk returned from tcache like fast, small, large bin |

Commit Message

wangxu May 15, 2020, 7:27 a.m. UTC
  From: Wang Xu <wangxu72@huawei.com>

mchunk returned from tcache donot have the chance to be perturbed
like what from fastbin, smallbin and largebin.

This patch perturbs the mchunk returned from tcache.

Thanks Carlos O'Donell for comment.

---
 malloc/malloc.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
  

Patch

diff --git a/malloc/malloc.c b/malloc/malloc.c
index e8abb4e..057e5ad 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3108,7 +3108,9 @@  __libc_malloc (size_t bytes)
       && tcache
       && tcache->counts[tc_idx] > 0)
     {
-      return tcache_get (tc_idx);
+      void *p = tcache_get (tc_idx);
+      alloc_perturb (p, bytes);
+      return p;
     }
   DIAG_POP_NEEDS_COMMENT;
 #endif
@@ -3963,7 +3965,9 @@  _int_malloc (mstate av, size_t bytes)
 	  && mp_.tcache_unsorted_limit > 0
 	  && tcache_unsorted_count > mp_.tcache_unsorted_limit)
 	{
-	  return tcache_get (tc_idx);
+	  void *p = tcache_get (tc_idx);
+	  alloc_perturb (p, bytes);
+	  return p;
 	}
 #endif
 
@@ -3976,7 +3980,9 @@  _int_malloc (mstate av, size_t bytes)
       /* If all the small chunks we found ended up cached, return one now.  */
       if (return_cached)
 	{
-	  return tcache_get (tc_idx);
+	  void *p = tcache_get (tc_idx);
+	  alloc_perturb (p, bytes);
+	  return p;
 	}
 #endif