summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSam James <sam@gentoo.org>2022-02-22 00:35:29 +0000
committerSam James <sam@gentoo.org>2022-02-22 00:41:22 +0000
commit08d38917b110ea52b3107556e283498a09a89a36 (patch)
tree1e22f92cde2672e94382e9d28d83a0c562fc0aa4 /dev-lang/ocaml/files
parentdev-lang/ocaml: fix 4.09 with glibc 2.34 (diff)
downloadgentoo-08d38917b110ea52b3107556e283498a09a89a36.tar.gz
gentoo-08d38917b110ea52b3107556e283498a09a89a36.tar.bz2
gentoo-08d38917b110ea52b3107556e283498a09a89a36.zip
dev-lang/ocaml: fix 4.10 with glibc 2.34
Bug: https://bugs.gentoo.org/804498 See: https://github.com/gentoo/gentoo/pull/22851#pullrequestreview-882504245 Signed-off-by: Sam James <sam@gentoo.org>
Diffstat (limited to 'dev-lang/ocaml/files')
-rw-r--r--dev-lang/ocaml/files/ocaml-4.10.2-glibc-2.34.patch257
1 files changed, 257 insertions, 0 deletions
diff --git a/dev-lang/ocaml/files/ocaml-4.10.2-glibc-2.34.patch b/dev-lang/ocaml/files/ocaml-4.10.2-glibc-2.34.patch
new file mode 100644
index 000000000000..8ce76701366a
--- /dev/null
+++ b/dev-lang/ocaml/files/ocaml-4.10.2-glibc-2.34.patch
@@ -0,0 +1,257 @@
+https://github.com/ocaml/ocaml/commit/4b4c643d1d5d28738f6d900cd902851ed9dc5364
+https://bugs.gentoo.org/804498
+
+From 4b4c643d1d5d28738f6d900cd902851ed9dc5364 Mon Sep 17 00:00:00 2001
+From: Xavier Leroy <xavierleroy@users.noreply.github.com>
+Date: Fri, 5 Mar 2021 19:14:07 +0100
+Subject: [PATCH] Dynamically allocate the alternate signal stack
+
+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
+even for the main thread. It reuses the code already in place to allocate
+the alternate signal stack for other threads.
+
+The alternate signal stack is freed when the main OCaml code / an OCaml thread
+stops.
+
+(partial back-port of PR#10266 and PR#10726)
+---
+ otherlibs/systhreads/st_stubs.c | 2 +
+ runtime/fail_nat.c | 7 ++-
+ runtime/signals_nat.c | 87 ++++++++++++++++++++++++++++-----
+ runtime/startup_nat.c | 7 ++-
+ runtime/sys.c | 5 ++
+ 5 files changed, 95 insertions(+), 13 deletions(-)
+
+diff --git a/otherlibs/systhreads/st_stubs.c b/otherlibs/systhreads/st_stubs.c
+index e46a67be9dc..0b441934ae9 100644
+--- a/otherlibs/systhreads/st_stubs.c
++++ b/otherlibs/systhreads/st_stubs.c
+@@ -140,6 +140,7 @@ static st_retcode caml_threadstatus_wait (value);
+ #ifdef NATIVE_CODE
+ extern struct longjmp_buffer caml_termination_jmpbuf;
+ extern void (*caml_termination_hook)(void);
++extern int caml_stop_stack_overflow_detection(void);
+ #endif
+
+ /* Hook for scanning the stacks of the other threads */
+@@ -576,6 +577,7 @@ static ST_THREAD_FUNCTION caml_thread_start(void * arg)
+ caml_thread_stop();
+ #ifdef NATIVE_CODE
+ }
++ caml_stop_stack_overflow_detection();
+ #endif
+ /* The thread now stops running */
+ return 0;
+diff --git a/runtime/fail_nat.c b/runtime/fail_nat.c
+index 380578ac47b..4ea658684b4 100644
+--- a/runtime/fail_nat.c
++++ b/runtime/fail_nat.c
+@@ -32,6 +32,8 @@
+ #include "caml/roots.h"
+ #include "caml/callback.h"
+
++extern void caml_terminate_signals(void);
++
+ /* The globals holding predefined exceptions */
+
+ typedef value caml_generated_constant[1];
+@@ -62,7 +64,10 @@ CAMLno_asan
+ void caml_raise(value v)
+ {
+ Unlock_exn();
+- if (Caml_state->exception_pointer == NULL) caml_fatal_uncaught_exception(v);
++ if (Caml_state->exception_pointer == NULL) {
++ caml_terminate_signals();
++ caml_fatal_uncaught_exception(v);
++ }
+
+ while (Caml_state->local_roots != NULL &&
+ (char *) Caml_state->local_roots < Caml_state->exception_pointer) {
+diff --git a/runtime/signals_nat.c b/runtime/signals_nat.c
+index 017298394e9..b4c58259fc2 100644
+--- a/runtime/signals_nat.c
++++ b/runtime/signals_nat.c
+@@ -191,8 +191,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
+ #error "CONTEXT_SP is required if HAS_STACK_OVERFLOW_DETECTION is defined"
+ #endif
+
+-static char sig_alt_stack[SIGSTKSZ];
+-
+ /* Code compiled with ocamlopt never accesses more than
+ EXTRA_STACK bytes below the stack pointer. */
+ #define EXTRA_STACK 256
+@@ -254,6 +252,10 @@ DECLARE_SIGNAL_HANDLER(segv_handler)
+
+ /* Initialization of signal stuff */
+
++#ifdef HAS_STACK_OVERFLOW_DETECTION
++static int setup_stack_overflow_detection(void);
++#endif
++
+ void caml_init_signals(void)
+ {
+ /* Bound-check trap handling */
+@@ -278,28 +280,91 @@ void caml_init_signals(void)
+ #endif
+
+ #ifdef HAS_STACK_OVERFLOW_DETECTION
+- {
+- stack_t stk;
++ if (setup_stack_overflow_detection() != -1) {
+ struct sigaction act;
+- stk.ss_sp = sig_alt_stack;
+- stk.ss_size = SIGSTKSZ;
+- stk.ss_flags = 0;
+ SET_SIGACT(act, segv_handler);
+ act.sa_flags |= SA_ONSTACK | SA_NODEFER;
+ sigemptyset(&act.sa_mask);
+- if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
++ sigaction(SIGSEGV, &act, NULL);
+ }
+ #endif
+ }
+
+-void caml_setup_stack_overflow_detection(void)
++/* Termination of signal stuff */
++
++#if defined(TARGET_power) || defined(TARGET_s390x) \
++ || defined(HAS_STACK_OVERFLOW_DETECTION)
++static void set_signal_default(int signum)
++{
++ struct sigaction act;
++ sigemptyset(&act.sa_mask);
++ act.sa_handler = SIG_DFL;
++ act.sa_flags = 0;
++ sigaction(signum, &act, NULL);
++}
++#endif
++
++int caml_stop_stack_overflow_detection(void);
++
++void caml_terminate_signals(void)
+ {
++#if defined(TARGET_power)
++ set_signal_default(SIGTRAP);
++#endif
++
++#if defined(TARGET_s390x)
++ set_signal_default(SIGFPE);
++#endif
++
+ #ifdef HAS_STACK_OVERFLOW_DETECTION
++ set_signal_default(SIGSEGV);
++ caml_stop_stack_overflow_detection();
++#endif
++}
++
++/* 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). */
++
++#ifdef HAS_STACK_OVERFLOW_DETECTION
++static int setup_stack_overflow_detection(void)
++{
+ stack_t stk;
+ stk.ss_sp = malloc(SIGSTKSZ);
++ if (stk.ss_sp == NULL) return -1;
+ stk.ss_size = SIGSTKSZ;
+ stk.ss_flags = 0;
+- if (stk.ss_sp)
+- sigaltstack(&stk, NULL);
++ if (sigaltstack(&stk, NULL) == -1) {
++ free(stk.ss_sp);
++ return -1;
++ }
++ /* Success (or stack overflow detection not available) */
++ return 0;
++}
++#endif
++
++CAMLexport void caml_setup_stack_overflow_detection(void)
++{
++#ifdef HAS_STACK_OVERFLOW_DETECTION
++ setup_stack_overflow_detection();
++#endif
++}
++
++CAMLexport int caml_stop_stack_overflow_detection(void)
++{
++#ifdef HAS_STACK_OVERFLOW_DETECTION
++ stack_t oldstk, stk;
++ stk.ss_flags = SS_DISABLE;
++ if (sigaltstack(&stk, &oldstk) == -1) return -1;
++ /* If caml_setup_stack_overflow_detection failed, we are not using
++ an alternate signal stack. SS_DISABLE will be set in oldstk,
++ and there is nothing to free in this case. */
++ if (! (oldstk.ss_flags & SS_DISABLE)) free(oldstk.ss_sp);
++ return 0;
++#else
++ return 0;
+ #endif
+ }
+diff --git a/runtime/startup_nat.c b/runtime/startup_nat.c
+index 91ff81b3fac..b80d0ffbd5e 100644
+--- a/runtime/startup_nat.c
++++ b/runtime/startup_nat.c
+@@ -93,6 +93,7 @@ void (*caml_termination_hook)(void *) = NULL;
+ extern value caml_start_program (caml_domain_state*);
+ extern void caml_init_ieee_floats (void);
+ extern void caml_init_signals (void);
++extern void caml_terminate_signals(void);
+ #ifdef _WIN32
+ extern void caml_win32_overflow_detection (void);
+ #endif
+@@ -107,6 +108,7 @@ extern void caml_install_invalid_parameter_handler();
+ value caml_startup_common(char_os **argv, int pooling)
+ {
+ char_os * exe_name, * proc_self_exe;
++ value res;
+ char tos;
+
+ /* Initialize the domain */
+@@ -156,10 +158,13 @@ value caml_startup_common(char_os **argv, int pooling)
+ exe_name = caml_search_exe_in_path(exe_name);
+ caml_sys_init(exe_name, argv);
+ if (sigsetjmp(caml_termination_jmpbuf.buf, 0)) {
++ caml_terminate_signals();
+ if (caml_termination_hook != NULL) caml_termination_hook(NULL);
+ return Val_unit;
+ }
+- return caml_start_program(Caml_state);
++ res = caml_start_program(Caml_state);
++ caml_terminate_signals();
++ return res;
+ }
+
+ value caml_startup_exn(char_os **argv)
+diff --git a/runtime/sys.c b/runtime/sys.c
+index d5636199c9f..45f8c8e8100 100644
+--- a/runtime/sys.c
++++ b/runtime/sys.c
+@@ -112,6 +112,8 @@ static void caml_sys_check_path(value name)
+ }
+ }
+
++extern void caml_terminate_signals(void);
++
+ CAMLprim value caml_sys_exit(value retcode_v)
+ {
+ int retcode = Int_val(retcode_v);
+@@ -156,6 +158,9 @@ CAMLprim value caml_sys_exit(value retcode_v)
+ caml_shutdown();
+ #ifdef _WIN32
+ caml_restore_win32_terminal();
++#endif
++#ifdef NATIVE_CODE
++ caml_terminate_signals();
+ #endif
+ exit(retcode);
+ }
+