[libcxx-commits] [libcxx] [libc++] Add checks for misused hardening macros (PR #150669)

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Fri Jul 25 11:07:28 PDT 2025


https://github.com/ldionne created https://github.com/llvm/llvm-project/pull/150669

Libc++ hardening went through several iterations, sometimes within a single release. However, some folks in the wide have picked up these macros that were either public at some point or that were used temporarily on `main`, and unfortunately those are now ignored.

This can lead to some users thinking they enable hardening when in reality they don't, which is a pretty big deal. This patch simply checks various old hardening-related macros and ensures that they are not set, which will catch such misuse.

>From 42480f3d9c5dbfab92539f5a2be8c02382870290 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Fri, 25 Jul 2025 14:04:33 -0400
Subject: [PATCH] [libc++] Add checks for misused hardening macros

Libc++ hardening went through several iterations, sometimes within
a single release. However, some folks in the wide have picked up
these macros that were either public at some point or that were
used temporarily on `main`, and unfortunately those are now ignored.

This can lead to some users thinking they enable hardening when in
reality they don't, which is a pretty big deal. This patch simply
checks various old hardening-related macros and ensures that they
are not set, which will catch such misuse.
---
 libcxx/include/__config | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/libcxx/include/__config b/libcxx/include/__config
index 3fe377aac4816..ef7aa0a69ba92 100644
--- a/libcxx/include/__config
+++ b/libcxx/include/__config
@@ -40,9 +40,21 @@
 
 // HARDENING {
 
-// TODO: Remove in LLVM 21. We're making this an error to catch folks who might not have migrated.
-#  ifdef _LIBCPP_ENABLE_ASSERTIONS
-#    error "_LIBCPP_ENABLE_ASSERTIONS has been removed, please use _LIBCPP_HARDENING_MODE instead"
+// TODO: Remove in LLVM 23. We're making these an error to catch folks who might not have migrated.
+//       Since hardening went through several changes (many of which impacted user-facing macros),
+//       we're keeping these checks around for a bit longer than usual. Failure to properly configure
+//       hardening results in checks being dropped silently, which is a pretty big deal.
+#  if defined(_LIBCPP_ENABLE_ASSERTIONS)
+#    error "_LIBCPP_ENABLE_ASSERTIONS has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
+#  endif
+#  if defined(_LIBCPP_ENABLE_HARDENED_MODE)
+#    error "_LIBCPP_ENABLE_HARDENED_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
+#  endif
+#  if defined(_LIBCPP_ENABLE_SAFE_MODE)
+#    error "_LIBCPP_ENABLE_SAFE_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
+#  endif
+#  if defined(_LIBCPP_ENABLE_DEBUG_MODE)
+#    error "_LIBCPP_ENABLE_DEBUG_MODE has been removed, please use _LIBCPP_HARDENING_MODE=<mode> instead (see docs)"
 #  endif
 
 // The library provides the macro `_LIBCPP_HARDENING_MODE` which can be set to one of the following values:



More information about the libcxx-commits mailing list