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 | |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 13 | #include <cassert> |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 14 | #include <cstdlib> |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 15 | #include <exception> |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 16 | |
| 17 | void my_terminate () { exit ( 0 ); } |
| 18 | |
| 19 | // Wrapper routines |
| 20 | void *my_alloc2 ( size_t sz ) { |
| 21 | void *p = std::malloc ( sz ); |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 22 | // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p ); |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 23 | return p; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 24 | } |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 25 | |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 26 | void my_dealloc2 ( void *p ) { |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 27 | // std::printf ( "Freeing %lx\n", (unsigned long) p ); |
| 28 | std::free ( p ); |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 29 | } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 30 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 31 | void my_dealloc3 ( void *p, size_t ) { |
Louis Dionne | e1c6727 | 2020-04-17 10:06:55 -0400 | [diff] [blame] | 32 | // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz ); |
| 33 | std::free ( p ); |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 34 | } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 35 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 36 | void my_construct ( void *) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 37 | // std::printf ( "Constructing %lx\n", (unsigned long) p ); |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 38 | } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 39 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 40 | void my_destruct ( void *) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 41 | // std::printf ( "Destructing %lx\n", (unsigned long) p ); |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 42 | } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 43 | |
| 44 | int gCounter; |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 45 | void count_construct ( void * ) { ++gCounter; } |
| 46 | void count_destruct ( void * ) { --gCounter; } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 47 | |
| 48 | |
| 49 | int gConstructorCounter; |
| 50 | int gConstructorThrowTarget; |
| 51 | int gDestructorCounter; |
| 52 | int gDestructorThrowTarget; |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 53 | void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; } |
| 54 | void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; } |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 55 | |
| 56 | struct 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 ); } |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 60 | }; |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | // Make sure the constructors and destructors are matched |
| 64 | void test_exception_in_destructor ( ) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 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; |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 73 | } catch ( int i ) { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 74 | |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 77 | assert(false && "should never get here"); |
| 78 | } |
| 79 | |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 80 | |
| 81 | |
Eric Fiselier | a140cba | 2016-12-24 00:37:13 +0000 | [diff] [blame] | 82 | int main () { |
Marshall Clow | 280ddee | 2011-06-10 03:40:19 +0000 | [diff] [blame] | 83 | std::set_terminate ( my_terminate ); |
| 84 | test_exception_in_destructor (); |
| 85 | return 1; // we failed if we get here |
Louis Dionne | cc69d21 | 2020-10-13 15:47:31 -0400 | [diff] [blame] | 86 | } |