197 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// UNSUPPORTED: no-exceptions10// Test asan vector annotations with a class that throws in a CTOR.11 12#include <vector>13#include <cassert>14 15#include "test_macros.h"16#include "asan_testing.h"17 18class X {19public:20 X(const X& x) { Init(x.a); }21 X(char arg) { Init(arg); }22 X() { Init(42); }23 X& operator=(const X& x) {24 Init(x.a);25 return *this;26 }27 void Init(char arg) {28 if (arg == 42)29 throw 0;30 if (arg == 66)31 arg = 42;32 a = arg;33 }34 char get() const { return a; }35 void set(char arg) { a = arg; }36 37private:38 char a;39};40 41class ThrowOnCopy {42public:43 ThrowOnCopy() : should_throw(false) {}44 explicit ThrowOnCopy(bool xshould_throw) : should_throw(xshould_throw) {}45 46 ThrowOnCopy(ThrowOnCopy const& other) : should_throw(other.should_throw) {47 if (should_throw) {48 throw 0;49 }50 }51 ThrowOnCopy& operator=(ThrowOnCopy const&) = default;52 53 bool should_throw;54};55 56void test_push_back() {57 std::vector<X> v;58 v.reserve(2);59 v.push_back(X(2));60 assert(v.size() == 1);61 try {62 v.push_back(X(66));63 assert(0);64 } catch (int e) {65 assert(v.size() == 1);66 }67 assert(v.size() == 1);68 assert(is_contiguous_container_asan_correct(v));69}70 71void test_insert_range() {72 std::vector<X> v;73 v.reserve(4);74 v.push_back(X(1));75 v.push_back(X(2));76 assert(v.size() == 2);77 assert(v.capacity() >= 4);78 try {79 char a[2] = {21, 42};80 v.insert(v.end(), a, a + 2);81 assert(0);82 } catch (int e) {83 assert(v.size() == 2);84 }85 assert(v.size() == 2);86 assert(is_contiguous_container_asan_correct(v));87}88 89void test_insert() {90 std::vector<X> v;91 v.reserve(3);92 v.insert(v.end(), X(1));93 v.insert(v.begin(), X(2));94 assert(v.size() == 2);95 try {96 v.insert(v.end(), X(66));97 assert(0);98 } catch (int e) {99 assert(v.size() == 2);100 }101 assert(v.size() == 2);102 assert(is_contiguous_container_asan_correct(v));103}104 105void test_insert_range2() {106 std::vector<X> v;107 v.reserve(4);108 v.insert(v.end(), X(1));109 v.insert(v.begin(), X(2));110 assert(v.size() == 2);111 assert(v.capacity() >= 4);112 try {113 char a[2] = {10, 42};114 v.insert(v.begin(), a, a + 2);115 assert(0);116 } catch (int e) {117 assert(v.size() <= 4);118 assert(is_contiguous_container_asan_correct(v));119 return;120 }121 assert(0);122}123 124void test_insert_n() {125 std::vector<X> v;126 v.reserve(10);127 v.insert(v.end(), X(1));128 v.insert(v.begin(), X(2));129 assert(v.size() == 2);130 try {131 v.insert(v.begin(), 1, X(66));132 assert(0);133 } catch (int e) {134 assert(v.size() <= 3);135 assert(is_contiguous_container_asan_correct(v));136 return;137 }138 assert(0);139}140 141void test_insert_n2() {142 std::vector<ThrowOnCopy> v(10);143 v.reserve(100);144 assert(v.size() == 10);145 v[6].should_throw = true;146 try {147 v.insert(v.cbegin(), 5, ThrowOnCopy());148 assert(0);149 } catch (int e) {150 assert(v.size() == 11);151 assert(is_contiguous_container_asan_correct(v));152 return;153 }154 assert(0);155}156 157void test_resize() {158 std::vector<X> v;159 v.reserve(3);160 v.push_back(X(0));161 try {162 v.resize(3);163 assert(0);164 } catch (int e) {165 assert(v.size() == 1);166 }167 assert(v.size() == 1);168 assert(is_contiguous_container_asan_correct(v));169}170 171void test_resize_param() {172 std::vector<X> v;173 v.reserve(3);174 v.push_back(X(0));175 try {176 v.resize(3, X(66));177 assert(0);178 } catch (int e) {179 assert(v.size() == 1);180 }181 assert(v.size() == 1);182 assert(is_contiguous_container_asan_correct(v));183}184 185int main(int, char**) {186 test_push_back();187 test_insert_range();188 test_insert();189 test_insert_range2();190 test_insert_n();191 test_insert_n2();192 test_resize();193 test_resize_param();194 195 return 0;196}197