|
| 1 | +From f9e2b20a12a230efa30f1d479563ae07d276a94b Mon Sep 17 00:00:00 2001 |
| 2 | +From: Paul Eggert <eggert@cs.ucla.edu> |
| 3 | +Date: Wed, 30 Sep 2020 13:50:36 -0700 |
| 4 | +Subject: [PATCH] c-stack: stop using SIGSTKSZ |
| 5 | +MIME-Version: 1.0 |
| 6 | +Content-Type: text/plain; charset=utf8 |
| 7 | +Content-Transfer-Encoding: 8bit |
| 8 | + |
| 9 | +It's been proposed to stop making SIGSTKSZ an integer constant: |
| 10 | +https://sourceware.org/pipermail/libc-alpha/2020-September/118028.html |
| 11 | +Also, using SIGSTKSZ in #if did not conform to current POSIX. |
| 12 | +Also, avoiding SIGSTKSZ makes the code simpler and easier to grok. |
| 13 | +* lib/c-stack.c (SIGSTKSZ): Remove. |
| 14 | +(alternate_signal_stack): Now a 64 KiB array, for simplicity. |
| 15 | +All uses changed. |
| 16 | + |
| 17 | +(Note for Ubuntu: The patch originates from gnulib, not m4, and has been |
| 18 | +heavily edited to fit the older version used in our current version of m4) |
| 19 | + |
| 20 | +--- a/lib/c-stack.c |
| 21 | ++++ b/lib/c-stack.c |
| 22 | +@@ -50,15 +50,16 @@ |
| 23 | + #if ! HAVE_STACK_T && ! defined stack_t |
| 24 | + typedef struct sigaltstack stack_t; |
| 25 | + #endif |
| 26 | +-#ifndef SIGSTKSZ |
| 27 | +-# define SIGSTKSZ 16384 |
| 28 | +-#elif HAVE_LIBSIGSEGV && SIGSTKSZ < 16384 |
| 29 | +-/* libsigsegv 2.6 through 2.8 have a bug where some architectures use |
| 30 | +- more than the Linux default of an 8k alternate stack when deciding |
| 31 | +- if a fault was caused by stack overflow. */ |
| 32 | +-# undef SIGSTKSZ |
| 33 | +-# define SIGSTKSZ 16384 |
| 34 | +-#endif |
| 35 | ++/* Storage for the alternate signal stack. |
| 36 | ++ 64 KiB is not too large for Gnulib-using apps, and is large enough |
| 37 | ++ for all known platforms. Smaller sizes may run into trouble. |
| 38 | ++ For example, libsigsegv 2.6 through 2.8 have a bug where some |
| 39 | ++ architectures use more than the Linux default of an 8 KiB alternate |
| 40 | ++ stack when deciding if a fault was caused by stack overflow. */ |
| 41 | ++static max_align_t alternate_signal_stack[(64 * 1024 |
| 42 | ++ + sizeof (max_align_t) - 1) |
| 43 | ++ / sizeof (max_align_t)]; |
| 44 | ++ |
| 45 | + |
| 46 | + #include <stdlib.h> |
| 47 | + #include <string.h> |
| 48 | +@@ -128,18 +129,6 @@ |
| 49 | + #if (HAVE_SIGALTSTACK && HAVE_DECL_SIGALTSTACK \ |
| 50 | + && HAVE_STACK_OVERFLOW_HANDLING) || HAVE_LIBSIGSEGV |
| 51 | + |
| 52 | +-/* Storage for the alternate signal stack. */ |
| 53 | +-static union |
| 54 | +-{ |
| 55 | +- char buffer[SIGSTKSZ]; |
| 56 | +- |
| 57 | +- /* These other members are for proper alignment. There's no |
| 58 | +- standard way to guarantee stack alignment, but this seems enough |
| 59 | +- in practice. */ |
| 60 | +- long double ld; |
| 61 | +- long l; |
| 62 | +- void *p; |
| 63 | +-} alternate_signal_stack; |
| 64 | + |
| 65 | + static void |
| 66 | + null_action (int signo __attribute__ ((unused))) |
| 67 | +@@ -205,8 +194,8 @@ |
| 68 | + |
| 69 | + /* Always install the overflow handler. */ |
| 70 | + if (stackoverflow_install_handler (overflow_handler, |
| 71 | +- alternate_signal_stack.buffer, |
| 72 | +- sizeof alternate_signal_stack.buffer)) |
| 73 | ++ alternate_signal_stack, |
| 74 | ++ sizeof alternate_signal_stack)) |
| 75 | + { |
| 76 | + errno = ENOTSUP; |
| 77 | + return -1; |
| 78 | +@@ -279,14 +268,14 @@ |
| 79 | + stack_t st; |
| 80 | + struct sigaction act; |
| 81 | + st.ss_flags = 0; |
| 82 | ++ st.ss_sp = alternate_signal_stack; |
| 83 | ++ st.ss_size = sizeof alternate_signal_stack; |
| 84 | + # if SIGALTSTACK_SS_REVERSED |
| 85 | + /* Irix mistakenly treats ss_sp as the upper bound, rather than |
| 86 | + lower bound, of the alternate stack. */ |
| 87 | +- st.ss_sp = alternate_signal_stack.buffer + SIGSTKSZ - sizeof (void *); |
| 88 | +- st.ss_size = sizeof alternate_signal_stack.buffer - sizeof (void *); |
| 89 | +-# else |
| 90 | +- st.ss_sp = alternate_signal_stack.buffer; |
| 91 | +- st.ss_size = sizeof alternate_signal_stack.buffer; |
| 92 | ++ st.ss_size -= sizeof (void *); |
| 93 | ++ char *ss_sp = st.ss_sp; |
| 94 | ++ st.ss_sp = ss_sp + st.ss_size; |
| 95 | + # endif |
| 96 | + r = sigaltstack (&st, NULL); |
| 97 | + if (r != 0) |
| 98 | +--- a/lib/c-stack.h |
| 99 | ++++ b/lib/c-stack.h |
| 100 | +@@ -34,7 +34,7 @@ |
| 101 | + A null ACTION acts like an action that does nothing. |
| 102 | + |
| 103 | + ACTION must be async-signal-safe. ACTION together with its callees |
| 104 | +- must not require more than SIGSTKSZ bytes of stack space. Also, |
| 105 | ++ must not require more than 64 KiB of stack space. Also, |
| 106 | + ACTION should not call longjmp, because this implementation does |
| 107 | + not guarantee that it is safe to return to the original stack. |
| 108 | + |
| 109 | + |
0 commit comments