Skip to content

Commit 82b38e8

Browse files
authored
[ASan][libc++] Optimization of container annotations (#76082)
This commit implements conditional compilation for ASan helper code. As convey to me by @EricWF, string benchmarks with UBSan have been experiencing significant performance hit after the commit with ASan string annotations. This is likely due to the fact that no-op ASan code is not optimized out with UBSan. To address this issue, this commit conditionalizes the inclusion of ASan helper function bodies using `#ifdef` directives. This approach allows us to selectively include only the ASan code when it's actually required, thereby enhancing optimizations and improving performance. While issue was noticed in string benchmarks, I expect same overhead (just less noticeable) in other containers, therefore `std::vector` and `std::deque` have same changes. To see impact of that change run `string.libcxx.out` with UBSan and `--benchmark_filter=BM_StringAssign` or `--benchmark_filter=BM_StringConstruct`.
1 parent 12250c4 commit 82b38e8

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

libcxx/include/deque

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,19 @@ private:
998998
}
999999

10001000
_LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
1001+
(void)__current_size;
1002+
#ifndef _LIBCPP_HAS_NO_ASAN
10011003
if (__current_size == 0)
10021004
__annotate_from_to(0, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
10031005
else {
10041006
__annotate_from_to(0, __start_, __asan_poison, __asan_front_moved);
10051007
__annotate_from_to(__start_ + __current_size, __map_.size() * __block_size, __asan_poison, __asan_back_moved);
10061008
}
1009+
#endif
10071010
}
10081011

10091012
_LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
1013+
#ifndef _LIBCPP_HAS_NO_ASAN
10101014
if (empty()) {
10111015
for (size_t __i = 0; __i < __map_.size(); ++__i) {
10121016
__annotate_whole_block(__i, __asan_unposion);
@@ -1015,30 +1019,52 @@ private:
10151019
__annotate_from_to(0, __start_, __asan_unposion, __asan_front_moved);
10161020
__annotate_from_to(__start_ + size(), __map_.size() * __block_size, __asan_unposion, __asan_back_moved);
10171021
}
1022+
#endif
10181023
}
10191024

10201025
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_front(size_type __n) const _NOEXCEPT {
1026+
(void)__n;
1027+
#ifndef _LIBCPP_HAS_NO_ASAN
10211028
__annotate_from_to(__start_ - __n, __start_, __asan_unposion, __asan_front_moved);
1029+
#endif
10221030
}
10231031

10241032
_LIBCPP_HIDE_FROM_ABI void __annotate_increase_back(size_type __n) const _NOEXCEPT {
1033+
(void)__n;
1034+
#ifndef _LIBCPP_HAS_NO_ASAN
10251035
__annotate_from_to(__start_ + size(), __start_ + size() + __n, __asan_unposion, __asan_back_moved);
1036+
#endif
10261037
}
10271038

10281039
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_front(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1040+
(void)__old_size;
1041+
(void)__old_start;
1042+
#ifndef _LIBCPP_HAS_NO_ASAN
10291043
__annotate_from_to(__old_start, __old_start + (__old_size - size()), __asan_poison, __asan_front_moved);
1044+
#endif
10301045
}
10311046

10321047
_LIBCPP_HIDE_FROM_ABI void __annotate_shrink_back(size_type __old_size, size_type __old_start) const _NOEXCEPT {
1048+
(void)__old_size;
1049+
(void)__old_start;
1050+
#ifndef _LIBCPP_HAS_NO_ASAN
10331051
__annotate_from_to(__old_start + size(), __old_start + __old_size, __asan_poison, __asan_back_moved);
1052+
#endif
10341053
}
10351054

10361055
_LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1056+
(void)__beginning;
1057+
(void)__end;
1058+
#ifndef _LIBCPP_HAS_NO_ASAN
10371059
__annotate_double_ended_contiguous_container(__beginning, __end, __beginning, __end, __end, __end);
1060+
#endif
10381061
}
10391062

10401063
_LIBCPP_HIDE_FROM_ABI void
10411064
__annotate_whole_block(size_t __block_index, __asan_annotation_type __annotation_type) const _NOEXCEPT {
1065+
(void)__block_index;
1066+
(void)__annotation_type;
1067+
#ifndef _LIBCPP_HAS_NO_ASAN
10421068
__map_const_iterator __block_it = __map_.begin() + __block_index;
10431069
const void* __block_start = std::__to_address(*__block_it);
10441070
const void* __block_end = std::__to_address(*__block_it + __block_size);
@@ -1049,6 +1075,7 @@ private:
10491075
__annotate_double_ended_contiguous_container(
10501076
__block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
10511077
}
1078+
#endif
10521079
}
10531080
#if !defined(_LIBCPP_HAS_NO_ASAN)
10541081

libcxx/include/string

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,23 +1903,34 @@ private:
19031903
}
19041904

19051905
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_new(size_type __current_size) const _NOEXCEPT {
1906+
(void) __current_size;
1907+
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
19061908
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19071909
__annotate_contiguous_container(data() + capacity() + 1, data() + __current_size + 1);
1910+
#endif
19081911
}
19091912

19101913
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
1914+
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
19111915
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19121916
__annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
1917+
#endif
19131918
}
19141919

19151920
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_increase(size_type __n) const _NOEXCEPT {
1921+
(void) __n;
1922+
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
19161923
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19171924
__annotate_contiguous_container(data() + size() + 1, data() + size() + 1 + __n);
1925+
#endif
19181926
}
19191927

19201928
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
1929+
(void) __old_size;
1930+
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
19211931
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
19221932
__annotate_contiguous_container(data() + __old_size + 1, data() + size() + 1);
1933+
#endif
19231934
}
19241935

19251936
template <size_type __a>

libcxx/include/vector

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -845,19 +845,30 @@ private:
845845
}
846846

847847
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {
848+
(void)__current_size;
849+
#ifndef _LIBCPP_HAS_NO_ASAN
848850
__annotate_contiguous_container(data(), data() + capacity(), data() + capacity(), data() + __current_size);
851+
#endif
849852
}
850853

851854
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
855+
#ifndef _LIBCPP_HAS_NO_ASAN
852856
__annotate_contiguous_container(data(), data() + capacity(), data() + size(), data() + capacity());
857+
#endif
853858
}
854859

855860
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_increase(size_type __n) const _NOEXCEPT {
861+
(void)__n;
862+
#ifndef _LIBCPP_HAS_NO_ASAN
856863
__annotate_contiguous_container(data(), data() + capacity(), data() + size(), data() + size() + __n);
864+
#endif
857865
}
858866

859867
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_shrink(size_type __old_size) const _NOEXCEPT {
868+
(void)__old_size;
869+
#ifndef _LIBCPP_HAS_NO_ASAN
860870
__annotate_contiguous_container(data(), data() + capacity(), data() + __old_size, data() + size());
871+
#endif
861872
}
862873

863874
struct _ConstructTransaction {

0 commit comments

Comments
 (0)