autoconf
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: detecting mmap?


From: Akim Demaille
Subject: Re: detecting mmap?
Date: 19 Jul 2002 12:05:40 +0200
User-agent: Gnus/5.0808 (Gnus v5.8.8) XEmacs/21.4 (Honest Recruiter)

>>>>> "Paul" == Paul Eggert <address@hidden> writes:

Paul> The argument for such a change would be stronger if (1) we had a
Paul> proposed patch, and (2) the existing AC_FUNC_MMAP was seriously
Paul> hurting some applications on some platforms.  (Do you have any
Paul> examples of this in mind?)

FWIW, an old thread about this.  ISTR there is PR too.

Subject: Topics

Topics:
   Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows
   Re: Check for `mmap' fails on Cygwin/Windows

--- Begin Message --- Subject: Check for `mmap' fails on Cygwin/Windows Date: Wed, 25 Oct 2000 11:38:40 +0200
When running configure on a Cygwin system, the check for `mmap()'
fails for the following reason:

Windows doesn't support the mmap flags MAP_FIXED the same way
Unix/Linux does. The returned address may be different from the
given address as well as it may fail if the address is in a mem
range already allocated by the calling process.

The reason I think it's a bug in autoconf, though, is that the
usage of MAP_FIXED is discouraged which is documented in the Linux
man pages as well as in SUSv2 documentation:

"Use of MAP_FIXED may result in unspecified behaviour in further
use of brk(), sbrk(), malloc() and shmat(). The use of MAP_FIXED is
discouraged, as it may prevent an implementation from making the
most effective use of resources."

I have attached a patch to acfunctions.m4 which will result in a
working check for `mmap()' on all systems.

Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                        mailto:address@hidden
Red Hat, Inc.
mailto:address@hidden
Index: acfunctions.m4
===================================================================
RCS file: /cvs/autoconf/acfunctions.m4,v
retrieving revision 1.9
diff -u -p -r1.9 acfunctions.m4
--- acfunctions.m4      2000/09/28 14:26:50     1.9
+++ acfunctions.m4      2000/10/05 09:17:26
@@ -1033,12 +1033,8 @@ main ()
   fd = open ("conftestmmap", O_RDWR);
   if (fd < 0)
     exit (1);
-  data2 = (char *) malloc (2 * pagesize);
-  if (!data2)
-    exit (1);
-  data2 += (pagesize - ((int) data2 & (pagesize - 1))) & (pagesize - 1);
-  if (data2 != mmap (data2, pagesize, PROT_READ | PROT_WRITE,
-                     MAP_PRIVATE | MAP_FIXED, fd, 0L))
+  if ((data2 = mmap (NULL, pagesize, PROT_READ | PROT_WRITE,
+                     MAP_PRIVATE, fd, 0L)) == MAP_FAILED)
     exit (1);
   for (i = 0; i < pagesize; ++i)
     if (*(data + i) != *(data2 + i))

--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: 25 Oct 2000 19:07:59 +0200
Thanks for the patch.  I'm not responding because I'm incompetent.  In
addition, Autoconf's checking of mmap has been criticized a lot, and
it is unclear how the test should be performed (lots of combinations
which might interest some people and not others).

I prefer letting Jim, Paul and Alexandre answer this.




--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: 25 Oct 2000 15:56:59 -0700
Akim Demaille <address@hidden> writes:

> Thanks for the patch.  I'm not responding because I'm incompetent.  In
> addition, Autoconf's checking of mmap has been criticized a lot, and
> it is unclear how the test should be performed (lots of combinations
> which might interest some people and not others).

> I prefer letting Jim, Paul and Alexandre answer this.

For what it's worth, this is what we replaced the check for mmap with for
INN, since it checks something much closer to what we actually do.  Note
that we also ripped out all the getpagesize stuff, since a working mmap
should be able to map a memory region of any size, not just a multiple of
the page size (it's the *offset* that has to be a multiple of the page
size).

