// DEFINE: %if target={{.*-windows-msvc.*}} %{ -Wl,/STACK:8388608 %}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#ifdef _WIN32
# include <windows.h>
#endif
__attribute__((noinline))
char *pretend_to_do_something(char *x) {
__asm__ __volatile__("" : : "r" (x) : "memory");
return x;
}
__attribute__((noinline))
char *LeakStack() {
char x[1024];
memset(x, 0, sizeof(x));
return pretend_to_do_something(x);
}
template<size_t kFrameSize>
__attribute__((noinline))
void RecursiveFunctionWithStackFrame(int depth) {
if (depth <= 0) return;
char x[kFrameSize];
x[0] = depth;
pretend_to_do_something(x);
RecursiveFunctionWithStackFrame<kFrameSize>(depth - 1);
}
int main(int argc, char **argv) {
#ifdef _WIN32
SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX);
#endif
int n_iter = argc >= 2 ? atoi(argv[1]) : 1000;
int depth = argc >= 3 ? atoi(argv[2]) : 500;
for (int i = 0; i < n_iter; i++) {
RecursiveFunctionWithStackFrame<10>(depth);
RecursiveFunctionWithStackFrame<100>(depth);
RecursiveFunctionWithStackFrame<500>(depth);
RecursiveFunctionWithStackFrame<1024>(depth);
RecursiveFunctionWithStackFrame<2000>(depth);
#if !defined(__FreeBSD__) && !defined(__NetBSD__)
RecursiveFunctionWithStackFrame<5000>(depth);
RecursiveFunctionWithStackFrame<10000>(depth);
#endif
}
char *stale_stack = LeakStack();
RecursiveFunctionWithStackFrame<1024>(10);
stale_stack[100]++;
return 0;
}