| 1 |
/* Test that stack protection is done on chosen functions. */ |
| 2 |
|
| 3 |
/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */ |
| 4 |
/* { dg-options "-O2 -fstack-protector-strong" } */ |
| 5 |
|
| 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 } */ |
| 10 |
|
| 11 |
#include<string.h> |
| 12 |
|
| 13 |
extern int g0; |
| 14 |
extern int* pg0; |
| 15 |
int |
| 16 |
goo (int *); |
| 17 |
int |
| 18 |
hoo (int); |
| 19 |
|
| 20 |
/* Function frame address escaped function call. */ |
| 21 |
int |
| 22 |
foo1 () |
| 23 |
{ |
| 24 |
int i; |
| 25 |
return goo (&i); |
| 26 |
} |
| 27 |
|
| 28 |
struct ArrayStruct |
| 29 |
{ |
| 30 |
int a; |
| 31 |
int array[10]; |
| 32 |
}; |
| 33 |
|
| 34 |
struct AA |
| 35 |
{ |
| 36 |
int b; |
| 37 |
struct ArrayStruct as; |
| 38 |
}; |
| 39 |
|
| 40 |
/* Function frame contains array. */ |
| 41 |
int |
| 42 |
foo2 () |
| 43 |
{ |
| 44 |
struct AA aa; |
| 45 |
int i; |
| 46 |
#pragma GCC novector |
| 47 |
#pragma GCC unroll 0 |
| 48 |
for (i = 0; i < 10; ++i) |
| 49 |
{ |
| 50 |
aa.as.array[i] = i * (i-1) + i / 2; |
| 51 |
} |
| 52 |
return aa.as.array[5]; |
| 53 |
} |
| 54 |
|
| 55 |
/* Address computation based on a function frame address. */ |
| 56 |
int |
| 57 |
foo3 () |
| 58 |
{ |
| 59 |
int a; |
| 60 |
int *p; |
| 61 |
p = &a + 5; |
| 62 |
return goo (p); |
| 63 |
} |
| 64 |
|
| 65 |
/* Address cast based on a function frame address. */ |
| 66 |
int |
| 67 |
foo4 () |
| 68 |
{ |
| 69 |
int a; |
| 70 |
return goo (g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0); |
| 71 |
} |
| 72 |
|
| 73 |
/* Address cast based on a local array. */ |
| 74 |
int |
| 75 |
foo5 () |
| 76 |
{ |
| 77 |
short array[10]; |
| 78 |
return goo ((int *)(array + 5)); |
| 79 |
} |
| 80 |
|
| 81 |
struct BB |
| 82 |
{ |
| 83 |
int one; |
| 84 |
int two; |
| 85 |
int three; |
| 86 |
}; |
| 87 |
|
| 88 |
/* Address computaton based on a function frame address.*/ |
| 89 |
int |
| 90 |
foo6 () |
| 91 |
{ |
| 92 |
struct BB bb; |
| 93 |
return goo (&bb.one + sizeof(int)); |
| 94 |
} |
| 95 |
|
| 96 |
/* Function frame address escaped via global variable. */ |
| 97 |
int |
| 98 |
foo7 () |
| 99 |
{ |
| 100 |
int a; |
| 101 |
pg0 = &a; |
| 102 |
goo (pg0); |
| 103 |
return *pg0; |
| 104 |
} |
| 105 |
|
| 106 |
/* Check that this covers -fstack-protector. */ |
| 107 |
int |
| 108 |
foo8 () |
| 109 |
{ |
| 110 |
char base[100]; |
| 111 |
memcpy ((void *)base, (const void *)pg0, 105); /* { dg-warning "writing 105 bytes into a region of size 100" } */ |
| 112 |
return (int)(base[32]); |
| 113 |
} |
| 114 |
|
| 115 |
/* Check that this covers -fstack-protector. */ |
| 116 |
int |
| 117 |
foo9 () |
| 118 |
{ |
| 119 |
char* p = __builtin_alloca (100); |
| 120 |
return goo ((int *)(p + 50)); |
| 121 |
} |
| 122 |
|
| 123 |
int |
| 124 |
global2 (struct BB* pbb); |
| 125 |
|
| 126 |
/* Address taken on struct. */ |
| 127 |
int |
| 128 |
foo10 () |
| 129 |
{ |
| 130 |
struct BB bb; |
| 131 |
int i; |
| 132 |
bb.one = global2 (&bb); |
| 133 |
for (i = 0; i < 10; ++i) |
| 134 |
{ |
| 135 |
bb.two = bb.one + bb.two; |
| 136 |
bb.three = bb.one + bb.two + bb.three; |
| 137 |
} |
| 138 |
return bb.three; |
| 139 |
} |
| 140 |
|
| 141 |
struct B |
| 142 |
{ |
| 143 |
/* Discourage passing this struct in registers. */ |
| 144 |
int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10; |
| 145 |
}; |
| 146 |
|
| 147 |
struct B global3 (void); |
| 148 |
|
| 149 |
int foo11 () |
| 150 |
{ |
| 151 |
return global3 ().a1; |
| 152 |
} |
| 153 |
|
| 154 |
void foo12 () |
| 155 |
{ |
| 156 |
global3 (); |
| 157 |
} |
| 158 |
|
| 159 |
/* { dg-final { scan-assembler-times "stack_chk_fail" 12 } } */ |