Skip to content

Commit 61f1f13

Browse files
authored
[libc++][NFC] Move basic ASan annotation functions into a utility header (#87220)
1 parent 2685256 commit 61f1f13

File tree

8 files changed

+115
-68
lines changed

8 files changed

+115
-68
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,7 @@ set(files
345345
__coroutine/noop_coroutine_handle.h
346346
__coroutine/trivial_awaitables.h
347347
__debug_utils/randomize_range.h
348+
__debug_utils/sanitizers.h
348349
__debug_utils/strict_weak_ordering_check.h
349350
__exception/exception.h
350351
__exception/exception_ptr.h

libcxx/include/__config

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,15 +1061,6 @@ typedef __char32_t char32_t;
10611061
# define _LIBCPP_CONSTEXPR_SINCE_CXX23
10621062
# endif
10631063

1064-
# ifndef _LIBCPP_HAS_NO_ASAN
1065-
extern "C" _LIBCPP_EXPORTED_FROM_ABI void
1066-
__sanitizer_annotate_contiguous_container(const void*, const void*, const void*, const void*);
1067-
extern "C" _LIBCPP_EXPORTED_FROM_ABI void __sanitizer_annotate_double_ended_contiguous_container(
1068-
const void*, const void*, const void*, const void*, const void*, const void*);
1069-
extern "C" _LIBCPP_EXPORTED_FROM_ABI int
1070-
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
1071-
# endif
1072-
10731064
// Try to find out if RTTI is disabled.
10741065
# if !defined(__cpp_rtti) || __cpp_rtti < 199711L
10751066
# define _LIBCPP_HAS_NO_RTTI
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H
10+
#define _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H
11+
12+
#include <__config>
13+
#include <__type_traits/integral_constant.h>
14+
#include <__type_traits/is_constant_evaluated.h>
15+
16+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
17+
# pragma GCC system_header
18+
#endif
19+
20+
#ifndef _LIBCPP_HAS_NO_ASAN
21+
22+
extern "C" {
23+
_LIBCPP_EXPORTED_FROM_ABI void
24+
__sanitizer_annotate_contiguous_container(const void*, const void*, const void*, const void*);
25+
_LIBCPP_EXPORTED_FROM_ABI void __sanitizer_annotate_double_ended_contiguous_container(
26+
const void*, const void*, const void*, const void*, const void*, const void*);
27+
_LIBCPP_EXPORTED_FROM_ABI int
28+
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
29+
}
30+
31+
#endif // _LIBCPP_HAS_NO_ASAN
32+
33+
_LIBCPP_BEGIN_NAMESPACE_STD
34+
35+
// ASan choices
36+
#ifndef _LIBCPP_HAS_NO_ASAN
37+
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
38+
#endif
39+
40+
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS
41+
// __asan_annotate_container_with_allocator determines whether containers with custom allocators are annotated. This is
42+
// a public customization point to disable annotations if the custom allocator assumes that the memory isn't poisoned.
43+
// See the https://libcxx.llvm.org/UsingLibcxx.html#turning-off-asan-annotation-in-containers for more information.
44+
template <class _Alloc>
45+
struct __asan_annotate_container_with_allocator : true_type {};
46+
#endif
47+
48+
// Annotate a double-ended contiguous range.
49+
// - [__first_storage, __last_storage) is the allocated memory region,
50+
// - [__first_old_contained, __last_old_contained) is the previously allowed (unpoisoned) range, and
51+
// - [__first_new_contained, __last_new_contained) is the new allowed (unpoisoned) range.
52+
template <class _Allocator>
53+
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
54+
const void* __first_storage,
55+
const void* __last_storage,
56+
const void* __first_old_contained,
57+
const void* __last_old_contained,
58+
const void* __first_new_contained,
59+
const void* __last_new_contained) {
60+
#ifdef _LIBCPP_HAS_NO_ASAN
61+
(void)__first_storage;
62+
(void)__last_storage;
63+
(void)__first_old_contained;
64+
(void)__last_old_contained;
65+
(void)__first_new_contained;
66+
(void)__last_new_contained;
67+
#else
68+
if (__asan_annotate_container_with_allocator<_Allocator>::value && __first_storage != nullptr)
69+
__sanitizer_annotate_double_ended_contiguous_container(
70+
__first_storage,
71+
__last_storage,
72+
__first_old_contained,
73+
__last_old_contained,
74+
__first_new_contained,
75+
__last_new_contained);
76+
#endif
77+
}
78+
79+
// Annotate a contiguous range.
80+
// [__first_storage, __last_storage) is the allocated memory region,
81+
// __old_last_contained is the previously last allowed (unpoisoned) element, and
82+
// __new_last_contained is the new last allowed (unpoisoned) element.
83+
template <class _Allocator>
84+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __annotate_contiguous_container(
85+
const void* __first_storage,
86+
const void* __last_storage,
87+
const void* __old_last_contained,
88+
const void* __new_last_contained) {
89+
#ifdef _LIBCPP_HAS_NO_ASAN
90+
(void)__first_storage;
91+
(void)__last_storage;
92+
(void)__old_last_contained;
93+
(void)__new_last_contained;
94+
#else
95+
if (!__libcpp_is_constant_evaluated() && __asan_annotate_container_with_allocator<_Allocator>::value &&
96+
__first_storage != nullptr)
97+
__sanitizer_annotate_contiguous_container(
98+
__first_storage, __last_storage, __old_last_contained, __new_last_contained);
99+
#endif
100+
}
101+
102+
_LIBCPP_END_NAMESPACE_STD
103+
104+
#endif // _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H

libcxx/include/__memory/allocator_traits.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -407,18 +407,6 @@ struct __is_cpp17_copy_insertable<
407407
__has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > >
408408
: __is_cpp17_move_insertable<_Alloc> {};
409409

410-
// ASan choices
411-
#ifndef _LIBCPP_HAS_NO_ASAN
412-
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
413-
#endif
414-
415-
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS
416-
template <class _Alloc>
417-
struct __asan_annotate_container_with_allocator : true_type {};
418-
template <class _Tp>
419-
struct __asan_annotate_container_with_allocator<allocator<_Tp> > : true_type {};
420-
#endif
421-
422410
#undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX
423411

424412
_LIBCPP_END_NAMESPACE_STD

libcxx/include/deque

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ template <class T, class Allocator, class Predicate>
191191
#include <__assert>
192192
#include <__availability>
193193
#include <__config>
194+
#include <__debug_utils/sanitizers.h>
194195
#include <__format/enable_insertable.h>
195196
#include <__fwd/deque.h>
196197
#include <__iterator/distance.h>
@@ -864,33 +865,6 @@ private:
864865
__asan_back_moved,
865866
};
866867

