Skip to content

Commit 02f4b36

Browse files
authored
[libc++] Refactor of ASan annotation functions (#74023)
This commit refactors the ASan annotation functions in libc++ to reduce unnecessary code duplication. Additionally it adds a small optimization. - Eliminates two redundant function versions by utilizing the `[[maybe_unused]]` attribute and guarding function bodies with `#ifndef _LIBCPP_HAS_NO_ASAN`. - Introduces an additional guard to an auxiliary function, allowing the removal of a no-ops function body. This approach avoids relying on the optimizer for code elimination. Fixes #73043
1 parent df2485b commit 02f4b36

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

libcxx/include/deque

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -966,25 +966,27 @@ public:
966966
// __asan_annotate_container_with_allocator to false.
967967
// For more details, see the "Using libc++" documentation page or
968968
// the documentation for __sanitizer_annotate_contiguous_container.
969-
#if !defined(_LIBCPP_HAS_NO_ASAN)
970969
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
971-
const void* __beg,
972-
const void* __end,
973-
const void* __old_con_beg,
974-
const void* __old_con_end,
975-
const void* __new_con_beg,
976-
const void* __new_con_end) const {
970+
[[__maybe_unused__]] const void* __beg,
971+
[[__maybe_unused__]] const void* __end,
972+
[[__maybe_unused__]] const void* __old_con_beg,
973+
[[__maybe_unused__]] const void* __old_con_end,
974+
[[__maybe_unused__]] const void* __new_con_beg,
975+
[[__maybe_unused__]] const void* __new_con_end) const {
976+
#ifndef _LIBCPP_HAS_NO_ASAN
977977
if (__beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
978978
__sanitizer_annotate_double_ended_contiguous_container(
979979
__beg, __end, __old_con_beg, __old_con_end, __new_con_beg, __new_con_end);
980+
#endif
980981
}
981-
#else
982-
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
983-
const void*, const void*, const void*, const void*, const void*, const void*) const _NOEXCEPT {}
984-
#endif // !defined(_LIBCPP_HAS_NO_ASAN)
985982

986983
_LIBCPP_HIDE_FROM_ABI
987-
void __annotate_from_to(size_type __beg, size_type __end, __asan_annotation_type __annotation_type, __asan_annotation_place __place) const _NOEXCEPT {
984+
void __annotate_from_to(
985+
[[__maybe_unused__]] size_type __beg,
986+
[[__maybe_unused__]] size_type __end,
987+
[[__maybe_unused__]] __asan_annotation_type __annotation_type,
988+
[[__maybe_unused__]] __asan_annotation_place __place) const _NOEXCEPT {
989+
#ifndef _LIBCPP_HAS_NO_ASAN
988990
// __beg - index of the first item to annotate
989991
// __end - index behind the last item to annotate (so last item + 1)
990992
// __annotation_type - __asan_unposion or __asan_poison
@@ -1075,6 +1077,7 @@ public:
10751077

10761078
__annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
10771079
}
1080+
#endif // !_LIBCPP_HAS_NO_ASAN
10781081
}
10791082

10801083
_LIBCPP_HIDE_FROM_ABI

libcxx/include/vector

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -850,20 +850,19 @@ private:
850850
// __asan_annotate_container_with_allocator to false.
851851
// For more details, see the "Using libc++" documentation page or
852852
// the documentation for __sanitizer_annotate_contiguous_container.
853-
#ifndef _LIBCPP_HAS_NO_ASAN
854-
_LIBCPP_CONSTEXPR_SINCE_CXX20
855-
void __annotate_contiguous_container(const void *__beg, const void *__end,
856-
const void *__old_mid,
857-
const void *__new_mid) const
853+
854+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
855+
void __annotate_contiguous_container([[__maybe_unused__]] const void *__beg,
856+
[[__maybe_unused__]] const void *__end,
857+
[[__maybe_unused__]] const void *__old_mid,
858+
[[__maybe_unused__]] const void *__new_mid) const
858859
{
860+
#ifndef _LIBCPP_HAS_NO_ASAN
859861
if (!__libcpp_is_constant_evaluated() && __beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
860862
__sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
861-
}
862-
#else
863-
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
864-
void __annotate_contiguous_container(const void*, const void*, const void*,
865-
const void*) const _NOEXCEPT {}
866863
#endif
864+
}
865+
867866
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
868867
void __annotate_new(size_type __current_size) const _NOEXCEPT {
869868
__annotate_contiguous_container(data(), data() + capacity(),

0 commit comments

Comments
 (0)