[libc++][NFC] Move basic ASan annotation functions into a utility header (#87220)

This commit is contained in:
Nikolas Klauser 2024-04-13 18:24:12 +02:00 committed by GitHub
parent 26852565a5
commit 61f1f13002
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 115 additions and 68 deletions

View File

@ -345,6 +345,7 @@ set(files
__coroutine/noop_coroutine_handle.h
__coroutine/trivial_awaitables.h
__debug_utils/randomize_range.h
__debug_utils/sanitizers.h
__debug_utils/strict_weak_ordering_check.h
__exception/exception.h
__exception/exception_ptr.h

View File

@ -1061,15 +1061,6 @@ typedef __char32_t char32_t;
# define _LIBCPP_CONSTEXPR_SINCE_CXX23
# endif
# ifndef _LIBCPP_HAS_NO_ASAN
extern "C" _LIBCPP_EXPORTED_FROM_ABI void
__sanitizer_annotate_contiguous_container(const void*, const void*, const void*, const void*);
extern "C" _LIBCPP_EXPORTED_FROM_ABI void __sanitizer_annotate_double_ended_contiguous_container(
const void*, const void*, const void*, const void*, const void*, const void*);
extern "C" _LIBCPP_EXPORTED_FROM_ABI int
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
# endif
// Try to find out if RTTI is disabled.
# if !defined(__cpp_rtti) || __cpp_rtti < 199711L
# define _LIBCPP_HAS_NO_RTTI

View File

@ -0,0 +1,104 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H
#define _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H
#include <__config>
#include <__type_traits/integral_constant.h>
#include <__type_traits/is_constant_evaluated.h>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
# pragma GCC system_header
#endif
#ifndef _LIBCPP_HAS_NO_ASAN
extern "C" {
_LIBCPP_EXPORTED_FROM_ABI void
__sanitizer_annotate_contiguous_container(const void*, const void*, const void*, const void*);
_LIBCPP_EXPORTED_FROM_ABI void __sanitizer_annotate_double_ended_contiguous_container(
const void*, const void*, const void*, const void*, const void*, const void*);
_LIBCPP_EXPORTED_FROM_ABI int
__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);
}
#endif // _LIBCPP_HAS_NO_ASAN
_LIBCPP_BEGIN_NAMESPACE_STD
// ASan choices
#ifndef _LIBCPP_HAS_NO_ASAN
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
#endif
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS
// __asan_annotate_container_with_allocator determines whether containers with custom allocators are annotated. This is
// a public customization point to disable annotations if the custom allocator assumes that the memory isn't poisoned.
// See the https://libcxx.llvm.org/UsingLibcxx.html#turning-off-asan-annotation-in-containers for more information.
template <class _Alloc>
struct __asan_annotate_container_with_allocator : true_type {};
#endif
// Annotate a double-ended contiguous range.
// - [__first_storage, __last_storage) is the allocated memory region,
// - [__first_old_contained, __last_old_contained) is the previously allowed (unpoisoned) range, and
// - [__first_new_contained, __last_new_contained) is the new allowed (unpoisoned) range.
template <class _Allocator>
_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(
const void* __first_storage,
const void* __last_storage,
const void* __first_old_contained,
const void* __last_old_contained,
const void* __first_new_contained,
const void* __last_new_contained) {
#ifdef _LIBCPP_HAS_NO_ASAN
(void)__first_storage;
(void)__last_storage;
(void)__first_old_contained;
(void)__last_old_contained;
(void)__first_new_contained;
(void)__last_new_contained;
#else
if (__asan_annotate_container_with_allocator<_Allocator>::value && __first_storage != nullptr)
__sanitizer_annotate_double_ended_contiguous_container(
__first_storage,
__last_storage,
__first_old_contained,
__last_old_contained,
__first_new_contained,
__last_new_contained);
#endif
}
// Annotate a contiguous range.
// [__first_storage, __last_storage) is the allocated memory region,
// __old_last_contained is the previously last allowed (unpoisoned) element, and
// __new_last_contained is the new last allowed (unpoisoned) element.
template <class _Allocator>
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __annotate_contiguous_container(
const void* __first_storage,
const void* __last_storage,
const void* __old_last_contained,
const void* __new_last_contained) {
#ifdef _LIBCPP_HAS_NO_ASAN
(void)__first_storage;
(void)__last_storage;
(void)__old_last_contained;
(void)__new_last_contained;
#else
if (!__libcpp_is_constant_evaluated() && __asan_annotate_container_with_allocator<_Allocator>::value &&
__first_storage != nullptr)
__sanitizer_annotate_contiguous_container(
__first_storage, __last_storage, __old_last_contained, __new_last_contained);
#endif
}
_LIBCPP_END_NAMESPACE_STD
#endif // _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H

View File

@ -407,18 +407,6 @@ struct __is_cpp17_copy_insertable<
__has_construct<_Alloc, typename _Alloc::value_type*, const typename _Alloc::value_type&>::value > >
: __is_cpp17_move_insertable<_Alloc> {};
// ASan choices
#ifndef _LIBCPP_HAS_NO_ASAN
# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 1
#endif
#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS
template <class _Alloc>
struct __asan_annotate_container_with_allocator : true_type {};
template <class _Tp>
struct __asan_annotate_container_with_allocator<allocator<_Tp> > : true_type {};
#endif
#undef _LIBCPP_ALLOCATOR_TRAITS_HAS_XXX
_LIBCPP_END_NAMESPACE_STD

View File

@ -191,6 +191,7 @@ template <class T, class Allocator, class Predicate>
#include <__assert>
#include <__availability>
#include <__config>
#include <__debug_utils/sanitizers.h>
#include <__format/enable_insertable.h>
#include <__fwd/deque.h>
#include <__iterator/distance.h>
@ -864,33 +865,6 @@ private:
__asan_back_moved,
};
// The following functions are no-ops outside of AddressSanitizer mode.
// We call annotations for every allocator, unless explicitly disabled.
//
// To disable annotations for a particular allocator, change value of
// __asan_annotate_container_with_allocator to false.
// For more details, see the "Using libc++" documentation page or
// the documentation for __sanitizer_annotate_contiguous_container.
_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 {
(void)__beg;
(void)__end;
(void)__old_con_beg;
(void)__old_con_end;
(void)__new_con_beg;
(void)__new_con_end;
#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
}
_LIBCPP_HIDE_FROM_ABI void __annotate_from_to(
size_type __beg,
size_type __end,
@ -990,7 +964,8 @@ private:
const void* __new_beg = __front ? __new_edge : __old_edge;
const void* __new_end = __front ? __old_edge : __new_edge;
__annotate_double_ended_contiguous_container(__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
std::__annotate_double_ended_contiguous_container<_Allocator>(
__mem_beg, __mem_end, __old_beg, __old_end, __new_beg, __new_end);
}
#endif // !_LIBCPP_HAS_NO_ASAN
}
@ -1051,11 +1026,7 @@ private:
}
_LIBCPP_HIDE_FROM_ABI void __annotate_poison_block(const void* __beginning, const void* __end) const _NOEXCEPT {
(void)__beginning;
(void)__end;
#ifndef _LIBCPP_HAS_NO_ASAN
__annotate_double_ended_contiguous_container(__beginning, __end, __beginning, __end, __end, __end);
#endif
std::__annotate_double_ended_contiguous_container<_Allocator>(__beginning, __end, __beginning, __end, __end, __end);
}
_LIBCPP_HIDE_FROM_ABI void
@ -1070,7 +1041,7 @@ private:
if (__annotation_type == __asan_poison)
__annotate_poison_block(__block_start, __block_end);
else {
__annotate_double_ended_contiguous_container(
std::__annotate_double_ended_contiguous_container<_Allocator>(
__block_start, __block_end, __block_start, __block_start, __block_start, __block_end);
}
#endif

View File

@ -1254,6 +1254,7 @@ module std_private_coroutine_noop_coroutine_handle [system] { header "__coroutin
module std_private_coroutine_trivial_awaitables [system] { header "__coroutine/trivial_awaitables.h" }
module std_private_debug_utils_randomize_range [system] { header "__debug_utils/randomize_range.h" }
module std_private_debug_utils_sanitizers [system] { header "__debug_utils/sanitizers.h" }
module std_private_debug_utils_strict_weak_ordering_check [system] {
header "__debug_utils/strict_weak_ordering_check.h"
export std_private_type_traits_is_constant_evaluated

View File

@ -574,6 +574,7 @@ basic_string<char32_t> operator""s( const char32_t *str, size_t len );
#include <__algorithm/remove_if.h>
#include <__assert>
#include <__config>
#include <__debug_utils/sanitizers.h>
#include <__format/enable_insertable.h>
#include <__functional/hash.h>
#include <__functional/unary_function.h>
@ -1909,10 +1910,7 @@ private:
(void)__old_mid;
(void)__new_mid;
#if !defined(_LIBCPP_HAS_NO_ASAN) && defined(_LIBCPP_INSTRUMENTED_WITH_ASAN)
const void* __begin = data();
const void* __end = data() + capacity() + 1;
if (__asan_annotate_container_with_allocator<allocator_type>::value && !__libcpp_is_constant_evaluated())
__sanitizer_annotate_contiguous_container(__begin, __end, __old_mid, __new_mid);
std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity() + 1, __old_mid, __new_mid);
#endif
}

View File

@ -320,6 +320,7 @@ template<class T, class charT> requires is-vector-bool-reference<T> // Since C++
#include <__bit_reference>
#include <__concepts/same_as.h>
#include <__config>
#include <__debug_utils/sanitizers.h>
#include <__format/enable_insertable.h>
#include <__format/formatter.h>
#include <__format/formatter_bool.h>
@ -834,15 +835,7 @@ private:
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void
__annotate_contiguous_container(const void* __old_mid, const void* __new_mid) const {
(void)__old_mid;
(void)__new_mid;
#ifndef _LIBCPP_HAS_NO_ASAN
const void* __beg = data();
const void* __end = data() + capacity();
if (!__libcpp_is_constant_evaluated() && __beg != nullptr &&
__asan_annotate_container_with_allocator<_Allocator>::value)
__sanitizer_annotate_contiguous_container(__beg, __end, __old_mid, __new_mid);
#endif
std::__annotate_contiguous_container<_Allocator>(data(), data() + capacity(), __old_mid, __new_mid);
}
_LIBCPP_CONSTEXPR_SINCE_CXX20 _LIBCPP_HIDE_FROM_ABI void __annotate_new(size_type __current_size) const _NOEXCEPT {