1 //===----------------------- cxa_thread_atexit.cpp ------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "abort_message.h"
10 #include "cxxabi.h"
11 #include <__threading_support>
12 #ifndef _LIBCXXABI_HAS_NO_THREADS
13 #if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
14 #pragma comment(lib, "pthread")
15 #endif
16 #endif
17 
18 #include <stdlib.h>
19 
20 namespace __cxxabiv1 {
21 
22   using Dtor = void(*)(void*);
23 
24   extern "C"
25 #ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
26   // A weak symbol is used to detect this function's presence in the C library
27   // at runtime, even if libc++ is built against an older libc
28   _LIBCXXABI_WEAK
29 #endif
30   int __cxa_thread_atexit_impl(Dtor, void*, void*);
31 
32 #ifndef HAVE___CXA_THREAD_ATEXIT_IMPL
33 
34 namespace {
35   // This implementation is used if the C library does not provide
36   // __cxa_thread_atexit_impl() for us.  It has a number of limitations that are
37   // difficult to impossible to address without ..._impl():
38   //
39   // - dso_symbol is ignored.  This means that a shared library may be unloaded
40   //   (via dlclose()) before its thread_local destructors have run.
41   //
42   // - thread_local destructors for the main thread are run by the destructor of
43   //   a static object.  This is later than expected; they should run before the
44   //   destructors of any objects with static storage duration.
45   //
46   // - thread_local destructors on non-main threads run on the first iteration
47   //   through the __libccpp_tls_key destructors.
48   //   std::notify_all_at_thread_exit() and similar functions must be careful to
49   //   wait until the second iteration to provide their intended ordering
50   //   guarantees.
51   //
52   // Another limitation, though one shared with ..._impl(), is that any
53   // thread_locals that are first initialized after non-thread_local global
54   // destructors begin to run will not be destroyed.  [basic.start.term] states
55   // that all thread_local destructors are sequenced before the destruction of
56   // objects with static storage duration, resulting in a contradiction if a
57   // thread_local is constructed after that point.  Thus we consider such
58   // programs ill-formed, and don't bother to run those destructors.  (If the
59   // program terminates abnormally after such a thread_local is constructed,
60   // the destructor is not expected to run and thus there is no contradiction.
61   // So construction still has to work.)
62 
63   struct DtorList {
64     Dtor dtor;
65     void* obj;
66     DtorList* next;
67   };
68 
69   // The linked list of thread-local destructors to run
70   __thread DtorList* dtors = nullptr;
71   // True if the destructors are currently scheduled to run on this thread
72   __thread bool dtors_alive = false;
73   // Used to trigger destructors on thread exit; value is ignored
74   std::__libcpp_tls_key dtors_key;
75 
76   void run_dtors(void*) {
77     while (auto head = dtors) {
78       dtors = head->next;
79       head->dtor(head->obj);
80       ::free(head);
81     }
82 
83     dtors_alive = false;
84   }
85 
86   struct DtorsManager {
87     DtorsManager() {
88       // There is intentionally no matching std::__libcpp_tls_delete call, as
89       // __cxa_thread_atexit() may be called arbitrarily late (for example, from
90       // global destructors or atexit() handlers).
91       if (std::__libcpp_tls_create(&dtors_key, run_dtors) != 0) {
92         abort_message("std::__libcpp_tls_create() failed in __cxa_thread_atexit()");
93       }
94     }
95 
96     ~DtorsManager() {
97       // std::__libcpp_tls_key destructors do not run on threads that call exit()
98       // (including when the main thread returns from main()), so we explicitly
99       // call the destructor here.  This runs at exit time (potentially earlier
100       // if libc++abi is dlclose()'d).  Any thread_locals initialized after this
101       // point will not be destroyed.
102       run_dtors(nullptr);
103     }
104   };
105 } // namespace
106 
107 #endif // HAVE___CXA_THREAD_ATEXIT_IMPL
108 
109 extern "C" {
110 
111   _LIBCXXABI_FUNC_VIS int __cxa_thread_atexit(Dtor dtor, void* obj, void* dso_symbol) throw() {
112 #ifdef HAVE___CXA_THREAD_ATEXIT_IMPL
113     return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
114 #else
115     if (__cxa_thread_atexit_impl) {
116       return __cxa_thread_atexit_impl(dtor, obj, dso_symbol);
117     } else {
118       // Initialize the dtors std::__libcpp_tls_key (uses __cxa_guard_*() for
119       // one-time initialization and __cxa_atexit() for destruction)
120       static DtorsManager manager;
121 
122       if (!dtors_alive) {
123         if (std::__libcpp_tls_set(dtors_key, &dtors_key) != 0) {
124           return -1;
125         }
126         dtors_alive = true;
127       }
128 
129       auto head = static_cast<DtorList*>(::malloc(sizeof(DtorList)));
130       if (!head) {
131         return -1;
132       }
133 
134       head->dtor = dtor;
135       head->obj = obj;
136       head->next = dtors;
137       dtors = head;
138 
139       return 0;
140     }
141 #endif // HAVE___CXA_THREAD_ATEXIT_IMPL
142   }
143 
144 } // extern "C"
145 } // namespace __cxxabiv1
146