| 1 |
// This test fails on VxWorks in kernel mode because it depends on the |
| 2 |
// library version of "::operator new[]" calling the "::operator new" |
| 3 |
// defined in this module. This doesn't work because the library version |
| 4 |
// of "::operator new[]" is built into the kernel itself; library relocations |
| 5 |
// are resolved when the kernel is linked. |
| 6 |
// { dg-do run { xfail { powerpc-ibm-aix* || vxworks_kernel } } } |
| 7 |
// { dg-options "-flat_namespace" { target *-*-darwin[67]* } } |
| 8 |
// Avoid use of non-overridable new/delete operators in shared |
| 9 |
// { dg-options "-static" { target *-*-mingw* } } |
| 10 |
// { dg-skip-if "requires hosted libstdc++ for stdlib malloc" { ! hostedlib } } |
| 11 |
|
| 12 |
// Test __cxa_vec routines |
| 13 |
// Copyright (C) 2000, 2005 Free Software Foundation, Inc. |
| 14 |
// Contributed by Nathan Sidwell 7 Apr 2000 <nathan@[email protected]> |
| 15 |
|
| 16 |
#if defined (__GXX_ABI_VERSION) && __GXX_ABI_VERSION >= 100 |
| 17 |
#include <cxxabi.h> |
| 18 |
#include <stdio.h> |
| 19 |
#include <new> |
| 20 |
#include <exception> |
| 21 |
#include <stdlib.h> |
| 22 |
#include <setjmp.h> |
| 23 |
|
| 24 |
static int ctor_count = 0; |
| 25 |
static int dtor_count = 0; |
| 26 |
static bool dtor_repeat = false; |
| 27 |
|
| 28 |
// Allocate enough padding to hold an array cookie. |
| 29 |
#ifdef __ARM_EABI__ |
| 30 |
#define padding 8 |
| 31 |
#else |
| 32 |
#define padding (sizeof (std::size_t)) |
| 33 |
#endif |
| 34 |
|
| 35 |
// our pseudo ctors and dtors |
| 36 |
static abi::__cxa_cdtor_return_type ctor (void *x) |
| 37 |
{ |
| 38 |
if (!ctor_count) |
| 39 |
throw 1; |
| 40 |
ctor_count--; |
| 41 |
#ifdef __ARM_EABI__ |
| 42 |
return x; |
| 43 |
#endif |
| 44 |
} |
| 45 |
|
| 46 |
static abi::__cxa_cdtor_return_type dtor (void *x) |
| 47 |
{ |
| 48 |
if (!dtor_count) |
| 49 |
{ |
| 50 |
if (!dtor_repeat) |
| 51 |
dtor_count--; |
| 52 |
throw 1; |
| 53 |
} |
| 54 |
dtor_count--; |
| 55 |
#ifdef __ARM_EABI__ |
| 56 |
return x; |
| 57 |
#endif |
| 58 |
} |
| 59 |
|
| 60 |
// track new and delete |
| 61 |
static int blocks = 0; |
| 62 |
void *operator new[] (std::size_t size) |
| 63 |
#if __cplusplus <= 199711L |
| 64 |
throw (std::bad_alloc) |
| 65 |
#endif |
| 66 |
{ |
| 67 |
void *ptr = malloc (size); |
| 68 |
|
| 69 |
if (!ptr) |
| 70 |
throw std::bad_alloc (); |
| 71 |
blocks++; |
| 72 |
return ptr; |
| 73 |
} |
| 74 |
|
| 75 |
void operator delete[] (void *ptr) throw () |
| 76 |
{ |
| 77 |
if (ptr) |
| 78 |
{ |
| 79 |
free (ptr); |
| 80 |
blocks--; |
| 81 |
} |
| 82 |
} |
| 83 |
static jmp_buf jump; |
| 84 |
|
| 85 |
// allocate and delete an array with no problems |
| 86 |
void test0 () |
| 87 |
{ |
| 88 |
static bool started = false; |
| 89 |
|
| 90 |
if (!started) |
| 91 |
{ |
| 92 |
started = true; |
| 93 |
std::set_terminate (test0); |
| 94 |
|
| 95 |
ctor_count = dtor_count = 5; |
| 96 |
dtor_repeat = false; |
| 97 |
blocks = 0; |
| 98 |
|
| 99 |
try |
| 100 |
{ |
| 101 |
void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor); |
| 102 |
abi::__cxa_vec_delete (ary, 1, padding, dtor); |
| 103 |
if (ctor_count || dtor_count || blocks) |
| 104 |
longjmp (jump, 1); |
| 105 |
} |
| 106 |
catch (...) |
| 107 |
{ |
| 108 |
longjmp (jump, 2); |
| 109 |
} |
| 110 |
} |
| 111 |
else |
| 112 |
{ |
| 113 |
longjmp (jump, 3); |
| 114 |
} |
| 115 |
return; |
| 116 |
} |
| 117 |
|
| 118 |
// allocate and delete an array with exception on ctor |
| 119 |
void test1 () |
| 120 |
{ |
| 121 |
static bool started = false; |
| 122 |
|
| 123 |
if (!started) |
| 124 |
{ |
| 125 |
started = true; |
| 126 |
std::set_terminate (test1); |
| 127 |
|
| 128 |
ctor_count = dtor_count = 5; |
| 129 |
dtor_repeat = false; |
| 130 |
blocks = 0; |
| 131 |
|
| 132 |
ctor_count = 4; |
| 133 |
try |
| 134 |
{ |
| 135 |
void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor); |
| 136 |
longjmp (jump, 1); |
| 137 |
} |
| 138 |
catch (...) |
| 139 |
{ |
| 140 |
// we expect to get here |
| 141 |
if (ctor_count || dtor_count != 1 || blocks) |
| 142 |
longjmp (jump, 2); |
| 143 |
} |
| 144 |
} |
| 145 |
else |
| 146 |
{ |
| 147 |
longjmp (jump, 3); |
| 148 |
} |
| 149 |
return; |
| 150 |
} |
| 151 |
|
| 152 |
// allocate and delete an array with exception on dtor |
| 153 |
void test2 () |
| 154 |
{ |
| 155 |
static bool started = false; |
| 156 |
|
| 157 |
if (!started) |
| 158 |
{ |
| 159 |
started = true; |
| 160 |
std::set_terminate (test2); |
| 161 |
ctor_count = dtor_count = 5; |
| 162 |
dtor_repeat = false; |
| 163 |
blocks = 0; |
| 164 |
|
| 165 |
dtor_count = 3; |
| 166 |
try |
| 167 |
{ |
| 168 |
void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor); |
| 169 |
abi::__cxa_vec_delete (ary, 1, padding, dtor); |
| 170 |
longjmp (jump, 1); |
| 171 |
} |
| 172 |
catch (...) |
| 173 |
{ |
| 174 |
// we expect to get here |
| 175 |
if (ctor_count || dtor_count != -2u || blocks) |
| 176 |
longjmp (jump, 2); |
| 177 |
} |
| 178 |
} |
| 179 |
else |
| 180 |
{ |
| 181 |
longjmp (jump, 3); |
| 182 |
} |
| 183 |
return; |
| 184 |
} |
| 185 |
|
| 186 |
// allocate an array with double exception on dtor |
| 187 |
void test3 () |
| 188 |
{ |
| 189 |
static bool started = false; |
| 190 |
|
| 191 |
if (!started) |
| 192 |
{ |
| 193 |
started = true; |
| 194 |
std::set_terminate (test3); |
| 195 |
|
| 196 |
ctor_count = dtor_count = 5; |
| 197 |
dtor_repeat = false; |
| 198 |
blocks = 0; |
| 199 |
|
| 200 |
dtor_count = 3; |
| 201 |
dtor_repeat = true; |
| 202 |
try |
| 203 |
{ |
| 204 |
void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor); |
| 205 |
abi::__cxa_vec_delete (ary, 1, padding, dtor); |
| 206 |
longjmp (jump, 1); |
| 207 |
} |
| 208 |
catch (...) |
| 209 |
{ |
| 210 |
// we do not expect to get here |
| 211 |
longjmp (jump, 2); |
| 212 |
} |
| 213 |
} |
| 214 |
else |
| 215 |
{ |
| 216 |
// we expect to get here (via terminate) |
| 217 |
if (ctor_count || dtor_count || blocks != 1) |
| 218 |
longjmp (jump, 3); |
| 219 |
longjmp (jump, -1); |
| 220 |
} |
| 221 |
return; |
| 222 |
} |
| 223 |
|
| 224 |
// allocate an array with exception on ctor and exception in cleanup |
| 225 |
void test4 () |
| 226 |
{ |
| 227 |
static bool started = false; |
| 228 |
|
| 229 |
if (!started) |
| 230 |
{ |
| 231 |
started = true; |
| 232 |
std::set_terminate (test4); |
| 233 |
|
| 234 |
ctor_count = dtor_count = 5; |
| 235 |
dtor_repeat = false; |
| 236 |
blocks = 0; |
| 237 |
|
| 238 |
ctor_count = 3; |
| 239 |
dtor_count = 2; |
| 240 |
try |
| 241 |
{ |
| 242 |
void *ary = abi::__cxa_vec_new (5, 1, padding, ctor, dtor); |
| 243 |
longjmp (jump, 1); |
| 244 |
} |
| 245 |
catch (...) |
| 246 |
{ |
| 247 |
// we do not expect to get here |
| 248 |
longjmp (jump, 2); |
| 249 |
} |
| 250 |
} |
| 251 |
else |
| 252 |
{ |
| 253 |
// we expect to get here (via terminate) |
| 254 |
if (ctor_count || dtor_count != -1u || blocks != 1) |
| 255 |
longjmp (jump, 3); |
| 256 |
longjmp (jump, -1); |
| 257 |
} |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
static void (*tests[])() = |
| 262 |
{ |
| 263 |
test0, |
| 264 |
test1, |
| 265 |
test2, |
| 266 |
test3, |
| 267 |
test4, |
| 268 |
NULL |
| 269 |
}; |
| 270 |
|
| 271 |
int main () |
| 272 |
{ |
| 273 |
int ix; |
| 274 |
int n; |
| 275 |
int errors = 0; |
| 276 |
|
| 277 |
for (ix = 0; tests[ix]; ix++) |
| 278 |
{ |
| 279 |
if (n = setjmp (jump)) |
| 280 |
{ |
| 281 |
if (n > 0) |
| 282 |
{ |
| 283 |
printf ("test %d failed %d\n", ix, n); |
| 284 |
errors++; |
| 285 |
} |
| 286 |
} |
| 287 |
else |
| 288 |
tests[ix] (); |
| 289 |
} |
| 290 |
return errors; |
| 291 |
} |
| 292 |
|
| 293 |
#else |
| 294 |
int main () |
| 295 |
{ |
| 296 |
return 0; |
| 297 |
} |
| 298 |
#endif |