Debian Codegraph @

gcc/testsuite/g++.dg/ext/cleanup-11.C

raw
1 /* { dg-do run { target hppa*-*-hpux* *-*-linux* *-*-gnu* powerpc*-*-darwin* *-*-darwin[912]* *-*-uclinux* } } */
2 /* { dg-options "-fexceptions -fnon-call-exceptions -O2" } */
3 /* { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } */
4 /* Verify that cleanups work with exception handling through realtime signal
5 frames on alternate stack. */
6
7 #include <unwind.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 static _Unwind_Reason_Code
14 force_unwind_stop (int version, _Unwind_Action actions,
15 _Unwind_Exception_Class exc_class,
16 struct _Unwind_Exception *exc_obj,
17 struct _Unwind_Context *context,
18 void *stop_parameter)
19 {
20 if (actions & _UA_END_OF_STACK)
21 abort ();
22 return _URC_NO_REASON;
23 }
24
25 static void force_unwind ()
26 {
27 struct _Unwind_Exception *exc
28 = (struct _Unwind_Exception*) malloc (sizeof (*exc));
29 memset (&exc->exception_class, 0, sizeof (exc->exception_class));
30 exc->exception_cleanup = 0;
31
32 #ifndef __USING_SJLJ_EXCEPTIONS__
33 _Unwind_ForcedUnwind (exc, force_unwind_stop, 0);
34 #else
35 _Unwind_SjLj_ForcedUnwind (exc, force_unwind_stop, 0);
36 #endif
37
38 abort ();
39 }
40
41 int count;
42 char *null;
43
44 static void counter (void *p __attribute__((unused)))
45 {
46 ++count;
47 }
48
49 static void handler (void *p __attribute__((unused)))
50 {
51 if (count != 2)
52 abort ();
53 exit (0);
54 }
55
56 static int __attribute__((noinline)) fn5 ()
57 {
58 char dummy __attribute__((cleanup (counter)));
59 force_unwind ();
60 return 0;
61 }
62
63 static void fn4 (int sig, siginfo_t *info, void *ctx)
64 {
65 char dummy __attribute__((cleanup (counter)));
66 fn5 ();
67 null = NULL;
68 }
69
70 static void fn3 ()
71 {
72 abort ();
73 }
74
75 static int __attribute__((noinline)) fn2 ()
76 {
77 *null = 0;
78 fn3 ();
79 return 0;
80 }
81
82 static int __attribute__((noinline)) fn1 ()
83 {
84 stack_t ss;
85 struct sigaction s;
86
87 ss.ss_size = 4 * sysconf (_SC_PAGESIZE);
88 if (ss.ss_size < SIGSTKSZ)
89 ss.ss_size = SIGSTKSZ;
90 ss.ss_sp = malloc (ss.ss_size);
91 if (ss.ss_sp == NULL)
92 exit (1);
93 ss.ss_flags = 0;
94 if (sigaltstack (&ss, NULL) < 0)
95 exit (1);
96
97 sigemptyset (&s.sa_mask);
98 s.sa_sigaction = fn4;
99 s.sa_flags = SA_RESETHAND | SA_ONSTACK | SA_SIGINFO;
100 sigaction (SIGSEGV, &s, NULL);
101 sigaction (SIGBUS, &s, NULL);
102 fn2 ();
103 return 0;
104 }
105
106 static int __attribute__((noinline)) fn0 ()
107 {
108 char dummy __attribute__((cleanup (handler)));
109 fn1 ();
110 null = 0;
111 return 0;
112 }
113
114 int main()
115 {
116 fn0 ();
117 abort ();
118 }