Skip to content

Commit

Permalink
c++: Check for indirect change of active union member in constexpr [P…
Browse files Browse the repository at this point in the history
…R101631,PR102286]

On Wed, Oct 11, 2023 at 12:48:12AM +1100, Nathaniel Shead wrote:
> On Mon, Oct 09, 2023 at 04:46:46PM -0400, Jason Merrill wrote:
> > On 10/8/23 21:03, Nathaniel Shead wrote:
> > > Ping for https://gcc.gnu.org/pipermail/gcc-patches/2023-September/631203.html
> > >
> > > +	  && (TREE_CODE (t) == MODIFY_EXPR
> > > +	      /* Also check if initializations have implicit change of active
> > > +		 member earlier up the access chain.  */
> > > +	      || !refs->is_empty())
> >
> > I'm not sure what the cumulative point of these two tests is.  TREE_CODE (t)
> > will be either MODIFY_EXPR or INIT_EXPR, and either should be OK.
> >
> > As I understand it, the problematic case is something like
> > constexpr-union2.C, where we're also looking at a MODIFY_EXPR.  So what is
> > this check doing?
>
> The reasoning was to correctly handle cases like the the following (in
> constexpr-union6.C):
>
>   constexpr int test1() {
>     U u {};
>     std::construct_at(&u.s, S{ 1, 2 });
>     return u.s.b;
>   }
>   static_assert(test1() == 2);
>
> The initialisation of &u.s here is not a member access expression within
> the call to std::construct_at, since it's just a pointer, but this code
> is still legal; in general, an INIT_EXPR to initialise a union member
> should always be OK (I believe?), hence constraining to just
> MODIFY_EXPR.
>
> However, just that would then (incorrectly) allow all the following
> cases in that test to compile, such as
>
>   constexpr int test2() {
>     U u {};
>     int* p = &u.s.b;
>     std::construct_at(p, 5);
>     return u.s.b;
>   }
>   constexpr int x2 = test2();
>
> since the INIT_EXPR is really only initialising 'b', but the implicit
> "modification" of active member to 'u.s' is illegal.
>
> Maybe a better way of expressing this condition would be something like
> this?
>
>   /* An INIT_EXPR of the last member in an access chain is always OK,
>      but still check implicit change of members earlier on; see
>      cpp2a/constexpr-union6.C.  */
>   && !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ())
>
> Otherwise I'll see if I can rework some of the other conditions instead.
>
> > Incidentally, I think constexpr-union6.C could use a test where we pass &u.s
> > to a function other than construct_at, and then try (and fail) to assign to
> > the b member from that function.
> >
> > Jason
> >
>
> Sounds good; I've added the following test:
>
>   constexpr void foo(S* s) {
>     s->b = 10;  // { dg-error "accessing .U::s. member instead of initialized .U::k." }
>   }
>   constexpr int test3() {
>     U u {};
>     foo(&u.s);  // { dg-message "in .constexpr. expansion" }
>     return u.s.b;
>   }
>   constexpr int x3 = test3();  // { dg-message "in .constexpr. expansion" }
>
> Incidentally I found this particular example caused a very unhelpful
> error + ICE due to reporting that S could not be value-initialized in
> the current version of the patch. The updated version below fixes that
> by using 'build_zero_init' instead -- is this an appropriate choice
> here?
>
> A similar (but unrelated) issue is with e.g.
>
>   struct S { const int a; int b; };
>   union U { int k; S s; };
>
>   constexpr int test() {
>     U u {};
>     return u.s.b;
>   }
>   constexpr int x = test();
>
> giving me this pretty unhelpful error message:
>
> /home/ns/main.cpp:8:23:   in ‘constexpr’ expansion of ‘test()’
> /home/ns/main.cpp:6:12: error: use of deleted function ‘S::S()’
>     6 |   return u.s.b;
>       |          ~~^
> /home/ns/main.cpp:1:8: note: ‘S::S()’ is implicitly deleted because the default definition would be ill-formed:
>     1 | struct S { const int a; int b; };
>       |        ^
> /home/ns/main.cpp:1:8: error: uninitialised const member in ‘struct S’
> /home/ns/main.cpp:1:22: note: ‘const int S::a’ should be initialised
>     1 | struct S { const int a; int b; };
>       |                      ^
> /home/ns/main.cpp:8:23:   in ‘constexpr’ expansion of ‘test()’
> /home/ns/main.cpp:6:12: error: use of deleted function ‘S::S()’
>     6 |   return u.s.b;
>       |          ~~^
> /home/ns/main.cpp:8:23:   in ‘constexpr’ expansion of ‘test()’
> /home/ns/main.cpp:6:12: error: use of deleted function ‘S::S()’
>
> but I'll try and fix this separately (it exists on current trunk without
> this patch as well).

