[compiler-rt] r201670 - [asan] Ensure that stack is limited before attempting to overflow it.

Evgeniy Stepanov eugeni.stepanov at gmail.com
Wed Feb 19 04:43:28 PST 2014


Author: eugenis
Date: Wed Feb 19 06:43:27 2014
New Revision: 201670

URL: http://llvm.org/viewvc/llvm-project?rev=201670&view=rev
Log:
[asan] Ensure that stack is limited before attempting to overflow it.

Very bad things happen otherwise.

Modified:
    compiler-rt/trunk/test/asan/TestCases/stack-overflow.cc

Modified: compiler-rt/trunk/test/asan/TestCases/stack-overflow.cc
URL: http://llvm.org/viewvc/llvm-project/compiler-rt/trunk/test/asan/TestCases/stack-overflow.cc?rev=201670&r1=201669&r2=201670&view=diff
==============================================================================
--- compiler-rt/trunk/test/asan/TestCases/stack-overflow.cc (original)
+++ compiler-rt/trunk/test/asan/TestCases/stack-overflow.cc Wed Feb 19 06:43:27 2014
@@ -17,6 +17,9 @@
 #include <assert.h>
 #include <stdlib.h>
 #include <pthread.h>
+#include <unistd.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 
 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);





More information about the llvm-commits mailing list