78 lines · c
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-exception6//7//===----------------------------------------------------------------------===//8 9#ifndef ASAN_TESTING_H10#define ASAN_TESTING_H11 12#include "test_macros.h"13#include <vector>14#include <string>15#include <memory>16#include <type_traits>17 18#if TEST_HAS_FEATURE(address_sanitizer)19extern "C" int __sanitizer_verify_contiguous_container(const void* beg, const void* mid, const void* end);20 21template <typename T, typename Alloc>22TEST_CONSTEXPR bool is_contiguous_container_asan_correct(const std::vector<T, Alloc>& c) {23 if (TEST_IS_CONSTANT_EVALUATED)24 return true;25 if (std::is_same<Alloc, std::allocator<T> >::value && c.data() != NULL)26 return __sanitizer_verify_contiguous_container(c.data(), c.data() + c.size(), c.data() + c.capacity()) != 0;27 return true;28}29#else30template <typename T, typename Alloc>31TEST_CONSTEXPR bool is_contiguous_container_asan_correct(const std::vector<T, Alloc>&) {32 return true;33}34#endif // TEST_HAS_FEATURE(address_sanitizer)35 36#if TEST_HAS_FEATURE(address_sanitizer)37extern "C" int __sanitizer_verify_double_ended_contiguous_container(38 const void* beg, const void* con_beg, const void* con_end, const void* end);39extern "C" bool __sanitizer_is_annotable(const void* address, const unsigned long size);40#include <deque>41 42template <class T, class Alloc>43TEST_CONSTEXPR bool is_double_ended_contiguous_container_asan_correct(const std::deque<T, Alloc>& c) {44 if (TEST_IS_CONSTANT_EVALUATED)45 return true;46 if (std::is_same<Alloc, std::allocator<T> >::value)47 return c.__verify_asan_annotations();48 return true;49}50#else51# include <deque>52template <class T, class Alloc>53TEST_CONSTEXPR bool is_double_ended_contiguous_container_asan_correct(const std::deque<T, Alloc>&) {54 return true;55}56#endif57 58#if TEST_HAS_FEATURE(address_sanitizer)59template <typename ChrT, typename TraitsT, typename Alloc>60TEST_CONSTEXPR bool is_string_asan_correct(const std::basic_string<ChrT, TraitsT, Alloc>& c) {61 if (TEST_IS_CONSTANT_EVALUATED)62 return true;63 64 if (std::__asan_annotate_container_with_allocator<Alloc>::value)65 return __sanitizer_verify_contiguous_container(c.data(), c.data() + c.size() + 1, c.data() + c.capacity() + 1) != 0;66 else67 return __sanitizer_verify_contiguous_container(68 c.data(), c.data() + c.capacity() + 1, c.data() + c.capacity() + 1) != 0;69}70#else71# include <string>72template <typename ChrT, typename TraitsT, typename Alloc>73TEST_CONSTEXPR bool is_string_asan_correct(const std::basic_string<ChrT, TraitsT, Alloc>&) {74 return true;75}76#endif // TEST_HAS_FEATURE(address_sanitizer)77#endif // ASAN_TESTING_H78