test_vector1.pass.cpp
10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//===---------------------------- test_vector.cpp -------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "cxxabi.h"
#include <iostream>
#include <cstdlib>
#include <cassert>
#include "test_macros.h"
// Wrapper routines
void *my_alloc2 ( size_t sz ) {
void *p = std::malloc ( sz );
// std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
return p;
}
void my_dealloc2 ( void *p ) {
// std::printf ( "Freeing %lx\n", (unsigned long) p );
std::free ( p );
}
void my_dealloc3 ( void *p, size_t ) {
// std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
std::free ( p );
}
void my_construct ( void * ) {
// std::printf ( "Constructing %lx\n", (unsigned long) p );
}
void my_destruct ( void * ) {
// std::printf ( "Destructing %lx\n", (unsigned long) p );
}
int gCounter;
void count_construct ( void * ) { ++gCounter; }
void count_destruct ( void * ) { --gCounter; }
int gConstructorCounter;
int gConstructorThrowTarget;
int gDestructorCounter;
int gDestructorThrowTarget;
void throw_construct ( void * ) {
#ifndef TEST_HAS_NO_EXCEPTIONS
if ( gConstructorCounter == gConstructorThrowTarget )
throw 1;
++gConstructorCounter;
#endif
}
void throw_destruct ( void * ) {
#ifndef TEST_HAS_NO_EXCEPTIONS
if ( ++gDestructorCounter == gDestructorThrowTarget )
throw 2;
#endif
}
#if __cplusplus >= 201103L
# define CAN_THROW noexcept(false)
#else
# define CAN_THROW
#endif
struct vec_on_stack {
void *storage;
vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct )) {}
~vec_on_stack () CAN_THROW {__cxxabiv1::__cxa_vec_delete ( storage, 40, 8, throw_destruct ); }
};
// Test calls with empty constructors and destructors
int test_empty ( ) {
void *one, *two, *three;
// Try with no padding and no con/destructors
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, NULL, NULL );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, NULL, NULL, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, NULL, NULL, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 0, NULL );
__cxxabiv1::__cxa_vec_delete2( two, 40, 0, NULL, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 0, NULL, my_dealloc3 );
// Try with no padding
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, my_construct, my_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, my_construct, my_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, my_construct, my_destruct, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 0, my_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 0, my_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 0, my_destruct, my_dealloc3 );
// Padding and no con/destructors
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, NULL, NULL );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, NULL, NULL, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, NULL, NULL, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 8, NULL );
__cxxabiv1::__cxa_vec_delete2( two, 40, 8, NULL, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 8, NULL, my_dealloc3 );
// Padding with con/destructors
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, my_construct, my_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, my_construct, my_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, my_construct, my_destruct, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 8, my_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 8, my_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 8, my_destruct, my_dealloc3 );
return 0;
}
// Make sure the constructors and destructors are matched
int test_counted ( ) {
int retVal = 0;
void *one, *two, *three;
// Try with no padding
gCounter = 0;
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, count_construct, count_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, count_construct, count_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, count_construct, count_destruct, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 0, count_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 0, count_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 0, count_destruct, my_dealloc3 );
// Since there was no padding, the # of elements in the array are not stored
// and the destructors are not called.
if ( gCounter != 30 ) {
std::cerr << "Mismatched Constructor/Destructor calls (1)" << std::endl;
std::cerr << " Expected 30, got " << gCounter << std::endl;
retVal = 1;
}
gCounter = 0;
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, count_construct, count_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, count_construct, count_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, count_construct, count_destruct, my_alloc2, my_dealloc3 );
__cxxabiv1::__cxa_vec_delete ( one, 40, 8, count_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 8, count_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 8, count_destruct, my_dealloc3 );
if ( gCounter != 0 ) {
std::cerr << "Mismatched Constructor/Destructor calls (2)" << std::endl;
std::cerr << " Expected 0, got " << gCounter << std::endl;
retVal = 1;
}
return retVal;
}
#ifndef TEST_HAS_NO_EXCEPTIONS
// Make sure the constructors and destructors are matched
int test_exception_in_constructor ( ) {
int retVal = 0;
void *one, *two, *three;
// Try with no padding
gConstructorCounter = gDestructorCounter = 0;
gConstructorThrowTarget = 15;
gDestructorThrowTarget = -1;
try {
one = two = three = NULL;
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 0, throw_construct, throw_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 0, throw_construct, throw_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 0, throw_construct, throw_destruct, my_alloc2, my_dealloc3 );
}
catch ( int i ) {}
__cxxabiv1::__cxa_vec_delete ( one, 40, 0, throw_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 0, throw_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 0, throw_destruct, my_dealloc3 );
// Since there was no padding, the # of elements in the array are not stored
// and the destructors are not called.
// Since we threw after 15 calls to the constructor, we should see 5 calls to
// the destructor from the partially constructed array.
if ( gConstructorCounter - gDestructorCounter != 10 ) {
std::cerr << "Mismatched Constructor/Destructor calls (1C)" << std::endl;
std::cerr << gConstructorCounter << " constructors, but " <<
gDestructorCounter << " destructors" << std::endl;
retVal = 1;
}
gConstructorCounter = gDestructorCounter = 0;
gConstructorThrowTarget = 15;
gDestructorThrowTarget = -1;
try {
one = two = three = NULL;
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc2 );
three = __cxxabiv1::__cxa_vec_new3( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc3 );
}
catch ( int i ) {}
__cxxabiv1::__cxa_vec_delete ( one, 40, 8, throw_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 8, throw_destruct, my_dealloc2 );
__cxxabiv1::__cxa_vec_delete3( three, 40, 8, throw_destruct, my_dealloc3 );
if ( gConstructorCounter != gDestructorCounter ) {
std::cerr << "Mismatched Constructor/Destructor calls (2C)" << std::endl;
std::cerr << gConstructorCounter << " constructors, but " <<
gDestructorCounter << " destructors" << std::endl;
retVal = 1;
}
return retVal;
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
// Make sure the constructors and destructors are matched
int test_exception_in_destructor ( ) {
int retVal = 0;
void *one, *two, *three;
one = two = three = NULL;
// Throw from within a destructor
gConstructorCounter = gDestructorCounter = 0;
gConstructorThrowTarget = -1;
gDestructorThrowTarget = 15;
try {
one = two = NULL;
one = __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct, throw_destruct );
two = __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct, throw_destruct, my_alloc2, my_dealloc2 );
}
catch ( int i ) {}
try {
__cxxabiv1::__cxa_vec_delete ( one, 40, 8, throw_destruct );
__cxxabiv1::__cxa_vec_delete2( two, 40, 8, throw_destruct, my_dealloc2 );
assert(false);
}
catch ( int i ) {}
// We should have thrown in the middle of cleaning up "two", which means that
// there should be 20 calls to the destructor and the try block should exit
// before the assertion.
if ( gConstructorCounter != 20 || gDestructorCounter != 20 ) {
std::cerr << "Unexpected Constructor/Destructor calls (1D)" << std::endl;
std::cerr << "Expected (20, 20), but got (" << gConstructorCounter << ", " <<
gDestructorCounter << ")" << std::endl;
retVal = 1;
}
// Try throwing from a destructor - should be fine.
gConstructorCounter = gDestructorCounter = 0;
gConstructorThrowTarget = -1;
gDestructorThrowTarget = 5;
try { vec_on_stack v; }
catch ( int i ) {}
if ( gConstructorCounter != gDestructorCounter ) {
std::cerr << "Mismatched Constructor/Destructor calls (2D)" << std::endl;
std::cerr << gConstructorCounter << " constructors, but " <<
gDestructorCounter << " destructors" << std::endl;
retVal = 1;
}
return retVal;
}
#endif
int main () {
int retVal = 0;
retVal += test_empty ();
retVal += test_counted ();
#ifndef TEST_HAS_NO_EXCEPTIONS
retVal += test_exception_in_constructor ();
retVal += test_exception_in_destructor ();
#endif
return retVal;
}