Skip to content

Commit 2a54098

Browse files
authoredJan 19, 2024
Revert "[ASan][libc++] Turn on ASan annotations for short strings" (#78627)
Reverts #75882 To recover build bots : https://lab.llvm.org/buildbot/#/builders/239/builds/5361 https://lab.llvm.org/buildbot/#/builders/168/builds/18126
1 parent 20a3484 commit 2a54098

File tree

5 files changed

+34
-429
lines changed

5 files changed

+34
-429
lines changed
 

‎libcxx/include/string

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,7 @@ _LIBCPP_PUSH_MACROS
659659
#else
660660
# define _LIBCPP_STRING_INTERNAL_MEMORY_ACCESS
661661
#endif
662+
#define _LIBCPP_SHORT_STRING_ANNOTATIONS_ALLOWED false
662663

663664
_LIBCPP_BEGIN_NAMESPACE_STD
664665

@@ -1895,33 +1896,38 @@ private:
18951896
#endif
18961897
}
18971898

1899+
// ASan: short string is poisoned if and only if this function returns true.
1900+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 bool __asan_short_string_is_annotated() const _NOEXCEPT {
1901+
return _LIBCPP_SHORT_STRING_ANNOTATIONS_ALLOWED && !__libcpp_is_constant_evaluated();
1902+
}
1903+
18981904
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
18991905
(void) __current_size;
19001906
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1901-
if (!__libcpp_is_constant_evaluated())
1907+
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19021908
__annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
19031909
#endif
19041910
}
19051911

19061912
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
19071913
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1908-
if (!__libcpp_is_constant_evaluated())
1914+
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19091915
__annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
19101916
#endif
19111917
}
19121918

19131919
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
19141920
(void) __n;
19151921
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1916-
if (!__libcpp_is_constant_evaluated())
1922+
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19171923
__annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
19181924
#endif
19191925
}
19201926

19211927
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
19221928
(void) __old_size;
19231929
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1924-
if (!__libcpp_is_constant_evaluated())
1930+
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19251931
__annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
19261932
#endif
19271933
}

‎libcxx/test/libcxx/containers/strings/basic.string/asan_deque_integration.pass.cpp

Lines changed: 0 additions & 182 deletions
This file was deleted.

‎libcxx/test/libcxx/containers/strings/basic.string/asan_short.pass.cpp

Lines changed: 0 additions & 56 deletions
This file was deleted.

‎libcxx/test/libcxx/containers/strings/basic.string/asan_vector_integration.pass.cpp

Lines changed: 0 additions & 182 deletions
This file was deleted.

‎libcxx/test/support/asan_testing.h

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,35 @@ TEST_CONSTEXPR bool is_double_ended_contiguous_container_asan_correct(const std:
5656
#endif
5757

5858
#if TEST_HAS_FEATURE(address_sanitizer)
59+
template <typename S>
60+
bool is_string_short(S const& s) {
61+
// We do not have access to __is_long(), but we can check if strings
62+
// buffer is inside strings memory. If strings memory contains its content,
63+
// SSO is in use. To check it, we can just confirm that the beginning is in
64+
// the string object memory block.
65+
// &s - beginning of objects memory
66+
// &s[0] - beginning of the buffer
67+
// (&s+1) - end of objects memory
68+
return (void*)std::addressof(s) <= (void*)std::addressof(s[0]) &&
69+
(void*)std::addressof(s[0]) < (void*)(std::addressof(s) + 1);
70+
}
71+
5972
template <typename ChrT, typename TraitsT, typename Alloc>
6073
TEST_CONSTEXPR bool is_string_asan_correct(const std::basic_string<ChrT, TraitsT, Alloc>& c) {
6174
if (TEST_IS_CONSTANT_EVALUATED)
6275
return true;
6376

64-
if (std::__asan_annotate_container_with_allocator<Alloc>::value)
65-
return __sanitizer_verify_contiguous_container(c.data(), c.data() + c.size() + 1, c.data() + c.capacity() + 1) != 0;
66-
else
67-
return __sanitizer_verify_contiguous_container(
68-
c.data(), c.data() + c.capacity() + 1, c.data() + c.capacity() + 1) != 0;
77+
if (!is_string_short(c) || _LIBCPP_SHORT_STRING_ANNOTATIONS_ALLOWED) {
78+
if (std::__asan_annotate_container_with_allocator<Alloc>::value)
79+
return __sanitizer_verify_contiguous_container(c.data(), c.data() + c.size() + 1, c.data() + c.capacity() + 1) !=
80+
0;
81+
else
82+
return __sanitizer_verify_contiguous_container(
83+
c.data(), c.data() + c.capacity() + 1, c.data() + c.capacity() + 1) != 0;
84+
} else {
85+
return __sanitizer_verify_contiguous_container(std::addressof(c), std::addressof(c) + 1, std::addressof(c) + 1) !=
86+
0;
87+
}
6988
}
7089
#else
7190
# include <string>

0 commit comments

Comments
 (0)
Please sign in to comment.