Skip to content

Commit d58f029

Browse files
authored
Use page-sized stack overflow detection window. (ruby#17436)
* Remove stack overflow space ratio * Cache page size.
1 parent 00f824c commit d58f029

1 file changed

Lines changed: 10 additions & 13 deletions

File tree

thread_pthread.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1731,9 +1731,15 @@ ruby_thread_set_native(rb_thread_t *th)
17311731
static void native_thread_setup(struct rb_native_thread *nt);
17321732
static void native_thread_setup_on_thread(struct rb_native_thread *nt);
17331733

1734+
// Internal cache of page size:
1735+
static size_t RB_THREAD_PAGE_SIZE;
1736+
17341737
void
17351738
Init_native_thread(rb_thread_t *main_th)
17361739
{
1740+
// Get the system page size for later use in stack allocation and stack overflow checks:
1741+
RB_THREAD_PAGE_SIZE = sysconf(_SC_PAGESIZE);
1742+
17371743
#if defined(HAVE_PTHREAD_CONDATTR_SETCLOCK)
17381744
if (condattr_monotonic) {
17391745
int r = pthread_condattr_init(condattr_monotonic);
@@ -1969,7 +1975,7 @@ get_stack(void **addr, size_t *size)
19691975
# ifdef HAVE_PTHREAD_ATTR_GETGUARDSIZE
19701976
CHECK_ERR(pthread_attr_getguardsize(&attr, &guard));
19711977
# else
1972-
guard = getpagesize();
1978+
guard = RB_THREAD_PAGE_SIZE;
19731979
# endif
19741980
*size -= guard;
19751981
pthread_attr_destroy(&attr);
@@ -2037,10 +2043,6 @@ static struct {
20372043
extern void *STACK_END_ADDRESS;
20382044
#endif
20392045

2040-
enum {
2041-
RUBY_STACK_SPACE_RATIO = 5
2042-
};
2043-
20442046
static void
20452047
native_thread_init_main_thread_stack(void *addr)
20462048
{
@@ -2074,26 +2076,22 @@ native_thread_init_main_thread_stack(void *addr)
20742076
{
20752077
#if defined(HAVE_GETRLIMIT)
20762078
#if defined(PTHREAD_STACK_DEFAULT)
2077-
# if PTHREAD_STACK_DEFAULT < RUBY_STACK_SPACE*5
2078-
# error "PTHREAD_STACK_DEFAULT is too small"
2079-
# endif
20802079
size_t size = PTHREAD_STACK_DEFAULT;
20812080
#else
20822081
size_t size = RUBY_VM_THREAD_VM_STACK_SIZE;
20832082
#endif
20842083
size_t space;
2085-
int pagesize = getpagesize();
20862084
struct rlimit rlim;
20872085
STACK_GROW_DIR_DETECTION;
20882086
if (getrlimit(RLIMIT_STACK, &rlim) == 0) {
20892087
size = (size_t)rlim.rlim_cur;
20902088
}
20912089
addr = native_main_thread.stack_start;
20922090
if (IS_STACK_DIR_UPPER()) {
2093-
space = ((size_t)((char *)addr + size) / pagesize) * pagesize - (size_t)addr;
2091+
space = ((size_t)((char *)addr + size) / RB_THREAD_PAGE_SIZE) * RB_THREAD_PAGE_SIZE - (size_t)addr;
20942092
}
20952093
else {
2096-
space = (size_t)addr - ((size_t)((char *)addr - size) / pagesize + 1) * pagesize;
2094+
space = (size_t)addr - ((size_t)((char *)addr - size) / RB_THREAD_PAGE_SIZE + 1) * RB_THREAD_PAGE_SIZE;
20972095
}
20982096
native_main_thread.stack_maxsize = space;
20992097
#endif
@@ -3253,7 +3251,7 @@ ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
32533251
{
32543252
void *base;
32553253
size_t size;
3256-
const size_t water_mark = 1024 * 1024;
3254+
const size_t water_mark = RB_THREAD_PAGE_SIZE;
32573255
STACK_GROW_DIR_DETECTION;
32583256

32593257
if (th) {
@@ -3277,7 +3275,6 @@ ruby_stack_overflowed_p(const rb_thread_t *th, const void *addr)
32773275
return 0;
32783276
}
32793277

3280-
size /= RUBY_STACK_SPACE_RATIO;
32813278
if (size > water_mark) size = water_mark;
32823279
if (IS_STACK_DIR_UPPER()) {
32833280
if (size > ~(size_t)base+1) size = ~(size_t)base+1;

0 commit comments

Comments
 (0)