From cc8e58bc4656b8523b2ae2caae2f86f4fe6717d4 Mon Sep 17 00:00:00 2001 From: Evgeniy Stepanov Date: Wed, 19 Feb 2014 12:43:27 +0000 Subject: [PATCH] [asan] Ensure that stack is limited before attempting to overflow it. Very bad things happen otherwise. llvm-svn: 201670 --- .../test/asan/TestCases/stack-overflow.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/compiler-rt/test/asan/TestCases/stack-overflow.cc b/compiler-rt/test/asan/TestCases/stack-overflow.cc index e400669bd5b3..56d510db48e1 100644 --- a/compiler-rt/test/asan/TestCases/stack-overflow.cc +++ b/compiler-rt/test/asan/TestCases/stack-overflow.cc @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include const int BS = 1024; volatile char x; @@ -80,7 +83,22 @@ void *ThreadFn(void* unused) { return 0; } +void LimitStackAndReexec(int argc, char **argv) { + struct rlimit rlim; + int res = getrlimit(RLIMIT_STACK, &rlim); + assert(res == 0); + if (rlim.rlim_cur == RLIM_INFINITY) { + rlim.rlim_cur = 128 * 1024; + res = setrlimit(RLIMIT_STACK, &rlim); + assert(res == 0); + + execv(argv[0], argv); + assert(0 && "unreachable"); + } +} + int main(int argc, char **argv) { + LimitStackAndReexec(argc, argv); #ifdef THREAD pthread_t t; pthread_create(&t, 0, ThreadFn, 0);