]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - test/libcxx/containers/sequences/vector/asan.pass.cpp
Vendor import of libc++ trunk r290819:
[FreeBSD/FreeBSD.git] / test / libcxx / containers / sequences / vector / asan.pass.cpp
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 // UNSUPPORTED: clang-3.3, clang-3.4, clang-3.5
11
12 // <vector>
13
14 // reference operator[](size_type n);
15
16 #include <vector>
17 #include <cassert>
18 #include <cstdlib>
19
20 #include "asan_testing.h"
21 #include "min_allocator.h"
22 #include "test_iterators.h"
23 #include "test_macros.h"
24
25 #ifndef _LIBCPP_HAS_NO_ASAN
26 extern "C" void __sanitizer_set_death_callback(void (*callback)(void));
27
28 void do_exit() {
29   exit(0);
30 }
31
32 int main()
33 {
34 #if TEST_STD_VER >= 11
35     {
36         typedef int T;
37         typedef std::vector<T, min_allocator<T>> C;
38         const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
39         C c(std::begin(t), std::end(t));
40         c.reserve(2*c.size());
41         volatile T foo = c[c.size()];    // bad, but not caught by ASAN
42         ((void)foo);
43     }
44 #endif
45
46     {
47         typedef input_iterator<int*> MyInputIter;
48         // Sould not trigger ASan.
49         std::vector<int> v;
50         v.reserve(1);
51         int i[] = {42};
52         v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1));
53         assert(v[0] == 42);
54         assert(is_contiguous_container_asan_correct(v));
55     }
56
57     __sanitizer_set_death_callback(do_exit);
58     {
59         typedef int T;
60         typedef std::vector<T> C;
61         const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
62         C c(std::begin(t), std::end(t));
63         c.reserve(2*c.size());
64         assert(is_contiguous_container_asan_correct(c));
65         assert(!__sanitizer_verify_contiguous_container( c.data(), c.data() + 1, c.data() + c.capacity()));
66         volatile T foo = c[c.size()]; // should trigger ASAN. Use volatile to prevent being optimized away.
67         assert(false);          // if we got here, ASAN didn't trigger
68         ((void)foo);
69     }
70 }
71 #else
72 int main () { return 0; }
73 #endif