Skip to content

Commit d55a13c

Browse files
author
Advenam Tacet
committed
[ASan][libc++] Optimize __annotate_delete for the default allocator
This commit optimizes the ASan helper functions `__annotate_delete()`, in `std::basic_string`, `std::vector` and `std::deque`, by adding `if` statements to prevent unpoisoning of memory for the default allocator. Unpoisoning is not required by the default allocator, and since it is widely used, this optimization should yield a meaningful performance improvement. The optimization was suggested by @EricWF.
1 parent c03745d commit d55a13c

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

libcxx/include/deque

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,9 @@ private:
10111011

10121012
_LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
10131013
#ifndef _LIBCPP_HAS_NO_ASAN
1014+
// The default allocator does not require unpoisoning before returning memory.
1015+
if _LIBCPP_CONSTEXPR (is_same<allocator_type, allocator<_Tp>>::value)
1016+
return;
10141017
if (empty()) {
10151018
for (size_t __i = 0; __i < __map_.size(); ++__i) {
10161019
__annotate_whole_block(__i, __asan_unposion);

libcxx/include/string

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,8 +1912,10 @@ private:
19121912

19131913
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 void __annotate_delete() const _NOEXCEPT {
19141914
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1915-
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
1916-
__annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
1915+
// The default allocator does not require unpoisoning before returning memory.
1916+
if _LIBCPP_CONSTEXPR (!is_same<allocator_type, allocator<__default_allocator_type>>::value)
1917+
if (!__libcpp_is_constant_evaluated() && (__asan_short_string_is_annotated() || __is_long()))
1918+
__annotate_contiguous_container(data() + size() + 1, data() + capacity() + 1);
19171919
#endif
19181920
}
19191921

libcxx/include/vector

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,9 @@ private:
853853

854854
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_delete() const _NOEXCEPT {
855855
#ifndef _LIBCPP_HAS_NO_ASAN
856-
__annotate_contiguous_container(data(), data() + capacity(), data() + size(), data() + capacity());
856+
// The default allocator does not require unpoisoning before returning memory.
857+
if _LIBCPP_CONSTEXPR (!is_same<allocator_type, __default_allocator_type>::value)
858+
__annotate_contiguous_container(data(), data() + capacity(), data() + size(), data() + capacity());
857859
#endif
858860
}
859861

0 commit comments

Comments
 (0)