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
6 // XFAIL: target={{sparc.*-.*-linux.*}}
14 #include <sys/resource.h>
20 void recursive_func(char *p
) {
27 x
= 1; // prevent tail call optimization
28 // CHECK: {{stack-overflow on address 0x.* \(pc 0x.* bp 0x.* sp 0x.* T.*\)}}
31 void LimitStackAndReexec(int argc
, char **argv
) {
33 int res
= getrlimit(RLIMIT_STACK
, &rlim
);
35 if (rlim
.rlim_cur
== RLIM_INFINITY
) {
36 rlim
.rlim_cur
= 256 * 1024;
37 res
= setrlimit(RLIMIT_STACK
, &rlim
);
41 assert(0 && "unreachable");
45 int main(int argc
, char **argv
) {
46 LimitStackAndReexec(argc
, argv
);
48 // Map some memory just before the start of the current stack vma.
49 // When the stack grows down and crashes into it, Linux can send
50 // SIGBUS instead of SIGSEGV. See:
51 // http://lkml.iu.edu/hypermail/linux/kernel/1008.1/02299.html
52 const long pagesize
= sysconf(_SC_PAGESIZE
);
53 FILE *f
= fopen("/proc/self/maps", "r");
56 while (fgets(a
, sizeof a
, f
)) {
57 if (strstr(a
, "[stack]")) {
59 if (sscanf(a
, "%lx", &addr
) == 1)
60 p
= mmap((void *)(addr
- 4 * pagesize
), pagesize
, PROT_READ
| PROT_WRITE
, MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);