Skip to content

[libc++] Add checks for misused hardening macros #150669

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 31, 2025

Conversation

ldionne
Copy link
Member

@ldionne ldionne commented Jul 25, 2025

Libc++ hardening went through several iterations, sometimes within a single release. However, some folks in the wild 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.

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.
@ldionne ldionne added this to the LLVM 21.x Release milestone Jul 25, 2025
@ldionne ldionne requested a review from a team as a code owner July 25, 2025 18:07
@github-project-automation github-project-automation bot moved this to Needs Triage in LLVM Release Status Jul 25, 2025
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Jul 25, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 25, 2025

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/150669.diff

1 Files Affected:

  • (modified) libcxx/include/__config (+15-3)
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:

@ldionne ldionne added the hardening Issues related to the hardening effort label Jul 25, 2025
@tru tru moved this from Needs Triage to Needs Fix in LLVM Release Status Jul 28, 2025
Co-authored-by: Nikolas Klauser <[email protected]>
@ldionne
Copy link
Member Author

ldionne commented Jul 29, 2025

@philnik777 Are you OK with this? This originated when a user reached out to me and we realized that they had hardening turned off because they had been an early adopter of the feature. So I think there's value in catching that explicitly.

@github-project-automation github-project-automation bot moved this from Needs Fix to Needs Merge in LLVM Release Status Jul 30, 2025
@ldionne ldionne merged commit 955ece4 into llvm:main Jul 31, 2025
71 of 76 checks passed
@github-project-automation github-project-automation bot moved this from Needs Merge to Done in LLVM Release Status Jul 31, 2025
@ldionne ldionne deleted the review/catch-hardened-mode-misuse branch July 31, 2025 19:18
@ldionne
Copy link
Member Author

ldionne commented Jul 31, 2025

/cherry-pick 955ece4

@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2025

/pull-request #151582

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hardening Issues related to the hardening effort libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
Development

Successfully merging this pull request may close these issues.

3 participants