|
| 1 | +/* MP support header */ |
| 2 | +#include "MacportsLegacySupport.h" |
| 3 | +#if __MP_LEGACY_SUPPORT_PTHREAD_GET_STACKSIZE_NP_FIX__ |
| 4 | + |
| 5 | +#include <pthread.h> |
| 6 | +#include <sys/resource.h> |
| 7 | +#include <dlfcn.h> |
| 8 | +#include <stdlib.h> |
| 9 | + |
| 10 | +#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 |
| 11 | +/* private system call available on OS X Mavericks (version 10.9) and later */ |
| 12 | +/* see https://github.com/apple-oss-distributions/libpthread/blob/ba8e1488a0e6848b710c5daad2e226f66cfed656/private/pthread/private.h#L34 */ |
| 13 | +pthread_t pthread_main_thread_np(void); |
| 14 | +#endif |
| 15 | + |
| 16 | +#define kMaxThreadStackSize 0x40000000 /* from LLVM: 1 << 30 or 1Gb */ |
| 17 | + |
| 18 | +size_t pthread_get_stacksize_np(pthread_t t) { |
| 19 | +#if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1090 |
| 20 | + int is_main_thread = pthread_equal(t, pthread_main_thread_np()); |
| 21 | +#else |
| 22 | + /* taken from Apple Libc */ |
| 23 | + /* see https://github.com/apple-oss-distributions/Libc/blob/224a8105d573730ae33f474ae5b63b113123aee4/pthreads/pthread.c#L1167 */ |
| 24 | + /* for _PTHREAD_CREATE_PARENT, see https://github.com/apple-oss-distributions/Libc/blob/224a8105d573730ae33f474ae5b63b113123aee4/pthreads/pthread_internals.h#L647C9-L647C31 */ |
| 25 | + /* for pthread_t, see https://github.com/apple-oss-distributions/Libc/blob/224a8105d573730ae33f474ae5b63b113123aee4/pthreads/pthread_internals.h#L107 */ |
| 26 | + /* for pthread_lock_t, see https://github.com/apple-oss-distributions/Libc/blob/224a8105d573730ae33f474ae5b63b113123aee4/pthreads/pthread_machdep.h#L214C13-L214C27 */ |
| 27 | + struct |
| 28 | + { |
| 29 | + long sig; |
| 30 | + struct __darwin_pthread_handler_rec *cleanup_stack; |
| 31 | + int lock; |
| 32 | + __int32_t detached:8, |
| 33 | + inherit:8, |
| 34 | + policy:8, |
| 35 | + freeStackOnExit:1, |
| 36 | + newstyle:1, |
| 37 | + kernalloc:1, |
| 38 | + schedset:1, |
| 39 | + wqthread:1, |
| 40 | + pad:3; |
| 41 | + char opaque[__PTHREAD_SIZE__-sizeof(int)-sizeof(__int32_t)]; |
| 42 | + } *thread = (void*) t; |
| 43 | + int is_main_thread = ((thread->detached & 4) == 4); |
| 44 | +#endif |
| 45 | + if ( is_main_thread ) { |
| 46 | + /* use LLVM workaround */ |
| 47 | + /* see https://github.com/llvm/llvm-project/blob/617a15a9eac96088ae5e9134248d8236e34b91b1/compiler-rt/lib/sanitizer_common/sanitizer_mac.cpp#L414 */ |
| 48 | + /* OpenJDK also has a workaround */ |
| 49 | + /* see https://github.com/openjdk/jdk/blob/e833bfc8ac6104522d037e7eb300f5aa112688bb/src/hotspot/os_cpu/bsd_x86/os_bsd_x86.cpp#L715 */ |
| 50 | + struct rlimit limit; |
| 51 | + if( getrlimit(RLIMIT_STACK, &limit) ) { |
| 52 | + exit(EXIT_FAILURE); |
| 53 | + } |
| 54 | + if( limit.rlim_cur < kMaxThreadStackSize ) { |
| 55 | + return limit.rlim_cur; |
| 56 | + } else { |
| 57 | + return kMaxThreadStackSize; |
| 58 | + } |
| 59 | + } else { |
| 60 | + /* bug only affects main thread */ |
| 61 | + size_t (*real_pthread_get_stacksize_np)(pthread_t); |
| 62 | + real_pthread_get_stacksize_np = dlsym(RTLD_NEXT, "pthread_get_stacksize_np"); |
| 63 | + if (real_pthread_get_stacksize_np == NULL) { |
| 64 | + exit(EXIT_FAILURE); |
| 65 | + } |
| 66 | + return real_pthread_get_stacksize_np(t); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#endif /* __MP_LEGACY_SUPPORT_PTHREAD_GET_STACKSIZE_NP_FIX__ */ |
0 commit comments