Subject: New autoconf tests - let's try this again
From: "Zack Weinberg" <[email-masked]>
To: [email-masked]
Cc: [email-masked]
Date: Tue, 9 Jan 2001 09:49:44 -0800

The previous generation of this test script got a bunch of spurious
failures (code 7) because it didn't try hard enough to get two
consecutive maps allocated next to each other.  This one should work
better - I still can't get a success on HP/UX, though (clues
appreciated).

If you tried the previous version of this script and got "buggy"
reports, I'd appreciate it if you would try this one.

[Russ - SGI cc doesn't work on the raptors.]

zw

#! /bin/sh

mkdir mmt$$ || exit 1
cd mmt$$ || exit 1

if test -z "$CC"; then
  if command -v cc >/dev/null 2>&1; then
    CC=cc
  elif command -v gcc >/dev/null 2>&1; then
    CC=gcc
  else
    echo "Cannot find compiler, set CC in environment"
    exit 1
  fi
fi

cat >testcore.h <<EOF
/* Test by Richard Henderson and Alexandre Oliva.
   Check whether mmap MAP_ANONYMOUS or mmap from /dev/zero works. */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>

#ifndef MAP_ANON
# ifdef MAP_ANONYMOUS
#  define MAP_ANON MAP_ANONYMOUS
# else
#  define MAP_ANON 0
# endif
#endif

#ifndef MAP_FAILED
# define MAP_FAILED -1
#endif

#undef perror_exit
#define perror_exit(str, val) \
  do { perror(str); exit(val); } while (0)

static char *
anonmap (size, loc)
     size_t size;
     char *loc;
{
  int flags;
#ifndef USE_MAP_ANON
  static int devzero = -1;
#endif

  if (loc)
    flags = MAP_PRIVATE|MAP_FIXED;
  else
    flags = MAP_PRIVATE;

#ifdef USE_MAP_ANON
  return (char *) mmap (loc, size, PROT_READ|PROT_WRITE, flags|MAP_ANON, -1, 0);
#else
  if (devzero == -1)
    {
      devzero = open ("/dev/zero", O_RDWR);
      if (devzero < 0)
	perror_exit ("open /dev/zero", 64);
    }
  return (char *) mmap (loc, size, PROT_READ|PROT_WRITE, flags, devzero, 0);
#endif
}
EOF

cat >test1.c <<EOF
#include "testcore.h"

int
main()
{
  int pg = getpagesize ();
  char *x = anonmap (pg, 0);

  if (x == (char *) MAP_FAILED)
    perror_exit("mmap", 1);

  *(int *)x += 1;

  if (munmap(x, pg) < 0)
    perror_exit("munmap", 2);

  exit(0);
}
EOF

if $CC test1.c -o test1z.x >test1z.l 2>&1 && ./test1z.x >>test1z.l 2>&1
then echo "Have mmap from /dev/zero."
     have_dev_zero=y
else echo "No mmap from /dev/zero."
fi

if $CC test1.c -DUSE_MAP_ANON -o test1a.x >test1a.l 2>&1 \
    && ./test1a.x >>test1a.l 2>&1
then echo "Have mmap with MAP_ANON(YMOUS)."
     have_map_anon=y
else echo "No mmap with MAP_ANON(YMOUS)."
fi

cat >test2.c <<EOF
#include "testcore.h"
#include <signal.h>
#include <setjmp.h>

/* Some versions of cygwin mmap require that munmap is called with the
   same parameters as mmap.  GCC expects that this is not the case.
   Test for various forms of this problem.  Warning - icky signal games.  */
static jmp_buf r;
static size_t pg;

static void
sigsegv (unused)
     int unused;
{
  longjmp (r, 1);
}

/* 1. If we map a 2-page region and unmap its second page, the first page
   must remain.  */
void
test_1 ()
{
  char *x = anonmap (pg * 2, 0);
  if (x == (char *)MAP_FAILED)
    perror_exit ("test 1 mmap", 1);

  signal (SIGSEGV, sigsegv);
  if (setjmp (r))
    perror_exit ("test 1 fault", 2);

  x[0] = 1;
  x[pg] = 1;

  munmap (x + pg, pg);
  x[0] = 2;

  if (setjmp (r))
    {
      munmap (x, pg);
      return;
    }
  x[pg] = 1;
  perror_exit ("test 1 no fault", 3);
}

/* 2. If we map a 2-page region and unmap its first page, the second
   page must remain.  */
void
test_2 ()
{
  char *x = anonmap (pg * 2, 0);
  if (x == (char *)MAP_FAILED)
    perror_exit ("test 2 mmap", 4);

  signal (SIGSEGV, sigsegv);
  if (setjmp (r))
    perror_exit ("test 2 fault", 5);

  x[0] = 1;
  x[pg] = 1;

  munmap (x, pg);
  x[pg] = 2;

  if (setjmp (r))
    {
      munmap (x + pg, pg);
      return;
    }
  x[0] = 1;
  perror_exit ("test 2 no fault", 6);
}

/* 3. If we map two consecutive 1-page regions and unmap them both with
   one munmap, both must go away.  */

void
test_3 ()
{
  char *x, *y;
  x = anonmap (pg, 0);
  if (x == (char *)MAP_FAILED)
    perror_exit ("test 3 mmap 1", 7);
  y = anonmap (pg, x+pg);
  if (y == (char *)MAP_FAILED || x + pg != y)
    perror_exit ("test 3 mmap 2", 7);

  signal (SIGSEGV, sigsegv);
  if (setjmp (r))
    perror_exit ("test 3 fault", 8);

  x[0] = 1;
  x[pg] = 1;

  munmap (x, pg * 2);

  if (setjmp (r) == 0)
    {
      x[0] = 1;
      perror_exit ("test 3 no fault 1", 9);
    }
  
  signal (SIGSEGV, sigsegv);
  if (setjmp (r) == 0)
    {
      x[pg] = 1;
      perror_exit ("test 3 no fault 2", 10);
    }
}

int
main ()
{
  pg = getpagesize ();

  test_1();
  test_2();
  test_3();

  exit(0);
}
EOF

if test -n "$have_dev_zero"; then
  if $CC test2.c  -o test2z.x >test2z.l 2>&1 \
     && ./test2z.x >>test2z.l 2>&1
  then echo "mmap of /dev/zero works."
  else echo "mmap of /dev/zero is buggy. ($?)"
  fi
fi

if test -n "$have_map_anon"; then 
  if $CC test2.c -DUSE_MAP_ANON -o test2a.x >test2a.l 2>&1 \
     && ./test2a.x >>test2a.l 2>&1
  then echo "mmap with MAP_ANON(YMOUS) works."
  else echo "mmap with MAP_ANON(YMOUS) is buggy. ($?)"
  fi
fi

ls -l *.l
uname -a

--
Want to unsubscribe from this list?
Check out: http://cygwin.com/ml/#unsubscribe-simple