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