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