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 <stdlib.h>
8 #include <sys/types.h>
9 #include <sys/resource.h>
10 
11 /* Use a noinline function to ensure that the buffer is not removed
12    from the stack.  */
13 static void use_buffer (char *buf) __attribute__ ((noinline));
14 static void
use_buffer(char * buf)15 use_buffer (char *buf)
16 {
17   buf[0] = '\0';
18 }
19 
20 /* Each recursive call uses 10,000 bytes.  We call it 1000 times,
21    using a total of 10,000,000 bytes.  If -fsplit-stack is not
22    working, that will overflow our stack limit.  */
23 
24 static void
down(int i)25 down (int i)
26 {
27   char buf[10000];
28 
29   if (i > 0)
30     {
31       use_buffer (buf);
32       down (i - 1);
33     }
34 }
35 
36 int
main(void)37 main (void)
38 {
39   struct rlimit r;
40 
41   /* We set a stack limit because we are usually invoked via make, and
42      make sets the stack limit to be as large as possible.  */
43   r.rlim_cur = 8192 * 1024;
44   r.rlim_max = 8192 * 1024;
45   if (setrlimit (RLIMIT_STACK, &r) != 0)
46     abort ();
47   down (1000);
48   return 0;
49 }
50