aboutsummaryrefslogtreecommitdiff
path: root/gnu/packages/patches/ocaml-4.09-dynamically-allocate-signal-stack.patch
blob: 2af1582976dd6acc9526490703a000095fe742bd (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
From 95f5016955e519c392c746e38e0c9460f2c1aa0c Mon Sep 17 00:00:00 2001
From: Julien Lepiller <julien@lepiller.eu>
Date: Sun, 19 Feb 2023 09:58:57 +0100
Subject: [PATCH] Dynamically allocate the alternate signal stack.

This patch is a backport of https://github.com/ocaml/ocaml/pull/10266.
---
 runtime/signals_nat.c | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/runtime/signals_nat.c b/runtime/signals_nat.c
index 29a5f49..a193fc2 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
@@ -274,15 +273,19 @@ void caml_init_signals(void)
 #ifdef HAS_STACK_OVERFLOW_DETECTION
   {
     stack_t stk;
-    struct sigaction act;
-    stk.ss_sp = sig_alt_stack;
+    stk.ss_sp = malloc(SIGSTKSZ);
     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); }
+    if (stk.ss_sp != NULL) {
+      if (sigaltstack(&stk, NULL) != -1) {
+        struct sigaction act;
+        SET_SIGACT(act, segv_handler);
+        act.sa_flags |= SA_ONSTACK | SA_NODEFER;
+        sigemptyset(&act.sa_mask);
+        system_stack_top = (char *) &act;
+        sigaction(SIGSEGV, &act, NULL);
+      }
+    }
   }
 #endif
 }
-- 
2.38.1