[libc++] Assert that erase and extract are not passed end() in the associative containers #232661
@philnik777
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @ilyas-mallah :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-libcxx
Author: Ilyas Mallah (ilyas-mallah)
<details>
<summary>Changes</summary>
erase(end()) and extract(end()) on map, multimap, set and multiset treat the end node as an element and crash with a SIGSEGV in the fast mode. m.erase(m.find(k)) with a missing key is one easy way to get there. On the unordered containers, extract(end()) reads through a null node.
This adds the __p != end() assertion that __hash_table::erase already has to erase in __tree and to extract in both. A range erase that runs past end() now traps too, since it loops over the single-iterator erase.
The tests cover erase and extract with end() on all eight containers, including the unordered erase check, which had no hardening test.
AI usage: Claude Code helped with the test runs and test scaffolding. I traced the issue, made the fix, and validated every change.
---
Full diff: https://github.com/llvm/llvm-project/pull/232661.diff
8 Files Affected:
````````````diff --git a/libcxx/include/__hash_table b/libcxx/include/__hash_table
index 23916e3a3ecf32..c3df10b305488f 100644
--- a/libcxx/include/__hash_table
+++ b/libcxx/include/__hash_table
@@ -1695,6 +1695,8 @@ __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(key_type const&
template <class _Tp, class _Hash, class _Equal, class _Alloc>
template <class _NodeHandle>
_LIBCPP_HIDE_FROM_ABI _NodeHandle __hash_table<_Tp, _Hash, _Equal, _Alloc>::__node_handle_extract(const_iterator __p) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __p != end(), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
allocator_type __alloc(__node_alloc());
return _NodeHandle(remove(__p).release(), __alloc);
}
diff --git a/libcxx/include/__tree b/libcxx/include/__tree
index 7cfa8e86fdc028..8c977d74bd53af 100644
--- a/libcxx/include/__tree
+++ b/libcxx/include/__tree
@@ -2133,6 +2133,8 @@ template <class _Tp, class _Compare, class _Allocator>
template <class _NodeHandle>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX26 _NodeHandle
__tree<_Tp, _Compare, _Allocator>::__node_handle_extract(const_iterator __p) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __p != end(), "associative container::extract(iterator) called with a non-dereferenceable iterator");
__node_pointer __np = __p.__get_np();
__remove_node_pointer(__np);
return _NodeHandle(__np, __alloc());
@@ -2200,6 +2202,8 @@ __tree<_Tp, _Compare, _Allocator>::__node_handle_merge_multi(__tree<_Tp, _Comp2,
template <class _Tp, class _Compare, class _Allocator>
_LIBCPP_CONSTEXPR_SINCE_CXX26
typename __tree<_Tp, _Compare, _Allocator>::iterator __tree<_Tp, _Compare, _Allocator>::erase(const_iterator __p) {
+ _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS(
+ __p != end(), "associative container::erase(iterator) called with a non-dereferenceable iterator");
__node_pointer __np = __p.__get_np();
iterator __r = __remove_node_pointer(__np);
__node_allocator& __na = __node_alloc();
diff --git a/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..c135643b88fa24
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/map/assert.erase_extract.end.pass.cpp
@@ -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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ std::map<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::map<int, int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::multimap<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::multimap<int, int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..83ac0a69618508
--- /dev/null
+++ b/libcxx/test/libcxx/containers/associative/set/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,61 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ int a[] = {1, 2, 3};
+ std::set<int> c(a, a + 3);
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::set<int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ int a[] = {1, 2, 3};
+ std::multiset<int> c(a, a + 3);
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "associative container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::multiset<int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "associative container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..560621191b1bd1
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.map/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ std::unordered_map<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::unordered_map<int, int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..6ea3e3f04bd341
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.multimap/assert.erase_extract.end.pass.cpp
@@ -0,0 +1,41 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_map>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_map>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ std::unordered_multimap<int, int> c{{1, 1}, {2, 2}, {3, 3}};
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::unordered_multimap<int, int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..58c3c348e96243
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.multiset/assert.erase_extract.end.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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ int a[] = {1, 2, 3};
+ std::unordered_multiset<int> c(a, a + 3);
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::unordered_multiset<int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
diff --git a/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp b/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.pass.cpp
new file mode 100644
index 00000000000000..568aca8125af88
--- /dev/null
+++ b/libcxx/test/libcxx/containers/unord/unord.set/assert.erase_extract.end.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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: can-test-hardening-assertions-fast
+// UNSUPPORTED: c++03, c++11, c++14
+// UNSUPPORTED: libcpp-assertion-semantic={{ignore|observe}}
+
+// <unordered_set>
+
+// Call erase(const_iterator) and extract(const_iterator) with end().
+
+#include <unordered_set>
+
+#include "check_assertion.h"
+
+int main(int, char**) {
+ {
+ int a[] = {1, 2, 3};
+ std::unordered_set<int> c(a, a + 3);
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.end()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.erase(c.cend()), "unordered container::erase(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.cend()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ {
+ std::unordered_set<int> c;
+ TEST_LIBCPP_ASSERT_FAILURE(
+ c.extract(c.end()), "unordered container::extract(iterator) called with a non-dereferenceable iterator");
+ }
+
+ return 0;
+}
</details>
/libcxx-bot benchmark libcxx/test/benchmarks/containers/associative/map.bench.cpp libcxx/test/benchmarks/containers/associative/multimap.bench.cpp libcxx/test/benchmarks/containers/associative/multiset.bench.cpp libcxx/test/benchmarks/containers/associative/set.bench.cpp libcxx/test/benchmarks/containers/associative/unordered_map.bench.cpp libcxx/test/benchmarks/containers/associative/unordered_multimap.bench.cpp libcxx/test/benchmarks/containers/associative/unordered_multiset.bench.cpp libcxx/test/benchmarks/containers/associative/unordered_set.bench.cpp
_Running benchmarks in https://github.com/llvm/llvm-project/actions/runs/39672355684_
Let's move these tests to libcxx/test/libcxx/containers/associative/multiset/assert.erase_extract.end.pass.cpp, and same for the multimap ones.
Requested changes.
(Apart from that, this looks reasonable to me)
The multiset and multimap tests now have their own files under associative/multiset and associative/multimap, in https://github.com/llvm/llvm-project/commit/b4432791c29ef4cec27b6db4f8e860cfad5d807f. The map test had the same mix, so I split it the same way. The blocks are moved unchanged.
@ldionne Everything is green on https://github.com/llvm/llvm-project/commit/65ae7c1fae840eb484a705b21a6d2aed15672f77 except the macOS generic-cxx03 job. It failed while setting up, when GitHub answered 429 three times to the checkout download. Could you rerun it? The test split you asked for is in.
@ldionne GitHub still shows your changes-requested review on this PR, though the test split you asked for is pushed. Could you re-review or dismiss it? I don't have commit access, so someone would need to merge it after that.
Thanks for starting the benchmarks on 10-21. The report job failed and posted nothing, so I read the run's artifacts. Only the macOS hardenedfast run compiles the new check in.
On map, set, multimap and multiset, the 16 erase(iterator) rows are between -4.8% and +5.1%, median +0.15%. One is marked significant, multiset<int>/32 at +1.1%. The 16 range erase rows are between -2.6% and +2.5%. Six of those are significant, and four of the six are faster. No erase(key) row with an existing key is significant. There's no extract benchmark.
I've also merged main to resolve the conflict with #245965.
ldionne
ilyas-mallah
erase(end())andextract(end())on map, multimap, set and multiset treat the end node as an element and crash with a SIGSEGV in the fast mode.m.erase(m.find(k))with a missing key is one easy way to get there. On the unordered containers,extract(end())reads through a null node.This adds the
__p != end()assertion that__hash_table::erasealready has toerasein__treeand toextractin both. A rangeerasethat runs pastend()now traps too, since it loops over the single-iteratorerase.The tests cover
eraseandextractwithend()on all eight containers, including the unorderederasecheck, which had no hardening test.AI usage: Claude Code helped with the test runs and test scaffolding. I traced the issue, made the fix, and validated every change.