867-
// The following functions are no-ops outside of AddressSanitizer mode.
868-
// We call annotations for every allocator, unless explicitly disabled.
869-
//
870-
// To disable annotations for a particular allocator, change value of
871-
// __asan_annotate_container_with_allocator to false.
872-
// For more details, see the "Using libc++" documentation page or
873-
// the documentation for __sanitizer_annotate_contiguous_container.
874-
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
875-
const void* __beg,
876-
const void* __end,
877-
const void* __old_con_beg,
878-
const void* __old_con_end,
879-
const void* __new_con_beg,
880-
const void* __new_con_end) const {
881-
(void)__beg;
882-
(void)__end;
883-
(void)__old_con_beg;
884-
(void)__old_con_end;
885-
(void)__new_con_beg;
886-
(void)__new_con_end;
887-
#ifndef _LIBCPP_HAS_NO_ASAN
888-
if (__beg != nullptr && __asan_annotate_container_with_allocator<_Allocator>::value)
889-
__sanitizer_annotate_double_ended_contiguous_container(
890-
__beg, __end, __old_con_beg, __old_con_end, __new_con_beg, __new_con_end);
891-
#endif
892-
}
893-
894868
_LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
895869
size_type __beg,
896870
size_type __end,
@@ -990,7 +964,8 @@ private:
990964
const void* __new_beg = __front ? __new_edge : __old_edge;
991965
const void* __new_end = __front ? __old_edge : __new_edge;
992966

