[libc-commits] [libc] [libc] at_quick_exit function implemented (PR #94317)

via libc-commits libc-commits at lists.llvm.org
Wed Jun 5 10:48:54 PDT 2024


https://github.com/aaryanshukla updated https://github.com/llvm/llvm-project/pull/94317

>From 8dfda6ddc7971df95212ffffc3d27d12a4f715b8 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 07:27:36 +0000
Subject: [PATCH 1/8] [libc] at_quit_exit function implemented

---
 libc/config/linux/x86_64/entrypoints.txt    |  1 +
 libc/spec/stdc.td                           |  1 +
 libc/src/stdlib/CMakeLists.txt              | 35 ++++++++
 libc/src/stdlib/at_quick_exit.cpp           | 37 +++++++++
 libc/src/stdlib/at_quick_exit.h             | 17 ++++
 libc/src/stdlib/atexit.cpp                  | 74 ++---------------
 libc/src/stdlib/atexit.h                    |  5 --
 libc/src/stdlib/exit_handler.cpp            | 46 +++++++++++
 libc/src/stdlib/exit_handler.h              | 52 ++++++++++++
 libc/src/stdlib/quick_exit.cpp              |  6 +-
 libc/test/src/stdlib/CMakeLists.txt         | 18 +++++
 libc/test/src/stdlib/at_quick_exit_test.cpp | 90 +++++++++++++++++++++
 12 files changed, 307 insertions(+), 75 deletions(-)
 create mode 100644 libc/src/stdlib/at_quick_exit.cpp
 create mode 100644 libc/src/stdlib/at_quick_exit.h
 create mode 100644 libc/src/stdlib/exit_handler.cpp
 create mode 100644 libc/src/stdlib/exit_handler.h
 create mode 100644 libc/test/src/stdlib/at_quick_exit_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 62434298890f0..17325cc934a6a 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -174,6 +174,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.bsearch
     libc.src.stdlib.div
     libc.src.stdlib.quick_exit
+    libc.src.stdlib.at_quick_exit
     libc.src.stdlib.labs
     libc.src.stdlib.ldiv
     libc.src.stdlib.llabs
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index cacc91ce8789a..31b50257cb47c 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1083,6 +1083,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
           FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
           FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
+          FunctionSpec<"at_quick_exit", RetValSpec<IntType>, [ArgSpec<IntType>]>,
       ]
   >;
 
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index e0bff5198b590..88e30026e14c4 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -50,6 +50,7 @@ add_entrypoint_object(
     quick_exit.h
   DEPENDS
     libc.src.__support.OSUtil.osutil
+    .exit_handler
 )
 
 add_entrypoint_object(
@@ -415,6 +416,23 @@ add_entrypoint_object(
     libc.src.__support.OSUtil.osutil
 )
 
+add_object_library(
+  exit_handler
+  SRCS
+    exit_handler.cpp
+  HDRS
+    exit_handler.h
+  CXX_STANDARD
+    20 # For constinit 
+  DEPENDS
+    libc.src.__support.CPP.mutex
+    libc.src.__support.CPP.new
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.blockstore
+    libc.src.__support.fixedvector
+    libc.src.__support.threads.mutex
+)
+
 add_entrypoint_object(
   atexit
   SRCS
@@ -430,6 +448,22 @@ add_entrypoint_object(
     libc.src.__support.blockstore
     libc.src.__support.fixedvector
     libc.src.__support.threads.mutex
+    .exit_handler
+)
+add_entrypoint_object(
+  at_quick_exit
+  SRCS
+    at_quick_exit.cpp
+  HDRS
+    at_quick_exit.h
+  DEPENDS
+    libc.src.__support.CPP.mutex
+    libc.src.__support.CPP.new
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.blockstore
+    libc.src.__support.fixedvector
+    libc.src.__support.threads.mutex
+    .exit_handler
 )
 
 add_entrypoint_object(
@@ -442,6 +476,7 @@ add_entrypoint_object(
     ._Exit
     .atexit
     libc.src.__support.OSUtil.osutil
+    .exit_handler
 )
 
 add_entrypoint_object(
diff --git a/libc/src/stdlib/at_quick_exit.cpp b/libc/src/stdlib/at_quick_exit.cpp
new file mode 100644
index 0000000000000..9d06dbd324a46
--- /dev/null
+++ b/libc/src/stdlib/at_quick_exit.cpp
@@ -0,0 +1,37 @@
+//===-- Implementation of at_quick_exit -----------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/at_quick_exit.h"
+#include "src/__support/CPP/mutex.h" // lock_guard
+#include "src/__support/blockstore.h"
+#include "src/__support/common.h"
+#include "src/__support/fixedvector.h"
+#include "src/__support/threads/mutex.h"
+#include "src/stdlib/exit_handler.h"
+namespace LIBC_NAMESPACE {
+
+extern "C" {
+
+int __cxa_at_quick_exit(AtExitCallback *callback, void *payload, void *) {
+  return add_atexit_unit(at_quick_exit_callbacks, {callback, payload});
+}
+
+void __cxa_finalize_quick_exit(void *dso) {
+  if (!dso)
+    call_exit_callbacks(at_quick_exit_callbacks);
+}
+
+} // extern "C"
+
+LLVM_LIBC_FUNCTION(int, at_quick_exit, (StdCAtExitCallback * callback)) {
+  return add_atexit_unit(
+      at_quick_exit_callbacks,
+      {&stdc_at_exit_func, reinterpret_cast<void *>(callback)});
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdlib/at_quick_exit.h b/libc/src/stdlib/at_quick_exit.h
new file mode 100644
index 0000000000000..d089e5462605b
--- /dev/null
+++ b/libc/src/stdlib/at_quick_exit.h
@@ -0,0 +1,17 @@
+//===-- Implementation header for at_quick_exit -----------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_AT_QUICK_EXIT_H
+#define LLVM_LIBC_SRC_STDLIB_AT_QUICK_EXIT_H
+
+namespace LIBC_NAMESPACE {
+
+int at_quick_exit(void (*function)());
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDLIB_AT_QUICK_EXIT_H
diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index 9e37c4cf256c9..8f40291420cf5 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -12,91 +12,29 @@
 #include "src/__support/common.h"
 #include "src/__support/fixedvector.h"
 #include "src/__support/threads/mutex.h"
+#include "src/stdlib/exit_handler.h"
 
 namespace LIBC_NAMESPACE {
 
-namespace {
-
-Mutex handler_list_mtx(/*timed=*/false, /*recursive=*/false, /*robust=*/false,
-                       /*pshared=*/false);
-
-using AtExitCallback = void(void *);
-using StdCAtExitCallback = void(void);
-
-struct AtExitUnit {
-  AtExitCallback *callback = nullptr;
-  void *payload = nullptr;
-  constexpr AtExitUnit() = default;
-  constexpr AtExitUnit(AtExitCallback *c, void *p) : callback(c), payload(p) {}
-};
-
-#if defined(LIBC_TARGET_ARCH_IS_GPU)
-// The GPU build cannot handle the potentially recursive definitions required by
-// the BlockStore class. Additionally, the liklihood that someone exceeds this
-// while executing on the GPU is extremely small.
-// FIXME: It is not generally safe to use 'atexit' on the GPU because the
-//        mutexes simply passthrough. We will need a lock free stack.
-using ExitCallbackList = FixedVector<AtExitUnit, 64>;
-#elif defined(LIBC_COPT_PUBLIC_PACKAGING)
-using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
-#else
-// BlockStore uses dynamic memory allocation. To avoid dynamic memory
-// allocation in tests, we use a fixed size callback list when built for
-// tests.
-// If we use BlockStore, then we will have to pull in malloc etc into
-// the tests. While this is not bad, the problem we have currently is
-// that LLVM libc' allocator is SCUDO. So, we will end up pulling SCUDO's
-// deps also (some of which are not yet available in LLVM libc) into the
-// integration tests.
-using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
-#endif // LIBC_COPT_PUBLIC_PACKAGING
-
-constinit ExitCallbackList exit_callbacks;
-
-void stdc_at_exit_func(void *payload) {
-  reinterpret_cast<StdCAtExitCallback *>(payload)();
-}
-
-void call_exit_callbacks() {
-  handler_list_mtx.lock();
-  while (!exit_callbacks.empty()) {
-    AtExitUnit &unit = exit_callbacks.back();
-    exit_callbacks.pop_back();
-    handler_list_mtx.unlock();
-    unit.callback(unit.payload);
-    handler_list_mtx.lock();
-  }
-  ExitCallbackList::destroy(&exit_callbacks);
-}
-
-int add_atexit_unit(const AtExitUnit &unit) {
-  cpp::lock_guard lock(handler_list_mtx);
-  if (exit_callbacks.push_back(unit))
-    return 0;
-  return -1;
-}
-
-} // namespace
-
 extern "C" {
 
-// TODO: Handle the last dso handle argument.
 int __cxa_atexit(AtExitCallback *callback, void *payload, void *) {
-  return add_atexit_unit({callback, payload});
+  return add_atexit_unit(atexit_callbacks, {callback, payload});
 }
 
-// TODO: Handle the dso handle argument. call_exit_callbacks should only invoke
-// the callbacks from this DSO. Requires adding support for __dso_handle.
 void __cxa_finalize(void *dso) {
   if (!dso)
-    call_exit_callbacks();
+    call_exit_callbacks(atexit_callbacks);
 }
 
 } // extern "C"
 
 LLVM_LIBC_FUNCTION(int, atexit, (StdCAtExitCallback * callback)) {
   return add_atexit_unit(
+      atexit_callbacks,
       {&stdc_at_exit_func, reinterpret_cast<void *>(callback)});
 }
 
 } // namespace LIBC_NAMESPACE
+
+
diff --git a/libc/src/stdlib/atexit.h b/libc/src/stdlib/atexit.h
index 7cf9d7c92191d..f9323be257084 100644
--- a/libc/src/stdlib/atexit.h
+++ b/libc/src/stdlib/atexit.h
@@ -8,13 +8,8 @@
 
 #ifndef LLVM_LIBC_SRC_STDLIB_ATEXIT_H
 #define LLVM_LIBC_SRC_STDLIB_ATEXIT_H
-
-#include <stddef.h> // For size_t
-
 namespace LIBC_NAMESPACE {
 
-constexpr size_t CALLBACK_LIST_SIZE_FOR_TESTS = 1024;
-
 int atexit(void (*function)());
 
 } // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdlib/exit_handler.cpp b/libc/src/stdlib/exit_handler.cpp
new file mode 100644
index 0000000000000..bd4ec72070910
--- /dev/null
+++ b/libc/src/stdlib/exit_handler.cpp
@@ -0,0 +1,46 @@
+//===-- Implementation of exit_handler-------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/stdlib/exit_handler.h"
+#include "src/__support/CPP/mutex.h" // lock_guard
+#include "src/__support/blockstore.h"
+#include "src/__support/common.h"
+#include "src/__support/fixedvector.h"
+#include "src/__support/threads/mutex.h"
+
+namespace LIBC_NAMESPACE {
+
+constinit ExitCallbackList at_quick_exit_callbacks;
+constinit ExitCallbackList atexit_callbacks;
+
+Mutex handler_list_mtx(false, false, false, false);
+
+void stdc_at_exit_func(void *payload) {
+  reinterpret_cast<StdCAtExitCallback *>(payload)();
+}
+
+void call_exit_callbacks(ExitCallbackList &callbacks) {
+  handler_list_mtx.lock();
+  while (!callbacks.empty()) {
+    AtExitUnit &unit = callbacks.back();
+    callbacks.pop_back();
+    handler_list_mtx.unlock();
+    unit.callback(unit.payload);
+    handler_list_mtx.lock();
+  }
+  ExitCallbackList::destroy(&callbacks);
+}
+
+int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit) {
+  cpp::lock_guard lock(handler_list_mtx);
+  if (callbacks.push_back(unit))
+    return 0;
+  return -1;
+}
+
+} // namespace LIBC_NAMESPACE
diff --git a/libc/src/stdlib/exit_handler.h b/libc/src/stdlib/exit_handler.h
new file mode 100644
index 0000000000000..473a4d32aa3ca
--- /dev/null
+++ b/libc/src/stdlib/exit_handler.h
@@ -0,0 +1,52 @@
+//===-- Implementation header for exit_handler ------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDLIB_EXIT_HANDLER_H
+#define LLVM_LIBC_SRC_STDLIB_EXIT_HANDLER_H
+
+#include "src/__support/CPP/mutex.h" // lock_guard
+#include "src/__support/blockstore.h"
+#include "src/__support/common.h"
+#include "src/__support/fixedvector.h"
+#include "src/__support/threads/mutex.h"
+
+namespace LIBC_NAMESPACE {
+
+using AtExitCallback = void(void *);
+using StdCAtExitCallback = void(void);
+constexpr size_t CALLBACK_LIST_SIZE_FOR_TESTS = 1024;
+
+struct AtExitUnit {
+  AtExitCallback *callback = nullptr;
+  void *payload = nullptr;
+  constexpr AtExitUnit() = default;
+  constexpr AtExitUnit(AtExitCallback *c, void *p) : callback(c), payload(p) {}
+};
+
+#if defined(LIBC_TARGET_ARCH_IS_GPU)
+using ExitCallbackList = FixedVector<AtExitUnit, 64>;
+#elif defined(LIBC_COPT_PUBLIC_PACKAGING)
+using ExitCallbackList = ReverseOrderBlockStore<AtExitUnit, 32>;
+#else
+using ExitCallbackList = FixedVector<AtExitUnit, CALLBACK_LIST_SIZE_FOR_TESTS>;
+#endif
+
+extern ExitCallbackList atexit_callbacks;
+extern ExitCallbackList at_quick_exit_callbacks;
+
+extern Mutex handler_list_mtx;
+
+void stdc_at_exit_func(void *payload);
+
+void call_exit_callbacks(ExitCallbackList &callbacks);
+
+int add_atexit_unit(ExitCallbackList &callbacks, const AtExitUnit &unit);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDLIB_EXIT_HANDLER_H
diff --git a/libc/src/stdlib/quick_exit.cpp b/libc/src/stdlib/quick_exit.cpp
index cf7f07bf2439a..38f0a3db3e2cf 100644
--- a/libc/src/stdlib/quick_exit.cpp
+++ b/libc/src/stdlib/quick_exit.cpp
@@ -9,13 +9,15 @@
 #include "src/stdlib/quick_exit.h"
 #include "src/__support/OSUtil/exit.h"
 #include "src/__support/common.h"
+#include "src/stdlib/exit_handler.h"
 
 // extern "C" void __cxa_finalize(void *);
-
 namespace LIBC_NAMESPACE {
 
+extern ExitCallbackList at_quick_exit_callbacks;
+
 [[noreturn]] LLVM_LIBC_FUNCTION(void, quick_exit, (int status)) {
-  // __cxa_finalize(nullptr);
+  call_exit_callbacks(at_quick_exit_callbacks);
   internal::exit(status);
 }
 
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 6a7faedece380..088a9bbcd95c5 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -354,6 +354,24 @@ if(LLVM_LIBC_FULL_BUILD)
       libc.src.stdlib.exit
       libc.src.stdlib.atexit
       libc.src.__support.CPP.array
+      .exit_handler
+      libc.src.__support.CPP.utility
+  )
+
+  add_libc_test(
+    at_quick_exit_test
+    # The EXPECT_EXITS test is only availible for unit tests.
+    UNIT_TEST_ONLY
+    SUITE
+      libc-stdlib-tests
+    SRCS
+      at_quick_exit_test.cpp
+    DEPENDS
+      libc.include.stdlib
+      libc.src.stdlib._Exit
+      libc.src.stdlib.quick_exit
+      libc.src.stdlib.at_quick_exit
+      libc.src.__support.CPP.array
       libc.src.__support.CPP.utility
   )
 
diff --git a/libc/test/src/stdlib/at_quick_exit_test.cpp b/libc/test/src/stdlib/at_quick_exit_test.cpp
new file mode 100644
index 0000000000000..2c94b59d47bef
--- /dev/null
+++ b/libc/test/src/stdlib/at_quick_exit_test.cpp
@@ -0,0 +1,90 @@
+//===-- Unittests for at_quick_exit ---------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "src/__support/CPP/array.h"
+#include "src/__support/CPP/utility.h"
+#include "src/stdlib/at_quick_exit.h"
+#include "src/stdlib/quick_exit.h"
+#include "test/UnitTest/Test.h"
+
+static int a;
+TEST(LlvmLibcAtQuickExit, Basic) {
+  // In case tests ever run multiple times.
+  a = 0;
+
+  auto test = [] {
+    int status = LIBC_NAMESPACE::at_quick_exit(+[] {
+      if (a != 1)
+        __builtin_trap();
+    });
+    status |= LIBC_NAMESPACE::at_quick_exit(+[] { a++; });
+    if (status)
+      __builtin_trap();
+
+    LIBC_NAMESPACE::quick_exit(0);
+  };
+  EXPECT_EXITS(test, 0);
+}
+
+TEST(LlvmLibcAtQuickExit, AtQuickExitCallsSysExit) {
+  auto test = [] {
+    LIBC_NAMESPACE::at_quick_exit(+[] { _Exit(1); });
+    LIBC_NAMESPACE::quick_exit(0);
+  };
+  EXPECT_EXITS(test, 1);
+}
+
+static int size;
+static LIBC_NAMESPACE::cpp::array<int, 256> arr;
+
+template <int... Ts>
+void register_at_quick_exit_handlers(
+    LIBC_NAMESPACE::cpp::integer_sequence<int, Ts...>) {
+  (LIBC_NAMESPACE::at_quick_exit(+[] { arr[size++] = Ts; }), ...);
+}
+
+template <int count> constexpr auto get_test() {
+  return [] {
+    LIBC_NAMESPACE::at_quick_exit(+[] {
+      if (size != count)
+        __builtin_trap();
+      for (int i = 0; i < count; i++)
+        if (arr[i] != count - 1 - i)
+          __builtin_trap();
+    });
+    register_at_quick_exit_handlers(
+        LIBC_NAMESPACE::cpp::make_integer_sequence<int, count>{});
+    LIBC_NAMESPACE::quick_exit(0);
+  };
+}
+
+TEST(LlvmLibcAtQuickExit, ReverseOrder) {
+  // In case tests ever run multiple times.
+  size = 0;
+
+  auto test = get_test<32>();
+  EXPECT_EXITS(test, 0);
+}
+
+TEST(LlvmLibcAtQuickExit, Many) {
+  // In case tests ever run multiple times.
+  size = 0;
+
+  auto test = get_test<256>();
+  EXPECT_EXITS(test, 0);
+}
+
+TEST(LlvmLibcAtQuickExit, HandlerCallsAtQuickExit) {
+  auto test = [] {
+    LIBC_NAMESPACE::at_quick_exit(+[] {
+      LIBC_NAMESPACE::at_quick_exit(+[] { LIBC_NAMESPACE::quick_exit(1); });
+    });
+    LIBC_NAMESPACE::quick_exit(0);
+  };
+  EXPECT_EXITS(test, 1);
+}
\ No newline at end of file

>From 2f8265cabc3e9cca1477c6752942644a705f6180 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 17:19:00 +0000
Subject: [PATCH 2/8] [libc] added at_quick_exit function

---
 libc/test/src/stdlib/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 088a9bbcd95c5..76c325db55109 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -354,7 +354,6 @@ if(LLVM_LIBC_FULL_BUILD)
       libc.src.stdlib.exit
       libc.src.stdlib.atexit
       libc.src.__support.CPP.array
-      .exit_handler
       libc.src.__support.CPP.utility
   )
 

>From faaf2ee5d86cd3a5c9f3befd365e833d3170de37 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 17:32:51 +0000
Subject: [PATCH 3/8] [libc] adding at_quick_exit function

---
 libc/src/stdlib/exit_handler.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdlib/exit_handler.cpp b/libc/src/stdlib/exit_handler.cpp
index bd4ec72070910..a066ef729da95 100644
--- a/libc/src/stdlib/exit_handler.cpp
+++ b/libc/src/stdlib/exit_handler.cpp
@@ -1,4 +1,4 @@
-//===-- Implementation of exit_handler-------------------------------------===//
+//===--- Implementation of exit_handler------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
 // See https://llvm.org/LICENSE.txt for license information.

>From 75f5202488a11908f03836602aa0336587a03d47 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 20:01:47 +0000
Subject: [PATCH 4/8] moved quick_exit and at_quick_exit to full build

---
 libc/config/linux/x86_64/entrypoints.txt | 5 +++--
 libc/src/stdlib/CMakeLists.txt           | 3 +++
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 48c42cb5055ce..8b7bc874aacd9 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -173,8 +173,6 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdlib.atoll
     libc.src.stdlib.bsearch
     libc.src.stdlib.div
-    libc.src.stdlib.quick_exit
-    libc.src.stdlib.at_quick_exit
     libc.src.stdlib.labs
     libc.src.stdlib.ldiv
     libc.src.stdlib.llabs
@@ -752,6 +750,9 @@ if(LLVM_LIBC_FULL_BUILD)
     libc.src.stdlib.abort
     libc.src.stdlib.atexit
     libc.src.stdlib.exit
+    libc.src.stdlib.quick_exit
+    libc.src.stdlib.at_quick_exit
+
     libc.src.stdlib.getenv
 
     # signal.h entrypoints
diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 88e30026e14c4..bf54324b111be 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -449,6 +449,7 @@ add_entrypoint_object(
     libc.src.__support.fixedvector
     libc.src.__support.threads.mutex
     .exit_handler
+    libc.include.stdlib
 )
 add_entrypoint_object(
   at_quick_exit
@@ -464,6 +465,7 @@ add_entrypoint_object(
     libc.src.__support.fixedvector
     libc.src.__support.threads.mutex
     .exit_handler
+    libc.include.stdlib
 )
 
 add_entrypoint_object(
@@ -477,6 +479,7 @@ add_entrypoint_object(
     .atexit
     libc.src.__support.OSUtil.osutil
     .exit_handler
+    libc.include.stdlib
 )
 
 add_entrypoint_object(

>From 131f28dc8198b0b1b3a48c689432c24cb32155bd Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 21:39:32 +0000
Subject: [PATCH 5/8] addressed comments

- fixed line spacing
- removed duplicate includes
- added inline declarations
---
 libc/config/linux/x86_64/entrypoints.txt    | 5 ++---
 libc/spec/stdc.td                           | 4 ++--
 libc/src/stdlib/at_quick_exit.cpp           | 4 ----
 libc/src/stdlib/atexit.cpp                  | 6 ------
 libc/src/stdlib/exit_handler.cpp            | 4 ----
 libc/src/stdlib/exit_handler.h              | 5 +++--
 libc/test/src/stdlib/at_quick_exit_test.cpp | 2 +-
 7 files changed, 8 insertions(+), 22 deletions(-)

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 8b7bc874aacd9..7637e44838dc2 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -748,12 +748,11 @@ if(LLVM_LIBC_FULL_BUILD)
     # stdlib.h entrypoints
     libc.src.stdlib._Exit
     libc.src.stdlib.abort
+    libc.src.stdlib.at_quick_exit
     libc.src.stdlib.atexit
     libc.src.stdlib.exit
-    libc.src.stdlib.quick_exit
-    libc.src.stdlib.at_quick_exit
-
     libc.src.stdlib.getenv
+    libc.src.stdlib.quick_exit
 
     # signal.h entrypoints
     libc.src.signal.raise
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index 1e0f3ba18c106..cb8bda441e035 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1086,10 +1086,10 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"free", RetValSpec<VoidType>, [ArgSpec<VoidPtr>]>,
 
           FunctionSpec<"_Exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
-          FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
+          FunctionSpec<"at_quick_exit", RetValSpec<IntType>, [ArgSpec<IntType>]>,
           FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
+          FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
           FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
-          FunctionSpec<"at_quick_exit", RetValSpec<IntType>, [ArgSpec<IntType>]>,
       ]
   >;
 
diff --git a/libc/src/stdlib/at_quick_exit.cpp b/libc/src/stdlib/at_quick_exit.cpp
index 9d06dbd324a46..7c74fa078ffbc 100644
--- a/libc/src/stdlib/at_quick_exit.cpp
+++ b/libc/src/stdlib/at_quick_exit.cpp
@@ -7,11 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/stdlib/at_quick_exit.h"
-#include "src/__support/CPP/mutex.h" // lock_guard
-#include "src/__support/blockstore.h"
 #include "src/__support/common.h"
-#include "src/__support/fixedvector.h"
-#include "src/__support/threads/mutex.h"
 #include "src/stdlib/exit_handler.h"
 namespace LIBC_NAMESPACE {
 
diff --git a/libc/src/stdlib/atexit.cpp b/libc/src/stdlib/atexit.cpp
index 8f40291420cf5..e82fd84ad4df3 100644
--- a/libc/src/stdlib/atexit.cpp
+++ b/libc/src/stdlib/atexit.cpp
@@ -7,11 +7,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "src/stdlib/atexit.h"
-#include "src/__support/CPP/mutex.h" // lock_guard
-#include "src/__support/blockstore.h"
 #include "src/__support/common.h"
-#include "src/__support/fixedvector.h"
-#include "src/__support/threads/mutex.h"
 #include "src/stdlib/exit_handler.h"
 
 namespace LIBC_NAMESPACE {
@@ -36,5 +32,3 @@ LLVM_LIBC_FUNCTION(int, atexit, (StdCAtExitCallback * callback)) {
 }
 
 } // namespace LIBC_NAMESPACE
-
-
diff --git a/libc/src/stdlib/exit_handler.cpp b/libc/src/stdlib/exit_handler.cpp
index a066ef729da95..ed41247e4a31d 100644
--- a/libc/src/stdlib/exit_handler.cpp
+++ b/libc/src/stdlib/exit_handler.cpp
@@ -8,10 +8,6 @@
 
 #include "src/stdlib/exit_handler.h"
 #include "src/__support/CPP/mutex.h" // lock_guard
-#include "src/__support/blockstore.h"
-#include "src/__support/common.h"
-#include "src/__support/fixedvector.h"
-#include "src/__support/threads/mutex.h"
 
 namespace LIBC_NAMESPACE {
 
diff --git a/libc/src/stdlib/exit_handler.h b/libc/src/stdlib/exit_handler.h
index 473a4d32aa3ca..8494c2f2e526e 100644
--- a/libc/src/stdlib/exit_handler.h
+++ b/libc/src/stdlib/exit_handler.h
@@ -24,8 +24,9 @@ constexpr size_t CALLBACK_LIST_SIZE_FOR_TESTS = 1024;
 struct AtExitUnit {
   AtExitCallback *callback = nullptr;
   void *payload = nullptr;
-  constexpr AtExitUnit() = default;
-  constexpr AtExitUnit(AtExitCallback *c, void *p) : callback(c), payload(p) {}
+  LIBC_INLINE constexpr AtExitUnit() = default;
+  LIBC_INLINE constexpr AtExitUnit(AtExitCallback *c, void *p)
+      : callback(c), payload(p) {}
 };
 
 #if defined(LIBC_TARGET_ARCH_IS_GPU)
diff --git a/libc/test/src/stdlib/at_quick_exit_test.cpp b/libc/test/src/stdlib/at_quick_exit_test.cpp
index 2c94b59d47bef..e0a258d9fb2d9 100644
--- a/libc/test/src/stdlib/at_quick_exit_test.cpp
+++ b/libc/test/src/stdlib/at_quick_exit_test.cpp
@@ -87,4 +87,4 @@ TEST(LlvmLibcAtQuickExit, HandlerCallsAtQuickExit) {
     LIBC_NAMESPACE::quick_exit(0);
   };
   EXPECT_EXITS(test, 1);
-}
\ No newline at end of file
+}

>From d8010f2391ba6b7e847575f05273df88e4a18364 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 22:08:54 +0000
Subject: [PATCH 6/8] removed unnecesary dependencies within cmake files

---
 libc/src/stdlib/CMakeLists.txt      | 16 +---------------
 libc/test/src/stdlib/CMakeLists.txt |  2 --
 2 files changed, 1 insertion(+), 17 deletions(-)

diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index bf54324b111be..857e4f32db010 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -442,15 +442,9 @@ add_entrypoint_object(
   CXX_STANDARD
     20 # For constinit of the atexit callback list.
   DEPENDS
-    libc.src.__support.CPP.mutex
-    libc.src.__support.CPP.new
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.blockstore
-    libc.src.__support.fixedvector
-    libc.src.__support.threads.mutex
     .exit_handler
-    libc.include.stdlib
 )
+
 add_entrypoint_object(
   at_quick_exit
   SRCS
@@ -458,14 +452,7 @@ add_entrypoint_object(
   HDRS
     at_quick_exit.h
   DEPENDS
-    libc.src.__support.CPP.mutex
-    libc.src.__support.CPP.new
-    libc.src.__support.OSUtil.osutil
-    libc.src.__support.blockstore
-    libc.src.__support.fixedvector
-    libc.src.__support.threads.mutex
     .exit_handler
-    libc.include.stdlib
 )
 
 add_entrypoint_object(
@@ -479,7 +466,6 @@ add_entrypoint_object(
     .atexit
     libc.src.__support.OSUtil.osutil
     .exit_handler
-    libc.include.stdlib
 )
 
 add_entrypoint_object(
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index 76c325db55109..b0f541098a986 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -366,8 +366,6 @@ if(LLVM_LIBC_FULL_BUILD)
     SRCS
       at_quick_exit_test.cpp
     DEPENDS
-      libc.include.stdlib
-      libc.src.stdlib._Exit
       libc.src.stdlib.quick_exit
       libc.src.stdlib.at_quick_exit
       libc.src.__support.CPP.array

>From d004c17e2b6a35105cf5226dc2a3c0afa75367b0 Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Tue, 4 Jun 2024 22:47:31 +0000
Subject: [PATCH 7/8] removed cxa functions from at_quick_exit

- removed cxx_standard from cmake file
- removed unnecesary utility from tests
---
 libc/src/stdlib/CMakeLists.txt      |  2 --
 libc/src/stdlib/at_quick_exit.cpp   | 13 -------------
 libc/test/src/stdlib/CMakeLists.txt |  2 --
 3 files changed, 17 deletions(-)

diff --git a/libc/src/stdlib/CMakeLists.txt b/libc/src/stdlib/CMakeLists.txt
index 857e4f32db010..219c85dda6757 100644
--- a/libc/src/stdlib/CMakeLists.txt
+++ b/libc/src/stdlib/CMakeLists.txt
@@ -439,8 +439,6 @@ add_entrypoint_object(
     atexit.cpp
   HDRS
     atexit.h
-  CXX_STANDARD
-    20 # For constinit of the atexit callback list.
   DEPENDS
     .exit_handler
 )
diff --git a/libc/src/stdlib/at_quick_exit.cpp b/libc/src/stdlib/at_quick_exit.cpp
index 7c74fa078ffbc..9806b1c960de6 100644
--- a/libc/src/stdlib/at_quick_exit.cpp
+++ b/libc/src/stdlib/at_quick_exit.cpp
@@ -11,19 +11,6 @@
 #include "src/stdlib/exit_handler.h"
 namespace LIBC_NAMESPACE {
 
-extern "C" {
-
-int __cxa_at_quick_exit(AtExitCallback *callback, void *payload, void *) {
-  return add_atexit_unit(at_quick_exit_callbacks, {callback, payload});
-}
-
-void __cxa_finalize_quick_exit(void *dso) {
-  if (!dso)
-    call_exit_callbacks(at_quick_exit_callbacks);
-}
-
-} // extern "C"
-
 LLVM_LIBC_FUNCTION(int, at_quick_exit, (StdCAtExitCallback * callback)) {
   return add_atexit_unit(
       at_quick_exit_callbacks,
diff --git a/libc/test/src/stdlib/CMakeLists.txt b/libc/test/src/stdlib/CMakeLists.txt
index b0f541098a986..38488778c657c 100644
--- a/libc/test/src/stdlib/CMakeLists.txt
+++ b/libc/test/src/stdlib/CMakeLists.txt
@@ -354,7 +354,6 @@ if(LLVM_LIBC_FULL_BUILD)
       libc.src.stdlib.exit
       libc.src.stdlib.atexit
       libc.src.__support.CPP.array
-      libc.src.__support.CPP.utility
   )
 
   add_libc_test(
@@ -369,7 +368,6 @@ if(LLVM_LIBC_FULL_BUILD)
       libc.src.stdlib.quick_exit
       libc.src.stdlib.at_quick_exit
       libc.src.__support.CPP.array
-      libc.src.__support.CPP.utility
   )
 
   add_libc_test(

>From a0d3026a20fd0ffa5c08800727a42019139a960b Mon Sep 17 00:00:00 2001
From: Aaryan Shukla <aaryanshukla at google.com>
Date: Wed, 5 Jun 2024 17:47:29 +0000
Subject: [PATCH 8/8] added atexithandler to proxy headers

- fixed line spacing under include statements in at_quick_exit
---
 libc/hdr/types/CMakeLists.txt     |  9 +++++++++
 libc/hdr/types/atexithandler_t.h  | 20 ++++++++++++++++++++
 libc/spec/stdc.td                 |  2 +-
 libc/src/stdlib/at_quick_exit.cpp |  1 +
 4 files changed, 31 insertions(+), 1 deletion(-)
 create mode 100644 libc/hdr/types/atexithandler_t.h

diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 8e87642f66088..9b3373a0ca390 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -117,3 +117,12 @@ add_proxy_header_library(
     libc.include.llvm-libc-types.pid_t
     libc.include.sys_types
 )
+
+add_proxy_header_library(
+  atexithandler_t
+  HDRS
+    atexithandler_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.atexithandler_t
+    libc.include.stdlib
+)
diff --git a/libc/hdr/types/atexithandler_t.h b/libc/hdr/types/atexithandler_t.h
new file mode 100644
index 0000000000000..4ae080e5e62f0
--- /dev/null
+++ b/libc/hdr/types/atexithandler_t.h
@@ -0,0 +1,20 @@
+//===-- Definition of macros from atexithandler_t.h -----------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_ATEXITHANDLER_T_H
+#define LLVM_LIBC_HDR_ATEXITHANDLER_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/atexithandler_t.h"
+
+#error // type not available in overlay mode
+
+#endif // LLVM_LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_ATEXITHANDLER_T_H
diff --git a/libc/spec/stdc.td b/libc/spec/stdc.td
index cb8bda441e035..1d3baaf76c364 100644
--- a/libc/spec/stdc.td
+++ b/libc/spec/stdc.td
@@ -1086,7 +1086,7 @@ def StdC : StandardSpec<"stdc"> {
           FunctionSpec<"free", RetValSpec<VoidType>, [ArgSpec<VoidPtr>]>,
 
           FunctionSpec<"_Exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
-          FunctionSpec<"at_quick_exit", RetValSpec<IntType>, [ArgSpec<IntType>]>,
+          FunctionSpec<"at_quick_exit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
           FunctionSpec<"atexit", RetValSpec<IntType>, [ArgSpec<AtexitHandlerT>]>,
           FunctionSpec<"exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
           FunctionSpec<"quick_exit", RetValSpec<NoReturn>, [ArgSpec<IntType>]>,
diff --git a/libc/src/stdlib/at_quick_exit.cpp b/libc/src/stdlib/at_quick_exit.cpp
index 9806b1c960de6..ec400dc24bc89 100644
--- a/libc/src/stdlib/at_quick_exit.cpp
+++ b/libc/src/stdlib/at_quick_exit.cpp
@@ -9,6 +9,7 @@
 #include "src/stdlib/at_quick_exit.h"
 #include "src/__support/common.h"
 #include "src/stdlib/exit_handler.h"
+
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, at_quick_exit, (StdCAtExitCallback * callback)) {



More information about the libc-commits mailing list