1*7c3d14c8STreehugger Robot // Test ASan detection of stack-overflow condition when Linux sends SIGBUS.
2*7c3d14c8STreehugger Robot
3*7c3d14c8STreehugger Robot // RUN: %clangxx_asan -O0 %s -o %t && %env_asan_opts=use_sigaltstack=1 not %run %t 2>&1 | FileCheck %s
4*7c3d14c8STreehugger Robot
5*7c3d14c8STreehugger Robot #include <assert.h>
6*7c3d14c8STreehugger Robot #include <stdio.h>
7*7c3d14c8STreehugger Robot #include <stdlib.h>
8*7c3d14c8STreehugger Robot #include <string.h>
9*7c3d14c8STreehugger Robot #include <unistd.h>
10*7c3d14c8STreehugger Robot #include <sys/mman.h>
11*7c3d14c8STreehugger Robot #include <sys/resource.h>
12*7c3d14c8STreehugger Robot
13*7c3d14c8STreehugger Robot const int BS = 1024;
14*7c3d14c8STreehugger Robot volatile char x;
15*7c3d14c8STreehugger Robot volatile int y = 1;
16*7c3d14c8STreehugger Robot
recursive_func(char * p)17*7c3d14c8STreehugger Robot void recursive_func(char *p) {
18*7c3d14c8STreehugger Robot char buf[BS];
19*7c3d14c8STreehugger Robot buf[rand() % BS] = 1;
20*7c3d14c8STreehugger Robot buf[rand() % BS] = 2;
21*7c3d14c8STreehugger Robot x = buf[rand() % BS];
22*7c3d14c8STreehugger Robot if (y)
23*7c3d14c8STreehugger Robot recursive_func(buf);
24*7c3d14c8STreehugger Robot x = 1; // prevent tail call optimization
25*7c3d14c8STreehugger Robot // CHECK: {{stack-overflow on address 0x.* \(pc 0x.* bp 0x.* sp 0x.* T.*\)}}
26*7c3d14c8STreehugger Robot }
27*7c3d14c8STreehugger Robot
LimitStackAndReexec(int argc,char ** argv)28*7c3d14c8STreehugger Robot void LimitStackAndReexec(int argc, char **argv) {
29*7c3d14c8STreehugger Robot struct rlimit rlim;
30*7c3d14c8STreehugger Robot int res = getrlimit(RLIMIT_STACK, &rlim);
31*7c3d14c8STreehugger Robot assert(res == 0);
32*7c3d14c8STreehugger Robot if (rlim.rlim_cur == RLIM_INFINITY) {
33*7c3d14c8STreehugger Robot rlim.rlim_cur = 256 * 1024;
34*7c3d14c8STreehugger Robot res = setrlimit(RLIMIT_STACK, &rlim);
35*7c3d14c8STreehugger Robot assert(res == 0);
36*7c3d14c8STreehugger Robot
37*7c3d14c8STreehugger Robot execv(argv[0], argv);
38*7c3d14c8STreehugger Robot assert(0 && "unreachable");
39*7c3d14c8STreehugger Robot }
40*7c3d14c8STreehugger Robot }
41*7c3d14c8STreehugger Robot
main(int argc,char ** argv)42*7c3d14c8STreehugger Robot int main(int argc, char **argv) {
43*7c3d14c8STreehugger Robot LimitStackAndReexec(argc, argv);
44*7c3d14c8STreehugger Robot
45*7c3d14c8STreehugger Robot // Map some memory just before the start of the current stack vma.
46*7c3d14c8STreehugger Robot // When the stack grows down and crashes into it, Linux can send
47*7c3d14c8STreehugger Robot // SIGBUS instead of SIGSEGV. See:
48*7c3d14c8STreehugger Robot // http://lkml.iu.edu/hypermail/linux/kernel/1008.1/02299.html
49*7c3d14c8STreehugger Robot const long pagesize = sysconf(_SC_PAGESIZE);
50*7c3d14c8STreehugger Robot FILE *f = fopen("/proc/self/maps", "r");
51*7c3d14c8STreehugger Robot char a[1000];
52*7c3d14c8STreehugger Robot void *p = 0;
53*7c3d14c8STreehugger Robot while (fgets(a, sizeof a, f)) {
54*7c3d14c8STreehugger Robot if (strstr(a, "[stack]")) {
55*7c3d14c8STreehugger Robot unsigned long addr;
56*7c3d14c8STreehugger Robot if (sscanf(a, "%lx", &addr) == 1)
57*7c3d14c8STreehugger Robot p = mmap((void *)(addr - 4 * pagesize), pagesize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
58*7c3d14c8STreehugger Robot }
59*7c3d14c8STreehugger Robot }
60*7c3d14c8STreehugger Robot assert(p);
61*7c3d14c8STreehugger Robot
62*7c3d14c8STreehugger Robot recursive_func(0);
63*7c3d14c8STreehugger Robot return 0;
64*7c3d14c8STreehugger Robot }
65