[libcxx-commits] [libcxx] [libc++] Automatically detect the libc++ hardening mode from the test suite (PR #172505)
Louis Dionne via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Jan 20 09:26:45 PST 2026
https://github.com/ldionne updated https://github.com/llvm/llvm-project/pull/172505
>From 9cad974814c003848b2a46922e01b75dcce980e8 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 16 Dec 2025 10:44:23 -0500
Subject: [PATCH 1/4] [libc++] Automatically detect the libc++ hardening mode
from the test suite
This prevents hardcoding the hardening mode via compiler flags, and
allows testing what the default hardening mode is on platforms that
set it to something that isn't `none`. Otherwise, a platform setting
a default (which is done via -DLIBCXX_HARDENING_MODE=mode at CMake
configuration time) would end up passing `-D_LIBCPP_HARDENING_MODE=mode`
to the compiler, which does not allow checking what the default mode is.
---
libcxx/test/CMakeLists.txt | 2 --
.../libcxx/test/features/libcxx_macros.py | 18 +++++++++++++++++-
libcxx/utils/libcxx/test/params.py | 1 -
3 files changed, 17 insertions(+), 4 deletions(-)
diff --git a/libcxx/test/CMakeLists.txt b/libcxx/test/CMakeLists.txt
index 6294319815b42..39d383922e1df 100644
--- a/libcxx/test/CMakeLists.txt
+++ b/libcxx/test/CMakeLists.txt
@@ -41,8 +41,6 @@ if (NOT LIBCXX_ENABLE_RTTI)
serialize_lit_param(SERIALIZED_LIT_PARAMS enable_rtti False)
endif()
-serialize_lit_string_param(SERIALIZED_LIT_PARAMS hardening_mode "${LIBCXX_HARDENING_MODE}")
-
if (CMAKE_CXX_COMPILER_TARGET)
serialize_lit_string_param(SERIALIZED_LIT_PARAMS target_triple "${CMAKE_CXX_COMPILER_TARGET}")
else()
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 7a465f2e87866..50a4ab9d300e2 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -6,7 +6,7 @@
#
# ===----------------------------------------------------------------------===##
-from libcxx.test.dsl import Feature, compilerMacros
+from libcxx.test.dsl import Feature, compilerMacros, programSucceeds
features = []
@@ -74,3 +74,19 @@
and compilerMacros(cfg)[m] == "0",
)
)
+
+for mode in ("none", "fast", "extensive", "debug"):
+ check_program = f"""
+ #include <cassert>
+ #if defined(_LIBCPP_HARDENING_MODE)
+ int main(int, char**) {{ return _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_{mode.upper()} ? 0 : 1; }}
+ #else
+ int main(int, char**) {{ return 0; }}
+ #endif
+ """
+ features.append(
+ Feature(
+ name=f"libcpp-hardening-mode={mode}",
+ when=lambda cfg: programSucceeds(cfg, check_program)
+ )
+ )
diff --git a/libcxx/utils/libcxx/test/params.py b/libcxx/utils/libcxx/test/params.py
index 299aa28777fd5..b0e0c48873eb3 100644
--- a/libcxx/utils/libcxx/test/params.py
+++ b/libcxx/utils/libcxx/test/params.py
@@ -419,7 +419,6 @@ def getSuitableClangTidy(cfg):
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST") if hardening_mode == "fast" else None,
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE") if hardening_mode == "extensive" else None,
AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG") if hardening_mode == "debug" else None,
- AddFeature("libcpp-hardening-mode={}".format(hardening_mode)) if hardening_mode != "undefined" else None,
],
),
),
>From 23c3a8ececae102711f94f8d1b9c94c9f7ffe8f6 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Wed, 14 Jan 2026 17:27:41 -0500
Subject: [PATCH 2/4] Don't detect a hardening mode when we're not on libc++
---
libcxx/utils/libcxx/test/features/libcxx_macros.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 50a4ab9d300e2..8ed96e960592b 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -81,7 +81,7 @@
#if defined(_LIBCPP_HARDENING_MODE)
int main(int, char**) {{ return _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_{mode.upper()} ? 0 : 1; }}
#else
- int main(int, char**) {{ return 0; }}
+ int main(int, char**) {{ return 1; }}
#endif
"""
features.append(
>From ce8c73d25444b95f6b21cc29fa8a2f727c58b34f Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 20 Jan 2026 11:25:09 -0500
Subject: [PATCH 3/4] Fix scope issue with Python
---
libcxx/utils/libcxx/test/features/libcxx_macros.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 8ed96e960592b..4dca6cfe1938f 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -87,6 +87,6 @@
features.append(
Feature(
name=f"libcpp-hardening-mode={mode}",
- when=lambda cfg: programSucceeds(cfg, check_program)
+ when=lambda cfg, prog=check_program: programSucceeds(cfg, prog)
)
)
>From 7493b13cd544a02a3315ac52f4ae091273c1889c Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 20 Jan 2026 12:26:30 -0500
Subject: [PATCH 4/4] Don't include C++ header
---
libcxx/utils/libcxx/test/features/libcxx_macros.py | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 4dca6cfe1938f..a5983a80303c1 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -77,7 +77,7 @@
for mode in ("none", "fast", "extensive", "debug"):
check_program = f"""
- #include <cassert>
+ #include <stddef.h> // any header to get the definitions
#if defined(_LIBCPP_HARDENING_MODE)
int main(int, char**) {{ return _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_{mode.upper()} ? 0 : 1; }}
#else
More information about the libcxx-commits
mailing list