1// Test that ASan doesn't hang on stack overflow in recovery mode.
2//
3// RUN: %clang_asan -O0 -fsanitize-recover=address %s -o %t
4// RUN: %env_asan_opts=halt_on_error=false not %run %t 2>&1 | FileCheck %s
5
6#include <assert.h>
7#include <unistd.h>
8#include <sys/mman.h>
9#include <sys/resource.h>
10
11static volatile int *recurse(volatile int n, volatile int *p) {
12  // CHECK: {{stack-overflow on address 0x.* \(pc 0x.* bp 0x.* sp 0x.* T.*\)}}
13  if (n >= 0) *recurse(n + 1, p) += n;
14  return p;
15}
16
17
18void LimitStackAndReexec(int argc, char **argv) {
19  struct rlimit rlim;
20  int res = getrlimit(RLIMIT_STACK, &rlim);
21  assert(res == 0);
22  if (rlim.rlim_cur == RLIM_INFINITY) {
23    rlim.rlim_cur = 256 * 1024;
24    res = setrlimit(RLIMIT_STACK, &rlim);
25    assert(res == 0);
26
27    execv(argv[0], argv);
28    assert(0 && "unreachable");
29  }
30}
31
32int main(int argc, char **argv) {
33  LimitStackAndReexec(argc, argv);
34  volatile int res;
35  return *recurse(argc + 1, &res);
36}
37