993-
__annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
967+
std::__annotate_double_ended_contiguous_container<_Allocator>(
968+
__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
994969
}
995970
#endif // !_LIBCPP_HAS_NO_ASAN
996971
}
@@ -1051,11 +1026,7 @@ private:
10511026
}
10521027

10531028
_LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
1054-
(void)__beginning;
1055-
(void)__end;
1056-
#ifndef _LIBCPP_HAS_NO_ASAN
1057-
__annotate_double_ended_contiguous_container(__beginning, __end, __beginning, __end, __end, __end);
1058-
#endif
1029+
std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
10591030
}
10601031

10611032
_LIBCPP_HIDE_FROM_ABI void
@@ -1070,7 +1041,7 @@ private:
10701041
if (__annotation_type == __asan_poison)
10711042
__annotate_poison_block(__block_start, __block_end);
10721043
else {
1073-
__annotate_double_ended_contiguous_container(
1044+
std::__annotate_double_ended_contiguous_container<_Allocator>(
10741045
__block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
10751046
}
10761047
#endif

libcxx/include/module.modulemap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ module std_private_coroutine_noop_coroutine_handle [system] { header "__coroutin
12541254
module std_private_coroutine_trivial_awaitables [system] { header "__coroutine/trivial_awaitables.h" }
12551255

12561256
module std_private_debug_utils_randomize_range [system] { header "__debug_utils/randomize_range.h" }
1257+
module std_private_debug_utils_sanitizers [system] { header "__debug_utils/sanitizers.h" }
12571258
module std_private_debug_utils_strict_weak_ordering_check [system] {
12581259
header "__debug_utils/strict_weak_ordering_check.h"
12591260
export std_private_type_traits_is_constant_evaluated

libcxx/include/string

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
574574
#include <__algorithm/remove_if.h>
575575
#include <__assert>
576576
#include <__config>
577+
#include <__debug_utils/sanitizers.h>
577578
#include <__format/enable_insertable.h>
578579
#include <__functional/hash.h>
579580
#include <__functional/unary_function.h>
@@ -1909,10 +1910,7 @@ private:
19091910
(void)__old_mid;
19101911
(void)__new_mid;
19111912
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
1912-
const void* __begin = data();
1913-
const void* __end = data() + capacity() + 1;
1914-
if (__asan_annotate_container_with_allocator<allocator_type>::value && !__libcpp_is_constant_evaluated())
1915-
__sanitizer_annotate_contiguous_container(__begin, __end, __old_mid, __new_mid);
1913+
std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity() + 1, __old_mid, __new_mid);
19161914
#endif
19171915
}
19181916

libcxx/include/vector

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
320320
#include <__bit_reference>
321321
#include <__concepts/same_as.h>
322322
#include <__config>
323+
#include <__debug_utils/sanitizers.h>
323324
#include <__format/enable_insertable.h>
324325
#include <__format/formatter.h>
325326
#include <__format/formatter_bool.h>
@@ -834,15 +835,7 @@ private:
834835

835836
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
836837
__annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
837-
(void)__old_mid;
838-
(void)__new_mid;
839-
#ifndef _LIBCPP_HAS_NO_ASAN
840-
const void* __beg = data();
841-
const void* __end = data() + capacity();
842-
if (!__libcpp_is_constant_evaluated() && __beg != nullptr &&
843-
__asan_annotate_container_with_allocator<_Allocator>::value)
844-
__sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
845-
#endif
838+
std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity(), __old_mid, __new_mid);
846839
}
847840

848841
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {

0 commit comments

Comments
 (0)