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