Skip to content

Commit

Permalink
Fix ocaml compilation in ubuntu 22.04 LTS.
Browse files Browse the repository at this point in the history
Upstream glibc broke ocaml compilation in ubuntu
21 onward. Backport upstream ocaml modification
from ocaml/ocaml#10266
to address the build issue.

Signed-off-by: Karn Jye Lau <karn.jye.lau@intel.com>
  • Loading branch information
kjlau0112 committed Feb 19, 2024
1 parent d487b95 commit 829fe5d
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
@@ -0,0 +1,96 @@
From 68e52932d29f9947c26e04057b631e7b31a05933 Mon Sep 17 00:00:00 2001
From: kjlau0112 <karn.jye.lau@intel.com>
Date: Mon, 11 Dec 2023 09:35:23 +0530
Subject: [PATCH] Update ocaml-native to fix upstream glic build

In Glibc 2.34 and later, SIGSTKSZ may not be a compile-time constant.
SIGSTKSZ may not be a compile-time constant.
It is no longer possible to statically allocate the alternate
signal stack for the main thread.

Upstream-Status:Backport
Reference: https://github.com/ocaml/ocaml/pull/10266

Signed-off-by: kjlau0112 <karn.jye.lau@intel.com>
---
runtime/caml/signals.h | 1 +
runtime/signals_byt.c | 2 ++
runtime/signals_nat.c | 30 +++++++++++++++++++++++-------
3 files changed, 26 insertions(+), 7 deletions(-)

diff --git a/runtime/caml/signals.h b/runtime/caml/signals.h
index 46e65dd..2bb4ca6 100644
--- a/runtime/caml/signals.h
+++ b/runtime/caml/signals.h
@@ -46,6 +46,7 @@ void caml_record_signal(int signal_number);
void caml_process_pending_signals(void);
void caml_process_event(void);
int caml_set_signal_action(int signo, int action);
+CAMLexport int caml_setup_stack_overflow_detection(void) ;

CAMLextern void (*caml_enter_blocking_section_hook)(void);
CAMLextern void (*caml_leave_blocking_section_hook)(void);
diff --git a/runtime/signals_byt.c b/runtime/signals_byt.c
index bdbcf72..a25bc64 100644
--- a/runtime/signals_byt.c
+++ b/runtime/signals_byt.c
@@ -99,3 +99,5 @@ int caml_set_signal_action(int signo, int action)
else
return 0;
}
+
+CAMLexport int caml_setup_stack_overflow_detection(void) { return 0; }
\ No newline at end of file
diff --git a/runtime/signals_nat.c b/runtime/signals_nat.c
index 29a5f49..a3ea159 100644
--- a/runtime/signals_nat.c
+++ b/runtime/signals_nat.c
@@ -182,7 +182,6 @@ DECLARE_SIGNAL_HANDLER(trap_handler)
#ifdef HAS_STACK_OVERFLOW_DETECTION

static char * system_stack_top;
-static char sig_alt_stack[SIGSTKSZ];

#if defined(SYS_linux)
/* PR#4746: recent Linux kernels with support for stack randomization
@@ -272,17 +271,34 @@ void caml_init_signals(void)

/* Stack overflow handling */
#ifdef HAS_STACK_OVERFLOW_DETECTION
+ if (caml_setup_stack_overflow_detection() != -1)
{
- stack_t stk;
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);
- system_stack_top = (char *) &act;
- if (sigaltstack(&stk, NULL) == 0) { sigaction(SIGSEGV, &act, NULL); }
+ sigaction(SIGSEGV, &act, NULL);
}
#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). */
+
+CAMLexport int caml_setup_stack_overflow_detection(void)
+{
+#ifdef HAS_STACK_OVERFLOW_DETECTION
+ stack_t stk;
+ stk.ss_sp = malloc(SIGSTKSZ);
+ if (stk.ss_sp == NULL) return -1;
+ stk.ss_size = SIGSTKSZ;
+ stk.ss_flags = 0;
+ return sigaltstack(&stk, NULL);
+#else
+ return 0;
+#endif
+}
1 change: 1 addition & 0 deletions recipes-devtools/ocaml/ocaml-native_4.08.1.bb
Expand Up @@ -16,6 +16,7 @@ PARALLEL_MAKEINST = ""
SRC_URI += "https://github.com/ocaml/ocaml/archive/4.08.1.tar.gz;sha256sum=b53ed3d487b83fd49bc181bded066ae8e6fb592cf40514261d27d36050d5db85"
SRC_URI += "file://0001-4.08.1-link-error.patch"
SRC_URI += "file://0002-4.08.1-shebang-use-env.patch"
SRC_URI += "file://0001-Update-ocaml-native-to-fix-upstream-glic-build.patch"
S = "${WORKDIR}/ocaml-4.08.1"

do_configure () {
Expand Down

0 comments on commit 829fe5d

Please sign in to comment.