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