forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++][hardening][NFC] Add macros to enable hardened mode.
This patch only adds new configuration knobs -- the actual assertions will be added in follow-up patches. Differential Revision: https://reviews.llvm.org/D153902
- Loading branch information
Showing
34 changed files
with
529 additions
and
14 deletions.
There are no files selected for viewing
This file contains 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 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 @@ | ||
set(LIBCXX_HARDENING_MODE "debug" CACHE STRING "") |
This file contains 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 @@ | ||
set(LIBCXX_HARDENING_MODE "hardened" CACHE STRING "") |
This file contains 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,41 @@ | ||
============= | ||
Hardened Mode | ||
============= | ||
|
||
.. contents:: | ||
:local: | ||
|
||
.. _using-hardened-mode: | ||
|
||
Using the hardened mode | ||
======================= | ||
|
||
The hardened mode enables a set of security-critical assertions that prevent | ||
undefined behavior caused by violating preconditions of the standard library. | ||
These assertions can be done with relatively little overhead in constant time | ||
and are intended to be used in production. | ||
|
||
In addition to the hardened mode, libc++ also provides the debug mode which | ||
contains all the checks from the hardened mode and additionally more expensive | ||
checks that may affect the complexity of algorithms. The debug mode is intended | ||
to be used for testing, not in production. | ||
|
||
Vendors can set the default hardened mode by using the ``LIBCXX_HARDENING_MODE`` | ||
CMake variable. Setting ``LIBCXX_HARDENING_MODE`` to ``hardened`` enables the | ||
hardened mode, and similarly setting the variable to ``debug`` enables the debug | ||
mode. The default value is ``unchecked`` which doesn't enable the hardened mode. | ||
Users can control whether the hardened mode or the debug mode is enabled | ||
on a per translation unit basis by setting the ``_LIBCPP_ENABLE_HARDENED_MODE`` | ||
or ``_LIBCPP_ENABLE_DEBUG_MODE`` macro to ``1``. | ||
|
||
The hardened mode requires ``LIBCXX_ENABLE_ASSERTIONS`` to work. If | ||
``LIBCXX_ENABLE_ASSERTIONS`` was not set explicitly, enabling the hardened mode | ||
(or the debug mode) will implicitly enable ``LIBCXX_ENABLE_ASSERTIONS``. If | ||
``LIBCXX_ENABLE_ASSERTIONS`` was explicitly disabled, this will effectively | ||
disable the hardened mode. | ||
|
||
Enabling the hardened mode (or the debug mode) has no impact on the ABI. | ||
|
||
Iterator bounds checking | ||
------------------------ | ||
TODO(hardening) |
This file contains 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 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 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 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 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 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 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 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 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 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 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 assertions trigger without the user having to do anything when the debug mode has been enabled | ||
// by default. | ||
|
||
// UNSUPPORTED: !libcpp-has-debug-mode | ||
// `check_assertion.h` is only available starting from C++11. | ||
// UNSUPPORTED: c++03 | ||
// `check_assertion.h` requires Unix headers. | ||
// REQUIRES: has-unix-headers | ||
|
||
#include <cassert> | ||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(true, "Should not fire"); | ||
TEST_LIBCPP_ASSERT_FAILURE([] { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(false, "Should fire"); | ||
}(), "Should fire"); | ||
|
||
return 0; | ||
} |
23 changes: 23 additions & 0 deletions
23
libcxx/test/libcxx/assertions/modes/debug_mode_disabled_in_tu.pass.cpp
This file contains 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,23 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 disable the debug mode on a per-TU basis regardless of how the library was built. | ||
|
||
// TODO(hardening): currently, explicitly enabling assertions enables all uncategorized assertions and overrides | ||
// disabling the debug mode. | ||
// UNSUPPORTED: libcpp-has-hardened-mode, libcpp-has-assertions | ||
// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ENABLE_DEBUG_MODE=0 | ||
|
||
#include <cassert> | ||
|
||
int main(int, char**) { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(true, "Should not fire"); | ||
_LIBCPP_ASSERT_UNCATEGORIZED(false, "Also should not fire"); | ||
|
||
return 0; | ||
} |
31 changes: 31 additions & 0 deletions
31
libcxx/test/libcxx/assertions/modes/debug_mode_enabled_in_tu.pass.cpp
This file contains 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,31 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 enable the debug mode on a per-TU basis regardless of how the library was built. | ||
|
||
// Hardened mode would additionally trigger the error that hardened and debug modes are mutually exclusive. | ||
// UNSUPPORTED: libcpp-has-hardened-mode | ||
// `check_assertion.h` is only available starting from C++11. | ||
// UNSUPPORTED: c++03 | ||
// `check_assertion.h` requires Unix headers. | ||
// REQUIRES: has-unix-headers | ||
// The ability to set a custom abort message is required to compare the assertion message. | ||
// XFAIL: availability-verbose_abort-missing | ||
// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ENABLE_DEBUG_MODE=1 | ||
|
||
#include <cassert> | ||
#include "check_assertion.h" | ||
|
||
int main(int, char**) { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(true, "Should not fire"); | ||
TEST_LIBCPP_ASSERT_FAILURE([] { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(false, "Should fire"); | ||
}(), "Should fire"); | ||
|
||
return 0; | ||
} |
19 changes: 19 additions & 0 deletions
19
libcxx/test/libcxx/assertions/modes/debug_mode_not_1_or_0.verify.cpp
This file contains 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,19 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 debug mode to a value other than `0` or `1` triggers a compile-time error. | ||
|
||
// Hardened mode would additionally trigger the error that hardened and debug modes are mutually exclusive. | ||
// UNSUPPORTED: libcpp-has-hardened-mode | ||
// Modules build produces a different error ("Could not build module 'std'"). | ||
// UNSUPPORTED: modules-build | ||
// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ENABLE_DEBUG_MODE=2 | ||
|
||
#include <cassert> | ||
|
||
// expected-error@*:* {{_LIBCPP_ENABLE_DEBUG_MODE must be set to 0 or 1.}} |
21 changes: 21 additions & 0 deletions
21
libcxx/test/libcxx/assertions/modes/debug_no_assertions.pass.cpp
This file contains 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,21 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Test that we can override whether assertions are enabled regardless of the hardening mode in use. | ||
|
||
// UNSUPPORTED: !libcpp-has-debug-mode | ||
// ADDITIONAL_COMPILE_FLAGS: -Wno-macro-redefined -D_LIBCPP_ENABLE_ASSERTIONS=0 | ||
|
||
#include <cassert> | ||
|
||
int main(int, char**) { | ||
_LIBCPP_ASSERT_UNCATEGORIZED(true, "Should not fire"); | ||
_LIBCPP_ASSERT_UNCATEGORIZED(false, "Also should not fire"); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.