1 //===--------------------------- test_vector2.cpp -------------------------===//
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 // UNSUPPORTED: no-exceptions
10
11 #include "cxxabi.h"
12
13 #include <cassert>
14 #include <cstdlib>
15 #include <exception>
16
my_terminate()17 void my_terminate () { exit ( 0 ); }
18
19 // Wrapper routines
my_alloc2(size_t sz)20 void *my_alloc2 ( size_t sz ) {
21 void *p = std::malloc ( sz );
22 // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
23 return p;
24 }
25
my_dealloc2(void * p)26 void my_dealloc2 ( void *p ) {
27 // std::printf ( "Freeing %lx\n", (unsigned long) p );
28 std::free ( p );
29 }
30
my_dealloc3(void * p,size_t)31 void my_dealloc3 ( void *p, size_t ) {
32 // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
33 std::free ( p );
34 }
35
my_construct(void *)36 void my_construct ( void *) {
37 // std::printf ( "Constructing %lx\n", (unsigned long) p );
38 }
39
my_destruct(void *)40 void my_destruct ( void *) {
41 // std::printf ( "Destructing %lx\n", (unsigned long) p );
42 }
43
44 int gCounter;
count_construct(void *)45 void count_construct ( void * ) { ++gCounter; }
count_destruct(void *)46 void count_destruct ( void * ) { --gCounter; }
47
48
49 int gConstructorCounter;
50 int gConstructorThrowTarget;
51 int gDestructorCounter;
52 int gDestructorThrowTarget;
throw_construct(void *)53 void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
throw_destruct(void *)54 void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; }
55
56 struct vec_on_stack {
57 void *storage;
vec_on_stackvec_on_stack58 vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
~vec_on_stackvec_on_stack59 ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
60 };
61
62
63 // Make sure the constructors and destructors are matched
test_exception_in_destructor()64 void test_exception_in_destructor ( ) {
65
66 // Try throwing from a destructor while unwinding the stack -- should abort
67 gConstructorCounter = gDestructorCounter = 0;
68 gConstructorThrowTarget = -1;
69 gDestructorThrowTarget = 5;
70 try {
71 vec_on_stack v;
72 throw 3;
73 } catch ( int i ) {
74
75 }
76
77 assert(false && "should never get here");
78 }
79
80
81
main()82 int main () {
83 std::set_terminate ( my_terminate );
84 test_exception_in_destructor ();
85 return 1; // we failed if we get here
86 }
87