2011-06-10 11:40:19 +08:00
|
|
|
//===--------------------------- test_vector2.cpp -------------------------===//
|
|
|
|
//
|
2019-01-19 18:56:40 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-06-10 11:40:19 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-04-17 22:29:15 +08:00
|
|
|
// UNSUPPORTED: no-exceptions
|
2016-05-31 20:01:32 +08:00
|
|
|
|
2011-06-10 11:40:19 +08:00
|
|
|
#include "cxxabi.h"
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <cstdlib>
|
|
|
|
|
|
|
|
void my_terminate () { exit ( 0 ); }
|
|
|
|
|
|
|
|
// Wrapper routines
|
|
|
|
void *my_alloc2 ( size_t sz ) {
|
|
|
|
void *p = std::malloc ( sz );
|
2020-04-17 22:06:55 +08:00
|
|
|
// std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
|
2011-06-10 11:40:19 +08:00
|
|
|
return p;
|
|
|
|
}
|
2020-04-17 22:06:55 +08:00
|
|
|
|
2011-06-10 11:40:19 +08:00
|
|
|
void my_dealloc2 ( void *p ) {
|
2020-04-17 22:06:55 +08:00
|
|
|
// std::printf ( "Freeing %lx\n", (unsigned long) p );
|
|
|
|
std::free ( p );
|
2011-06-10 11:40:19 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 08:37:13 +08:00
|
|
|
void my_dealloc3 ( void *p, size_t ) {
|
2020-04-17 22:06:55 +08:00
|
|
|
// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
|
|
|
|
std::free ( p );
|
2011-06-10 11:40:19 +08:00
|
|
|
}
|
|
|
|
|
2016-12-24 08:37:13 +08:00
|
|
|
void my_construct ( void *) {
|
2011-06-10 11:40:19 +08:00
|
|
|
// std::printf ( "Constructing %lx\n", (unsigned long) p );
|
|
|
|
}
|
|
|
|
|
2016-12-24 08:37:13 +08:00
|
|
|
void my_destruct ( void *) {
|
2011-06-10 11:40:19 +08:00
|
|
|
// std::printf ( "Destructing %lx\n", (unsigned long) p );
|
|
|
|
}
|
|
|
|
|
|
|
|
int gCounter;
|
2016-12-24 08:37:13 +08:00
|
|
|
void count_construct ( void * ) { ++gCounter; }
|
|
|
|
void count_destruct ( void * ) { --gCounter; }
|
2011-06-10 11:40:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
int gConstructorCounter;
|
|
|
|
int gConstructorThrowTarget;
|
|
|
|
int gDestructorCounter;
|
|
|
|
int gDestructorThrowTarget;
|
2016-12-24 08:37:13 +08:00
|
|
|
void throw_construct ( void * ) { if ( gConstructorCounter == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
|
|
|
|
void throw_destruct ( void * ) { if ( ++gDestructorCounter == gDestructorThrowTarget ) throw 2; }
|
2011-06-10 11:40:19 +08:00
|
|
|
|
|
|
|
struct vec_on_stack {
|
|
|
|
void *storage;
|
|
|
|
vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
|
|
|
|
~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Make sure the constructors and destructors are matched
|
|
|
|
void test_exception_in_destructor ( ) {
|
|
|
|
|
|
|
|
// Try throwing from a destructor while unwinding the stack -- should abort
|
|
|
|
gConstructorCounter = gDestructorCounter = 0;
|
|
|
|
gConstructorThrowTarget = -1;
|
|
|
|
gDestructorThrowTarget = 5;
|
|
|
|
try {
|
|
|
|
vec_on_stack v;
|
|
|
|
throw 3;
|
|
|
|
}
|
|
|
|
catch ( int i ) {}
|
|
|
|
|
2020-04-17 22:06:55 +08:00
|
|
|
std::cerr << "should never get here" << std::endl;
|
2011-06-10 11:40:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-12-24 08:37:13 +08:00
|
|
|
int main () {
|
2011-06-10 11:40:19 +08:00
|
|
|
std::set_terminate ( my_terminate );
|
|
|
|
test_exception_in_destructor ();
|
|
|
|
return 1; // we failed if we get here
|
|
|
|
}
|