1 // Test ASan detection of stack-overflow condition when Linux sends SIGBUS.
3 // RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=use_sigaltstack=1 not %run %t 2>&1 | FileCheck %s
11 #include <sys/resource.h>
17 void recursive_func(char *p) {
24 x = 1; // prevent tail call optimization
25 // CHECK: {{stack-overflow on address 0x.* \(pc 0x.* bp 0x.* sp 0x.* T.*\)}}
28 void LimitStackAndReexec(int argc, char **argv) {
30 int res = getrlimit(RLIMIT_STACK, &rlim);
32 if (rlim.rlim_cur == RLIM_INFINITY) {
33 rlim.rlim_cur = 256 * 1024;
34 res = setrlimit(RLIMIT_STACK, &rlim);
38 assert(0 && "unreachable");
42 int main(int argc, char **argv) {
43 LimitStackAndReexec(argc, argv);
45 // Map some memory just before the start of the current stack vma.
46 // When the stack grows down and crashes into it, Linux can send
47 // SIGBUS instead of SIGSEGV. See:
48 // http://lkml.iu.edu/hypermail/linux/kernel/1008.1/02299.html
49 const long pagesize = sysconf(_SC_PAGESIZE);
50 FILE *f = fopen("/proc/self/maps", "r");
53 while (fgets(a, sizeof a, f)) {
54 if (strstr(a, "[stack]")) {
56 if (sscanf(a, "%lx", &addr) == 1)
57 p = mmap((void *)(addr - 4 * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);