xref: /freebsd/lib/libc/tests/stdlib/cxa_thread_atexit_test.cc (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
1 /*-
2  * Copyright (c) 2016 Mahdi Mokhtari <mokhi64@gmail.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 #include <dlfcn.h>
29 #include <atf-c++.hpp>
30 #include <cstdio>
31 #include <cstdlib>
32 #include <thread>
33 
34 static FILE *output = NULL;
35 
36 struct Foo {
37 	Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); }
38 	~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); }
39 	void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); }
40 };
41 
42 struct Bar {
43 	Bar() {}
44 	~Bar() {
45 		thread_local static Foo foo;
46 		ATF_REQUIRE(fprintf(output, "DIED\n") > 0);
47 	}
48 	void use() {}
49 };
50 
51 extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *);
52 
53 static void
54 again(void *arg)
55 {
56 
57 	__cxa_thread_atexit(again, arg, &output);
58 }
59 
60 struct Baz {
61 	Baz() {}
62 	~Baz() {
63 		again(NULL);
64 	}
65 	void use() {}
66 };
67 
68 static thread_local Foo f;
69 static thread_local Foo g;
70 static thread_local Bar h;
71 static thread_local Baz e;
72 
73 /*
74  * This test must be linked to libpthread.
75  */
76 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thr);
77 ATF_TEST_CASE_BODY(cxx__thr)
78 {
79 	void *libthr_handle;
80 
81 	/* Avoid coredump during f construction. */
82 	output = stderr;
83 
84 	libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL |
85 	    RTLD_NOLOAD);
86 	ATF_REQUIRE(libthr_handle != NULL);
87 	dlclose(libthr_handle);
88 }
89 
90 /*
91  * In this test f.use() will test cxa_thread_atexit() in non-threaded mode.
92  * After f.use() main will be threaded and we'll have one additional thread
93  * with its own TLS data.
94  */
95 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_before);
96 ATF_TEST_CASE_BODY(cxx__thread_local_before)
97 {
98 	static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
99 	    "Created\nUsed\nCreated\nDIED\nDestroyed\nDestroyed\nDestroyed\n";
100 
101 	ATF_REQUIRE((output = fopen("test_before.txt", "w")) != NULL);
102 
103 	f.use();
104 	std::thread t([]() { f.use(); });
105 	t.join();
106 
107 	fflush(output);
108 
109 	ATF_REQUIRE(atf::utils::compare_file("test_before.txt", out_log));
110 }
111 
112 /*
113  * In this test, f.use() will test __cxa_thread_atexit()
114  * in threaded mode (but still in main-threaed).
115  */
116 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_after);
117 ATF_TEST_CASE_BODY(cxx__thread_local_after)
118 {
119 	static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
120 	    "DIED\nDestroyed\nDestroyed\nDestroyed\nCreated\nCreated\nUsed\n";
121 
122 	ATF_REQUIRE((output = fopen("test_after.txt", "w")) != NULL);
123 
124 	std::thread t([]() { g.use(); });
125 	t.join();
126 	sleep(1);
127 	g.use();
128 
129 	fflush(output);
130 
131 	ATF_REQUIRE(atf::utils::compare_file("test_after.txt", out_log));
132 }
133 
134 /*
135  * In this test, we register a new dtor while dtors are being run
136  * in __cxa_thread_atexit().
137  */
138 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_add_while_calling_dtors);
139 ATF_TEST_CASE_BODY(cxx__thread_local_add_while_calling_dtors)
140 {
141 	static const char out_log[] = "Created\nCreated\nCreated\nDIED\n"
142 	    "Destroyed\nDestroyed\nDestroyed\n";
143 
144 	ATF_REQUIRE((output = fopen("test_add_meanwhile.txt", "w")) != NULL);
145 
146 	std::thread t([]() { h.use(); });
147 	t.join();
148 	sleep(1);
149 
150 	fflush(output);
151 
152 	ATF_REQUIRE(atf::utils::compare_file("test_add_meanwhile.txt", out_log));
153 }
154 
155 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors);
156 ATF_TEST_CASE_BODY(cxx__thread_inf_dtors)
157 {
158 
159 	/*
160 	 * Only added to make isolated run of this test not
161 	 * coredumping.  Construction of Foo objects require filled
162 	 * output.
163 	 */
164 	output = stderr;
165 
166 	std::thread t([]() { e.use(); });
167 	t.join();
168 }
169 
170 ATF_INIT_TEST_CASES(tcs)
171 {
172 
173 	ATF_ADD_TEST_CASE(tcs, cxx__thr);
174 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_before);
175 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_after);
176 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_add_while_calling_dtors);
177 	ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors);
178 }
179