From 6b1eb459ae2246706505d6486ff3ef9681041aa0 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:47 +0000 Subject: [PATCH 1/6] Use the runtime count for partially initialized multidimensional VLAs [GH #557] Partially initialized multidimensional GNU variable-length arrays (e.g., "E a[n][3] = {{E(3)}};") could be constructed and destroyed with the wrong number of elements, and some shapes caused an assertion failure: the repeat count of the remaining elements was taken from the total element count of the array, and the starting element was not scaled for rows of variable length. The repeat counts of such initializers are now encoded and decoded in one place (documented in il_def.h). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/decl_inits.c | 9 +- src/il_def.h | 9 +- src/lower_eh.c | 13 +- src/lower_il.h | 10 + src/lower_init.c | 333 ++++++++++++------ ...-vla-partial-init-runtime-repeat-count.txt | 10 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../default.2.1.txt | 1 + .../default.2.2.txt | 1 + ...ray-construction-vla-partial-throw.sft.cpp | 64 ++++ .../array-construction-vla-partial.sft.cpp | 268 ++++++++++++++ 14 files changed, 612 insertions(+), 110 deletions(-) create mode 100644 src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt create mode 100644 tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp create mode 100644 tests/tests/regressions/array-construction-vla-partial.sft.cpp diff --git a/src/decl_inits.c b/src/decl_inits.c index c077697a93..9e89b2f573 100644 --- a/src/decl_inits.c +++ b/src/decl_inits.c @@ -1983,7 +1983,7 @@ the position at which diagnostics should be issued. if (type_is(etype, tk_array)) { /* The element is a sub-array. Create a single potentially-repeated initializer for all array levels. */ - if (!etype->variant.array.is_vla) { + if (!is_vla_type(etype)) { count *= num_array_elements(etype); } else { /* is->variable_size_array should be TRUE, and therefore we should @@ -2037,8 +2037,11 @@ the position at which diagnostics should be issued. ck_repeat_init on top of it if needed. */ check_assertion(type_is(skip_typerefs(array_con->type), tk_array) && constant_is(array_con, ck_aggregate)); - if (is->variable_size_array && !is->non_top_level_aggregate) { - /* Something like "new T[x]{...}" where x is a run-time value. + if (is->variable_size_array && + (!is->non_top_level_aggregate || + is_vla_type(array_con->type))) { + /* Something like "new T[x]{...}" where x is a run-time value, or + a (possibly nested) VLA such as a row of "T a[2][n] = {...}". We don't know a priori how many default initializations are needed, but we have to represent it in some way so that lowering (or a back end) knows which routine to call. We use a zero count, diff --git a/src/il_def.h b/src/il_def.h index 7f5cc12d80..001461c24f 100644 --- a/src/il_def.h +++ b/src/il_def.h @@ -5353,7 +5353,14 @@ typedef struct a_constant { zero is used for new and delete of an array, and means "use the number of elements recorded along with the storage allocation". A similar case occurs - when initializing a variable-length array. */ + when initializing a variable-length array: a count + of zero (which can also appear in a nested + variable-length row of a multidimensional array) + initializes all the remaining elements, a number + known only at run time. In that case the repeated + constant always initializes leaf elements and the + ck_init_repeat is the last constant of its + aggregate. */ a_byte_boolean multidimensional_aggr_tail_not_repeated; /* This flag is used to indicate that the repeated diff --git a/src/lower_eh.c b/src/lower_eh.c index 0b96251195..1c213f5114 100644 --- a/src/lower_eh.c +++ b/src/lower_eh.c @@ -3406,7 +3406,9 @@ region description entry). The value put in the table is arbitrary, but use -1. */ elem_count = -1; elem_type = entity_type; - if (!ipdp->array_element_sequence) { + if (is_array_type(entity_type)) { + /* The count is in elements of the innermost array, both for a whole + VLA and for a sequence of array elements that are arrays. */ elem_type = underlying_array_element_type(entity_type); } /* if */ } else if (ipdp->array_element_sequence) { @@ -4098,6 +4100,12 @@ The region table variable is created if necessary. set_var_init_pos_descr(dip->variable->vla_element_count_variable, &ipd); make_handle_for_entity(&ipd, &vla_elem_count_handle, insert_location); } /* if */ + } else if (dedp->runtime_elem_count_var != NULL) { + /* A sequence of array elements whose number is known only at run time + is described like a VLA. */ + is_vla = TRUE; + set_var_init_pos_descr(dedp->runtime_elem_count_var, &ipd); + make_handle_for_entity(&ipd, &vla_elem_count_handle, insert_location); } /* if */ #endif /* ABI_COMPATIBILITY_VERSION < 306 */ dedp->next_in_region_table = next_dip; @@ -4207,7 +4215,8 @@ stop_before == NULL means stop at the beginning of the current lifetime. } else #endif /* !RUNTIME_VLA_DEALLOCATION_NEEDS_ELEMENT_COUNT */ /* Do not insert code here. */ - if (is_dynamic_init_for_vla(dip)) { + if (is_dynamic_init_for_vla(dip) || + dedp->runtime_elem_count_var != NULL) { /* The entry has a VLA element count entry, so clone that too. */ (void)clone_raw_region_table_entry(next_entry, ®ion_number); } /* if */ diff --git a/src/lower_il.h b/src/lower_il.h index 7989b2eb1b..8c81e488e9 100644 --- a/src/lower_il.h +++ b/src/lower_il.h @@ -337,6 +337,16 @@ typedef struct a_destructible_entity_descr { slightly different than the front end order (reflected by the dynamic init next_in_destruction_list pointer). */ + a_variable_ptr + runtime_elem_count_var; + /* When exceptions are enabled and the entity is a + sequence of array elements whose number is known + only at run time (e.g., the default-initialized + remainder of a row of a partially initialized + multidimensional variable-length array), points to + a size_t variable holding that number, which the + region table entry refers to like the element count + of a VLA. NULL otherwise. */ #endif /* GENERATE_EH_TABLES */ a_byte_boolean initialization_done; diff --git a/src/lower_init.c b/src/lower_init.c index 156d63c8cc..1aa243a8bc 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -1251,6 +1251,7 @@ Clear the fields of a destructible entity description to default values. dedp->region_number = null_eh_region_number; dedp->region_table_entry = NULL; dedp->next_in_region_table = NULL; + dedp->runtime_elem_count_var = NULL; #endif /* GENERATE_EH_TABLES */ dedp->initialization_done = FALSE; dedp->needs_subobject_construction_vtbl = FALSE; @@ -1775,8 +1776,11 @@ initialization (when ipdp->array_element_sequence is TRUE). if (ipdp->array_element_count == 0) { num_elem_node = num_elem_node_if_array(ipdp); } /* if */ - /* Create a helper routine to do the initialization and call it. */ - insert_call_to_initialize_entity(type_from_init_pos_descr(ipdp), + /* Create a helper routine to do the initialization and call it. The + entity type is the type of each element of the sequence (the + position itself can be a variably-sized row of a multidimensional + array). */ + insert_call_to_initialize_entity(ipdp->array_element_type, have_complete_object, add_address_of_to_node(entity_node), num_elem_node, @@ -2771,8 +2775,8 @@ all dimensions. if (ipdp->variable != NULL && ipdp->variable->is_vla && !ipdp->indirect_through_variable && - (ipdp->modifiers == NULL || ipdp->array_element_sequence)) { - /* The entity being accessed is a variable-length array (VLA). */ + ipdp->modifiers == NULL) { + /* The entity being accessed is a whole variable-length array (VLA). */ a_variable_ptr num_elem_var; /* Get the variable that has been set to the number of elements in the VLA. For multi-dimensional arrays, it gives the total across all @@ -2810,8 +2814,24 @@ all dimensions. if (ipdp->array_element_count == 0 && ipdp->num_elem_node != NULL) { /* For variably-sized arrays, use the expression that has already been created to describe the number of elements in the array. */ + /* For a VLA variable, the expression was built (by + set_partial_initialization_start) from the VLA dimension + temporaries, which do not change once set, and it is not + otherwise evaluated, so it must not be replaced by an + assignment to a temporary. */ + a_boolean vars_can_change = !(ipdp->variable != NULL && + ipdp->variable->is_vla && + !ipdp->indirect_through_variable); num_elem_node = make_reusable_copy(ipdp->num_elem_node, - /*vars_can_change=*/TRUE); + vars_can_change); + num_elem_node = add_cast_if_necessary(num_elem_node, + integer_type( +#if IA64_ABI + targ_size_t_int_kind +#else /* !IA64_ABI */ + targ_runtime_elem_count_int_kind +#endif /* IA64_ABI */ + )); } else { num_elem_node = num_elem_node_from_count(array_element_count); } /* if */ @@ -6563,6 +6583,156 @@ to lower_dynamic_init_aggregate_constant; see their description there. } /* helper_routine_to_initialize_repeated_constant */ +static void set_partial_initialization_start(an_init_pos_descr_ptr ipdp, + a_type_ptr aggr_type, + a_targ_size_t curr_elem) +/* +ipdp describes the elements of the variably-sized array type aggr_type that +remain to be default initialized, beginning with element curr_elem, to +complete a partial initialization. Set ipdp->num_elem_node and +ipdp->partial_initialization_starting_element so that num_elem_node_if_array +yields the number of remaining elements, counted (like the total number of +elements) in elements of the innermost array. When the element type of +aggr_type is itself a VLA (e.g., "A a[2][n] = {{A()}};"), the size of an +element is known only at runtime, so the remaining count is computed as an +expression. +*/ +{ + a_type_ptr elem_type = array_element_type(aggr_type); + a_type_ptr count_type; + an_expr_node_ptr count_node, used_node; + + if (is_vla_type(aggr_type)) { + /* For VLA types (e.g., "A a[n] = {A()};"), determine the number + of elements in the array from the VLA type (which is assumed + to have been lowered already, if needed). */ + ipdp->num_elem_node = vla_dimension_expr_for_type(aggr_type); + } /* if */ + check_assertion(ipdp->num_elem_node != NULL); + ipdp->partial_initialization_starting_element = + (a_host_large_integer)curr_elem; + if (is_array_type(elem_type)) { + /* For the multi-dimensional array case, ensure that the starting + element takes into account all of the elements that have already + been initialized. */ + if (is_vla_type(elem_type)) { + if (curr_elem != 0) { + count_type = integer_type(targ_size_t_int_kind); + used_node = add_cast_if_necessary( + vla_dimension_expr_for_type(elem_type), + count_type); + used_node->next = node_for_host_large_integer( + (a_host_large_integer)curr_elem, + targ_size_t_int_kind); + used_node = make_operator_node((an_expr_operator_kind)eok_multiply, + count_type, used_node); + count_node = add_cast_if_necessary(ipdp->num_elem_node, count_type); + count_node->next = used_node; + ipdp->num_elem_node = + make_operator_node((an_expr_operator_kind)eok_subtract, + count_type, count_node); + } /* if */ + ipdp->partial_initialization_starting_element = 0; + } else { + ipdp->partial_initialization_starting_element *= + (a_host_large_integer)num_array_elements(elem_type); + } /* if */ + } /* if */ +} /* set_partial_initialization_start */ + + +static a_constant_ptr set_init_pos_for_init_repeat( + an_init_pos_descr_ptr ipdp, + an_init_pos_descr_ptr outer_ipdp, + a_type_ptr aggr_type, + a_targ_size_t curr_elem, + a_constant_ptr repeat_con, + a_boolean strip_nested_repeats) +/* +repeat_con is a ck_init_repeat constant that initializes elements of the +array type aggr_type, beginning with element curr_elem. outer_ipdp describes +the array and ipdp (a copy of it positioned at element curr_elem) is updated +to describe the sequence of elements initialized by repeat_con. This is the +only place where lowering decodes the repeat count (see il_def.h); all users +of the resulting sequence get the number of elements from +num_elem_node_if_array (or ipdp->array_element_count when that is nonzero). +A count of zero means that the remaining elements of a variably-sized array +(possibly a nested row of one) are initialized; the number is known only at +run time, and the repeated constant then initializes the innermost elements. +A leaf entity repeated with a nonzero count for an array whose rows have +variable length (as for "A a[2][n];") initializes the whole array; the count +then covers only the constant dimensions and the number of innermost elements +is likewise known only at run time. +If strip_nested_repeats is TRUE and the repeated constant is an aggregate +consisting of a single repeat (as for a multidimensional array), the nested +repeats are folded into the count. Return the constant to be repeated. +*/ +{ + a_constant_ptr repeated_con = repeat_con->variant.init_repeat.constant; + a_targ_size_t count = repeat_con->variant.init_repeat.count; + + ipdp->array_element_sequence = TRUE; + ipdp->array_element_type = repeated_con->type; + if (count == 0) { + /* This initialization completes a partial initialization of a + variably-sized array. Make a note of the starting element that + needs initialization (which could be zero, in cases like + "new A[n] {}"). */ + check_assertion(!is_array_type(repeated_con->type)); + set_partial_initialization_start(ipdp, aggr_type, curr_elem); + } else if (is_vla_type(array_element_type(aggr_type)) && + !is_array_type(repeated_con->type)) { + /* A leaf entity repeated for all the elements of an array with rows of + variable length (e.g., "A a[2][n];"). The count is the number of + leaf elements known at compile time, i.e., the product of the + constant dimensions; the actual number, which also depends on the + variable dimensions, is known only at run time. */ + a_type_ptr type; + a_targ_size_t const_elems = 1; + + check_assertion(!outer_ipdp->array_element_sequence && curr_elem == 0); + for (type = skip_typerefs(aggr_type); is_array_type(type); + type = skip_typerefs(array_element_type(type))) { + if (!type->variant.array.is_vla) { + const_elems *= type->variant.array.variant.number_of_elements; + } /* if */ + } /* for */ + check_assertion(const_elems == count); + ipdp->num_elem_node = add_cast_if_necessary( + vla_dimension_expr_for_type(aggr_type), + integer_type(targ_size_t_int_kind)); + ipdp->array_element_count = 0; + ipdp->partial_initialization_starting_element = 0; + } else { + if (outer_ipdp->array_element_sequence) { + /* Flatten multi-dimensional arrays. */ + ipdp->array_element_count = outer_ipdp->array_element_count * + (a_targ_ptrdiff_t)count; + } else { + ipdp->array_element_count = (a_targ_ptrdiff_t)count; + } /* if */ + if (strip_nested_repeats) { + /* See if the constant is a repeated aggregate (used to initialize + multi-dimensional arrays). If so, strip any applicable + ck_init_repeat constants and increase the count accordingly. */ + while (repeated_con->kind == ck_aggregate && + repeated_con->variant.aggregate.first_constant->kind == + ck_init_repeat && + repeated_con->variant.aggregate.first_constant == + repeated_con->variant.aggregate.last_constant) { + a_constant_ptr rcon = repeated_con->variant.aggregate.first_constant; + check_assertion(rcon->variant.init_repeat.count != 0); + ipdp->array_element_count *= + (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; + repeated_con = rcon->variant.init_repeat.constant; + ipdp->array_element_type = repeated_con->type; + } /* while */ + } /* if */ + } /* if */ + return repeated_con; +} /* set_init_pos_for_init_repeat */ + + static void lower_dynamic_init_aggregate_constant( a_constant_ptr aggr_const, an_init_pos_descr_ptr ipdp, @@ -6794,13 +6964,15 @@ dealt with). "lower_dynamic_init_aggregate_constant: repeat on non-array"); repeated_con = con_ptr->variant.init_repeat.constant; elem_type = array_element_type(aggr_type); - if (has_aggregate_with_dynamic(repeated_con) && + if (con_ptr->variant.init_repeat.count != 0 && + has_aggregate_with_dynamic(repeated_con) && !identical_types_ignoring_qualifiers(elem_type, repeated_con->type)) { /* As a shortcut, the IL allows a single "leaf" entity to be repeated for a multi-dimensional aggregate constant. Remove this shortcut and create IL that represents the structure of the - multi-dimensional array. */ + multi-dimensional array. (A run-time count, i.e., zero, is + instead handled as a flattened sequence of leaf entities.) */ handle_multidimensional_ck_init_repeat(con_ptr, elem_type); /* Update information about the revised repeated constant. */ check_assertion(con_ptr->kind == (a_constant_repr_kind)ck_init_repeat); @@ -6817,43 +6989,9 @@ dealt with). aggr_type->variant.array.bound_is_zero) { /* Nothing to initialize here. */ } else { - ipd.array_element_sequence = TRUE; - ipd.array_element_type = repeated_con->type; - if (con_ptr->variant.init_repeat.count == 0) { - /* If the repeat count is zero, this initialization is being used - to complete a partial-initialization of a variably-sized array. - Make a note of the starting element that needs initialization - (which could be zero, in cases like "new A[n] {}"). */ - if (is_vla_type(aggr_type)) { - /* For VLA types (e.g., "A a[n] = {A()};"), determine the number - of elements in the array from the VLA type (which is assumed - to have been lowered already, if needed). */ - ipd.num_elem_node = vla_dimension_expr_for_type(aggr_type); - } /* if */ - check_assertion(ipd.num_elem_node != NULL); - ipd.partial_initialization_starting_element = - (a_host_large_integer)ipmp->curr_elem; - if (!is_variably_modified_type(aggr_type) && - is_array_type(array_element_type(aggr_type))) { - /* For the multi-dimensional array case, ensure that the - starting element takes into account all of the elements - that have already been initialized. */ - a_host_large_integer num_elems = - (a_host_large_integer)num_array_elements( - array_element_type(aggr_type)); - - ipd.partial_initialization_starting_element *= num_elems; - } /* if */ - } else { - if (ipdp->array_element_sequence) { - /* Flatten multi-dimensional arrays. */ - ipd.array_element_count = ipdp->array_element_count * - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } else { - ipd.array_element_count = - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } /* if */ - } /* if */ + (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/FALSE); if (repeated_con->kind == (a_constant_repr_kind)ck_dynamic_init && repeated_con->variant.dynamic_init.ptr->kind != (a_dynamic_init_kind)dik_nonconstant_aggregate) { @@ -6881,10 +7019,9 @@ dealt with). /*using_as_dest=*/FALSE); if (con_ptr->variant.init_repeat.count == 0) { /* The repeat count isn't known at compilation time; use - ipd.num_elem_node for the count. */ - check_assertion(ipd.num_elem_node != NULL); - args->next = make_reusable_copy(ipd.num_elem_node, - /*vars_can_change=*/TRUE); + the run-time count of the remaining elements. */ + args->next = num_elem_node_if_array(&ipd); + check_assertion(args->next != NULL); } else { args->next = node_for_host_large_integer( (a_host_large_integer)con_ptr->variant.init_repeat.count, @@ -6912,65 +7049,21 @@ dealt with). } else { lower_constant(repeated_con); } /* if */ - if (ipd.indirect_through_variable) { + if (ipd.indirect_through_variable || + con_ptr->variant.init_repeat.count == 0) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant aggregate initialization. This can occur when initializing a (portion of a) variably-sized array with a constant value - (from a constexpr constructor). Generate executable statements - to perform the initialization. */ + (from a constexpr constructor). A run-time count (zero) cannot + be represented in a constant either, even for a simple + variable. Generate executable statements to perform the + initialization. */ an_expr_node_ptr entity_node; - ipd.array_element_sequence = TRUE; - ipd.array_element_type = repeated_con->type; - if (con_ptr->variant.init_repeat.count == 0) { - /* If the repeat count is zero, this initialization is being used - to complete a partial-initialization of a variably-sized array. - Make a note of the starting element that needs initialization - (which could be zero, in cases like "new A[n] {}"). */ - if (is_vla_type(aggr_type)) { - /* For VLA types (e.g., "A a[n] = {A()};"), determine the number - of elements in the array from the VLA type (which is assumed - to have been lowered already, if needed). */ - ipd.num_elem_node = vla_dimension_expr_for_type(aggr_type); - } /* if */ - check_assertion(ipd.num_elem_node != NULL); - ipd.partial_initialization_starting_element = - (a_host_large_integer)ipmp->curr_elem; - if (is_array_type(array_element_type(aggr_type))) { - /* For the multi-dimensional array case, ensure that the - starting element takes into account all of the elements - that have already been initialized. */ - a_host_large_integer num_elems = - (a_host_large_integer)num_array_elements( - array_element_type(aggr_type)); - - ipd.partial_initialization_starting_element *= num_elems; - } /* if */ - } else { - if (ipdp->array_element_sequence) { - /* Flatten multi-dimensional arrays. */ - ipd.array_element_count = ipdp->array_element_count * - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } else { - ipd.array_element_count = - (a_targ_ptrdiff_t)con_ptr->variant.init_repeat.count; - } /* if */ - /* See if the constant is a repeated aggregate (used to initialize - multi-dimensional arrays). If so, strip any applicable - ck_init_repeat constants and increase the count accordingly. */ - while (repeated_con->kind == ck_aggregate && - repeated_con->variant.aggregate.first_constant->kind == - ck_init_repeat && - repeated_con->variant.aggregate.first_constant == - repeated_con->variant.aggregate.last_constant) { - a_constant_ptr rcon = - repeated_con->variant.aggregate.first_constant; - ipd.array_element_count *= - (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; - repeated_con = rcon-> variant.init_repeat.constant; - ipd.array_element_type = repeated_con->type; - } /* while */ - } /* if */ + repeated_con = set_init_pos_for_init_repeat( + &ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/TRUE); entity_node = make_init_entity_node(&ipd, /*result_is_lvalue=*/TRUE, /*using_as_dest=*/TRUE); @@ -6981,6 +7074,11 @@ dealt with). insert_location, /*is_lambda_capture=*/FALSE, &ipd); + if (con_ptr->variant.init_repeat.count == 0) { + /* Remove the ck_init_repeat constant, in case the overall + aggregate is kept for the constant parts. */ + remove_constant = TRUE; + } /* if */ } else { /* Repeated non-dynamic constants are possible with designators and when constexpr default constructors are folded. Either way, leave @@ -7722,6 +7820,28 @@ Any code needed is inserted at *insert_location. context->latest_initialization = dip; if (exceptions_enabled) { #if GENERATE_EH_TABLES + if (ipdp->array_element_sequence && + ipdp->array_element_count == 0 && + ipdp->num_elem_node != NULL && + ipdp->variable != NULL && + ipdp->variable->is_vla && + !ipdp->indirect_through_variable && + !is_dynamic_init_for_vla(dip)) { + /* The entity is a sequence of elements of a variable-length array + (e.g., the default-initialized remainder of a row of + "A a[2][n] = {{A()}};") whose number is computed at run time. If + an exception occurs later, the runtime needs that number to destroy + the elements, so save it in a variable that the region table entry + can refer to. (For a new-expression, such a sequence is the last + part of the initialization, so its cleanup is done by the + construction routine itself.) */ + an_expr_node_ptr count_node = num_elem_node_if_array(ipdp); + count_node = add_cast_if_necessary(count_node, + integer_type(targ_size_t_int_kind)); + dedp->runtime_elem_count_var = make_lowered_temporary(count_node->type); + (void)insert_var_assignment_statement(dedp->runtime_elem_count_var, + count_node, insert_location); + } /* if */ /* Make a region table entry for the entity (and for its conditional flag, if it has one). */ make_dyn_init_region_table_entry(dip, @@ -10500,12 +10620,17 @@ C99 mode for the same reason. is_or_was_ptr_to_member_function_type(entity_type) || ipdp->array_element_sequence) { /* Aggregate. Use a runtime routine call to zero it. */ - a_targ_size_t array_element_count = 0; + a_targ_size_t array_element_count = 0; + an_expr_node_ptr num_elem_node = NULL; if (ipdp->array_element_sequence) { /* If this assertion fails the array element count is unknown and no call to initialize the entity can be formed. */ check_assertion(ipdp->array_element_count >= 0); array_element_count = (a_targ_size_t)ipdp->array_element_count; + if (array_element_count == 0) { + /* The count is determined at run time. */ + num_elem_node = num_elem_node_if_array(ipdp); + } /* if */ entity_type = ipdp->array_element_type; } /* if */ entity_node = make_address_of_init_entity_node(ipdp, @@ -10513,7 +10638,7 @@ C99 mode for the same reason. insert_call_to_zero_entity(entity_type, have_complete_object, entity_node, - (an_expr_node_ptr)NULL, + num_elem_node, array_element_count, eff_insert_location); } else { diff --git a/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt b/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt new file mode 100644 index 0000000000..b28ef212a0 --- /dev/null +++ b/src/pending_changes/gh-557-vla-partial-init-runtime-repeat-count.txt @@ -0,0 +1,10 @@ +[GH #557] +Partially initialized multidimensional variable-length arrays + +A partially initialized multidimensional GNU variable-length array of a class +type, e.g., + + E a[3][n] = {{E(1)}, {}}; + +could be constructed and destroyed with the wrong number of elements (writing +past the end of the array), and some shapes caused an internal error. diff --git a/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.array-construction-vla-partial.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp b/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp new file mode 100644 index 0000000000..8ee4266694 --- /dev/null +++ b/tests/tests/regressions/array-construction-vla-partial-throw.sft.cpp @@ -0,0 +1,64 @@ +//type:rp +//options_all:--c++11 --g++ --exceptions + +// When a default constructor throws while a partially initialized +// multidimensional GNU variable-length array is being constructed, exactly +// the elements already constructed must be destroyed. + +int constructed; +int destroyed; +int errors; +int throw_at; + +struct Element { + int live; + Element() : live(1) { + if (++constructed == throw_at) throw 1; + } + Element(int) : live(1) { ++constructed; } + ~Element() { + if (live != 1) ++errors; + live = 0; + ++destroyed; + } +}; + +static void touch(void *) {} + +void rows_variable(int n) { + Element a[n][3] = {{Element(1)}}; + touch(a); +} + +void columns_variable(int n) { + Element a[3][n] = {{Element(1)}, {}}; + touch(a); +} + +void both_variable(int n, int m) { + Element a[n][2][m] = {{{Element(1)}}}; + touch(a); +} + +static void run(void (*construct)(int, int), int n, int m, int elements) { + for (throw_at = 2; throw_at <= elements; ++throw_at) { + constructed = destroyed = 0; + try { + construct(n, m); + ++errors; + } catch (int) { + } + if (constructed != throw_at || destroyed != throw_at - 1) ++errors; + } +} + +static void rows(int n, int) { rows_variable(n); } +static void columns(int n, int) { columns_variable(n); } +static void both(int n, int m) { both_variable(n, m); } + +int main() { + run(rows, 3, 0, 9); + run(columns, 3, 0, 9); + run(both, 2, 3, 12); + return errors; +} diff --git a/tests/tests/regressions/array-construction-vla-partial.sft.cpp b/tests/tests/regressions/array-construction-vla-partial.sft.cpp new file mode 100644 index 0000000000..6f2face644 --- /dev/null +++ b/tests/tests/regressions/array-construction-vla-partial.sft.cpp @@ -0,0 +1,268 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Partially initialized multidimensional GNU variable-length arrays: each +// element must be constructed and destroyed exactly once, and elements +// without an explicit initializer must be default-initialized. This also +// covers elements whose default constructor is constexpr (the remaining +// elements are then initialized with a constant, repeated a number of times +// known only at run time). + +int constructed; +int destroyed; +int errors; + +struct Element { + int value; + int live; + Element() : value(-1), live(1) { ++constructed; } + Element(int v) : value(v), live(1) { ++constructed; } + ~Element() { + if (live != 1) ++errors; + live = 0; + ++destroyed; + } +}; + +struct Pair { + Element first; + Element second; +}; + +struct Folded { + int value; + constexpr Folded() : value(-1) {} + constexpr Folded(int v) : value(v) {} + ~Folded() { ++destroyed; } +}; + +struct Holder { + Folded folded; + int extra = 3; +}; + +static void expect(int value, int wanted) { + if (value != wanted) ++errors; +} + +static void finish(int elements) { + expect(constructed, elements); + expect(destroyed, elements); + constructed = destroyed = 0; +} + +void rows_variable(int n) { + { + Element a[n][3] = {{Element(1)}, {Element(2), Element(3)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 3; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + if (i == 1 && j == 1) wanted = 3; + expect(a[i][j].value, wanted); + } + } + } + finish(n * 3); +} + +void columns_variable(int n) { + { + Element a[3][n] = {{Element(1)}, {Element(2), Element(3)}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + if (i == 1 && j == 1) wanted = 3; + expect(a[i][j].value, wanted); + } + } + } + finish(3 * n); +} + +void both_variable(int n, int m) { + { + Element a[n][m] = {{Element(1)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != m; ++j) { + expect(a[i][j].value, i == 0 && j == 0 ? 1 : -1); + } + } + } + finish(n * m); +} + +void middle_variable(int n) { + { + Element a[2][n][3] = {{{Element(1)}}, {{}, {Element(2)}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != n; ++j) { + for (int k = 0; k != 3; ++k) { + int wanted = -1; + if (i == 0 && j == 0 && k == 0) wanted = 1; + if (i == 1 && j == 1 && k == 0) wanted = 2; + expect(a[i][j][k].value, wanted); + } + } + } + } + finish(2 * n * 3); +} + +void innermost_variable(int n) { + { + Element a[2][3][n] = {{{Element(1)}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != 3; ++j) { + for (int k = 0; k != n; ++k) { + expect(a[i][j][k].value, i == 0 && j == 0 && k == 0 ? 1 : -1); + } + } + } + } + finish(2 * 3 * n); +} + +void aggregate_elements(int n, int m) { + { + Pair a[n][2][m] = {{{{Element(1)}}}, {{}, {{Element(2)}}}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 2; ++j) { + for (int k = 0; k != m; ++k) { + int wanted = -1; + if (i == 0 && j == 0 && k == 0) wanted = 1; + if (i == 1 && j == 1 && k == 0) wanted = 2; + expect(a[i][j][k].first.value, wanted); + expect(a[i][j][k].second.value, -1); + } + } + } + } + finish(n * 2 * m * 2); +} + +void empty_initializer(int n, int m) { + { + Element a[n][m] = {}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != m; ++j) { + expect(a[i][j].value, -1); + } + } + } + finish(n * m); +} + +void constexpr_elements(int n) { + { + Folded a[n] = {Folded(1)}; + for (int i = 0; i != n; ++i) expect(a[i].value, i == 0 ? 1 : -1); + } + expect(destroyed, n); + destroyed = 0; + { + Folded a[n][3] = {{Folded(1)}}; + for (int i = 0; i != n; ++i) { + for (int j = 0; j != 3; ++j) { + expect(a[i][j].value, i == 0 && j == 0 ? 1 : -1); + } + } + } + expect(destroyed, n * 3); + destroyed = 0; + { + Folded a[3][n] = {{Folded(1)}, {Folded(2)}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + int wanted = -1; + if (i == 0 && j == 0) wanted = 1; + if (i == 1 && j == 0) wanted = 2; + expect(a[i][j].value, wanted); + } + } + } + expect(destroyed, 3 * n); + destroyed = 0; +} + +void constexpr_members(int n) { + { + Holder a[3][n] = {{Holder()}}; + for (int i = 0; i != 3; ++i) { + for (int j = 0; j != n; ++j) { + expect(a[i][j].folded.value, -1); + expect(a[i][j].extra, 3); + } + } + } + expect(destroyed, 3 * n); + destroyed = 0; + { + Holder a[2][n][2] = {{{Holder()}}}; + for (int i = 0; i != 2; ++i) { + for (int j = 0; j != n; ++j) { + for (int k = 0; k != 2; ++k) { + expect(a[i][j][k].folded.value, -1); + expect(a[i][j][k].extra, 3); + } + } + } + } + expect(destroyed, 2 * n * 2); + destroyed = 0; +} + +void no_initializer(int n) { + // Default initialization of arrays with rows of variable length: the + // number of elements known at compile time covers only the constant + // dimensions. + { + Element a[2][n]; + expect(a[1][n - 1].value, -1); + } + finish(2 * n); + { + Element a[2][n][2]; + expect(a[1][n - 1][1].value, -1); + } + finish(2 * n * 2); + { + Element a[2][2][n]; + expect(a[1][1][n - 1].value, -1); + } + finish(2 * 2 * n); + { + Element a[2][n][n]; + expect(a[1][n - 1][n - 1].value, -1); + } + finish(2 * n * n); + { + typedef Element Row[n]; + Row a[3]; + expect(a[2][n - 1].value, -1); + } + finish(3 * n); +} + +int main() { + rows_variable(4); + columns_variable(4); + both_variable(3, 2); + both_variable(1, 1); + middle_variable(3); + innermost_variable(2); + aggregate_elements(3, 2); + empty_initializer(3, 2); + empty_initializer(0, 2); + empty_initializer(2, 0); + constexpr_elements(4); + constexpr_elements(1); + constexpr_members(3); + no_initializer(3); + no_initializer(1); + return errors; +} From cf8c82ec544245c9a9349228dff3ea3de47bcc31 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:49 +0000 Subject: [PATCH 2/6] Fix brace initialization of VLAs whose rows have variable length [GH #558] An initializer such as "int a[2][n] = {{1}, {2, 3}};" (or "= {}") can be neither kept as a C initializer nor copied from a static temporary, but it caused an internal error (class types) or generated C that used an undeclared temporary. Zero the whole array and then assign the elements individually; elements without an explicit initializer are zero (or default-initialized). Requires the runtime repeat count change for partially initialized multidimensional VLAs. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_il.c | 7 +- src/lower_il.h | 9 ++ src/lower_init.c | 87 +++++++++- .../gh-558-vla-rows-brace-init.txt | 10 ++ .../.vla-rows-brace-init.rto/default.1.1.txt | 1 + .../.vla-rows-brace-init.rto/default.1.2.txt | 1 + .../.vla-rows-brace-init.rto/default.2.1.txt | 1 + .../.vla-rows-brace-init.rto/default.2.2.txt | 1 + .../regressions/vla-rows-brace-init.sft.cpp | 150 ++++++++++++++++++ 9 files changed, 260 insertions(+), 7 deletions(-) create mode 100644 src/pending_changes/gh-558-vla-rows-brace-init.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt create mode 100644 tests/tests/regressions/vla-rows-brace-init.sft.cpp diff --git a/src/lower_il.c b/src/lower_il.c index 832fee1d98..cd2717435f 100644 --- a/src/lower_il.c +++ b/src/lower_il.c @@ -4578,7 +4578,12 @@ called in C mode. handles them correctly. If they are partially initialized, the first member is (at least partially) initialized, which is all we need here. */ - if (is_array_type(type) && contains_ptr_to_data_member(type)) { + if (is_vla_type(type)) { + /* A constant of VLA type (for an array with rows of variable length) + cannot be filled out; the whole entity is set to its zero value + before the constant's elements are assigned (see + lower_vla_rows_constant_init in lower_init.c). */ + } else if (is_array_type(type) && contains_ptr_to_data_member(type)) { a_targ_size_t elem = 0; /* Count the number of elements that have been explicitly initialized. Note that they have previously been run through diff --git a/src/lower_il.h b/src/lower_il.h index 8c81e488e9..3c47702b93 100644 --- a/src/lower_il.h +++ b/src/lower_il.h @@ -227,6 +227,15 @@ typedef struct an_init_pos_descr { lowered. Such an element cannot refer to that aggregate through an implicit "this" (see lower_param_ref). */ + a_byte_boolean + assign_constants; + /* TRUE if no part of the initialization can be kept + as a constant, so that constants must be assigned + by executable code even though the entity is a + simple variable. This is the case for an array + with rows of variable length, which can be neither + initialized by a C initializer nor copied from a + static constant. */ a_type_ptr base_type; /* Base entity type. */ an_init_pos_modifier_ptr diff --git a/src/lower_init.c b/src/lower_init.c index 1aa243a8bc..8a75179bf7 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -1133,6 +1133,7 @@ Clear an initialization position description entry to default values. ipdp->base_class_subobject = FALSE; ipdp->base_of_complete_object = FALSE; ipdp->not_this_candidate = FALSE; + ipdp->assign_constants = FALSE; ipdp->base_type = NULL; ipdp->modifiers = NULL; ipdp->array_element_count = 0; @@ -7049,7 +7050,7 @@ dealt with). } else { lower_constant(repeated_con); } /* if */ - if (ipd.indirect_through_variable || + if (ipd.indirect_through_variable || ipd.assign_constants || con_ptr->variant.init_repeat.count == 0) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant @@ -7098,7 +7099,10 @@ dealt with). in the initialization, but if that's not possible, rewrite the initialization with executable statements. */ mark_as_visited(con_ptr); - if (ipd.indirect_through_variable) { + if (ipd.assign_constants) { + /* The entire array has already been zeroed (see + lower_vla_rows_constant_init). */ + } else if (ipd.indirect_through_variable) { insert_call_to_zero_entity(con_ptr->type, /*have_complete_object=*/TRUE, make_address_of_init_entity_node(&ipd, @@ -7126,7 +7130,7 @@ dealt with). /* C++ mode. */ lower_constant(con_ptr); } /* if */ - if (ipd.indirect_through_variable) { + if (ipd.indirect_through_variable || ipd.assign_constants) { /* The entity being initialized is not a simple variable, so we don't want to keep any part of the initialization as a constant aggregate initialization. This comes up with return value @@ -10127,6 +10131,51 @@ statement/expression lowering process (mostly by lower_dynamic_init). #endif /* LOWERING_REMOVES_UNNEEDED_CONSTRUCTIONS_AND_DESTRUCTIONS */ +static a_boolean has_variable_length_rows(a_type_ptr type) +/* +Return TRUE if type is an array whose element type is a VLA, i.e., an +array with rows of variable length (e.g., "int[2][n]" or "int[n][n]"). +*/ +{ + type = skip_typerefs(type); + return is_array_type(type) && + is_vla_type(type->variant.array.element_type); +} /* has_variable_length_rows */ + + +static void lower_vla_rows_constant_init(a_dynamic_init_ptr dip, + an_init_pos_descr_ptr ipdp, + an_insert_location_ptr + insert_location) +/* +The dik_constant dynamic initialization dip initializes the entity described +by ipdp, an array whose rows have variable length (e.g., "int a[2][n] = { +{1}, {2, 3} };"). The entity is always treated as partially initialized, so +it has already been zeroed in full (see +stretch_partial_initialization_if_necessary); insert assignments of the +individual element values at *insert_location. +*/ +{ + a_constant_ptr con = dip->variant.constant.ptr; + an_init_pos_descr ipd; + a_boolean keep_constant = FALSE; + + check_assertion(dip->is_partially_initialized && + has_variable_length_rows(type_from_init_pos_descr(ipdp))); + if (con->kind == (a_constant_repr_kind)ck_aggregate && + con->variant.aggregate.first_constant != NULL) { + ipd = *ipdp; + ipd.assign_constants = TRUE; + lower_dynamic_init_aggregate_constant(con, &ipd, /*dtor_case=*/FALSE, + (an_implied_copy_source *)NULL, + /*others_follow_in_aggr=*/FALSE, + insert_location, &keep_constant, + (a_lower_dynamic_init_options_set)0); + check_assertion(!keep_constant); + } /* if */ +} /* lower_vla_rows_constant_init */ + + static void stretch_partial_initialization_if_necessary( a_dynamic_init_ptr dip, an_init_pos_descr_ptr ipdp, @@ -10185,12 +10234,30 @@ un-initialized. } /* if */ } /* if */ if (needs_initializing) { - insert_call_to_zero_entity(entity_type, + a_type_ptr zero_type = entity_type; + an_expr_node_ptr num_elem_node = ipdp->num_elem_node; + a_targ_size_t array_element_count = 0; + if (ipdp->array_element_sequence) { + /* A sequence of array elements (e.g., the remaining elements of an + array with rows of variable length, "A a[2][n] = {{A()}};"): zero + that many elements of the sequence element type, as for dik_zero + in lower_dynamic_init. */ + check_assertion(ipdp->array_element_count >= 0); + array_element_count = (a_targ_size_t)ipdp->array_element_count; + num_elem_node = array_element_count != 0 ? (an_expr_node_ptr)NULL : + num_elem_node_if_array(ipdp); + zero_type = ipdp->array_element_type; + } else if (is_vla_type(entity_type)) { + /* insert_call_to_zero_entity gets the number of elements of a VLA + from the type itself. */ + num_elem_node = NULL; + } /* if */ + insert_call_to_zero_entity(zero_type, have_complete_object, make_address_of_init_entity_node(ipdp, /*using_as_dest=*/TRUE), - ipdp->num_elem_node, - (a_targ_size_t)0, + num_elem_node, + array_element_count, insert_location); } /* if */ } /* stretch_partial_initialization_if_necessary */ @@ -10673,6 +10740,14 @@ C99 mode for the same reason. have_complete_object, eff_insert_location); } /* if */ + if (has_variable_length_rows(type_from_init_pos_descr(ipdp))) { + /* The constant can be neither kept as an initializer nor copied from + a static temporary, because its layout differs from that of the + entity (whose rows have a length known only at run time); assign + its elements individually. */ + lower_vla_rows_constant_init(dip, ipdp, eff_insert_location); + break; + } /* if */ /* For the normal cases, go on and generate an assignment. */ goto do_assignment; case dik_expression: diff --git a/src/pending_changes/gh-558-vla-rows-brace-init.txt b/src/pending_changes/gh-558-vla-rows-brace-init.txt new file mode 100644 index 0000000000..c2f8436699 --- /dev/null +++ b/src/pending_changes/gh-558-vla-rows-brace-init.txt @@ -0,0 +1,10 @@ +[GH #558] +Brace initialization of variable-length arrays with variable-length rows + +An initializer for a GNU variable-length array whose rows have variable +length, e.g., + + int a[2][n] = {{1}, {2, 3}}; + D b[2][n] = {}; // D has a destructor + +generated C that used an undeclared temporary, or caused an internal error. diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-rows-brace-init.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/vla-rows-brace-init.sft.cpp b/tests/tests/regressions/vla-rows-brace-init.sft.cpp new file mode 100644 index 0000000000..6d8371c01e --- /dev/null +++ b/tests/tests/regressions/vla-rows-brace-init.sft.cpp @@ -0,0 +1,150 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Brace initialization of GNU variable-length arrays whose rows have +// variable length (e.g., "int a[2][n] = {{1}, {2, 3}};"). The initializer +// can be neither kept as a C initializer nor copied from a static temporary +// (the layout of the rows is known only at run time), so the elements are +// assigned individually after the whole array has been zeroed. Elements +// without an explicit initializer must be zero (or, for a class with a +// default member initializer or constexpr constructor, default-initialized). + +int errors; + +static void check(int got, int expected) { + if (got != expected) ++errors; +} + +struct P { int x, y; }; +struct CD { int v; constexpr CD() : v(-1) {} ~CD() {} }; +struct W { CD c; int i = 3; }; +struct DO { int v; ~DO() {} }; +struct S { int a, b; }; +struct Q { int S::*p; int v; }; + +static void empty_rows(int n) { + int a[2][n] = {}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) check(a[i][j], 0); +} + +static void partial_rows(int n) { + int a[3][n] = {{1}, {2, 3}, {}}; + for (int i = 0; i < 3; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void middle_variable_dim(int n) { + int a[2][n][2] = {{{1, 2}, {3}}, {{4}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + for (int k = 0; k < 2; k++) + check(a[i][j][k], i == 0 && j == 0 ? k + 1 : + i == 0 && j == 1 && k == 0 ? 3 : + i == 1 && j == 0 && k == 0 ? 4 : 0); +} + +static void inner_variable_dim(int n) { + int a[2][2][n] = {{{1}, {2, 3}}, {{}, {4}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < 2; j++) + for (int k = 0; k < n; k++) + check(a[i][j][k], i == 0 && j == 0 && k == 0 ? 1 : + i == 0 && j == 1 && k == 0 ? 2 : + i == 0 && j == 1 && k == 1 ? 3 : + i == 1 && j == 1 && k == 0 ? 4 : 0); +} + +static void all_variable_dims(int n) { + // The initializer has the fixed type int[2][2] here. + int a[n][n] = {{1}, {2, 3}}; + for (int i = 0; i < n; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void typedef_rows(int n) { + typedef int R[n]; + R a[2] = {{1}, {2, 3}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 1 : + i == 1 && j == 0 ? 2 : + i == 1 && j == 1 ? 3 : 0); +} + +static void nonconstant_element(int n) { + int x = n * 10; + int a[2][n] = {{x, 1}, {2}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? x : + i == 0 && j == 1 ? 1 : + i == 1 && j == 0 ? 2 : 0); +} + +static void string_rows(int n) { + char a[2][n] = {"ab", "c"}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) + check(a[i][j], i == 0 && j == 0 ? 'a' : + i == 0 && j == 1 ? 'b' : + i == 1 && j == 0 ? 'c' : 0); +} + +static void struct_rows(int n) { + P a[2][n] = {{{1, 2}}, {{3}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].x, i == 0 && j == 0 ? 1 : i == 1 && j == 0 ? 3 : 0); + check(a[i][j].y, i == 0 && j == 0 ? 2 : 0); + } +} + +static void class_rows(int n) { + W w[2][n] = {}; + CD c[2][n] = {}; + DO d[2][n] = {{{5}}, {{6}, {7}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(w[i][j].i, 3); + check(w[i][j].c.v, -1); + check(c[i][j].v, -1); + check(d[i][j].v, i == 0 && j == 0 ? 5 : + i == 1 && j == 0 ? 6 : + i == 1 && j == 1 ? 7 : 0); + } +} + +static void ptr_to_member_rows(int n) { + // A null pointer to data member is not all-bits-zero in the IA-64 ABI. + Q a[2][n] = {{{&S::b, 5}}}; + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(a[i][j].v, i == 0 && j == 0 ? 5 : 0); + } +} + +int main() { + for (int n = 3; n <= 4; n++) { + empty_rows(n); + partial_rows(n); + middle_variable_dim(n); + inner_variable_dim(n); + all_variable_dims(n); + typedef_rows(n); + nonconstant_element(n); + string_rows(n); + struct_rows(n); + class_rows(n); + ptr_to_member_rows(n); + } + return errors != 0; +} From d015e679ff7444ed62d068d1ecb20fdebcab5bbc Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:54 +0000 Subject: [PATCH 3/6] Fix brace initialization of VLAs of a class with a destructor [GH #559] Without exceptions, such an initialization keeps the constant part of the initializer and leaves zeroing the remainder to the back end. That did not zero the array at all when no constant part was kept ("= {}"), and zeroing gives the wrong value for a null pointer to data member, which is not all-bits-zero in the IA-64 ABI. Requires the fix for brace initialization of VLAs whose rows have variable length. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 23 ++++-- .../gh-559-vla-init-class-dtor.txt | 7 ++ .../.vla-init-class-dtor.rto/default.1.1.txt | 1 + .../.vla-init-class-dtor.rto/default.1.2.txt | 1 + .../.vla-init-class-dtor.rto/default.2.1.txt | 1 + .../.vla-init-class-dtor.rto/default.2.2.txt | 1 + .../regressions/vla-init-class-dtor.sft.cpp | 77 +++++++++++++++++++ 7 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 src/pending_changes/gh-559-vla-init-class-dtor.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt create mode 100644 tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt create mode 100644 tests/tests/regressions/vla-init-class-dtor.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 8a75179bf7..88abd28b91 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -10968,6 +10968,8 @@ do_assignment:; break; case dik_nonconstant_aggregate: { + an_init_pos_descr vla_ipd; + an_init_pos_descr_ptr agg_ipdp; /* Initialization with a nonconstant aggregate constant. This is usually a whole-variable initialization, but can be used in a ctor-initializer or lambda capture to iterate over an array @@ -10976,12 +10978,21 @@ do_assignment:; latest_initialization_on_entry = eff_context->latest_initialization; } /* if */ keep_constant = FALSE; - if (dip->is_partially_initialized && - (variable == NULL || !is_vla_type(variable->type))) { + agg_ipdp = ipdp; + if (variable != NULL && is_vla_type(variable->type)) { + /* The back end cannot be relied on to complete the initialization + of a VLA: nothing zeroes it if no constant is kept, and zeroing + gives the wrong null pointer-to-data-member value for the + IA-64 ABI. Initialize the whole VLA here and assign (rather + than keep) the constant parts. */ + vla_ipd = *ipdp; + vla_ipd.assign_constants = TRUE; + agg_ipdp = &vla_ipd; + } /* if */ + if (dip->is_partially_initialized) { /* For cases where the initialization only partially covers the entity being initialized, initialize the remaining portion - of the entity if necessary. No need to stretch VLA - initialization; the back end will do that. */ + of the entity if necessary. */ stretch_partial_initialization_if_necessary(dip, ipdp, have_complete_object, eff_insert_location); @@ -11016,12 +11027,14 @@ do_assignment:; copy_node(init_entity_expr)); insert_expr(master_entry_assignment, eff_insert_location); } /* if */ - lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, ipdp, + lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, + agg_ipdp, /*dtor_case=*/FALSE, source_desc, others_follow_in_aggr, eff_insert_location, &keep_constant, options); + check_assertion(agg_ipdp == ipdp || !keep_constant); do_keep_constant: if (keep_constant) { /* There is a constant part of the initialization to be kept. */ diff --git a/src/pending_changes/gh-559-vla-init-class-dtor.txt b/src/pending_changes/gh-559-vla-init-class-dtor.txt new file mode 100644 index 0000000000..73b4c8f9a1 --- /dev/null +++ b/src/pending_changes/gh-559-vla-init-class-dtor.txt @@ -0,0 +1,7 @@ +[GH #559] +Zeroing of variable-length arrays of a class with a destructor + +With exceptions disabled, "D a[n] = {};" for a class D with a destructor left +the array uninitialized, and a pointer to data member element of such an array +was zeroed rather than set to the null member pointer value (which is not +all-bits-zero in the IA-64 ABI). diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt new file mode 100644 index 0000000000..2766ef361c --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++11 --g++ --exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt new file mode 100644 index 0000000000..615286a368 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=2 --c++11 --g++ --no_exceptions Test_name.c diff --git a/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.vla-init-class-dtor.rto/default.2.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/vla-init-class-dtor.sft.cpp b/tests/tests/regressions/vla-init-class-dtor.sft.cpp new file mode 100644 index 0000000000..e059885a45 --- /dev/null +++ b/tests/tests/regressions/vla-init-class-dtor.sft.cpp @@ -0,0 +1,77 @@ +//type:rp +//options_all:--c++11 --g++ +//options:--exceptions:--no_exceptions + +// Brace initialization of GNU variable-length arrays of a class type with a +// nontrivial destructor. When exceptions are disabled, such an +// initialization is lowered by keeping the constant part of the initializer +// and leaving the zeroing of the remainder to the back end; that did not +// zero the array at all when no constant part was kept ("= {}"), and zeroing +// gives the wrong value for a null pointer to data member, which is not +// all-bits-zero in the IA-64 ABI. + +int errors; + +static void check(int got, int expected) { + if (got != expected) ++errors; +} + +// Leave nonzero garbage on the stack so that missing zeroing is detected. +static void scribble() { + volatile int junk[256]; + for (int i = 0; i < 256; i++) junk[i] = 0x5a5a5a5a; +} + +struct S { int a, b; }; +struct DO { int v; ~DO() {} }; +struct M { int S::*p; int v; ~M() {} }; +struct W { int k; M m; }; + +static int empty_init(int n) { + DO a[n] = {}; + DO b[2][n] = {}; + DO c[n][2] = {}; + DO d[n]{}; + int sum = 0; + for (int i = 0; i < n; i++) { + sum += a[i].v + d[i].v + c[i][0].v + c[i][1].v; + sum += b[0][i].v + b[1][i].v; + } + return sum; +} + +static void ptr_to_member(int n) { + M a[n] = {{&S::b, 5}}; + M b[n][2] = {{{&S::b, 5}}}; + M c[2][n] = {{{&S::b, 5}}}; + W w[n] = {{1, {&S::b, 5}}}; + M e[n + 1] = {{&S::b, 5}, {nullptr, 6}}; + for (int i = 0; i < n; i++) { + check(a[i].p == (i == 0 ? &S::b : nullptr), 1); + check(a[i].v, i == 0 ? 5 : 0); + for (int j = 0; j < 2; j++) { + check(b[i][j].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(b[i][j].v, i == 0 && j == 0 ? 5 : 0); + check(c[j][i].p == (i == 0 && j == 0 ? &S::b : nullptr), 1); + check(c[j][i].v, i == 0 && j == 0 ? 5 : 0); + } + check(w[i].k, i == 0 ? 1 : 0); + check(w[i].m.p == (i == 0 ? &S::b : nullptr), 1); + check(w[i].m.v, i == 0 ? 5 : 0); + } + check(e[0].p == &S::b, 1); + for (int i = 1; i <= n; i++) { + check(e[i].p == nullptr, 1); + check(e[i].v, i == 1 ? 6 : 0); + } +} + +int main() { + for (int n = 1; n <= 4; n++) { + scribble(); + check(empty_init(n), 0); + scribble(); + ptr_to_member(n); + } + return errors != 0; +} From 35fd9bc8c89367e3bd6562be357dcdc0fc759d17 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Wed, 18 Nov 2026 18:08:56 +0000 Subject: [PATCH 4/6] Destroy constructed array elements when value-initialization throws [GH #554] For "G g{};" where G has a member "P a[4]" and P has a nontrivial default constructor and a destructor, the elements were initialized by a generated helper routine without cleanups for the elements already constructed (or with a cleanup for only one of them), cleanups of preceding members were lost, and the helper's exception-handling epilogue was missing. Also affected: default member initializers, multidimensional and nested arrays, and new G{}. Initialize such element sequences with the runtime array construction routine, using a helper that initializes one element and the element destructor, and add the return statements of generated helper routines to the return memo list. Requires the fix for brace initialization of VLAs of a class with a destructor (textual overlap only). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- src/lower_init.c | 212 ++++++++++++++++-- .../gh-554-value-init-array-member-throw.txt | 13 ++ .../default.1.1.txt | 1 + .../default.1.2.txt | 1 + .../value-init-array-member-throw.sft.cpp | 55 +++++ 5 files changed, 263 insertions(+), 19 deletions(-) create mode 100644 src/pending_changes/gh-554-value-init-array-member-throw.txt create mode 100644 tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt create mode 100644 tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt create mode 100644 tests/tests/regressions/value-init-array-member-throw.sft.cpp diff --git a/src/lower_init.c b/src/lower_init.c index 88abd28b91..f4c5cf72f0 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -167,6 +167,12 @@ static a_boolean recompute_partially_initialized_flag( a_constant_ptr aggr_con, a_type_ptr aggr_type); +static void stretch_partial_initialization_if_necessary( + a_dynamic_init_ptr dip, + an_init_pos_descr_ptr ipdp, + a_boolean have_complete_object, + an_insert_location_ptr insert_location); + static a_type_ptr make_function_type(a_type_ptr return_type, a_type_ptr param_1_type, a_type_ptr param_2_type) @@ -6514,7 +6520,7 @@ to lower_dynamic_init_aggregate_constant; see their description there. an_insert_location insert_location, loop_insert_location; a_generated_routine_context context; a_variable_ptr entity_var, count_var = NULL; - a_statement_ptr loop_stmt = NULL; + a_statement_ptr loop_stmt = NULL, return_stmt; an_expr_node_ptr ptr_increment, expr; a_param_type_ptr count_param_type = NULL; an_init_pos_descr ipd; @@ -6537,6 +6543,12 @@ to lower_dynamic_init_aggregate_constant; see their description there. scope = make_routine_definition(rp, /*make_return=*/TRUE, curr_il_region_number, &il_region); push_generated_routine_context(scope, il_region, &context); + /* Add the return statement at the end of the routine to the return memo + list, so that any exception handling epilogue is added before it. */ + return_stmt = scope->assoc_block->variant.block.statements; + check_assertion(return_stmt != NULL && + return_stmt->kind == (a_statement_kind)stmk_return); + add_to_return_memo_list(return_stmt); /* Create the parameters. */ entity_var = make_lowered_param_variable(rtsp->param_type_list->type); count_var = make_lowered_param_variable(count_param_type->type); @@ -6584,6 +6596,95 @@ to lower_dynamic_init_aggregate_constant; see their description there. } /* helper_routine_to_initialize_repeated_constant */ +static a_routine_ptr helper_routine_to_initialize_aggregate_element( + a_dynamic_init_ptr dip, + a_type_ptr type, + an_implied_copy_source *source_desc, + a_lower_dynamic_init_options_set options) +/* +Create, and return a pointer to, a "helper" routine that initializes one +element of a sequence of array elements with the dik_nonconstant_aggregate +dynamic initialization dip. The routine is passed as the "constructor" to +the runtime array construction routine (which loops over the elements and, +if an exception is thrown, destroys the elements already constructed), so +it has the generic constructor type (see make_ctor_type): + + static void helper(type *ptr) { + + } + +(returning ptr when constructors return a pointer). Only the +initialization of the element is done here; the destruction of the elements +is handled for the whole sequence by the caller. "type" is a pointer to the +type of the element. source_desc and options are passed through to +lower_dynamic_init_aggregate_constant; see their description there. +*/ +{ + a_routine_ptr rp; + a_type_ptr pointer_type, return_type; + a_memory_region_number il_region; + a_scope_ptr scope; + an_insert_location insert_location; + a_generated_routine_context context; + a_variable_ptr entity_var; + a_statement_ptr return_stmt; + an_init_pos_descr ipd; + a_boolean keep_constant = FALSE; + a_boolean return_ptr; + + check_assertion(dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate); + pointer_type = skip_typerefs(type); + check_assertion(is_pointer_type(pointer_type)); +#if IA64_ABI + return_ptr = ctors_return_this; +#else /* !IA64_ABI */ + return_ptr = TRUE; +#endif /* IA64_ABI */ + return_type = return_ptr ? pointer_type : void_type(); + rp = make_rout_entry((char *)NULL, (a_storage_class)sc_static, + return_type, pointer_type); + /* Build the definition of the routine. Note that the same memory region + is used (because lowering of the aggregate may involve IL that is + already in the current memory region). */ + scope = make_routine_definition(rp, /*make_return=*/TRUE, + curr_il_region_number, &il_region); + push_generated_routine_context(scope, il_region, &context); + /* Add the return statement at the end of the routine to the return memo + list, so that any exception handling epilogue is added before it. */ + return_stmt = scope->assoc_block->variant.block.statements; + check_assertion(return_stmt != NULL && + return_stmt->kind == (a_statement_kind)stmk_return); + add_to_return_memo_list(return_stmt); + entity_var = make_lowered_param_variable( + rp->type->variant.routine.extra_info->param_type_list-> + type); + scope->variant.routine.parameters = entity_var; + if (return_ptr) return_stmt->expr = var_rvalue_expr(entity_var); + set_block_start_insert_location(scope->assoc_block, &insert_location); + /* Mark the location where any generated stmk_init statements should go. */ + set_insert_location_mark(&insert_location); + /* Insert any generated stmk_inits at the previously marked location. */ + insert_pending_stmk_init_statements_at_mark(&insert_location); + /* Set ipd to point to the element being passed in. */ + set_var_indirect_init_pos_descr(entity_var, &ipd); + if (dip->is_partially_initialized) { + stretch_partial_initialization_if_necessary(dip, &ipd, + /*have_complete_object=*/TRUE, + &insert_location); + } /* if */ + lower_dynamic_init_aggregate_constant(dip->variant.constant.ptr, &ipd, + /*dtor_case=*/FALSE, source_desc, + /*others_follow_in_aggr=*/FALSE, + &insert_location, &keep_constant, + options); + check_assertion(!keep_constant); + /* Clean up. */ + pop_generated_routine_context(scope, il_region, &context); + return rp; +} /* helper_routine_to_initialize_aggregate_element */ + + static void set_partial_initialization_start(an_init_pos_descr_ptr ipdp, a_type_ptr aggr_type, a_targ_size_t curr_elem) @@ -6990,6 +7091,27 @@ dealt with). aggr_type->variant.array.bound_is_zero) { /* Nothing to initialize here. */ } else { + an_init_pos_descr leaf_ipd; + a_constant_ptr leaf_con; + a_dynamic_init_ptr leaf_dip; + a_boolean init_leaf_sequence = FALSE; + /* Determine the repeated leaf entity (stripping the repeats of + a multi-dimensional array), and whether it must be destroyed + if an exception occurs during the initialization of a later + element. */ + leaf_ipd = ipd; + leaf_con = set_init_pos_for_init_repeat(&leaf_ipd, ipdp, aggr_type, + ipmp->curr_elem, con_ptr, + /*strip_nested_repeats=*/TRUE); + if (exceptions_enabled && !dtor_case && + leaf_con->kind == (a_constant_repr_kind)ck_dynamic_init) { + leaf_dip = leaf_con->variant.dynamic_init.ptr; + init_leaf_sequence = + leaf_dip->destructor != NULL && + (leaf_dip->kind == + (a_dynamic_init_kind)dik_nonconstant_aggregate || + leaf_dip->kind == (a_dynamic_init_kind)dik_constructor); + } /* if */ (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, ipmp->curr_elem, con_ptr, /*strip_nested_repeats=*/FALSE); @@ -7014,29 +7136,39 @@ dealt with). will be re-invoked to lower repeated_con, but in the context of a different function). */ an_expr_node_ptr args; - /* Call the routine: helper(ptr, count); */ check_assertion(con_ptr->variant.init_repeat.constant != NULL); - args = make_address_of_init_entity_node(&ipd, - /*using_as_dest=*/FALSE); - if (con_ptr->variant.init_repeat.count == 0) { - /* The repeat count isn't known at compilation time; use - the run-time count of the remaining elements. */ - args->next = num_elem_node_if_array(&ipd); - check_assertion(args->next != NULL); + if (init_leaf_sequence) { + /* The elements must be destroyed if the initialization of + a later element throws an exception. Lower the leaf + initialization for the whole (flattened) sequence of + elements; lower_dynamic_init uses the runtime array + construction routine for that, and arranges for the + destruction of the sequence. */ + lower_ck_dynamic_init(leaf_con, &leaf_ipd, dtor_case, + source_desc, others_follow, + insert_location, keep_constant, options); } else { - args->next = node_for_host_large_integer( + /* Call the routine: helper(ptr, count); */ + args = make_address_of_init_entity_node(&ipd, + /*using_as_dest=*/FALSE); + if (con_ptr->variant.init_repeat.count == 0) { + /* The repeat count isn't known at compilation time; use + the run-time count of the remaining elements. */ + args->next = num_elem_node_if_array(&ipd); + check_assertion(args->next != NULL); + } else { + args->next = node_for_host_large_integer( (a_host_large_integer)con_ptr->variant.init_repeat.count, targ_size_t_int_kind); + } /* if */ + make_call_statement( + helper_routine_to_initialize_repeated_constant( + repeated_con, args->type, + dtor_case, source_desc, + others_follow, + keep_constant, options), + args, (an_expr_node_ptr)NULL, insert_location); } /* if */ - make_call_statement(helper_routine_to_initialize_repeated_constant( - repeated_con, - args->type, - dtor_case, - source_desc, - others_follow, - keep_constant, - options), - args, (an_expr_node_ptr)NULL, insert_location); /* Remove the ck_init_repeat constant, in case the overall aggregate is kept for the constant parts. */ remove_constant = TRUE; @@ -10978,6 +11110,48 @@ do_assignment:; latest_initialization_on_entry = eff_context->latest_initialization; } /* if */ keep_constant = FALSE; + if (ipdp->array_element_sequence) { + /* A sequence of array elements, each initialized with the + aggregate (see lower_dynamic_init_aggregate_constant). Use the + runtime array construction routine, which destroys the elements + already constructed if an exception is thrown, with a helper + routine that initializes one element: + __cxa_vec_ctor(ptr, count, size, helper, dtor); + The destruction of the sequence is arranged below, as for an + array constructor call. */ + a_routine_ptr helper, dtor_routine = NULL; + an_expr_node_ptr num_elem_node; + if (exceptions_enabled && dip->destructor != NULL) { + dtor_routine = dip->destructor; +#if IA64_ABI + dtor_routine = alternate_entry_point(dtor_routine, + (a_ctor_or_dtor_kind)cdk_complete, + /*define_now=*/FALSE); +#endif /* IA64_ABI */ + } /* if */ + /* For a multi-dimensional array, the address is that of the first + row; convert it to a pointer to the first element. */ + entity_node = make_address_of_init_entity_node(ipdp, + /*using_as_dest=*/TRUE); + entity_node = add_cast_if_necessary( + entity_node, + make_pointer_type(ipdp->array_element_type)); + num_elem_node = num_elem_node_if_array(ipdp); + check_assertion(num_elem_node != NULL); + helper = helper_routine_to_initialize_aggregate_element( + dip, + entity_node->type, + source_desc, + options); + (void)insert_expr_statement_set_pos( + make_vec_new_call(entity_node, entity_node->type, + num_elem_node, helper, dtor_routine, + (a_routine_ptr)NULL, + (a_routine_ptr)NULL, + /*zero_storage=*/FALSE), + eff_insert_location); + break; + } /* if */ agg_ipdp = ipdp; if (variable != NULL && is_vla_type(variable->type)) { /* The back end cannot be relied on to complete the initialization diff --git a/src/pending_changes/gh-554-value-init-array-member-throw.txt b/src/pending_changes/gh-554-value-init-array-member-throw.txt new file mode 100644 index 0000000000..b2d596052a --- /dev/null +++ b/src/pending_changes/gh-554-value-init-array-member-throw.txt @@ -0,0 +1,13 @@ +[GH #554] +Array members not cleaned up when their value-initialization throws + +When a constructor called for the value-initialization of an array member +threw, the elements already constructed were not destroyed (and cleanups of +preceding members could be lost), e.g., + + struct P { int i; E e; }; // E has a constructor and a destructor + struct G { P a[4]; }; + G g{}; + +Default member initializers, multidimensional and nested arrays, and +"new G{}" were affected as well. diff --git a/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt new file mode 100644 index 0000000000..06f761968e --- /dev/null +++ b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.1.txt @@ -0,0 +1 @@ +eccp -DTEST_NUMBER=1 --c++17 --exceptions Test_name.c diff --git a/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt new file mode 100644 index 0000000000..ff2642c6d5 --- /dev/null +++ b/tests/tests/regressions/.value-init-array-member-throw.rto/default.1.2.txt @@ -0,0 +1 @@ +./a.out diff --git a/tests/tests/regressions/value-init-array-member-throw.sft.cpp b/tests/tests/regressions/value-init-array-member-throw.sft.cpp new file mode 100644 index 0000000000..eea6080f71 --- /dev/null +++ b/tests/tests/regressions/value-init-array-member-throw.sft.cpp @@ -0,0 +1,55 @@ +//type:rp +//options_all:--c++17 --exceptions + +// Value-/brace-initialization of an aggregate whose array member has a +// repeated initializer for a class with a destructor (e.g., "G g{}" where +// G has a member "P a[4]" and P has a nontrivial default constructor and +// destructor). When the constructor of one element throws, the elements +// that were already constructed must be destroyed exactly once. Before +// the fix, the generated per-element helper had no cleanup for earlier +// elements (or registered the cleanup of one element only), and cleanups +// of preceding array members were lost. + +int ctors, dtors, throw_at, live, errors; + +struct E { + int v; + E() : v(-1) { if (++ctors == throw_at) throw 1; ++live; } + ~E() { ++dtors; --live; } +}; +struct P { int i; E e; }; +struct Q { E e1; int i; E e2; }; +struct H { P a[4]; int tail = 9; }; +struct G { P a[4]; }; +struct GQ { Q a[3]; }; +struct G2 { P a[2][3]; }; +struct G3 { P a[2][2][2]; }; +struct N { G g[2]; }; +struct GV { int k = 5; Q a[2]; P b[2][2]; }; + +template static void check(int n, F f) { + for (throw_at = 1; throw_at <= n; ++throw_at) { + ctors = dtors = live = 0; + try { f(); } catch (int) {} + if (dtors != throw_at - 1 || live != 0) ++errors; + } +} + +int main() { + check(4, [] { H x{}; }); + check(4, [] { G x{}; }); + check(6, [] { GQ x{}; }); + check(6, [] { G2 x{}; }); + check(8, [] { G3 x{}; }); + check(8, [] { N x{}; }); + check(8, [] { GV x{}; }); + check(12, [] { G2 x[2]{}; }); + check(4, [] { static G x{}; }); + check(4, [] { (void)G{}; }); + check(4, [] { G *p = new G{}; delete p; }); + /* Success path: everything constructed is destroyed. */ + throw_at = 0; live = 0; + { G2 a{}; GV b{}; G c[2]{}; N d{}; } + if (live != 0) ++errors; + return errors != 0; +} From 146f9905f84b1b0029200e5d305a5c00120615c1 Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 12:43:14 +0000 Subject: [PATCH 5/6] Use kind-test macros instead of enumerator casts [GH #557] Following review feedback on a related pull request, test dynamic initialization and constant kinds with dyn_init_is and constant_is, drop the remaining redundant enumerator casts, and rewrap the calls that now fit on fewer lines. No functional change. --- src/lower_init.c | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index f4c5cf72f0..c8b22b03c9 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -6546,8 +6546,7 @@ to lower_dynamic_init_aggregate_constant; see their description there. /* Add the return statement at the end of the routine to the return memo list, so that any exception handling epilogue is added before it. */ return_stmt = scope->assoc_block->variant.block.statements; - check_assertion(return_stmt != NULL && - return_stmt->kind == (a_statement_kind)stmk_return); + check_assertion(return_stmt != NULL && return_stmt->kind == stmk_return); add_to_return_memo_list(return_stmt); /* Create the parameters. */ entity_var = make_lowered_param_variable(rtsp->param_type_list->type); @@ -6633,7 +6632,7 @@ lower_dynamic_init_aggregate_constant; see their description there. a_boolean keep_constant = FALSE; a_boolean return_ptr; - check_assertion(dip->kind == (a_dynamic_init_kind)dik_nonconstant_aggregate); + check_assertion(dyn_init_is(dip, dik_nonconstant_aggregate)); pointer_type = skip_typerefs(type); check_assertion(is_pointer_type(pointer_type)); #if IA64_ABI @@ -6653,8 +6652,7 @@ lower_dynamic_init_aggregate_constant; see their description there. /* Add the return statement at the end of the routine to the return memo list, so that any exception handling epilogue is added before it. */ return_stmt = scope->assoc_block->variant.block.statements; - check_assertion(return_stmt != NULL && - return_stmt->kind == (a_statement_kind)stmk_return); + check_assertion(return_stmt != NULL && return_stmt->kind == stmk_return); add_to_return_memo_list(return_stmt); entity_var = make_lowered_param_variable( rp->type->variant.routine.extra_info->param_type_list-> @@ -6726,13 +6724,11 @@ expression. used_node->next = node_for_host_large_integer( (a_host_large_integer)curr_elem, targ_size_t_int_kind); - used_node = make_operator_node((an_expr_operator_kind)eok_multiply, - count_type, used_node); + used_node = make_operator_node(eok_multiply, count_type, used_node); count_node = add_cast_if_necessary(ipdp->num_elem_node, count_type); count_node->next = used_node; - ipdp->num_elem_node = - make_operator_node((an_expr_operator_kind)eok_subtract, - count_type, count_node); + ipdp->num_elem_node = make_operator_node(eok_subtract, + count_type, count_node); } /* if */ ipdp->partial_initialization_starting_element = 0; } else { @@ -7104,13 +7100,12 @@ dealt with). ipmp->curr_elem, con_ptr, /*strip_nested_repeats=*/TRUE); if (exceptions_enabled && !dtor_case && - leaf_con->kind == (a_constant_repr_kind)ck_dynamic_init) { + constant_is(leaf_con, ck_dynamic_init)) { leaf_dip = leaf_con->variant.dynamic_init.ptr; init_leaf_sequence = leaf_dip->destructor != NULL && - (leaf_dip->kind == - (a_dynamic_init_kind)dik_nonconstant_aggregate || - leaf_dip->kind == (a_dynamic_init_kind)dik_constructor); + (dyn_init_is(leaf_dip, dik_nonconstant_aggregate) || + dyn_init_is(leaf_dip, dik_constructor)); } /* if */ (void)set_init_pos_for_init_repeat(&ipd, ipdp, aggr_type, ipmp->curr_elem, con_ptr, @@ -10294,7 +10289,7 @@ individual element values at *insert_location. check_assertion(dip->is_partially_initialized && has_variable_length_rows(type_from_init_pos_descr(ipdp))); - if (con->kind == (a_constant_repr_kind)ck_aggregate && + if (constant_is(con, ck_aggregate) && con->variant.aggregate.first_constant != NULL) { ipd = *ipdp; ipd.assign_constants = TRUE; @@ -11124,8 +11119,7 @@ do_assignment:; if (exceptions_enabled && dip->destructor != NULL) { dtor_routine = dip->destructor; #if IA64_ABI - dtor_routine = alternate_entry_point(dtor_routine, - (a_ctor_or_dtor_kind)cdk_complete, + dtor_routine = alternate_entry_point(dtor_routine, cdk_complete, /*define_now=*/FALSE); #endif /* IA64_ABI */ } /* if */ From 8717ac69537cd07c19e840bd8b0b567000d3b4df Mon Sep 17 00:00:00 2001 From: Alexander Herz Date: Fri, 20 Nov 2026 17:49:45 +0000 Subject: [PATCH 6/6] Destroy single-element array rows when a later element throws [GH #554] When a member array whose rows have a single element, such as P a[3][1], was value-initialized, the repeated initializer of the row was not stripped down to its element, so the elements already constructed were not destroyed when a later constructor threw. Treat a single-element array row like a nested repeat when finding the repeated leaf. Reported by CodeRabbit. The regression test now also requires that an exception is actually thrown and covers [3][1] and [3][1][2] members. The VLA rows test also covers a default member initializer that is evaluated for each element. --- src/lower_init.c | 32 +++++++++++++++---- .../value-init-array-member-throw.sft.cpp | 12 +++++-- .../regressions/vla-rows-brace-init.sft.cpp | 18 +++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) diff --git a/src/lower_init.c b/src/lower_init.c index c8b22b03c9..5b811b50ec 100644 --- a/src/lower_init.c +++ b/src/lower_init.c @@ -6739,6 +6739,17 @@ expression. } /* set_partial_initialization_start */ +static a_boolean is_single_element_array_type(a_type_ptr type) +/* +Return TRUE if type is a (fixed-size) array type with a single element. +*/ +{ + type = skip_typerefs(type); + return is_array_type(type) && !type->variant.array.is_vla && + type->variant.array.variant.number_of_elements == 1; +} /* is_single_element_array_type */ + + static a_constant_ptr set_init_pos_for_init_repeat( an_init_pos_descr_ptr ipdp, an_init_pos_descr_ptr outer_ipdp, @@ -6763,7 +6774,9 @@ then covers only the constant dimensions and the number of innermost elements is likewise known only at run time. If strip_nested_repeats is TRUE and the repeated constant is an aggregate consisting of a single repeat (as for a multidimensional array), the nested -repeats are folded into the count. Return the constant to be repeated. +repeats are folded into the count; likewise, a row of a single element +(as for "A a[3][1]") is replaced by that element. Return the constant to be +repeated. */ { a_constant_ptr repeated_con = repeat_con->variant.init_repeat.constant; @@ -6814,15 +6827,22 @@ repeats are folded into the count. Return the constant to be repeated. multi-dimensional arrays). If so, strip any applicable ck_init_repeat constants and increase the count accordingly. */ while (repeated_con->kind == ck_aggregate && - repeated_con->variant.aggregate.first_constant->kind == - ck_init_repeat && + (repeated_con->variant.aggregate.first_constant->kind == + ck_init_repeat || + is_single_element_array_type(repeated_con->type)) && repeated_con->variant.aggregate.first_constant == repeated_con->variant.aggregate.last_constant) { a_constant_ptr rcon = repeated_con->variant.aggregate.first_constant; - check_assertion(rcon->variant.init_repeat.count != 0); - ipdp->array_element_count *= + if (constant_is(rcon, ck_init_repeat)) { + check_assertion(rcon->variant.init_repeat.count != 0); + ipdp->array_element_count *= (a_targ_ptrdiff_t)rcon->variant.init_repeat.count; - repeated_con = rcon->variant.init_repeat.constant; + repeated_con = rcon->variant.init_repeat.constant; + } else { + /* A row of a single element (e.g., of "A a[3][1]"); the count + is unchanged. */ + repeated_con = rcon; + } /* if */ ipdp->array_element_type = repeated_con->type; } /* while */ } /* if */ diff --git a/tests/tests/regressions/value-init-array-member-throw.sft.cpp b/tests/tests/regressions/value-init-array-member-throw.sft.cpp index eea6080f71..fc052c3ce9 100644 --- a/tests/tests/regressions/value-init-array-member-throw.sft.cpp +++ b/tests/tests/regressions/value-init-array-member-throw.sft.cpp @@ -26,12 +26,15 @@ struct G2 { P a[2][3]; }; struct G3 { P a[2][2][2]; }; struct N { G g[2]; }; struct GV { int k = 5; Q a[2]; P b[2][2]; }; +struct G1 { P a[3][1]; }; +struct G12 { P a[3][1][2]; }; template static void check(int n, F f) { for (throw_at = 1; throw_at <= n; ++throw_at) { + bool threw = false; ctors = dtors = live = 0; - try { f(); } catch (int) {} - if (dtors != throw_at - 1 || live != 0) ++errors; + try { f(); } catch (int) { threw = true; } + if (!threw || dtors != throw_at - 1 || live != 0) ++errors; } } @@ -44,12 +47,15 @@ int main() { check(8, [] { N x{}; }); check(8, [] { GV x{}; }); check(12, [] { G2 x[2]{}; }); + check(3, [] { G1 x{}; }); + check(6, [] { G12 x{}; }); + check(3, [] { (void)G1{}; }); check(4, [] { static G x{}; }); check(4, [] { (void)G{}; }); check(4, [] { G *p = new G{}; delete p; }); /* Success path: everything constructed is destroyed. */ throw_at = 0; live = 0; - { G2 a{}; GV b{}; G c[2]{}; N d{}; } + { G2 a{}; GV b{}; G c[2]{}; N d{}; G1 e{}; G12 f{}; } if (live != 0) ++errors; return errors != 0; } diff --git a/tests/tests/regressions/vla-rows-brace-init.sft.cpp b/tests/tests/regressions/vla-rows-brace-init.sft.cpp index 6d8371c01e..df1ed00761 100644 --- a/tests/tests/regressions/vla-rows-brace-init.sft.cpp +++ b/tests/tests/regressions/vla-rows-brace-init.sft.cpp @@ -22,6 +22,9 @@ struct W { CD c; int i = 3; }; struct DO { int v; ~DO() {} }; struct S { int a, b; }; struct Q { int S::*p; int v; }; +static int counter; +static int next_value() { return ++counter; } +struct NM { int v = next_value(); }; static void empty_rows(int n) { int a[2][n] = {}; @@ -122,6 +125,20 @@ static void class_rows(int n) { } } +static void dynamic_member_init_rows(int n) { + // The default member initializer is evaluated for each element. + counter = 0; + NM a[2][n] = {}; + check(counter, 2 * n); + NM b[2][n][2] = {}; + check(counter, 6 * n); + for (int i = 0; i < 2; i++) + for (int j = 0; j < n; j++) { + check(a[i][j].v >= 1 && a[i][j].v <= 2 * n, 1); + check(b[i][j][1].v > 2 * n, 1); + } +} + static void ptr_to_member_rows(int n) { // A null pointer to data member is not all-bits-zero in the IA-64 ABI. Q a[2][n] = {{{&S::b, 5}}}; @@ -144,6 +161,7 @@ int main() { string_rows(n); struct_rows(n); class_rows(n); + dynamic_member_init_rows(n); ptr_to_member_rows(n); } return errors != 0;