diff --git a/libcxx/include/string b/libcxx/include/string --- a/libcxx/include/string +++ b/libcxx/include/string @@ -1842,7 +1842,11 @@ const void* __old_mid, const void* __new_mid) const { const void* __begin = data(); const void* __end = data() + capacity() + 1; + # if _LIBCPP_CLANG_VER < 1600 if (!__libcpp_is_constant_evaluated() && __begin != nullptr && is_same::value) + #else + if (!__libcpp_is_constant_evaluated() && __begin != nullptr && __asan_annotate_container_with_allocator::value) + # endif __sanitizer_annotate_contiguous_container(__begin, __end, __old_mid, __new_mid); } #else diff --git a/libcxx/test/libcxx/containers/strings/no_asan.pass.cpp b/libcxx/test/libcxx/containers/strings/no_asan.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/containers/strings/no_asan.pass.cpp @@ -0,0 +1,73 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03 + +// + +// Test based on: https://bugs.chromium.org/p/chromium/issues/detail?id=1419798#c5 + +#include +#include +#include +#include + +struct reuse_allocator { + static size_t const N = 100; + reuse_allocator() { + for (size_t i = 0; i < N; ++i) + __buffers[i] = malloc(8*1024); + } + ~reuse_allocator() { + for (size_t i = 0; i < N; ++i) + free(__buffers[i]); + } + void* alloc() { + assert(__next_id < N); + return __buffers[__next_id++]; + } + void reset() { __next_id = 0; } + void* __buffers[N]; + size_t __next_id = 0; +} reuse_buffers; + +template +struct user_allocator { + using value_type = T; + user_allocator() = default; + template + user_allocator(user_allocator) {} + friend bool operator==(user_allocator, user_allocator) {return true;} + friend bool operator!=(user_allocator x, user_allocator y) {return !(x == y);} + + T* allocate(size_t) { return (T*)reuse_buffers.alloc(); } + void deallocate(T*, size_t) noexcept {} +}; + +template +struct std::__asan_annotate_container_with_allocator> { + static bool const value = false; +}; + +int main() { + using S = std::basic_string, user_allocator>; + + { + S* s = new (reuse_buffers.alloc()) S(); + for (int i = 0; i < 100; i++) + s->push_back('a'); + } + reuse_buffers.reset(); + { + S s; + for (int i = 0; i < 1000; i++) + s.push_back('b'); + } + + return 0; +} diff --git a/libcxx/test/support/asan_testing.h b/libcxx/test/support/asan_testing.h --- a/libcxx/test/support/asan_testing.h +++ b/libcxx/test/support/asan_testing.h @@ -53,18 +53,9 @@ if (c.data() != NULL) { #if TEST_CLANG_VER < 16000 || defined(_LIBCPP_ASAN_ANNOTATE_ONLY_LONG) // TODO LLVM18: remove special case - if(!is_string_short(c)) { -# endif - if (std::is_same>::value) - return __sanitizer_verify_contiguous_container( - c.data(), c.data() + c.size() + 1, c.data() + c.capacity() + 1) != 0; - else - return __sanitizer_verify_contiguous_container( - c.data(), c.data() + c.capacity() + 1, c.data() + c.capacity() + 1) != 0; -# if TEST_CLANG_VER < 16000 || defined(_LIBCPP_ASAN_ANNOTATE_ONLY_LONG) - // TODO LLVM18: remove special case - } + if(!is_string_short(c)) #endif + return __sanitizer_verify_contiguous_container(c.data(), c.data() + c.size() + 1, c.data() + c.capacity() + 1) != 0; } return true; }