blob: dd09d2a8848f58a5bdf2d01d39ff4639acc00f88 [file] [log] [blame]
Marshall Clow280ddee2011-06-10 03:40:19 +00001//===--------------------------- test_vector2.cpp -------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:40 +00003// 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 Clow280ddee2011-06-10 03:40:19 +00006//
7//===----------------------------------------------------------------------===//
8
Louis Dionne8c611142020-04-17 10:29:15 -04009// UNSUPPORTED: no-exceptions
Asiri Rathnayake57e446d2016-05-31 12:01:32 +000010
Marshall Clow280ddee2011-06-10 03:40:19 +000011#include "cxxabi.h"
12
Louis Dionnecc69d212020-10-13 15:47:31 -040013#include <cassert>
Marshall Clow280ddee2011-06-10 03:40:19 +000014#include <cstdlib>
Louis Dionnecc69d212020-10-13 15:47:31 -040015#include <exception>
Marshall Clow280ddee2011-06-10 03:40:19 +000016
17void my_terminate () { exit ( 0 ); }
18
19// Wrapper routines
20void *my_alloc2 ( size_t sz ) {
21 void *p = std::malloc ( sz );
Louis Dionnee1c67272020-04-17 10:06:55 -040022// std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
Marshall Clow280ddee2011-06-10 03:40:19 +000023 return p;
Louis Dionnecc69d212020-10-13 15:47:31 -040024}
Louis Dionnee1c67272020-04-17 10:06:55 -040025
Marshall Clow280ddee2011-06-10 03:40:19 +000026void my_dealloc2 ( void *p ) {
Louis Dionnee1c67272020-04-17 10:06:55 -040027// std::printf ( "Freeing %lx\n", (unsigned long) p );
28 std::free ( p );
Louis Dionnecc69d212020-10-13 15:47:31 -040029}
Marshall Clow280ddee2011-06-10 03:40:19 +000030
Eric Fiseliera140cba2016-12-24 00:37:13 +000031void my_dealloc3 ( void *p, size_t ) {
Louis Dionnee1c67272020-04-17 10:06:55 -040032// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
33 std::free ( p );
Louis Dionnecc69d212020-10-13 15:47:31 -040034}
Marshall Clow280ddee2011-06-10 03:40:19 +000035
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 );
Louis Dionnecc69d212020-10-13 15:47:31 -040038}
Marshall Clow280ddee2011-06-10 03:40:19 +000039
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 );
Louis Dionnecc69d212020-10-13 15:47:31 -040042}
Marshall Clow280ddee2011-06-10 03:40:19 +000043
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 ); }
Louis Dionnecc69d212020-10-13 15:47:31 -040060};
Marshall Clow280ddee2011-06-10 03:40:19 +000061
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;
Louis Dionnecc69d212020-10-13 15:47:31 -040073 } catch ( int i ) {
Marshall Clow280ddee2011-06-10 03:40:19 +000074
Marshall Clow280ddee2011-06-10 03:40:19 +000075 }
76
Louis Dionnecc69d212020-10-13 15:47:31 -040077 assert(false && "should never get here");
78}
79
Marshall Clow280ddee2011-06-10 03:40:19 +000080
81
Eric Fiseliera140cba2016-12-24 00:37:13 +000082int main () {
Marshall Clow280ddee2011-06-10 03:40:19 +000083 std::set_terminate ( my_terminate );
84 test_exception_in_destructor ();
85 return 1; // we failed if we get here
Louis Dionnecc69d212020-10-13 15:47:31 -040086}