blob: 5a5ff06f4eb0e9f1a962d91a2dc01438a8b24f09 [file] [log] [blame]
Marshall Clow280ddee2011-06-10 03:40:19 +00001//===--------------------------- test_vector2.cpp -------------------------===//
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
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000010// UNSUPPORTED: libcxxabi-no-exceptions
11
Marshall Clow280ddee2011-06-10 03:40:19 +000012#include "cxxabi.h"
13
14#include <iostream>
15#include <cstdlib>
16
17void my_terminate () { exit ( 0 ); }
18
19// Wrapper routines
20void *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
26void my_dealloc2 ( void *p ) {
27// std::printf ( "Freeing %lx\n", (unsigned long) p );
28 std::free ( p );
29 }
30
Eric Fiseliera140cba2016-12-24 00:37:13 +000031void my_dealloc3 ( void *p, size_t ) {
Marshall Clow280ddee2011-06-10 03:40:19 +000032// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
33 std::free ( p );
34 }
35
Eric Fiseliera140cba2016-12-24 00:37:13 +000036void my_construct ( void *) {
Marshall Clow280ddee2011-06-10 03:40:19 +000037// std::printf ( "Constructing %lx\n", (unsigned long) p );
38 }
39
Eric Fiseliera140cba2016-12-24 00:37:13 +000040void my_destruct ( void *) {
Marshall Clow280ddee2011-06-10 03:40:19 +000041// std::printf ( "Destructing %lx\n", (unsigned long) p );
42 }
43
44int gCounter;
Eric Fiseliera140cba2016-12-24 00:37:13 +000045void count_construct ( void * ) { ++gCounter; }
46void count_destruct ( void * ) { --gCounter; }
Marshall Clow280ddee2011-06-10 03:40:19 +000047
48
49int gConstructorCounter;
50int gConstructorThrowTarget;
51int gDestructorCounter;
52int gDestructorThrowTarget;
Eric Fiseliera140cba2016-12-24 00:37:13 +000053void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
54void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; }
Marshall Clow280ddee2011-06-10 03:40:19 +000055
56struct vec_on_stack {
57 void *storage;
58 vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
59 ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
60 };
61
62
63// Make sure the constructors and destructors are matched
64void test_exception_in_destructor ( ) {
Marshall Clow280ddee2011-06-10 03:40:19 +000065
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 }
74 catch ( int i ) {}
75
76 std::cerr << "should never get here" << std::endl;
77 }
78
79
80
Eric Fiseliera140cba2016-12-24 00:37:13 +000081int main () {
Marshall Clow280ddee2011-06-10 03:40:19 +000082 std::set_terminate ( my_terminate );
83 test_exception_in_destructor ();
84 return 1; // we failed if we get here
85 }