Skip to content

Commit 41658ba

Browse files
authored
[libc++][hardening] Add iterator validity checks on unordered containers (llvm#80230)
These are simply null checks, so use `_LIBCPP_ASSERT_NON_NULL`. This allows us to restore a bunch of the old debug tests. I've extended them to also cover the const iterators, as those run through different codepaths than the const ones. This does the easier (and less important) half of llvm#80212.
1 parent 672fc89 commit 41658ba

33 files changed

+923
-709
lines changed

libcxx/include/__hash_table

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -284,13 +284,21 @@ public:
284284

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

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

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

293299
_LIBCPP_HIDE_FROM_ABI __hash_iterator& operator++() {
300+
_LIBCPP_ASSERT_NON_NULL(
301+
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container iterator");
294302
__node_ = __node_->__next_;
295303
return *this;
296304
}
@@ -345,12 +353,20 @@ public:
345353

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

348-
_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
356+
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
357+
_LIBCPP_ASSERT_NON_NULL(
358+
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
359+
return __node_->__upcast()->__get_value();
360+
}
349361
_LIBCPP_HIDE_FROM_ABI pointer operator->() const {
362+
_LIBCPP_ASSERT_NON_NULL(
363+
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
350364
return pointer_traits<pointer>::pointer_to(__node_->__upcast()->__get_value());
351365
}
352366

353367
_LIBCPP_HIDE_FROM_ABI __hash_const_iterator& operator++() {
368+
_LIBCPP_ASSERT_NON_NULL(
369+
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_iterator");
354370
__node_ = __node_->__next_;
355371
return *this;
356372
}
@@ -400,13 +416,21 @@ public:
400416

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

403-
_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
419+
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
420+
_LIBCPP_ASSERT_NON_NULL(
421+
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
422+
return __node_->__upcast()->__get_value();
423+
}
404424

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

409431
_LIBCPP_HIDE_FROM_ABI __hash_local_iterator& operator++() {
432+
_LIBCPP_ASSERT_NON_NULL(
433+
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container local_iterator");
410434
__node_ = __node_->__next_;
411435
if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
412436
__node_ = nullptr;
@@ -475,13 +499,21 @@ public:
475499
__bucket_(__x.__bucket_),
476500
__bucket_count_(__x.__bucket_count_) {}
477501

478-
_LIBCPP_HIDE_FROM_ABI reference operator*() const { return __node_->__upcast()->__get_value(); }
502+
_LIBCPP_HIDE_FROM_ABI reference operator*() const {
503+
_LIBCPP_ASSERT_NON_NULL(
504+
__node_ != nullptr, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
505+
return __node_->__upcast()->__get_value();
506+
}
479507

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

484514
_LIBCPP_HIDE_FROM_ABI __hash_const_local_iterator& operator++() {
515+
_LIBCPP_ASSERT_NON_NULL(
516+
__node_ != nullptr, "Attempted to increment a non-incrementable unordered container const_local_iterator");
485517
__node_ = __node_->__next_;
486518
if (__node_ != nullptr && std::__constrain_hash(__node_->__hash(), __bucket_count_) != __bucket_)
487519
__node_ = nullptr;
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// Dereference non-dereferenceable iterator.
12+
13+
// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
14+
// UNSUPPORTED: c++03
15+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
16+
17+
#include <unordered_map>
18+
#include <string>
19+
20+
#include "check_assertion.h"
21+
#include "min_allocator.h"
22+
23+
int main(int, char**) {
24+
{
25+
typedef std::unordered_map<int, std::string> C;
26+
C c;
27+
c.insert(std::make_pair(1, "one"));
28+
C::iterator i = c.end();
29+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container iterator");
30+
C::const_iterator i2 = c.cend();
31+
TEST_LIBCPP_ASSERT_FAILURE(
32+
*i2, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
33+
}
34+
35+
{
36+
typedef std::unordered_map<int,
37+
std::string,
38+
std::hash<int>,
39+
std::equal_to<int>,
40+
min_allocator<std::pair<const int, std::string>>>
41+
C;
42+
C c;
43+
c.insert(std::make_pair(1, "one"));
44+
C::iterator i = c.end();
45+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container iterator");
46+
C::const_iterator i2 = c.cend();
47+
TEST_LIBCPP_ASSERT_FAILURE(
48+
*i2, "Attempted to dereference a non-dereferenceable unordered container const_iterator");
49+
}
50+
51+
return 0;
52+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// Increment iterator past end.
12+
13+
// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
14+
// UNSUPPORTED: c++03
15+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
16+
17+
#include <unordered_map>
18+
#include <cassert>
19+
#include <string>
20+
21+
#include "check_assertion.h"
22+
#include "min_allocator.h"
23+
24+
int main(int, char**) {
25+
{
26+
typedef std::unordered_map<int, std::string> C;
27+
C c;
28+
c.insert(std::make_pair(1, "one"));
29+
C::iterator i = c.begin();
30+
++i;
31+
assert(i == c.end());
32+
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container iterator");
33+
C::const_iterator i2 = c.cbegin();
34+
++i2;
35+
assert(i2 == c.cend());
36+
TEST_LIBCPP_ASSERT_FAILURE(++i2, "Attempted to increment a non-incrementable unordered container const_iterator");
37+
}
38+
39+
{
40+
typedef std::unordered_map<int,
41+
std::string,
42+
std::hash<int>,
43+
std::equal_to<int>,
44+
min_allocator<std::pair<const int, std::string>>>
45+
C;
46+
C c;
47+
c.insert(std::make_pair(1, "one"));
48+
C::iterator i = c.begin();
49+
++i;
50+
assert(i == c.end());
51+
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container iterator");
52+
C::const_iterator i2 = c.cbegin();
53+
++i2;
54+
assert(i2 == c.cend());
55+
TEST_LIBCPP_ASSERT_FAILURE(++i2, "Attempted to increment a non-incrementable unordered container const_iterator");
56+
}
57+
58+
return 0;
59+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// Dereference non-dereferenceable iterator.
12+
13+
// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
14+
// UNSUPPORTED: c++03
15+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
16+
17+
#include <unordered_map>
18+
#include <string>
19+
20+
#include "check_assertion.h"
21+
#include "min_allocator.h"
22+
23+
int main(int, char**) {
24+
{
25+
typedef std::unordered_map<int, std::string> C;
26+
C c(1);
27+
C::local_iterator i = c.end(0);
28+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
29+
C::const_local_iterator i2 = c.cend(0);
30+
TEST_LIBCPP_ASSERT_FAILURE(
31+
*i2, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
32+
}
33+
34+
{
35+
typedef std::unordered_map<int,
36+
std::string,
37+
std::hash<int>,
38+
std::equal_to<int>,
39+
min_allocator<std::pair<const int, std::string>>>
40+
C;
41+
C c(1);
42+
C::local_iterator i = c.end(0);
43+
TEST_LIBCPP_ASSERT_FAILURE(*i, "Attempted to dereference a non-dereferenceable unordered container local_iterator");
44+
C::const_local_iterator i2 = c.cend(0);
45+
TEST_LIBCPP_ASSERT_FAILURE(
46+
*i2, "Attempted to dereference a non-dereferenceable unordered container const_local_iterator");
47+
}
48+
49+
return 0;
50+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// <unordered_map>
10+
11+
// Increment local_iterator past end.
12+
13+
// REQUIRES: has-unix-headers, libcpp-hardening-mode={{extensive|debug}}
14+
// UNSUPPORTED: c++03
15+
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
16+
17+
#include <unordered_map>
18+
#include <string>
19+
#include <cassert>
20+
21+
#include "check_assertion.h"
22+
#include "min_allocator.h"
23+
24+
int main(int, char**) {
25+
{
26+
typedef std::unordered_map<int, std::string> C;
27+
C c;
28+
c.insert(std::make_pair(42, std::string()));
29+
C::size_type b = c.bucket(42);
30+
C::local_iterator i = c.begin(b);
31+
assert(i != c.end(b));
32+
++i;
33+
assert(i == c.end(b));
34+
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container local_iterator");
35+
C::const_local_iterator i2 = c.cbegin(b);
36+
assert(i2 != c.cend(b));
37+
++i2;
38+
assert(i2 == c.cend(b));
39+
TEST_LIBCPP_ASSERT_FAILURE(
40+
++i2, "Attempted to increment a non-incrementable unordered container const_local_iterator");
41+
}
42+
43+
{
44+
typedef std::unordered_map<int,
45+
std::string,
46+
std::hash<int>,
47+
std::equal_to<int>,
48+
min_allocator<std::pair<const int, std::string>>>
49+
C;
50+
C c({{42, std::string()}});
51+
C::size_type b = c.bucket(42);
52+
C::local_iterator i = c.begin(b);
53+
assert(i != c.end(b));
54+
++i;
55+
assert(i == c.end(b));
56+
TEST_LIBCPP_ASSERT_FAILURE(++i, "Attempted to increment a non-incrementable unordered container local_iterator");
57+
C::const_local_iterator i2 = c.cbegin(b);
58+
assert(i2 != c.cend(b));
59+
++i2;
60+
assert(i2 == c.cend(b));
61+
TEST_LIBCPP_ASSERT_FAILURE(
62+
++i2, "Attempted to increment a non-incrementable unordered container const_local_iterator");
63+
}
64+
65+
return 0;
66+
}

libcxx/test/libcxx/containers/unord/unord.map/debug.iterator.dereference.pass.cpp

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)