Blame malloc/tst-tcfree3.c

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