dnl The set of standard includes, used for checking if functions need to be
dnl declared and for tests that need to use standard functions.
define([_INN_HEADER_SOURCE],
[#include <stdio.h>
#include <sys/types.h>
#if STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
# if !HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
#endif
#if HAVE_STRING_H
# if !STDC_HEADERS && HAVE_MEMORY_H
#  include <memory.h>
# endif
# include <string.h>
#else
# if HAVE_STRINGS_H
#  include <strings.h>
# endif
#endif
#if HAVE_UNISTD_H
# include <unistd.h>
#endif])

dnl Source used by INN_FUNC_MMAP.
define([_INN_FUNC_MMAP_SOURCE],
[_INN_HEADER_SOURCE()]
[[#include <fcntl.h>
#include <sys/mman.h>

int
main()
{
  int *data, *data2;
  int i, fd;

  /* First, make a file with some known garbage in it.  Use something
     larger than one page but still an odd page size. */
  data = malloc (20000);
  if (!data) return 1;
  for (i = 0; i < 20000 / sizeof (int); i++)
    data[i] = rand();
  umask (0);
  fd = creat ("conftestmmaps", 0600);
  if (fd < 0) return 1;
  if (write (fd, data, 20000) != 20000) return 1;
  close (fd);

  /* Next, try to mmap the file and make sure we see the same garbage. */
  fd = open ("conftestmmaps", O_RDWR);
  if (fd < 0) return 1;
  data2 = mmap (0, 20000, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
  if (data2 == (int *) -1) return 1;
  for (i = 0; i < 20000 / sizeof (int); i++)
    if (data[i] != data2[i])
      return 1;

  close (fd);
  unlink ("conftestmmaps");
  return 0;
}]])


dnl This portion is similar to what AC_FUNC_MMAP does, only it tests shared,
dnl non-fixed mmaps.
AC_DEFUN([INN_FUNC_MMAP],
[AC_CACHE_CHECK(for working mmap, inn_cv_func_mmap,
[AC_TRY_RUN(_INN_FUNC_MMAP_SOURCE(),
            inn_cv_func_mmap=yes,
            inn_cv_func_mmap=no,
            inn_cv_func_mmap=no)])
if test $inn_cv_func_mmap = yes ; then
    AC_DEFINE(HAVE_MMAP)
fi])

-- 
Russ Allbery (address@hidden)             <http://www.eyrie.org/~eagle/>




--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: 26 Oct 2000 06:04:18 -0200
On Oct 25, 2000, Akim Demaille <address@hidden> wrote:

> it is unclear how the test should be performed

IMO, we should deprecate AC_FUNC_MMAP and either create variants that
test for certain features of mmap or just wait for them to show up in
the macro archive and maybe incorporate them.  I don't like the idea
of changing the meaning of an existing macro, though.  Programs that
currently use this macro may rely on the fact that it tests for
MAP_FIXED support, and, if we stop testing that, they'll just break.

Corinna, GCC's aclocal.m4 already has a modified version of this
macro, and so does INN.  Your best bet is probably to pick one of
them, or the modified AC_FUNC_MMAP, and try to get it into the
autoconf macro repository or into the packages of interest.


But there's still one issue: if the current test will return true on
MS-Windows but it shouldn't, we may have to find some way to make it
stricter.  Is that the case?

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  address@hidden, redhat.com}
CS PhD student at IC-Unicamp        address@hidden, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me



--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: Thu, 26 Oct 2000 10:28:20 +0200
Alexandre Oliva wrote:
> 
> On Oct 25, 2000, Akim Demaille <address@hidden> wrote:
> 
> > it is unclear how the test should be performed
> 
> IMO, we should deprecate AC_FUNC_MMAP and either create variants that
> test for certain features of mmap or just wait for them to show up in
> the macro archive and maybe incorporate them.  I don't like the idea
> of changing the meaning of an existing macro, though.  Programs that
> currently use this macro may rely on the fact that it tests for
> MAP_FIXED support, and, if we stop testing that, they'll just break.
> 
> Corinna, GCC's aclocal.m4 already has a modified version of this
> macro, and so does INN.  Your best bet is probably to pick one of
> them, or the modified AC_FUNC_MMAP, and try to get it into the
> autoconf macro repository or into the packages of interest.

This isn't the problem I'm addressing. I'm talking about many
existing packages which check for mmap and they all fail on
Cygwin because of the usage of a depricated functionality. This
is what I think is an error in autoconf _and_ in the packages
which use the MAP_FIXED flag.

> But there's still one issue: if the current test will return true on
> MS-Windows but it shouldn't, we may have to find some way to make it
> stricter.  Is that the case?

No, it's the other way around. It fails due to the usage of MAP_FIXED
so Cygwin is treated as having no mmap while it has.

Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                        mailto:address@hidden
Red Hat, Inc.
mailto:address@hidden



--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: 26 Oct 2000 07:03:51 -0200
On Oct 26, 2000, Corinna Vinschen <address@hidden> wrote:

> This isn't the problem I'm addressing. I'm talking about many
> existing packages which check for mmap and they all fail on
> Cygwin because of the usage of a depricated functionality.

Yep.  The problem is that the macro that is currently in autoconf was
borrowed from a package (GNU grep, IIRC) that actually depended on
MAP_FIXED.  We shouldn't just change its meaning.  We should have
never added such a specific macro with such a generic name.  It should
have always been called AC_FUNC_MMAP_FIXED, or something alike.  Maybe
it's time to rename it and make the original name a deprecated alias
that would be adjusted by autoupdate, so that we can easily add other
alternatives of MMAP later on.

> This is what I think is an error in autoconf _and_ in the packages
> which use the MAP_FIXED flag.

Agreed.  It's just the kind of error we can't just fix, because some
packages depend on its current behavior.

> No, it's the other way around. It fails due to the usage of MAP_FIXED
> so Cygwin is treated as having no mmap while it has.

Yep.  It just doesn't have the kind of mmap the test tests for.  It's
just the test macro that's mis-named, and misleads people into using
it for things other than its original purpose.  What you probably want
is something like GCC's AC_FUNC_MMAP_ANYWHERE and AC_FUNC_MMAP_FILE.
We might just steal those macros as part of the renaming of
AC_FUNC_MMAP.  Here they are, for the record:

# Check whether mmap can map an arbitrary page from /dev/zero or with
# MAP_ANONYMOUS, without MAP_FIXED.
AC_DEFUN([AC_FUNC_MMAP_ANYWHERE],
[AC_CHECK_HEADERS(unistd.h)
AC_CHECK_FUNCS(getpagesize)
AC_CACHE_CHECK(for working mmap which provides zeroed pages anywhere,
  ac_cv_func_mmap_anywhere,
[AC_TRY_RUN([
/* Test by Richard Henderson and Alexandre Oliva.
   Check whether mmap MAP_ANONYMOUS or mmap from /dev/zero works. */
#include <sys/types.h>
#include <fcntl.h>
#include <sys/mman.h>

#if !defined (MAP_ANONYMOUS) && defined (MAP_ANON)
# define MAP_ANONYMOUS MAP_ANON
#endif

/* This mess was copied from the GNU getpagesize.h.  */
#ifndef HAVE_GETPAGESIZE
# ifdef HAVE_UNISTD_H
#  include <unistd.h>
# endif

/* Assume that all systems that can run configure have sys/param.h.  */
# ifndef HAVE_SYS_PARAM_H
#  define HAVE_SYS_PARAM_H 1
# endif

# ifdef _SC_PAGESIZE
#  define getpagesize() sysconf(_SC_PAGESIZE)
# else /* no _SC_PAGESIZE */
#  ifdef HAVE_SYS_PARAM_H
#   include <sys/param.h>
#   ifdef EXEC_PAGESIZE
#    define getpagesize() EXEC_PAGESIZE
#   else /* no EXEC_PAGESIZE */
#    ifdef NBPG
#     define getpagesize() NBPG * CLSIZE
#     ifndef CLSIZE
#      define CLSIZE 1
#     endif /* no CLSIZE */
#    else /* no NBPG */
#     ifdef NBPC
#      define getpagesize() NBPC
#     else /* no NBPC */
#      ifdef PAGESIZE
#       define getpagesize() PAGESIZE
#      endif /* PAGESIZE */
#     endif /* no NBPC */
#    endif /* no NBPG */
#   endif /* no EXEC_PAGESIZE */
#  else /* no HAVE_SYS_PARAM_H */
#   define getpagesize() 8192   /* punt totally */
#  endif /* no HAVE_SYS_PARAM_H */
# endif /* no _SC_PAGESIZE */

#endif /* no HAVE_GETPAGESIZE */

int main()
{
  char *x;
  int fd, pg;

#ifndef MAP_ANONYMOUS
  fd = open("/dev/zero", O_RDWR);
  if (fd < 0)
    exit(1);
#endif

  pg = getpagesize();
#ifdef MAP_ANONYMOUS
  x = (char*)mmap(0, pg, PROT_READ|PROT_WRITE,
                  MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#else
  x = (char*)mmap(0, pg, PROT_READ|PROT_WRITE, MAP_PRIVATE, fd, 0);
#endif
  if (x == (char *) -1)
    exit(2);

  *(int *)x += 1;

  if (munmap(x, pg) < 0)
    exit(3);

  exit(0);
}], ac_cv_func_mmap_anywhere=yes, ac_cv_func_mmap_anywhere=no,
ac_cv_func_mmap_anywhere=no)])
if test $ac_cv_func_mmap_anywhere = yes; then
  AC_DEFINE(HAVE_MMAP_ANYWHERE, 1,
            [Define if mmap can get us zeroed pages without MAP_FIXED.])
fi
])

# Check whether mmap can map a plain file, without MAP_FIXED.
AC_DEFUN([AC_FUNC_MMAP_FILE], 
[AC_REQUIRE([AC_FUNC_MMAP_ANYWHERE])dnl
AC_CACHE_CHECK(for working mmap of a file, ac_cv_func_mmap_file,
[# Create a file one thousand bytes long.
for i in 1 2 3 4 5 6 7 8 9 0
do for j in 1 2 3 4 5 6 7 8 9 0
do echo $i $j xxxxx
done
done > conftestdata$$

AC_TRY_RUN([
/* Test by Zack Weinberg.  Modified from MMAP_ANYWHERE test by
   Richard Henderson and Alexandre Oliva.
   Check whether read-only mmap of a plain file works. */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>

int main()
{
  char *x;
  int fd;
  struct stat st;

  fd = open("conftestdata$$", O_RDONLY);
  if (fd < 0)
    exit(1);

  if (fstat (fd, &st))
    exit(2);

  x = (char*)mmap(0, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
  if (x == (char *) -1)
    exit(3);

  if (x[0] != '1' || x[1] != ' ' || x[2] != '1' || x[3] != ' ')
    exit(4);

  if (munmap(x, st.st_size) < 0)
    exit(5);

  exit(0);
}], ac_cv_func_mmap_file=yes, ac_cv_func_mmap_file=no,
ac_cv_func_mmap_file=no)])
if test $ac_cv_func_mmap_file = yes; then
  AC_DEFINE(HAVE_MMAP_FILE, 1,
            [Define if read-only mmap of a plain file works.])
fi
])

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  address@hidden, redhat.com}
CS PhD student at IC-Unicamp        address@hidden, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me


--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: Thu, 26 Oct 2000 11:26:06 +0200
Alexandre Oliva wrote:
> Yep.  The problem is that the macro that is currently in autoconf was
> borrowed from a package (GNU grep, IIRC) that actually depended on
> MAP_FIXED.  We shouldn't just change its meaning.  We should have
> never added such a specific macro with such a generic name.  It should
> have always been called AC_FUNC_MMAP_FIXED, or something alike.  Maybe
> it's time to rename it and make the original name a deprecated alias
> that would be adjusted by autoupdate, so that we can easily add other
> alternatives of MMAP later on.

Agree. IMO, the usage of that macro should in future autoconf releases
always raise a warning message at least.

> What you probably want
> is something like GCC's AC_FUNC_MMAP_ANYWHERE and AC_FUNC_MMAP_FILE.
> We might just steal those macros as part of the renaming of
> AC_FUNC_MMAP.  Here they are, for the record:
> [...]

I agree. AFAICS, the AC_FUNC_MMAP_ANYWHERE would fail in Cygwin but
that's a Cygwin problem which could be avoided by a change in Cygwin.

Thanks,
Corinna

-- 
Corinna Vinschen                  Please, send mails regarding Cygwin to
Cygwin Developer                        mailto:address@hidden
Red Hat, Inc.
mailto:address@hidden


--- End Message ---
--- Begin Message --- Subject: Re: Check for `mmap' fails on Cygwin/Windows Date: 26 Oct 2000 07:39:12 -0200
On Oct 26, 2000, Corinna Vinschen <address@hidden> wrote:

> Agree. IMO, the usage of that macro should in future autoconf releases
> always raise a warning message at least.

That's what the change I propose would accomplish, I think.  There's
this new macro in autoconf called AU_ALIAS that will not only cause
autoupdate to rename invocations of the macro, but also for autoconf
to warn about its uses.  I'm not sure we warn by default, though.

-- 
Alexandre Oliva   Enjoy Guarana', see http://www.ic.unicamp.br/~oliva/
Red Hat GCC Developer                  address@hidden, redhat.com}
CS PhD student at IC-Unicamp        address@hidden, gnu.org}
Free Software Evangelist    *Please* write to mailing lists, not to me



--- End Message ---

reply via email to

[Prev in Thread] Current Thread [Next in Thread]