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