Blame malloc/tst-tcfree3.c

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