Skip to content

Commit 5208926

Browse files
committed
- Add new tests to check that the choice of the assertion semantic is
orthogonal to the selected hardening mode; - Tweak some checks in existing tests to rely on the assertion semantic rather than using the hardening mode as a proxy.
1 parent 2a2469b commit 5208926

File tree

6 files changed

+139
-5
lines changed

6 files changed

+139
-5
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test verifies that setting the assertion semantic to a value that's not part of the predefined constants
10+
// triggers a compile-time error.
11+
12+
// Modules build produces a different error ("Could not build module 'std'").
13+
// UNSUPPORTED: clang-modules-build
14+
// REQUIRES: verify-support
15+
16+
// Note that GCC doesn't support `-Wno-macro-redefined`.
17+
// RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=42
18+
// Make sure that common cases of misuse produce readable errors. We deliberately disallow setting the assertion
19+
// semantic as if it were a boolean flag.
20+
// RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=0
21+
// RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=1
22+
// RUN: %{verify} -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC
23+
24+
#include <cassert>
25+
26+
// 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}}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test ensures that we can override the assertion semantic used by any checked hardening mode with `enforce` (this
10+
// is valid for the `debug` mode as well, though a no-op).
11+
12+
// `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support.
13+
// REQUIRES: has-unix-headers
14+
// UNSUPPORTED: c++03, no-localization
15+
// UNSUPPORTED: libcpp-hardening-mode=none
16+
// The ability to set a custom abort message is required to compare the assertion message.
17+
// XFAIL: availability-verbose_abort-missing
18+
// ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_ENFORCE
19+
20+
#include <cassert>
21+
#include "check_assertion.h"
22+
23+
int main(int, char**) {
24+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire");
25+
TEST_LIBCPP_ASSERT_FAILURE([] { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Should fire and log a message"); }(),
26+
"Should fire and log a message");
27+
28+
return 0;
29+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test ensures that we can override the assertion semantic used by any hardening mode with `ignore` (this is valid
10+
// for the `none` mode as well, though a no-op).
11+
12+
// `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support.
13+
// REQUIRES: has-unix-headers
14+
// UNSUPPORTED: c++03, no-localization
15+
// ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_IGNORE
16+
17+
#include <cassert>
18+
#include "check_assertion.h"
19+
20+
int main(int, char**) {
21+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire");
22+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Also should not fire");
23+
24+
return 0;
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test ensures that we can override the assertion semantic used by any checked hardening mode with `observe`.
10+
11+
// `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support.
12+
// REQUIRES: has-unix-headers
13+
// UNSUPPORTED: c++03, no-localization
14+
// UNSUPPORTED: libcpp-hardening-mode=none
15+
// ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_OBSERVE
16+
17+
#include <cassert>
18+
#include "check_assertion.h"
19+
20+
int main(int, char**) {
21+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire");
22+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Also should not fire");
23+
// TODO(hardening): check that a message is logged.
24+
25+
return 0;
26+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// This test ensures that we can override the assertion semantic used by any checked hardening mode with `quick-enforce`
10+
// (this is valid for the `fast` and `extensive` modes as well, though a no-op).
11+
12+
// `check_assertion.h` is only available starting from C++11 and requires Unix headers and regex support.
13+
// REQUIRES: has-unix-headers
14+
// UNSUPPORTED: c++03, no-localization
15+
// UNSUPPORTED: libcpp-hardening-mode=none
16+
// ADDITIONAL_COMPILE_FLAGS: -U_LIBCPP_ASSERTION_SEMANTIC -D_LIBCPP_ASSERTION_SEMANTIC=_LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
17+
18+
#include <cassert>
19+
#include "check_assertion.h"
20+
21+
int main(int, char**) {
22+
_LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(true, "Should not fire");
23+
TEST_LIBCPP_ASSERT_FAILURE([] { _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(false, "Should fire without logging a message"); }(),
24+
"The message should not matter");
25+
26+
return 0;
27+
}
28+

libcxx/test/support/test.support/test_check_assertion.pass.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ template <class Func>
2121
bool TestDeathTest(
2222
Outcome expected_outcome, DeathCause expected_cause, const char* stmt, Func&& func, const Matcher& matcher) {
2323
auto get_matcher = [&] {
24-
#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
25-
return matcher;
26-
#else
24+
#if _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
2725
(void)matcher;
2826
return MakeAnyMatcher();
27+
#else
28+
return matcher;
2929
#endif
3030
};
3131

@@ -69,7 +69,7 @@ bool TestDeathTest(
6969

7070
// clang-format on
7171

72-
#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
72+
#if _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
7373
DeathCause assertion_death_cause = DeathCause::VerboseAbort;
7474
#else
7575
DeathCause assertion_death_cause = DeathCause::Trap;
@@ -99,7 +99,7 @@ int main(int, char**) {
9999
// Success -- assertion failure with a specific matcher.
100100
TEST_DEATH_TEST_MATCHES(Outcome::Success, assertion_death_cause, good_matcher, fail_assert());
101101

102-
# if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
102+
#if _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
103103
// Failure -- error message doesn't match.
104104
TEST_DEATH_TEST_MATCHES(Outcome::UnexpectedErrorMessage, assertion_death_cause, bad_matcher, fail_assert());
105105
# endif

0 commit comments

Comments
 (0)