summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gcc.dg/fstack-protector-strong.c
blob: 8e9d891aceaf84b1f06f8fb08b38e20a40d74289 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Test that stack protection is done on chosen functions. */

/* { dg-do compile { target i?86-*-* x86_64-*-* rs6000-*-* s390x-*-* } } */
/* { dg-options "-O2 -fstack-protector-strong" } */

/* This test checks the presence of __stack_chk_fail function in assembler.
 * Compiler generates _stack_chk_fail_local (wrapper) calls instead for PIC.
 */
/* { dg-require-effective-target nonpic } */

#include<string.h>

extern int g0;
extern int* pg0;
int
goo (int *);
int
hoo (int);

/* Function frame address escaped function call. */
int
foo1 ()
{
  int i;
  return goo (&i);
}

struct ArrayStruct
{
  int a;
  int array[10];
};

struct AA
{
  int b;
  struct ArrayStruct as;
};

/* Function frame contains array. */
int
foo2 ()
{
  struct AA aa;
  int i;
  for (i = 0; i < 10; ++i)
    {
      aa.as.array[i] = i * (i-1) + i / 2;
    }
  return aa.as.array[5];
}

/* Address computation based on a function frame address. */
int
foo3 ()
{
  int a;
  int *p;
  p = &a + 5;
  return goo (p);
}

/* Address cast based on a function frame address. */
int
foo4 ()
{
  int a;
  return goo (g0 << 2 ? (int *)(3 * (long)(void *)(&a)) : 0);
}

/* Address cast based on a local array. */
int
foo5 ()
{
  short array[10];
  return goo ((int *)(array + 5));
}

struct BB
{
  int one;
  int two;
  int three;
};

/* Address computaton based on a function frame address.*/
int
foo6 ()
{
  struct BB bb;
  return goo (&bb.one + sizeof(int));
}

/* Function frame address escaped via global variable. */
int
foo7 ()
{
  int a;
  pg0 = &a;
  goo (pg0);
  return *pg0;
}

/* Check that this covers -fstack-protector. */
int
foo8 ()
{
  char base[100];
  memcpy ((void *)base, (const void *)pg0, 105);
  return (int)(base[32]);
}

/* Check that this covers -fstack-protector. */
int
foo9 ()
{
  char* p = __builtin_alloca (100);
  return goo ((int *)(p + 50));
}

int
global2 (struct BB* pbb);

/* Address taken on struct. */
int
foo10 ()
{
  struct BB bb;
  int i;
  bb.one = global2 (&bb);
  for (i = 0; i < 10; ++i)
    {
      bb.two = bb.one + bb.two;
      bb.three = bb.one + bb.two + bb.three;
    }
  return bb.three;
}

struct B
{
  /* Discourage passing this struct in registers. */
  int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
};

struct B global3 (void);

int foo11 ()
{
  return global3 ().a1;
}

void foo12 ()
{
  global3 ();
}

/* { dg-final { scan-assembler-times "stack_chk_fail" 12 } } */