69 lines · cpp
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// REQUIRES: asan10 11// <deque>12 13// reference operator[](size_type n);14 15#include "asan_testing.h"16#include <deque>17#include <cassert>18#include <cstdlib>19 20#include "min_allocator.h"21#include "test_iterators.h"22#include "test_macros.h"23 24extern "C" void __sanitizer_set_death_callback(void (*callback)(void));25 26void do_exit() { exit(0); }27 28int main(int, char**) {29 {30 typedef cpp17_input_iterator<int*> MyInputIter;31 // Should not trigger ASan.32 std::deque<int> v;33 int i[] = {42};34 v.insert(v.begin(), MyInputIter(i), MyInputIter(i + 1));35 assert(v[0] == 42);36 assert(is_double_ended_contiguous_container_asan_correct(v));37 }38 {39 typedef int T;40 typedef std::deque<T, min_allocator<T> > C;41 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};42 C c(std::begin(t), std::end(t));43 assert(is_double_ended_contiguous_container_asan_correct(c));44 }45 {46 typedef char T;47 typedef std::deque<T, safe_allocator<T> > C;48 const T t[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};49 C c(std::begin(t), std::end(t));50 c.pop_front();51 assert(is_double_ended_contiguous_container_asan_correct(c));52 }53 __sanitizer_set_death_callback(do_exit);54 {55 typedef int T;56 typedef std::deque<T> C;57 const T t[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};58 C c(std::begin(t), std::end(t));59 assert(is_double_ended_contiguous_container_asan_correct(c));60 T* ptr = &c[0];61 for (size_t i = 0; i < (8 + sizeof(T) - 1) / sizeof(T); ++i)62 c.pop_front();63 *ptr = 1;64 volatile T foo = c[c.size()]; // should trigger ASAN. Use volatile to prevent being optimized away.65 assert(false); // if we got here, ASAN didn't trigger66 ((void)foo);67 }68}69