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