public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults.
@ 2022-01-04 22:09 johannes.kalmbach at googlemail dot com
  2022-01-05  8:14 ` [Bug c++/103909] " iains at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: johannes.kalmbach at googlemail dot com @ 2022-01-04 22:09 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103909
           Summary: co_yield of aggregate-initialized temporaries leads to
                    segmentation faults.
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: johannes.kalmbach at googlemail dot com
  Target Milestone: ---

co_yield of aggregate-initialized temporaries leads to segmentation faults.

Expected behavior:
Let `generator` be a reasonably defined generator type (e.g.
cppcoro::generator). Let `T` be an arbitrary type.
The following pattern is supposed to work:

generator<T> f() {
  co_yield T{<valid-init-statement-for-T>};
}

(the lifetime of the temporary crosses the suspension point inside `co_yield`.

Actual behavior (for different types):

-  Aggregates containing arithmetic types work as expected 
   (e.g. std::array<int, N>, or struct F{int i;};

-  Types where {}-initialization uses a initializer-list constructor lead to a 
   compilation error, but is is already reported as 
   https://gcc.gnu.org/bugzilla/show_bug.cgi?id=98056

-  Aggregates containing std::string (e.g. std::array<std::string, N>, or
struct 
   F{std::string s};) compile, but lead to free(): invalid pointer, 
   munmap_chunk(): invalid pointer, or segmentation faults.

-  Types with exactly the same layouts as these aggregates, but with an
explicit 
   constructor which disables aggregate initialization work correctly, e.g.
   struct G {
     std::string s;
     G(std::string s_in) : s{std::move(s_in)} {} 
   };

GCC version that show this behavior: 11.1, 11.2, trunk (via godbolt)
Compiler Flags: -fcoroutines -std=c++2a -O2

minimal Example code that illustrates the bug (also printed below):
https://godbolt.org/z/nrGG5zKjq

All of the above cases compile and work as expected on clang 13.

Best regards
Johannes


Source code of minimal example (identical to godbolt link)

#include <iostream>
#include <coroutine>
#include <array>

using namespace std;

template <typename T>
struct generator {
  struct promise_type {

  generator get_return_object() noexcept {
  return generator{coroutine_handle<promise_type>::from_promise(*this)};
  }

  suspend_always initial_suspend() const noexcept { return {};}
  suspend_always final_suspend() const noexcept {return {};}

  suspend_always yield_value(T& v) noexcept {m_v = &v; return {};}
  suspend_always yield_value(T&& v) noexcept {m_v = &v; return {}; }
  void unhandled_exception() { }
  void return_void() {}

  T& value() const noexcept { return *m_v; }

 private:
  T* m_v;
};


  ~generator() {m_coroutine.destroy(); }

  void move_next() { m_coroutine.resume();}

  T& value() {return m_coroutine.promise().value();}

  generator(coroutine_handle<promise_type> coroutine) noexcept
      : m_coroutine(coroutine) {}

  std::coroutine_handle<promise_type> m_coroutine;
};


generator<std::array<std::string, 3>> arr(){
    // Compiles, but leads to segfault/ invalid free when accessed.
    co_yield {"a", "b", "c"};
}

generator<std::array<int, 3>> arrInt(){
    // Works fine
     co_yield {1, 2, 3};
}

struct F {
    std::string x;
    const std::string& operator[](size_t) const {
        return x;
    }
};

generator<F> f() {
    // leads to "munmap_chunk(): invalid pointer";
    co_yield {"abc"};
}

struct G {
    std::string s;
    G(std::string s_in) : s{std::move(s_in)} {} 
    const std::string& operator[](size_t) const {
        return s;
    }
};

generator<G> g() {
    // Works as expected, only difference to F/f() is the manually
    // specified constructor.
    co_yield {"abc"};
}


template<typename Generator>
void outputOne(Generator g) {
    g.move_next();
    const auto& el = g.value();
    std::cout << el[0] << el[1] << el[2] << std::endl;
}

int main() {
    outputOne(g());
    outputOne(f());
    outputOne(arrInt());
    outputOne(arr());
}

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

* [Bug c++/103909] co_yield of aggregate-initialized temporaries leads to segmentation faults.
  2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
@ 2022-01-05  8:14 ` iains at gcc dot gnu.org
  2022-04-21  7:51 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: iains at gcc dot gnu.org @ 2022-01-05  8:14 UTC (permalink / raw)
  To: gcc-bugs

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

