2 dnl Copyright (C) 1993-2021 Free Software Foundation, Inc.
3 dnl This file is free software, distributed under the terms of the GNU
4 dnl General Public License as published by the Free Software Foundation;
5 dnl either version 2 of the License, or (at your option) any later version.
6 dnl As a special exception to the GNU General Public License, this file
7 dnl may be distributed as part of a program that contains a configuration
8 dnl script generated by Autoconf, under the same distribution terms as
9 dnl the rest of that program.
11 dnl Test whether mprotect() works.
12 dnl Sets gl_cv_func_mprotect_works and defines HAVE_WORKING_MPROTECT.
14 AC_DEFUN([gl_FUNC_MPROTECT_WORKS],
16 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 AC_REQUIRE([gl_FUNC_MMAP_ANON])
19 AC_CHECK_FUNCS([mprotect])
20 if test $ac_cv_func_mprotect = yes; then
21 AC_CACHE_CHECK([for working mprotect], [gl_cv_func_mprotect_works],
22 [if test $cross_compiling = no; then
24 #include <sys/types.h>
25 /* Declare malloc(). */
27 /* Declare getpagesize(). */
36 int getpagesize (void);
38 /* Declare mprotect(). */
43 unsigned long pagesize = getpagesize ();
44 #define page_align(address) (char*)((unsigned long)(address) & -pagesize)
50 if ((pagesize - 1) & pagesize)
59 mprotect_prog="$mprotect_prog"'
60 char* area = (char*) malloc (6 * pagesize);
61 char* fault_address = area + pagesize*7/2;
63 if test -z "$no_mprotect"; then
69 if (mprotect (page_align (fault_address), pagesize, PROT_NONE) < 0)
71 foo = *fault_address; /* this should cause an exception or signal */
80 if test -z "$no_mprotect"; then
86 if (mprotect (page_align (fault_address), pagesize, PROT_NONE) < 0)
88 *fault_address = 'z'; /* this should cause an exception or signal */
97 if test -z "$no_mprotect"; then
103 if (mprotect (page_align (fault_address), pagesize, PROT_READ) < 0)
105 *fault_address = 'z'; /* this should cause an exception or signal */
114 if test -z "$no_mprotect"; then
120 if (mprotect (page_align (fault_address), pagesize, PROT_READ) < 0)
122 if (mprotect (page_align (fault_address), pagesize, PROT_READ | PROT_WRITE) < 0)
124 *fault_address = 'z'; /* this should not cause an exception or signal */
133 if test -z "$no_mprotect"; then
134 gl_cv_func_mprotect_works=yes
136 gl_cv_func_mprotect_works=no
139 dnl When cross-compiling, assume the known behaviour.
141 dnl Guess yes on Linux systems, glibc systems,
142 dnl macOS, BSD systems, AIX, HP-UX, IRIX, Solaris, Cygwin.
143 linux-* | linux | *-gnu* | gnu* | \
144 darwin* | freebsd* | dragonfly* | netbsd* | openbsd* | \
145 aix* | hpux* | irix* | solaris* | cygwin*)
146 gl_cv_func_mprotect_works="guessing yes" ;;
148 gl_cv_func_mprotect_works="guessing no" ;;
150 dnl If we don't know, obey --enable-cross-guesses.
151 gl_cv_func_mprotect_works="$gl_cross_guess_normal" ;;
155 case "$gl_cv_func_mprotect_works" in
157 AC_DEFINE([HAVE_WORKING_MPROTECT], [1],
158 [have a working mprotect() function])