Blame malloc/tst-tcfree3.c

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