-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[libc++][hardening] Add a bounds check for valarray and bitset.
#120685
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //===----------------------------------------------------------------------===// | ||
|
Member
Author
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. Note: I found it easier to just test all the assertions in a single file (we have precedent in
Member
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. I think this looks reasonable to me. I don't know that we want to elevate this choice into a policy, but I certainly won't push back on the way you've done it here. |
||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // <valarray> | ||
|
|
||
| // Test hardening assertions for std::valarray. | ||
|
|
||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: libcpp-hardening-mode=none | ||
| // UNSUPPORTED: c++03 | ||
| // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing | ||
|
|
||
| #include <valarray> | ||
|
|
||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| { // Empty valarray | ||
| std::valarray<int> c; | ||
| const auto& const_c = c; | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[0], "valarray::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[0], "valarray::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[42], "valarray::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[42], "valarray::operator[] index out of bounds"); | ||
| } | ||
|
|
||
| { // Non-empty valarray | ||
| std::valarray<int> c(4); | ||
| const auto& const_c = c; | ||
| (void)c[3]; // Check that there's no assertion on valid access. | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[4], "valarray::operator[] index out of bounds"); | ||
| (void)const_c[3]; // Check that there's no assertion on valid access. | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[4], "valarray::operator[] index out of bounds"); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| // <bitset> | ||
|
|
||
| // Test hardening assertions for std::bitset. | ||
|
|
||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: libcpp-hardening-mode=none | ||
| // UNSUPPORTED: c++03 | ||
| // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing | ||
|
|
||
| #include <bitset> | ||
|
|
||
| #include "check_assertion.h" | ||
|
|
||
| int main(int, char**) { | ||
| { // Empty bitset | ||
| std::bitset<0> c; | ||
| const auto& const_c = c; | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[0], "bitset::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[0], "bitset::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[42], "bitset::operator[] index out of bounds"); | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[42], "bitset::operator[] index out of bounds"); | ||
| } | ||
|
|
||
| { // Non-empty bitset | ||
| std::bitset<4> c(42); | ||
| const auto& const_c = c; | ||
| (void)c[3]; // Check that there's no assertion on valid access. | ||
| TEST_LIBCPP_ASSERT_FAILURE(c[4], "bitset::operator[] index out of bounds"); | ||
| (void)const_c[3]; // Check that there's no assertion on valid access. | ||
| TEST_LIBCPP_ASSERT_FAILURE(const_c[4], "bitset::operator[] index out of bounds"); | ||
| } | ||
|
|
||
| return 0; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still hesitant to list
valarrayas fully hardened given the numerous helper classes -- that would require some investigation!