Re: AC_FUNC_MMAP lacks <unistd.h> include?

Paul Eggert <[email protected]>
Newsgroups gmane.comp.sysutils.autoconf.bugs
Organization UCLA Computer Science Department
Message-ID <[email protected]>
On 2023-05-10 21:06, Matt Turner wrote:

> We're mmap'ing a 1-byte file, and since the pagesize on amd64 is 4096
> bytes, only the first 4096 byte page (of two) is mapped. Accessing the
> second page triggers the SIGBUS.

Ouch, I misread that part of the test. Thanks for reporting that. I 
suppose we should go back to invoking getpagesize despite the hassle of 
configuring it. I installed the attached further patch; please give it a 
try.
0001-Fix-port-of-AC_FUNC_MMAP.patch (text/x-patch, 2.8 KB)
From 33c26d2700f927432c756ccf7a4fc89403d35b95 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Wed, 10 May 2023 22:57:27 -0700
Subject: [PATCH] Fix port of AC_FUNC_MMAP
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Problem reported by Matt Turner in:
https://lists.gnu.org/r/bug-autoconf/2023-05/msg00005.html
* lib/autoconf/functions.m4 (AC_FUNC_MMAP): Go back to getting the
page size, since the zero-fill test needs this after all.
However, prefer sysconf (_SC_PAGESIZE) or sysconf (_SC_PAGE_SIZE)
to getpagesize (), and use ‘long’ not ‘int’ to store the page size.
Also, declare getpagesize if it is used as a function.
---
 lib/autoconf/functions.m4 | 41 ++++++++++++++++++++++++++++++++++-----
 1 file changed, 36 insertions(+), 5 deletions(-)

diff --git a/lib/autoconf/functions.m4 b/lib/autoconf/functions.m4
index 5a0f0185..3d6e4aca 100644
--- a/lib/autoconf/functions.m4
+++ b/lib/autoconf/functions.m4
@@ -1283,7 +1283,6 @@ AU_ALIAS([AM_FUNC_MKTIME], [AC_FUNC_MKTIME])
 AN_FUNCTION([mmap], [AC_FUNC_MMAP])
 AC_DEFUN([AC_FUNC_MMAP],
 [AC_REQUIRE([AC_CANONICAL_HOST])dnl for cross-compiles
-dnl FIXME: Remove the unnecessary checks for unistd.h, sys/param.h, getpagesize.
 AC_CHECK_HEADERS_ONCE([unistd.h sys/param.h])
 AC_CHECK_FUNCS_ONCE([getpagesize])
 AC_CACHE_CHECK([for working mmap], [ac_cv_func_mmap_fixed_mapped],
@@ -1311,17 +1310,49 @@ AC_CACHE_CHECK([for working mmap], [ac_cv_func_mmap_fixed_mapped],
 #include <fcntl.h>
 #include <sys/mman.h>
 
+#ifndef getpagesize
+# ifdef _SC_PAGESIZE
+#  define getpagesize() sysconf (_SC_PAGESIZE)
+# elif defined _SC_PAGE_SIZE
+#  define getpagesize() sysconf (_SC_PAGE_SIZE)
+# elif HAVE_GETPAGESIZE
+int getpagesize ();
+# else
+#  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
+#endif
+
 int
 main (void)
 {
   char *data, *data2, *data3;
   const char *cdata2;
-  int i, pagesize;
+  long i, pagesize;
   int fd, fd2;
 
-  /* The "page size" need not equal the system page size,
-     and need not even be a power of 2.  */
-  pagesize = 8192;
+  pagesize = getpagesize ();
 
   /* First, make a file with some known garbage in it. */
   data = (char *) malloc (pagesize);
-- 
2.39.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.