PR tree-optimization/81303
[official-gcc.git] / gcc / testsuite / gcc.dg / fstack-protector-strong.c
blob94dc3508f1adb46678271e466c2f566e8a34a8f5
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 /* This test checks the presence of __stack_chk_fail function in assembler.
7 * Compiler generates _stack_chk_fail_local (wrapper) calls instead for PIC.
8 */
9 /* { dg-require-effective-target nonpic } */
11 #include<string.h>
13 extern int g0;
14 extern int* pg0;
15 int
16 goo (int *);
17 int
18 hoo (int);
20 /* Function frame address escaped function call. */
21 int
22 foo1 ()
24 int i;
25 return goo (&i);
28 struct ArrayStruct
30 int a;
31 int array[10];
34 struct AA
36 int b;
37 struct ArrayStruct as;
40 /* Function frame contains array. */
41 int
42 foo2 ()
44 struct AA aa;
45 int i;
46 for (i = 0; i < 10; ++i)
48 aa.as.array[i] = i * (i-1) + i / 2;
50 return aa.as.array[5];
53 /* Address computation based on a function frame address. */
54 int
55 foo3 ()
57 int a;
58 int *p;
59 p = &a + 5;
60 return goo (p);
63 /* Address cast based on a function frame address. */
64 int
65 foo4 ()
67 int a;
68 return goo (g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0);
71 /* Address cast based on a local array. */
72 int
73 foo5 ()
75 short array[10];
76 return goo ((int *)(array + 5));
79 struct BB
81 int one;
82 int two;
83 int three;
86 /* Address computaton based on a function frame address.*/
87 int
88 foo6 ()
90 struct BB bb;
91 return goo (&bb.one + sizeof(int));
94 /* Function frame address escaped via global variable. */
95 int
96 foo7 ()
98 int a;
99 pg0 = &a;
100 goo (pg0);
101 return *pg0;
104 /* Check that this covers -fstack-protector. */
106 foo8 ()
108 char base[100];
109 memcpy ((void *)base, (const void *)pg0, 105); /* { dg-warning "writing 105 bytes into a region of size 100" } */
110 return (int)(base[32]);
113 /* Check that this covers -fstack-protector. */
115 foo9 ()
117 char* p = __builtin_alloca (100);
118 return goo ((int *)(p + 50));
122 global2 (struct BB* pbb);
124 /* Address taken on struct. */
126 foo10 ()
128 struct BB bb;
129 int i;
130 bb.one = global2 (&bb);
131 for (i = 0; i < 10; ++i)
133 bb.two = bb.one + bb.two;
134 bb.three = bb.one + bb.two + bb.three;
136 return bb.three;
139 struct B
141 /* Discourage passing this struct in registers. */
142 int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
145 struct B global3 (void);
147 int foo11 ()
149 return global3 ().a1;
152 void foo12 ()
154 global3 ();
157 /* { dg-final { scan-assembler-times "stack_chk_fail" 12 } } */