Iain Sandoe <iains at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |wrong-code
   Last reconfirmed|                            |2022-01-05
     Ever confirmed|0                           |1
   Target Milestone|---                         |11.3

--- Comment #1 from Iain Sandoe <iains at gcc dot gnu.org> ---
thanks for the report and the reproducer.

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

* [Bug c++/103909] co_yield of aggregate-initialized temporaries leads to segmentation faults.
  2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
  2022-01-05  8:14 ` [Bug c++/103909] " iains at gcc dot gnu.org
@ 2022-04-21  7:51 ` rguenth at gcc dot gnu.org
  2023-04-22 19:39 ` [Bug c++/103909] coroutines: " StevenSun2021 at hotmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-04-21  7:51 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.3                        |11.4

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 11.3 is being released, retargeting bugs to GCC 11.4.

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

* [Bug c++/103909] coroutines: co_yield of aggregate-initialized temporaries leads to segmentation faults.
  2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
  2022-01-05  8:14 ` [Bug c++/103909] " iains at gcc dot gnu.org
  2022-04-21  7:51 ` rguenth at gcc dot gnu.org
@ 2023-04-22 19:39 ` StevenSun2021 at hotmail dot com
  2023-05-29 10:06 ` jakub at gcc dot gnu.org
  2024-05-06  6:28 ` accelerator0099 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: StevenSun2021 at hotmail dot com @ 2023-04-22 19:39 UTC (permalink / raw)
  To: gcc-bugs

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

Steven Sun <StevenSun2021 at hotmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |StevenSun2021 at hotmail dot com

--- Comment #3 from Steven Sun <StevenSun2021 at hotmail dot com> ---
seems that 103909, 104384, 107288 are related (probably the same bug)

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

* [Bug c++/103909] coroutines: co_yield of aggregate-initialized temporaries leads to segmentation faults.
  2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
                   ` (2 preceding siblings ...)
  2023-04-22 19:39 ` [Bug c++/103909] coroutines: " StevenSun2021 at hotmail dot com
@ 2023-05-29 10:06 ` jakub at gcc dot gnu.org
  2024-05-06  6:28 ` accelerator0099 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-29 10:06 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.4                        |11.5

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 11.4 is being released, retargeting bugs to GCC 11.5.

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

* [Bug c++/103909] coroutines: co_yield of aggregate-initialized temporaries leads to segmentation faults.
  2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
                   ` (3 preceding siblings ...)
  2023-05-29 10:06 ` jakub at gcc dot gnu.org
@ 2024-05-06  6:28 ` accelerator0099 at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: accelerator0099 at gmail dot com @ 2024-05-06  6:28 UTC (permalink / raw)
  To: gcc-bugs

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

accelerator0099 at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |accelerator0099 at gmail dot com

--- Comment #5 from accelerator0099 at gmail dot com ---
No error on GCC 13.2

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

end of thread, other threads:[~2024-05-06  6:28 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-04 22:09 [Bug c++/103909] New: co_yield of aggregate-initialized temporaries leads to segmentation faults johannes.kalmbach at googlemail dot com
2022-01-05  8:14 ` [Bug c++/103909] " iains at gcc dot gnu.org
2022-04-21  7:51 ` rguenth at gcc dot gnu.org
2023-04-22 19:39 ` [Bug c++/103909] coroutines: " StevenSun2021 at hotmail dot com
2023-05-29 10:06 ` jakub at gcc dot gnu.org
2024-05-06  6:28 ` accelerator0099 at gmail dot com

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).