While attempting to fix this I found a much better way of handling
value-initialised unions. Here's a new version of the patch which also
includes the fix for accessing the wrong member of a value-initialised
union as well.

Additionally includes an `auto_diagnostic_group` for the `inform`
diagnostics as Marek helpfully informed me about in my other patch.

Bootstrapped and regtested on x86_64-pc-linux-gnu.

-- >8 --

This patch adds checks for attempting to change the active member of a
union by methods other than a member access expression.

To be able to properly distinguish `*(&u.a) = ` from `u.a = `, this
patch redoes the solution for c++/59950 to avoid extranneous *&; it
seems that the only case that needed the workaround was when copying
empty classes.

This patch also ensures that constructors for a union field mark that
field as the active member before entering the call itself; this ensures
that modifications of the field within the constructor's body don't
cause false positives (as these will not appear to be member access
expressions). This means that we no longer need to start the lifetime of
empty union members after the constructor body completes.

As a drive-by fix, this patch also ensures that value-initialised unions
are considered to have activated their initial member for the purpose of
checking stores and accesses, which catches some additional mistakes
pre-C++20.

	PR c++/101631
	PR c++/102286

gcc/cp/ChangeLog:

	* call.cc (build_over_call): Fold more indirect refs for trivial
	assignment op.
	* class.cc (type_has_non_deleted_trivial_default_ctor): Create.
	* constexpr.cc (cxx_eval_call_expression): Start lifetime of
	union member before entering constructor.
        (cxx_eval_component_reference): Check against first member of
        value-initialised union.
	(cxx_eval_store_expression): Activate member for
	value-initialised union. Check for accessing inactive union
	member indirectly.
	* cp-tree.h (type_has_non_deleted_trivial_default_ctor):
	Forward declare.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/constexpr-89336-3.C: Fix union initialisation.
	* g++.dg/cpp1y/constexpr-union6.C: New test.
	* g++.dg/cpp1y/constexpr-union7.C: New test.
	* g++.dg/cpp2a/constexpr-union2.C: New test.
	* g++.dg/cpp2a/constexpr-union3.C: New test.
	* g++.dg/cpp2a/constexpr-union4.C: New test.
	* g++.dg/cpp2a/constexpr-union5.C: New test.
	* g++.dg/cpp2a/constexpr-union6.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
  • Loading branch information
wreien authored and ouuleilei-bot committed Oct 12, 2023
1 parent c2d62cd commit 6d98efc
Show file tree
Hide file tree
Showing 12 changed files with 402 additions and 52 deletions.
11 changes: 6 additions & 5 deletions gcc/cp/call.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10322,18 +10322,19 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain)
&& DECL_OVERLOADED_OPERATOR_IS (fn, NOP_EXPR)
&& trivial_fn_p (fn))
{
/* Don't use cp_build_fold_indirect_ref, op= returns an lvalue even if
the object argument isn't one. */
tree to = cp_build_indirect_ref (input_location, argarray[0],
RO_ARROW, complain);
tree to = cp_build_fold_indirect_ref (argarray[0]);
tree type = TREE_TYPE (to);
tree as_base = CLASSTYPE_AS_BASE (type);
tree arg = argarray[1];
location_t loc = cp_expr_loc_or_input_loc (arg);

if (is_really_empty_class (type, /*ignore_vptr*/true))
{
/* Avoid copying empty classes. */
/* Avoid copying empty classes, but ensure op= returns an lvalue even
if the object argument isn't one. This isn't needed in other cases
since MODIFY_EXPR is always considered an lvalue. */
to = cp_build_addr_expr (to, tf_none);
to = cp_build_indirect_ref (input_location, to, RO_ARROW, complain);
val = build2 (COMPOUND_EXPR, type, arg, to);
suppress_warning (val, OPT_Wunused);
}
Expand Down
8 changes: 8 additions & 0 deletions gcc/cp/class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5651,6 +5651,14 @@ type_has_virtual_destructor (tree type)
return (dtor && DECL_VIRTUAL_P (dtor));
}

