Debian Codegraph @

m4/sigaltstack.m4

raw
1 # sigaltstack.m4
2 # serial 16
3 dnl Copyright (C) 2002-2025 Free Software Foundation, Inc.
4 dnl This file is free software, distributed under the terms of the GNU
5 dnl General Public License as published by the Free Software Foundation;
6 dnl either version 2 of the License, or (at your option) any later version.
7 dnl As a special exception to the GNU General Public License, this file
8 dnl may be distributed as part of a program that contains a configuration
9 dnl script generated by Autoconf, under the same distribution terms as
10 dnl the rest of that program.
11
12 dnl Written by Bruno Haible and Eric Blake.
13
14 AC_DEFUN([SV_SIGALTSTACK],
15 [
16 AC_REQUIRE([AC_PROG_CC])
17 AC_REQUIRE([AC_CANONICAL_HOST])
18
19 AC_CHECK_FUNCS_ONCE([sigaltstack setrlimit])
20
21 if test "$ac_cv_func_sigaltstack" = yes; then
22 AC_CHECK_TYPE([stack_t], ,
23 [AC_DEFINE(stack_t, [struct sigaltstack],
24 [Define to 'struct sigaltstack' if that's the type of the argument to sigaltstack])
25 ],
26 [
27 #include <signal.h>
28 #if HAVE_SYS_SIGNAL_H
29 # include <sys/signal.h>
30 #endif
31 ])
32 fi
33
34 AC_CACHE_CHECK([for working sigaltstack], [sv_cv_sigaltstack], [
35 if test "$ac_cv_func_sigaltstack" = yes; then
36 case "$host_os" in
37 macos* | darwin[[6-9]]* | darwin[[1-9]][[0-9]]*)
38 # On MacOS X 10.2 or newer, just assume that if it compiles, it will
39 # work. If we were to perform the real test, 1 Crash Report dialog
40 # window would pop up.
41 AC_LINK_IFELSE([
42 AC_LANG_PROGRAM([[#include <signal.h>]],
43 [[int x = SA_ONSTACK; stack_t ss; sigaltstack ((stack_t*)0, &ss);]])],
44 [sv_cv_sigaltstack="guessing yes"],
45 [sv_cv_sigaltstack=no])
46 ;;
47 *)
48 AC_RUN_IFELSE([
49 AC_LANG_SOURCE([[
50 #include <stdlib.h>
51 #include <signal.h>
52 #if HAVE_SYS_SIGNAL_H
53 # include <sys/signal.h>
54 #endif
55 #if HAVE_SETRLIMIT
56 # include <sys/types.h>
57 # include <sys/time.h>
58 # include <sys/resource.h>
59 #endif
60 void stackoverflow_handler (int sig)
61 {
62 /* If we get here, the stack overflow was caught. */
63 exit (0);
64 }
65 volatile int * recurse_1 (volatile int n, volatile int *p)
66 {
67 if (n >= 0)
68 *recurse_1 (n + 1, p) += n;
69 return p;
70 }
71 int recurse (volatile int n)
72 {
73 int sum = 0;
74 return *recurse_1 (n, &sum);
75 }
76 char mystack[2 * (1 << 24)];
77 int main ()
78 {
79 stack_t altstack;
80 struct sigaction action;
81 #if defined HAVE_SETRLIMIT && defined RLIMIT_STACK
82 /* Before starting the endless recursion, try to be friendly to the user's
83 machine. On some Linux 2.2.x systems, there is no stack limit for user
84 processes at all. We don't want to kill such systems. */
85 struct rlimit rl;
86 rl.rlim_cur = rl.rlim_max = 0x100000; /* 1 MB */
87 setrlimit (RLIMIT_STACK, &rl);
88 #endif
89 /* Install the alternate stack. Use the midpoint of mystack, to guard
90 against a buggy interpretation of ss_sp on IRIX. */
91 #ifdef SIGSTKSZ
92 if (sizeof mystack / 2 < SIGSTKSZ)
93 exit (3);
94 #endif
95 altstack.ss_sp = mystack + sizeof mystack / 2;
96 altstack.ss_size = sizeof mystack / 2;
97 altstack.ss_flags = 0; /* no SS_DISABLE */
98 if (sigaltstack (&altstack, NULL) < 0)
99 exit (1);
100 /* Install the SIGSEGV handler. */
101 sigemptyset (&action.sa_mask);
102 action.sa_handler = &stackoverflow_handler;
103 action.sa_flags = SA_ONSTACK;
104 sigaction (SIGSEGV, &action, (struct sigaction *) NULL);
105 sigaction (SIGBUS, &action, (struct sigaction *) NULL);
106 /* Provoke a stack overflow. */
107 recurse (0);
108 exit (2);
109 }]])],
110 [sv_cv_sigaltstack=yes],
111 [sv_cv_sigaltstack=no],
112 [
113 dnl FIXME: Put in some more known values here.
114 case "$host_os" in
115 *)
116 AC_LINK_IFELSE([
117 AC_LANG_PROGRAM([[#include <signal.h>]],
118 [[int x = SA_ONSTACK; stack_t ss; sigaltstack ((stack_t*)0, &ss);]])],
119 [sv_cv_sigaltstack="guessing yes"],
120 [sv_cv_sigaltstack=no])
121 ;;
122 esac
123 ])
124 ;;
125 esac
126 else
127 sv_cv_sigaltstack=no
128 fi
129 ])
130 if test "$sv_cv_sigaltstack" != no; then
131 AC_DEFINE([HAVE_WORKING_SIGALTSTACK], [1],
132 [Define if you have the sigaltstack() function and it works.])
133
134 dnl The ss_sp field of a stack_t is, according to POSIX, the lowest address
135 dnl of the memory block designated as an alternate stack. But IRIX 5.3
136 dnl interprets it as the highest address!
137 AC_CACHE_CHECK([for correct stack_t interpretation],
138 [sv_cv_sigaltstack_low_base], [
139 AC_RUN_IFELSE([
140 AC_LANG_SOURCE([[
141 #include <stdlib.h>
142 #include <signal.h>
143 #if HAVE_SYS_SIGNAL_H
144 # include <sys/signal.h>
145 #endif
146 volatile char *stack_lower_bound;
147 volatile char *stack_upper_bound;
148 static void check_stack_location (volatile char *addr)
149 {
150 if (addr >= stack_lower_bound && addr <= stack_upper_bound)
151 exit (0);
152 else
153 exit (1);
154 }
155 static void stackoverflow_handler (int sig)
156 {
157 char dummy;
158 check_stack_location (&dummy);
159 }
160 char mystack[2 * (1 << 24)];
161 int main ()
162 {
163 stack_t altstack;
164 struct sigaction action;
165 /* Install the alternate stack. */
166 altstack.ss_sp = mystack + sizeof mystack / 2;
167 altstack.ss_size = sizeof mystack / 2;
168 stack_lower_bound = (char *) altstack.ss_sp;
169 stack_upper_bound = (char *) altstack.ss_sp + altstack.ss_size - 1;
170 altstack.ss_flags = 0; /* no SS_DISABLE */
171 if (sigaltstack (&altstack, NULL) < 0)
172 exit (2);
173 /* Install the SIGSEGV handler. */
174 sigemptyset (&action.sa_mask);
175 action.sa_handler = &stackoverflow_handler;
176 action.sa_flags = SA_ONSTACK;
177 if (sigaction (SIGSEGV, &action, (struct sigaction *) NULL) < 0)
178 exit(3);
179 /* Provoke a SIGSEGV. */
180 raise (SIGSEGV);
181 exit (3);
182 }]])],
183 [sv_cv_sigaltstack_low_base=yes],
184 [sv_cv_sigaltstack_low_base=no],
185 [
186 dnl FIXME: Put in some more known values here.
187 case "$host_os" in
188 irix5*) sv_cv_sigaltstack_low_base="no" ;;
189 *) sv_cv_sigaltstack_low_base="guessing yes" ;;
190 esac
191 ])
192 ])
193 if test "$sv_cv_sigaltstack_low_base" = no; then
194 AC_DEFINE([SIGALTSTACK_SS_REVERSED], [1],
195 [Define if sigaltstack() interprets the stack_t.ss_sp field incorrectly,
196 as the highest address of the alternate stack range rather than as the
197 lowest address.])
198 fi
199 fi
200 ])