Debian Codegraph @

gcc/testsuite/g++.dg/ext/cleanup-9.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
5 signal frames. */
6
7 #include <unwind.h>
8 #include <stdlib.h>
9 #include <signal.h>
10 #include <string.h>
11
12 static _Unwind_Reason_Code
13 force_unwind_stop (int version, _Unwind_Action actions,
14 _Unwind_Exception_Class exc_class,
15 struct _Unwind_Exception *exc_obj,
16 struct _Unwind_Context *context,
17 void *stop_parameter)
18 {
19 if (actions & _UA_END_OF_STACK)
20 abort ();
21 return _URC_NO_REASON;
22 }
23
24 static void force_unwind ()
25 {
26 struct _Unwind_Exception *exc
27 = (struct _Unwind_Exception *) malloc (sizeof (*exc));
28 memset (&exc->exception_class, 0, sizeof (exc->exception_class));
29 exc->exception_cleanup = 0;
30
31 #ifndef __USING_SJLJ_EXCEPTIONS__
32 _Unwind_ForcedUnwind (exc, force_unwind_stop, 0);
33 #else
34 _Unwind_SjLj_ForcedUnwind (exc, force_unwind_stop, 0);
35 #endif
36
37 abort ();
38 }
39
40 int count;
41 char *null;
42
43 static void counter (void *p __attribute__((unused)))
44 {
45 ++count;
46 }
47
48 static void handler (void *p __attribute__((unused)))
49 {
50 if (count != 2)
51 abort ();
52 exit (0);
53 }
54
55 static int __attribute__((noinline)) fn5 ()
56 {
57 char dummy __attribute__((cleanup (counter)));
58 force_unwind ();
59 return 0;
60 }
61
62 static void fn4 (int sig, siginfo_t *info, void *ctx)
63 {
64 char dummy __attribute__((cleanup (counter)));
65 fn5 ();
66 null = NULL;
67 }
68
69 static void fn3 ()
70 {
71 abort ();
72 }
73
74 static int __attribute__((noinline)) fn2 ()
75 {
76 *null = 0;
77 fn3 ();
78 return 0;
79 }
80
81 static int __attribute__((noinline)) fn1 ()
82 {
83 struct sigaction s;
84 sigemptyset (&s.sa_mask);
85 s.sa_sigaction = fn4;
86 s.sa_flags = SA_RESETHAND | SA_SIGINFO;
87 sigaction (SIGSEGV, &s, NULL);
88 sigaction (SIGBUS, &s, NULL);
89 fn2 ();
90 return 0;
91 }
92
93 static int __attribute__((noinline)) fn0 ()
94 {
95 char dummy __attribute__((cleanup (handler)));
96 fn1 ();
97 null = 0;
98 return 0;
99 }
100
101 int main()
102 {
103 fn0 ();
104 abort ();
105 }