Skip to content

Commit b819c66

Browse files
authored
Cherry-pick of ocaml/ocaml 1eeb0e7fe595f5f9e1ea1edbdf785ff3b49feeeb (ocaml-flambda#12)
1 parent bb363d4 commit b819c66

File tree

5 files changed

+95
-13
lines changed

5 files changed

+95
-13
lines changed

otherlibs/systhreads/st_stubs.c

+2
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ static st_retcode caml_threadstatus_wait (value);
133133
#ifdef NATIVE_CODE
134134
extern struct longjmp_buffer caml_termination_jmpbuf;
135135
extern void (*caml_termination_hook)(void);
136+
extern int caml_stop_stack_overflow_detection(void);
136137
#endif
137138

138139
/* Hook for scanning the stacks of the other threads */
@@ -543,6 +544,7 @@ static ST_THREAD_FUNCTION caml_thread_start(void * arg)
543544
caml_thread_stop();
544545
#ifdef NATIVE_CODE
545546
}
547+
caml_stop_stack_overflow_detection();
546548
#endif
547549
/* The thread now stops running */
548550
return 0;

runtime/fail_nat.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
#include "caml/roots.h"
3333
#include "caml/callback.h"
3434

35+
extern void caml_terminate_signals(void);
36+
3537
/* The globals holding predefined exceptions */
3638

3739
typedef value caml_generated_constant[1];
@@ -70,7 +72,10 @@ void caml_raise(value v)
7072
if (Is_exception_result(v))
7173
v = Extract_exception(v);
7274

73-
if (Caml_state->exception_pointer == NULL) caml_fatal_uncaught_exception(v);
75+
if (Caml_state->exception_pointer == NULL) {
76+
caml_terminate_signals();
77+
caml_fatal_uncaught_exception(v);
78+
}
7479

