Blame malloc/tst-tcfree3.c

Packit Bot eb54d8
/* Test that malloc tcache catches double free.
Packit Bot eb54d8
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Bot eb54d8
   This file is part of the GNU C Library.
Packit Bot eb54d8
Packit Bot eb54d8
   The GNU C Library is free software; you can redistribute it and/or
Packit Bot eb54d8
   modify it under the terms of the GNU Lesser General Public
Packit Bot eb54d8
   License as published by the Free Software Foundation; either
Packit Bot eb54d8
   version 2.1 of the License, or (at your option) any later version.
Packit Bot eb54d8
Packit Bot eb54d8
   The GNU C Library is distributed in the hope that it will be useful,
Packit Bot eb54d8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Bot eb54d8
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Bot eb54d8
   Lesser General Public License for more details.
Packit Bot eb54d8
Packit Bot eb54d8
   You should have received a copy of the GNU Lesser General Public
Packit Bot eb54d8
   License along with the GNU C Library; if not, see
Packit Bot eb54d8
   <http://www.gnu.org/licenses/>.  */
Packit Bot eb54d8
Packit Bot eb54d8
#include <malloc.h>
Packit Bot eb54d8
#include <string.h>
Packit Bot eb54d8
Packit Bot eb54d8
/* Prevent GCC from optimizing away any malloc/free pairs.  */
Packit Bot eb54d8
#pragma GCC optimize ("O0")
Packit Bot eb54d8
Packit Bot eb54d8
static int
Packit Bot eb54d8
do_test (void)
Packit Bot eb54d8
{
Packit Bot eb54d8
  /* Do two allocation of any size that fit in tcache, and one that
Packit Bot eb54d8
     doesn't.  */
Packit Bot eb54d8
  int ** volatile a = malloc (32);
Packit Bot eb54d8
  int ** volatile b = malloc (32);
Packit Bot eb54d8
  /* This is just under the mmap threshold.  */
Packit Bot eb54d8
  int ** volatile c = malloc (127 * 1024);
Packit Bot eb54d8
Packit Bot eb54d8
  /* The invalid "tcache bucket" we might dereference will likely end
Packit Bot eb54d8
     up somewhere within this memory block, so make all the accidental
Packit Bot eb54d8
     "next" pointers cause segfaults.  BZ #23907.  */
Packit Bot eb54d8
  memset (c, 0xff, 127 * 1024);
Packit Bot eb54d8
Packit Bot eb54d8
  free (a); // puts in tcache
Packit Bot eb54d8
Packit Bot eb54d8
  /* A is now free and contains the key we use to detect in-tcache.
Packit Bot eb54d8
     Copy the key to the other chunks.  */
Packit Bot eb54d8
  memcpy (b, a, 32);
Packit Bot eb54d8
  memcpy (c, a, 32);
Packit Bot eb54d8
Packit Bot eb54d8
  /* This free tests the "are we in the tcache already" loop with a
Packit Bot eb54d8
     VALID bin but "coincidental" matching key.  */
Packit Bot eb54d8
  free (b); // should NOT abort
Packit Bot eb54d8
  /* This free tests the "is it a valid tcache bin" test.  */
Packit Bot eb54d8
  free (c); // should NOT abort
Packit Bot eb54d8
Packit Bot eb54d8
  return 0;
Packit Bot eb54d8
}
Packit Bot eb54d8
Packit Bot eb54d8
#include <support/test-driver.c>