-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc++][hardening] Allow setting the assertion semantic via CMake. #167636
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
var-const
merged 11 commits into
llvm:main
from
var-const:varconst-assertion-semantic-via-cmake
Nov 14, 2025
+327
−25
Merged
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a3a6fc
[libc++][hardening] Allow setting the assertion semantic via CMake.
var-const 827c6ab
Fix the check for empty string in `CMakeLists.txt`.
var-const 2a2469b
Fix formatting.
var-const 5208926
- Add new tests to check that the choice of the assertion semantic is
var-const b0f8174
Make `hardening_dependent` a special value for choosing the assertion
var-const 096249d
CI fixes (formatting, disable semantic-related tests in the C++03 mode)
var-const 9b0daf4
Fix CI (formatting, make semantics tests require experimental)
var-const f613fde
- Reflect the assertion semantic in the library ABI tag (with a test);
var-const ae0a272
Address feedback: update documentation, marka test unsupported in C++03.
var-const 76119b1
Fix the check for C++03 in a test
var-const a6c5e9f
Mark a test as unsupported, take 3
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
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
71 changes: 71 additions & 0 deletions
71
libcxx/test/extensions/libcxx/odr_signature.assertion_semantics.sh.cpp
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,71 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // ABI tags have no effect in MSVC mode. | ||
| // XFAIL: msvc | ||
|
|
||
| // XFAIL: FROZEN-CXX03-HEADERS-FIXME | ||
var-const marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Test that we encode the assertion semantic in an ABI tag to avoid ODR violations when linking TUs that have different | ||
| // values for it. | ||
|
|
||
| // Note that GCC doesn't support `-Wno-macro-redefined`. | ||
var-const marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU1 -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_IGNORE -o %t.tu1.o | ||
| // RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU2 -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE -o %t.tu2.o | ||
| // RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU3 -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE -o %t.tu3.o | ||
| // RUN: %{cxx} %s %{flags} %{compile_flags} -c -DTU4 -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_ENFORCE -o %t.tu4.o | ||
| // RUN: %{cxx} %s %{flags} %{compile_flags} -c -DMAIN -o %t.main.o | ||
| // RUN: %{cxx} %t.tu1.o %t.tu2.o %t.tu3.o %t.tu4.o %t.main.o %{flags} %{link_flags} -o %t.exe | ||
| // RUN: %{exec} %t.exe | ||
|
|
||
| #include "test_macros.h" | ||
|
|
||
| // `ignore` assertion semantic. | ||
| #ifdef TU1 | ||
| # include <__config> | ||
| _LIBCPP_HIDE_FROM_ABI TEST_NOINLINE inline int f() { return 1; } | ||
| int tu1() { return f(); } | ||
| #endif // TU1 | ||
|
|
||
| // `observe` assertion semantic. | ||
| #ifdef TU2 | ||
| # include <__config> | ||
| _LIBCPP_HIDE_FROM_ABI TEST_NOINLINE inline int f() { return 2; } | ||
| int tu2() { return f(); } | ||
| #endif // TU2 | ||
|
|
||
| // `quick-enforce` assertion semantic. | ||
| #ifdef TU3 | ||
| # include <__config> | ||
| _LIBCPP_HIDE_FROM_ABI TEST_NOINLINE inline int f() { return 3; } | ||
| int tu3() { return f(); } | ||
| #endif // TU3 | ||
|
|
||
| // `enforce` assertion semantic. | ||
| #ifdef TU4 | ||
| # include <__config> | ||
| _LIBCPP_HIDE_FROM_ABI TEST_NOINLINE inline int f() { return 4; } | ||
| int tu4() { return f(); } | ||
| #endif // TU4 | ||
|
|
||
| #ifdef MAIN | ||
| # include <cassert> | ||
|
|
||
| int tu1(); | ||
| int tu2(); | ||
| int tu3(); | ||
| int tu4(); | ||
|
|
||
| int main(int, char**) { | ||
| assert(tu1() == 1); | ||
| assert(tu2() == 2); | ||
| assert(tu3() == 3); | ||
| assert(tu4() == 4); | ||
| return 0; | ||
| } | ||
| #endif // MAIN | ||
29 changes: 29 additions & 0 deletions
29
libcxx/test/libcxx/assertions/semantics/assertion_semantic_incorrect_value.sh.cpp
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,29 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // This test verifies that setting the assertion semantic to a value that's not part of the predefined constants | ||
| // triggers a compile-time error. | ||
|
|
||
| // Modules build produces a different error ("Could not build module 'std'"). | ||
| // UNSUPPORTED: clang-modules-build | ||
| // UNSUPPORTED: c++03, libcpp-has-no-experimental-hardening-observe-semantic | ||
| // REQUIRES: verify-support | ||
|
|
||
| // RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=42 | ||
| // `hardening-dependent` cannot be set as the semantic (it's only an indicator to use hardening-related logic to pick | ||
| // the final semantic). | ||
| // RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_HARDENING_DEPENDENT | ||
| // Make sure that common cases of misuse produce readable errors. We deliberately disallow setting the assertion | ||
| // semantic as if it were a boolean flag. | ||
| // RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=0 | ||
| // RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=1 | ||
| // RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC | ||
var-const marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| #include <cassert> | ||
|
|
||
| // expected-error@*:* {{_LIBCPP_ASSERTION_SEMANTIC must be set to one of the following values: _LIBCPP_ASSERTION_SEMANTIC_IGNORE, _LIBCPP_ASSERTION_SEMANTIC_OBSERVE, _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE, _LIBCPP_ASSERTION_SEMANTIC_ENFORCE}} | ||
29 changes: 29 additions & 0 deletions
29
libcxx/test/libcxx/assertions/semantics/override_with_enforce_semantic.pass.cpp
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,29 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // This test ensures that we can override the assertion semantic used by any checked hardening mode with `enforce` on | ||
| // a per-TU basis (this is valid for the `debug` mode as well, though a no-op). | ||
|
|
||
| // `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support. | ||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: c++03, no-localization | ||
| // UNSUPPORTED: libcpp-hardening-mode=none, libcpp-has-no-experimental-hardening-observe-semantic | ||
| // The ability to set a custom abort message is required to compare the assertion message. | ||
| // XFAIL: availability-verbose_abort-missing | ||
| // ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_ENFORCE | ||
|
|
||
| #include <cassert> | ||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire"); | ||
| TEST_LIBCPP_ASSERT_FAILURE([] { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Should fire and log a message"); }(), | ||
| "Should fire and log a message"); | ||
|
|
||
| return 0; | ||
| } |
26 changes: 26 additions & 0 deletions
26
libcxx/test/libcxx/assertions/semantics/override_with_ignore_semantic.pass.cpp
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,26 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // This test ensures that we can override the assertion semantic used by any hardening mode with `ignore` on a per-TU | ||
| // basis (this is valid for the `none` mode as well, though a no-op). | ||
|
|
||
| // `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support. | ||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: c++03, no-localization | ||
| // UNSUPPORTED: libcpp-has-no-experimental-hardening-observe-semantic | ||
| // ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_IGNORE | ||
|
|
||
| #include <cassert> | ||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire"); | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Also should not fire"); | ||
|
|
||
| return 0; | ||
| } |
27 changes: 27 additions & 0 deletions
27
libcxx/test/libcxx/assertions/semantics/override_with_observe_semantic.pass.cpp
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,27 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // This test ensures that we can override the assertion semantic used by any checked hardening mode with `observe` on | ||
| // a per-TU basis. | ||
|
|
||
| // `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support. | ||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: c++03, no-localization | ||
| // UNSUPPORTED: libcpp-hardening-mode=none, libcpp-has-no-experimental-hardening-observe-semantic | ||
| // ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE | ||
|
|
||
| #include <cassert> | ||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire"); | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Also should not fire"); | ||
| // TODO(hardening): check that a message is logged. | ||
|
|
||
| return 0; | ||
| } |
28 changes: 28 additions & 0 deletions
28
libcxx/test/libcxx/assertions/semantics/override_with_quick_enforce_semantic.pass.cpp
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,28 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // This test ensures that we can override the assertion semantic used by any checked hardening mode with `quick-enforce` | ||
| // on a per-TU basis (this is valid for the `fast` and `extensive` modes as well, though a no-op). | ||
|
|
||
| // `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support. | ||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: c++03, no-localization | ||
| // UNSUPPORTED: libcpp-hardening-mode=none, libcpp-has-no-experimental-hardening-observe-semantic | ||
| // ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE | ||
|
|
||
| #include <cassert> | ||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire"); | ||
| TEST_LIBCPP_ASSERT_FAILURE( | ||
| [] { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Should fire without logging a message"); }(), | ||
| "The message should not matter"); | ||
|
|
||
| return 0; | ||
| } |
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.