1 //===----------------------------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // Reports leaks after https://github.com/llvm/llvm-project/pull/66285
12 // UNSUPPORTED: no-exceptions
20 void my_terminate () { exit ( 0 ); }
23 void *my_alloc2 ( size_t sz
) {
24 void *p
= std::malloc ( sz
);
25 // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
29 void my_dealloc2 ( void *p
) {
30 // std::printf ( "Freeing %lx\n", (unsigned long) p );
34 void my_dealloc3 ( void *p
, size_t ) {
35 // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
39 void my_construct ( void *) {
40 // std::printf ( "Constructing %lx\n", (unsigned long) p );
43 void my_destruct ( void *) {
44 // std::printf ( "Destructing %lx\n", (unsigned long) p );
48 void count_construct ( void * ) { ++gCounter
; }
49 void count_destruct ( void * ) { --gCounter
; }
52 int gConstructorCounter
;
53 int gConstructorThrowTarget
;
54 int gDestructorCounter
;
55 int gDestructorThrowTarget
;
56 void throw_construct ( void * ) { if ( gConstructorCounter
== gConstructorThrowTarget
) throw 1; ++gConstructorCounter
; }
57 void throw_destruct ( void * ) { if ( ++gDestructorCounter
== gDestructorThrowTarget
) throw 2; }
61 vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct
, throw_destruct
)) {}
62 ~vec_on_stack () { __cxxabiv1::__cxa_vec_delete ( storage
, 40, 8, throw_destruct
); }
66 // Make sure the constructors and destructors are matched
67 void test_exception_in_destructor ( ) {
69 // Try throwing from a destructor while unwinding the stack -- should abort
70 gConstructorCounter
= gDestructorCounter
= 0;
71 gConstructorThrowTarget
= -1;
72 gDestructorThrowTarget
= 5;
80 assert(false && "should never get here");
86 std::set_terminate ( my_terminate
);
87 test_exception_in_destructor ();
88 return 1; // we failed if we get here