Skip to content

Commit b7506bb

Browse files
committed
Revert "Cherry-pick of ocaml/ocaml 1eeb0e7fe595f5f9e1ea1edbdf785ff3b49feeeb (#12)"
This reverts commit b819c66.
1 parent 183f688 commit b7506bb

File tree

5 files changed

+13
-95
lines changed

5 files changed

+13
-95
lines changed

otherlibs/systhreads/st_stubs.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,6 @@ 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);
137136
#endif
138137

139138
/* Hook for scanning the stacks of the other threads */
@@ -544,7 +543,6 @@ static ST_THREAD_FUNCTION caml_thread_start(void * arg)
544543
caml_thread_stop();
545544
#ifdef NATIVE_CODE
546545
}
547-
caml_stop_stack_overflow_detection();
548546
#endif
549547
/* The thread now stops running */
550548
return 0;

runtime/fail_nat.c

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

35-
extern void caml_terminate_signals(void);
36-
3735
/* The globals holding predefined exceptions */
3836

3937
typedef value caml_generated_constant[1];
@@ -72,10 +70,7 @@ void caml_raise(value v)
7270
if (Is_exception_result(v))
7371
v = Extract_exception(v);
7472

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

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

runtime/signals_nat.c

Lines changed: 11 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ 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+
177179
/* Code compiled with ocamlopt never accesses more than
178180
EXTRA_STACK bytes below the stack pointer. */
179181
#define EXTRA_STACK 256
@@ -243,10 +245,6 @@ DECLARE_SIGNAL_HANDLER(segv_handler)
243245

244246
/* Initialization of signal stuff */
245247

246-
#ifdef HAS_STACK_OVERFLOW_DETECTION
247-
static int setup_stack_overflow_detection(void);
248-
#endif
249-
250248
void caml_init_signals(void)
251249
{
252250
/* Bound-check trap handling */
@@ -271,91 +269,28 @@ void caml_init_signals(void)
271269
#endif
272270

273271
#ifdef HAS_STACK_OVERFLOW_DETECTION
274-
if (setup_stack_overflow_detection() != -1) {
272+
{
273+
stack_t stk;
275274
struct sigaction act;
275+
stk.ss_sp = sig_alt_stack;
276+
stk.ss_size = SIGSTKSZ;
277+
stk.ss_flags = 0;
276278
SET_SIGACT(act, segv_handler);
277279
act.sa_flags |= SA_ONSTACK | SA_NODEFER;
278280
sigemptyset(&act.sa_mask);
279-
sigaction(SIGSEGV, &act, NULL);
281+
if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
280282
}
281283
#endif
282284
}
283285

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)
289-
{
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)
286+
CAMLexport void caml_setup_stack_overflow_detection(void)
301287
{
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-
310288
#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-
{
326289
stack_t stk;
327290
stk.ss_sp = malloc(SIGSTKSZ);
328-
if (stk.ss_sp == NULL) return -1;
329291
stk.ss_size = SIGSTKSZ;
330292
stk.ss_flags = 0;
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;
293+
if (stk.ss_sp)
294+
sigaltstack(&stk, NULL);
360295
#endif
361296
}

runtime/startup_nat.c

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ 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);
9695
#ifdef _WIN32
9796
extern void caml_win32_overflow_detection (void);
9897
#endif
@@ -107,7 +106,6 @@ extern void caml_install_invalid_parameter_handler();
107106
value caml_startup_common(char_os **argv, int pooling)
108107
{
109108
char_os * exe_name, * proc_self_exe;
110-
value res;
111109
char tos;
112110

113111
/* Initialize the domain */
@@ -154,13 +152,10 @@ value caml_startup_common(char_os **argv, int pooling)
154152
exe_name = caml_search_exe_in_path(exe_name);
155153
caml_sys_init(exe_name, argv);
156154
if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
157-
caml_terminate_signals();
158155
if (caml_termination_hook != NULL) caml_termination_hook(NULL);
159156
return Val_unit;
160157
}
161-
res = caml_start_program(Caml_state);
162-
caml_terminate_signals();
163-
return res;
158+
return caml_start_program(Caml_state);
164159
}
165160

166161
value caml_startup_exn(char_os **argv)

runtime/sys.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ static void caml_sys_check_path(value name)
112112
}
113113
}
114114

115-
extern void caml_terminate_signals(void);
116-
117115
CAMLprim value caml_sys_exit(value retcode_v)
118116
{
119117
int retcode = Int_val(retcode_v);
@@ -161,9 +159,6 @@ CAMLprim value caml_sys_exit(value retcode_v)
161159
caml_shutdown();
162160
#ifdef _WIN32
163161
caml_restore_win32_terminal();
164-
#endif
165-
#ifdef NATIVE_CODE
166-
caml_terminate_signals();
167162
#endif
168163
exit(retcode);
169164
}

0 commit comments

Comments
 (0)