50f89d
commit 7c9a7c68363051cfc5fa1ebb96b3b2c1f82dcb76
50f89d
Author: DJ Delorie <dj@redhat.com>
50f89d
Date:   Fri Nov 30 22:13:09 2018 -0500
50f89d
50f89d
    malloc: Add another test for tcache double free check.
50f89d
    
50f89d
    This one tests for BZ#23907 where the double free
50f89d
    test didn't check the tcache bin bounds before dereferencing
50f89d
    the bin.
50f89d
    
50f89d
    [BZ #23907]
50f89d
    * malloc/tst-tcfree3.c: New.
50f89d
    * malloc/Makefile: Add it.
50f89d
50f89d
diff --git a/malloc/Makefile b/malloc/Makefile
50f89d
index e6dfbfc14cb3d140..388cf7e9ee3a2569 100644
50f89d
--- a/malloc/Makefile
50f89d
+++ b/malloc/Makefile
50f89d
@@ -38,7 +38,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
50f89d
 	 tst-malloc_info \
50f89d
 	 tst-malloc-too-large \
50f89d
 	 tst-malloc-stats-cancellation \
50f89d
-	 tst-tcfree1 tst-tcfree2 \
50f89d
+	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
50f89d
 
50f89d
 tests-static := \
50f89d
 	 tst-interpose-static-nothread \
50f89d
diff --git a/malloc/tst-tcfree3.c b/malloc/tst-tcfree3.c
50f89d
new file mode 100644
50f89d
index 0000000000000000..016d30ddd8114082
50f89d
--- /dev/null
50f89d
+++ b/malloc/tst-tcfree3.c
50f89d
@@ -0,0 +1,56 @@
50f89d
+/* Test that malloc tcache catches double free.
50f89d
+   Copyright (C) 2018 Free Software Foundation, Inc.
50f89d
+   This file is part of the GNU C Library.
50f89d
+
50f89d
+   The GNU C Library is free software; you can redistribute it and/or
50f89d
+   modify it under the terms of the GNU Lesser General Public
50f89d
+   License as published by the Free Software Foundation; either
50f89d
+   version 2.1 of the License, or (at your option) any later version.
50f89d
+
50f89d
+   The GNU C Library is distributed in the hope that it will be useful,
50f89d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
50f89d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
50f89d
+   Lesser General Public License for more details.
50f89d
+
50f89d
+   You should have received a copy of the GNU Lesser General Public
50f89d
+   License along with the GNU C Library; if not, see
50f89d
+   <http://www.gnu.org/licenses/>.  */
50f89d
+
50f89d
+#include <malloc.h>
50f89d
+#include <string.h>
50f89d
+
50f89d
+/* Prevent GCC from optimizing away any malloc/free pairs.  */
50f89d
+#pragma GCC optimize ("O0")
50f89d
+
50f89d
+static int
50f89d
+do_test (void)
50f89d
+{
50f89d
+  /* Do two allocation of any size that fit in tcache, and one that
50f89d
+     doesn't.  */
50f89d
+  int ** volatile a = malloc (32);
50f89d
+  int ** volatile b = malloc (32);
50f89d
+  /* This is just under the mmap threshold.  */
50f89d
+  int ** volatile c = malloc (127 * 1024);
50f89d
+
50f89d
+  /* The invalid "tcache bucket" we might dereference will likely end
50f89d
+     up somewhere within this memory block, so make all the accidental
50f89d
+     "next" pointers cause segfaults.  BZ #23907.  */
50f89d
+  memset (c, 0xff, 127 * 1024);
50f89d
+
50f89d
+  free (a); // puts in tcache
50f89d
+
50f89d
+  /* A is now free and contains the key we use to detect in-tcache.
50f89d
+     Copy the key to the other chunks.  */
50f89d
+  memcpy (b, a, 32);
50f89d
+  memcpy (c, a, 32);
50f89d
+
50f89d
+  /* This free tests the "are we in the tcache already" loop with a
50f89d
+     VALID bin but "coincidental" matching key.  */
50f89d
+  free (b); // should NOT abort
50f89d
+  /* This free tests the "is it a valid tcache bin" test.  */
50f89d
+  free (c); // should NOT abort
50f89d
+
50f89d
+  return 0;
50f89d
+}
50f89d
+
50f89d
+#include <support/test-driver.c>