1/* Test that stack protection is done on chosen functions. */
2
3/* { dg-do compile { target i?86-*-* x86_64-*-* } } */
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
11class A
12{
13public:
14  A() {}
15  ~A() {}
16  void method();
17  int state;
18};
19
20/* Frame address exposed to A::method via "this". */
21int
22foo1 ()
23{
24  A a;
25  a.method ();
26  return a.state;
27}
28
29/* Possible destroying foo2's stack via &a. */
30int
31global_func (A& a);
32
33/* Frame address exposed to global_func. */
34int foo2 ()
35{
36  A a;
37  return global_func (a);
38}
39
40/* Frame addressed exposed through return slot. */
41
42struct B
43{
44  /* Discourage passing this struct in registers. */
45  int a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
46  int method ();
47  B return_slot();
48};
49
50B global_func ();
51void noop ();
52
53int foo3 ()
54{
55  return global_func ().a1;
56}
57
58int foo4 ()
59{
60  try {
61    noop ();
62    return 0;
63  } catch (...) {
64    return global_func ().a1;
65  }
66}
67
68int foo5 ()
69{
70  try {
71    return global_func ().a1;
72  } catch (...) {
73    return 0;
74  }
75}
76
77int foo6 ()
78{
79  B b;
80  return b.method ();
81}
82
83int foo7 (B *p)
84{
85  return p->return_slot ().a1;
86}
87
88/* { dg-final { scan-assembler-times "stack_chk_fail" 7 } } */
89