1//===----------------------------------------------------------------------===//
2//
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
6//
7//
8// This file implements the "Array Construction and Destruction APIs"
9// https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-ctor
10//
11//===----------------------------------------------------------------------===//
12
13#include "cxxabi.h"
14#include "__cxxabi_config.h"
15
16#include <exception> // for std::terminate
17#include <new> // for std::bad_array_new_length
18
19#include "abort_message.h"
20
21#ifndef __has_builtin
22#define __has_builtin(x) 0
23#endif
24
25namespace __cxxabiv1 {
26
27//
28// Helper routines and classes
29//
30
31namespace {
32 inline static size_t __get_element_count ( void *p ) {
33 return static_cast <size_t *> (p)[-1];
34 }
35
36 inline static void __set_element_count ( void *p, size_t element_count ) {
37 static_cast <size_t *> (p)[-1] = element_count;
38 }
39
40
41// A pair of classes to simplify exception handling and control flow.
42// They get passed a block of memory in the constructor, and unless the
43// 'release' method is called, they deallocate the memory in the destructor.
44// Preferred usage is to allocate some memory, attach it to one of these objects,
45// and then, when all the operations to set up the memory block have succeeded,
46// call 'release'. If any of the setup operations fail, or an exception is
47// thrown, then the block is automatically deallocated.
48//
49// The only difference between these two classes is the signature for the
50// deallocation function (to match new2/new3 and delete2/delete3.
51 class st_heap_block2 {
52 public:
53 typedef void (*dealloc_f)(void *);
54
55 st_heap_block2 ( dealloc_f dealloc, void *ptr )
56 : dealloc_ ( dealloc ), ptr_ ( ptr ), enabled_ ( true ) {}
57 ~st_heap_block2 () { if ( enabled_ ) dealloc_ ( ptr_ ) ; }
58 void release () { enabled_ = false; }
59
60 private:
61 dealloc_f dealloc_;
62 void *ptr_;
63 bool enabled_;
64 };
65
66 class st_heap_block3 {
67 public:
68 typedef void (*dealloc_f)(void *, size_t);
69
70 st_heap_block3 ( dealloc_f dealloc, void *ptr, size_t size )
71 : dealloc_ ( dealloc ), ptr_ ( ptr ), size_ ( size ), enabled_ ( true ) {}
72 ~st_heap_block3 () { if ( enabled_ ) dealloc_ ( ptr_, size_ ) ; }
73 void release () { enabled_ = false; }
74
75 private:
76 dealloc_f dealloc_;
77 void *ptr_;
78 size_t size_;
79 bool enabled_;
80 };
81
82 class st_cxa_cleanup {
83 public:
84 typedef void (*destruct_f)(void *);
85
86 st_cxa_cleanup ( void *ptr, size_t &idx, size_t element_size, destruct_f destructor )
87 : ptr_ ( ptr ), idx_ ( idx ), element_size_ ( element_size ),
88 destructor_ ( destructor ), enabled_ ( true ) {}
89 ~st_cxa_cleanup () {
90 if ( enabled_ )
91 __cxa_vec_cleanup ( array_address: ptr_, element_count: idx_, element_size: element_size_, destructor: destructor_ );
92 }
93
94 void release () { enabled_ = false; }
95
96 private:
97 void *ptr_;
98 size_t &idx_;
99 size_t element_size_;
100 destruct_f destructor_;
101 bool enabled_;
102 };
103
104 class st_terminate {
105 public:
106 st_terminate ( bool enabled = true ) : enabled_ ( enabled ) {}
107 ~st_terminate () { if ( enabled_ ) std::terminate (); }
108 void release () { enabled_ = false; }
109 private:
110 bool enabled_ ;
111 };
112}
113
114//
115// Externally visible routines
116//
117
118namespace {
119[[noreturn]] void throw_bad_array_new_length() {
120#ifndef _LIBCXXABI_NO_EXCEPTIONS
121 throw std::bad_array_new_length();
122#else
123 __abort_message("__cxa_vec_new failed to allocate memory");
124#endif
125}
126
127bool mul_overflow(size_t x, size_t y, size_t *res) {
128#if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_mul_overflow)) \
129 || defined(_LIBCXXABI_COMPILER_GCC)
130 return __builtin_mul_overflow(x, y, res);
131#else
132 *res = x * y;
133 return x && ((*res / x) != y);
134#endif
135}
136
137bool add_overflow(size_t x, size_t y, size_t *res) {
138#if (defined(_LIBCXXABI_COMPILER_CLANG) && __has_builtin(__builtin_add_overflow)) \
139 || defined(_LIBCXXABI_COMPILER_GCC)
140 return __builtin_add_overflow(x, y, res);
141#else
142 *res = x + y;
143 return *res < y;
144#endif
145}
146
147size_t calculate_allocation_size_or_throw(size_t element_count,
148 size_t element_size,
149 size_t padding_size) {
150 size_t element_heap_size;
151 if (mul_overflow(x: element_count, y: element_size, res: &element_heap_size))
152 throw_bad_array_new_length();
153
154 size_t allocation_size;
155 if (add_overflow(x: element_heap_size, y: padding_size, res: &allocation_size))
156 throw_bad_array_new_length();
157
158 return allocation_size;
159}
160
161} // namespace
162
163extern "C" {
164
165// Equivalent to
166//
167// __cxa_vec_new2(element_count, element_size, padding_size, constructor,
168// destructor, &::operator new[], &::operator delete[])
169_LIBCXXABI_FUNC_VIS void *
170__cxa_vec_new(size_t element_count, size_t element_size, size_t padding_size,
171 void (*constructor)(void *), void (*destructor)(void *)) {
172 return __cxa_vec_new2 ( element_count, element_size, padding_size,
173 constructor, destructor, alloc: &::operator new [], dealloc: &::operator delete [] );
174}
175
176
177// Given the number and size of elements for an array and the non-negative
178// size of prefix padding for a cookie, allocate space (using alloc) for
179// the array preceded by the specified padding, initialize the cookie if
180// the padding is non-zero, and call the given constructor on each element.
181// Return the address of the array proper, after the padding.
182//
183// If alloc throws an exception, rethrow the exception. If alloc returns
184// NULL, return NULL. If the constructor throws an exception, call
185// destructor for any already constructed elements, and rethrow the
186// exception. If the destructor throws an exception, call std::terminate.
187//
188// The constructor may be NULL, in which case it must not be called. If the
189// padding_size is zero, the destructor may be NULL; in that case it must
190// not be called.
191//
192// Neither alloc nor dealloc may be NULL.
193_LIBCXXABI_FUNC_VIS void *
194__cxa_vec_new2(size_t element_count, size_t element_size, size_t padding_size,
195 void (*constructor)(void *), void (*destructor)(void *),
196 void *(*alloc)(size_t), void (*dealloc)(void *)) {
197 const size_t heap_size = calculate_allocation_size_or_throw(
198 element_count, element_size, padding_size);
199 char* const heap_block = static_cast<char*>(alloc(heap_size));
200 char* vec_base = heap_block;
201
202 if (NULL != vec_base) {
203 st_heap_block2 heap(dealloc, heap_block);
204
205 // put the padding before the array elements
206 if ( 0 != padding_size ) {
207 vec_base += padding_size;
208 __set_element_count ( p: vec_base, element_count );
209 }
210
211 // Construct the elements
212 __cxa_vec_ctor ( array_address: vec_base, element_count, element_size, constructor, destructor );
213 heap.release (); // We're good!
214 }
215
216 return vec_base;
217}
218
219
220// Same as __cxa_vec_new2 except that the deallocation function takes both
221// the object address and its size.
222_LIBCXXABI_FUNC_VIS void *
223__cxa_vec_new3(size_t element_count, size_t element_size, size_t padding_size,
224 void (*constructor)(void *), void (*destructor)(void *),
225 void *(*alloc)(size_t), void (*dealloc)(void *, size_t)) {
226 const size_t heap_size = calculate_allocation_size_or_throw(
227 element_count, element_size, padding_size);
228 char* const heap_block = static_cast<char*>(alloc(heap_size));
229 char* vec_base = heap_block;
230
231 if (NULL != vec_base) {
232 st_heap_block3 heap(dealloc, heap_block, heap_size);
233
234 // put the padding before the array elements
235 if ( 0 != padding_size ) {
236 vec_base += padding_size;
237 __set_element_count ( p: vec_base, element_count );
238 }
239
240 // Construct the elements
241 __cxa_vec_ctor ( array_address: vec_base, element_count, element_size, constructor, destructor );
242 heap.release (); // We're good!
243 }
244
245 return vec_base;
246}
247
248
249// Given the (data) addresses of a destination and a source array, an
250// element count and an element size, call the given copy constructor to
251// copy each element from the source array to the destination array. The
252// copy constructor's arguments are the destination address and source
253// address, respectively. If an exception occurs, call the given destructor
254// (if non-NULL) on each copied element and rethrow. If the destructor
255// throws an exception, call terminate(). The constructor and or destructor
256// pointers may be NULL. If either is NULL, no action is taken when it
257// would have been called.
258
259_LIBCXXABI_FUNC_VIS void __cxa_vec_cctor(void *dest_array, void *src_array,
260 size_t element_count,
261 size_t element_size,
262 void (*constructor)(void *, void *),
263 void (*destructor)(void *)) {
264 if ( NULL != constructor ) {
265 size_t idx = 0;
266 char *src_ptr = static_cast<char *>(src_array);
267 char *dest_ptr = static_cast<char *>(dest_array);
268 st_cxa_cleanup cleanup ( dest_array, idx, element_size, destructor );
269
270 for ( idx = 0; idx < element_count;
271 ++idx, src_ptr += element_size, dest_ptr += element_size )
272 constructor ( dest_ptr, src_ptr );
273 cleanup.release (); // We're good!
274 }
275}
276
277
278// Given the (data) address of an array, not including any cookie padding,
279// and the number and size of its elements, call the given constructor on
280// each element. If the constructor throws an exception, call the given
281// destructor for any already-constructed elements, and rethrow the
282// exception. If the destructor throws an exception, call terminate(). The
283// constructor and/or destructor pointers may be NULL. If either is NULL,
284// no action is taken when it would have been called.
285_LIBCXXABI_FUNC_VIS void
286__cxa_vec_ctor(void *array_address, size_t element_count, size_t element_size,
287 void (*constructor)(void *), void (*destructor)(void *)) {
288 if ( NULL != constructor ) {
289 size_t idx;
290 char *ptr = static_cast <char *> ( array_address );
291 st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );
292
293 // Construct the elements
294 for ( idx = 0; idx < element_count; ++idx, ptr += element_size )
295 constructor ( ptr );
296 cleanup.release (); // We're good!
297 }
298}
299
300// Given the (data) address of an array, the number of elements, and the
301// size of its elements, call the given destructor on each element. If the
302// destructor throws an exception, rethrow after destroying the remaining
303// elements if possible. If the destructor throws a second exception, call
304// terminate(). The destructor pointer may be NULL, in which case this
305// routine does nothing.
306_LIBCXXABI_FUNC_VIS void __cxa_vec_dtor(void *array_address,
307 size_t element_count,
308 size_t element_size,
309 void (*destructor)(void *)) {
310 if ( NULL != destructor ) {
311 char *ptr = static_cast <char *> (array_address);
312 size_t idx = element_count;
313 st_cxa_cleanup cleanup ( array_address, idx, element_size, destructor );
314 {
315 st_terminate exception_guard (__cxa_uncaught_exception ());
316 ptr += element_count * element_size; // one past the last element
317
318 while ( idx-- > 0 ) {
319 ptr -= element_size;
320 destructor ( ptr );
321 }
322 exception_guard.release (); // We're good !
323 }
324 cleanup.release (); // We're still good!
325 }
326}
327
328// Given the (data) address of an array, the number of elements, and the
329// size of its elements, call the given destructor on each element. If the
330// destructor throws an exception, call terminate(). The destructor pointer
331// may be NULL, in which case this routine does nothing.
332_LIBCXXABI_FUNC_VIS void __cxa_vec_cleanup(void *array_address,
333 size_t element_count,
334 size_t element_size,
335 void (*destructor)(void *)) {
336 if ( NULL != destructor ) {
337 char *ptr = static_cast <char *> (array_address);
338 size_t idx = element_count;
339 st_terminate exception_guard;
340
341 ptr += element_count * element_size; // one past the last element
342 while ( idx-- > 0 ) {
343 ptr -= element_size;
344 destructor ( ptr );
345 }
346 exception_guard.release (); // We're done!
347 }
348}
349
350
351// If the array_address is NULL, return immediately. Otherwise, given the
352// (data) address of an array, the non-negative size of prefix padding for
353// the cookie, and the size of its elements, call the given destructor on
354// each element, using the cookie to determine the number of elements, and
355// then delete the space by calling ::operator delete[](void *). If the
356// destructor throws an exception, rethrow after (a) destroying the
357// remaining elements, and (b) deallocating the storage. If the destructor
358// throws a second exception, call terminate(). If padding_size is 0, the
359// destructor pointer must be NULL. If the destructor pointer is NULL, no
360// destructor call is to be made.
361//
362// The intent of this function is to permit an implementation to call this
363// function when confronted with an expression of the form delete[] p in
364// the source code, provided that the default deallocation function can be
365// used. Therefore, the semantics of this function are consistent with
366// those required by the standard. The requirement that the deallocation
367// function be called even if the destructor throws an exception derives
368// from the resolution to DR 353 to the C++ standard, which was adopted in
369// April, 2003.
370_LIBCXXABI_FUNC_VIS void __cxa_vec_delete(void *array_address,
371 size_t element_size,
372 size_t padding_size,
373 void (*destructor)(void *)) {
374 __cxa_vec_delete2 ( array_address, element_size, padding_size,
375 destructor, dealloc: &::operator delete [] );
376}
377
378// Same as __cxa_vec_delete, except that the given function is used for
379// deallocation instead of the default delete function. If dealloc throws
380// an exception, the result is undefined. The dealloc pointer may not be
381// NULL.
382_LIBCXXABI_FUNC_VIS void
383__cxa_vec_delete2(void *array_address, size_t element_size, size_t padding_size,
384 void (*destructor)(void *), void (*dealloc)(void *)) {
385 if ( NULL != array_address ) {
386 char *vec_base = static_cast <char *> (array_address);
387 char *heap_block = vec_base - padding_size;
388 st_heap_block2 heap ( dealloc, heap_block );
389
390 if ( 0 != padding_size && NULL != destructor ) // call the destructors
391 __cxa_vec_dtor ( array_address, element_count: __get_element_count ( p: vec_base ),
392 element_size, destructor );
393 }
394}
395
396
397// Same as __cxa_vec_delete, except that the given function is used for
398// deallocation instead of the default delete function. The deallocation
399// function takes both the object address and its size. If dealloc throws
400// an exception, the result is undefined. The dealloc pointer may not be
401// NULL.
402_LIBCXXABI_FUNC_VIS void
403__cxa_vec_delete3(void *array_address, size_t element_size, size_t padding_size,
404 void (*destructor)(void *), void (*dealloc)(void *, size_t)) {
405 if ( NULL != array_address ) {
406 char *vec_base = static_cast <char *> (array_address);
407 char *heap_block = vec_base - padding_size;
408 const size_t element_count = padding_size ? __get_element_count ( p: vec_base ) : 0;
409 const size_t heap_block_size = element_size * element_count + padding_size;
410 st_heap_block3 heap ( dealloc, heap_block, heap_block_size );
411
412 if ( 0 != padding_size && NULL != destructor ) // call the destructors
413 __cxa_vec_dtor ( array_address, element_count, element_size, destructor );
414 }
415}
416
417
418} // extern "C"
419
420} // abi
421