Blame support/next_to_fault.c

Packit 6c4009
/* Memory allocation next to an unmapped page.
Packit 6c4009
   Copyright (C) 2017-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
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the 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; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
#include <support/next_to_fault.h>
Packit 6c4009
#include <support/xunistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
#include <sys/param.h>
Packit 6c4009
Packit 6c4009
struct support_next_to_fault
Packit 6c4009
support_next_to_fault_allocate (size_t size)
Packit 6c4009
{
Packit 6c4009
  long page_size = sysconf (_SC_PAGE_SIZE);
Packit 6c4009
  TEST_VERIFY_EXIT (page_size > 0);
Packit 6c4009
  struct support_next_to_fault result;
Packit 6c4009
  result.region_size = roundup (size, page_size) + page_size;
Packit 6c4009
  if (size + page_size <= size || result.region_size <= size)
Packit 6c4009
    FAIL_EXIT1 ("support_next_to_fault_allocate (%zu): overflow", size);
Packit 6c4009
  result.region_start
Packit 6c4009
    = xmmap (NULL, result.region_size, PROT_READ | PROT_WRITE,
Packit 6c4009
             MAP_PRIVATE | MAP_ANONYMOUS, -1);
Packit 6c4009
  /* Unmap the page after the allocation.  */
Packit 6c4009
  xmprotect (result.region_start + (result.region_size - page_size),
Packit 6c4009
             page_size, PROT_NONE);
Packit 6c4009
  /* Align the allocation within the region so that it ends just
Packit 6c4009
     before the PROT_NONE page.  */
Packit 6c4009
  result.buffer = result.region_start + result.region_size - page_size - size;
Packit 6c4009
  result.length = size;
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
void
Packit 6c4009
support_next_to_fault_free (struct support_next_to_fault *ntf)
Packit 6c4009
{
Packit 6c4009
  xmunmap (ntf->region_start, ntf->region_size);
Packit 6c4009
  *ntf = (struct support_next_to_fault) { NULL, };
Packit 6c4009
}