[libcxx-commits] [libcxx] ac1d560 - [libc++][hardening] Add a bounds check for `valarray` and `bitset`. (#120685)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 24 18:22:22 PST 2024
Author: Konstantin Varlamov
Date: 2024-12-24T18:22:18-08:00
New Revision: ac1d560709d2ecfe83a98285d4a13afae6db4316
URL: https://github.com/llvm/llvm-project/commit/ac1d560709d2ecfe83a98285d4a13afae6db4316
DIFF: https://github.com/llvm/llvm-project/commit/ac1d560709d2ecfe83a98285d4a13afae6db4316.diff
LOG: [libc++][hardening] Add a bounds check for `valarray` and `bitset`. (#120685)
Add a `valid-element-access` check to `valarray::operator[]` and
`bitset::operator[]`.
Added:
libcxx/test/libcxx/numerics/numarray/assert.pass.cpp
libcxx/test/libcxx/utilities/template.bitset/assert.pass.cpp
Modified:
libcxx/docs/Hardening.rst
libcxx/include/bitset
libcxx/include/valarray
libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp
Removed:
################################################################################
diff --git a/libcxx/docs/Hardening.rst b/libcxx/docs/Hardening.rst
index 42aacfdcfb41a7..4002f40e1dad33 100644
--- a/libcxx/docs/Hardening.rst
+++ b/libcxx/docs/Hardening.rst
@@ -458,7 +458,7 @@ Hardened containers status
- Partial
- N/A
* - ``bitset``
- - ❌
+ - ✅
- N/A
Note: for ``vector`` and ``string``, the iterator does not check for
diff --git a/libcxx/include/bitset b/libcxx/include/bitset
index 8b361824805571..919d2a0f07e096 100644
--- a/libcxx/include/bitset
+++ b/libcxx/include/bitset
@@ -133,6 +133,7 @@ template <size_t N> struct hash<std::bitset<N>>;
# include <__algorithm/fill.h>
# include <__algorithm/fill_n.h>
# include <__algorithm/find.h>
+# include <__assert>
# include <__bit_reference>
# include <__config>
# include <__functional/hash.h>
@@ -683,13 +684,18 @@ public:
// element access:
# ifdef _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL
- _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const { return __base::__make_ref(__p); }
+ _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR bool operator[](size_t __p) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
+ return __base::__make_ref(__p);
+ }
# else
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR const_reference operator[](size_t __p) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
return __base::__make_ref(__p);
}
# endif
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 reference operator[](size_t __p) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__p < _Size, "bitset::operator[] index out of bounds");
return __base::__make_ref(__p);
}
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX23 unsigned long to_ulong() const;
diff --git a/libcxx/include/valarray b/libcxx/include/valarray
index d0b76ee06e796c..abc7d391ada070 100644
--- a/libcxx/include/valarray
+++ b/libcxx/include/valarray
@@ -821,9 +821,15 @@ public:
_LIBCPP_HIDE_FROM_ABI valarray& operator=(const __val_expr<_ValExpr>& __v);
// element access:
- _LIBCPP_HIDE_FROM_ABI const value_type& operator[](size_t __i) const { return __begin_[__i]; }
+ _LIBCPP_HIDE_FROM_ABI const value_type& operator[](size_t __i) const {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "valarray::operator[] index out of bounds");
+ return __begin_[__i];
+ }
- _LIBCPP_HIDE_FROM_ABI value_type& operator[](size_t __i) { return __begin_[__i]; }
+ _LIBCPP_HIDE_FROM_ABI value_type& operator[](size_t __i) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(__i < size(), "valarray::operator[] index out of bounds");
+ return __begin_[__i];
+ }
// subset operations:
_LIBCPP_HIDE_FROM_ABI __val_expr<__slice_expr<const valarray&> > operator[](slice __s) const;
diff --git a/libcxx/test/libcxx/numerics/numarray/assert.pass.cpp b/libcxx/test/libcxx/numerics/numarray/assert.pass.cpp
new file mode 100644
index 00000000000000..2bdf52340abfc8
--- /dev/null
+++ b/libcxx/test/libcxx/numerics/numarray/assert.pass.cpp
@@ -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;
+}
diff --git a/libcxx/test/libcxx/utilities/template.bitset/assert.pass.cpp b/libcxx/test/libcxx/utilities/template.bitset/assert.pass.cpp
new file mode 100644
index 00000000000000..4019bdf1318eb9
--- /dev/null
+++ b/libcxx/test/libcxx/utilities/template.bitset/assert.pass.cpp
@@ -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;
+}
diff --git a/libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp b/libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp
index e8ab264f4bab62..d87fd91b0356c3 100644
--- a/libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp
+++ b/libcxx/test/std/utilities/template.bitset/bitset.members/op_and_eq.pass.cpp
@@ -6,6 +6,8 @@
//
//===----------------------------------------------------------------------===//
+// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=2000000
+
// bitset<N>& operator&=(const bitset<N>& rhs); // constexpr since C++23
#include <bitset>
More information about the libcxx-commits
mailing list