/* True iff class TYPE has a non-deleted trivial default
constructor. */

bool type_has_non_deleted_trivial_default_ctor (tree type)
{
return TYPE_HAS_TRIVIAL_DFLT (type) && locate_ctor (type);
}

/* Returns true iff T, a class, has a move-assignment or
move-constructor. Does not lazily declare either.
If USER_P is false, any move function will do. If it is true, the
Expand Down
164 changes: 118 additions & 46 deletions gcc/cp/constexpr.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3095,40 +3095,34 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t,
cxx_set_object_constness (ctx, new_obj, /*readonly_p=*/false,
non_constant_p, overflow_p);

/* If this is a constructor, we are beginning the lifetime of the
object we are initializing. */
if (new_obj
&& DECL_CONSTRUCTOR_P (fun)
&& TREE_CODE (new_obj) == COMPONENT_REF
&& TREE_CODE (TREE_TYPE (TREE_OPERAND (new_obj, 0))) == UNION_TYPE)
{
tree activate = build2 (INIT_EXPR, TREE_TYPE (new_obj),
new_obj,
build_constructor (TREE_TYPE (new_obj),
NULL));
cxx_eval_constant_expression (ctx, activate,
lval, non_constant_p, overflow_p);
ggc_free (activate);
}

tree jump_target = NULL_TREE;
cxx_eval_constant_expression (&ctx_with_save_exprs, body,
vc_discard, non_constant_p, overflow_p,
&jump_target);

if (DECL_CONSTRUCTOR_P (fun))
{
/* This can be null for a subobject constructor call, in
which case what we care about is the initialization
side-effects rather than the value. We could get at the
value by evaluating *this, but we don't bother; there's
no need to put such a call in the hash table. */
result = lval ? ctx->object : ctx->ctor;

/* If we've just evaluated a subobject constructor call for an
empty union member, it might not have produced a side effect
that actually activated the union member. So produce such a
side effect now to ensure the union appears initialized. */
if (!result && new_obj
&& TREE_CODE (new_obj) == COMPONENT_REF
&& TREE_CODE (TREE_TYPE
(TREE_OPERAND (new_obj, 0))) == UNION_TYPE
&& is_really_empty_class (TREE_TYPE (new_obj),
/*ignore_vptr*/false))
{
tree activate = build2 (MODIFY_EXPR, TREE_TYPE (new_obj),
new_obj,
build_constructor (TREE_TYPE (new_obj),
NULL));
cxx_eval_constant_expression (ctx, activate, lval,
non_constant_p, overflow_p);
ggc_free (activate);
}
}
/* This can be null for a subobject constructor call, in
which case what we care about is the initialization
side-effects rather than the value. We could get at the
value by evaluating *this, but we don't bother; there's
no need to put such a call in the hash table. */
result = lval ? ctx->object : ctx->ctor;
else if (VOID_TYPE_P (TREE_TYPE (res)))
result = void_node;
else
Expand Down Expand Up @@ -4351,6 +4345,7 @@ cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
*non_constant_p = true;
return t;
}

