Skip to content

[libc++][hardening] Add iterator validity checks on unordered containers #80230

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

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions libcxx/include/__hash_table
Original file line number Diff line number Diff line change
Expand Up @@ -284,13 +284,21 @@ public:

_LIBCPP_HIDE_FROM_ABI __hash_iterator() _NOEXCEPT : __node_(nullptr) {}

_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
return __node_->__upcast()->__get_value();
}

_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container iterator");
return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
}

_LIBCPP_HIDE_FROM_ABI __hash_iterator& operator++() {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container iterator");
__node_ = __node_->__next_;
return *this;
}
Expand Down Expand Up @@ -345,12 +353,20 @@ public:

_LIBCPP_HIDE_FROM_ABI __hash_const_iterator(const __non_const_iterator& __x) _NOEXCEPT : __node_(__x.__node_) {}

_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
return __node_->__upcast()->__get_value();
}
_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
}

_LIBCPP_HIDE_FROM_ABI __hash_const_iterator& operator++() {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_iterator");
__node_ = __node_->__next_;
return *this;
}
Expand Down Expand Up @@ -400,13 +416,21 @@ public:

_LIBCPP_HIDE_FROM_ABI __hash_local_iterator() _NOEXCEPT : __node_(nullptr) {}

_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
return __node_->__upcast()->__get_value();
}

_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
}

_LIBCPP_HIDE_FROM_ABI __hash_local_iterator& operator++() {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container local_iterator");
__node_ = __node_->__next_;
if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
__node_ = nullptr;
Expand Down Expand Up @@ -475,13 +499,21 @@ public:
__bucket_(__x.__bucket_),
__bucket_count_(__x.__bucket_count_) {}

_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
return __node_->__upcast()->__get_value();
}

_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
}

_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator& operator++() {
_LIBCPP_ASSERT_NON_NULL(
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_local_iterator");
__node_ = __node_->__next_;
if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
__node_ = nullptr;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <unordered_map>

// Dereference non-dereferenceable iterator.

// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
// UNSUPPORTED: c++03
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

#include <unordered_map>
#include <string>

#include "check_assertion.h"
#include "min_allocator.h"

int main(int, char**) {
{
typedef std::unordered_map<int, std::string> C;
C c;
c.insert(std::make_pair(1, "one"));
C::iterator i = c.end();
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container iterator");
C::const_iterator i2 = c.cend();
TEST_LIBCPP_ASSERT_FAILURE(
*i2, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
}

{
typedef std::unordered_map<int,
std::string,
std::hash<int>,
std::equal_to<int>,
min_allocator<std::pair<const int, std::string>>>
C;
C c;
c.insert(std::make_pair(1, "one"));
C::iterator i = c.end();
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container iterator");
C::const_iterator i2 = c.cend();
TEST_LIBCPP_ASSERT_FAILURE(
*i2, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <unordered_map>

// Increment iterator past end.

// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
// UNSUPPORTED: c++03
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

#include <unordered_map>
#include <cassert>
#include <string>

#include "check_assertion.h"
#include "min_allocator.h"

int main(int, char**) {
{
typedef std::unordered_map<int, std::string> C;
C c;
c.insert(std::make_pair(1, "one"));
C::iterator i = c.begin();
++i;
assert(i == c.end());
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container iterator");
C::const_iterator i2 = c.cbegin();
++i2;
assert(i2 == c.cend());
TEST_LIBCPP_ASSERT_FAILURE(++i2, "Attempted to increment a non-incrementable unordered container const_iterator");
}

{
typedef std::unordered_map<int,
std::string,
std::hash<int>,
std::equal_to<int>,
min_allocator<std::pair<const int, std::string>>>
C;
C c;
c.insert(std::make_pair(1, "one"));
C::iterator i = c.begin();
++i;
assert(i == c.end());
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container iterator");
C::const_iterator i2 = c.cbegin();
++i2;
assert(i2 == c.cend());
TEST_LIBCPP_ASSERT_FAILURE(++i2, "Attempted to increment a non-incrementable unordered container const_iterator");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <unordered_map>

// Dereference non-dereferenceable iterator.

// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
// UNSUPPORTED: c++03
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

#include <unordered_map>
#include <string>

#include "check_assertion.h"
#include "min_allocator.h"

int main(int, char**) {
{
typedef std::unordered_map<int, std::string> C;
C c(1);
C::local_iterator i = c.end(0);
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
C::const_local_iterator i2 = c.cend(0);
TEST_LIBCPP_ASSERT_FAILURE(
*i2, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
}

{
typedef std::unordered_map<int,
std::string,
std::hash<int>,
std::equal_to<int>,
min_allocator<std::pair<const int, std::string>>>
C;
C c(1);
C::local_iterator i = c.end(0);
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
C::const_local_iterator i2 = c.cend(0);
TEST_LIBCPP_ASSERT_FAILURE(
*i2, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
}

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

// <unordered_map>

// Increment local_iterator past end.

// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
// UNSUPPORTED: c++03
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing

#include <unordered_map>
#include <string>
#include <cassert>

#include "check_assertion.h"
#include "min_allocator.h"

int main(int, char**) {
{
typedef std::unordered_map<int, std::string> C;
C c;
c.insert(std::make_pair(42, std::string()));
C::size_type b = c.bucket(42);
C::local_iterator i = c.begin(b);
assert(i != c.end(b));
++i;
assert(i == c.end(b));
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container local_iterator");
C::const_local_iterator i2 = c.cbegin(b);
assert(i2 != c.cend(b));
++i2;
assert(i2 == c.cend(b));
TEST_LIBCPP_ASSERT_FAILURE(
++i2, "Attempted to increment a non-incrementable unordered container const_local_iterator");
}

{
typedef std::unordered_map<int,
std::string,
std::hash<int>,
std::equal_to<int>,
min_allocator<std::pair<const int, std::string>>>
C;
C c({{42, std::string()}});
C::size_type b = c.bucket(42);
C::local_iterator i = c.begin(b);
assert(i != c.end(b));
++i;
assert(i == c.end(b));
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container local_iterator");
C::const_local_iterator i2 = c.cbegin(b);
assert(i2 != c.cend(b));
++i2;
assert(i2 == c.cend(b));
TEST_LIBCPP_ASSERT_FAILURE(
++i2, "Attempted to increment a non-incrementable unordered container const_local_iterator");
}

return 0;
}

This file was deleted.

Loading