d8307d
commit 58d2672f64176fcb323859d3bd5240fb1cf8f25c
d8307d
Author: Wilco Dijkstra <wdijkstr@arm.com>
d8307d
Date:   Fri May 10 16:38:21 2019 +0100
d8307d
d8307d
    Fix tcache count maximum (BZ #24531)
d8307d
    
d8307d
    The tcache counts[] array is a char, which has a very small range and thus
d8307d
    may overflow.  When setting tcache_count tunable, there is no overflow check.
d8307d
    However the tunable must not be larger than the maximum value of the tcache
d8307d
    counts[] array, otherwise it can overflow when filling the tcache.
d8307d
    
d8307d
            [BZ #24531]
d8307d
            * malloc/malloc.c (MAX_TCACHE_COUNT): New define.
d8307d
            (do_set_tcache_count): Only update if count is small enough.
d8307d
            * manual/tunables.texi (glibc.malloc.tcache_count): Document max value.
d8307d
    
d8307d
    (cherry picked from commit 5ad533e8e65092be962e414e0417112c65d154fb)
d8307d
d8307d
diff --git a/malloc/malloc.c b/malloc/malloc.c
d8307d
index 723d393f529bdb4c..92239b3324584060 100644
d8307d
--- a/malloc/malloc.c
d8307d
+++ b/malloc/malloc.c
d8307d
@@ -2919,6 +2919,8 @@ typedef struct tcache_perthread_struct
d8307d
   tcache_entry *entries[TCACHE_MAX_BINS];
d8307d
 } tcache_perthread_struct;
d8307d
 
d8307d
+#define MAX_TCACHE_COUNT 127	/* Maximum value of counts[] entries.  */
d8307d
+
d8307d
 static __thread bool tcache_shutting_down = false;
d8307d
 static __thread tcache_perthread_struct *tcache = NULL;
d8307d
 
d8307d
@@ -5124,8 +5126,11 @@ static inline int
d8307d
 __always_inline
d8307d
 do_set_tcache_count (size_t value)
d8307d
 {
d8307d
-  LIBC_PROBE (memory_tunable_tcache_count, 2, value, mp_.tcache_count);
d8307d
-  mp_.tcache_count = value;
d8307d
+  if (value <= MAX_TCACHE_COUNT)
d8307d
+    {
d8307d
+      LIBC_PROBE (memory_tunable_tcache_count, 2, value, mp_.tcache_count);
d8307d
+      mp_.tcache_count = value;
d8307d
+    }
d8307d
   return 1;
d8307d
 }
d8307d
 
d8307d
diff --git a/manual/tunables.texi b/manual/tunables.texi
d8307d
index bb4819bdf1de273e..9dccf2ee7f8eec17 100644
d8307d
--- a/manual/tunables.texi
d8307d
+++ b/manual/tunables.texi
d8307d
@@ -188,8 +188,8 @@ per-thread cache.  The default (and maximum) value is 1032 bytes on
d8307d
 
d8307d
 @deftp Tunable glibc.malloc.tcache_count
d8307d
 The maximum number of chunks of each size to cache. The default is 7.
d8307d
-There is no upper limit, other than available system memory.  If set
d8307d
-to zero, the per-thread cache is effectively disabled.
d8307d
+The upper limit is 127.  If set to zero, the per-thread cache is effectively
d8307d
+disabled.
d8307d
 
d8307d
 The approximate maximum overhead of the per-thread cache is thus equal
d8307d
 to the number of bins times the chunk count in each bin times the size