[libc-commits] [libc] [llvm] [libc] Decouple LIBC_ASSERT failure handling and raise SIGABRT in full-build mode (PR #239277)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Sat Nov 7 10:03:39 PST 2026
https://github.com/amanmaurya92 updated https://github.com/llvm/llvm-project/pull/239277
>From a3602734d0cad4d4c089133c4f9ab8066672b20e Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Thu, 5 Nov 2026 21:13:03 +0530
Subject: [PATCH 1/3] [libc] Decouple LIBC_ASSERT failure handling and raise
SIGABRT in full-build mode
Decouple LIBC_ASSERT and LIBC_REQUIRE assertion 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 long-standing FIXME in libc_assert.h:
- Eliminates circular dependencies between __support, stdlib (abort), and signal (RawRwLock / SigAbortGuard) at the header level.
- Calls __assert_fail which triggers abort_utils::abort(), raising SIGABRT in full-build mode and bringing LLVM libc in line with LSB requirements and debugger stop-on-signal expectations.
- Adds an atomic re-entrancy guard in __assert_fail to prevent infinite recursion if an assertion failure occurs inside abort or locking routines.
- Links __assert_fail into hermetic unit test support targets.
- Updates unique_ptr_test to expect SIGABRT across both overlay and full-build configurations.
---
libc/src/__support/libc_assert.h | 22 +++++++++----------
libc/src/assert/generic/CMakeLists.txt | 1 +
libc/src/assert/generic/__assert_fail.cpp | 7 ++++++
libc/test/UnitTest/CMakeLists.txt | 8 +++++++
.../src/__support/CPP/unique_ptr_test.cpp | 6 -----
.../llvm-project-overlay/libc/BUILD.bazel | 1 -
6 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/libc/src/__support/libc_assert.h b/libc/src/__support/libc_assert.h
index 81135fe1b3d501..d4cd63d7303ba5 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/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 2a135457400061..0b40928958a4c7 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -138,6 +138,11 @@ if(LIBC_TEST_SUBPROCESS_TESTS)
libc.include.llvm-libc-types.pid_t
libc.include.llvm-libc-types.struct_pollfd
)
+ if(TARGET libc.src.assert.__assert_fail)
+ list(APPEND libc_death_test_deps
+ libc.src.assert.__assert_fail
+ )
+ endif()
if(TARGET libc.src.sys.prctl.prctl)
list(APPEND libc_death_test_deps
libc.src.sys.prctl.prctl
@@ -171,6 +176,9 @@ set(libc_hermetic_test_support_deps
libc.hdr.stdint_proxy
libc.src.__support.libc_errno
)
+if(TARGET libc.src.assert.__assert_fail)
+ list(APPEND libc_hermetic_test_support_deps libc.src.assert.__assert_fail)
+endif()
if(LIBC_TARGET_OS_IS_BAREMETAL)
if(LIBC_TARGET_ARCHITECTURE_IS_ARM)
list(APPEND libc_hermetic_test_support_srcs ArmBaremetalTestUtils.cpp)
diff --git a/libc/test/src/__support/CPP/unique_ptr_test.cpp b/libc/test/src/__support/CPP/unique_ptr_test.cpp
index 7e26a8e3449b3a..07d0438bc39c59 100644
--- a/libc/test/src/__support/CPP/unique_ptr_test.cpp
+++ b/libc/test/src/__support/CPP/unique_ptr_test.cpp
@@ -260,13 +260,7 @@ TEST(LlvmLibcUniquePtrTest, BaseDerivedConversion) {
}
#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
#define EXPECT_ASSERT_FAILURE(FUNC) EXPECT_DEATH(FUNC, WITH_SIGNAL(SIGABRT))
-#endif
// Test that dereferencing a null unique_ptr asserts.
TEST(LlvmLibcUniquePtrTest, NullDereferenceDeath) {
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": [],
>From ec1c262a8b7a6fff83f3402aa949f3687ab6e146 Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Thu, 5 Nov 2026 23:11:06 +0530
Subject: [PATCH 2/3] [libc] Fix CI failures for LIBC_ASSERT abort decoupling
---
libc/cmake/modules/LLVMLibCTestRules.cmake | 3 +++
libc/src/__support/libc_assert.h | 2 +-
libc/test/UnitTest/CMakeLists.txt | 8 --------
libc/test/src/__support/CPP/unique_ptr_test.cpp | 4 ++--
4 files changed, 6 insertions(+), 11 deletions(-)
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 d4cd63d7303ba5..f2651990f3b685 100644
--- a/libc/src/__support/libc_assert.h
+++ b/libc/src/__support/libc_assert.h
@@ -53,7 +53,7 @@ namespace LIBC_NAMESPACE_DECL {
#define LIBC_REQUIRE(COND, MSG) \
do { \
if (LIBC_UNLIKELY(!(COND))) \
- LIBC_NAMESPACE::__assert_fail(MSG, __FILE__, __LINE__, \
+ LIBC_NAMESPACE::__assert_fail(MSG, __FILE__, __LINE__, \
__PRETTY_FUNCTION__); \
} while (false)
#endif // LIBC_FULL_BUILD
diff --git a/libc/test/UnitTest/CMakeLists.txt b/libc/test/UnitTest/CMakeLists.txt
index 0b40928958a4c7..2a135457400061 100644
--- a/libc/test/UnitTest/CMakeLists.txt
+++ b/libc/test/UnitTest/CMakeLists.txt
@@ -138,11 +138,6 @@ if(LIBC_TEST_SUBPROCESS_TESTS)
libc.include.llvm-libc-types.pid_t
libc.include.llvm-libc-types.struct_pollfd
)
- if(TARGET libc.src.assert.__assert_fail)
- list(APPEND libc_death_test_deps
- libc.src.assert.__assert_fail
- )
- endif()
if(TARGET libc.src.sys.prctl.prctl)
list(APPEND libc_death_test_deps
libc.src.sys.prctl.prctl
@@ -176,9 +171,6 @@ set(libc_hermetic_test_support_deps
libc.hdr.stdint_proxy
libc.src.__support.libc_errno
)
-if(TARGET libc.src.assert.__assert_fail)
- list(APPEND libc_hermetic_test_support_deps libc.src.assert.__assert_fail)
-endif()
if(LIBC_TARGET_OS_IS_BAREMETAL)
if(LIBC_TARGET_ARCHITECTURE_IS_ARM)
list(APPEND libc_hermetic_test_support_srcs ArmBaremetalTestUtils.cpp)
diff --git a/libc/test/src/__support/CPP/unique_ptr_test.cpp b/libc/test/src/__support/CPP/unique_ptr_test.cpp
index 07d0438bc39c59..5279baf0deb24d 100644
--- a/libc/test/src/__support/CPP/unique_ptr_test.cpp
+++ b/libc/test/src/__support/CPP/unique_ptr_test.cpp
@@ -259,7 +259,7 @@ TEST(LlvmLibcUniquePtrTest, BaseDerivedConversion) {
ASSERT_EQ(base_ptr->val, 100);
}
-#ifndef NDEBUG
+#if !defined(NDEBUG) && defined(SIGABRT)
#define EXPECT_ASSERT_FAILURE(FUNC) EXPECT_DEATH(FUNC, WITH_SIGNAL(SIGABRT))
// Test that dereferencing a null unique_ptr asserts.
@@ -274,4 +274,4 @@ TEST(LlvmLibcUniquePtrTest, NullDereferenceDeath) {
unique_ptr<int[]> arr;
EXPECT_ASSERT_FAILURE([&] { (void)arr[0]; });
}
-#endif // NDEBUG
+#endif // !defined(NDEBUG) && defined(SIGABRT)
>From b2b91f26b49b6a257061ab77aa9792b35d95216c Mon Sep 17 00:00:00 2001
From: amanmaurya92 <amanmaurya9209 at gmail.com>
Date: Sat, 7 Nov 2026 23:32:20 +0530
Subject: [PATCH 3/3] [libc][bazel] Add __assert_fail target and dependencies
for full-build mode
---
utils/bazel/llvm-project-overlay/libc/BUILD.bazel | 14 ++++++++++++++
.../libc/test/UnitTest/BUILD.bazel | 1 +
.../libc/test/libc_test_rules.bzl | 1 +
3 files changed, 16 insertions(+)
diff --git a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
index a52b871270b536..a3408eabe273d4 100644
--- a/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/BUILD.bazel
@@ -17177,6 +17177,20 @@ libc_support_library(
],
)
+libc_support_library(
+ name = "__assert_fail",
+ srcs = ["src/assert/generic/__assert_fail.cpp"],
+ hdrs = ["src/assert/__assert_fail.h"],
+ full_build_only = True,
+ deps = [
+ ":__support_cpp_atomic",
+ ":__support_libc_assert",
+ ":__support_macros_config",
+ ":__support_osutil_io",
+ ":stdlib_abort_utils",
+ ],
+)
+
libc_support_library(
name = "qsort_util",
hdrs = [
diff --git a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
index bc1be3481c3587..35ed4d663f657f 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/libc/test/UnitTest/BUILD.bazel
@@ -124,6 +124,7 @@ libc_test_library(
"//conditions:default": ["@platforms//:incompatible"],
}),
deps = [
+ "//libc:__assert_fail",
"//libc:__support_common",
"//libc:__support_libc_errno",
"//libc:__support_macros_config",
diff --git a/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl b/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl
index 54f9cdaac025dc..116ecd2a5789c4 100644
--- a/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl
+++ b/utils/bazel/llvm-project-overlay/libc/test/libc_test_rules.bzl
@@ -66,6 +66,7 @@ def libc_test(
# The compiler may emit references to symbols it expects to exist,
# like `memset`. These dependencies hermetically provide them.
"//libc/test/UnitTest:HermeticTestUtils",
+ "//libc:__assert_fail",
"//libc:__stack_chk_fail", # Needed if -fstack-protector is set.
],
"//conditions:default": [],
More information about the libc-commits
mailing list