1 /* $NetBSD: h_atexit.c,v 1.1 2011/01/12 19:44:08 pgoyette Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Jason R. Thorpe.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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.
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 $");
42 extern int __cxa_atexit(void (*func)(void *), void *, void *);
43 extern void __cxa_finalize(void *);
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.
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,
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.
64 static void *dso_handle_1 = (void *)1;
65 static void *dso_handle_2 = (void *)2;
66 static void *dso_handle_3 = (void *)3;
68 static int dso_handle_1;
69 static int dso_handle_2;
70 static int dso_handle_3;
77 static int exiting_state;
79 #define ASSERT(expr) \
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); \
89 } while (/*CONSTCOND*/0)
93 write(STDOUT_FILENO, __func__, strlen(__func__)); \
94 write(STDOUT_FILENO, "\n", 1); \
95 } while (/*CONSTCOND*/0)
101 kill(getpid(), SIGABRT);
106 cxa_handler_5(void *arg)
108 static int cxa_handler_5_called;
110 ASSERT(arg == (void *)&arg_1);
111 ASSERT(cxa_handler_5_called == 0);
112 ASSERT(exiting_state == 5);
115 cxa_handler_5_called = 1;
120 cxa_handler_4(void *arg)
122 static int cxa_handler_4_called;
124 ASSERT(arg == (void *)&arg_1);
125 ASSERT(cxa_handler_4_called == 0);
126 ASSERT(exiting_state == 4);
129 cxa_handler_4_called = 1;
134 cxa_handler_3(void *arg)
136 static int cxa_handler_3_called;
138 ASSERT(arg == (void *)&arg_2);
139 ASSERT(cxa_handler_3_called == 0);
140 ASSERT(exiting_state == 3);
143 cxa_handler_3_called = 1;
148 cxa_handler_2(void *arg)
150 static int cxa_handler_2_called;
152 ASSERT(arg == (void *)&arg_3);
153 ASSERT(cxa_handler_2_called == 0);
154 ASSERT(exiting_state == 2);
157 cxa_handler_2_called = 1;
162 normal_handler_1(void)
164 static int normal_handler_1_called;
166 ASSERT(normal_handler_1_called == 0);
167 ASSERT(exiting_state == 1);
170 normal_handler_1_called = 1;
175 normal_handler_0(void)
177 static int normal_handler_0_called;
179 ASSERT(normal_handler_0_called == 0);
180 ASSERT(exiting_state == 0);
182 normal_handler_0_called = 1;
187 main(int argc, char *argv[])
192 ASSERT(0 == atexit(normal_handler_0));
193 ASSERT(0 == atexit(normal_handler_1));
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));
200 __cxa_finalize(dso_handle_1);
201 __cxa_finalize(dso_handle_2);
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));
208 __cxa_finalize(&dso_handle_1);
209 __cxa_finalize(&dso_handle_2);