Blame support/blob_repeat.c

Packit Service 20a62b
/* Repeating a memory blob, with alias mapping optimization.
Packit Service 20a62b
   Copyright (C) 2018 Free Software Foundation, Inc.
Packit Service 20a62b
   This file is part of the GNU C Library.
Packit Service 20a62b
Packit Service 20a62b
   The GNU C Library is free software; you can redistribute it and/or
Packit Service 20a62b
   modify it under the terms of the GNU Lesser General Public
Packit Service 20a62b
   License as published by the Free Software Foundation; either
Packit Service 20a62b
   version 2.1 of the License, or (at your option) any later version.
Packit Service 20a62b
Packit Service 20a62b
   The GNU C Library is distributed in the hope that it will be useful,
Packit Service 20a62b
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 20a62b
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service 20a62b
   Lesser General Public License for more details.
Packit Service 20a62b
Packit Service 20a62b
   You should have received a copy of the GNU Lesser General Public
Packit Service 20a62b
   License along with the GNU C Library; if not, see
Packit Service 20a62b
   <http://www.gnu.org/licenses/>.  */
Packit Service 20a62b
Packit Service 20a62b
#include <errno.h>
Packit Service 20a62b
#include <fcntl.h>
Packit Service 20a62b
#include <stdbool.h>
Packit Service 20a62b
#include <stdlib.h>
Packit Service 20a62b
#include <string.h>
Packit Service 20a62b
#include <support/blob_repeat.h>
Packit Service 20a62b
#include <support/check.h>
Packit Service 20a62b
#include <support/test-driver.h>
Packit Service 20a62b
#include <support/support.h>
Packit Service 20a62b
#include <support/xunistd.h>
Packit Service 20a62b
#include <sys/mman.h>
Packit Service 20a62b
#include <unistd.h>
Packit Service 20a62b
#include <wchar.h>
Packit Service 20a62b
Packit Service 20a62b
/* Small allocations should use malloc directly instead of the mmap
Packit Service 20a62b
   optimization because mappings carry a lot of overhead.  */
Packit Service 20a62b
static const size_t maximum_small_size = 4 * 1024 * 1024;
Packit Service 20a62b
Packit Service 20a62b
/* Internal helper for fill.  */
Packit Service 20a62b
static void
Packit Service 20a62b
fill0 (char *target, const char *element, size_t element_size,
Packit Service 20a62b
       size_t count)
