public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106973] New: coroutine generator and setjmp
@ 2022-09-20  8:00 woohp135 at gmail dot com
  2022-09-20  8:44 ` [Bug c++/106973] " marxin at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: woohp135 at gmail dot com @ 2022-09-20  8:00 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106973
           Summary: coroutine generator and setjmp
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: woohp135 at gmail dot com
  Target Milestone: ---

#include "generator.hpp"
#include <setjmp.h>
using namespace std;


cppcoro::generator<int> foo()
{
    jmp_buf env;
    setjmp(env);
    co_yield 1;
}


int main()
{
    return 0;
}


The generator.hpp file is from
https://github.com/lewissbaker/cppcoro/blob/master/include/cppcoro/generator.hpp

I'm getting this error:


> g++ -std=c++2a -Wall -Wextra -freport-bug foo.cpp
during GIMPLE pass: coro-early-expand-ifns
foo.cpp: In function ‘void foo(foo()::_Z3foov.Frame*)’:
foo.cpp:6:25: internal compiler error: Segmentation fault
    6 | cppcoro::generator<int> foo()
      |                         ^~~
0x19eab38 internal_error(char const*, ...)
        ???:0


I believe it is the setjmp that is causing the issue. This code compiles fine
on clang.

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
@ 2022-09-20  8:44 ` marxin at gcc dot gnu.org
  2022-09-20  8:54 ` woohp135 at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-09-20  8:44 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |WAITING
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-09-20
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #1 from Martin Liška <marxin at gcc dot gnu.org> ---
Please provide a pre-processed source file (-E option).

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
  2022-09-20  8:44 ` [Bug c++/106973] " marxin at gcc dot gnu.org
@ 2022-09-20  8:54 ` woohp135 at gmail dot com
  2022-09-20  9:23 ` marxin at gcc dot gnu.org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: woohp135 at gmail dot com @ 2022-09-20  8:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Hui Peng Hu <woohp135 at gmail dot com> ---
Created attachment 53592
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=53592&action=edit
preprocessed cpp file

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
  2022-09-20  8:44 ` [Bug c++/106973] " marxin at gcc dot gnu.org
  2022-09-20  8:54 ` woohp135 at gmail dot com
@ 2022-09-20  9:23 ` marxin at gcc dot gnu.org
  2022-09-20 10:32 ` iains at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: marxin at gcc dot gnu.org @ 2022-09-20  9:23 UTC (permalink / raw)
  To: gcc-bugs

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |iains at gcc dot gnu.org
             Status|WAITING                     |NEW

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
Thanks, reduced to:

cat preprocessed.cpp
namespace std {
template <typename> using remove_reference_t = int;
template <typename _Result> struct coroutine_traits : _Result {};
template <typename = void> struct coroutine_handle {
  operator coroutine_handle<>();
};
struct suspend_always {
  bool await_ready() noexcept;
  void await_suspend(coroutine_handle<>) noexcept;
  void await_resume() noexcept;
};
} // namespace std
struct generator_promise {
  void get_return_object();
  std::suspend_always initial_suspend();
  std::suspend_always final_suspend() noexcept;
  std::suspend_always yield_value(std::remove_reference_t<int>);
  void unhandled_exception();
};
struct generator {
  using promise_type = generator_promise;
};
void setjmp(int);
int foo_env;
generator foo() {
  setjmp(foo_env);
  co_yield 1;
}

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
                   ` (2 preceding siblings ...)
  2022-09-20  9:23 ` marxin at gcc dot gnu.org
@ 2022-09-20 10:32 ` iains at gcc dot gnu.org
  2022-09-20 10:43 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: iains at gcc dot gnu.org @ 2022-09-20 10:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Iain Sandoe <iains at gcc dot gnu.org> ---
Of course, we should not crash in this case.

However, I am curious as to how you would expect setjmp/longjmp to work with a
coroutine (I recall that the coroutine implementators discussed this with,
AFAIR, the outcome that it was not really supportable).

Do you have an example of a complete code where setjmp/longjmp is used together
with coroutines in clang or MSVC?

AFAICT Lewis' code for the generator makes no use of the setjmp/longjump.

The exception mechanism that *is* defined in the standard for coroutines is the
regular C++ exceptions.

So - we need to fix this problem, of course, but also to revisit what the
correct behaviours should be for setjmp/longjmp.

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
                   ` (3 preceding siblings ...)
  2022-09-20 10:32 ` iains at gcc dot gnu.org
@ 2022-09-20 10:43 ` redi at gcc dot gnu.org
  2022-09-20 10:52 ` iains at gcc dot gnu.org
  2022-09-21  0:33 ` woohp135 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu.org @ 2022-09-20 10:43 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The standard says:

> A call to setjmp or longjmp has undefined behavior if invoked in a suspension context of a coroutine (7.6.2.4).

Given that, I don't see why using setjmp in example would be useful.

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
                   ` (4 preceding siblings ...)
  2022-09-20 10:43 ` redi at gcc dot gnu.org
@ 2022-09-20 10:52 ` iains at gcc dot gnu.org
  2022-09-21  0:33 ` woohp135 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: iains at gcc dot gnu.org @ 2022-09-20 10:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Iain Sandoe <iains at gcc dot gnu.org> ---
(In reply to Jonathan Wakely from comment #5)
> The standard says:
> 
> > A call to setjmp or longjmp has undefined behavior if invoked in a suspension context of a coroutine (7.6.2.4).
> 
> Given that, I don't see why using setjmp in example would be useful.

Exactly, but maybe we should diagnose (we should not crash anyway).

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

* [Bug c++/106973] coroutine generator and setjmp
  2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
                   ` (5 preceding siblings ...)
  2022-09-20 10:52 ` iains at gcc dot gnu.org
@ 2022-09-21  0:33 ` woohp135 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: woohp135 at gmail dot com @ 2022-09-21  0:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Hui Peng Hu <woohp135 at gmail dot com> ---
I was trying to call libjpeg to decode images inside a generator. A snippet of
code that I found online used setjmp/longjmp for error handling, hence I ran
into this issue. Given that the longjmp is called while the coroutine is
executing, I'd imagine that the behavior is fairly well-defined. I've since
moved on to using regular c++ exceptions for error handling, and so this issue
isn't affecting me anymore, but I thought it'll still be nice to get it fixed
(or at least print a helpful warning/error message) so that gcc is not
segfaulting

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

end of thread, other threads:[~2022-09-21  0:33 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-20  8:00 [Bug c++/106973] New: coroutine generator and setjmp woohp135 at gmail dot com
2022-09-20  8:44 ` [Bug c++/106973] " marxin at gcc dot gnu.org
2022-09-20  8:54 ` woohp135 at gmail dot com
2022-09-20  9:23 ` marxin at gcc dot gnu.org
2022-09-20 10:32 ` iains at gcc dot gnu.org
2022-09-20 10:43 ` redi at gcc dot gnu.org
2022-09-20 10:52 ` iains at gcc dot gnu.org
2022-09-21  0:33 ` woohp135 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).