7580
while (Caml_state->local_roots != NULL &&
7681
(char *) Caml_state->local_roots < Caml_state->exception_pointer) {

runtime/signals_nat.c

+76-11
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
174174
#error "CONTEXT_SP is required if HAS_STACK_OVERFLOW_DETECTION is defined"
175175
#endif
176176

177-
static char sig_alt_stack[SIGSTKSZ];
178-
179177
/* Code compiled with ocamlopt never accesses more than
180178
EXTRA_STACK bytes below the stack pointer. */
181179
#define EXTRA_STACK 256
@@ -245,6 +243,10 @@ DECLARE_SIGNAL_HANDLER(segv_handler)
245243

246244
/* Initialization of signal stuff */
247245

246+
#ifdef HAS_STACK_OVERFLOW_DETECTION
247+
static int setup_stack_overflow_detection(void);
248+
#endif
249+
248250
void caml_init_signals(void)
249251
{
250252
/* Bound-check trap handling */
@@ -269,28 +271,91 @@ void caml_init_signals(void)
269271
#endif
270272

271273
#ifdef HAS_STACK_OVERFLOW_DETECTION
272-
{
273-
stack_t stk;
274+
if (setup_stack_overflow_detection() != -1) {
274275
struct sigaction act;
275-
stk.ss_sp = sig_alt_stack;
276-
stk.ss_size = SIGSTKSZ;
277-
stk.ss_flags = 0;
278276
SET_SIGACT(act, segv_handler);
279277
act.sa_flags |= SA_ONSTACK | SA_NODEFER;
280278
sigemptyset(&act.sa_mask);
281-
if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
279+
sigaction(SIGSEGV, &act, NULL);
282280
}
283281
#endif
284282
}
285283

286-
CAMLexport void caml_setup_stack_overflow_detection(void)
284+
/* Termination of signal stuff */
285+
286+
#if defined(TARGET_power) || defined(TARGET_s390x) \
287+
|| defined(HAS_STACK_OVERFLOW_DETECTION)
288+
static void set_signal_default(int signum)
287289
{
290+
struct sigaction act;
291+
sigemptyset(&act.sa_mask);
292+
act.sa_handler = SIG_DFL;
293+
act.sa_flags = 0;
294+
sigaction(signum, &act, NULL);
295+
}
296+
#endif
297+
298+
int caml_stop_stack_overflow_detection(void);
299+
300+
void caml_terminate_signals(void)
301+
{
302+
#if defined(TARGET_power)
303+
set_signal_default(SIGTRAP);
304+
#endif
305+
306+
#if defined(TARGET_s390x)
307+
set_signal_default(SIGFPE);
308+
#endif
309+
288310
#ifdef HAS_STACK_OVERFLOW_DETECTION
311+
set_signal_default(SIGSEGV);
312+
caml_stop_stack_overflow_detection();
313+
#endif
314+
}
315+
316+
/* Allocate and select an alternate stack for handling signals,
317+
especially SIGSEGV signals.
318+
Each thread needs its own alternate stack.
319+
The alternate stack used to be statically-allocated for the main thread,
320+
but this is incompatible with Glibc 2.34 and newer, where SIGSTKSZ
321+
may not be a compile-time constant (issue #10250). */
322+
323+
#ifdef HAS_STACK_OVERFLOW_DETECTION
324+
static int setup_stack_overflow_detection(void)
325+
{
289326
stack_t stk;
290327
stk.ss_sp = malloc(SIGSTKSZ);
328+
if (stk.ss_sp == NULL) return -1;
291329
stk.ss_size = SIGSTKSZ;
292330
stk.ss_flags = 0;
293-
if (stk.ss_sp)
294-
sigaltstack(&stk, NULL);
331+
if (sigaltstack(&stk, NULL) == -1) {
332+
free(stk.ss_sp);
333+
return -1;
334+
}
335+
/* Success (or stack overflow detection not available) */
336+
return 0;
337+
}
338+
#endif
339+
340+
CAMLexport void caml_setup_stack_overflow_detection(void)
341+
{
342+
#ifdef HAS_STACK_OVERFLOW_DETECTION
343+
setup_stack_overflow_detection();
344+
#endif
345+
}
346+
347+
CAMLexport int caml_stop_stack_overflow_detection(void)
348+
{
349+
#ifdef HAS_STACK_OVERFLOW_DETECTION
350+
stack_t oldstk, stk;
351+
stk.ss_flags = SS_DISABLE;
352+
if (sigaltstack(&stk, &oldstk) == -1) return -1;
353+
/* If caml_setup_stack_overflow_detection failed, we are not using
354+
an alternate signal stack. SS_DISABLE will be set in oldstk,
355+
and there is nothing to free in this case. */
356+
if (! (oldstk.ss_flags & SS_DISABLE)) free(oldstk.ss_sp);
357+
return 0;
358+
#else
359+
return 0;
295360
#endif
296361
}

runtime/startup_nat.c

+6-1
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ void (*caml_termination_hook)(void *) = NULL;
9292

9393
extern value caml_start_program (caml_domain_state*);
9494
extern void caml_init_signals (void);
95+
extern void caml_terminate_signals(void);
9596
#ifdef _WIN32
9697
extern void caml_win32_overflow_detection (void);
9798
#endif
@@ -106,6 +107,7 @@ extern void caml_install_invalid_parameter_handler();
106107
value caml_startup_common(char_os **argv, int pooling)
107108
{
108109
char_os * exe_name, * proc_self_exe;
110+
value res;
109111
char tos;
110112

111113
/* Initialize the domain */
@@ -152,10 +154,13 @@ value caml_startup_common(char_os **argv, int pooling)
152154
exe_name = caml_search_exe_in_path(exe_name);
153155
caml_sys_init(exe_name, argv);
154156
if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
157+
caml_terminate_signals();
155158
if (caml_termination_hook != NULL) caml_termination_hook(NULL);
156159
return Val_unit;
157160
}
158-
return caml_start_program(Caml_state);
161+
res = caml_start_program(Caml_state);
162+
caml_terminate_signals();
163+
return res;
159164
}
160165

161166
value caml_startup_exn(char_os **argv)

runtime/sys.c

+5
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ static void caml_sys_check_path(value name)
112112
}
113113
}
114114

115+
extern void caml_terminate_signals(void);
116+
115117
CAMLprim value caml_sys_exit(value retcode_v)
116118
{
117119
int retcode = Int_val(retcode_v);
@@ -159,6 +161,9 @@ CAMLprim value caml_sys_exit(value retcode_v)
159161
caml_shutdown();
160162
#ifdef _WIN32
161163
caml_restore_win32_terminal();
164+
#endif
165+
#ifdef NATIVE_CODE
166+
caml_terminate_signals();
162167
#endif
163168
exit(retcode);
164169
}

0 commit comments

Comments
 (0)