1 /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code was contributed to The NetBSD Foundation by Jason R. Thorpe.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/cdefs.h>
32 __COPYRIGHT("@(#) Copyright (c) 2011\
33 The NetBSD Foundation, inc. All rights reserved.");
34 __RCSID("$NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $");
35
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 extern int __cxa_atexit(void (*func)(void *), void *, void *);
43 extern void __cxa_finalize(void *);
44
45 #ifdef __FreeBSD__
46 /*
47 * On shared object unload, in __cxa_finalize, call and clear all installed
48 * atexit and __cxa_atexit handlers that are either installed by unloaded
49 * dso, or points to the functions provided by the dso.
50 *
51 * The reason of the change is to ensure that there is no lingering pointers
52 * to the unloaded code after the dlclose(3). It is known reason for infinite
53 * stream of the crash reports for many programs which use loadable modules
54 * and fail to properly clean on module unload. Examples are apache, php,
55 * perl etc.
56 *
57 * You pass the &dso_handle_1 and &dso_handle_2, which points inside the
58 * main binary, to the registration function. The code from r211706,
59 * correctly detects that they are for the main binary, and on the first
60 * call to __cxa_finalize(), which also pass pointer to main binary, all
61 * registered functions from the main binary are executed.
62 */
63
64 static void *dso_handle_1 = (void *)1;
65 static void *dso_handle_2 = (void *)2;
66 static void *dso_handle_3 = (void *)3;
67 #else
68 static int dso_handle_1;
69 static int dso_handle_2;
70 static int dso_handle_3;
71 #endif
72
73 static int arg_1;
74 static int arg_2;
75 static int arg_3;
76
77 static int exiting_state;
78
79 #define ASSERT(expr) \
80 do { \
81 if ((expr) == 0) { \
82 write(STDERR_FILENO, __func__, strlen(__func__)); \
83 write(STDERR_FILENO, ": ", 2); \
84 write(STDERR_FILENO, __STRING(expr), \
85 strlen(__STRING(expr))); \
86 write(STDERR_FILENO, "\n", 1); \
87 my_abort(); \
88 } \
89 } while (/*CONSTCOND*/0)
90
91 #define SUCCESS() \
92 do { \
93 write(STDOUT_FILENO, __func__, strlen(__func__)); \
94 write(STDOUT_FILENO, "\n", 1); \
95 } while (/*CONSTCOND*/0)
96
97 static void
my_abort(void)98 my_abort(void)
99 {
100
101 kill(getpid(), SIGABRT);
102 /* NOTREACHED */
103 }
104
105 static void
cxa_handler_5(void * arg)106 cxa_handler_5(void *arg)
107 {
108 static int cxa_handler_5_called;
109
110 ASSERT(arg == (void *)&arg_1);
111 ASSERT(cxa_handler_5_called == 0);
112 ASSERT(exiting_state == 5);
113
114 exiting_state--;
115 cxa_handler_5_called = 1;
116 SUCCESS();
117 }
118
119 static void
cxa_handler_4(void * arg)120 cxa_handler_4(void *arg)
121 {
122 static int cxa_handler_4_called;
123
124 ASSERT(arg == (void *)&arg_1);
125 ASSERT(cxa_handler_4_called == 0);
126 ASSERT(exiting_state == 4);
127
128 exiting_state--;
129 cxa_handler_4_called = 1;
130 SUCCESS();
131 }
132
133 static void
cxa_handler_3(void * arg)134 cxa_handler_3(void *arg)
135 {
136 static int cxa_handler_3_called;
137
138 ASSERT(arg == (void *)&arg_2);
139 ASSERT(cxa_handler_3_called == 0);
140 ASSERT(exiting_state == 3);
141
142 exiting_state--;
143 cxa_handler_3_called = 1;
144 SUCCESS();
145 }
146
147 static void
cxa_handler_2(void * arg)148 cxa_handler_2(void *arg)
149 {
150 static int cxa_handler_2_called;
151
152 ASSERT(arg == (void *)&arg_3);
153 ASSERT(cxa_handler_2_called == 0);
154 ASSERT(exiting_state == 2);
155
156 exiting_state--;
157 cxa_handler_2_called = 1;
158 SUCCESS();
159 }
160
161 static void
normal_handler_1(void)162 normal_handler_1(void)
163 {
164 static int normal_handler_1_called;
165
166 ASSERT(normal_handler_1_called == 0);
167 ASSERT(exiting_state == 1);
168
169 exiting_state--;
170 normal_handler_1_called = 1;
171 SUCCESS();
172 }
173
174 static void
normal_handler_0(void)175 normal_handler_0(void)
176 {
177 static int normal_handler_0_called;
178
179 ASSERT(normal_handler_0_called == 0);
180 ASSERT(exiting_state == 0);
181
182 normal_handler_0_called = 1;
183 SUCCESS();
184 }
185
186 int
main(int argc,char * argv[])187 main(int argc, char *argv[])
188 {
189
190 exiting_state = 5;
191
192 ASSERT(0 == atexit(normal_handler_0));
193 ASSERT(0 == atexit(normal_handler_1));
194 #ifdef __FreeBSD__
195 ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, dso_handle_1));
196 ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, dso_handle_1));
197 ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, dso_handle_2));
198 ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, dso_handle_3));
199
200 __cxa_finalize(dso_handle_1);
201 __cxa_finalize(dso_handle_2);
202 #else
203 ASSERT(0 == __cxa_atexit(cxa_handler_4, &arg_1, &dso_handle_1));
204 ASSERT(0 == __cxa_atexit(cxa_handler_5, &arg_1, &dso_handle_1));
205 ASSERT(0 == __cxa_atexit(cxa_handler_3, &arg_2, &dso_handle_2));
206 ASSERT(0 == __cxa_atexit(cxa_handler_2, &arg_3, &dso_handle_3));
207
208 __cxa_finalize(&dso_handle_1);
209 __cxa_finalize(&dso_handle_2);
210 #endif
211 exit(0);
212 }
213