From 54f5f3d969e765e541b0e7fd062dd573e41d3c5c Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:37 +0000 Subject: [PATCH 1/2] Free over-aligned array new storage correctly on exception (non-IA-64) [GH #552] With the non-IA-64 ABI, new[] of an over-aligned type uses the alignment as the size of the prefix, but the cleanup for a throwing brace or list initialization freed p - __array_new_prefix_size: a bad free and a leak. Pass the actual prefix size to the cleanup. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 22 ++- .../gh-552-noia64-aligned-array-new-eh.txt | 11 ++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../array-new-aligned-throw-delete.sft.cpp | 130 ++++++++++++++++++ 5 files changed, 164 insertions(+), 1 deletion(-) create mode 100644 src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt create mode 100644 tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.2.txt create mode 100644 tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..74eb3b28df 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -115,6 +115,7 @@ static void turn_off_freeing_of_storage_on_exception( an_init_pos_descr_ptr ipdp, an_expr_node_ptr delete_args, a_routine_ptr new_routine, + an_expr_node_ptr alloc_prefix_size, an_expr_node_ptr init_expr, an_insert_location *insert_location); static void insert_pending_stmk_init_statements_at_mark( @@ -11902,6 +11903,7 @@ arrays with class elements. a_boolean zero_storage = FALSE; a_boolean needs_dynamic_initialization = FALSE; a_boolean aligned_delete = FALSE; + an_expr_node_ptr aligned_prefix_size_node = NULL; #if ABI_CHANGES_FOR_PLACEMENT_DELETE a_boolean sized_delete = FALSE; an_expr_node_ptr prefix_size_node = NULL; @@ -11997,6 +11999,12 @@ arrays with class elements. /*vars_can_change=*/FALSE); prefix_size_node = add_cast_if_necessary(prefix_size_node, size_node->type); + /* get_prefix_size_node does not account for the alignment, so + the code that frees the storage on an exception must use the + same prefix size. */ + aligned_prefix_size_node = make_reusable_copy( + prefix_size_node, + /*vars_can_change=*/FALSE); } else #endif /* !IA64_ABI */ /* Do not insert code here. */ @@ -12234,6 +12242,7 @@ arrays with class elements. exception. */ turn_off_freeing_of_storage_on_exception(ndsp, &ipd, delete_args, new_routine, + aligned_prefix_size_node, init_insert_location.variant.expr, &insert_location); /* Insert the value of the temporary as the final value of the @@ -12426,6 +12435,7 @@ static void turn_off_freeing_of_storage_on_exception( an_init_pos_descr_ptr ipdp, an_expr_node_ptr delete_args, a_routine_ptr new_routine, + an_expr_node_ptr alloc_prefix_size, an_expr_node_ptr init_expr, an_insert_location *insert_location) /* @@ -12436,6 +12446,10 @@ set_up_freeing_of_storage_on_exception. ipdp describes the location of the allocated storage. delete_args points to the list of arguments for a deallocation call, if one is needed. If new_routine is non-NULL, it is the placement new routine that is being called to allocate the memory. +If alloc_prefix_size is non-NULL, it is a reusable expression for the size +of the array prefix that was used when the storage was allocated (e.g., the +alignment for an aligned array new in the Cfront ABI); otherwise, the +prefix size is determined from the array element type. In the case of an initialized array (e.g., "new A[4] {1, 2}"), the caller has generated two logical pieces of code: a run-time call to allocate the @@ -12488,9 +12502,14 @@ as well as any additional code needed to process the deletion. new_delete_base_type_from_operation_type(ndsp->type), /*check_constructor=*/TRUE)) { /* Get the size of a prefix if there is one. */ - prefix_size_node = get_prefix_size_node( + if (alloc_prefix_size != NULL) { + prefix_size_node = make_reusable_copy(alloc_prefix_size, + /*vars_can_change=*/FALSE); + } else { + prefix_size_node = get_prefix_size_node( array_element_type(ndsp->type), new_routine); + } /* if */ if (prefix_size_node != NULL) { /* Subtract the array prefix size. */ entity_node = add_cast_if_necessary(entity_node, char_star_type()); @@ -12823,6 +12842,7 @@ The subtree of the node has not yet been lowered. exception. */ turn_off_freeing_of_storage_on_exception(ndsp, &ipd, delete_args, (a_routine_ptr)NULL, + (an_expr_node_ptr)NULL, init_insert_location.variant.expr, &insert_location); } /* if */ diff --git a/src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt b/src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt new file mode 100644 index 0000000000..c243daf293 --- /dev/null +++ b/src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt @@ -0,0 +1,11 @@ +[GH #552] +Freeing over-aligned array new storage on an exception (non-IA-64 ABI) + +With the non-IA-64 ABI, when an element constructor threw during the brace or +list initialization of an array new of an over-aligned class, e.g., + + struct alignas(64) A { A(); ~A(); }; + new A[n]{}; + +the storage was freed using the default array new prefix size instead of the +alignment of the class: the wrong pointer was passed to operator delete[]. diff --git a/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.1.txt b/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.1.txt new file mode 100644 index 0000000000..06f761968e --- /dev/null +++ b/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++17 --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.2.txt b/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-new-aligned-throw-delete.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp b/tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp new file mode 100644 index 0000000000..54a4186cd4 --- /dev/null +++ b/tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp @@ -0,0 +1,130 @@ +//type:rp +//options_all:--c++17 --exceptions + +typedef decltype(sizeof(0)) Size; +namespace std { + enum class align_val_t : Size {}; +} + +alignas(128) unsigned char storage[1024]; +void *allocated_ptr; +Size allocated_size; +void *deallocated_ptr; +Size deallocated_size; +int deallocations; + +void *allocate(Size size) { + allocated_ptr = storage; + allocated_size = size; + return storage; +} +void deallocate(void *p, Size size) { + deallocated_ptr = p; + deallocated_size = size; + ++deallocations; +} + +void *operator new[](Size size, std::align_val_t) { + return allocate(size); +} +void operator delete[](void *p, std::align_val_t) noexcept { + deallocate(p, 0); +} +void operator delete[](void *p, Size size, std::align_val_t) noexcept { + deallocate(p, size); +} + +int constructions; +int throw_at; + +void constructed() { + if (++constructions == throw_at) throw 1; +} + +struct alignas(64) Global { + int value; + Global() : value(1) { constructed(); } + Global(int v) : value(v) { constructed(); } + ~Global() {} +}; + +struct alignas(64) Member { + int value; + Member() : value(1) { constructed(); } + Member(int v) : value(v) { constructed(); } + ~Member() {} + void *operator new[](Size size, std::align_val_t) { + return allocate(size); + } + void operator delete[](void *p, std::align_val_t) noexcept { + deallocate(p, 0); + } +}; + +/* Return 0 if the storage was freed exactly once using the pointer (and, + for a sized delete, the size) that was allocated. */ +int check(int (*make)(int), int count, int throw_on) { + int result = 0; + constructions = 0; + throw_at = throw_on; + deallocations = 0; + allocated_ptr = deallocated_ptr = 0; + allocated_size = deallocated_size = 0; + try { + make(count); + if (throw_on != 0) result = 1; + } catch (int) { + if (throw_on == 0) result = 1; + } + if (result == 0 && deallocations != 1) result = 2; + if (result == 0 && deallocated_ptr != allocated_ptr) result = 3; + if (result == 0 && deallocated_size != 0 && + deallocated_size != allocated_size) { + result = 4; + } + return result; +} + +template int make_brace(int count) { + delete[] new T[count]{}; + return 0; +} +template int make_list(int count) { + delete[] new T[count]{T(7)}; + return 0; +} +template int make_multi_brace(int count) { + delete[] new T[count][3]{}; + return 0; +} +template int make_multi_list(int count) { + delete[] new T[count][3]{{T(7)}}; + return 0; +} + +template int check_all(int count, int throw_on) { + int result; + if ((result = check(make_brace, count, throw_on)) != 0) { + return 10 + result; + } + if ((result = check(make_list, count, throw_on)) != 0) { + return 20 + result; + } + if ((result = check(make_multi_brace, count, throw_on)) != 0) { + return 30 + result; + } + if ((result = check(make_multi_list, count, throw_on)) != 0) { + return 40 + result; + } + return 0; +} + +int main(int argc, char **) { + int count = argc + 2; + int result; + if ((result = check_all(count, 2)) != 0) return 50 + result; + if ((result = check_all(count, 2)) != 0) return 100 + result; + if ((result = check_all(count, 0)) != 0) return 150 + result; + if ((result = check_all(count, 0)) != 0) return 200 + result; + return 0; +} From aa88a44d459ca6f9321dc49344123cef23cb12ce Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:41:56 +0000 Subject: [PATCH 2/2] Keep call arguments next to the opening parenthesis [GH #552] Following review feedback on a related pull request, do not start the argument list of the make_reusable_copy call on a new line; break the assignment after the "=" instead, as elsewhere in lower_init.c. No functional change. --- src/lower_init.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index 74eb3b28df..33847f5e5c 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -12002,9 +12002,9 @@ arrays with class elements. /* get_prefix_size_node does not account for the alignment, so the code that frees the storage on an exception must use the same prefix size. */ - aligned_prefix_size_node = make_reusable_copy( - prefix_size_node, - /*vars_can_change=*/FALSE); + aligned_prefix_size_node = + make_reusable_copy(prefix_size_node, + /*vars_can_change=*/FALSE); } else #endif /* !IA64_ABI */ /* Do not insert code here. */