Skip to content

Commit aef36eb

Browse files
authored
[clang] Remove old Linux kernel workaround for ensuring stack space (#81533)
PR #71709 broke the Linux PIE build with `undefined symbol: alloca` errors. With the newly included `clang/Basic/Builtins.h` in that PR, it surfaces an issue with a combination of two previous patches. 26670dc added `#undef alloca` so clang builtins handling of alloca would work under MSVC (unsure if this is still necessary). 194b6a3 added code that calls `alloca` to workaround a Linux kernel < 4.1 bug. Given that Linux 4.1 was EOL in 2018, it should be ok to remove this workaround.
1 parent 61f64d1 commit aef36eb

File tree

1 file changed

+0
-62
lines changed

1 file changed

+0
-62
lines changed

Diff for: clang/tools/driver/cc1_main.cpp

-62
Original file line numberDiff line numberDiff line change
@@ -78,64 +78,6 @@ static void LLVMErrorHandler(void *UserData, const char *Message,
7878
}
7979

8080
#ifdef CLANG_HAVE_RLIMITS
81-
#if defined(__linux__) && defined(__PIE__)
82-
static size_t getCurrentStackAllocation() {
83-
// If we can't compute the current stack usage, allow for 512K of command
84-
// line arguments and environment.
85-
size_t Usage = 512 * 1024;
86-
if (FILE *StatFile = fopen("/proc/self/stat", "r")) {
87-
// We assume that the stack extends from its current address to the end of
88-
// the environment space. In reality, there is another string literal (the
89-
// program name) after the environment, but this is close enough (we only
90-
// need to be within 100K or so).
91-
unsigned long StackPtr, EnvEnd;
92-
// Disable silly GCC -Wformat warning that complains about length
93-
// modifiers on ignored format specifiers. We want to retain these
94-
// for documentation purposes even though they have no effect.
95-
#if defined(__GNUC__) && !defined(__clang__)
96-
#pragma GCC diagnostic push
97-
#pragma GCC diagnostic ignored "-Wformat"
98-
#endif
99-
if (fscanf(StatFile,
100-
"%*d %*s %*c %*d %*d %*d %*d %*d %*u %*lu %*lu %*lu %*lu %*lu "
101-
"%*lu %*ld %*ld %*ld %*ld %*ld %*ld %*llu %*lu %*ld %*lu %*lu "
102-
"%*lu %*lu %lu %*lu %*lu %*lu %*lu %*lu %*llu %*lu %*lu %*d %*d "
103-
"%*u %*u %*llu %*lu %*ld %*lu %*lu %*lu %*lu %*lu %*lu %lu %*d",
104-
&StackPtr, &EnvEnd) == 2) {
105-
#if defined(__GNUC__) && !defined(__clang__)
106-
#pragma GCC diagnostic pop
107-
#endif
108-
Usage = StackPtr < EnvEnd ? EnvEnd - StackPtr : StackPtr - EnvEnd;
109-
}
110-
fclose(StatFile);
111-
}
112-
return Usage;
113-
}
114-
115-
#include <alloca.h>
116-
117-
LLVM_ATTRIBUTE_NOINLINE
118-
static void ensureStackAddressSpace() {
119-
// Linux kernels prior to 4.1 will sometimes locate the heap of a PIE binary
120-
// relatively close to the stack (they are only guaranteed to be 128MiB
121-
// apart). This results in crashes if we happen to heap-allocate more than
122-
// 128MiB before we reach our stack high-water mark.
123-
//
124-
// To avoid these crashes, ensure that we have sufficient virtual memory
125-
// pages allocated before we start running.
126-
size_t Curr = getCurrentStackAllocation();
127-
const int kTargetStack = DesiredStackSize - 256 * 1024;
128-
if (Curr < kTargetStack) {
129-
volatile char *volatile Alloc =
130-
static_cast<volatile char *>(alloca(kTargetStack - Curr));
131-
Alloc[0] = 0;
132-
Alloc[kTargetStack - Curr - 1] = 0;
133-
}
134-
}
135-
#else
136-
static void ensureStackAddressSpace() {}
137-
#endif
138-
13981
/// Attempt to ensure that we have at least 8MiB of usable stack space.
14082
static void ensureSufficientStack() {
14183
struct rlimit rlim;
@@ -159,10 +101,6 @@ static void ensureSufficientStack() {
159101
rlim.rlim_cur != DesiredStackSize)
160102
return;
161103
}
162-
163-
// We should now have a stack of size at least DesiredStackSize. Ensure
164-
// that we can actually use that much, if necessary.
165-
ensureStackAddressSpace();
166104
}
167105
#else
168106
static void ensureSufficientStack() {}

0 commit comments

Comments
 (0)