c-stack.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Stack overflow handling.
  2. Copyright (C) 2002, 2004, 2006, 2008-2016 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 HAVE_LIBSIGSEGV && SIGSTKSZ < 16384
  42. /* libsigsegv 2.6 through 2.8 have a bug where some architectures use
  43. more than the Linux default of an 8k alternate stack when deciding
  44. if a fault was caused by stack overflow. */
  45. # undef SIGSTKSZ
  46. # define SIGSTKSZ 16384
  47. #endif
  48. #include <stdlib.h>
  49. #include <string.h>
  50. /* Posix 2001 declares ucontext_t in <ucontext.h>, Posix 200x in
  51. <signal.h>. */
  52. #if HAVE_UCONTEXT_H
  53. # include <ucontext.h>
  54. #endif
  55. #include <unistd.h>
  56. #if HAVE_LIBSIGSEGV
  57. # error #include <sigsegv.h>
  58. #endif
  59. #include "c-stack.h"
  60. #include "exitfail.h"
  61. #include "ignore-value.h"
  62. #include "getprogname.h"
  63. #if defined SA_ONSTACK && defined SA_SIGINFO
  64. # define SIGINFO_WORKS 1
  65. #else
  66. # define SIGINFO_WORKS 0
  67. # ifndef SA_ONSTACK
  68. # define SA_ONSTACK 0
  69. # endif
  70. #endif
  71. /* The user-specified action to take when a SEGV-related program error
  72. or stack overflow occurs. */
  73. static void (* volatile segv_action) (int);
  74. /* Translated messages for program errors and stack overflow. Do not
  75. translate them in the signal handler, since gettext is not
  76. async-signal-safe. */
  77. static char const * volatile program_error_message;
  78. static char const * volatile stack_overflow_message;
  79. /* Output an error message, then exit with status EXIT_FAILURE if it
  80. appears to have been a stack overflow, or with a core dump
  81. otherwise. This function is async-signal-safe. */
  82. static _Noreturn void
  83. die (int signo)
  84. {
  85. char const *message;
  86. #if !SIGINFO_WORKS && !HAVE_LIBSIGSEGV
  87. /* We can't easily determine whether it is a stack overflow; so
  88. assume that the rest of our program is perfect (!) and that
  89. this segmentation violation is a stack overflow. */
  90. signo = 0;
  91. #endif /* !SIGINFO_WORKS && !HAVE_LIBSIGSEGV */
  92. segv_action (signo);
  93. message = signo ? program_error_message : stack_overflow_message;
  94. ignore_value (write (STDERR_FILENO, getprogname (), strlen (getprogname ())));
  95. ignore_value (write (STDERR_FILENO, ": ", 2));
  96. ignore_value (write (STDERR_FILENO, message, strlen (message)));
  97. ignore_value (write (STDERR_FILENO, "\n", 1));
  98. if (! signo)
  99. _exit (exit_failure);
  100. raise (signo);
  101. abort ();
  102. }
  103. #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \
  104. && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV
  105. /* Storage for the alternate signal stack. */
  106. static union
  107. {
  108. char buffer[SIGSTKSZ];
  109. /* These other members are for proper alignment. There's no
  110. standard way to guarantee stack alignment, but this seems enough
  111. in practice. */
  112. long double ld;
  113. long l;
  114. void *p;
  115. } alternate_signal_stack;
  116. static void
  117. null_action (int signo __attribute__ ((unused)))
  118. {
  119. }
  120. #endif /* SIGALTSTACK || LIBSIGSEGV */
  121. /* Only use libsigsegv if we need it; platforms like Solaris can
  122. detect stack overflow without the overhead of an external
  123. library. */
  124. #if HAVE_LIBSIGSEGV && ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
  125. /* Nonzero if general segv handler could not be installed. */
  126. static volatile int segv_handler_missing;
  127. /* Handle a segmentation violation and exit if it cannot be stack
  128. overflow. This function is async-signal-safe. */
  129. static int segv_handler (void *address __attribute__ ((unused)),
  130. int serious)
  131. {
  132. # if DEBUG
  133. {
  134. char buf[1024];
  135. sprintf (buf, "segv_handler serious=%d\n", serious);
  136. write (STDERR_FILENO, buf, strlen (buf));
  137. }
  138. # endif
  139. /* If this fault is not serious, return 0 to let the stack overflow
  140. handler take a shot at it. */
  141. if (!serious)
  142. return 0;
  143. die (SIGSEGV);
  144. }
  145. /* Handle a segmentation violation that is likely to be a stack
  146. overflow and exit. This function is async-signal-safe. */
  147. static _Noreturn void
  148. overflow_handler (int emergency,
  149. stackoverflow_context_t context __attribute__ ((unused)))
  150. {
  151. # if DEBUG
  152. {
  153. char buf[1024];
  154. sprintf (buf, "overflow_handler emergency=%d segv_handler_missing=%d\n",
  155. emergency, segv_handler_missing);
  156. write (STDERR_FILENO, buf, strlen (buf));
  157. }
  158. # endif
  159. die ((!emergency || segv_handler_missing) ? 0 : SIGSEGV);
  160. }
  161. int
  162. c_stack_action (void (*action) (int))
  163. {
  164. segv_action = action ? action : null_action;
  165. program_error_message = _("program error");
  166. stack_overflow_message = _("stack overflow");
  167. /* Always install the overflow handler. */
  168. if (stackoverflow_install_handler (overflow_handler,
  169. alternate_signal_stack.buffer,
  170. sizeof alternate_signal_stack.buffer))
  171. {
  172. errno = ENOTSUP;
  173. return -1;
  174. }
  175. /* Try installing a general handler; if it fails, then treat all
  176. segv as stack overflow. */
  177. segv_handler_missing = sigsegv_install_handler (segv_handler);
  178. return 0;
  179. }
  180. #elif HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK && HAVE_STACK_OVERFLOW_HANDLING
  181. # if SIGINFO_WORKS
  182. /* Handle a segmentation violation and exit. This function is
  183. async-signal-safe. */
  184. static _Noreturn void
  185. segv_handler (int signo, siginfo_t *info,
  186. void *context __attribute__ ((unused)))
  187. {
  188. /* Clear SIGNO if it seems to have been a stack overflow. */
  189. # if ! HAVE_XSI_STACK_OVERFLOW_HEURISTIC
  190. /* We can't easily determine whether it is a stack overflow; so
  191. assume that the rest of our program is perfect (!) and that
  192. this segmentation violation is a stack overflow.
  193. Note that although both Linux and Solaris provide
  194. sigaltstack, SA_ONSTACK, and SA_SIGINFO, currently only
  195. Solaris satisfies the XSI heuristic. This is because
  196. Solaris populates uc_stack with the details of the
  197. interrupted stack, while Linux populates it with the details
  198. of the current stack. */
  199. signo = 0;
  200. # else
  201. if (0 < info->si_code)
  202. {
  203. /* If the faulting address is within the stack, or within one
  204. page of the stack, assume that it is a stack overflow. */
  205. ucontext_t const *user_context = context;
  206. char const *stack_base = user_context->uc_stack.ss_sp;
  207. size_t stack_size = user_context->uc_stack.ss_size;
  208. char const *faulting_address = info->si_addr;
  209. size_t page_size = sysconf (_SC_PAGESIZE);
  210. size_t s = faulting_address - stack_base + page_size;
  211. if (s < stack_size + 2 * page_size)
  212. signo = 0;
  213. # if DEBUG
  214. {
  215. char buf[1024];
  216. sprintf (buf,
  217. "segv_handler fault=%p base=%p size=%lx page=%lx signo=%d\n",
  218. faulting_address, stack_base, (unsigned long) stack_size,
  219. (unsigned long) page_size, signo);
  220. write (STDERR_FILENO, buf, strlen (buf));
  221. }
  222. # endif
  223. }
  224. # endif
  225. die (signo);
  226. }
  227. # endif
  228. int
  229. c_stack_action (void (*action) (int))
  230. {
  231. int r;
  232. stack_t st;
  233. struct sigaction act;
  234. st.ss_flags = 0;
  235. # if SIGALTSTACK_SS_REVERSED
  236. /* Irix mistakenly treats ss_sp as the upper bound, rather than
  237. lower bound, of the alternate stack. */
  238. st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ - sizeof (void *);
  239. st.ss_size = sizeof alternate_signal_stack.buffer - sizeof (void *);
  240. # else
  241. st.ss_sp = alternate_signal_stack.buffer;
  242. st.ss_size = sizeof alternate_signal_stack.buffer;
  243. # endif
  244. r = sigaltstack (&st, NULL);
  245. if (r != 0)
  246. return r;
  247. segv_action = action ? action : null_action;
  248. program_error_message = _("program error");
  249. stack_overflow_message = _("stack overflow");
  250. sigemptyset (&act.sa_mask);
  251. # if SIGINFO_WORKS
  252. /* POSIX 1003.1-2001 says SA_RESETHAND implies SA_NODEFER, but
  253. this is not true on Solaris 8 at least. It doesn't hurt to use
  254. SA_NODEFER here, so leave it in. */
  255. act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND | SA_SIGINFO;
  256. act.sa_sigaction = segv_handler;
  257. # else
  258. act.sa_flags = SA_NODEFER | SA_ONSTACK | SA_RESETHAND;
  259. act.sa_handler = die;
  260. # endif
  261. # if FAULT_YIELDS_SIGBUS
  262. if (sigaction (SIGBUS, &act, NULL) < 0)
  263. return -1;
  264. # endif
  265. return sigaction (SIGSEGV, &act, NULL);
  266. }
  267. #else /* ! ((HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK
  268. && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV) */
  269. int
  270. c_stack_action (void (*action) (int) __attribute__ ((unused)))
  271. {
  272. errno = ENOTSUP;
  273. return -1;
  274. }
  275. #endif