Blame malloc/tst-interpose-aux.c

Packit 6c4009
/* Minimal malloc implementation for interposition tests.
Packit 6c4009
   Copyright (C) 2016-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public License as
Packit 6c4009
   published by the Free Software Foundation; either version 2.1 of the
Packit 6c4009
   License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; see the file COPYING.LIB.  If
Packit 6c4009
   not, see <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include "tst-interpose-aux.h"
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdarg.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/uio.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
#include <pthread.h>
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
/* Print the error message and terminate the process with status 1.  */
Packit 6c4009
__attribute__ ((noreturn))
Packit 6c4009
__attribute__ ((format (printf, 1, 2)))
Packit 6c4009
static void *
Packit 6c4009
fail (const char *format, ...)
Packit 6c4009
{
Packit 6c4009
  /* This assumes that vsnprintf will not call malloc.  It does not do
Packit 6c4009
     so for the format strings we use.  */
Packit 6c4009
  char message[4096];
Packit 6c4009
  va_list ap;
Packit 6c4009
  va_start (ap, format);
Packit 6c4009
  vsnprintf (message, sizeof (message), format, ap);
Packit 6c4009
  va_end (ap);
Packit 6c4009
Packit 6c4009
  enum { count = 3 };
Packit 6c4009
  struct iovec iov[count];
Packit 6c4009
Packit 6c4009
  iov[0].iov_base = (char *) "error: ";
Packit 6c4009
  iov[1].iov_base = (char *) message;
Packit 6c4009
  iov[2].iov_base = (char *) "\n";
Packit 6c4009
Packit 6c4009
  for (int i = 0; i < count; ++i)
Packit 6c4009
    iov[i].iov_len = strlen (iov[i].iov_base);
Packit 6c4009
Packit 6c4009
  int unused __attribute__ ((unused));
Packit 6c4009
  unused = writev (STDOUT_FILENO, iov, count);
Packit 6c4009
  _exit (1);
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
Packit 6c4009
#endif
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
lock (void)
Packit 6c4009
{
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
  int ret = pthread_mutex_lock (&mutex);
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    {
Packit 6c4009
      errno = ret;
Packit 6c4009
      fail ("pthread_mutex_lock: %m");
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
unlock (void)
Packit 6c4009
{
Packit 6c4009
#if INTERPOSE_THREADS
Packit 6c4009
  int ret = pthread_mutex_unlock (&mutex);
Packit 6c4009
  if (ret != 0)
Packit 6c4009
    {
Packit 6c4009
      errno = ret;
Packit 6c4009
      fail ("pthread_mutex_unlock: %m");
Packit 6c4009
    }
Packit 6c4009
#endif
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
struct __attribute__ ((aligned (__alignof__ (max_align_t)))) allocation_header
Packit 6c4009
{
Packit 6c4009
  size_t allocation_index;
Packit 6c4009
  size_t allocation_size;
Packit 6c4009
};
Packit 6c4009
Packit 6c4009
/* Array of known allocations, to track invalid frees.  */
Packit 6c4009
enum { max_allocations = 65536 };
Packit 6c4009
static struct allocation_header *allocations[max_allocations];
Packit 6c4009
static size_t allocation_index;
Packit 6c4009
static size_t deallocation_count;
Packit 6c4009
Packit 6c4009
/* Sanity check for successful malloc interposition.  */
Packit 6c4009
__attribute__ ((destructor))
Packit 6c4009
static void
Packit 6c4009
check_for_allocations (void)
Packit 6c4009
{
Packit 6c4009
  if (allocation_index == 0)
Packit 6c4009
    {
Packit 6c4009
      /* Make sure that malloc is called at least once from libc.  */
Packit 6c4009
      void *volatile ptr = strdup ("ptr");
Packit 6c4009
      /* Compiler barrier.  The strdup function calls malloc, which
Packit 6c4009
         updates allocation_index, but strdup is marked __THROW, so
Packit 6c4009
         the compiler could optimize away the reload.  */
Packit 6c4009
      __asm__ volatile ("" ::: "memory");
Packit 6c4009
      free (ptr);
Packit 6c4009
      /* If the allocation count is still zero, it means we did not
Packit 6c4009
         interpose malloc successfully.  */
Packit 6c4009
      if (allocation_index == 0)
Packit 6c4009
        fail ("malloc does not seem to have been interposed");
Packit 6c4009
    }
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static struct allocation_header *get_header (const char *op, void *ptr)
Packit 6c4009
{
Packit 6c4009
  struct allocation_header *header = ((struct allocation_header *) ptr) - 1;
Packit 6c4009
  if (header->allocation_index >= allocation_index)
Packit 6c4009
    fail ("%s: %p: invalid allocation index: %zu (not less than %zu)",
Packit 6c4009
          op, ptr, header->allocation_index, allocation_index);
Packit 6c4009
  if (allocations[header->allocation_index] != header)
Packit 6c4009
    fail ("%s: %p: allocation pointer does not point to header, but %p",
Packit 6c4009
          op, ptr, allocations[header->allocation_index]);
Packit 6c4009
  return header;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Internal helper functions.  Those must be called while the lock is
Packit 6c4009
   acquired.  */
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
malloc_internal (size_t size)
Packit 6c4009
{
Packit 6c4009
  if (allocation_index == max_allocations)
Packit 6c4009
    {
Packit 6c4009
      errno = ENOMEM;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  size_t allocation_size = size + sizeof (struct allocation_header);
Packit 6c4009
  if (allocation_size < size)
Packit 6c4009
    {
Packit 6c4009
      errno = ENOMEM;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  size_t index = allocation_index++;
Packit 6c4009
  void *result = mmap (NULL, allocation_size, PROT_READ | PROT_WRITE,
Packit 6c4009
                       MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
Packit 6c4009
  if (result == MAP_FAILED)
Packit 6c4009
    return NULL;
Packit 6c4009
  allocations[index] = result;
Packit 6c4009
  *allocations[index] = (struct allocation_header)
Packit 6c4009
    {
Packit 6c4009
      .allocation_index = index,
Packit 6c4009
      .allocation_size = allocation_size
Packit 6c4009
    };
Packit 6c4009
  return allocations[index] + 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
free_internal (const char *op, struct allocation_header *header)
Packit 6c4009
{
Packit 6c4009
  size_t index = header->allocation_index;
Packit 6c4009
  int result = mprotect (header, header->allocation_size, PROT_NONE);
Packit 6c4009
  if (result != 0)
Packit 6c4009
    fail ("%s: mprotect (%p, %zu): %m", op, header, header->allocation_size);
Packit 6c4009
  /* Catch double-free issues.  */
Packit 6c4009
  allocations[index] = NULL;
Packit 6c4009
  ++deallocation_count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
static void *
Packit 6c4009
realloc_internal (void *ptr, size_t new_size)
Packit 6c4009
{
Packit 6c4009
  struct allocation_header *header = get_header ("realloc", ptr);
Packit 6c4009
  size_t old_size = header->allocation_size - sizeof (struct allocation_header);
Packit 6c4009
  if (old_size >= new_size)
Packit 6c4009
    return ptr;
Packit 6c4009
Packit 6c4009
  void *newptr = malloc_internal (new_size);
Packit 6c4009
  if (newptr == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
  memcpy (newptr, ptr, old_size);
Packit 6c4009
  free_internal ("realloc", header);
Packit 6c4009
  return newptr;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Public interfaces.  These functions must perform locking.  */
Packit 6c4009
Packit 6c4009
size_t
Packit 6c4009
malloc_allocation_count (void)
Packit 6c4009
{
Packit 6c4009
  lock ();
Packit 6c4009
  size_t count = allocation_index;
Packit 6c4009
  unlock ();
Packit 6c4009
  return count;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
size_t
Packit 6c4009
malloc_deallocation_count (void)
Packit 6c4009
{
Packit 6c4009
  lock ();
Packit 6c4009
  size_t count = deallocation_count;
Packit 6c4009
  unlock ();
Packit 6c4009
  return count;
Packit 6c4009
}
Packit 6c4009
void *
Packit 6c4009
malloc (size_t size)
Packit 6c4009
{
Packit 6c4009
  lock ();
Packit 6c4009
  void *result = malloc_internal (size);
Packit 6c4009
  unlock ();
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
free (void *ptr)
Packit 6c4009
{
Packit 6c4009
  if (ptr == NULL)
Packit 6c4009
    return;
Packit 6c4009
  lock ();
Packit 6c4009
  struct allocation_header *header = get_header ("free", ptr);
Packit 6c4009
  free_internal ("free", header);
Packit 6c4009
  unlock ();
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
calloc (size_t a, size_t b)
Packit 6c4009
{
Packit 6c4009
  if (b > 0 && a > SIZE_MAX / b)
Packit 6c4009
    {
Packit 6c4009
      errno = ENOMEM;
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  lock ();
Packit 6c4009
  /* malloc_internal uses mmap, so the memory is zeroed.  */
Packit 6c4009
  void *result = malloc_internal (a * b);
Packit 6c4009
  unlock ();
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void *
Packit 6c4009
realloc (void *ptr, size_t n)
Packit 6c4009
{
Packit 6c4009
  if (n ==0)
Packit 6c4009
    {
Packit 6c4009
      free (ptr);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
  else if (ptr == NULL)
Packit 6c4009
    return malloc (n);
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      lock ();
Packit 6c4009
      void *result = realloc_internal (ptr, n);
Packit 6c4009
      unlock ();
Packit 6c4009
      return result;
Packit 6c4009
    }
Packit 6c4009
}