Skip to content

Commit

Permalink
Revert "[ASan][libcxx] Annotating std::vector with all allocators"
Browse files Browse the repository at this point in the history
This reverts commit a9356a5.
  • Loading branch information
philnik777 committed Mar 8, 2023
1 parent 6a0f0fb commit 5ece59b
Show file tree
Hide file tree
Showing 37 changed files with 119 additions and 608 deletions.
23 changes: 2 additions & 21 deletions libcxx/include/vector
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,11 @@ private:
_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());
}
}
Expand Down Expand Up @@ -743,25 +743,8 @@ private:
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<allocator_type, __default_allocator_type>::value)
# endif
__sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
}
#else
Expand Down Expand Up @@ -883,7 +866,6 @@ private:
if (__alloc() != __c.__alloc())
{
__clear();
__annotate_delete();
__alloc_traits::deallocate(__alloc(), this->__begin_, capacity());
this->__begin_ = this->__end_ = __end_cap() = nullptr;
}
Expand Down Expand Up @@ -972,7 +954,6 @@ vector<_Tp, _Allocator>::__vdeallocate() _NOEXCEPT
if (this->__begin_ != nullptr)
{
clear();
__annotate_delete();
__alloc_traits::deallocate(this->__alloc(), this->__begin_, capacity());
this->__begin_ = this->__end_ = this->__end_cap() = nullptr;
}
Expand Down
91 changes: 33 additions & 58 deletions libcxx/test/libcxx/containers/sequences/vector/asan.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,65 +29,40 @@ void do_exit() {

int main(int, char**)
{
#if TEST_STD_VER >= 11 && TEST_CLANG_VER < 1600
// TODO LLVM18: Remove the special-test
{
typedef int T;
typedef std::vector<T, min_allocator<T>> 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
{
typedef int T;
typedef std::vector<T, min_allocator<T>> 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

#if TEST_STD_VER >= 11 && TEST_CLANG_VER >= 1600
// TODO LLVM18: Remove the special-casing
{
typedef int T;
typedef cpp17_input_iterator<T*> MyInputIter;
std::vector<T, min_allocator<T>> 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<T*> MyInputIter;
std::vector<T, unaligned_allocator<T>> 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<int*> MyInputIter;
// Sould not trigger ASan.
std::vector<int> 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 cpp17_input_iterator<int*> MyInputIter;
// Sould not trigger ASan.
std::vector<int> 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<T> 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<T> 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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
test<std::vector<int> >();
#if TEST_STD_VER >= 11
test<std::vector<int, min_allocator<int> > >();
test<std::vector<int, safe_allocator<int> > >();
#endif
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@ TEST_CONSTEXPR_CXX20 bool tests()
test_contiguous(C(A{}));
test_contiguous(C(9, 11.0, A{}));
}
{
typedef double T;
typedef safe_allocator<T> A;
typedef std::vector<T, A> C;
test_contiguous(C(A{}));
test_contiguous(C(9, 11.0, A{}));
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
c.clear();
assert(c.empty());
}
{
typedef std::vector<int, safe_allocator<int>> C;
C c;
ASSERT_NOEXCEPT(c.empty());
assert(c.empty());
c.push_back(C::value_type(1));
assert(!c.empty());
c.clear();
assert(c.empty());
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(v.capacity() == 150);
assert(is_contiguous_container_asan_correct(v));
}
{
std::vector<int, safe_allocator<int>> v;
v.reserve(10);
assert(v.capacity() >= 10);
assert(is_contiguous_container_asan_correct(v));
}
{
std::vector<int, safe_allocator<int>> v(100);
assert(v.capacity() == 100);
v.reserve(50);
assert(v.size() == 100);
assert(v.capacity() == 100);
v.reserve(150);
assert(v.size() == 100);
assert(v.capacity() == 150);
assert(is_contiguous_container_asan_correct(v));
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!TEST_IS_CONSTANT_EVALUATED) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(v.capacity() >= 200);
assert(is_contiguous_container_asan_correct(v));
}
{
std::vector<int, safe_allocator<int>> v(100);
v.resize(50);
assert(v.size() == 50);
assert(v.capacity() == 100);
assert(is_contiguous_container_asan_correct(v));
v.resize(200);
assert(v.size() == 200);
assert(v.capacity() >= 200);
assert(is_contiguous_container_asan_correct(v));
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(v.capacity() >= 200);
assert(is_contiguous_container_asan_correct(v));
}
{
std::vector<int, safe_allocator<int>> v(100);
v.resize(50, 1);
assert(v.size() == 50);
assert(v.capacity() == 100);
assert(is_contiguous_container_asan_correct(v));
assert((v == std::vector<int, safe_allocator<int>>(50)));
v.resize(200, 1);
assert(v.size() == 200);
assert(v.capacity() >= 200);
assert(is_contiguous_container_asan_correct(v));
for (unsigned i = 0; i < 50; ++i)
assert(v[i] == 0);
for (unsigned i = 50; i < 200; ++i)
assert(v[i] == 1);
}
{
std::vector<int, safe_allocator<int>> v(100);
v.resize(50, 1);
assert(v.size() == 50);
assert(v.capacity() == 100);
assert(is_contiguous_container_asan_correct(v));
v.resize(200, 1);
assert(v.size() == 200);
assert(v.capacity() >= 200);
assert(is_contiguous_container_asan_correct(v));
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(v.size() == 101);
assert(is_contiguous_container_asan_correct(v));
}
{
std::vector<int, safe_allocator<int>> v(100);
v.push_back(1);
assert(is_contiguous_container_asan_correct(v));
v.shrink_to_fit();
assert(v.capacity() == 101);
assert(v.size() == 101);
assert(is_contiguous_container_asan_correct(v));
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ TEST_CONSTEXPR_CXX20 bool tests()
c.erase(c.begin());
assert(c.size() == 0);
}
{
typedef std::vector<int, safe_allocator<int>> C;
C c;
ASSERT_NOEXCEPT(c.size());
assert(c.size() == 0);
c.push_back(C::value_type(2));
assert(c.size() == 1);
c.push_back(C::value_type(1));
assert(c.size() == 2);
c.push_back(C::value_type(3));
assert(c.size() == 3);
c.erase(c.begin());
assert(c.size() == 2);
c.erase(c.begin());
assert(c.size() == 1);
c.erase(c.begin());
assert(c.size() == 0);
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(v2.capacity() == 100);
assert(is_contiguous_container_asan_correct(v2));
}
{
std::vector<int, safe_allocator<int>> v1(100);
std::vector<int, safe_allocator<int>> v2(200);
assert(is_contiguous_container_asan_correct(v1));
assert(is_contiguous_container_asan_correct(v2));
v1.swap(v2);
assert(v1.size() == 200);
assert(v1.capacity() == 200);
assert(is_contiguous_container_asan_correct(v1));
assert(v2.size() == 100);
assert(v2.capacity() == 100);
assert(is_contiguous_container_asan_correct(v2));
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(l2 == l);
assert(l2.get_allocator() == min_allocator<int>());
}
{
std::vector<int, safe_allocator<int> > l(3, 2, safe_allocator<int>());
std::vector<int, safe_allocator<int> > l2(l, safe_allocator<int>());
l2 = l;
assert(l2 == l);
assert(l2.get_allocator() == safe_allocator<int>());
}
#endif

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,24 +95,6 @@ TEST_CONSTEXPR_CXX20 bool tests() {
assert(l2.get_allocator() == lo.get_allocator());
assert(is_contiguous_container_asan_correct(l2));
}
{
std::vector<MoveOnly, safe_allocator<MoveOnly> > l((safe_allocator<MoveOnly>()));
std::vector<MoveOnly, safe_allocator<MoveOnly> > lo((safe_allocator<MoveOnly>()));
assert(is_contiguous_container_asan_correct(l));
assert(is_contiguous_container_asan_correct(lo));
for (int i = 1; i <= 3; ++i) {
l.push_back(i);
lo.push_back(i);
}
assert(is_contiguous_container_asan_correct(l));
assert(is_contiguous_container_asan_correct(lo));
std::vector<MoveOnly, safe_allocator<MoveOnly> > l2((safe_allocator<MoveOnly>()));
l2 = std::move(l);
assert(l2 == lo);
assert(l.empty());
assert(l2.get_allocator() == lo.get_allocator());
assert(is_contiguous_container_asan_correct(l2));
}

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,6 @@ TEST_CONSTEXPR_CXX20 void basic_test_cases() {
random_access_iterator<const int*>(a),
random_access_iterator<const int*>(an));
test<std::vector<int> >(a, an);
test<std::vector<int, safe_allocator<int> > >(
cpp17_input_iterator<const int*>(a), cpp17_input_iterator<const int*>(an));
test<std::vector<int, safe_allocator<int> > >(forward_iterator<const int*>(a), forward_iterator<const int*>(an));
test<std::vector<int, safe_allocator<int> > >(
bidirectional_iterator<const int*>(a), bidirectional_iterator<const int*>(an));
test<std::vector<int, safe_allocator<int> > >(
random_access_iterator<const int*>(a), random_access_iterator<const int*>(an));
#endif
}

Expand Down

0 comments on commit 5ece59b

Please sign in to comment.