[libcxx-commits] [libcxx] [ASan][libc++] Refactor of ASan annotation functions (PR #74023)
via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Dec 5 08:04:18 PST 2023
https://github.com/AdvenamTacet updated https://github.com/llvm/llvm-project/pull/74023
>From 52245e6153dd449579863cd979a4c372b0d5c8aa Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Fri, 1 Dec 2023 02:21:21 +0100
Subject: [PATCH 1/3] [ASan][libc++] Refactor of ASan annotation functions
This commit refactors the ASan annotation functions in libc++ to improve code maintainability and reduce unnecessary code duplication.
- 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.
Suggested by @ldionne in https://github.com/llvm/llvm-project/issues/73043
---
libcxx/include/deque | 27 +++++++++++++++------------
libcxx/include/vector | 17 ++++++++---------
2 files changed, 23 insertions(+), 21 deletions(-)
diff --git a/libcxx/include/deque b/libcxx/include/deque
index 1438f1e992e20..5fd7b25cc8122 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -966,25 +966,27 @@ public:
// __asan_annotate_container_with_allocator to false.
// For more details, see the "Using libc++" documentation page or
// the documentation for __sanitizer_annotate_contiguous_container.
-#if !defined(_LIBCPP_HAS_NO_ASAN)
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
- const void* __beg,
- const void* __end,
- const void* __old_con_beg,
- const void* __old_con_end,
- const void* __new_con_beg,
- const void* __new_con_end) const {
+ [[__maybe_unused__]] const void* __beg,
+ [[__maybe_unused__]] const void* __end,
+ [[__maybe_unused__]] const void* __old_con_beg,
+ [[__maybe_unused__]] const void* __old_con_end,
+ [[__maybe_unused__]] const void* __new_con_beg,
+ [[__maybe_unused__]] const void* __new_con_end) const {
+#ifndef _LIBCPP_HAS_NO_ASAN
if (__beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
__sanitizer_annotate_double_ended_contiguous_container(
__beg, __end, __old_con_beg, __old_con_end, __new_con_beg, __new_con_end);
+#endif
}
-#else
- _LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
- const void*, const void*, const void*, const void*, const void*, const void*) const _NOEXCEPT {}
-#endif // !defined(_LIBCPP_HAS_NO_ASAN)
_LIBCPP_HIDE_FROM_ABI
- void __annotate_from_to(size_type __beg, size_type __end, __asan_annotation_type __annotation_type, __asan_annotation_place __place) const _NOEXCEPT {
+ void __annotate_from_to(
+ [[__maybe_unused__]] size_type __beg,
+ [[__maybe_unused__]] size_type __end,
+ [[__maybe_unused__]] __asan_annotation_type __annotation_type,
+ [[__maybe_unused__]] __asan_annotation_place __place) const _NOEXCEPT {
+#ifndef _LIBCPP_HAS_NO_ASAN
// __beg - index of the first item to annotate
// __end - index behind the last item to annotate (so last item + 1)
// __annotation_type - __asan_unposion or __asan_poison
@@ -1075,6 +1077,7 @@ public:
__annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
}
+#endif
}
_LIBCPP_HIDE_FROM_ABI
diff --git a/libcxx/include/vector b/libcxx/include/vector
index a91a4d00c42ec..fee8dd31f5b9c 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -850,20 +850,19 @@ private:
// __asan_annotate_container_with_allocator to false.
// For more details, see the "Using libc++" documentation page or
// the documentation for __sanitizer_annotate_contiguous_container.
-#ifndef _LIBCPP_HAS_NO_ASAN
+
_LIBCPP_CONSTEXPR_SINCE_CXX20
- void __annotate_contiguous_container(const void *__beg, const void *__end,
- const void *__old_mid,
- const void *__new_mid) const
+ void __annotate_contiguous_container([[__maybe_unused__]] const void *__beg,
+ [[__maybe_unused__]] const void *__end,
+ [[__maybe_unused__]] const void *__old_mid,
+ [[__maybe_unused__]] const void *__new_mid) const
{
+#ifndef _LIBCPP_HAS_NO_ASAN
if (!__libcpp_is_constant_evaluated() && __beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
__sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
- }
-#else
- _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
- void __annotate_contiguous_container(const void*, const void*, const void*,
- const void*) const _NOEXCEPT {}
#endif
+ }
+
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
void __annotate_new(size_type __current_size) const _NOEXCEPT {
__annotate_contiguous_container(data(), data() + capacity(),
>From e32aa27ba4a5851904034dc5f3f4e6c4afbc0e36 Mon Sep 17 00:00:00 2001
From: Advenam Tacet <advenam.tacet at trailofbits.com>
Date: Tue, 5 Dec 2023 05:57:31 +0100
Subject: [PATCH 2/3] [libc++][ASan] Add missing "_LIBCPP_HIDE_FROM_ABI"
It wasn't catched by tests before, but the function should be hidden from ABI always.
---
libcxx/include/vector | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/vector b/libcxx/include/vector
index fee8dd31f5b9c..fd2d5e11f0ea4 100644
--- a/libcxx/include/vector
+++ b/libcxx/include/vector
@@ -851,7 +851,7 @@ private:
// For more details, see the "Using libc++" documentation page or
// the documentation for __sanitizer_annotate_contiguous_container.
- _LIBCPP_CONSTEXPR_SINCE_CXX20
+ _LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI
void __annotate_contiguous_container([[__maybe_unused__]] const void *__beg,
[[__maybe_unused__]] const void *__end,
[[__maybe_unused__]] const void *__old_mid,
>From 3e68c1b59d8dda9c594487aeb7db4d2242fa35d2 Mon Sep 17 00:00:00 2001
From: Tacet <4922191+AdvenamTacet at users.noreply.github.com>
Date: Tue, 5 Dec 2023 17:04:10 +0100
Subject: [PATCH 3/3] Add a comment to `#endif`
Co-authored-by: Louis Dionne <ldionne.2 at gmail.com>
---
libcxx/include/deque | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/libcxx/include/deque b/libcxx/include/deque
index 5fd7b25cc8122..23785b586fc0f 100644
--- a/libcxx/include/deque
+++ b/libcxx/include/deque
@@ -1077,7 +1077,7 @@ public:
__annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
}
-#endif
+#endif // !_LIBCPP_HAS_NO_ASAN
}
_LIBCPP_HIDE_FROM_ABI
More information about the libcxx-commits
mailing list