gcc/
[official-gcc.git] / gcc / testsuite / gcc.dg / fstack-protector-strong.c
blob7c232fff24377ce290dd829d0515ef8179135992
1 /* Test that stack protection is done on chosen functions. */
3 /* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */
4 /* { dg-options "-O2 -fstack-protector-strong" } */
6 #include<string.h>
8 extern int g0;
9 extern int* pg0;
10 int
11 goo (int *);
12 int
13 hoo (int);
15 /* Function frame address escaped function call. */
16 int
17 foo1 ()
19 int i;
20 return goo (&i);
23 struct ArrayStruct
25 int a;
26 int array[10];
29 struct AA
31 int b;
32 struct ArrayStruct as;
35 /* Function frame contains array. */
36 int
37 foo2 ()
39 struct AA aa;
40 int i;
41 for (i = 0; i < 10; ++i)
43 aa.as.array[i] = i * (i-1) + i / 2;
45 return aa.as.array[5];
48 /* Address computation based on a function frame address. */
49 int
50 foo3 ()
52 int a;
53 int *p;
54 p = &a + 5;
55 return goo (p);
58 /* Address cast based on a function frame address. */
59 int
60 foo4 ()
62 int a;
63 return goo (g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0);
66 /* Address cast based on a local array. */
67 int
68 foo5 ()
70 short array[10];
71 return goo ((int *)(array + 5));
74 struct BB
76 int one;
77 int two;
78 int three;
81 /* Address computaton based on a function frame address.*/
82 int
83 foo6 ()
85 struct BB bb;
86 return goo (&bb.one + sizeof(int));
89 /* Function frame address escaped via global variable. */
90 int
91 foo7 ()
93 int a;
94 pg0 = &a;
95 goo (pg0);
96 return *pg0;
99 /* Check that this covers -fstack-protector. */
101 foo8 ()
103 char base[100];
104 memcpy ((void *)base, (const void *)pg0, 105);
105 return (int)(base[32]);
108 /* Check that this covers -fstack-protector. */
110 foo9 ()
112 char* p = __builtin_alloca (100);
113 return goo ((int *)(p + 50));
117 global2 (struct BB* pbb);
119 /* Address taken on struct. */
121 foo10 ()
123 struct BB bb;
124 int i;
125 bb.one = global2 (&bb);
126 for (i = 0; i < 10; ++i)
128 bb.two = bb.one + bb.two;
129 bb.three = bb.one + bb.two + bb.three;
131 return bb.three;
134 /* { dg-final { scan-assembler-times "stack_chk_fail" 10 } } */