Blame posix/tst-mmap.c

Packit 6c4009
#include <assert.h>
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stdio.h>
Packit 6c4009
#include <stdlib.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <sys/mman.h>
Packit 6c4009
Packit 6c4009
Packit 6c4009
static int
Packit 6c4009
do_test (void)
Packit 6c4009
{
Packit 6c4009
  int result = 0;
Packit 6c4009
  FILE *fp;
Packit 6c4009
  size_t c;
Packit 6c4009
  char buf[1000];
Packit 6c4009
  int fd;
Packit 6c4009
  unsigned char *ptr;
Packit 6c4009
  size_t ps = sysconf (_SC_PAGESIZE);
Packit 6c4009
  void *mem;
Packit 6c4009
Packit 6c4009
  /* Create a file and put some data in it.  */
Packit 6c4009
  fp = tmpfile ();
Packit 6c4009
  if (fp == NULL)
Packit 6c4009
    {
Packit 6c4009
      printf ("Cannot create temporary file: %m\n");
Packit 6c4009
      return 1;
Packit 6c4009
    }
Packit 6c4009
  fd = fileno (fp);
Packit 6c4009
Packit 6c4009
  for (c = 0; c < sizeof (buf); ++c)
Packit 6c4009
    buf[c] = '0' + (c % 10);
Packit 6c4009
Packit 6c4009
  for (c = 0; c < (ps * 4) / sizeof (buf); ++c)
Packit 6c4009
    if (fwrite (buf, 1, sizeof (buf), fp) != sizeof (buf))
Packit 6c4009
      {
Packit 6c4009
	printf ("`fwrite' failed: %m\n");
Packit 6c4009
	return 1;
Packit 6c4009
      }
Packit 6c4009
  fflush (fp);
Packit 6c4009
  assert (ps + 1000 < c * sizeof (buf));
Packit 6c4009
Packit 6c4009
  /* First try something which is not allowed: map at an offset which is
Packit 6c4009
     not modulo the pagesize.  */
Packit 6c4009
  ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
Packit 6c4009
  if (ptr != MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
    {
Packit 6c4009
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try the same for mmap64.  */
Packit 6c4009
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps - 1);
Packit 6c4009
  if (ptr != MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
    {
Packit 6c4009
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And the same for private mapping.  */
Packit 6c4009
  ptr = mmap (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
Packit 6c4009
  if (ptr != MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
    {
Packit 6c4009
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Try the same for mmap64.  */
Packit 6c4009
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_PRIVATE, fd, ps - 1);
Packit 6c4009
  if (ptr != MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      puts ("mapping at offset with mod pagesize != 0 succeeded!");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
  else if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
    {
Packit 6c4009
      puts ("wrong error value for mapping at offset with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
      result = 1;
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Get a valid address.  */
Packit 6c4009
  mem = malloc (2 * ps);
Packit 6c4009
  if (mem != NULL)
Packit 6c4009
    {
Packit 6c4009
      /* Now we map at an address which is not mod pagesize.  */
Packit 6c4009
      ptr = mmap (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
Packit 6c4009
      if (ptr != MAP_FAILED)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else  if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Try the same for mmap64.  */
Packit 6c4009
      ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_SHARED | MAP_FIXED, fd, ps);
Packit 6c4009
      if (ptr != MAP_FAILED)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else  if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* And again for MAP_PRIVATE.  */
Packit 6c4009
      ptr = mmap (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
Packit 6c4009
      if (ptr != MAP_FAILED)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else  if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      /* Try the same for mmap64.  */
Packit 6c4009
      ptr = mmap64 (mem + 1, 1000, PROT_READ, MAP_PRIVATE | MAP_FIXED, fd, ps);
Packit 6c4009
      if (ptr != MAP_FAILED)
Packit 6c4009
	{
Packit 6c4009
	  puts ("mapping at address with mod pagesize != 0 succeeded!");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
      else  if (errno != EINVAL && errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  puts ("wrong error value for mapping at address with mod pagesize != 0: %m (should be EINVAL)");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
Packit 6c4009
      free (mem);
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* Now map the memory and see whether the content of the mapped area
Packit 6c4009
     is correct.  */
Packit 6c4009
  ptr = mmap (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
Packit 6c4009
  if (ptr == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      if (errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot mmap file: %m\n");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      for (c = ps; c < ps + 1000; ++c)
Packit 6c4009
	if (ptr[c - ps] != '0' + (c % 10))
Packit 6c4009
	  {
Packit 6c4009
	    printf ("wrong data mapped at offset %zd\n", c);
Packit 6c4009
	    result = 1;
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* And for mmap64. */
Packit 6c4009
  ptr = mmap64 (NULL, 1000, PROT_READ, MAP_SHARED, fd, ps);
Packit 6c4009
  if (ptr == MAP_FAILED)
Packit 6c4009
    {
Packit 6c4009
      if (errno != ENOSYS)
Packit 6c4009
	{
Packit 6c4009
	  printf ("cannot mmap file: %m\n");
Packit 6c4009
	  result = 1;
Packit 6c4009
	}
Packit 6c4009
    }
Packit 6c4009
  else
Packit 6c4009
    {
Packit 6c4009
      for (c = ps; c < ps + 1000; ++c)
Packit 6c4009
	if (ptr[c - ps] != '0' + (c % 10))
Packit 6c4009
	  {
Packit 6c4009
	    printf ("wrong data mapped at offset %zd\n", c);
Packit 6c4009
	    result = 1;
Packit 6c4009
	  }
Packit 6c4009
    }
Packit 6c4009
Packit 6c4009
  /* That's it.  */
Packit 6c4009
  return result;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
#define TEST_FUNCTION do_test ()
Packit 6c4009
#include "../test-skeleton.c"