Skip to content

Commit

Permalink
Dynamically allocate the alternate signal stack (ocaml#10266)
Browse files Browse the repository at this point in the history
In Glibc 2.34 and later, SIGSTKSZ may not be a compile-time constant.
It is no longer possible to statically allocate the alternate signal
stack for the main thread, as we've been doing for the last 25 years.

This commit implements dynamic allocation of the alternate signal stack.

Fixes: ocaml#10250.
  • Loading branch information
xavierleroy authored and dra27 committed Oct 25, 2021
1 parent 59d268b commit 631dbbe
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 9 additions & 2 deletions runtime/signals_nat.c
Expand Up @@ -182,7 +182,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
#ifdef HAS_STACK_OVERFLOW_DETECTION

static char * system_stack_top;
static char sig_alt_stack[SIGSTKSZ];

#if defined(SYS_linux)
/* PR#4746: recent Linux kernels with support for stack randomization
Expand Down Expand Up @@ -275,7 +274,15 @@ void caml_init_signals(void)
{
stack_t stk;
struct sigaction act;
stk.ss_sp = sig_alt_stack;
stk.ss_sp = malloc(SIGSTKSZ);
/* Allocate and select an alternate stack for handling signals,
especially SIGSEGV signals.
Each thread needs its own alternate stack.
The alternate stack used to be statically-allocated for the main thread,
but this is incompatible with Glibc 2.34 and newer, where SIGSTKSZ
may not be a compile-time constant (issue #10250). */
if (stk.ss_sp == NULL)
return;
stk.ss_size = SIGSTKSZ;
stk.ss_flags = 0;
SET_SIGACT(act, segv_handler);
Expand Down
2 changes: 2 additions & 0 deletions tools/ci/inria/lsan-suppr.txt
@@ -1,2 +1,4 @@
# ocamlyacc doesn't clean memory on exit
leak:ocamlyacc
# Alternate signal stacks are currently never freed (see #10266)
leak:caml_setup_stack_overflow_detection

0 comments on commit 631dbbe

Please sign in to comment.