Packit Service 20a62b
{
Packit Service 20a62b
  while (count > 0)
Packit Service 20a62b
    {
Packit Service 20a62b
      memcpy (target, element, element_size);
Packit Service 20a62b
      target += element_size;
Packit Service 20a62b
      --count;
Packit Service 20a62b
    }
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
/* Fill the buffer at TARGET with COUNT copies of the ELEMENT_SIZE
Packit Service 20a62b
   bytes starting at ELEMENT.  */
Packit Service 20a62b
static void
Packit Service 20a62b
fill (char *target, const char *element, size_t element_size,
Packit Service 20a62b
      size_t count)
Packit Service 20a62b
{
Packit Service 20a62b
  if (element_size == 0 || count == 0)
Packit Service 20a62b
    return;
Packit Service 20a62b
  else if (element_size == 1)
Packit Service 20a62b
    memset (target, element[0], count);
Packit Service 20a62b
  else if (element_size == sizeof (wchar_t))
Packit Service 20a62b
    {
Packit Service 20a62b
      wchar_t wc;
Packit Service 20a62b
      memcpy (&wc, element, sizeof (wc));
Packit Service 20a62b
      wmemset ((wchar_t *) target, wc, count);
Packit Service 20a62b
    }
Packit Service 20a62b
  else if (element_size < 1024 && count > 4096)
Packit Service 20a62b
    {
Packit Service 20a62b
      /* Use larger copies for really small element sizes.  */
Packit Service 20a62b
      char buffer[8192];
Packit Service 20a62b
      size_t buffer_count = sizeof (buffer) / element_size;
Packit Service 20a62b
      fill0 (buffer, element, element_size, buffer_count);
Packit Service 20a62b
      while (count > 0)
Packit Service 20a62b
        {
Packit Service 20a62b
          size_t copy_count = buffer_count;
Packit Service 20a62b
          if (copy_count > count)
Packit Service 20a62b
            copy_count = count;
Packit Service 20a62b
          size_t copy_bytes = copy_count * element_size;
Packit Service 20a62b
          memcpy (target, buffer, copy_bytes);
Packit Service 20a62b
          target += copy_bytes;
Packit Service 20a62b
          count -= copy_count;
Packit Service 20a62b
        }
Packit Service 20a62b
    }
Packit Service 20a62b
  else
Packit Service 20a62b
    fill0 (target, element, element_size, count);
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
/* Use malloc instead of mmap for small allocations and unusual size
Packit Service 20a62b
   combinations.  */
Packit Service 20a62b
static struct support_blob_repeat
Packit Service 20a62b
allocate_malloc (size_t total_size, const void *element, size_t element_size,
Packit Service 20a62b
                 size_t count)
Packit Service 20a62b
{
Packit Service 20a62b
  void *buffer = malloc (total_size);
Packit Service 20a62b
  if (buffer == NULL)
Packit Service 20a62b
    return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
  fill (buffer, element, element_size, count);
Packit Service 20a62b
  return (struct support_blob_repeat)
Packit Service 20a62b
    {
Packit Service 20a62b
      .start = buffer,
Packit Service 20a62b
      .size = total_size,
Packit Service 20a62b
      .use_malloc = true
Packit Service 20a62b
    };
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
/* Return the least common multiple of PAGE_SIZE and ELEMENT_SIZE,
Packit Service 20a62b
   avoiding overflow.  This assumes that PAGE_SIZE is a power of
Packit Service 20a62b
   two.  */
Packit Service 20a62b
static size_t
Packit Service 20a62b
minimum_stride_size (size_t page_size, size_t element_size)
Packit Service 20a62b
{
Packit Service 20a62b
  TEST_VERIFY_EXIT (page_size > 0);
Packit Service 20a62b
  TEST_VERIFY_EXIT (element_size > 0);
Packit Service 20a62b
Packit Service 20a62b
  /* Compute the number of trailing zeros common to both sizes.  */
Packit Service 20a62b
  unsigned int common_zeros = __builtin_ctzll (page_size | element_size);
Packit Service 20a62b
Packit Service 20a62b
  /* In the product, this power of two appears twice, but in the least
Packit Service 20a62b
     common multiple, it appears only once.  Therefore, shift one
Packit Service 20a62b
     factor.  */
Packit Service 20a62b
  size_t multiple;
Packit Service 20a62b
  if (__builtin_mul_overflow (page_size >> common_zeros, element_size,
Packit Service 20a62b
                              &multiple))
Packit Service 20a62b
    return 0;
Packit Service 20a62b
  return multiple;
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
/* Allocations larger than maximum_small_size potentially use mmap
Packit Service 20a62b
   with alias mappings.  */
Packit Service 20a62b
static struct support_blob_repeat
Packit Service 20a62b
allocate_big (size_t total_size, const void *element, size_t element_size,
Packit Service 20a62b
              size_t count)
Packit Service 20a62b
{
Packit Service 20a62b
  unsigned long page_size = xsysconf (_SC_PAGESIZE);
Packit Service 20a62b
  size_t stride_size = minimum_stride_size (page_size, element_size);
Packit Service 20a62b
  if (stride_size == 0)
Packit Service 20a62b
    {
Packit Service 20a62b
      errno = EOVERFLOW;
Packit Service 20a62b
      return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
    }
Packit Service 20a62b
Packit Service 20a62b
  /* Ensure that the stride size is at least maximum_small_size.  This
Packit Service 20a62b
     is necessary to reduce the number of distinct mappings.  */
Packit Service 20a62b
  if (stride_size < maximum_small_size)
Packit Service 20a62b
    stride_size
Packit Service 20a62b
      = ((maximum_small_size + stride_size - 1) / stride_size) * stride_size;
Packit Service 20a62b
Packit Service 20a62b
  if (stride_size > total_size)
Packit Service 20a62b
    /* The mmap optimization would not save anything.  */
Packit Service 20a62b
    return allocate_malloc (total_size, element, element_size, count);
Packit Service 20a62b
Packit Service 20a62b
  /* Reserve the memory region.  If we cannot create the mapping,
Packit Service 20a62b
     there is no reason to set up the backing file.  */
Packit Service 20a62b
  void *target = mmap (NULL, total_size, PROT_NONE,
Packit Service 20a62b
                       MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Packit Service 20a62b
  if (target == MAP_FAILED)
Packit Service 20a62b
    return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
Packit Service 20a62b
  /* Create the backing file for the repeated mapping.  Call mkstemp
Packit Service 20a62b
     directly to remove the resources backing the temporary file
Packit Service 20a62b
     immediately, once support_blob_repeat_free is called.  Using
Packit Service 20a62b
     create_temp_file would result in a warning during post-test
Packit Service 20a62b
     cleanup.  */
Packit Service 20a62b
  int fd;
Packit Service 20a62b
  {
Packit Service 20a62b
    char *temppath = xasprintf ("%s/support_blob_repeat-XXXXXX", test_dir);
Packit Service 20a62b
    fd = mkstemp (temppath);
Packit Service 20a62b
    if (fd < 0)
Packit Service 20a62b
      FAIL_EXIT1 ("mkstemp (\"%s\"): %m", temppath);
Packit Service 20a62b
    xunlink (temppath);
Packit Service 20a62b
    free (temppath);
Packit Service 20a62b
  }
Packit Service 20a62b
Packit Service 20a62b
  /* Make sure that there is backing storage, so that the fill
Packit Service 20a62b
     operation will not fault.  */
Packit Service 20a62b
  if (posix_fallocate (fd, 0, stride_size) != 0)
Packit Service 20a62b
    FAIL_EXIT1 ("posix_fallocate (%zu): %m", stride_size);
Packit Service 20a62b
Packit Service 20a62b
  /* The stride size must still be a multiple of the page size and
Packit Service 20a62b
     element size.  */
Packit Service 20a62b
  TEST_VERIFY_EXIT ((stride_size % page_size) == 0);
Packit Service 20a62b
  TEST_VERIFY_EXIT ((stride_size % element_size) == 0);
Packit Service 20a62b
Packit Service 20a62b
  /* Fill the backing store.  */
Packit Service 20a62b
  {
Packit Service 20a62b
    void *ptr = mmap (target, stride_size, PROT_READ | PROT_WRITE,
Packit Service 20a62b
                      MAP_FIXED | MAP_FILE | MAP_SHARED, fd, 0);
Packit Service 20a62b
    if (ptr == MAP_FAILED)
Packit Service 20a62b
      {
Packit Service 20a62b
        int saved_errno = errno;
Packit Service 20a62b
        xmunmap (target, total_size);
Packit Service 20a62b
        xclose (fd);
Packit Service 20a62b
        errno = saved_errno;
Packit Service 20a62b
        return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
      }
Packit Service 20a62b
    if (ptr != target)
Packit Service 20a62b
      FAIL_EXIT1 ("mapping of %zu bytes moved from %p to %p",
Packit Service 20a62b
                  stride_size, target, ptr);
Packit Service 20a62b
Packit Service 20a62b
    /* Write the repeating data.  */
Packit Service 20a62b
    fill (target, element, element_size, stride_size / element_size);
Packit Service 20a62b
Packit Service 20a62b
    /* Return to a PROT_NONE mapping, just to be on the safe side.  */
Packit Service 20a62b
    ptr = mmap (target, stride_size, PROT_NONE,
Packit Service 20a62b
                MAP_FIXED | MAP_ANONYMOUS | MAP_PRIVATE, -1, 0);
Packit Service 20a62b
    if (ptr == MAP_FAILED)
Packit Service 20a62b
      FAIL_EXIT1 ("Failed to reinstate PROT_NONE mapping: %m");
Packit Service 20a62b
    if (ptr != target)
Packit Service 20a62b
      FAIL_EXIT1 ("PROT_NONE mapping of %zu bytes moved from %p to %p",
Packit Service 20a62b
                  stride_size, target, ptr);
Packit Service 20a62b
  }
Packit Service 20a62b
Packit Service 20a62b
  /* Create the alias mappings.  */
Packit Service 20a62b
  {
Packit Service 20a62b
    size_t remaining_size = total_size;
Packit Service 20a62b
    char *current = target;
Packit Service 20a62b
    int flags = MAP_FIXED | MAP_FILE | MAP_PRIVATE;
Packit Service 20a62b
#ifdef MAP_NORESERVE
Packit Service 20a62b
    flags |= MAP_NORESERVE;
Packit Service 20a62b
#endif
Packit Service 20a62b
    while (remaining_size > 0)
Packit Service 20a62b
      {
Packit Service 20a62b
        size_t to_map = stride_size;
Packit Service 20a62b
        if (to_map > remaining_size)
Packit Service 20a62b
          to_map = remaining_size;
Packit Service 20a62b
        void *ptr = mmap (current, to_map, PROT_READ | PROT_WRITE,
Packit Service 20a62b
                          flags, fd, 0);
Packit Service 20a62b
        if (ptr == MAP_FAILED)
Packit Service 20a62b
          {
Packit Service 20a62b
            int saved_errno = errno;
Packit Service 20a62b
            xmunmap (target, total_size);
Packit Service 20a62b
            xclose (fd);
Packit Service 20a62b
            errno = saved_errno;
Packit Service 20a62b
            return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
          }
Packit Service 20a62b
        if (ptr != current)
Packit Service 20a62b
          FAIL_EXIT1 ("MAP_PRIVATE mapping of %zu bytes moved from %p to %p",
Packit Service 20a62b
                      to_map, target, ptr);
Packit Service 20a62b
        remaining_size -= to_map;
Packit Service 20a62b
        current += to_map;
Packit Service 20a62b
      }
Packit Service 20a62b
  }
Packit Service 20a62b
Packit Service 20a62b
  xclose (fd);
Packit Service 20a62b
Packit Service 20a62b
  return (struct support_blob_repeat)
Packit Service 20a62b
    {
Packit Service 20a62b
      .start = target,
Packit Service 20a62b
      .size = total_size,
Packit Service 20a62b
      .use_malloc = false
Packit Service 20a62b
    };
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
struct support_blob_repeat
Packit Service 20a62b
support_blob_repeat_allocate (const void *element, size_t element_size,
Packit Service 20a62b
                              size_t count)
Packit Service 20a62b
{
Packit Service 20a62b
  size_t total_size;
Packit Service 20a62b
  if (__builtin_mul_overflow (element_size, count, &total_size))
Packit Service 20a62b
    {
Packit Service 20a62b
      errno = EOVERFLOW;
Packit Service 20a62b
      return (struct support_blob_repeat) { 0 };
Packit Service 20a62b
    }
Packit Service 20a62b
  if (total_size <= maximum_small_size)
Packit Service 20a62b
    return allocate_malloc (total_size, element, element_size, count);
Packit Service 20a62b
  else
Packit Service 20a62b
    return allocate_big (total_size, element, element_size, count);
Packit Service 20a62b
}
Packit Service 20a62b
Packit Service 20a62b
void
Packit Service 20a62b
support_blob_repeat_free (struct support_blob_repeat *blob)
Packit Service 20a62b
{
Packit Service 20a62b
  if (blob->size > 0)
Packit Service 20a62b
    {
Packit Service 20a62b
      int saved_errno = errno;
Packit Service 20a62b
      if (blob->use_malloc)
Packit Service 20a62b
        free (blob->start);
Packit Service 20a62b
      else
Packit Service 20a62b
        xmunmap (blob->start, blob->size);
Packit Service 20a62b
      errno = saved_errno;
Packit Service 20a62b
    }
Packit Service 20a62b
  *blob = (struct support_blob_repeat) { 0 };
Packit Service 20a62b
}