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
2 changes: 1 addition & 1 deletion src/lower_il.c
Original file line number Diff line number Diff line change
Expand Up @@ -4558,7 +4558,7 @@ or contain a pointer to data member, which must be initialized to -1.
} /* lower_zero_initialization */


static void fill_out_aggregate_ptr_to_data_member_initialization(
void fill_out_aggregate_ptr_to_data_member_initialization(
a_constant_ptr constant)
/*
The indicated aggregate constant is an initializer. If it does not
Expand Down
3 changes: 3 additions & 0 deletions src/lower_il.h
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,9 @@ extern an_expr_node_ptr get_virtual_function_address(

extern a_boolean contains_ptr_to_data_member(a_type_ptr type);

extern void fill_out_aggregate_ptr_to_data_member_initialization(
a_constant_ptr constant);

extern void lower_initializer(a_variable_ptr variable,
an_init_kind *init_kind,
an_initializer_ptr initializer);
Expand Down
67 changes: 60 additions & 7 deletions src/lower_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -10008,7 +10008,7 @@ statement/expression lowering process (mostly by lower_dynamic_init).

#endif /* LOWERING_REMOVES_UNNEEDED_CONSTRUCTIONS_AND_DESTRUCTIONS */

static void stretch_partial_initialization_if_necessary(
static a_boolean stretch_partial_initialization_if_necessary(
a_dynamic_init_ptr dip,
an_init_pos_descr_ptr ipdp,
a_boolean have_complete_object,
Expand All @@ -10019,9 +10019,9 @@ has the is_partially_initialized flag set, indicating that the initializer
did not specify a value for every element of the aggregate. The entity
points to a complete object if have_complete_object is TRUE. If necessary,
generate code to initialize the elements of the aggregate (by adding code
at insert_location). Note that this routine could be optimized to
zero-initialize only the elements/fields of the aggregate that are
un-initialized.
at insert_location). Return TRUE if such code was generated. Note that this
routine could be optimized to zero-initialize only the elements/fields of the
aggregate that are un-initialized.
*/
{
a_boolean needs_initializing = FALSE;
Expand Down Expand Up @@ -10074,8 +10074,38 @@ un-initialized.
(a_targ_size_t)0,
insert_location);
} /* if */
return needs_initializing;
} /* stretch_partial_initialization_if_necessary */

#if IA64_ABI

static void fill_out_nested_ptr_to_data_member_initialization(
a_constant_ptr constant)
/*
Like fill_out_aggregate_ptr_to_data_member_initialization, but first do the
same for the aggregate constants nested in the indicated aggregate constant.
That's needed for a constant that has been through
lower_dynamic_init_aggregate_constant, whose nested aggregates with dynamic
parts were not filled out (unlike those processed by lower_constant).
*/
{
a_constant_ptr cp;

for (cp = constant->variant.aggregate.first_constant; cp != NULL;
cp = cp->next) {
a_constant_ptr sub_con = cp;
if (sub_con->kind == (a_constant_repr_kind)ck_init_repeat) {
sub_con = sub_con->variant.init_repeat.constant;
} /* if */
if (sub_con->kind == (a_constant_repr_kind)ck_aggregate) {
fill_out_nested_ptr_to_data_member_initialization(sub_con);
} /* if */
} /* for */
fill_out_aggregate_ptr_to_data_member_initialization(constant);
} /* fill_out_nested_ptr_to_data_member_initialization */

#endif /* IA64_ABI */


