-
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 1 commit
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 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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,43 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 using ABI v1 (where the const overload of `operator[]` returns | ||
|
||
| // `const_reference` which is non-Standard behavior). | ||
|
|
||
| // 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; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // 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 using ABI >= v2 (where the const overload of `operator[]` returns `bool` as | ||
| // mandated by the Standard). | ||
|
|
||
| // REQUIRES: has-unix-headers | ||
| // UNSUPPORTED: libcpp-hardening-mode=none | ||
| // UNSUPPORTED: c++03 | ||
| // XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing | ||
|
|
||
| // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL=1 | ||
|
|
||
| #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.
Note: I found it easier to just test all the assertions in a single file (we have precedent in
deque). We could split by hardening mode later, but I think having a separate test file per hardened function is more trouble than it's worth. No strong feelings, though -- please feel free to push back on this.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 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.