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 ASAN_TESTING_H
10 #define ASAN_TESTING_H
11
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)
19 extern "C" int __sanitizer_verify_contiguous_container(const void* beg, const void* mid, const void* end);
20
21 template <typename T, typename Alloc>
is_contiguous_container_asan_correct(const std::vector<T,Alloc> & c)22 TEST_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 #else
30 template <typename T, typename Alloc>
is_contiguous_container_asan_correct(const std::vector<T,Alloc> &)31 TEST_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)
37 extern "C" int __sanitizer_verify_double_ended_contiguous_container(
38 const void* beg, const void* con_beg, const void* con_end, const void* end);
39 extern "C" bool __sanitizer_is_annotable(const void* address, const unsigned long size);
40 #include <deque>
41
42 template <class T, class Alloc>
is_double_ended_contiguous_container_asan_correct(const std::deque<T,Alloc> & c)43 TEST_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 #else
51 # include <deque>
52 template <class T, class Alloc>
is_double_ended_contiguous_container_asan_correct(const std::deque<T,Alloc> &)53 TEST_CONSTEXPR bool is_double_ended_contiguous_container_asan_correct(const std::deque<T, Alloc>&) {
54 return true;
55 }
56 #endif
57
58 #if TEST_HAS_FEATURE(address_sanitizer)
59 template <typename ChrT, typename TraitsT, typename Alloc>
is_string_asan_correct(const std::basic_string<ChrT,TraitsT,Alloc> & c)60 TEST_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 else
67 return __sanitizer_verify_contiguous_container(
68 c.data(), c.data() + c.capacity() + 1, c.data() + c.capacity() + 1) != 0;
69 }
70 #else
71 # include <string>
72 template <typename ChrT, typename TraitsT, typename Alloc>
is_string_asan_correct(const std::basic_string<ChrT,TraitsT,Alloc> &)73 TEST_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_H
78