Blame posix/tst-mmap-offset.c

Packit 6c4009
/* BZ #18877 and #21270 mmap offset test.
Packit 6c4009
Packit 6c4009
   Copyright (C) 2015-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 <stdint.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <string.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
Packit 6c4009
#include <support/check.h>
Packit 6c4009
Packit 6c4009
static int fd;
Packit 6c4009
static long int page_shift;
Packit 6c4009
static char fname[] = "/tmp/tst-mmap-offset-XXXXXX";
Packit 6c4009
Packit 6c4009
static void
Packit 6c4009
do_prepare (int argc, char **argv)
Packit 6c4009
{
Packit 6c4009
  fd = mkstemp64 (fname);
Packit 6c4009
  if (fd < 0)
Packit 6c4009
    FAIL_EXIT1 ("mkstemp failed");
Packit 6c4009
Packit 6c4009
  if (unlink (fname))
Packit 6c4009
    FAIL_EXIT1 ("unlink failed");
Packit 6c4009
Packit 6c4009
  long sz = sysconf(_SC_PAGESIZE);
Packit 6c4009
  if (sz == -1)
Packit 6c4009
    sz = 4096L;
Packit 6c4009
  page_shift = ffs (sz) - 1;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define PREPARE do_prepare
Packit 6c4009
Packit 6c4009
Packit 6c4009
/* Check if negative offsets are handled correctly by mmap.  */
Packit 6c4009
static int
Packit 6c4009
do_test_bz18877 (void)
Packit 6c4009
{
Packit 6c4009
  const int prot = PROT_READ | PROT_WRITE;
Packit 6c4009
  const int flags = MAP_SHARED;
Packit 6c4009
  const unsigned long length = 0x10000;
Packit 6c4009
  const unsigned long offset = 0xace00000;
Packit 6c4009
  const unsigned long size = offset + length;
Packit 6c4009
  void *addr;
Packit 6c4009
Packit 6c4009
  if (ftruncate64 (fd, size))
Packit 6c4009
    FAIL_RET ("ftruncate64 failed");
Packit 6c4009
Packit 6c4009
  addr = mmap (NULL, length, prot, flags, fd, offset);
Packit 6c4009
  if (MAP_FAILED == addr)
Packit 6c4009
    FAIL_RET ("mmap failed");
Packit 6c4009
Packit 6c4009
  /* This memcpy is likely to SIGBUS if mmap has messed up with offset.  */
Packit 6c4009
  memcpy (addr, fname, sizeof (fname));
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
/* Check if invalid offset are handled correctly by mmap.  */
Packit 6c4009
static int
Packit 6c4009
do_test_bz21270 (void)
Packit 6c4009
{
Packit 6c4009
  /* For architectures with sizeof (off_t) < sizeof (off64_t) mmap is
Packit 6c4009
     implemented with __SYS_mmap2 syscall and the offset is represented in
Packit 6c4009
     multiples of page size.  For offset larger than
Packit 6c4009
     '1 << (page_shift + 8 * sizeof (off_t))' (that is, 1<<44 on system with
Packit 6c4009
     page size of 4096 bytes) the system call silently truncates the offset.
Packit 6c4009
     For this case glibc mmap implementation returns EINVAL.  */
Packit 6c4009
  const int prot = PROT_READ | PROT_WRITE;
Packit 6c4009
  const int flags = MAP_SHARED;
Packit 6c4009
  const int64_t offset = 1ULL << (page_shift + 8 * sizeof (uint32_t));
Packit 6c4009
  const size_t length = 4096;
Packit 6c4009
Packit 6c4009
  void *addr = mmap64 (NULL, length, prot, flags, fd, offset);
Packit 6c4009
  if (sizeof (off_t) < sizeof (off64_t))
Packit 6c4009
    {
Packit 6c4009
      if ((addr != MAP_FAILED) && (errno != EINVAL))
Packit 6c4009
	FAIL_RET ("mmap succeed");
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      if (addr == MAP_FAILED)
Packit 6c4009
	FAIL_RET ("mmap failed");
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  return 0;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int ret = 0;
Packit 6c4009
Packit 6c4009
  ret += do_test_bz18877 ();
Packit 6c4009
  ret += do_test_bz21270 ();
Packit 6c4009
Packit 6c4009
  return ret;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#include <support/test-driver.c>