c-stack.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* Stack overflow handling.
  2. Copyright (C) 2002, 2004, 2006, 2008-2013 Free Software Foundation, Inc.
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  13. /* Written by Paul Eggert. */
  14. /* NOTES:
  15. A program that uses alloca, dynamic arrays, or large local
  16. variables may extend the stack by more than a page at a time. If
  17. so, when the stack overflows the operating system may not detect
  18. the overflow until the program uses the array, and this module may
  19. incorrectly report a program error instead of a stack overflow.
  20. To avoid this problem, allocate only small objects on the stack; a
  21. program should be OK if it limits single allocations to a page or
  22. less. Allocate larger arrays in static storage, or on the heap
  23. (e.g., with malloc). Yes, this is a pain, but we don't know of any
  24. better solution that is portable.
  25. No attempt has been made to deal with multithreaded applications. */
  26. #include <config.h>
  27. #ifndef __attribute__
  28. # if __GNUC__ < 3
  29. # define __attribute__(x)
  30. # endif
  31. #endif
  32. #include "gettext.h"
  33. #define _(msgid) gettext (msgid)
  34. #include <errno.h>
  35. #include <signal.h>
  36. #if ! HAVE_STACK_T && ! defined stack_t
  37. typedef struct sigaltstack stack_t;
  38. #endif
  39. #ifndef SIGSTKSZ
  40. # define SIGSTKSZ 16384
  41. #elif defined __USE_DYNAMIC_STACK_SIZE
  42. /* Redefining SIGSTKSZ here as dynamic stack size is not supported in this version of bison */
  43. # undef SIGSTKSZ
  44. # define SIGSTKSZ 16384
  45. #elif HAVE_LIBSIGSEGV && SIGSTKSZ < 16384
  46. /* libsigsegv 2.6 through 2.8 have a bug where some architectures use
  47. more than the Linux default of an 8k alternate stack when deciding
  48. if a fault was caused by stack overflow. */
  49. # undef SIGSTKSZ
  50. # define SIGSTKSZ 16384
  51. #endif
  52. #include <stdlib.h>
  53. #include <string.h>
  54. /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
  55. <signal.h>. */
  56. #if HAVE_UCONTEXT_H
  57. # include <ucontext.h>
  58. #endif
  59. #include <unistd.h>
  60. #if HAVE_LIBSIGSEGV
  61. # include <sigsegv.h>
  62. #endif
  63. #include "c-stack.h"
  64. #include "exitfail.h"
  65. #include "ignore-value.h"
  66. #if defined SA_ONSTACK && defined SA_SIGINFO
  67. # define SIGINFO_WORKS 1
  68. #else
  69. # define SIGINFO_WORKS 0
  70. # ifndef SA_ONSTACK
  71. # define SA_ONSTACK 0
  72. # endif
  73. #endif
  74. extern char *program_name;
  75. /* The user-specified action to take when a SEGV-related program error
  76. or stack overflow occurs. */
  77. static void (* volatile segv_action) (int);
  78. /* Translated messages for program errors and stack overflow. Do not
  79. translate them in the signal handler, since gettext is not
  80. async-signal-safe. */
  81. static char const * volatile program_error_message;
  82. static char const * volatile stack_overflow_message;
  83. /* Output an error message, then exit with status EXIT_FAILURE if it
  84. appears to have been a stack overflow, or with a core dump
  85. otherwise. This function is async-signal-safe. */
  86. static _Noreturn void
  87. die (int signo)
  88. {
  89. char const *message;
  90. #if !SIGINFO_WORKS && !HAVE_LIBSIGSEGV
  91. /* We can't easily determine whether it is a stack overflow; so
  92. assume that the rest of our program is perfect (!) and that
  93. this segmentation violation is a stack overflow. */
  94. signo = 0;
  95. #endif /* !SIGINFO_WORKS && !HAVE_LIBSIGSEGV */
  96. segv_action (signo);
  97. message = signo ? program_error_message : stack_overflow_message;
  98. ignore_value (write (STDERR_FILENO, program_name, strlen (program_name)));
  99. ignore_value (write (STDERR_FILENO, ": ", 2));
  100. ignore_value (write (STDERR_FILENO, message, strlen (message)));
  101. ignore_value (write (STDERR_FILENO, "\n", 1));
  102. if (! signo)
  103. _exit (exit_failure);
  104. raise (signo);
  105. abort ();
  106. }
  107. #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
  108. && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV
  109. /* Storage for the alternate signal stack. */
  110. static union
  111. {
  112. char buffer[SIGSTKSZ];
  113. /* These other members are for proper alignment. There's no
  114. standard way to guarantee stack alignment, but this seems enough
  115. in practice. */
  116. long double ld;
  117. long l;
  118. void *p;
  119. } alternate_signal_stack;
  120. static void
  121. null_action (int signo __attribute__ ((unused)))
  122. {
  123. }
  124. #endif /* SIGALTSTACK || LIBSIGSEGV */
  125. /* Only use libsigsegv if we need it; platforms like Solaris can
  126. detect stack overflow without the overhead of an external
  127. library. */
  128. #if HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
  129. /* Nonzero if general segv handler could not be installed. */
  130. static volatile int segv_handler_missing;
  131. /* Handle a segmentation violation and exit if it cannot be stack
  132. overflow. This function is async-signal-safe. */
  133. static int segv_handler (void *address __attribute__ ((unused)),
  134. int serious)
  135. {
  136. # if DEBUG
  137. {
  138. char buf[1024];
  139. sprintf (buf, "segv_handler serious=%d\n", serious);
  140. write (STDERR_FILENO, buf, strlen (buf));
  141. }
  142. # endif
  143. /* If this fault is not serious, return 0 to let the stack overflow
  144. handler take a shot at it. */
  145. if (!serious)
  146. return 0;
  147. die (SIGSEGV);
  148. }
  149. /* Handle a segmentation violation that is likely to be a stack
  150. overflow and exit. This function is async-signal-safe. */
  151. static _Noreturn void
  152. overflow_handler (int emergency,
  153. stackoverflow_context_t context __attribute__ ((unused)))
  154. {
  155. # if DEBUG
  156. {
  157. char buf[1024];
  158. sprintf (buf, "overflow_handler emergency=%d segv_handler_missing=%d\n",
  159. emergency, segv_handler_missing);
  160. write (STDERR_FILENO, buf, strlen (buf));
  161. }
  162. # endif
  163. die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
  164. }
  165. int
  166. c_stack_action (void (*action) (int))
  167. {
  168. segv_action = action ? action : null_action;
  169. program_error_message = _("program error");
  170. stack_overflow_message = _("stack overflow");
  171. /* Always install the overflow handler. */
  172. if (stackoverflow_install_handler (overflow_handler,
  173. alternate_signal_stack.buffer,
  174. sizeof alternate_signal_stack.buffer))
  175. {
  176. errno = ENOTSUP;
  177. return -1;
  178. }
  179. /* Try installing a general handler; if it fails, then treat all
  180. segv as stack overflow. */
  181. segv_handler_missing = sigsegv_install_handler (segv_handler);
  182. return 0;
  183. }
  184. #elif HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK && HAVE_STACK_OVERFLOW_HANDLING
  185. # if SIGINFO_WORKS
  186. /* Handle a segmentation violation and exit. This function is
  187. async-signal-safe. */
  188. static _Noreturn void
  189. segv_handler (int signo, siginfo_t *info,
  190. void *context __attribute__ ((unused)))
  191. {
  192. /* Clear SIGNO if it seems to have been a stack overflow. */
  193. # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
  194. /* We can't easily determine whether it is a stack overflow; so
  195. assume that the rest of our program is perfect (!) and that
  196. this segmentation violation is a stack overflow.
  197. Note that although both Linux and Solaris provide
  198. sigaltstack, SA_ONSTACK, and SA_SIGINFO, currently only
  199. Solaris satisfies the XSI heuristic. This is because
  200. Solaris populates uc_stack with the details of the
  201. interrupted stack, while Linux populates it with the details
  202. of the current stack. */
  203. signo = 0;
  204. # else
  205. if (0 < info->si_code)
  206. {
  207. /* If the faulting address is within the stack, or within one
  208. page of the stack, assume that it is a stack overflow. */
  209. ucontext_t const *user_context = context;
  210. char const *stack_base = user_context->uc_stack.ss_sp;
  211. size_t stack_size = user_context->uc_stack.ss_size;
  212. char const *faulting_address = info->si_addr;
  213. size_t page_size = sysconf (_SC_PAGESIZE);
  214. size_t s = faulting_address - stack_base + page_size;
  215. if (s < stack_size + 2 * page_size)
  216. signo = 0;
  217. # if DEBUG
  218. {
  219. char buf[1024];
  220. sprintf (buf,
  221. "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
  222. faulting_address, stack_base, (unsigned long) stack_size,
  223. (unsigned long) page_size, signo);
  224. write (STDERR_FILENO, buf, strlen (buf));
  225. }
  226. # endif
  227. }
  228. # endif
  229. die (signo);
  230. }
  231. # endif
  232. int
  233. c_stack_action (void (*action) (int))
  234. {
  235. int r;
  236. stack_t st;
  237. struct sigaction act;
  238. st.ss_flags = 0;
  239. # if SIGALTSTACK_SS_REVERSED
  240. /* Irix mistakenly treats ss_sp as the upper bound, rather than
  241. lower bound, of the alternate stack. */
  242. st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ - sizeof (void *);
  243. st.ss_size = sizeof alternate_signal_stack.buffer - sizeof (void *);
  244. # else
  245. st.ss_sp = alternate_signal_stack.buffer;
  246. st.ss_size = sizeof alternate_signal_stack.buffer;
  247. # endif
  248. r = sigaltstack (&st, NULL);
  249. if (r != 0)
  250. return r;
  251. segv_action = action ? action : null_action;
  252. program_error_message = _("program error");
  253. stack_overflow_message = _("stack overflow");
  254. sigemptyset (&act.sa_mask);
  255. # if SIGINFO_WORKS
  256. /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
  257. this is not true on Solaris 8 at least. It doesn't hurt to use
  258. SA_NODEFER here, so leave it in. */
  259. act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
  260. act.sa_sigaction = segv_handler;
  261. # else
  262. act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
  263. act.sa_handler = die;
  264. # endif
  265. # if FAULT_YIELDS_SIGBUS
  266. if (sigaction (SIGBUS, &act, NULL) < 0)
  267. return -1;
  268. # endif
  269. return sigaction (SIGSEGV, &act, NULL);
  270. }
  271. #else /* ! ((HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
  272. && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV) */
  273. int
  274. c_stack_action (void (*action) (int) __attribute__ ((unused)))
  275. {
  276. #if (defined _MSC_VER) && (_MSC_VER < 1800)
  277. #else
  278. errno = ENOTSUP;
  279. #endif
  280. return -1;
  281. }
  282. #endif