1 From 632563b19ca72ec0ae10c7ed767a025c342d3155 Mon Sep 17 00:00:00 2001
2 From: Xavier Leroy <xavier.leroy@college-de-france.fr>
3 Date: Tue, 2 Mar 2021 19:06:56 +0100
4 Subject: [PATCH 1/3] Dynamically allocate the alternate signal stack
6 In Glibc 2.34 and later, SIGSTKSZ may not be a compile-time constant.
7 It is no longer possible to statically allocate the alternate signal
8 stack for the main thread, as we've been doing for the last 25 years.
10 This commit implements dynamic allocation of the alternate signal stack
11 even for the main thread. It reuses the code already in place to allocate
12 the alternate signal stack for other threads.
16 runtime/caml/signals.h | 2 +-
17 runtime/signals_byt.c | 2 +-
18 runtime/signals_nat.c | 25 ++++++++++++++-----------
19 3 files changed, 16 insertions(+), 13 deletions(-)
21 diff --git a/runtime/caml/signals.h b/runtime/caml/signals.h
22 index 3ff152c2693..285dbd7febd 100644
23 --- a/runtime/caml/signals.h
24 +++ b/runtime/caml/signals.h
25 @@ -87,7 +87,7 @@ value caml_do_pending_actions_exn (void);
26 value caml_process_pending_actions_with_root (value extra_root); // raises
27 value caml_process_pending_actions_with_root_exn (value extra_root);
28 int caml_set_signal_action(int signo, int action);
29 -CAMLextern void caml_setup_stack_overflow_detection(void);
30 +CAMLextern int caml_setup_stack_overflow_detection(void);
32 CAMLextern void (*caml_enter_blocking_section_hook)(void);
33 CAMLextern void (*caml_leave_blocking_section_hook)(void);
34 diff --git a/runtime/signals_byt.c b/runtime/signals_byt.c
35 index 2183142da18..38eb5e3a47a 100644
36 --- a/runtime/signals_byt.c
37 +++ b/runtime/signals_byt.c
38 @@ -81,4 +81,4 @@ int caml_set_signal_action(int signo, int action)
42 -CAMLexport void caml_setup_stack_overflow_detection(void) {}
43 +CAMLexport int caml_setup_stack_overflow_detection(void) { return 0; }
44 diff --git a/runtime/signals_nat.c b/runtime/signals_nat.c
45 index 1be1b45d420..47b3e29af70 100644
46 --- a/runtime/signals_nat.c
47 +++ b/runtime/signals_nat.c
48 @@ -174,8 +174,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
49 #error "CONTEXT_SP is required if HAS_STACK_OVERFLOW_DETECTION is defined"
52 -static char sig_alt_stack[SIGSTKSZ];
54 /* Code compiled with ocamlopt never accesses more than
55 EXTRA_STACK bytes below the stack pointer. */
56 #define EXTRA_STACK 256
57 @@ -269,28 +267,33 @@ void caml_init_signals(void)
60 #ifdef HAS_STACK_OVERFLOW_DETECTION
63 + if (caml_setup_stack_overflow_detection() != -1) {
65 - stk.ss_sp = sig_alt_stack;
66 - stk.ss_size = SIGSTKSZ;
68 SET_SIGACT(act, segv_handler);
69 act.sa_flags |= SA_ONSTACK | SA_NODEFER;
70 sigemptyset(&act.sa_mask);
71 - if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
72 + sigaction(SIGSEGV, &act, NULL);
77 -CAMLexport void caml_setup_stack_overflow_detection(void)
78 +/* Allocate and select an alternate stack for handling signals,
79 + especially SIGSEGV signals.
80 + Each thread needs its own alternate stack.
81 + The alternate stack used to be statically-allocated for the main thread,
82 + but this is incompatible with Glibc 2.34 and never, where SIGSTKSZ
83 + may not be a compile-time constant (issue #10250). */
85 +CAMLexport int caml_setup_stack_overflow_detection(void)
87 #ifdef HAS_STACK_OVERFLOW_DETECTION
89 stk.ss_sp = malloc(SIGSTKSZ);
90 + if (stk.ss_sp == NULL) return -1;
91 stk.ss_size = SIGSTKSZ;
94 - sigaltstack(&stk, NULL);
95 + return sigaltstack(&stk, NULL);
101 From 9f5b312d457490032e21d54baa85774e78dcd6c9 Mon Sep 17 00:00:00 2001
102 From: Xavier Leroy <xavier.leroy@college-de-france.fr>
103 Date: Wed, 3 Mar 2021 17:57:25 +0100
104 Subject: [PATCH 2/3] Typo in comment
107 runtime/signals_nat.c | 2 +-
108 1 file changed, 1 insertion(+), 1 deletion(-)
110 diff --git a/runtime/signals_nat.c b/runtime/signals_nat.c
111 index 47b3e29af70..ca86956783c 100644
112 --- a/runtime/signals_nat.c
113 +++ b/runtime/signals_nat.c
114 @@ -281,7 +281,7 @@ void caml_init_signals(void)
115 especially SIGSEGV signals.
116 Each thread needs its own alternate stack.
117 The alternate stack used to be statically-allocated for the main thread,
118 - but this is incompatible with Glibc 2.34 and never, where SIGSTKSZ
119 + but this is incompatible with Glibc 2.34 and newer, where SIGSTKSZ
120 may not be a compile-time constant (issue #10250). */
122 CAMLexport int caml_setup_stack_overflow_detection(void)
124 From 1a6cc6022e35b5a5e8e3b97586e78b39d78df51a Mon Sep 17 00:00:00 2001
125 From: Xavier Leroy <xavier.leroy@college-de-france.fr>
126 Date: Fri, 5 Mar 2021 19:10:11 +0100
127 Subject: [PATCH 3/3] Update Changes
131 1 file changed, 5 insertions(+)
133 diff --git a/Changes b/Changes
134 index 1bb07e81d09..9c73ad02e32 100644
137 @@ -69,6 +69,11 @@ Working version
138 to the debugger via a socket.
139 (Antonin Décimo, review by Xavier Leroy)
141 +- #10250, #10266: Dynamically allocate alternate signal stacks to
142 + accommodate changes in Glibc 2.34.
143 + (Xavier Leroy, reports by Tomasz Kłoczko and R.W.M. Jones, review by Anil
144 + Madhavapeddy, Stephen Dolan, and Florian Angeletti)
146 ### Code generation and optimizations:
148 - #9876: do not cache the young_limit GC variable in a processor register.