|
| 1 | +From 2cd56ff98bdb291b9b5e2d0e3fd4cd315076c904 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Natanael Copa < [email protected]> |
| 3 | +Date: Wed, 14 Mar 2018 14:14:11 +0100 |
| 4 | +Subject: [PATCH] thread_pthread.c: make get_main_stack portable on linux |
| 5 | + |
| 6 | +We can not rely on the calculated stack size from pthread_getattr_np() |
| 7 | +for the main thread, because the way this is calculated may differ |
| 8 | +depending on implementation. The _np means non-portable. |
| 9 | + |
| 10 | +So we test if it is a known implementation (__GLIBC__) and fallback to a |
| 11 | +portable way on Linux by parsing /proc/self/maps to get the stack for |
| 12 | +main thread. We also add a 100MB safety limit so we don't reserve_stack |
| 13 | +with an insane value. |
| 14 | + |
| 15 | +Credit goes to Szabolcs Nagy for this code. |
| 16 | +--- |
| 17 | + thread_pthread.c | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- |
| 18 | + 1 file changed, 49 insertions(+), 3 deletions(-) |
| 19 | + |
| 20 | +diff --git a/thread_pthread.c b/thread_pthread.c |
| 21 | +index 951885ffa0..cf90321d1d 100644 |
| 22 | +--- a/thread_pthread.c |
| 23 | ++++ b/thread_pthread.c |
| 24 | +@@ -530,9 +530,6 @@ hpux_attr_getstackaddr(const pthread_attr_t *attr, void **addr) |
| 25 | + # define MAINSTACKADDR_AVAILABLE 0 |
| 26 | + # endif |
| 27 | + #endif |
| 28 | +-#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack) |
| 29 | +-# define get_main_stack(addr, size) get_stack(addr, size) |
| 30 | +-#endif |
| 31 | + |
| 32 | + #ifdef STACKADDR_AVAILABLE |
| 33 | + /* |
| 34 | +@@ -614,6 +611,55 @@ get_stack(void **addr, size_t *size) |
| 35 | + return 0; |
| 36 | + #undef CHECK_ERR |
| 37 | + } |
| 38 | ++ |
| 39 | ++#if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT) |
| 40 | ++ |
| 41 | ++#ifndef PAGE_SIZE |
| 42 | ++#include <unistd.h> |
| 43 | ++#define PAGE_SIZE sysconf(_SC_PAGE_SIZE) |
| 44 | ++#endif |
| 45 | ++ |
| 46 | ++static int |
| 47 | ++get_main_stack(void **addr, size_t *size) |
| 48 | ++{ |
| 49 | ++ size_t start, end, limit, prevend = 0; |
| 50 | ++ struct rlimit r; |
| 51 | ++ FILE *f; |
| 52 | ++ char buf[PATH_MAX+80], s[8]; |
| 53 | ++ int n; |
| 54 | ++ STACK_GROW_DIR_DETECTION; |
| 55 | ++ |
| 56 | ++ f = fopen("/proc/self/maps", "re"); |
| 57 | ++ if (!f) |
| 58 | ++ return -1; |
| 59 | ++ n = 0; |
| 60 | ++ while (fgets(buf, sizeof buf, f)) { |
| 61 | ++ n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", &start, &end, s); |
| 62 | ++ if (n >= 2) { |
| 63 | ++ if (n == 3 && strcmp(s, "[stack]") == 0) |
| 64 | ++ break; |
| 65 | ++ prevend = end; |
| 66 | ++ } |
| 67 | ++ n = 0; |
| 68 | ++ } |
| 69 | ++ fclose(f); |
| 70 | ++ if (n == 0) |
| 71 | ++ return -1; |
| 72 | ++ |
| 73 | ++ limit = 100 << 20; /* 100MB stack limit */ |
| 74 | ++ if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit) |
| 75 | ++ limit = r.rlim_cur & -PAGE_SIZE; |
| 76 | ++ if (limit > end) limit = end; |
| 77 | ++ if (prevend < end - limit) prevend = end - limit; |
| 78 | ++ if (start > prevend) start = prevend; |
| 79 | ++ *addr = IS_STACK_DIR_UPPER() ? (void *)start : (void *)end; |
| 80 | ++ *size = end - start; |
| 81 | ++ return 0; |
| 82 | ++} |
| 83 | ++#else |
| 84 | ++# define get_main_stack(addr, size) get_stack(addr, size) |
| 85 | ++#endif |
| 86 | ++ |
| 87 | + #endif |
| 88 | + |
| 89 | + static struct { |
| 90 | +-- |
| 91 | +2.16.2 |
| 92 | + |
0 commit comments