xref: /freebsd/lib/libc/tests/stdlib/cxa_thread_atexit_test.cc (revision e6bfd18d21b225af6a0ed67ceeaf1293b7b9eba5)
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 __FBSDID("$FreeBSD$");
29 
30 #include <dlfcn.h>
31 #include <atf-c++.hpp>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <thread>
35 
36 static FILE *output = NULL;
37 
38 struct Foo {
39 	Foo() { ATF_REQUIRE(fprintf(output, "Created\n") > 0); }
40 	~Foo() { ATF_REQUIRE(fprintf(output, "Destroyed\n") > 0); }
41 	void use() { ATF_REQUIRE(fprintf(output, "Used\n") > 0); }
42 };
43 
44 struct Bar {
45 	Bar() {}
46 	~Bar() {
47 		thread_local static Foo foo;
48 		ATF_REQUIRE(fprintf(output, "DIED\n") > 0);
49 	}
50 	void use() {}
51 };
52 
53 extern "C" int __cxa_thread_atexit(void (*)(void *), void *, void *);
54 
55 static void
56 again(void *arg)
57 {
58 
59 	__cxa_thread_atexit(again, arg, &output);
60 }
61 
62 struct Baz {
63 	Baz() {}
64 	~Baz() {
65 		again(NULL);
66 	}
67 	void use() {}
68 };
69 
70 static thread_local Foo f;
71 static thread_local Foo g;
72 static thread_local Bar h;
73 static thread_local Baz e;
74 
75 /*
76  * This test must be linked to libpthread.
77  */
78 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thr);
79 ATF_TEST_CASE_BODY(cxx__thr)
80 {
81 	void *libthr_handle;
82 
83 	/* Avoid coredump during f construction. */
84 	output = stderr;
85 
86 	libthr_handle = dlopen("libthr.so.3", RTLD_LAZY | RTLD_GLOBAL |
87 	    RTLD_NOLOAD);
88 	ATF_REQUIRE(libthr_handle != NULL);
89 	dlclose(libthr_handle);
90 }
91 
92 /*
93  * In this test f.use() will test cxa_thread_atexit() in non-threaded mode.
94  * After f.use() main will be threaded and we'll have one additional thread
95  * with its own TLS data.
96  */
97 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_before);
98 ATF_TEST_CASE_BODY(cxx__thread_local_before)
99 {
100 	static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
101 	    "Created\nUsed\nCreated\nDIED\nDestroyed\nDestroyed\nDestroyed\n";
102 
103 	ATF_REQUIRE((output = fopen("test_before.txt", "w")) != NULL);
104 
105 	f.use();
106 	std::thread t([]() { f.use(); });
107 	t.join();
108 
109 	fflush(output);
110 
111 	ATF_REQUIRE(atf::utils::compare_file("test_before.txt", out_log));
112 }
113 
114 /*
115  * In this test, f.use() will test __cxa_thread_atexit()
116  * in threaded mode (but still in main-threaed).
117  */
118 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_after);
119 ATF_TEST_CASE_BODY(cxx__thread_local_after)
120 {
121 	static const char out_log[] = "Created\nCreated\nUsed\nCreated\n"
122 	    "DIED\nDestroyed\nDestroyed\nDestroyed\nCreated\nCreated\nUsed\n";
123 
124 	ATF_REQUIRE((output = fopen("test_after.txt", "w")) != NULL);
125 
126 	std::thread t([]() { g.use(); });
127 	t.join();
128 	sleep(1);
129 	g.use();
130 
131 	fflush(output);
132 
133 	ATF_REQUIRE(atf::utils::compare_file("test_after.txt", out_log));
134 }
135 
136 /*
137  * In this test, we register a new dtor while dtors are being run
138  * in __cxa_thread_atexit().
139  */
140 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_local_add_while_calling_dtors);
141 ATF_TEST_CASE_BODY(cxx__thread_local_add_while_calling_dtors)
142 {
143 	static const char out_log[] = "Created\nCreated\nCreated\nDIED\n"
144 	    "Destroyed\nDestroyed\nDestroyed\n";
145 
146 	ATF_REQUIRE((output = fopen("test_add_meanwhile.txt", "w")) != NULL);
147 
148 	std::thread t([]() { h.use(); });
149 	t.join();
150 	sleep(1);
151 
152 	fflush(output);
153 
154 	ATF_REQUIRE(atf::utils::compare_file("test_add_meanwhile.txt", out_log));
155 }
156 
157 ATF_TEST_CASE_WITHOUT_HEAD(cxx__thread_inf_dtors);
158 ATF_TEST_CASE_BODY(cxx__thread_inf_dtors)
159 {
160 
161 	/*
162 	 * Only added to make isolated run of this test not
163 	 * coredumping.  Construction of Foo objects require filled
164 	 * output.
165 	 */
166 	output = stderr;
167 
168 	std::thread t([]() { e.use(); });
169 	t.join();
170 }
171 
172 ATF_INIT_TEST_CASES(tcs)
173 {
174 
175 	ATF_ADD_TEST_CASE(tcs, cxx__thr);
176 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_before);
177 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_after);
178 	ATF_ADD_TEST_CASE(tcs, cxx__thread_local_add_while_calling_dtors);
179 	ATF_ADD_TEST_CASE(tcs, cxx__thread_inf_dtors);
180 }
181