Blame malloc/tst-malloc-tcache-leak.c

Packit Service 82fcde
/* Bug 22111: Test that threads do not leak their per thread cache.
Packit Service 82fcde
   Copyright (C) 2015-2018 Free Software Foundation, Inc.
Packit Service 82fcde
   This file is part of the GNU C Library.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 82fcde
   modify it under the terms of the GNU Lesser General Public
Packit Service 82fcde
   License as published by the Free Software Foundation; either
Packit Service 82fcde
   version 2.1 of the License, or (at your option) any later version.
Packit Service 82fcde
Packit Service 82fcde
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 82fcde
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 82fcde
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 82fcde
   Lesser General Public License for more details.
Packit Service 82fcde
Packit Service 82fcde
   You should have received a copy of the GNU Lesser General Public
Packit Service 82fcde
   License along with the GNU C Library; if not, see
Packit Service 82fcde
   <http://www.gnu.org/licenses/>.  */
Packit Service 82fcde
Packit Service 82fcde
/* The point of this test is to start and exit a large number of
Packit Service 82fcde
   threads, while at the same time looking to see if the used
Packit Service 82fcde
   memory grows with each round of threads run.  If the memory
Packit Service 82fcde
   grows above some linear bound we declare the test failed and
Packit Service 82fcde
   that the malloc implementation is leaking memory with each
Packit Service 82fcde
   thread.  This is a good indicator that the thread local cache
Packit Service 82fcde
   is leaking chunks.  */
Packit Service 82fcde
Packit Service 82fcde
#include <stdio.h>
Packit Service 82fcde
#include <stdlib.h>
Packit Service 82fcde
#include <malloc.h>
Packit Service 82fcde
#include <pthread.h>
Packit Service 82fcde
#include <assert.h>
Packit Service 82fcde
Packit Service 82fcde
#include <support/check.h>
Packit Service 82fcde
#include <support/support.h>
Packit Service 82fcde
#include <support/xthread.h>
Packit Service 82fcde
Packit Service 82fcde
void *
Packit Service 82fcde
worker (void *data)
Packit Service 82fcde
{
Packit Service 82fcde
  void *ret;
Packit Service 82fcde
  /* Allocate an arbitrary amount of memory that is known to fit into
Packit Service 82fcde
     the thread local cache (tcache).  If we have at least 64 bins
Packit Service 82fcde
     (default e.g. TCACHE_MAX_BINS) we should be able to allocate 32
Packit Service 82fcde
     bytes and force malloc to fill the tcache.  We are assuming tcahce
Packit Service 82fcde
     init happens at the first small alloc, but it might in the future
Packit Service 82fcde
     be deferred to some other point.  Therefore to future proof this
Packit Service 82fcde
     test we include a full alloc/free/alloc cycle for the thread.  We
Packit Service 82fcde
     need a compiler barrier to avoid the removal of the useless
Packit Service 82fcde
     alloc/free.  We send some memory back to main to have the memory
Packit Service 82fcde
     freed after the thread dies, as just another check that the chunks
Packit Service 82fcde
     that were previously in the tcache are still OK to free after
Packit Service 82fcde
     thread death.  */
Packit Service 82fcde
  ret = xmalloc (32);
Packit Service 82fcde
  __asm__ volatile ("" ::: "memory");
Packit Service 82fcde
  free (ret);
Packit Service 82fcde
  return (void *) xmalloc (32);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
static int
Packit Service 82fcde
do_test (void)
Packit Service 82fcde
{
Packit Service 82fcde
  pthread_t *thread;
Packit Service 82fcde
  struct mallinfo info_before, info_after;
Packit Service 82fcde
  void *retval;
Packit Service 82fcde
Packit Service 82fcde
  /* This is an arbitrary choice. We choose a total of THREADS
Packit Service 82fcde
     threads created and joined.  This gives us enough iterations to
Packit Service 82fcde
     show a leak.  */
Packit Service 82fcde
  int threads = 100000;
Packit Service 82fcde
Packit Service 82fcde
  /* Avoid there being 0 malloc'd data at this point by allocating the
Packit Service 82fcde
     pthread_t required to run the test.  */
Packit Service 82fcde
  thread = (pthread_t *) xcalloc (1, sizeof (pthread_t));
Packit Service 82fcde
Packit Service 82fcde
  info_before = mallinfo ();
Packit Service 82fcde
Packit Service 82fcde
  assert (info_before.uordblks != 0);
Packit Service 82fcde
Packit Service 82fcde
  printf ("INFO: %d (bytes) are in use before starting threads.\n",
Packit Service 82fcde
          info_before.uordblks);
Packit Service 82fcde
Packit Service 82fcde
  for (int loop = 0; loop < threads; loop++)
Packit Service 82fcde
    {
Packit Service 82fcde
      *thread = xpthread_create (NULL, worker, NULL);
Packit Service 82fcde
      retval = xpthread_join (*thread);
Packit Service 82fcde
      free (retval);
Packit Service 82fcde
    }
Packit Service 82fcde
Packit Service 82fcde
  info_after = mallinfo ();
Packit Service 82fcde
  printf ("INFO: %d (bytes) are in use after all threads joined.\n",
Packit Service 82fcde
          info_after.uordblks);
Packit Service 82fcde
Packit Service 82fcde
  /* We need to compare the memory in use before and the memory in use
Packit Service 82fcde
     after starting and joining THREADS threads.  We almost always grow
Packit Service 82fcde
     memory slightly, but not much. Consider that if even 1-byte leaked
Packit Service 82fcde
     per thread we'd have THREADS bytes of additional memory, and in
Packit Service 82fcde
     general the in-use at the start of main is quite low.  We will
Packit Service 82fcde
     always leak a full malloc chunk, and never just 1-byte, therefore
Packit Service 82fcde
     anything above "+ threads" from the start (constant offset) is a
Packit Service 82fcde
     leak.  Obviously this assumes no thread-related malloc'd internal
Packit Service 82fcde
     libc data structures persist beyond the thread death, and any that
Packit Service 82fcde
     did would limit the number of times you could call pthread_create,
Packit Service 82fcde
     which is a QoI we'd want to detect and fix.  */
Packit Service 82fcde
  if (info_after.uordblks > (info_before.uordblks + threads))
Packit Service 82fcde
    FAIL_EXIT1 ("Memory usage after threads is too high.\n");
Packit Service 82fcde
Packit Service 82fcde
  /* Did not detect excessive memory usage.  */
Packit Service 82fcde
  free (thread);
Packit Service 82fcde
  exit (0);
Packit Service 82fcde
}
Packit Service 82fcde
Packit Service 82fcde
#define TIMEOUT 50
Packit Service 82fcde
#include <support/test-driver.c>