fix-get_main_stack.patch 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. diff --git a/thread_pthread.c b/thread_pthread.c
  2. index 951885ffa0..cf90321d1d 100644
  3. --- a/thread_pthread.c
  4. +++ b/thread_pthread.c
  5. @@ -656,9 +656,6 @@ hpux_attr_getstackaddr(const pthread_attr_t *attr, void **addr)
  6. # define MAINSTACKADDR_AVAILABLE 0
  7. # endif
  8. #endif
  9. -#if MAINSTACKADDR_AVAILABLE && !defined(get_main_stack)
  10. -# define get_main_stack(addr, size) get_stack(addr, size)
  11. -#endif
  12. #ifdef STACKADDR_AVAILABLE
  13. /*
  14. @@ -740,6 +737,55 @@ get_stack(void **addr, size_t *size)
  15. return 0;
  16. #undef CHECK_ERR
  17. }
  18. +
  19. +#if defined(__linux__) && !defined(__GLIBC__) && defined(HAVE_GETRLIMIT)
  20. +
  21. +#ifndef PAGE_SIZE
  22. +#include <unistd.h>
  23. +#define PAGE_SIZE sysconf(_SC_PAGE_SIZE)
  24. +#endif
  25. +
  26. +static int
  27. +get_main_stack(void **addr, size_t *size)
  28. +{
  29. + size_t start, end, limit, prevend = 0;
  30. + struct rlimit r;
  31. + FILE *f;
  32. + char buf[PATH_MAX+80], s[8];
  33. + int n;
  34. + STACK_GROW_DIR_DETECTION;
  35. +
  36. + f = fopen("/proc/self/maps", "re");
  37. + if (!f)
  38. + return -1;
  39. + n = 0;
  40. + while (fgets(buf, sizeof buf, f)) {
  41. + n = sscanf(buf, "%zx-%zx %*s %*s %*s %*s %7s", &start, &end, s);
  42. + if (n >= 2) {
  43. + if (n == 3 && strcmp(s, "[stack]") == 0)
  44. + break;
  45. + prevend = end;
  46. + }
  47. + n = 0;
  48. + }
  49. + fclose(f);
  50. + if (n == 0)
  51. + return -1;
  52. +
  53. + limit = 100 << 20; /* 100MB stack limit */
  54. + if (getrlimit(RLIMIT_STACK, &r)==0 && r.rlim_cur < limit)
  55. + limit = r.rlim_cur & -PAGE_SIZE;
  56. + if (limit > end) limit = end;
  57. + if (prevend < end - limit) prevend = end - limit;
  58. + if (start > prevend) start = prevend;
  59. + *addr = IS_STACK_DIR_UPPER() ? (void *)start : (void *)end;
  60. + *size = end - start;
  61. + return 0;
  62. +}
  63. +#else
  64. +# define get_main_stack(addr, size) get_stack(addr, size)
  65. +#endif
  66. +
  67. #endif
  68. static struct {