Path: blob/main/contrib/llvm-project/libcxx/include/__debug_utils/sanitizers.h
35233 views
//===----------------------------------------------------------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H9#define _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H1011#include <__config>12#include <__type_traits/integral_constant.h>13#include <__type_traits/is_constant_evaluated.h>1415#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)16# pragma GCC system_header17#endif1819#ifndef _LIBCPP_HAS_NO_ASAN2021extern "C" {22_LIBCPP_EXPORTED_FROM_ABI void23__sanitizer_annotate_contiguous_container(const void*, const void*, const void*, const void*);24_LIBCPP_EXPORTED_FROM_ABI void __sanitizer_annotate_double_ended_contiguous_container(25const void*, const void*, const void*, const void*, const void*, const void*);26_LIBCPP_EXPORTED_FROM_ABI int27__sanitizer_verify_double_ended_contiguous_container(const void*, const void*, const void*, const void*);28}2930#endif // _LIBCPP_HAS_NO_ASAN3132_LIBCPP_BEGIN_NAMESPACE_STD3334// ASan choices35#ifndef _LIBCPP_HAS_NO_ASAN36# define _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS 137#endif3839#ifdef _LIBCPP_HAS_ASAN_CONTAINER_ANNOTATIONS_FOR_ALL_ALLOCATORS40// __asan_annotate_container_with_allocator determines whether containers with custom allocators are annotated. This is41// a public customization point to disable annotations if the custom allocator assumes that the memory isn't poisoned.42// See the https://libcxx.llvm.org/UsingLibcxx.html#turning-off-asan-annotation-in-containers for more information.43template <class _Alloc>44struct __asan_annotate_container_with_allocator : true_type {};45#endif4647// Annotate a double-ended contiguous range.48// - [__first_storage, __last_storage) is the allocated memory region,49// - [__first_old_contained, __last_old_contained) is the previously allowed (unpoisoned) range, and50// - [__first_new_contained, __last_new_contained) is the new allowed (unpoisoned) range.51template <class _Allocator>52_LIBCPP_HIDE_FROM_ABI void __annotate_double_ended_contiguous_container(53const void* __first_storage,54const void* __last_storage,55const void* __first_old_contained,56const void* __last_old_contained,57const void* __first_new_contained,58const void* __last_new_contained) {59#ifdef _LIBCPP_HAS_NO_ASAN60(void)__first_storage;61(void)__last_storage;62(void)__first_old_contained;63(void)__last_old_contained;64(void)__first_new_contained;65(void)__last_new_contained;66#else67if (__asan_annotate_container_with_allocator<_Allocator>::value && __first_storage != nullptr)68__sanitizer_annotate_double_ended_contiguous_container(69__first_storage,70__last_storage,71__first_old_contained,72__last_old_contained,73__first_new_contained,74__last_new_contained);75#endif76}7778// Annotate a contiguous range.79// [__first_storage, __last_storage) is the allocated memory region,80// __old_last_contained is the previously last allowed (unpoisoned) element, and81// __new_last_contained is the new last allowed (unpoisoned) element.82template <class _Allocator>83_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 void __annotate_contiguous_container(84const void* __first_storage,85const void* __last_storage,86const void* __old_last_contained,87const void* __new_last_contained) {88#ifdef _LIBCPP_HAS_NO_ASAN89(void)__first_storage;90(void)__last_storage;91(void)__old_last_contained;92(void)__new_last_contained;93#else94if (!__libcpp_is_constant_evaluated() && __asan_annotate_container_with_allocator<_Allocator>::value &&95__first_storage != nullptr)96__sanitizer_annotate_contiguous_container(97__first_storage, __last_storage, __old_last_contained, __new_last_contained);98#endif99}100101_LIBCPP_END_NAMESPACE_STD102103#endif // _LIBCPP___LIBCXX_DEBUG_UTILS_SANITIZERS_H104105106