1*eb8650a7SLouis Dionne //===----------------------------------------------------------------------===//
2e434b34fSJonathan Roelofs //
357b08b09SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
457b08b09SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
557b08b09SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e434b34fSJonathan Roelofs //
7e434b34fSJonathan Roelofs //===----------------------------------------------------------------------===//
8e434b34fSJonathan Roelofs 
98c61114cSLouis Dionne // UNSUPPORTED: no-exceptions
1057e446daSAsiri Rathnayake 
11e434b34fSJonathan Roelofs #include "cxxabi.h"
12e434b34fSJonathan Roelofs 
13cc69d211SLouis Dionne #include <cassert>
14e434b34fSJonathan Roelofs #include <cstdlib>
15cc69d211SLouis Dionne #include <exception>
16e434b34fSJonathan Roelofs 
my_terminate()17e434b34fSJonathan Roelofs void my_terminate () { exit ( 0 ); }
18e434b34fSJonathan Roelofs 
19e434b34fSJonathan Roelofs //  Wrapper routines
my_alloc2(size_t sz)20e434b34fSJonathan Roelofs void *my_alloc2 ( size_t sz ) {
21e434b34fSJonathan Roelofs     void *p = std::malloc ( sz );
22e434b34fSJonathan Roelofs //  std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
23e434b34fSJonathan Roelofs     return p;
24e434b34fSJonathan Roelofs }
25e434b34fSJonathan Roelofs 
my_dealloc2(void * p)26e434b34fSJonathan Roelofs void my_dealloc2 ( void *p ) {
27e434b34fSJonathan Roelofs //  std::printf ( "Freeing %lx\n", (unsigned long) p );
28e434b34fSJonathan Roelofs     std::free ( p );
29e434b34fSJonathan Roelofs }
30e434b34fSJonathan Roelofs 
my_dealloc3(void * p,size_t)31a140cba7SEric Fiselier void my_dealloc3 ( void *p, size_t ) {
32e434b34fSJonathan Roelofs //  std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
33e434b34fSJonathan Roelofs     std::free ( p );
34e434b34fSJonathan Roelofs }
35e434b34fSJonathan Roelofs 
my_construct(void *)36a140cba7SEric Fiselier void my_construct ( void *) {
37e434b34fSJonathan Roelofs //  std::printf ( "Constructing %lx\n", (unsigned long) p );
38e434b34fSJonathan Roelofs }
39e434b34fSJonathan Roelofs 
my_destruct(void *)40a140cba7SEric Fiselier void my_destruct  ( void *) {
41e434b34fSJonathan Roelofs //  std::printf ( "Destructing  %lx\n", (unsigned long) p );
42e434b34fSJonathan Roelofs }
43e434b34fSJonathan Roelofs 
44e434b34fSJonathan Roelofs int gCounter;
count_construct(void *)45a140cba7SEric Fiselier void count_construct ( void * ) { ++gCounter; }
count_destruct(void *)46a140cba7SEric Fiselier void count_destruct  ( void * ) { --gCounter; }
47e434b34fSJonathan Roelofs 
48e434b34fSJonathan Roelofs 
49e434b34fSJonathan Roelofs int gConstructorCounter;
50e434b34fSJonathan Roelofs int gConstructorThrowTarget;
51e434b34fSJonathan Roelofs int gDestructorCounter;
52e434b34fSJonathan Roelofs int gDestructorThrowTarget;
throw_construct(void *)53a140cba7SEric Fiselier void throw_construct ( void * ) { if ( gConstructorCounter   == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
throw_destruct(void *)54a140cba7SEric Fiselier void throw_destruct  ( void * ) { if ( ++gDestructorCounter  == gDestructorThrowTarget  ) throw 2; }
55e434b34fSJonathan Roelofs 
56e434b34fSJonathan Roelofs struct vec_on_stack {
57e434b34fSJonathan Roelofs     void *storage;
vec_on_stackvec_on_stack58e434b34fSJonathan Roelofs     vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new    (            10, 40, 8, throw_construct, throw_destruct )) {}
~vec_on_stackvec_on_stack59e434b34fSJonathan Roelofs     ~vec_on_stack () {          __cxxabiv1::__cxa_vec_delete ( storage,       40, 8,                  throw_destruct );  }
60e434b34fSJonathan Roelofs };
61e434b34fSJonathan Roelofs 
62e434b34fSJonathan Roelofs 
63e434b34fSJonathan Roelofs //  Make sure the constructors and destructors are matched
test_exception_in_destructor()64e434b34fSJonathan Roelofs void test_exception_in_destructor ( ) {
65e434b34fSJonathan Roelofs 
66e434b34fSJonathan Roelofs //  Try throwing from a destructor while unwinding the stack -- should abort
67e434b34fSJonathan Roelofs     gConstructorCounter = gDestructorCounter = 0;
68e434b34fSJonathan Roelofs     gConstructorThrowTarget = -1;
69e434b34fSJonathan Roelofs     gDestructorThrowTarget  = 5;
70e434b34fSJonathan Roelofs     try {
71e434b34fSJonathan Roelofs         vec_on_stack v;
72e434b34fSJonathan Roelofs         throw 3;
73cc69d211SLouis Dionne     } catch ( int i ) {
74e434b34fSJonathan Roelofs 
75cc69d211SLouis Dionne     }
76cc69d211SLouis Dionne 
77cc69d211SLouis Dionne     assert(false && "should never get here");
78e434b34fSJonathan Roelofs }
79e434b34fSJonathan Roelofs 
80e434b34fSJonathan Roelofs 
81e434b34fSJonathan Roelofs 
main()82a140cba7SEric Fiselier int main () {
83e434b34fSJonathan Roelofs     std::set_terminate ( my_terminate );
84e434b34fSJonathan Roelofs     test_exception_in_destructor ();
85e434b34fSJonathan Roelofs     return 1;       // we failed if we get here
86e434b34fSJonathan Roelofs }
87