https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126746

--- Comment #4 from Yuxuan Chen <i at yuxuan dot ch> ---
(In reply to Ted Rodgers from comment #3)
> Failing to clear the cleanup on the temporary TARGET_EXPR causes the
> moved-from temporary's destructor to run and release resources that now
> belong to the destination
> 
> This patch sets TARGET_EXPR_CLEANUP to NULL_TREE for [[trivial_abi]]
> arguments in call.cc and return values in typeck.cc to prevent redundant
> destruction:
> 
> 
> --- a/gcc/cp/call.cc
> +++ b/gcc/cp/call.cc
> @@ -9896,6 +9896,14 @@
>         && TREE_CODE (TARGET_EXPR_INITIAL (val)) != CONSTRUCTOR)
>       set_target_expr_eliding (val);
>  
> +  /* An argument with [[trivial_abi]] is consumed by the callee,
> transferring
> +     ownership; the caller's copy must not be cleaned up (PR c++/126746). 
> */
> +  if (type
> +      && has_trivial_abi_attribute (type)
> +      && TREE_CODE (val) == TARGET_EXPR)
> +    TARGET_EXPR_CLEANUP (val) = NULL_TREE;
> +
>     return val;
>   }
>  
> --- a/gcc/cp/typeck.cc
> +++ b/gcc/cp/typeck.cc
> @@ -11994,8 +11994,15 @@
>         else if (! cfun->returns_struct
>                && TREE_CODE (retval) == TARGET_EXPR
>                && TREE_CODE (TARGET_EXPR_INITIAL (retval)) == AGGR_INIT_EXPR)
> -     retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
> -                      TARGET_EXPR_SLOT (retval));
> +     {
> +       /* A returned object with [[trivial_abi]] is consumed by the caller,
> +          transferring ownership; the temporary's destructor must not run 
> +          and invalidate the moved-from resource (PR c++/126746).  */
> +       if (has_trivial_abi_attribute (TREE_TYPE (retval)))
> +         TARGET_EXPR_CLEANUP (retval) = NULL_TREE;
> +       retval = build2 (COMPOUND_EXPR, TREE_TYPE (retval), retval,
> +                        TARGET_EXPR_SLOT (retval));
> +     }
>         else if (!processing_template_decl
>                && maybe_warn_about_returning_address_of_local (retval, loc)
>                && INDIRECT_TYPE_P (valtype))
> 
> 
> 
> 
> 
> 
> AI was used, but with patch applied, multiple testcases passed:
>     testcase in this bug
>     testcase from Bug 128661
>     successful compile of qtwebengine-6.11.3
>         (previously failed from same bug)
>     this additional testcase:
> 
> 
> int deleted = 0;
> 
> struct P {
>   int ref_ = 0;
>   void AddRef() { ++ref_; }
>   void Release() { if (--ref_ == 0) delete this; }
>   ~P() { deleted = 1; }
> };
> 
> struct __attribute__((trivial_abi)) S {
>   P* ptr_ = nullptr;
>   S(P* p) : ptr_(p) { if (ptr_) ptr_->AddRef(); }
>   S(S&& r) : ptr_(r.ptr_) { r.ptr_ = nullptr; }
>   ~S() { if (ptr_) ptr_->Release(); }
> };
> 
> S Make() { return S(new P); }
> 
> int main() {
>   S m = Make();
>   if (deleted || m.ptr_->ref_ != 1)
>     __builtin_trap();   // m.ptr_ was deleted by the consumed temporary
>   return 0;
> }

Thank you, Ted, for attempting at a fix. Do you plan to send this to the
mailing list for review or is this already done?

Reply via email to