[libc-commits] [libc] [libc][stdlib] Support non-null DSO handle in __cxa_finalize (PR #246876)

via libc-commits libc-commits at lists.llvm.org
Sat Nov 28 10:01:31 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libc

Author: Mayank meena (Mayank9963)

<details>
<summary>Changes</summary>


Fixes #<!-- -->85651
added dso in AtExitUnit ,  made a new func find_dso_callback and implemented the non-null dso handling in call_exit_callbacks , wired dso with __cxa_atexit and __cxa_finalize, and added CxaFinalizeDso test and CxaFinalizeReentrant test.
command ran to test -  
    ninja -C build-full libc.test.src.stdlib.atexit_test 
    ninja -C build-full libc.test.src.stdlib.at_quick_exit_test 

---
Full diff: https://github.com/llvm/llvm-project/pull/246876.diff


3 Files Affected:

- (modified) libc/src/stdlib/atexit.cpp (+4-7) 
- (modified) libc/src/stdlib/exit_handler.h (+42-8) 
- (modified) libc/test/src/stdlib/atexit_test.cpp (+81) 


``````````diff
diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index 58e8a614517a4f..2feab16fc95286 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -21,16 +21,13 @@ Mutex handler_list_mtx(/*is_priority_inherit=*/false, /*is_recursive=*/false,
 
 extern "C" {
 
-int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
-  return add_atexit_unit(atexit_callbacks, {callback, payload});
+int __cxa_atexit(AtExitCallback *callback, void *payload, void *dso) {
+  return add_atexit_unit(atexit_callbacks, {callback, payload, dso});
 }
 
-void __cxa_finalize(void *dso) {
-  if (!dso)
-    call_exit_callbacks(atexit_callbacks);
+void __cxa_finalize(void *dso) { call_exit_callbacks(atexit_callbacks, dso); }
 }
-
-} // extern "C"
+// extern "C"
 
 LLVM_LIBC_FUNCTION(int, atexit, (__atexithandler_t callback)) {
   return add_atexit_unit(
diff --git a/libc/src/stdlib/exit_handler.h b/libc/src/stdlib/exit_handler.h
index e9d163dfe90244..4f2bcc558fd19e 100644
--- a/libc/src/stdlib/exit_handler.h
+++ b/libc/src/stdlib/exit_handler.h
@@ -25,9 +25,11 @@ constexpr size_t CALLBACK_LIST_SIZE_FOR_TESTS = 1024;
 struct AtExitUnit {
   AtExitCallback *callback = nullptr;
   void *payload = nullptr;
+  void *dso = nullptr;
   LIBC_INLINE constexpr AtExitUnit() = default;
-  LIBC_INLINE constexpr AtExitUnit(AtExitCallback *c, void *p)
-      : callback(c), payload(p) {}
+  LIBC_INLINE constexpr AtExitUnit(AtExitCallback *c, void *p,
+                                   void *d = nullptr)
+      : callback(c), payload(p), dso(d) {}
 };
 
 #if defined(LIBC_TARGET_ARCH_IS_GPU)
@@ -45,16 +47,48 @@ LIBC_INLINE void stdc_at_exit_func(void *payload) {
   reinterpret_cast<StdCAtExitCallback *>(payload)();
 }
 
-LIBC_INLINE void call_exit_callbacks(ExitCallbackList &callbacks) {
+LIBC_INLINE AtExitUnit *find_dso_callback(ExitCallbackList &callbacks,
+                                          void *dso) {
+#if defined(LIBC_COPT_PUBLIC_PACKAGING) && !defined(LIBC_TARGET_ARCH_IS_GPU)
+  for (AtExitUnit &unit : callbacks) {
+    if (unit.callback && unit.dso == dso)
+      return &unit;
+  }
+#else
+  for (auto it = callbacks.rbegin(); it != callbacks.rend(); ++it) {
+    if (it->callback && it->dso == dso)
+      return &(*it);
+  }
+#endif
+  return nullptr;
+}
+
+LIBC_INLINE void call_exit_callbacks(ExitCallbackList &callbacks,
+                                     void *dso = nullptr) {
   handler_list_mtx.lock();
-  while (!callbacks.empty()) {
-    AtExitUnit unit = callbacks.back();
-    callbacks.pop_back();
+  if (!dso) {
+    while (!callbacks.empty()) {
+      AtExitUnit unit = callbacks.back();
+      callbacks.pop_back();
+      if (!unit.callback)
+        continue;
+      handler_list_mtx.unlock();
+      unit.callback(unit.payload);
+      handler_list_mtx.lock();
+    }
+    ExitCallbackList::destroy(&callbacks);
+    return;
+  }
+
+  while (AtExitUnit *unit = find_dso_callback(callbacks, dso)) {
+    AtExitCallback *callback = unit->callback;
+    void *payload = unit->payload;
+    unit->callback = nullptr;
     handler_list_mtx.unlock();
-    unit.callback(unit.payload);
+    callback(payload);
     handler_list_mtx.lock();
   }
-  ExitCallbackList::destroy(&callbacks);
+  handler_list_mtx.unlock();
 }
 
 LIBC_INLINE int add_atexit_unit(ExitCallbackList &callbacks,
diff --git a/libc/test/src/stdlib/atexit_test.cpp b/libc/test/src/stdlib/atexit_test.cpp
index c25202ff54f696..6667f6e99dfeea 100644
--- a/libc/test/src/stdlib/atexit_test.cpp
+++ b/libc/test/src/stdlib/atexit_test.cpp
@@ -88,3 +88,84 @@ TEST(LlvmLibcAtExit, HandlerCallsAtExit) {
   };
   EXPECT_EXITS(test, 1);
 }
+
+extern "C" {
+int __cxa_atexit(void (*callback)(void *), void *payload, void *dso);
+void __cxa_finalize(void *dso);
+}
+
+TEST(LlvmLibcAtExit, CxaFinalizeDso) {
+  size = 0;
+
+  auto test = [] {
+    static int dso1 = 1;
+    static int dso2 = 2;
+
+    auto record =
+        +[](void *arg) { arr[size++] = *reinterpret_cast<int *>(arg); };
+
+    static int v1 = 1, v2 = 2, v3 = 3;
+    if (__cxa_atexit(record, &v1, &dso1) != 0)
+      __builtin_trap();
+    if (__cxa_atexit(record, &v2, &dso2) != 0)
+      __builtin_trap();
+    if (__cxa_atexit(record, &v3, &dso1) != 0)
+      __builtin_trap();
+    if (LIBC_NAMESPACE::atexit(+[] { arr[size++] = 4; }) != 0)
+      __builtin_trap();
+
+    // Finalize only dso1: should run v3 then v1 (LIFO), leaving dso2 and
+    // atexit.
+    __cxa_finalize(&dso1);
+    if (size != 2 || arr[0] != 3 || arr[1] != 1)
+      __builtin_trap();
+
+    // Calling __cxa_finalize(&dso1) a second time should be a no-op.
+    __cxa_finalize(&dso1);
+    if (size != 2)
+      __builtin_trap();
+
+    // Finalize dso2 and verify it runs.
+    __cxa_finalize(&dso2);
+    if (size != 3 || arr[2] != 2)
+      __builtin_trap();
+
+    LIBC_NAMESPACE::exit(0);
+  };
+  EXPECT_EXITS(test, 0);
+}
+
+TEST(LlvmLibcAtExit, CxaFinalizeReentrant) {
+  size = 0;
+
+  auto test = [] {
+    static int dso = 1;
+    static int v1 = 1, v2 = 2;
+
+    auto record =
+        +[](void *arg) { arr[size++] = *reinterpret_cast<int *>(arg); };
+
+    auto register_more = +[](void *) {
+      arr[size++] = 1;
+      if (__cxa_atexit(
+              +[](void *arg) { arr[size++] = *reinterpret_cast<int *>(arg); },
+              &v2, &dso) != 0)
+        __builtin_trap();
+    };
+
+    if (__cxa_atexit(record, &v1, &dso) != 0)
+      __builtin_trap();
+    if (__cxa_atexit(register_more, nullptr, &dso) != 0)
+      __builtin_trap();
+
+    // register_more runs first (records 1) and registers v2 for the same dso.
+    // Restarting the scan should run v2 (records 2) before older v1 (records
+    // 1).
+    __cxa_finalize(&dso);
+    if (size != 3 || arr[0] != 1 || arr[1] != 2 || arr[2] != 1)
+      __builtin_trap();
+
+    LIBC_NAMESPACE::exit(0);
+  };
+  EXPECT_EXITS(test, 0);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/246876


More information about the libc-commits mailing list