From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1873) id DF4DE3858C20; Mon, 12 Feb 2024 16:08:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DF4DE3858C20 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1707754108; bh=o6OMmCmjnk6xhfCGJ7INpefCkR5O8HdJioD5PK03doA=; h=From:To:Subject:Date:From; b=GjUu4bIbHEiF036y5p7Bc+W+LCYWNkQ2CzUBFxVzme0tDB/8DLjKyTgh1sUIXk35b hsoG7x7/uj73NZgvC3Cc1IAtyZKQ3EwrJgF6PPoPcVDVgmrKWIRj4bpfqsgfeV5HqM y4B6QnQWEr1rrLlkwPwRX4FMA4mFXjCSawfSIiho= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Iain Buclaw To: gcc-cvs@gcc.gnu.org Subject: [gcc r14-8933] d: Fix callee destructor call invalidates the live object [PR113758] X-Act-Checkin: gcc X-Git-Author: Iain Buclaw X-Git-Refname: refs/heads/master X-Git-Oldrev: 1fcaa3a8225885a93d537025cb071651c13235f7 X-Git-Newrev: 3c57b1c12a8e34d50bdf6aaf44146760db6d1b33 Message-Id: <20240212160828.DF4DE3858C20@sourceware.org> Date: Mon, 12 Feb 2024 16:08:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:3c57b1c12a8e34d50bdf6aaf44146760db6d1b33 commit r14-8933-g3c57b1c12a8e34d50bdf6aaf44146760db6d1b33 Author: Iain Buclaw Date: Sun Feb 4 22:04:14 2024 +0100 d: Fix callee destructor call invalidates the live object [PR113758] When generating the argument, check the isCalleeDestroyingArgs hook, and force a TARGET_EXPR to be created if true, so that a reference to the live object isn't passed directly to the function that runs dtors. When instead dealing with caller running destructors, two temporaries were being generated, one explicit temporary generated by the D front-end, and another implicitly by the code generator. This has been reduced to one by setting DECL_VALUE_EXPR on the explicit temporary to bind it to the implicit slot created for the TARGET_EXPR, as that has the shorter lifetime of the two. PR d/113758 gcc/d/ChangeLog: * d-codegen.cc (d_build_call): Force a TARGET_EXPR when callee destorys its arguments. * decl.cc (DeclVisitor::visit (VarDeclaration *)): Set SET_DECL_VALUE_EXPR on the temporary variable to make it a placeholder for the TARGET_EXPR_SLOT. gcc/testsuite/ChangeLog: * gdc.dg/torture/pr113758.d: New test. Diff: --- gcc/d/d-codegen.cc | 15 +++++++++++---- gcc/d/decl.cc | 22 ++++++++++++++++++++-- gcc/testsuite/gdc.dg/torture/pr113758.d | 19 +++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/gcc/d/d-codegen.cc b/gcc/d/d-codegen.cc index 95dc8b6327ef..dc528164aaff 100644 --- a/gcc/d/d-codegen.cc +++ b/gcc/d/d-codegen.cc @@ -2270,10 +2270,17 @@ d_build_call (TypeFunction *tf, tree callable, tree object, Type *t = arg->type->toBasetype (); StructDeclaration *sd = t->baseElemOf ()->isTypeStruct ()->sym; - /* Nested structs also have ADDRESSABLE set, but if the type has - neither a copy constructor nor a destructor available, then we - need to take care of copying its value before passing it. */ - if (arg->op == EXP::structLiteral || (!sd->postblit && !sd->dtor)) + /* Need to take care of copying its value before passing the + argument in the following scenarios: + - The argument is a literal expression; a CONSTRUCTOR can't + have its address taken. + - The type has neither a copy constructor nor a destructor + available; nested structs also have ADDRESSABLE set. + - The ABI of the function expects the callee to destroy its + arguments; when the caller is handles destruction, then `targ' + has already been made into a temporary. */ + if (arg->op == EXP::structLiteral || (!sd->postblit && !sd->dtor) + || target.isCalleeDestroyingArgs (tf)) targ = force_target_expr (targ); targ = convert (build_reference_type (TREE_TYPE (targ)), diff --git a/gcc/d/decl.cc b/gcc/d/decl.cc index d2e84d3ba619..827495b3e303 100644 --- a/gcc/d/decl.cc +++ b/gcc/d/decl.cc @@ -863,10 +863,28 @@ public: /* Maybe put variable on list of things needing destruction. */ if (d->needsScopeDtor ()) { + /* Rewrite: `decl = exp' => TARGET_EXPR(decl, exp, dtor). */ vec_safe_push (d_function_chain->vars_in_scope, decl); + /* Force a TARGET_EXPR to add the corresponding cleanup. */ - exp = force_target_expr (compound_expr (exp, decl)); - TARGET_EXPR_CLEANUP (exp) = build_expr (d->edtor); + if (TREE_CODE (exp) != TARGET_EXPR) + { + if (VOID_TYPE_P (TREE_TYPE (exp))) + exp = compound_expr (exp, decl); + + exp = force_target_expr (exp); + } + + TARGET_EXPR_CLEANUP (exp) + = compound_expr (TARGET_EXPR_CLEANUP (exp), + build_expr (d->edtor)); + + /* The decl is really an alias for the TARGET_EXPR slot. */ + SET_DECL_VALUE_EXPR (decl, TARGET_EXPR_SLOT (exp)); + DECL_HAS_VALUE_EXPR_P (decl) = 1; + /* This tells the gimplifier not to emit a clobber for the decl + as its lifetime ends when the slot gets cleaned up. */ + TREE_ADDRESSABLE (decl) = 0; } add_stmt (exp); diff --git a/gcc/testsuite/gdc.dg/torture/pr113758.d b/gcc/testsuite/gdc.dg/torture/pr113758.d new file mode 100644 index 000000000000..dc53883a8dea --- /dev/null +++ b/gcc/testsuite/gdc.dg/torture/pr113758.d @@ -0,0 +1,19 @@ +// { dg-do run } +// { dg-skip-if "needs gcc/config.d" { ! d_runtime } } +struct S113758 +{ + int field; + ~this() { field = 0; } +} + +void main() +{ + auto var = S113758(1); + f113758d(var); + assert(var.field == 1); + f113758cxx(var); + assert(var.field == 1); +} + +extern (D) void f113758d(S113758 arg) { } +extern (C++) void f113758cxx(S113758 arg) { }