blob: 21b5e860e1434f37d002f57a19a8c42aea73fa3e [file] [log] [blame]
Marshall Clow280ddee2011-06-10 03:40:19 +00001//===--------------------------- test_vector2.cpp -------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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
Marshall Clow280ddee2011-06-10 03:40:19 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne8c611142020-04-17 10:29:15 -04009// UNSUPPORTED: no-exceptions
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000010
Marshall Clow280ddee2011-06-10 03:40:19 +000011#include "cxxabi.h"
12
13#include <iostream>
14#include <cstdlib>
15
16void my_terminate () { exit ( 0 ); }
17
18// Wrapper routines
19void *my_alloc2 ( size_t sz ) {
20 void *p = std::malloc ( sz );
Louis Dionnee1c67272020-04-17 10:06:55 -040021// std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
Marshall Clow280ddee2011-06-10 03:40:19 +000022 return p;
23 }
Louis Dionnee1c67272020-04-17 10:06:55 -040024
Marshall Clow280ddee2011-06-10 03:40:19 +000025void my_dealloc2 ( void *p ) {
Louis Dionnee1c67272020-04-17 10:06:55 -040026// std::printf ( "Freeing %lx\n", (unsigned long) p );
27 std::free ( p );
Marshall Clow280ddee2011-06-10 03:40:19 +000028 }
29
Eric Fiseliera140cba2016-12-24 00:37:13 +000030void my_dealloc3 ( void *p, size_t ) {
Louis Dionnee1c67272020-04-17 10:06:55 -040031// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
32 std::free ( p );
Marshall Clow280ddee2011-06-10 03:40:19 +000033 }
34
Eric Fiseliera140cba2016-12-24 00:37:13 +000035void my_construct ( void *) {
Marshall Clow280ddee2011-06-10 03:40:19 +000036// std::printf ( "Constructing %lx\n", (unsigned long) p );
37 }
38
Eric Fiseliera140cba2016-12-24 00:37:13 +000039void my_destruct ( void *) {
Marshall Clow280ddee2011-06-10 03:40:19 +000040// std::printf ( "Destructing %lx\n", (unsigned long) p );
41 }
42
43int gCounter;
Eric Fiseliera140cba2016-12-24 00:37:13 +000044void count_construct ( void * ) { ++gCounter; }
45void count_destruct ( void * ) { --gCounter; }
Marshall Clow280ddee2011-06-10 03:40:19 +000046
47
48int gConstructorCounter;
49int gConstructorThrowTarget;
50int gDestructorCounter;
51int gDestructorThrowTarget;
Eric Fiseliera140cba2016-12-24 00:37:13 +000052void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
53void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; }
Marshall Clow280ddee2011-06-10 03:40:19 +000054
55struct vec_on_stack {
56 void *storage;
57 vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
58 ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
59 };
60
61
62// Make sure the constructors and destructors are matched
63void test_exception_in_destructor ( ) {
Marshall Clow280ddee2011-06-10 03:40:19 +000064
65// Try throwing from a destructor while unwinding the stack -- should abort
66 gConstructorCounter = gDestructorCounter = 0;
67 gConstructorThrowTarget = -1;
68 gDestructorThrowTarget = 5;
69 try {
70 vec_on_stack v;
71 throw 3;
72 }
73 catch ( int i ) {}
74
Louis Dionnee1c67272020-04-17 10:06:55 -040075 std::cerr << "should never get here" << std::endl;
Marshall Clow280ddee2011-06-10 03:40:19 +000076 }
77
78
79
Eric Fiseliera140cba2016-12-24 00:37:13 +000080int main () {
Marshall Clow280ddee2011-06-10 03:40:19 +000081 std::set_terminate ( my_terminate );
82 test_exception_in_destructor ();
83 return 1; // we failed if we get here
84 }