commit:     08d38917b110ea52b3107556e283498a09a89a36
Author:     Sam James <sam <AT> gentoo <DOT> org>
AuthorDate: Tue Feb 22 00:35:29 2022 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Tue Feb 22 00:41:22 2022 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=08d38917

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 <AT> gentoo.org>

 dev-lang/ocaml/files/ocaml-4.10.2-glibc-2.34.patch | 257 +++++++++++++++++++++
 dev-lang/ocaml/ocaml-4.10.2-r3.ebuild              | 106 +++++++++
 2 files changed, 363 insertions(+)

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 <xavierle...@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);
+ }
+

diff --git a/dev-lang/ocaml/ocaml-4.10.2-r3.ebuild 
b/dev-lang/ocaml/ocaml-4.10.2-r3.ebuild
new file mode 100644
index 000000000000..b298bea0d51b
--- /dev/null
+++ b/dev-lang/ocaml/ocaml-4.10.2-r3.ebuild
@@ -0,0 +1,106 @@
+# Copyright 1999-2022 Gentoo Authors
+# Distributed under the terms of the GNU General Public License v2
+
+EAPI=7
+
+inherit flag-o-matic
+
+HOMEPAGE="https://ocaml.org/";
+SRC_URI="https://github.com/ocaml/ocaml/archive/${PV}.tar.gz -> ${P}.tar.gz"
+DESCRIPTION="Programming language supporting functional, imperative & 
object-oriented styles"
+
+LICENSE="LGPL-2.1"
+SLOT="0/$(ver_cut 1-2)"
+KEYWORDS="~alpha ~amd64 ~arm ~arm64 ~hppa ~ia64 ~mips ~ppc ~ppc64 ~x86 
~amd64-linux ~x86-linux ~ppc-macos ~sparc-solaris ~x86-solaris"
+IUSE="emacs flambda latex +ocamlopt spacetime xemacs"
+
+RDEPEND="sys-libs/binutils-libs:=
+       spacetime? ( sys-libs/libunwind:= )"
+BDEPEND="${RDEPEND}
+       virtual/pkgconfig"
+PDEPEND="emacs? ( app-emacs/ocaml-mode )
+       xemacs? ( app-xemacs/ocaml )"
+
+QA_FLAGS_IGNORED='usr/lib.*/ocaml/bigarray.cmxs'
+
+PATCHES=(
+       "${FILESDIR}"/${P}-cflags.patch
+       "${FILESDIR}"/${P}-glibc-2.34.patch
+)
+
+src_prepare() {
+       default
+
+       cp "${FILESDIR}"/ocaml.conf "${T}" || die
+
+       # Broken until 4.12
+       # bug #818445
+       filter-flags '-flto*'
+       append-flags -fno-strict-aliasing
+
+       # OCaml generates textrels on 32-bit arches
+       # We can't do anything about it, but disabling it means that tests
+       # for OCaml-based packages won't fail on unexpected output
+       # bug #773226
+       if use arm || use ppc || use x86 ; then
+               append-ldflags "-Wl,-z,notext"
+       fi
+
+       # Upstream build ignores LDFLAGS in several places.
+       sed -i -e 's/\(^MKDLL=.*\)/\1 $(LDFLAGS)/' \
+               -e 's/\(^OC_CFLAGS=.*\)/\1 $(LDFLAGS)/' \
+               -e 's/\(^OC_LDFLAGS=.*\)/\1 $(LDFLAGS)/' \
+               Makefile.config.in || die "LDFLAGS fix failed"
+       # ${P} overrides upstream build's own P due to a wrong assignment 
operator.
+       sed -i -e 's/^P ?=/P =/' stdlib/StdlibModules || die "P fix failed"
+}
+
+src_configure() {
+       local opt=(
+               --bindir="${EPREFIX}/usr/bin"
+               --libdir="${EPREFIX}/usr/$(get_libdir)/ocaml"
+               --mandir="${EPREFIX}/usr/share/man"
+               --prefix="${EPREFIX}/usr"
+               $(use_enable flambda)
+               $(use_enable spacetime)
+       )
+       econf ${opt[@]}
+}
+
+src_compile() {
+       if use ocamlopt ; then
+               emake world.opt
+       else
+               emake world
+       fi
+}
+
+src_test() {
+       if use ocamlopt ; then
+               # OCaml tests only work when run sequentially
+               emake -j1 -C testsuite all
+       else
+               ewarn "${PN} was built without 'ocamlopt' USE flag; skipping 
tests."
+       fi
+}
+
+src_install() {
+       default
+       dodir /usr/include
+
+       # Create symlink for header files
+       dosym "../$(get_libdir)/ocaml/caml" /usr/include/caml
+       dodoc Changes README.adoc
+
+       # Create envd entry for latex input files
+       if use latex ; then
+               echo 
"TEXINPUTS=\"${EPREFIX}/usr/$(get_libdir)/ocaml/ocamldoc:\"" > 
"${T}/99ocamldoc" || die
+               doenvd "${T}"/99ocamldoc
+       fi
+
+       sed -i -e "s:lib:$(get_libdir):" "${T}"/ocaml.conf || die
+
+       # Install ocaml-rebuild portage set
+       insinto /usr/share/portage/config/sets
+       doins "${T}"/ocaml.conf
+}

Reply via email to