Skip to content

Instantly share code, notes, and snippets.

@ncopa
Created March 14, 2018 12:31
Show Gist options
  • Save ncopa/be27252cb911591dab025e5e7eb0a2c2 to your computer and use it in GitHub Desktop.
Save ncopa/be27252cb911591dab025e5e7eb0a2c2 to your computer and use it in GitHub Desktop.
diff --git a/thread_pthread.c b/thread_pthread.c
index 951885ffa0..6c9241dd58 100644
--- a/thread_pthread.c
+++ b/thread_pthread.c
@@ -530,9 +530,6 @@ hpux_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
# define MAINSTACKADDR_AVAILABLE 0
# endif
#endif
-#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)
-# define get_main_stack(addr, size) get_stack(addr, size)
-#endif
#ifdef STACKADDR_AVAILABLE
/*
@@ -614,6 +611,29 @@ get_stack(void **addr, size_t *size)
return 0;
#undef CHECK_ERR
}
+
+#if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT)
+
+#ifndef PAGE_SIZE
+#include <unistd.h>
+#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
+#endif
+
+static int
+get_main_stack(void **addr, size_t *size)
+{
+ struct rlimit rl;
+ int r = get_stack(addr, size);
+ if (r != 0)
+ return r;
+ if (getrlimit(RLIMIT_STACK, &rl) == 0 && rl.rlim_cur > PAGE_SIZE && (rl.rlim_cur-PAGE_SIZE) > *size)
+ *size = (size_t)(rl.rlim_cur - PAGE_SIZE);
+ return 0;
+}
+#else
+# define get_main_stack(addr, size) get_stack(addr, size)
+#endif
+
#endif
static struct {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment