Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion src/lower_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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. */
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
/*
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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 */
Expand Down
11 changes: 11 additions & 0 deletions src/pending_changes/gh-552-noia64-aligned-array-new-eh.txt
Original file line number Diff line number Diff line change
@@ -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[].
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eccp -DTEST_NUMBER=1 --c++17 --exceptions Test_name.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./a.out
130 changes: 130 additions & 0 deletions tests/tests/regressions/array-new-aligned-throw-delete.sft.cpp
Original file line number Diff line number Diff line change
@@ -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 <class T> int make_brace(int count) {
delete[] new T[count]{};
return 0;
}
template <class T> int make_list(int count) {
delete[] new T[count]{T(7)};
return 0;
}
template <class T> int make_multi_brace(int count) {
delete[] new T[count][3]{};
return 0;
}
template <class T> int make_multi_list(int count) {
delete[] new T[count][3]{{T(7)}};
return 0;
}

template <class T> int check_all(int count, int throw_on) {
int result;
if ((result = check(make_brace<T>, count, throw_on)) != 0) {
return 10 + result;
}
if ((result = check(make_list<T>, count, throw_on)) != 0) {
return 20 + result;
}
if ((result = check(make_multi_brace<T>, count, throw_on)) != 0) {
return 30 + result;
}
if ((result = check(make_multi_list<T>, count, throw_on)) != 0) {
return 40 + result;
}
return 0;
}

int main(int argc, char **) {
int count = argc + 2;
int result;
if ((result = check_all<Global>(count, 2)) != 0) return 50 + result;
if ((result = check_all<Member>(count, 2)) != 0) return 100 + result;
if ((result = check_all<Global>(count, 0)) != 0) return 150 + result;
if ((result = check_all<Member>(count, 0)) != 0) return 200 + result;
return 0;
}
Loading