bool pmf = TYPE_PTRMEMFUNC_P (TREE_TYPE (whole));
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (whole), i, field, value)
{
Expand All @@ -4369,21 +4364,36 @@ cxx_eval_component_reference (const constexpr_ctx *ctx, tree t,
break;
}
}
if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE
&& CONSTRUCTOR_NELTS (whole) > 0)
if (TREE_CODE (TREE_TYPE (whole)) == UNION_TYPE)
{
/* DR 1188 says we don't have to deal with this. */
if (!ctx->quiet)
if (CONSTRUCTOR_NELTS (whole) > 0)
{
constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
if (cep->value == NULL_TREE)
error ("accessing uninitialized member %qD", part);
else
error ("accessing %qD member instead of initialized %qD member in "
"constant expression", part, cep->index);
/* DR 1188 says we don't have to deal with this. */
if (!ctx->quiet)
{
constructor_elt *cep = CONSTRUCTOR_ELT (whole, 0);
if (cep->value == NULL_TREE)
error ("accessing uninitialized member %qD", part);
else
error ("accessing %qD member instead of initialized %qD member in "
"constant expression", part, cep->index);
}
*non_constant_p = true;
return t;
}
else if (!CONSTRUCTOR_NO_CLEARING (whole))
{
/* Value-initialized union, check if looking at the first member. */
tree first = next_aggregate_field (TYPE_FIELDS (TREE_TYPE (whole)));
if (pmf ? DECL_NAME (first) != DECL_NAME (part) : first != part)
{
if (!ctx->quiet)
error ("accessing %qD member instead of initialized %qD "
"member in constant expression", part, first);
*non_constant_p = true;
return t;
}
}
*non_constant_p = true;
return t;
}

/* We only create a CONSTRUCTOR for a subobject when we modify it, so empty
Expand Down Expand Up @@ -5966,6 +5976,14 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
mutable_p)
&& const_object_being_modified == NULL_TREE)
const_object_being_modified = probe;

/* Track named member accesses for unions to validate modifications
that change active member. */
if (!evaluated && TREE_CODE (probe) == COMPONENT_REF)
vec_safe_push (refs, probe);
else
vec_safe_push (refs, NULL_TREE);

vec_safe_push (refs, elt);
vec_safe_push (refs, TREE_TYPE (probe));
probe = ob;
Expand All @@ -5974,13 +5992,15 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,

case REALPART_EXPR:
gcc_assert (probe == target);
vec_safe_push (refs, NULL_TREE);
vec_safe_push (refs, probe);
vec_safe_push (refs, TREE_TYPE (probe));
probe = TREE_OPERAND (probe, 0);
break;

case IMAGPART_EXPR:
gcc_assert (probe == target);
vec_safe_push (refs, NULL_TREE);
vec_safe_push (refs, probe);
vec_safe_push (refs, TREE_TYPE (probe));
probe = TREE_OPERAND (probe, 0);
Expand Down Expand Up @@ -6069,6 +6089,7 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
enum tree_code code = TREE_CODE (type);
tree reftype = refs->pop();
tree index = refs->pop();
bool is_access_expr = refs->pop() != NULL_TREE;

