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