Skip to content

Commit dcf3add

Browse files
KJ TsanaktsidisKJTsanaktsidis
authored andcommitted
Delete reserve_stack code
This code was working around a bug in the Linux kernel. It was previously possible for the kernel to place heap pages in a region where the stack was allowed to grow into, and then therefore run out of usable stack memory before RLIMIT_STACK was reached. This bug was fixed in Linux commit torvalds/linux@c204d21 for kernel 4.13 in 2017. Therefore, in 2024, we should be safe to delete this workaround. [Bug #20804]
1 parent 7d254e4 commit dcf3add

File tree

1 file changed

+0
-57
lines changed

1 file changed

+0
-57
lines changed

thread_pthread.c

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,62 +1933,6 @@ space_size(size_t stack_size)
19331933
}
19341934
}
19351935

1936-
#ifdef __linux__
1937-
static __attribute__((noinline)) void
1938-
reserve_stack(volatile char *limit, size_t size)
1939-
{
1940-
# ifdef C_ALLOCA
1941-
# error needs alloca()
1942-
# endif
1943-
struct rlimit rl;
1944-
volatile char buf[0x100];
1945-
enum {stack_check_margin = 0x1000}; /* for -fstack-check */
1946-
1947-
STACK_GROW_DIR_DETECTION;
1948-
1949-
if (!getrlimit(RLIMIT_STACK, &rl) && rl.rlim_cur == RLIM_INFINITY)
1950-
return;
1951-
1952-
if (size < stack_check_margin) return;
1953-
size -= stack_check_margin;
1954-
1955-
size -= sizeof(buf); /* margin */
1956-
if (IS_STACK_DIR_UPPER()) {
1957-
const volatile char *end = buf + sizeof(buf);
1958-
limit += size;
1959-
if (limit > end) {
1960-
/* |<-bottom (=limit(a)) top->|
1961-
* | .. |<-buf 256B |<-end | stack check |
1962-
* | 256B | =size= | margin (4KB)|
1963-
* | =size= limit(b)->| 256B | |
1964-
* | | alloca(sz) | | |
1965-
* | .. |<-buf |<-limit(c) [sz-1]->0> | |
1966-
*/
1967-
size_t sz = limit - end;
1968-
limit = alloca(sz);
1969-
limit[sz-1] = 0;
1970-
}
1971-
}
1972-
else {
1973-
limit -= size;
1974-
if (buf > limit) {
1975-
/* |<-top (=limit(a)) bottom->|
1976-
* | .. | 256B buf->| | stack check |
1977-
* | 256B | =size= | margin (4KB)|
1978-
* | =size= limit(b)->| 256B | |
1979-
* | | alloca(sz) | | |
1980-
* | .. | buf->| limit(c)-><0> | |
1981-
*/
1982-
size_t sz = buf - limit;
1983-
limit = alloca(sz);
1984-
limit[0] = 0;
1985-
}
1986-
}
1987-
}
1988-
#else
1989-
# define reserve_stack(limit, size) ((void)(limit), (void)(size))
1990-
#endif
1991-
19921936
static void
19931937
native_thread_init_main_thread_stack(void *addr)
19941938
{
@@ -2005,7 +1949,6 @@ native_thread_init_main_thread_stack(void *addr)
20051949
if (get_main_stack(&stackaddr, &size) == 0) {
20061950
native_main_thread.stack_maxsize = size;
20071951
native_main_thread.stack_start = stackaddr;
2008-
reserve_stack(stackaddr, size);
20091952
goto bound_check;
20101953
}
20111954
}

0 commit comments

Comments
 (0)