1 /* This test needs to use setrlimit to set the stack size, so it can
2    only run on Unix.  */
3 /* { dg-do run { target *-*-linux* *-*-gnu* *-*-solaris* *-*-darwin* } } */
4 /* { dg-require-effective-target split_stack } */
5 /* { dg-options "-fsplit-stack" } */
6 
7 #include <stdarg.h>
8 #include <stdlib.h>
9 #include <sys/types.h>
10 #include <sys/resource.h>
11 
12 /* Use a noinline function to ensure that the buffer is not removed
13    from the stack.  */
14 static void use_buffer (char *buf) __attribute__ ((noinline));
15 static void
use_buffer(char * buf)16 use_buffer (char *buf)
17 {
18   buf[0] = '\0';
19 }
20 
21 /* Each recursive call uses 10,000 bytes.  We call it 1000 times,
22    using a total of 10,000,000 bytes.  If -fsplit-stack is not
23    working, that will overflow our stack limit.  */
24 
25 static void
down(int i,...)26 down (int i, ...)
27 {
28   char buf[10000];
29   va_list ap;
30 
31   va_start (ap, i);
32   if (va_arg (ap, int) != 1
33       || va_arg (ap, int) != 2
34       || va_arg (ap, int) != 3
35       || va_arg (ap, int) != 4
36       || va_arg (ap, int) != 5
37       || va_arg (ap, int) != 6
38       || va_arg (ap, int) != 7
39       || va_arg (ap, int) != 8
40       || va_arg (ap, int) != 9
41       || va_arg (ap, int) != 10)
42     abort ();
43 
44   if (i > 0)
45     {
46       use_buffer (buf);
47       down (i - 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
48     }
49 }
50 
51 int
main(void)52 main (void)
53 {
54   struct rlimit r;
55 
56   /* We set a stack limit because we are usually invoked via make, and
57      make sets the stack limit to be as large as possible.  */
58   r.rlim_cur = 8192 * 1024;
59   r.rlim_max = 8192 * 1024;
60   if (setrlimit (RLIMIT_STACK, &r) != 0)
61     abort ();
62   down (1000, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
63   return 0;
64 }
65