LEFT | RIGHT |
1 /* Test that stack protection is done on chosen functions. */ | 1 /* Test that stack protection is done on chosen functions. */ |
2 | 2 |
3 /* { dg-do compile { target i?86-*-* x86_64-*-* } } */ | 3 /* { dg-do compile { target i?86-*-* x86_64-*-* } } */ |
4 /* { dg-options "-O2 -fstack-protector-strong" } */ | 4 /* { dg-options "-O2 -fstack-protector-strong" } */ |
5 | 5 |
6 #include<string.h> | 6 #include<string.h> |
7 #include<stdlib.h> | 7 #include<stdlib.h> |
8 | 8 |
9 extern int g0; | 9 extern int g0; |
10 extern int *pg0; | 10 extern int* pg0; |
11 int goo(int *); | 11 int |
12 int hoo(int); | 12 goo (int *); |
| 13 int |
| 14 hoo (int); |
13 | 15 |
14 /* Function frame address escaped function call. */ | 16 /* Function frame address escaped function call. */ |
15 int foo1() | 17 int |
| 18 foo1 () |
16 { | 19 { |
17 int i; | 20 int i; |
18 return goo(&i); | 21 return goo (&i); |
19 } | 22 } |
20 | 23 |
21 struct ArrayStruct { | 24 struct ArrayStruct |
| 25 { |
22 int a; | 26 int a; |
23 int array[10]; | 27 int array[10]; |
24 }; | 28 }; |
25 | 29 |
26 struct AA { | 30 struct AA |
| 31 { |
27 int b; | 32 int b; |
28 struct ArrayStruct as; | 33 struct ArrayStruct as; |
29 }; | 34 }; |
30 | 35 |
31 /* Function frame contains array. */ | 36 /* Function frame contains array. */ |
32 int foo2() { | 37 int |
| 38 foo2 () |
| 39 { |
33 struct AA aa; | 40 struct AA aa; |
34 int i; | 41 int i; |
35 for (i = 0; i < 10; ++i) { | 42 for (i = 0; i < 10; ++i) |
36 aa.as.array[i] = i * (i-1) + i / 2; | 43 { |
37 } | 44 aa.as.array[i] = i * (i-1) + i / 2; |
| 45 } |
38 return aa.as.array[5]; | 46 return aa.as.array[5]; |
39 } | 47 } |
40 | 48 |
41 /* Address computation based on a function frame address. */ | 49 /* Address computation based on a function frame address. */ |
42 int foo3() { | 50 int |
| 51 foo3 () |
| 52 { |
43 int a; | 53 int a; |
44 int *p; | 54 int *p; |
45 p = &a + 5; | 55 p = &a + 5; |
46 return goo(p); | 56 return goo (p); |
47 } | 57 } |
48 | 58 |
49 /* Address cast based on a function frame address. */ | 59 /* Address cast based on a function frame address. */ |
50 int foo4() { | 60 int |
| 61 foo4 () |
| 62 { |
51 int a; | 63 int a; |
52 return goo(g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0); | 64 return goo (g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0); |
53 | |
54 } | 65 } |
55 | 66 |
56 /* Address cast based on a local array. */ | 67 /* Address cast based on a local array. */ |
57 int foo5() { | 68 int |
| 69 foo5 () |
| 70 { |
58 short array[10]; | 71 short array[10]; |
59 return goo((int *)(array + 5)); | 72 return goo ((int *)(array + 5)); |
60 } | 73 } |
61 | 74 |
62 struct BB { | 75 struct BB |
| 76 { |
63 int one; | 77 int one; |
64 int two; | 78 int two; |
65 int three; | 79 int three; |
66 }; | 80 }; |
67 | 81 |
68 /* Address computaton based on a function frame address.*/ | 82 /* Address computaton based on a function frame address.*/ |
69 int foo6() { | 83 int |
| 84 foo6 () |
| 85 { |
70 struct BB bb; | 86 struct BB bb; |
71 return goo(&bb.one + sizeof(int)); | 87 return goo (&bb.one + sizeof(int)); |
72 } | 88 } |
73 | 89 |
74 /* Function frame address escaped via global variable. */ | 90 /* Function frame address escaped via global variable. */ |
75 int foo7() { | 91 int |
| 92 foo7 () |
| 93 { |
76 int a; | 94 int a; |
77 pg0 = &a; | 95 pg0 = &a; |
78 goo(pg0); | 96 goo (pg0); |
79 return *pg0; | 97 return *pg0; |
80 } | 98 } |
81 | 99 |
82 /* Check that this covers -fstack-protector. */ | 100 /* Check that this covers -fstack-protector. */ |
83 int foo8() { | 101 int |
| 102 foo8 () |
| 103 { |
84 char base[100]; | 104 char base[100]; |
85 memcpy((void *)base, (const void *)pg0, 105); | 105 memcpy ((void *)base, (const void *)pg0, 105); |
86 return (int)(base[32]); | 106 return (int)(base[32]); |
87 } | 107 } |
88 | 108 |
89 /* Check that this covers -fstack-protector. */ | 109 /* Check that this covers -fstack-protector. */ |
90 int foo9() { | 110 int |
91 char* p = alloca(100); | 111 foo9 () |
92 return goo((int *)(p + 50)); | 112 { |
| 113 char* p = alloca (100); |
| 114 return goo ((int *)(p + 50)); |
93 } | 115 } |
94 | 116 |
| 117 int |
| 118 global2 (struct BB* pbb); |
| 119 |
95 /* Address taken on struct. */ | 120 /* Address taken on struct. */ |
96 int foo10() { | 121 int |
| 122 foo10 () |
| 123 { |
97 struct BB bb; | 124 struct BB bb; |
98 int i; | 125 int i; |
99 memset(&bb, 5, sizeof bb); | 126 bb.one = global2 (&bb); |
100 for (i = 0; i < 10; ++i) { | 127 for (i = 0; i < 10; ++i) |
101 bb.one = i; | 128 { |
102 bb.two = bb.one + bb.two; | 129 bb.two = bb.one + bb.two; |
103 bb.three = bb.one + bb.two + bb.three; | 130 bb.three = bb.one + bb.two + bb.three; |
104 } | 131 } |
105 return bb.three; | 132 return bb.three; |
106 } | 133 } |
107 | 134 |
108 /* { dg-final { scan-assembler-times "stack_chk_fail" 10 } } */ | 135 /* { dg-final { scan-assembler-times "stack_chk_fail" 10 } } */ |
109 | |
110 | |
LEFT | RIGHT |