From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7917) id 5AF483858D1E; Mon, 13 Mar 2023 21:38:32 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AF483858D1E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678743512; bh=+xP6HwfejWcEh01bwMLNpJam9zCwUBQdjfXmgdUtNes=; h=From:To:Subject:Date:From; b=mdhzza1qq8etGS9RxKawXP82o6S9B3EDsgmnXHbfH05mPdorYC4l+P1Fwg29GwCzB e1B+beEFJlOtEJBMTneuOCIwlHzwUpANlhowQiMGJxerkV2TJ9dReZoHFTFo9q1LaS vbnhFUrtEOEO/Bp4uxRig7dnyzO/5SGBby7oQcmQ= MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="utf-8" From: =?utf-8?q?Arsen_Arsenovi=C4=87?= To: gcc-cvs@gcc.gnu.org Subject: [gcc r12-9248] c++: top level bind when rewriting coroutines [PR106188] X-Act-Checkin: gcc X-Git-Author: =?utf-8?q?Arsen_Arsenovi=C4=87?= X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: bcf467a1dfa7afcde57f4c132d1b666778145c93 X-Git-Newrev: 775be7d6cb55173950b62c3b9c11ee208cfff29a Message-Id: <20230313213832.5AF483858D1E@sourceware.org> Date: Mon, 13 Mar 2023 21:38:32 +0000 (GMT) List-Id: https://gcc.gnu.org/g:775be7d6cb55173950b62c3b9c11ee208cfff29a commit r12-9248-g775be7d6cb55173950b62c3b9c11ee208cfff29a Author: Arsen Arsenović Date: Sun Sep 4 21:04:23 2022 +0200 c++: top level bind when rewriting coroutines [PR106188] In the edge case of a coroutine not containing any locals, the ifcd/switch temporaries would get added to the coroutine frame, corrupting its layout. To prevent this, we can make sure there is always a BIND_EXPR at the top of the function body, and thus, always a place for our new temporaries to go without interfering with the coroutine frame. PR c++/106188 - Incorrect frame layout after transforming conditional statement without top-level bind expression PR c++/106713 - if (co_await ...) crashes with a jump to ud2 PR c++/106188 PR c++/106713 gcc/cp/ChangeLog: * coroutines.cc (coro_rewrite_function_body): Ensure we have a BIND_EXPR wrapping the function body. gcc/testsuite/ChangeLog: * g++.dg/coroutines/pr106188.C: New test. Diff: --- gcc/cp/coroutines.cc | 9 ++++++++ gcc/testsuite/g++.dg/coroutines/pr106188.C | 34 ++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/gcc/cp/coroutines.cc b/gcc/cp/coroutines.cc index 1d886b31c77..64251a4f087 100644 --- a/gcc/cp/coroutines.cc +++ b/gcc/cp/coroutines.cc @@ -4095,6 +4095,15 @@ coro_rewrite_function_body (location_t fn_start, tree fnbody, tree orig, BLOCK_SUPERCONTEXT (replace_blk) = top_block; BLOCK_SUBBLOCKS (top_block) = replace_blk; } + else + { + /* We are missing a top level BIND_EXPR. We need one to ensure that we + don't shuffle around the coroutine frame and corrupt it. */ + tree bind_wrap = build3_loc (fn_start, BIND_EXPR, void_type_node, + NULL, NULL, NULL); + BIND_EXPR_BODY (bind_wrap) = fnbody; + fnbody = bind_wrap; + } /* Wrap the function body in a try {} catch (...) {} block, if exceptions are enabled. */ diff --git a/gcc/testsuite/g++.dg/coroutines/pr106188.C b/gcc/testsuite/g++.dg/coroutines/pr106188.C new file mode 100644 index 00000000000..9db3778d079 --- /dev/null +++ b/gcc/testsuite/g++.dg/coroutines/pr106188.C @@ -0,0 +1,34 @@ +// { dg-do run { target c++20 } } +// test case from pr106188, w/o workaround +#include + +struct task { + struct promise_type { + task get_return_object() { return task{}; } + void return_void() {} + void unhandled_exception() {} + auto initial_suspend() noexcept { return std::suspend_never{}; } + auto final_suspend() noexcept { return std::suspend_never{}; } + }; +}; + +struct suspend_and_resume { + bool await_ready() const { return false; } + void await_suspend(std::coroutine_handle<> h) { h.resume(); } + void await_resume() {} +}; + +task f() { + if (co_await suspend_and_resume{}, false) {} +} + +task g() { + switch (co_await suspend_and_resume{}, 0) { + default: break; + } +} + +int main() { + f(); + g(); +}