1 /*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gtest/gtest.h>
18
19 #include <stdint.h>
20
21 #include <string>
22
23 static std::string class_with_dtor_output;
24
25 class ClassWithDtor {
26 public:
set_message(const std::string & msg)27 void set_message(const std::string& msg) {
28 message = msg;
29 }
30
~ClassWithDtor()31 ~ClassWithDtor() {
32 class_with_dtor_output += message;
33 }
34 private:
35 std::string message;
36 };
37
38 #if defined(__clang__) && defined(__aarch64__)
39 // b/25642296, aarch64 clang compiled "thread_local" does not link.
40 static ClassWithDtor class_with_dtor;
41 #else
42 static thread_local ClassWithDtor class_with_dtor;
43 #endif
44
thread_nop(void * arg)45 static void* thread_nop(void* arg) {
46 class_with_dtor.set_message(*static_cast<std::string*>(arg));
47 return nullptr;
48 }
49
TEST(thread_local,smoke)50 TEST(thread_local, smoke) {
51 std::string msg("dtor called.");
52 pthread_t t;
53 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_nop, &msg));
54 ASSERT_EQ(0, pthread_join(t, nullptr));
55 #if defined(__clang__) && defined(__aarch64__)
56 GTEST_LOG_(INFO) << "Skipping test, b/25642296, "
57 << "thread_local does not work with aarch64 clang/llvm.\n";
58 #else
59 ASSERT_EQ("dtor called.", class_with_dtor_output);
60 #endif
61 }
62
63 class ClassWithDtorForMainThread {
64 public:
set_message(const std::string & msg)65 void set_message(const std::string& msg) {
66 message = msg;
67 }
68
~ClassWithDtorForMainThread()69 ~ClassWithDtorForMainThread() {
70 fprintf(stderr, "%s", message.c_str());
71 }
72 private:
73 std::string message;
74 };
75
thread_atexit_main()76 static void thread_atexit_main() {
77 #if defined(__clang__) && defined(__aarch64__)
78 static ClassWithDtorForMainThread class_with_dtor_for_main_thread;
79 GTEST_LOG_(INFO) << "Skipping test, b/25642296, "
80 << "thread_local does not work with aarch64 clang/llvm.\n";
81 #else
82 static thread_local ClassWithDtorForMainThread class_with_dtor_for_main_thread;
83 #endif
84 class_with_dtor_for_main_thread.set_message("d-tor for main thread called.");
85 exit(0);
86 }
87
TEST(thread_local,dtor_for_main_thread)88 TEST(thread_local, dtor_for_main_thread) {
89 ASSERT_EXIT(thread_atexit_main(), testing::ExitedWithCode(0), "d-tor for main thread called.");
90 }
91
92 extern "C" int __cxa_thread_atexit_impl(void (*fn)(void*), void* arg, void* dso_handle);
93
thread_atexit_fn1(void * arg)94 static void thread_atexit_fn1(void* arg) {
95 std::string* call_sequence = static_cast<std::string*>(arg);
96 *call_sequence += "one, ";
97 }
98
thread_atexit_fn2(void * arg)99 static void thread_atexit_fn2(void* arg) {
100 std::string* call_sequence = static_cast<std::string*>(arg);
101 *call_sequence += "two, ";
102 }
103
thread_atexit_from_atexit(void * arg)104 static void thread_atexit_from_atexit(void* arg) {
105 std::string* call_sequence = static_cast<std::string*>(arg);
106 *call_sequence += "oops, ";
107 }
108
thread_atexit_fn3(void * arg)109 static void thread_atexit_fn3(void* arg) {
110 __cxa_thread_atexit_impl(thread_atexit_from_atexit, arg, nullptr);
111 std::string* call_sequence = static_cast<std::string*>(arg);
112 *call_sequence += "three, ";
113 }
114
thread_atexit_fn4(void * arg)115 static void thread_atexit_fn4(void* arg) {
116 std::string* call_sequence = static_cast<std::string*>(arg);
117 *call_sequence += "four, ";
118 }
119
thread_atexit_fn5(void * arg)120 static void thread_atexit_fn5(void* arg) {
121 std::string* call_sequence = static_cast<std::string*>(arg);
122 *call_sequence += "five.";
123 }
124
thread_main(void * arg)125 static void* thread_main(void* arg) {
126 __cxa_thread_atexit_impl(thread_atexit_fn5, arg, nullptr);
127 __cxa_thread_atexit_impl(thread_atexit_fn4, arg, nullptr);
128 __cxa_thread_atexit_impl(thread_atexit_fn3, arg, nullptr);
129 __cxa_thread_atexit_impl(thread_atexit_fn2, arg, nullptr);
130 __cxa_thread_atexit_impl(thread_atexit_fn1, arg, nullptr);
131 return nullptr;
132 }
133
TEST(__cxa_thread_atexit_impl,smoke)134 TEST(__cxa_thread_atexit_impl, smoke) {
135 std::string atexit_call_sequence;
136
137 pthread_t t;
138 ASSERT_EQ(0, pthread_create(&t, nullptr, thread_main, &atexit_call_sequence));
139 ASSERT_EQ(0, pthread_join(t, nullptr));
140 ASSERT_EQ("one, two, three, oops, four, five.", atexit_call_sequence);
141 }
142
143
144