]> git.saurik.com Git - apple/libpthread.git/blob - tests/helpers/stackoverflow_crash.c
libpthread-454.100.8.tar.gz
[apple/libpthread.git] / tests / helpers / stackoverflow_crash.c
1 #include<stdio.h>
2 #include<sys/resource.h>
3
4 static volatile int * array1_ref = NULL;
5 static long last_stack_addr = 0;
6
7 static void
8 recursive_fn(void)
9 {
10 volatile int array1[1024]; /* leave this as it is */
11 int addr;
12 last_stack_addr = (long)&addr;
13 array1_ref = array1; /* make sure compiler cannot discard storage */
14 array1[0] = 0;
15 if (array1_ref == 0) {
16 /* fool clang -Winfinite-recursion */
17 return;
18 }
19 recursive_fn();
20 return;
21 }
22
23 int
24 main(__unused int argc, __unused const char *argv[])
25 {
26 struct rlimit save;
27
28 if (getrlimit(RLIMIT_STACK, &save) == -1) {
29 printf("child: ERROR - getrlimit");
30 return 2;
31 }
32 printf("child: LOG - current stack limits cur=0x%llx, max=0x%llx, inf=0x%llx\n", save.rlim_cur, save.rlim_max, RLIM_INFINITY);
33
34 if(save.rlim_cur >= save.rlim_max) {
35 printf("child: ERROR - invalid limits");
36 return 2;
37 }
38
39 if(save.rlim_max == RLIM_INFINITY) {
40 printf("child: ERROR - rlim_max = RLIM_INFINITY");
41 return 2;
42 }
43
44 save.rlim_cur += 4;
45
46 printf("child: LOG - Raising setrlimit rlim_cur=0x%llx, rlim_max=0x%llx\n", save.rlim_cur, save.rlim_max);
47
48 if (setrlimit(RLIMIT_STACK, &save) == -1) {
49 printf("child: ERROR - Raising the limits failed.");
50 return 2;
51 }
52
53 printf("child: LOG - Make the stack grow such that a SIGSEGV is generated.\n");
54 recursive_fn();
55 return 0;
56 }