Blame malloc/tst-tcfree3.c

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