-
Notifications
You must be signed in to change notification settings - Fork 14.6k
[libc++][hardening] Introduce assertion semantics. #149459
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
Open
var-const
wants to merge
5
commits into
llvm:main
Choose a base branch
from
var-const:varconst/hardening-semantics-introduce-2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+353
−52
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
77aca80
[libc++][hardening] Introduce assertion semantics.
var-const 10fa9bd
Remove a not-really-necessary change
var-const 815cd72
Address feedback
var-const 93dfb6c
Address feedback
var-const cd2bd37
Fix the CI (frozen C++03 headers)
var-const File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
libcxx/cmake/caches/Generic-hardening-mode-extensive-observe-semantic.cmake
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
set(LIBCXX_HARDENING_MODE "extensive" CACHE STRING "") | ||
set(LIBCXX_TEST_PARAMS "assertion_semantic=observe" CACHE STRING "") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,30 @@ | |
# define _LIBCPP_FREESTANDING | ||
# endif | ||
|
||
// NOLINTNEXTLINE(libcpp-cpp-version-check) | ||
# if __cplusplus < 201103L | ||
# define _LIBCPP_CXX03_LANG | ||
# endif | ||
|
||
# if __has_feature(experimental_library) | ||
# ifndef _LIBCPP_ENABLE_EXPERIMENTAL | ||
# define _LIBCPP_ENABLE_EXPERIMENTAL | ||
# endif | ||
# endif | ||
|
||
// Incomplete features get their own specific disabling flags. This makes it | ||
// easier to grep for target specific flags once the feature is complete. | ||
# if defined(_LIBCPP_ENABLE_EXPERIMENTAL) || defined(_LIBCPP_BUILDING_LIBRARY) | ||
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 1 | ||
# else | ||
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 0 | ||
# endif | ||
|
||
# define _LIBCPP_HAS_EXPERIMENTAL_PSTL _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
|
||
// HARDENING { | ||
|
||
// TODO: Remove in LLVM 21. We're making this an error to catch folks who might not have migrated. | ||
|
@@ -147,16 +171,53 @@ _LIBCPP_HARDENING_MODE_EXTENSIVE, \ | |
_LIBCPP_HARDENING_MODE_DEBUG | ||
# endif | ||
|
||
// Hardening assertion semantics generally mirror the evaluation semantics of C++26 Contracts: | ||
// - `ignore` evaluates the assertion but doesn't do anything if it fails (note that it differs from the Contracts | ||
// `ignore` semantic which wouldn't evaluate the assertion at all); | ||
// - `observe` logs an error (indicating, if possible, that the error is fatal) and continues execution; | ||
// - `quick-enforce` terminates the program as fast as possible (via trapping); | ||
// - `enforce` logs an error and then terminates the program. | ||
// | ||
// Notes: | ||
// - Continuing execution after a hardening check fails results in undefined behavior; the `observe` semantic is meant | ||
// to make adopting hardening easier but should not be used outside of this scenario; | ||
// - C++26 wording for Library Hardening precludes a conforming Hardened implementation from using the Contracts | ||
// `ignore` semantic when evaluating hardened preconditions in the Library. Libc++ allows using this semantic for | ||
// hardened preconditions, however, be aware that using `ignore` does not produce a conforming "Hardened" | ||
// implementation, unlike the other semantics above. | ||
// clang-format off | ||
# define _LIBCPP_ASSERTION_SEMANTIC_IGNORE (1 << 1) | ||
# define _LIBCPP_ASSERTION_SEMANTIC_OBSERVE (1 << 2) | ||
# define _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE (1 << 3) | ||
# define _LIBCPP_ASSERTION_SEMANTIC_ENFORCE (1 << 4) | ||
// clang-format on | ||
|
||
// Allow users to define an arbitrary assertion semantic; otherwise, use the default mapping from modes to semantics. | ||
// The default is for production-capable modes to use `quick-enforce` (i.e., trap) and for the `debug` mode to use | ||
// `enforce` (i.e., log and abort). | ||
# ifndef _LIBCPP_ASSERTION_SEMANTIC | ||
var-const marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG | ||
# define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_ENFORCE | ||
# else | ||
# define _LIBCPP_ASSERTION_SEMANTIC _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE | ||
# endif | ||
|
||
# else | ||
# if !_LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# error "Assertion semantics are an experimental feature." | ||
# endif | ||
# if defined(_LIBCPP_CXX03_LANG) | ||
# error "Assertion semantics are not available in the C++03 mode." | ||
var-const marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# endif | ||
|
||
# endif // _LIBCPP_ASSERTION_SEMANTIC | ||
|
||
// } HARDENING | ||
|
||
# define _LIBCPP_TOSTRING2(x) #x | ||
# define _LIBCPP_TOSTRING(x) _LIBCPP_TOSTRING2(x) | ||
|
||
// NOLINTNEXTLINE(libcpp-cpp-version-check) | ||
# if __cplusplus < 201103L | ||
# define _LIBCPP_CXX03_LANG | ||
# endif | ||
Comment on lines
-156
to
-158
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about moving this to |
||
|
||
# ifndef __has_constexpr_builtin | ||
# define __has_constexpr_builtin(x) 0 | ||
# endif | ||
|
@@ -190,25 +251,6 @@ _LIBCPP_HARDENING_MODE_DEBUG | |
# define _LIBCPP_ABI_VCRUNTIME | ||
# endif | ||
|
||
# if __has_feature(experimental_library) | ||
# ifndef _LIBCPP_ENABLE_EXPERIMENTAL | ||
# define _LIBCPP_ENABLE_EXPERIMENTAL | ||
# endif | ||
# endif | ||
|
||
// Incomplete features get their own specific disabling flags. This makes it | ||
// easier to grep for target specific flags once the feature is complete. | ||
# if defined(_LIBCPP_ENABLE_EXPERIMENTAL) || defined(_LIBCPP_BUILDING_LIBRARY) | ||
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 1 | ||
# else | ||
# define _LIBCPP_HAS_EXPERIMENTAL_LIBRARY 0 | ||
# endif | ||
|
||
# define _LIBCPP_HAS_EXPERIMENTAL_PSTL _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_TZDB _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_SYNCSTREAM _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
# define _LIBCPP_HAS_EXPERIMENTAL_HARDENING_OBSERVE_SEMANTIC _LIBCPP_HAS_EXPERIMENTAL_LIBRARY | ||
|
||
# if defined(__MVS__) | ||
# include <features.h> // for __NATIVE_ASCII_F | ||
# endif | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.