Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame^] | 1 | //===--------------------------- 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 | |
| 10 | #include "cxxabi.h" |
| 11 | |
| 12 | #include <iostream> |
| 13 | #include <cstdlib> |
| 14 | |
| 15 | void my_terminate () { exit ( 0 ); } |
| 16 | |
| 17 | // Wrapper routines |
| 18 | void *my_alloc2 ( size_t sz ) { |
| 19 | void *p = std::malloc ( sz ); |
| 20 | // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p ); |
| 21 | return p; |
| 22 | } |
| 23 | |
| 24 | void my_dealloc2 ( void *p ) { |
| 25 | // std::printf ( "Freeing %lx\n", (unsigned long) p ); |
| 26 | std::free ( p ); |
| 27 | } |
| 28 | |
| 29 | void my_dealloc3 ( void *p, size_t sz ) { |
| 30 | // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz ); |
| 31 | std::free ( p ); |
| 32 | } |
| 33 | |
| 34 | void my_construct ( void *p ) { |
| 35 | // std::printf ( "Constructing %lx\n", (unsigned long) p ); |
| 36 | } |
| 37 | |
| 38 | void my_destruct ( void *p ) { |
| 39 | // std::printf ( "Destructing %lx\n", (unsigned long) p ); |
| 40 | } |
| 41 | |
| 42 | int gCounter; |
| 43 | void count_construct ( void *p ) { ++gCounter; } |
| 44 | void count_destruct ( void *p ) { --gCounter; } |
| 45 | |
| 46 | |
| 47 | int gConstructorCounter; |
| 48 | int gConstructorThrowTarget; |
| 49 | int gDestructorCounter; |
| 50 | int gDestructorThrowTarget; |
| 51 | void throw_construct ( void *p ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; } |
| 52 | void throw_destruct ( void *p ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; } |
| 53 | |
| 54 | struct vec_on_stack { |
| 55 | void *storage; |
| 56 | vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {} |
| 57 | ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); } |
| 58 | }; |
| 59 | |
| 60 | |
| 61 | // Make sure the constructors and destructors are matched |
| 62 | void test_exception_in_destructor ( ) { |
| 63 | void *one, *two, *three; |
| 64 | |
| 65 | // Throw from within a destructor |
| 66 | gConstructorCounter = gDestructorCounter = 0; |
| 67 | gConstructorThrowTarget = -1; |
| 68 | gDestructorThrowTarget = 15; |
| 69 | try { |
| 70 | one = two = three = NULL; |
| 71 | one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct ); |
| 72 | two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc2 ); |
| 73 | three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc3 ); |
| 74 | } |
| 75 | catch ( int i ) {} |
| 76 | |
| 77 | try { |
| 78 | __cxxabiv1::__cxa_vec_delete ( one, 40, 8, throw_destruct ); |
| 79 | __cxxabiv1::__cxa_vec_delete2( two, 40, 8, throw_destruct, my_dealloc2 ); |
| 80 | __cxxabiv1::__cxa_vec_delete3( three, 40, 8, throw_destruct, my_dealloc3 ); |
| 81 | } |
| 82 | catch ( int i ) {} |
| 83 | |
| 84 | // We should have thrown in the middle of cleaning up "two", which means that |
| 85 | // there should be 20 calls to the destructor, and "three" was not cleaned up. |
| 86 | if ( gConstructorCounter != 30 || gDestructorCounter != 20 ) { |
| 87 | std::cerr << "Unexpected Constructor/Destructor calls (1D)" << std::endl; |
| 88 | std::cerr << "Expected (30, 20), but got (" << gConstructorCounter << ", " << |
| 89 | gDestructorCounter << ")" << std::endl; |
| 90 | } |
| 91 | |
| 92 | // Try throwing from a destructor - should be fine. |
| 93 | gConstructorCounter = gDestructorCounter = 0; |
| 94 | gConstructorThrowTarget = -1; |
| 95 | gDestructorThrowTarget = 5; |
| 96 | try { vec_on_stack v; } |
| 97 | catch ( int i ) {} |
| 98 | |
| 99 | // there should be 20 calls to the destructor, and "three" was not cleaned up. |
| 100 | if ( gConstructorCounter != gDestructorCounter ) { |
| 101 | std::cerr << "Mismatched Constructor/Destructor calls (2C)" << std::endl; |
| 102 | std::cerr << gConstructorCounter << " constructors, but " << |
| 103 | gDestructorCounter << " destructors" << std::endl; |
| 104 | } |
| 105 | |
| 106 | // Try throwing from a destructor while unwinding the stack -- should abort |
| 107 | gConstructorCounter = gDestructorCounter = 0; |
| 108 | gConstructorThrowTarget = -1; |
| 109 | gDestructorThrowTarget = 5; |
| 110 | try { |
| 111 | vec_on_stack v; |
| 112 | throw 3; |
| 113 | } |
| 114 | catch ( int i ) {} |
| 115 | |
| 116 | std::cerr << "should never get here" << std::endl; |
| 117 | } |
| 118 | |
| 119 | |
| 120 | |
| 121 | int main ( int argc, char *argv [] ) { |
| 122 | std::set_terminate ( my_terminate ); |
| 123 | test_exception_in_destructor (); |
| 124 | return 1; // we failed if we get here |
| 125 | } |