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