Skip to content

Commit 9b853f6

Browse files
authored
[libc++] Fix vector sanitization annotations on destruction (#121031)
In https://reviews.llvm.org/D136765 / https://reviews.llvm.org/D144155, the asan annotations for `std::vector` were modified to unpoison freed backing memory on destruction, instead of leaving it poisoned. However, calling `__clear()` instead of `clear()` skips informing the asan runtime of this decrease in the accessible container size, which breaks the invariant that the value of `old_mid` should match the value of `new_mid` from the previous call to `__sanitizer_annotate_contiguous_container`, which can trip the sanity checks for the partial poison between [d1, d2) and the container redzone between [d2, c), if enabled. To fix this, ensure that `clear()` is called instead, as is already done by `__vdeallocate()`. Also remove `__clear()`, since it is no longer called.
1 parent 977d744 commit 9b853f6

File tree

1 file changed

+3
-7
lines changed

1 file changed

+3
-7
lines changed

libcxx/include/__vector/vector.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ class _LIBCPP_TEMPLATE_VIS vector {
242242

243243
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() {
244244
if (__vec_.__begin_ != nullptr) {
245-
__vec_.__clear();
245+
__vec_.clear();
246246
__vec_.__annotate_delete();
247247
__alloc_traits::deallocate(__vec_.__alloc_, __vec_.__begin_, __vec_.capacity());
248248
}
@@ -525,7 +525,7 @@ class _LIBCPP_TEMPLATE_VIS vector {
525525

526526
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
527527
size_type __old_size = size();
528-
__clear();
528+
__base_destruct_at_end(this->__begin_);
529529
__annotate_shrink(__old_size);
530530
}
531531

@@ -737,10 +737,6 @@ class _LIBCPP_TEMPLATE_VIS vector {
737737
++__tx.__pos_;
738738
}
739739

740-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __clear() _NOEXCEPT {
741-
__base_destruct_at_end(this->__begin_);
742-
}
743-
744740
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __base_destruct_at_end(pointer __new_last) _NOEXCEPT {
745741
pointer __soon_to_be_end = this->__end_;
746742
while (__new_last != __soon_to_be_end)
@@ -764,7 +760,7 @@ class _LIBCPP_TEMPLATE_VIS vector {
764760

765761
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __copy_assign_alloc(const vector& __c, true_type) {
766762
if (this->__alloc_ != __c.__alloc_) {
767-
__clear();
763+
clear();
768764
__annotate_delete();
769765
__alloc_traits::deallocate(this->__alloc_, this->__begin_, capacity());
770766
this->__begin_ = this->__end_ = this->__cap_ = nullptr;

0 commit comments

Comments
 (0)