Skip to content

Commit 1dcbf52

Browse files
Use internal __cxa_atexit and __cxa_finalize to handle dynamic destructors (ThePedroo#13)
1 parent 43acc1f commit 1dcbf52

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2020 The Android Open Source Project
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+
* * Redistributions of source code must retain the above copyright
9+
* notice, this list of conditions and the following disclaimer.
10+
* * Redistributions in binary form must reproduce the above copyright
11+
* notice, this list of conditions and the following disclaimer in
12+
* the documentation and/or other materials provided with the
13+
* distribution.
14+
*
15+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18+
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19+
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20+
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21+
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22+
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23+
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25+
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26+
* SUCH DAMAGE.
27+
*/
28+
29+
#include <cstdlib>
30+
#include <pthread.h>
31+
32+
namespace {
33+
struct AtexitEntry {
34+
void (*fn)(void *); // the __cxa_atexit callback
35+
void *arg; // argument for `fn` callback
36+
};
37+
38+
AtexitEntry *g_array = nullptr;
39+
size_t capacity = 8;
40+
size_t count = 0;
41+
pthread_mutex_t g_atexit_mutex = PTHREAD_MUTEX_INITIALIZER;
42+
inline void atexit_lock() {
43+
pthread_mutex_lock(&g_atexit_mutex);
44+
}
45+
46+
inline void atexit_unlock() {
47+
pthread_mutex_unlock(&g_atexit_mutex);
48+
}
49+
50+
// Register a function to be called either when a library is unloaded (dso != nullptr), or when the
51+
// program exits (dso == nullptr). The `dso` argument is typically the address of a hidden
52+
// __dso_handle variable. This function is also used as the backend for the atexit function.
53+
//
54+
// See https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor.
55+
//
56+
extern "C" [[gnu::used]] int __cxa_atexit(void (*func)(void *), void *arg, void * /* dso */) { // NOLINT(bugprone-reserved-identifier)
57+
int result = -1;
58+
59+
if (func != nullptr) {
60+
atexit_lock();
61+
count++;
62+
if (!g_array) {
63+
g_array = reinterpret_cast<AtexitEntry*>(malloc(capacity * sizeof(AtexitEntry)));
64+
}
65+
if (count > capacity) [[unlikely]] {
66+
capacity *= 2;
67+
g_array = reinterpret_cast<AtexitEntry*>(realloc(g_array, capacity * sizeof(AtexitEntry)));
68+
}
69+
g_array[count - 1].fn = func;
70+
g_array[count - 1].arg = arg;
71+
result = 0;
72+
atexit_unlock();
73+
}
74+
75+
return result;
76+
}
77+
78+
// This function will be called by __on_dlclose, which is a destructor of dso
79+
// https://cs.android.com/android/platform/superproject/main/+/main:bionic/libc/arch-common/bionic/crtbegin_so.c;l=34;drc=5501003be73b73de59044b44b12f6e20ba6e0021
80+
extern "C" [[gnu::used]] void __cxa_finalize(void * /* dso */) { // NOLINT(bugprone-reserved-identifier)
81+
atexit_lock();
82+
restart:
83+
if (count > 0) {
84+
size_t total = count;
85+
for (size_t i = count - 1;; --i) {
86+
if (g_array[i].fn == nullptr) continue;
87+
88+
// Clear the entry in the array to avoid calling an entry again
89+
// if __cxa_finalize is called recursively.
90+
const AtexitEntry entry = g_array[i];
91+
g_array[i] = {};
92+
93+
atexit_unlock();
94+
entry.fn(entry.arg);
95+
atexit_lock();
96+
97+
if (count != total) {
98+
goto restart;
99+
}
100+
if (i == 0) break;
101+
}
102+
103+
free(g_array);
104+
}
105+
atexit_unlock();
106+
}
107+
}

0 commit comments

Comments
 (0)