hjl / source-git / glibc

Forked from source-git/glibc 4 years ago
Clone

Blame malloc/tst-tcfree3.c

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