File: mprotect.m4

package info (click to toggle)
ffcall 2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,808 kB
  • sloc: asm: 43,409; ansic: 23,224; sh: 5,383; makefile: 1,517; cpp: 2
file content (152 lines) | stat: -rw-r--r-- 5,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
dnl -*- Autoconf -*-
dnl Copyright (C) 1993-2017 Free Software Foundation, Inc.
dnl This file is free software, distributed under the terms of the GNU
dnl General Public License as published by the Free Software Foundation;
dnl either version 2 of the License, or (at your option) any later version.
dnl As a special exception to the GNU General Public License, this file
dnl may be distributed as part of a program that contains a configuration
dnl script generated by Autoconf, under the same distribution terms as
dnl the rest of that program.

dnl From Bruno Haible, Marcus Daniels, Sam Steingold.

AC_PREREQ([2.57])

AC_DEFUN([FFCALL_MPROTECT],
[
  AC_REQUIRE([CL_GETPAGESIZE])
  AC_REQUIRE([FFCALL_MMAP])
  AC_REQUIRE([AC_CANONICAL_HOST])
  AC_CHECK_FUNCS([mprotect])
  if test $ac_cv_func_mprotect = yes; then
    AC_CACHE_CHECK([for working mprotect], [cl_cv_func_mprotect_works],
      [if test $cross_compiling = no; then
         mprotect_prog='
           #include <sys/types.h>
           /* declare malloc() */
           #include <stdlib.h>
           #ifdef HAVE_UNISTD_H
            #include <unistd.h>
           #endif
           /* declare getpagesize() and mprotect() */
           #include <sys/mman.h>
           #ifndef HAVE_GETPAGESIZE
            #include <sys/param.h>
            #define getpagesize() PAGESIZE
           #else
            ]AC_LANG_EXTERN[
            RETGETPAGESIZETYPE getpagesize (void);
           #endif
           char foo;
           int main ()
           {
             unsigned long pagesize = getpagesize();
           #define page_align(address)  (char*)((unsigned long)(address) & -pagesize)
         '
         AC_TRY_RUN(
           [$mprotect_prog
              if ((pagesize-1) & pagesize) exit(1);
              exit(0);
            }
           ],
           [],
           [no_mprotect=1],
           [dnl When cross-compiling, don't assume anything.
            no_mprotect=1
           ])
         mprotect_prog="$mprotect_prog"'
           char* area = (char*) malloc(6*pagesize);
           char* fault_address = area + pagesize*7/2;
         '
         if test -z "$no_mprotect"; then
           AC_TRY_RUN(GL_NOCRASH
             [$mprotect_prog
                nocrash_init();
                if (mprotect(page_align(fault_address),pagesize,PROT_NONE) < 0)
                  exit(0);
                foo = *fault_address; /* this should cause an exception or signal */
                exit(0);
              }
             ],
             [no_mprotect=1],
             [],
             [dnl When cross-compiling, don't assume anything.
              :
             ])
         fi
         if test -z "$no_mprotect"; then
           AC_TRY_RUN(GL_NOCRASH
             [$mprotect_prog
                nocrash_init();
                if (mprotect(page_align(fault_address),pagesize,PROT_NONE) < 0)
                  exit(0);
                *fault_address = 'z'; /* this should cause an exception or signal */
                exit(0);
              }
             ],
             [no_mprotect=1],
             [],
             [dnl When cross-compiling, don't assume anything.
              :
             ])
         fi
         if test -z "$no_mprotect"; then
           AC_TRY_RUN(GL_NOCRASH
             [$mprotect_prog
                nocrash_init();
                if (mprotect(page_align(fault_address),pagesize,PROT_READ) < 0)
                  exit(0);
                *fault_address = 'z'; /* this should cause an exception or signal */
                exit(0);
              }
             ],
             [no_mprotect=1],
             [],
             [dnl When cross-compiling, don't assume anything.
              :
             ])
         fi
         if test -z "$no_mprotect"; then
           AC_TRY_RUN(GL_NOCRASH
             [$mprotect_prog
                nocrash_init();
                if (mprotect(page_align(fault_address),pagesize,PROT_READ) < 0)
                  exit(1);
                if (mprotect(page_align(fault_address),pagesize,PROT_READ|PROT_WRITE) < 0)
                  exit(1);
                *fault_address = 'z'; /* this should not cause an exception or signal */
                exit(0);
              }
             ],
             [],
             [no_mprotect=1],
             [dnl When cross-compiling, don't assume anything.
              :
             ])
         fi
         if test -z "$no_mprotect"; then
           cl_cv_func_mprotect_works=yes
         else
           cl_cv_func_mprotect_works=no
         fi
       else
         dnl When cross-compiling, assume the known behaviour.
         dnl If we don't know, don't assume anything.
         case "$host_os" in
           aix* | cygwin* | darwin* | hpux* | irix* | linux* | solaris*)
             cl_cv_func_mprotect_works="guessing yes" ;;
           mingw*)
             cl_cv_func_mprotect_works="guessing no" ;;
           *)
             cl_cv_func_mprotect_works="guessing no" ;;
         esac
       fi
      ])
    case "$cl_cv_func_mprotect_works" in
      *yes)
        AC_DEFINE([HAVE_WORKING_MPROTECT], [],
          [have a working mprotect() function])
        ;;
    esac
  fi
])