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