Blame malloc/tst-tcfree3.c

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