public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "iains at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/95111] coroutines use-after-free with lambdas
Date: Wed, 13 May 2020 19:39:33 +0000	[thread overview]
Message-ID: <bug-95111-4-wppD3UgYCD@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-95111-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #6 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Avi Kivity from comment #5)
> This snippet from cppreference:
> 
>   If the coroutine is a non-static member function, such as task<void> 
>   my_class::method1(int x) const;, its Promise type is 
>   std::coroutine_traits<task<void>, const my_class&, int>::promise_type
> 
> implies that both gcc and my interpretation are wrong. gcc passes a pointer
> for the lambda, and I'd like the lambda to be passed by value. The
> difference between gcc and cppreference is trivial, and both of them make
> coroutine lambdas unusable if they contain state and if the lambda is
> asynchronous.

( assuming that this is the problem, I haven't yet had a chance to check *
could still be a bug ;) ).

I have a pending change to pass a reference to the lambda object to the traits,
promise preview and allocator lookups.

This was a source of considerable discussion amongst the implementors (GCC,
clang, MSVC) about how the std should be interpreted.  The change I mention
will make the lambda capture object pointer behave in the same manner as 'this'
(which was the intended behaviour as determined by the long discussion).  MSVC
implements this now, and clang will be changing to match it also.

That won't solve any lifetime issue with the capture object - you will still
need to ensure that it exists for the duration of the coroutine * as you would
for a class instance with a method coroutine *.

We (at least several of us) agree that this is a source of easy programming
errors - and I expect there to be some revisiting in c++23.  That's a
considerable challenge in the face of a mutable lambda - maybe (thinking aloud)
needs something like Apple blocks, but with an automatic promotion of the
closure to a heap object if it escapes the creating scope.

  parent reply	other threads:[~2020-05-13 19:39 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-13 17:36 [Bug c++/95111] New: " avi@cloudius-systems.com
2020-05-13 19:12 ` [Bug c++/95111] " iains at gcc dot gnu.org
2020-05-13 19:14 ` avi@cloudius-systems.com
2020-05-13 19:15 ` avi@cloudius-systems.com
2020-05-13 19:21 ` iains at gcc dot gnu.org
2020-05-13 19:27 ` avi@cloudius-systems.com
2020-05-13 19:39 ` iains at gcc dot gnu.org [this message]
2020-05-13 20:10 ` avi@cloudius-systems.com
2020-05-13 20:11 ` avi@cloudius-systems.com
2020-05-13 20:17 ` iains at gcc dot gnu.org
2020-05-13 20:24 ` avi@cloudius-systems.com
2020-05-14 11:15 ` avi@cloudius-systems.com
2020-05-14 11:24 ` ville.voutilainen at gmail dot com
2020-05-14 11:29 ` avi@cloudius-systems.com
2020-05-14 11:46 ` iains at gcc dot gnu.org
2020-05-14 11:54 ` avi@cloudius-systems.com
2020-05-14 11:57 ` ville.voutilainen at gmail dot com
2020-05-14 11:59 ` ville.voutilainen at gmail dot com
2020-05-14 12:01 ` avi@cloudius-systems.com
2020-05-14 12:02 ` iains at gcc dot gnu.org
2020-05-14 12:02 ` avi@cloudius-systems.com
2020-05-14 12:25 ` iains at gcc dot gnu.org
2020-05-14 12:33 ` avi@cloudius-systems.com
2020-05-14 12:34 ` avi@cloudius-systems.com
2020-05-14 18:02 ` egallager at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-95111-4-wppD3UgYCD@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).