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 <string.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, size_t) __attribute__ ((noinline));
15 static void
use_buffer(char * buf,size_t c)16 use_buffer (char *buf, size_t c)
17 {
18   size_t i;
19 
20   for (i = 0; i < c; ++i)
21     buf[i] = (char) i;
22 }
23 
24 /* Each recursive call uses 10 * i bytes.  We call it 1000 times,
25    using a total of 5,000,000 bytes.  If -fsplit-stack is not working,
26    that will overflow our stack limit.  */
27 
28 static void
down1(int i)29 down1 (int i)
30 {
31   char buf[10 * i];
32 
33   if (i > 0)
34     {
35       use_buffer (buf, 10 * i);
36       down1 (i - 1);
37     }
38 }
39 
40 /* Same thing, using alloca.  */
41 
42 static void
down2(int i)43 down2 (int i)
44 {
45   char *buf = alloca (10 * i);
46 
47   if (i > 0)
48     {
49       use_buffer (buf, 10 * i);
50       down2 (i - 1);
51     }
52 }
53 
54 int
main(void)55 main (void)
56 {
57   struct rlimit r;
58 
59   /* We set a stack limit because we are usually invoked via make, and
60      make sets the stack limit to be as large as possible.  */
61   r.rlim_cur = 8192 * 1024;
62   r.rlim_max = 8192 * 1024;
63   if (setrlimit (RLIMIT_STACK, &r) != 0)
64     abort ();
65   down1 (1000);
66   down2 (1000);
67   return 0;
68 }
69