Skip to content

Commit 4318f7e

Browse files
[libc][stdlib] initial support for __cxa_finalize (#85865)
I'm trying to break up the pieces of supporting __cxa_finalize into smaller commits. Provide this symbol first, and make use of it from exit. Next will be to store __dso_handle, then finally to only run callbacks that were registered from a specific dso. Link: #85651 Link: https://itanium-cxx-abi.github.io/cxx-abi/abi.html#dso-dtor
1 parent cdbec7b commit 4318f7e

File tree

2 files changed

+22
-17
lines changed

2 files changed

+22
-17
lines changed

libc/src/stdlib/atexit.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,10 @@ void stdc_at_exit_func(void *payload) {
5555
reinterpret_cast<StdCAtExitCallback *>(payload)();
5656
}
5757

58-
} // namespace
59-
60-
namespace internal {
61-
6258
void call_exit_callbacks() {
6359
handler_list_mtx.lock();
6460
while (!exit_callbacks.empty()) {
65-
auto unit = exit_callbacks.back();
61+
AtExitUnit &unit = exit_callbacks.back();
6662
exit_callbacks.pop_back();
6763
handler_list_mtx.unlock();
6864
unit.callback(unit.payload);
@@ -71,20 +67,31 @@ void call_exit_callbacks() {
7167
ExitCallbackList::destroy(&exit_callbacks);
7268
}
7369

74-
} // namespace internal
75-
76-
static int add_atexit_unit(const AtExitUnit &unit) {
70+
int add_atexit_unit(const AtExitUnit &unit) {
7771
MutexLock lock(&handler_list_mtx);
78-
if (!exit_callbacks.push_back(unit))
79-
return -1;
80-
return 0;
72+
if (exit_callbacks.push_back(unit))
73+
return 0;
74+
return -1;
8175
}
8276

77+
} // namespace
78+
79+
extern "C" {
80+
8381
// TODO: Handle the last dso handle argument.
84-
extern "C" int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
82+
int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
8583
return add_atexit_unit({callback, payload});
8684
}
8785

86+
// TODO: Handle the dso handle argument. call_exit_callbacks should only invoke
87+
// the callbacks from this DSO. Requires adding support for __dso_handle.
88+
void __cxa_finalize(void *dso) {
89+
if (!dso)
90+
call_exit_callbacks();
91+
}
92+
93+
} // extern "C"
94+
8895
LLVM_LIBC_FUNCTION(int, atexit, (StdCAtExitCallback * callback)) {
8996
return add_atexit_unit(
9097
{&stdc_at_exit_func, reinterpret_cast<void *>(callback)});

libc/src/stdlib/exit.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
#include "src/__support/OSUtil/quick_exit.h"
1111
#include "src/__support/common.h"
1212

13-
namespace LIBC_NAMESPACE {
13+
extern "C" void __cxa_finalize(void *);
1414

15-
namespace internal {
16-
void call_exit_callbacks();
17-
}
15+
namespace LIBC_NAMESPACE {
1816

1917
LLVM_LIBC_FUNCTION(void, exit, (int status)) {
20-
internal::call_exit_callbacks();
18+
__cxa_finalize(nullptr);
2119
quick_exit(status);
2220
__builtin_unreachable();
2321
}

0 commit comments

Comments
 (0)