Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 1 | //===--------------------------- test_vector2.cpp -------------------------===// |
| 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 +0000 | [diff] [blame] | 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 |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Louis Dionne | 8c61114 | 2020-04-17 10:29:15 -0400 | [diff] [blame] | 9 | // UNSUPPORTED: no-exceptions |
Asiri Rathnayake | 57e446d | 2016-05-31 12:01:32 +0000 | [diff] [blame] | 10 | |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 11 | #include "cxxabi.h" |
| 12 | |
| 13 | #include <iostream> |
| 14 | #include <cstdlib> |
| 15 | |
| 16 | void my_terminate () { exit ( 0 ); } |
| 17 | |
| 18 | // Wrapper routines |
| 19 | void *my_alloc2 ( size_t sz ) { |
| 20 | void *p = std::malloc ( sz ); |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 21 | // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p ); |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 22 | return p; |
| 23 | } |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 24 | |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 25 | void my_dealloc2 ( void *p ) { |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 26 | // std::printf ( "Freeing %lx\n", (unsigned long) p ); |
| 27 | std::free ( p ); |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 30 | void my_dealloc3 ( void *p, size_t ) { |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 31 | // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz ); |
| 32 | std::free ( p ); |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 35 | void my_construct ( void *) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 36 | // std::printf ( "Constructing %lx\n", (unsigned long) p ); |
| 37 | } |
| 38 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 39 | void my_destruct ( void *) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 40 | // std::printf ( "Destructing %lx\n", (unsigned long) p ); |
| 41 | } |
| 42 | |
| 43 | int gCounter; |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 44 | void count_construct ( void * ) { ++gCounter; } |
| 45 | void count_destruct ( void * ) { --gCounter; } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | int gConstructorCounter; |
| 49 | int gConstructorThrowTarget; |
| 50 | int gDestructorCounter; |
| 51 | int gDestructorThrowTarget; |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 52 | void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; } |
| 53 | void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 54 | |
| 55 | struct 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 |
| 63 | void test_exception_in_destructor ( ) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 64 | |
| 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 Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 75 | std::cerr << "should never get here" << std::endl; |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
| 79 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 80 | int main () { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 81 | std::set_terminate ( my_terminate ); |
| 82 | test_exception_in_destructor (); |
| 83 | return 1; // we failed if we get here |
| 84 | } |