public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially
@ 2021-08-19 10:43 alois1@gmx-topmail.de
  2022-08-11 23:05 ` [Bug c++/101976] " dprokoptsev at gmail dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: alois1@gmx-topmail.de @ 2021-08-19 10:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101976
           Summary: When constructing object, calling function and
                    performing co_await in same statement, temporary is
                    erroneously moved trivially
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: alois1@gmx-topmail.de
  Target Milestone: ---

Created attachment 51324
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=51324&action=edit
Reduced preprocessed code

GCC miscompiles the following C++ program:

$ cat test.cpp
#include <coroutine>

struct task
{
        struct promise_type
        {
                task get_return_object() { return
{std::coroutine_handle<promise_type>::from_promise(*this)}; }
                std::suspend_never initial_suspend() { return {}; }
                std::suspend_never final_suspend() noexcept { return {}; }
                void unhandled_exception() {}
                void return_void() {}
        };

        bool await_ready() { return true; }
        void await_suspend(std::coroutine_handle<>) {}
        void await_resume() { }

        std::coroutine_handle<promise_type> m_handle;
};

extern "C" int puts(const char *s);

struct nontrivial_move
{
        nontrivial_move() { puts("nontrivial_move()"); }
        nontrivial_move(nontrivial_move &&) {
puts("nontrivial_move(nontrivial_move &&)"); }
        ~nontrivial_move() { puts("~nontrivial_move()"); }
        char buf[128]{};
};

struct wrapper
{
        nontrivial_move member;
};

task subtask(wrapper)
{
        co_return;
}

task main_task()
{
        co_await subtask({});
}

int main()
{
        main_task();
}

$ gcc -v
Using built-in specs.
COLLECT_GCC=/usr/bin/gcc
COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-redhat-linux
Configured with: ../configure --enable-bootstrap
--enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,lto --prefix=/usr
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared
--enable-threads=posix --enable-checking=release --enable-multilib
--with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions
--enable-gnu-unique-object --enable-linker-build-id
--with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin
--enable-initfini-array
--with-isl=/builddir/build/BUILD/gcc-11.2.1-20210728/obj-x86_64-redhat-linux/isl-install
--enable-offload-targets=nvptx-none --without-cuda-driver
--enable-gnu-indirect-function --enable-cet --with-tune=generic
--with-arch_32=i686 --build=x86_64-redhat-linux
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.1 20210728 (Red Hat 11.2.1-1) (GCC) 

$ g++ -std=c++20 test.cpp; ./a.out
nontrivial_move()
nontrivial_move(nontrivial_move &&)
~nontrivial_move()
~nontrivial_move()
~nontrivial_move()

Examination of the generated assembly code shows that the temporary wrapper
object is erroneously copied under the assumption that it has trivial move
constructor (the large buffer in nontrivial_move is just there to make this
fact obvious).

This behavior does not happen if the construction of the wrapper object or the
co_await happens in a separate statement. Furthermore, the behavior does not
happen if the only argument of subtask is changed to have type nontrivial_move
(the move is elided entirely in this case, which is acceptable as far as I can
tell).

A manually reduced version of the preprocessed code is attached, which I have
confirmed to show the same behavior.

I think that the observed incorrect behavior might be a symptom of the same
underlying issue as one or more of the bugs 99576, 100611, 101243, 101367.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/101976] When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially
  2021-08-19 10:43 [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially alois1@gmx-topmail.de
@ 2022-08-11 23:05 ` dprokoptsev at gmail dot com
  2022-12-04 10:41 ` cvs-commit at gcc dot gnu.org
  2024-02-05 14:18 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: dprokoptsev at gmail dot com @ 2022-08-11 23:05 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Dmitry Prokoptsev <dprokoptsev at gmail dot com> ---
I believe I stumbled on this one as well -- see
https://godbolt.org/z/or31cz6eW, although it's not as trivial as the snippet
provided here.

Reproduces in 10.3 and all subsequent versions.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/101976] When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially
  2021-08-19 10:43 [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially alois1@gmx-topmail.de
  2022-08-11 23:05 ` [Bug c++/101976] " dprokoptsev at gmail dot com
@ 2022-12-04 10:41 ` cvs-commit at gcc dot gnu.org
  2024-02-05 14:18 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-12-04 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Iain D Sandoe <iains@gcc.gnu.org>:

https://gcc.gnu.org/g:58a7b1e354530d8dfe7d8fb859c8b8b5a9140f1f

commit r13-4479-g58a7b1e354530d8dfe7d8fb859c8b8b5a9140f1f
Author: Iain Sandoe <iain@sandoe.co.uk>
Date:   Wed Nov 30 17:05:56 2022 +0000

    coroutines: Do not promote temporaries that will be elided.

    We usually need to 'promote' (i.e. save to the coroutine frame) any
temporary
    variable that is in a target expression that must persist across an await
    expression.  However, if the TE is just used as a direct initializer for
    another object it will be elided - and we should not promote it since that
    would lead to a DTOR call for something that is never constructed.

    Since we now have a mechanism to tell if TEs will be elided, use that.

    Although the PRs referenced initially appear to be different issues, they
all
    stem from this.

    Co-Authored-By: Adrian Perl <adrian.perl@web.de>
    Signed-off-by: Iain Sandoe <iain@sandoe.co.uk>

            PR c++/100611
            PR c++/101367
            PR c++/101976
            PR c++/99576

    gcc/cp/ChangeLog:

            * coroutines.cc (find_interesting_subtree): Do not promote
temporaries
            that are only used as direct initializers for some other object.

    gcc/testsuite/ChangeLog:

            * g++.dg/coroutines/pr100611.C: New test.
            * g++.dg/coroutines/pr101367.C: New test.
            * g++.dg/coroutines/pr101976.C: New test.
            * g++.dg/coroutines/pr99576_1.C: New test.
            * g++.dg/coroutines/pr99576_2.C: New test.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/101976] When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially
  2021-08-19 10:43 [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially alois1@gmx-topmail.de
  2022-08-11 23:05 ` [Bug c++/101976] " dprokoptsev at gmail dot com
  2022-12-04 10:41 ` cvs-commit at gcc dot gnu.org
@ 2024-02-05 14:18 ` redi at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2024-02-05 14:18 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |C++-coroutines
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2024-02-05
                 CC|                            |iains at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Iain, this one seems to be fixed. Should we close it?

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-02-05 14:18 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-19 10:43 [Bug c++/101976] New: When constructing object, calling function and performing co_await in same statement, temporary is erroneously moved trivially alois1@gmx-topmail.de
2022-08-11 23:05 ` [Bug c++/101976] " dprokoptsev at gmail dot com
2022-12-04 10:41 ` cvs-commit at gcc dot gnu.org
2024-02-05 14:18 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).