void lower_dynamic_init(a_dynamic_init_ptr dip,
an_init_pos_descr_ptr ipdp,
Expand Down Expand Up @@ -10164,6 +10194,7 @@ C99 mode for the same reason.
a_dynamic_init_ptr latest_initialization_on_entry = NULL;
a_boolean have_complete_object = TRUE;
a_boolean entity_is_wholly_initialized = FALSE;
a_boolean remainder_zeroed = FALSE;
a_routine_ptr ctor_routine;
a_constructor_init_ptr
ctor_init = NULL;
Expand Down Expand Up @@ -10545,7 +10576,8 @@ C99 mode for the same reason.
/* For cases where the initialization only partially covers the
entity being initialized, initialize the remaining portion
of the entity if necessary. */
stretch_partial_initialization_if_necessary(dip, ipdp,
(void)stretch_partial_initialization_if_necessary(
dip, ipdp,
have_complete_object,
eff_insert_location);
} /* if */
Expand Down Expand Up @@ -10783,7 +10815,8 @@ do_assignment:;
entity being initialized, initialize the remaining portion
of the entity if necessary. No need to stretch VLA
initialization; the back end will do that. */
stretch_partial_initialization_if_necessary(dip, ipdp,
remainder_zeroed = stretch_partial_initialization_if_necessary(
dip, ipdp,
have_complete_object,
eff_insert_location);
} /* if */
Expand Down Expand Up @@ -10823,6 +10856,25 @@ do_assignment:;
eff_insert_location,
&keep_constant,
options);
if (variable == NULL && dip->is_partially_initialized &&
!remainder_zeroed) {
/* The entity is part of a variable or (through a "?" or ","
operator) a variable initialized elsewhere, so zeroing the
variable does not cover the elements this initializer omits,
e.g., "k" in:
struct P { C c; int k; }; // C has a constructor.
P a[2] = { P{}, P{} };
Keep the constant, whose implicit zero elements initialize them
ahead of the code for the dynamic parts. */
keep_constant = TRUE;
#if IA64_ABI
/* A null pointer to data member is not represented by zero bits,
so those elements (including any in nested aggregates) must be
explicit. */
fill_out_nested_ptr_to_data_member_initialization(
dip->variant.constant.ptr);
#endif /* IA64_ABI */
} /* if */
do_keep_constant:
if (keep_constant) {
/* There is a constant part of the initialization to be kept. */
Expand Down Expand Up @@ -10975,7 +11027,8 @@ do_assignment:;
/* For cases where the initialization only partially covers the
entity being initialized, initialize the remaining portion
of the entity if necessary. */
stretch_partial_initialization_if_necessary(dip, ipdp,
(void)stretch_partial_initialization_if_necessary(
dip, ipdp,
have_complete_object,
eff_insert_location);
} /* if */
Expand Down
24 changes: 24 additions & 0 deletions src/pending_changes/gh-625-nested-aggregate-init-zeroing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[GH #625]
Members omitted from nested or "?"/"," aggregate initializers not zeroed

In configurations that do IL lowering, a member omitted from a braced
aggregate initializer could be left uninitialized instead of being
zero-initialized. This happened when the other parts of the initializer
needed code (such as a constructor call) and the initializer was an operand
of a "?" or "," operator initializing a variable, or initialized a part of
an enclosing initializer that had no constant part. For example, with
--c++11:

struct C { C(); };
struct P { C c; int k; };
int g();
int f(bool b) {
P x = (g(), P{}); // Previously, x.k was not zeroed.
P y = b ? P{} : P{}; // Previously, y.k was not zeroed.
P z[1] = { P{} }; // Previously, z[0].k was not zeroed.
return x.k + y.k + z[0].k;
}

The omitted members are now zero-initialized in those cases (so an omitted
pointer to data member becomes a null pointer, which is not all zero bits in
the IA-64 ABI).
1 change: 1 addition & 0 deletions tests/tests/regressions/.gh-625.rto/default.1.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eccp -DTEST_NUMBER=1 --c++17 --g++ --gnu_version=110300 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/regressions/.gh-625.rto/default.1.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./a.out
1 change: 1 addition & 0 deletions tests/tests/regressions/.gh-625.rto/default.2.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eccp -DTEST_NUMBER=2 --c++11 Test_name.c
1 change: 1 addition & 0 deletions tests/tests/regressions/.gh-625.rto/default.2.2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
./a.out
143 changes: 143 additions & 0 deletions tests/tests/regressions/gh-625.sft.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
//type:rp
//options:--c++17 --g++ --gnu_version=110300:--c++11
//remark:Zeroing of members omitted from nested or "?"/"," aggregate inits

// A member omitted from a braced aggregate initializer whose other parts
// need code (here, a constructor call) was left uninitialized when the
// aggregate initialized part of an enclosing initializer or was an operand of
// a "?" or "," operator. Each check runs on a stack filled with garbage.
// An omitted pointer to data member must become a null pointer, which is not
// all zero bits under some ABIs.

struct Ctor { int v; Ctor(); };
Ctor::Ctor() : v(7) {}

struct Pair { Ctor c; int k; };
struct Triple { Ctor c; int k; int m; };
struct Plain { int x; int y; };
struct Outer { Pair p; int z; };
struct Outer3 { Triple t; int z; };
struct Empty { Empty() { } };
struct PtmEmpty { Empty e; int Empty::*k; };
struct PtmPair { Ctor c; int Ctor::*k; };
struct PtmArr { Ctor c; int Ctor::*k[2]; };
struct PtmOuter { PtmPair p; int Ctor::*m; };
struct PtmWrap { PtmPair p; };
struct PtmDeep { PtmWrap w; };
struct PtmWrapArr { PtmPair p[2]; };
struct EmptyN { int n; EmptyN() { } };
struct PtmEmptyN { EmptyN e; int EmptyN::*k; };
struct PtmWrapN { PtmEmptyN s; };
#if __cplusplus >= 201703L
struct PtmDerived : PtmPair { };
#endif

int one() { return 1; }
Pair make() { Pair p = { Ctor(), 9 }; return p; }

void dirty() {
volatile int junk[256];
for (int i = 0; i < 256; i++) junk[i] = 5;
}

int comma() { Pair a = (one(), Pair{}); return a.k; }
int cond(bool b) { Pair a = b ? Pair{} : make(); return a.k; }
int cond_false(bool b) { Pair a = b ? make() : Pair{}; return a.k; }
int nested_cond(bool b) {
Pair a = b ? (b ? Pair{} : make()) : make();
return a.k;
}
Pair ret(bool b) { return b ? Pair{} : make(); }
int plain() { Plain a = (one(), Plain{one()}); return a.y; }
int array() { Pair a[2] = { Pair{}, Pair{} }; return a[0].k + a[1].k; }
int array_comma() { Pair a[2] = { (one(), Pair{}), Pair{} }; return a[0].k; }
int member() { Outer a = { Pair{}, one() }; return a.p.k + a.z - 1; }
int member_cond(bool b) {
Outer a = { b ? Pair{} : make(), one() };
return a.p.k;
}
int outer_cond(bool b) {
Outer a = b ? Outer{ Pair{}, one() } : Outer{ make(), 0 };
return a.p.k;
}
// The constant part of the nested initializer must be kept.
int kept() {
Outer3 a = { Triple{ Ctor(), 5 }, one() };
return a.t.k == 5 && a.t.m == 0 ? 0 : 1;
}

void g() { }
int ptm_empty() { PtmEmpty a = (g(), PtmEmpty{}); return a.k != nullptr; }
int ptm_comma() { PtmPair a = (one(), PtmPair{}); return a.k != nullptr; }
int ptm_array() {
PtmPair a[2] = { PtmPair{}, PtmPair{} };
return a[0].k != nullptr || a[1].k != nullptr;
}
int ptm_array_member() {
PtmArr a = (one(), PtmArr{});
return a.k[0] != nullptr || a.k[1] != nullptr;
}
int ptm_nested() {
PtmOuter a = (one(), PtmOuter{ PtmPair{} });
return a.p.k != nullptr || a.m != nullptr;
}
// The pointer to data member is omitted from a nested initializer.
int ptm_inner() { PtmWrap a = (one(), PtmWrap{{}}); return a.p.k != nullptr; }
int ptm_inner_empty() {
PtmWrapN a = (g(), PtmWrapN{{}});
return a.s.k != nullptr;
}
int ptm_inner_cond(bool b) {
PtmWrap a = b ? PtmWrap{{}} : PtmWrap{{}};
return a.p.k != nullptr;
}
int ptm_deep() {
PtmDeep a = (one(), PtmDeep{{{}}});
return a.w.p.k != nullptr;
}
int ptm_inner_array() {
PtmWrapArr a = (one(), PtmWrapArr{{{}, {}}});
return a.p[0].k != nullptr || a.p[1].k != nullptr;
}
int ptm_inner_elems() {
PtmWrap a[2] = { PtmWrap{{}}, PtmWrap{{}} };
return a[0].p.k != nullptr || a[1].p.k != nullptr;
}
int ptm_base() {
#if __cplusplus >= 201703L
PtmDerived a = (one(), PtmDerived{{}});
return a.k != nullptr;
#else
return 0;
#endif
}

#define CHECK(code, call) dirty(); if ((call) != 0) return code

int main() {
CHECK(1, comma());
CHECK(2, cond(true));
CHECK(3, nested_cond(true));
CHECK(4, ret(true).k);
CHECK(5, plain());
CHECK(6, array());
CHECK(7, array_comma());
CHECK(8, member());
CHECK(9, member_cond(true));
CHECK(10, outer_cond(true));
CHECK(11, kept());
CHECK(12, cond_false(false));
CHECK(13, ptm_empty());
CHECK(14, ptm_comma());
CHECK(15, ptm_array());
CHECK(16, ptm_array_member());
CHECK(17, ptm_nested());
CHECK(18, ptm_inner());
CHECK(19, ptm_inner_empty());
CHECK(20, ptm_inner_cond(true));
CHECK(21, ptm_deep());
CHECK(22, ptm_inner_array());
CHECK(23, ptm_inner_elems());
CHECK(24, ptm_base());
return 0;
}
Loading