[libc-commits] [libc] [llvm] [libc] Decouple LIBC_ASSERT failure handling and raise SIGABRT in full-build mode (PR #239277)
via libc-commits
libc-commits at lists.llvm.org
Sat Nov 7 06:48:12 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libc
Author: Aman Maurya (amanmaurya92)
<details>
<summary>Changes</summary>
Decouple `LIBC_ASSERT` and `LIBC_REQUIRE` failure handling in full-build mode by forward-declaring `__assert_fail` in `libc_assert.h` instead of inlining `write_to_stderr` and calling `internal::exit(0xFF)`.
This resolves the FIXME in `libc_assert.h` by terminating with `SIGABRT` via `__assert_fail` / `abort_utils::abort()` without introducing circular dependencies with `__support`:
- Forward-declares `__assert_fail` in `libc_assert.h` and drops unused exit header includes.
- Adds an atomic re-entrancy guard in `__assert_fail` to prevent infinite recursion on nested assertion failures.
- Links `__assert_fail` into hermetic unit test support targets.
- Updates `unique_ptr_test` to expect `SIGABRT` across all build configurations.
---
Full diff: https://github.com/llvm/llvm-project/pull/239277.diff
6 Files Affected:
- (modified) libc/cmake/modules/LLVMLibCTestRules.cmake (+3)
- (modified) libc/src/__support/libc_assert.h (+10-12)
- (modified) libc/src/assert/generic/CMakeLists.txt (+1)
- (modified) libc/src/assert/generic/__assert_fail.cpp (+7)
- (modified) libc/test/src/__support/CPP/unique_ptr_test.cpp (+2-8)
- (modified) utils/bazel/llvm-project-overlay/libc/BUILD.bazel (-1)
``````````diff
diff --git a/libc/cmake/modules/LLVMLibCTestRules.cmake b/libc/cmake/modules/LLVMLibCTestRules.cmake
index f86f79af2d3bd2..bac4690777ac17 100644
--- a/libc/cmake/modules/LLVMLibCTestRules.cmake
+++ b/libc/cmake/modules/LLVMLibCTestRules.cmake
@@ -499,6 +499,9 @@ function(_get_libc_test_dependencies output_var user_depends startup_target_dep
if(libc.src.compiler.__stack_chk_fail IN_LIST TARGET_LLVMLIBC_ENTRYPOINTS)
list(APPEND fq_deps_list libc.src.compiler.__stack_chk_fail)
endif()
+ if(libc.src.assert.__assert_fail IN_LIST TARGET_LLVMLIBC_ENTRYPOINTS)
+ list(APPEND fq_deps_list libc.src.assert.__assert_fail)
+ endif()
if(NOT is_integration_test)
list(APPEND fq_deps_list libc.test.UnitTest.HermeticTestUtils)
if(LIBC_TARGET_ARCHITECTURE_IS_AARCH64 AND NOT(LIBC_TARGET_OS_IS_BAREMETAL))
diff --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h
index 81135fe1b3d501..f2651990f3b685 100644
--- a/libc/src/__support/libc_assert.h
+++ b/libc/src/__support/libc_assert.h
@@ -17,7 +17,6 @@
#include "src/__support/macros/properties/os.h"
#ifdef LIBC_FULL_BUILD
-#include "src/__support/OSUtil/exit.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/integer_to_string.h"
#endif
@@ -44,19 +43,18 @@
} while (false)
#endif // LIBC_TARGET_OS_IS_LINUX
#else
-// FIXME: Calling abort on assertion is actually required by standards like LSB.
-// Calling exit also confuses the debugger as exiting will not trigger
-// debugger's stop-on-signal behavior by default. Currently, adding abort will
-// result in cyclic dependency.
+namespace LIBC_NAMESPACE_DECL {
+// Forward declaration of __assert_fail to decouple assertion failure
+// handling and abort from internal header dependencies.
+[[noreturn]] void __assert_fail(const char *assertion, const char *file,
+ unsigned line, const char *function);
+} // namespace LIBC_NAMESPACE_DECL
+
#define LIBC_REQUIRE(COND, MSG) \
do { \
- if (LIBC_UNLIKELY(!(COND))) { \
- LIBC_NAMESPACE::write_to_stderr(__FILE__ ":" LLVM_LIBC_STRINGIFY( \
- __LINE__) ": Assertion failed: '" MSG "' in function: '"); \
- LIBC_NAMESPACE::write_to_stderr(__PRETTY_FUNCTION__); \
- LIBC_NAMESPACE::write_to_stderr("'\n"); \
- LIBC_NAMESPACE::internal::exit(0xFF); \
- } \
+ if (LIBC_UNLIKELY(!(COND))) \
+ LIBC_NAMESPACE::__assert_fail(MSG, __FILE__, __LINE__, \
+ __PRETTY_FUNCTION__); \
} while (false)
#endif // LIBC_FULL_BUILD
diff --git a/libc/src/assert/generic/CMakeLists.txt b/libc/src/assert/generic/CMakeLists.txt
index 95e47610576bc7..8bdf08fe75effd 100644
--- a/libc/src/assert/generic/CMakeLists.txt
+++ b/libc/src/assert/generic/CMakeLists.txt
@@ -7,6 +7,7 @@ add_entrypoint_object(
../assert.h
DEPENDS
libc.include.assert
+ libc.src.__support.CPP.atomic
libc.src.__support.OSUtil.osutil
libc.src.stdlib.abort_utils
)
diff --git a/libc/src/assert/generic/__assert_fail.cpp b/libc/src/assert/generic/__assert_fail.cpp
index c3ea0307965902..48c686a2d44ca7 100644
--- a/libc/src/assert/generic/__assert_fail.cpp
+++ b/libc/src/assert/generic/__assert_fail.cpp
@@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//
#include "src/assert/__assert_fail.h"
+#include "src/__support/CPP/atomic.h"
#include "src/__support/OSUtil/io.h"
#include "src/__support/libc_assert.h"
#include "src/__support/macros/config.h"
@@ -17,6 +18,12 @@ namespace LIBC_NAMESPACE_DECL {
LLVM_LIBC_FUNCTION(void, __assert_fail,
(const char *assertion, const char *file, unsigned line,
const char *function)) {
+ // Re-entrancy guard: if an assertion fails inside the abort or lock sequence,
+ // trap immediately to prevent infinite recursion.
+ static cpp::Atomic<bool> in_assertion{false};
+ if (in_assertion.exchange(true, cpp::MemoryOrder::ACQUIRE))
+ __builtin_trap();
+
LIBC_NAMESPACE::report_assertion_failure(assertion, file, line, function);
LIBC_NAMESPACE::abort_utils::abort();
}
diff --git a/libc/test/src/__support/CPP/unique_ptr_test.cpp b/libc/test/src/__support/CPP/unique_ptr_test.cpp
index 7e26a8e3449b3a..5279baf0deb24d 100644
--- a/libc/test/src/__support/CPP/unique_ptr_test.cpp
+++ b/libc/test/src/__support/CPP/unique_ptr_test.cpp
@@ -259,14 +259,8 @@ TEST(LlvmLibcUniquePtrTest, BaseDerivedConversion) {
ASSERT_EQ(base_ptr->val, 100);
}
-#ifndef NDEBUG
-// In full builds, LIBC_ASSERT exits with code 0xFF. Otherwise, it uses the
-// system assert, which raises SIGABRT.
-#if defined(LIBC_FULL_BUILD) && !defined(LIBC_COPT_USE_C_ASSERT)
-#define EXPECT_ASSERT_FAILURE(FUNC) EXPECT_EXITS(FUNC, 0xFF)
-#else
+#if !defined(NDEBUG) && defined(SIGABRT)
#define EXPECT_ASSERT_FAILURE(FUNC) EXPECT_DEATH(FUNC, WITH_SIGNAL(SIGABRT))
-#endif
// Test that dereferencing a null unique_ptr asserts.
TEST(LlvmLibcUniquePtrTest, NullDereferenceDeath) {
@@ -280,4 +274,4 @@ TEST(LlvmLibcUniquePtrTest, NullDereferenceDeath) {
unique_ptr<int[]> arr;
EXPECT_ASSERT_FAILURE([&] { (void)arr[0]; });
}
-#endif // NDEBUG
+#endif // !defined(NDEBUG) && defined(SIGABRT)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index 42e3ae6fffaf9c..a52b871270b536 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -2366,7 +2366,6 @@ libc_support_library(
] + select({
":full_build": [
":__support_integer_to_string",
- ":__support_osutil_exit_hdrs",
":__support_osutil_io",
],
"//conditions:default": [],
``````````
</details>
https://github.com/llvm/llvm-project/pull/239277
More information about the libc-commits
mailing list