diff --git a/libcxx/include/vector b/libcxx/include/vector --- a/libcxx/include/vector +++ b/libcxx/include/vector @@ -723,8 +723,11 @@ const void *__old_mid, const void *__new_mid) const { - +# if _LIBCPP_CLANG_VER >= 16000 + if (!__libcpp_is_constant_evaluated() && __beg) +# else if (!__libcpp_is_constant_evaluated() && __beg && is_same::value) +# endif __sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid); } #else 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,63 @@ 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 && _LIBCPP_CLANG_VER < 160000 + { + 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 && _LIBCPP_CLANG_VER >= 160000 + { + 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); + } } diff --git a/libcxx/test/support/min_allocator.h b/libcxx/test/support/min_allocator.h --- a/libcxx/test/support/min_allocator.h +++ b/libcxx/test/support/min_allocator.h @@ -432,4 +432,22 @@ TEST_CONSTEXPR_CXX20 friend bool operator!=(explicit_allocator x, explicit_allocator y) {return !(x == y);} }; +template +class unaligned_allocator { +public: + typedef T value_type; + + TEST_CONSTEXPR_CXX20 unaligned_allocator() TEST_NOEXCEPT {} + + template + TEST_CONSTEXPR_CXX20 explicit unaligned_allocator(unaligned_allocator) TEST_NOEXCEPT {} + + TEST_CONSTEXPR_CXX20 T* allocate(std::size_t n) { return static_cast(std::allocator().allocate(n + 1)) + 1; } + + TEST_CONSTEXPR_CXX20 void deallocate(T* p, std::size_t n) { std::allocator().deallocate(p - 1, n + 1); } + + TEST_CONSTEXPR_CXX20 friend bool operator==(unaligned_allocator, unaligned_allocator) { return true; } + TEST_CONSTEXPR_CXX20 friend bool operator!=(unaligned_allocator x, unaligned_allocator y) { return !(x == y); } +}; + #endif // MIN_ALLOCATOR_H