if (code == COMPLEX_TYPE)
{
Expand Down Expand Up @@ -6107,23 +6128,74 @@ cxx_eval_store_expression (const constexpr_ctx *ctx, tree t,
break;
}

/* Process value-initialization of a union. */
if (code == UNION_TYPE
&& !CONSTRUCTOR_NO_CLEARING (*valp)
&& CONSTRUCTOR_NELTS (*valp) == 0)
if (tree first = next_aggregate_field (TYPE_FIELDS (type)))
CONSTRUCTOR_APPEND_ELT (CONSTRUCTOR_ELTS (*valp), first, NULL_TREE);

type = reftype;

if (code == UNION_TYPE && CONSTRUCTOR_NELTS (*valp)
&& CONSTRUCTOR_ELT (*valp, 0)->index != index)
/* Check for implicit change of active member for a union. */
if (code == UNION_TYPE
&& (CONSTRUCTOR_NELTS (*valp) == 0
|| CONSTRUCTOR_ELT (*valp, 0)->index != index)
/* An INIT_EXPR of the last member in an access chain is always OK,
but still check implicit change of members earlier on; see
cpp2a/constexpr-union6.C. */
&& !(TREE_CODE (t) == INIT_EXPR && refs->is_empty ()))
{
if (cxx_dialect < cxx20)
bool has_active_member = CONSTRUCTOR_NELTS (*valp) != 0;
tree inner = strip_array_types (reftype);

if (has_active_member && cxx_dialect < cxx20)
{
if (!ctx->quiet)
error_at (cp_expr_loc_or_input_loc (t),
"change of the active member of a union "
"from %qD to %qD",
"from %qD to %qD is not a constant expression "
"before C++20",
CONSTRUCTOR_ELT (*valp, 0)->index,
index);
*non_constant_p = true;
}
else if (TREE_CODE (t) == MODIFY_EXPR
&& CONSTRUCTOR_NO_CLEARING (*valp))
else if (!is_access_expr
|| (TREE_CODE (t) == MODIFY_EXPR
&& CLASS_TYPE_P (inner)
&& !type_has_non_deleted_trivial_default_ctor (inner)))
{
/* Diagnose changing active union member after initialization
without a valid member access expression, as described in
[class.union.general] p5. */
if (!ctx->quiet)
{
auto_diagnostic_group d;
if (has_active_member)
error_at (cp_expr_loc_or_input_loc (t),
"accessing %qD member instead of initialized "
"%qD member in constant expression",
index, CONSTRUCTOR_ELT (*valp, 0)->index);
else
error_at (cp_expr_loc_or_input_loc (t),
"accessing uninitialized member %qD",
index);
if (is_access_expr)
inform (DECL_SOURCE_LOCATION (index),
"%qD does not implicitly begin its lifetime "
"because %qT does not have a non-deleted "
"trivial default constructor, use "
"%<std::construct_at%> instead",
index, inner);
else
inform (DECL_SOURCE_LOCATION (index),
"initializing %qD requires a member access "
"expression as the left operand of the assignment",
index);
}
*non_constant_p = true;
}
else if (has_active_member && CONSTRUCTOR_NO_CLEARING (*valp))
{
/* Diagnose changing the active union member while the union
is in the process of being initialized. */
Expand Down
1 change: 1 addition & 0 deletions gcc/cp/cp-tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -6759,6 +6759,7 @@ extern bool trivial_default_constructor_is_constexpr (tree);
extern bool type_has_constexpr_default_constructor (tree);
extern bool type_has_constexpr_destructor (tree);
extern bool type_has_virtual_destructor (tree);
extern bool type_has_non_deleted_trivial_default_ctor (tree);
extern bool classtype_has_move_assign_or_move_ctor_p (tree, bool user_declared);
extern bool classtype_has_non_deleted_move_ctor (tree);
extern tree classtype_has_depr_implicit_copy (tree);
Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/g++.dg/cpp1y/constexpr-89336-3.C
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ constexpr int
bar ()
{
union U { int a[5]; long b; };
union V { union U u; short v; };
union V { short v; union U u; };
V w {};
w.v = 5;
w.u.a[3] = w.u.a[1] = w.v; // { dg-error "change of the active member of a union from" "" { target c++17_down } }
Expand Down
13 changes: 13 additions & 0 deletions gcc/testsuite/g++.dg/cpp1y/constexpr-union6.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// { dg-do compile { target c++14 } }

union U {
int a;
float b;
};

constexpr bool foo() {
U u {};
u.b = 1.0f; // { dg-error "change of the active member" "" { target c++17_down } }
return u.b == 1.0f;
}
constexpr bool x = foo();
18 changes: 18 additions & 0 deletions gcc/testsuite/g++.dg/cpp1y/constexpr-union7.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// { dg-do compile { target c++14 } }

// this type is not value-initialisable
struct S { const int a; int b; };

union U1 { int k; S s; };
constexpr int test1() {
U1 u {};
return u.s.b; // { dg-error "accessing .U1::s. member instead of initialized .U1::k. member" }
}
constexpr int x = test1();

union U2 { int :0; static int s; void foo(); int k; };
constexpr int test2() {
U2 u {}; // should skip zero-width bitfields, static members, and functions
return u.k;
}
static_assert(test2() == 0, "");
30 changes: 30 additions & 0 deletions gcc/testsuite/g++.dg/cpp2a/constexpr-union2.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// PR c++/101631
// { dg-do compile { target c++20 } }

struct sso {
union {
int buf[10];
int* alloc;
};
};

constexpr bool direct() {
sso val;
val.alloc = nullptr;
val.buf[5] = 42;
return true;
}
constexpr bool ok = direct();


constexpr void perform_assignment(int& left, int right) noexcept {
left = right; // { dg-error "accessing .+ member instead of initialized" }
}

constexpr bool indirect() {
sso val;
val.alloc = nullptr;
perform_assignment(val.buf[5], 42); // { dg-message "in .constexpr. expansion" }
return true;
}
constexpr bool err = indirect(); // { dg-message "in .constexpr. expansion" }
Loading

0 comments on commit 6d98efc

Please sign in to comment.