diff --git a/libcxx/include/vector b/libcxx/include/vector --- a/libcxx/include/vector +++ b/libcxx/include/vector @@ -439,11 +439,11 @@ _LIBCPP_CONSTEXPR __destroy_vector(vector& __vec) : __vec_(__vec) {} _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void operator()() { - __vec_.__annotate_delete(); std::__debug_db_erase_c(std::addressof(__vec_)); if (__vec_.__begin_ != nullptr) { __vec_.__clear(); + __vec_.__annotate_delete(); __alloc_traits::deallocate(__vec_.__alloc(), __vec_.__begin_, __vec_.capacity()); } } @@ -743,8 +743,25 @@ const void *__old_mid, const void *__new_mid) const { - +# if _LIBCPP_CLANG_VER >= 1600 + if (!__libcpp_is_constant_evaluated() && __beg) + // Implementation of __sanitizer_annotate_contiguous_container function changed with commit + // rGdd1b7b797a116eed588fd752fbe61d34deeb24e4 and supports: + // - unaligned beginnings of buffers, + // - shared first/last granule (if memory granule is shared with a different object + // just after the end of unaligned end/before the unaligned beginning, memory of that object won't be poisoned). + // + // Therefore, check for standard allocator is not necessary. +# else + // TODO LLVM18: Remove the special-casing + // + // Vectors annotations rely on __sanitizer_annotate_contiguous_container function, + // its implementation from LLVM15 (and earlier) requires that + // beginning of a containers data buffer is aligned to shadow granularity and + // memory just after is not shared with another object. + // Default allocator satisfies that. if (!__libcpp_is_constant_evaluated() && __beg && is_same::value) +# endif __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid); } #else @@ -866,6 +883,7 @@ if (__alloc() != __c.__alloc()) { __clear(); + __annotate_delete(); __alloc_traits::deallocate(__alloc(), this->__begin_, capacity()); this->__begin_ = this->__end_ = __end_cap() = nullptr; } @@ -954,6 +972,7 @@ if (this->__begin_ != nullptr) { clear(); + __annotate_delete(); __alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity()); this->__begin_ = this->__end_ = this->__end_cap() = nullptr; } diff --git a/libcxx/test/libcxx/containers/sequences/vector/asan.pass.cpp b/libcxx/test/libcxx/containers/sequences/vector/asan.pass.cpp --- a/libcxx/test/libcxx/containers/sequences/vector/asan.pass.cpp +++ b/libcxx/test/libcxx/containers/sequences/vector/asan.pass.cpp @@ -29,40 +29,65 @@ int main(int, char**) { -#if TEST_STD_VER >= 11 - { - typedef int T; - typedef std::vector> C; - const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - C c(std::begin(t), std::end(t)); - c.reserve(2*c.size()); - volatile T foo = c[c.size()]; // bad, but not caught by ASAN - ((void)foo); - } +#if TEST_STD_VER >= 11 && TEST_CLANG_VER < 1600 + // TODO LLVM18: Remove the special-test + { + typedef int T; + typedef std::vector> C; + const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + C c(std::begin(t), std::end(t)); + c.reserve(2 * c.size()); + volatile T foo = c[c.size()]; // bad, but not caught by ASAN + ((void)foo); + } #endif - { - typedef cpp17_input_iterator MyInputIter; - // Sould not trigger ASan. - std::vector v; - v.reserve(1); - int i[] = {42}; - v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1)); - assert(v[0] == 42); - assert(is_contiguous_container_asan_correct(v)); - } +#if TEST_STD_VER >= 11 && TEST_CLANG_VER >= 1600 + // TODO LLVM18: Remove the special-casing + { + typedef int T; + typedef cpp17_input_iterator MyInputIter; + std::vector> v; + v.reserve(1); + int i[] = {42}; + v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1)); + assert(v[0] == 42); + assert(is_contiguous_container_asan_correct(v)); + } + { + typedef char T; + typedef cpp17_input_iterator MyInputIter; + std::vector> v; + v.reserve(1); + char i[] = {'a', 'b'}; + v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 2)); + assert(v[0] == 'a'); + assert(v[1] == 'b'); + assert(is_contiguous_container_asan_correct(v)); + } +#endif + { + typedef cpp17_input_iterator MyInputIter; + // Sould not trigger ASan. + std::vector v; + v.reserve(1); + int i[] = {42}; + v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1)); + assert(v[0] == 42); + assert(is_contiguous_container_asan_correct(v)); + } - __sanitizer_set_death_callback(do_exit); - { - typedef int T; - typedef std::vector C; - const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; - C c(std::begin(t), std::end(t)); - c.reserve(2*c.size()); - assert(is_contiguous_container_asan_correct(c)); - assert(!__sanitizer_verify_contiguous_container( c.data(), c.data() + 1, c.data() + c.capacity())); - volatile T foo = c[c.size()]; // should trigger ASAN. Use volatile to prevent being optimized away. - assert(false); // if we got here, ASAN didn't trigger - ((void)foo); - } + __sanitizer_set_death_callback(do_exit); + { + typedef int T; + typedef std::vector C; + const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; + C c(std::begin(t), std::end(t)); + c.reserve(2 * c.size()); + assert(is_contiguous_container_asan_correct(c)); + assert(!__sanitizer_verify_contiguous_container(c.data(), c.data() + 1, c.data() + c.capacity())); + volatile T foo = c[c.size()]; // should trigger ASAN. Use volatile to prevent being optimized away. + assert(false); // if we got here, ASAN didn't trigger + ((void)foo); + } }