From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by sourceware.org (Postfix) with ESMTPS id C167C3858401; Mon, 5 Jun 2023 06:09:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C167C3858401 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-4f3b9755961so5478905e87.0; Sun, 04 Jun 2023 23:09:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1685945387; x=1688537387; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=F1IFTPlsRhOVz+Edn+r5KXtOcWX/7jl37Nd1N9gLo78=; b=Szsd8So5SDSSIYVZK/PieVzuUMbDbGvZU2NSmjNMzSSAp+joA0PumSdgvbr0SqN9+g DPDqJFmofcZmiQ4Edxe2uezrrrEqUGQ3UE9OAmoZgOC9+PZTtn6fnhKQU/eEEgwmJWOd iXdDZ72HdtDyOWcxkfpHnCMuGn/X/+XWQtyqhMqkqnFw39BR/29U1khy4a9CMS4Z2low Hzt4irDoYJwZzvBx3GYYXuhlYJy/EDpowWS658cXkl3pfByhzYih8loUkiCm6Ruo3FgO sgs2FdZgOFOjhAGKLmKEgtMthjaa4c/D0lk6kZzlqsVXxxh3WMsFusahKnsoe0gTXSnt iwFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685945387; x=1688537387; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=F1IFTPlsRhOVz+Edn+r5KXtOcWX/7jl37Nd1N9gLo78=; b=eHFN00g9pMBiPHVSYsaRGLb7P8Mm9LJn1RLKXH1dxZv7q17WhKUnWjzKTuwV7xsw/R XzMmXmQalxFFhblhgiPljMPVQYqHYqk8xeU6QuMXmqI6rIDJpZBhZ7aVyRHiCubLlA6A kDVDqLqMi7OVzbBpx1sdRpM8xB6h6fvWDqP8Irm4n9DEM5kmm8g0dhYOmWfBIP/dB1BM N3XqjVc7HRH2gCoAjkRG9FrkT+QG1DmNDDqj6CUNg4NCgN+x9Ty5wSb9EfM4490/dX2U BOT1zcbXxRr/DoiHwtrbymDozsEANXcenenBJPsg59Uzgn6eIDREIL6Hdg26DkJQOPoi iD9g== X-Gm-Message-State: AC+VfDxL2rB4KNAseiahX5qCz46UCT6JPUfU6GeXILlTyxN9MLUPyjtW lXusgsDtIjfknLT/ijxJ/qTpR5YNO+Ae2KLBTjV9x0i0 X-Google-Smtp-Source: ACHHUZ5R48Zb7qswYO/7qzTIz0G7S1Cp43lFQqy/nZQmUDF2ibm7brqUJs6jk+YuwaLK+CnLnW+KbBG/C995W19NtIo= X-Received: by 2002:a2e:8eda:0:b0:2b1:e6eb:1fdf with SMTP id e26-20020a2e8eda000000b002b1e6eb1fdfmr640721ljl.42.1685945386878; Sun, 04 Jun 2023 23:09:46 -0700 (PDT) MIME-Version: 1.0 References: <20230524185559.1285583-1-jason@redhat.com> <8b75c557-6b03-8456-dc6e-48f1969c7900@redhat.com> In-Reply-To: <8b75c557-6b03-8456-dc6e-48f1969c7900@redhat.com> From: Richard Biener Date: Mon, 5 Jun 2023 08:09:34 +0200 Message-ID: Subject: Re: PING Re: [PATCH RFA (tree-eh)] c++: use __cxa_call_terminate for MUST_NOT_THROW [PR97720] To: Jason Merrill Cc: gcc-patches@gcc.gnu.org, libstdc++@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-7.2 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Fri, Jun 2, 2023 at 6:57=E2=80=AFPM Jason Merrill via Gcc-patches wrote: > > Since Jonathan approved the library change, I'm looking for middle-end > approval for the tree-eh change, even without advice on the potential > follow-up. > > On 5/24/23 14:55, Jason Merrill wrote: > > Middle-end folks: any thoughts about how best to make the change descri= bed in > > the last paragraph below? > > > > Library folks: any thoughts on the changes to __cxa_call_terminate? > > > > -- 8< -- > > > > [except.handle]/7 says that when we enter std::terminate due to a throw= , > > that is considered an active handler. We already implemented that prop= erly > > for the case of not finding a handler (__cxa_throw calls __cxa_begin_ca= tch > > before std::terminate) and the case of finding a callsite with no landi= ng > > pad (the personality function calls __cxa_call_terminate which calls > > __cxa_begin_catch), but for the case of a throw in a try/catch in a noe= xcept > > function, we were emitting a cleanup that calls std::terminate directly > > without ever calling __cxa_begin_catch to handle the exception. > > > > A straightforward way to fix this seems to be calling __cxa_call_termin= ate > > instead. However, that requires exporting it from libstdc++, which we = have > > not previously done. Despite the name, it isn't actually part of the A= BI > > standard. Nor is __cxa_call_unexpected, as far as I can tell, but that= one > > is also used by clang. For this case they use __clang_call_terminate; = it > > seems reasonable to me for us to stick with __cxa_call_terminate. > > > > I also change __cxa_call_terminate to take void* for simplicity in the = front > > end (and consistency with __cxa_call_unexpected) but that isn't necessa= ry if > > it's undesirable for some reason. > > > > This patch does not fix the issue that representing the noexcept as a > > cleanup is wrong, and confuses the handler search; since it looks like = a > > cleanup in the EH tables, the unwinder keeps looking until it finds the > > catch in main(), which it should never have gotten to. Without the > > try/catch in main, the unwinder would reach the end of the stack and sa= y no > > handler was found. The noexcept is a handler, and should be treated as= one, > > as it is when the landing pad is omitted. > > > > The best fix for that issue seems to me to be to represent an > > ERT_MUST_NOT_THROW after an ERT_TRY in an action list as though it were= an > > ERT_ALLOWED_EXCEPTIONS (since indeed it is an exception-specification).= The > > actual code generation shouldn't need to change (apart from the change = made > > by this patch), only the action table entry. > > > > PR c++/97720 > > > > gcc/cp/ChangeLog: > > > > * cp-tree.h (enum cp_tree_index): Add CPTI_CALL_TERMINATE_FN. > > (call_terminate_fn): New macro. > > * cp-gimplify.cc (gimplify_must_not_throw_expr): Use it. > > * except.cc (init_exception_processing): Set it. > > (cp_protect_cleanup_actions): Return it. > > > > gcc/ChangeLog: > > > > * tree-eh.cc (lower_resx): Pass the exception pointer to the > > failure_decl. > > * except.h: Tweak comment. > > > > libstdc++-v3/ChangeLog: > > > > * libsupc++/eh_call.cc (__cxa_call_terminate): Take void*. > > * config/abi/pre/gnu.ver: Add it. > > > > gcc/testsuite/ChangeLog: > > > > * g++.dg/eh/terminate2.C: New test. > > --- > > gcc/cp/cp-tree.h | 2 ++ > > gcc/except.h | 2 +- > > gcc/cp/cp-gimplify.cc | 2 +- > > gcc/cp/except.cc | 5 ++++- > > gcc/testsuite/g++.dg/eh/terminate2.C | 30 +++++++++++++++++++++++++++= + > > gcc/tree-eh.cc | 16 ++++++++++++++- > > libstdc++-v3/libsupc++/eh_call.cc | 4 +++- > > libstdc++-v3/config/abi/pre/gnu.ver | 7 +++++++ > > 8 files changed, 63 insertions(+), 5 deletions(-) > > create mode 100644 gcc/testsuite/g++.dg/eh/terminate2.C > > > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > > index a1b882f11fe..a8465a988b5 100644 > > --- a/gcc/cp/cp-tree.h > > +++ b/gcc/cp/cp-tree.h > > @@ -217,6 +217,7 @@ enum cp_tree_index > > definitions. */ > > CPTI_ALIGN_TYPE, > > CPTI_TERMINATE_FN, > > + CPTI_CALL_TERMINATE_FN, > > CPTI_CALL_UNEXPECTED_FN, > > > > /* These are lazily inited. */ > > @@ -358,6 +359,7 @@ extern GTY(()) tree cp_global_trees[CPTI_MAX]; > > /* Exception handling function declarations. */ > > #define terminate_fn cp_global_trees[CPTI_TERM= INATE_FN] > > #define call_unexpected_fn cp_global_trees[CPTI_CALL_UNEXPEC= TED_FN] > > +#define call_terminate_fn cp_global_trees[CPTI_CALL_TERMINA= TE_FN] > > #define get_exception_ptr_fn cp_global_trees[CPTI_GET_= EXCEPTION_PTR_FN] > > #define begin_catch_fn cp_global_trees[CPTI_BEGI= N_CATCH_FN] > > #define end_catch_fn cp_global_trees[CPTI_END_= CATCH_FN] > > diff --git a/gcc/except.h b/gcc/except.h > > index 5ecdbc0d1dc..378a9e4cb77 100644 > > --- a/gcc/except.h > > +++ b/gcc/except.h > > @@ -155,7 +155,7 @@ struct GTY(()) eh_region_d > > struct eh_region_u_must_not_throw { > > /* A function decl to be invoked if this region is actually rea= chable > > from within the function, rather than implementable from the run= time. > > - The normal way for this to happen is for there to be a CLEANUP r= egion > > + The normal way for this to happen is for there to be a TRY regio= n I only wondered about this, whether it shouldn't say CLEANUP or TRY instead of just TRY? Do you know of other frontends making use of MUST_NOT_THROW? Otherwise looks OK. Thanks, Richard. > > contained within this MUST_NOT_THROW region. Note that if the > > runtime handles the MUST_NOT_THROW region, we have no control ov= er > > what termination function is called; it will be decided by the > > diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc > > index 216a6231d6f..853b1e44236 100644 > > --- a/gcc/cp/cp-gimplify.cc > > +++ b/gcc/cp/cp-gimplify.cc > > @@ -343,7 +343,7 @@ gimplify_must_not_throw_expr (tree *expr_p, gimple_= seq *pre_p) > > gimple *mnt; > > > > gimplify_and_add (body, &try_); > > - mnt =3D gimple_build_eh_must_not_throw (terminate_fn); > > + mnt =3D gimple_build_eh_must_not_throw (call_terminate_fn); > > gimple_seq_add_stmt_without_update (&catch_, mnt); > > mnt =3D gimple_build_try (try_, catch_, GIMPLE_TRY_CATCH); > > > > diff --git a/gcc/cp/except.cc b/gcc/cp/except.cc > > index 91a5e049860..b04eb00d220 100644 > > --- a/gcc/cp/except.cc > > +++ b/gcc/cp/except.cc > > @@ -64,6 +64,9 @@ init_exception_processing (void) > > tmp =3D build_function_type_list (void_type_node, ptr_type_node, NU= LL_TREE); > > call_unexpected_fn > > =3D push_throw_library_fn (get_identifier ("__cxa_call_unexpected= "), tmp); > > + call_terminate_fn > > + =3D push_library_fn (get_identifier ("__cxa_call_terminate"), tmp,= NULL_TREE, > > + ECF_NORETURN | ECF_COLD | ECF_NOTHROW); > > } > > > > /* Returns an expression to be executed if an unhandled exception is > > @@ -76,7 +79,7 @@ cp_protect_cleanup_actions (void) > > > > When the destruction of an object during stack unwinding exits > > using an exception ... void terminate(); is called. */ > > - return terminate_fn; > > + return call_terminate_fn; > > } > > > > static tree > > diff --git a/gcc/testsuite/g++.dg/eh/terminate2.C b/gcc/testsuite/g++.d= g/eh/terminate2.C > > new file mode 100644 > > index 00000000000..1c69dab95f8 > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/eh/terminate2.C > > @@ -0,0 +1,30 @@ > > +// PR c++/97720 > > +// { dg-do run } > > + > > +// Test that there is an active exception when we reach the terminate = handler. > > + > > +#include > > +#include > > + > > +void bad_guy() throw() { > > + try { throw 0; } > > + catch (float) { } > > + // Don't catch int. > > +} > > + > > +void level1() { > > + bad_guy(); > > + throw "dead code"; > > +} > > + > > +void my_term() > > +{ > > + try { throw; } > > + catch(...) { std::exit(0); } > > +} > > + > > +int main() { > > + std::set_terminate (my_term); > > + try { level1(); } > > + catch (int) { } > > +} > > diff --git a/gcc/tree-eh.cc b/gcc/tree-eh.cc > > index 934209d205f..e8ceff36cc6 100644 > > --- a/gcc/tree-eh.cc > > +++ b/gcc/tree-eh.cc > > @@ -3382,8 +3382,22 @@ lower_resx (basic_block bb, gresx *stmt, > > lab =3D gimple_block_label (new_bb); > > gsi2 =3D gsi_start_bb (new_bb); > > > > + /* Handle failure fns that expect either no arguments or th= e > > + exception pointer. */ > > fn =3D dst_r->u.must_not_throw.failure_decl; > > - x =3D gimple_build_call (fn, 0); > > + if (TYPE_ARG_TYPES (TREE_TYPE (fn)) !=3D void_list_node) > > + { > > + tree epfn =3D builtin_decl_implicit (BUILT_IN_EH_POINTE= R); > > + src_nr =3D build_int_cst (integer_type_node, src_r->ind= ex); > > + x =3D gimple_build_call (epfn, 1, src_nr); > > + tree var =3D create_tmp_var (ptr_type_node); > > + var =3D make_ssa_name (var, x); > > + gimple_call_set_lhs (x, var); > > + gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING); > > + x =3D gimple_build_call (fn, 1, var); > > + } > > + else > > + x =3D gimple_build_call (fn, 0); > > gimple_set_location (x, dst_r->u.must_not_throw.failure_loc= ); > > gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING); > > > > diff --git a/libstdc++-v3/libsupc++/eh_call.cc b/libstdc++-v3/libsupc++= /eh_call.cc > > index 3cfc71a4ef1..2bec4e83a7d 100644 > > --- a/libstdc++-v3/libsupc++/eh_call.cc > > +++ b/libstdc++-v3/libsupc++/eh_call.cc > > @@ -36,8 +36,10 @@ using namespace __cxxabiv1; > > // terminate. > > > > extern "C" void > > -__cxa_call_terminate(_Unwind_Exception* ue_header) throw () > > +__cxa_call_terminate(void* ue_header_in) throw () > > { > > + _Unwind_Exception* ue_header > > + =3D reinterpret_cast<_Unwind_Exception*>(ue_header_in); > > > > if (ue_header) > > { > > diff --git a/libstdc++-v3/config/abi/pre/gnu.ver b/libstdc++-v3/config/= abi/pre/gnu.ver > > index 768cd4a4a6c..a2e5f3b4e74 100644 > > --- a/libstdc++-v3/config/abi/pre/gnu.ver > > +++ b/libstdc++-v3/config/abi/pre/gnu.ver > > @@ -2841,6 +2841,13 @@ CXXABI_1.3.14 { > > > > } CXXABI_1.3.13; > > > > +CXXABI_1.3.15 { > > + > > + global: > > + __cxa_call_terminate; > > + > > +} CXXABI_1.3.14; > > + > > # Symbols in the support library (libsupc++) supporting transactional= memory. > > CXXABI_TM_1 { > > > > > > base-commit: 2738955004256c2e9753364d78a7be340323b74b >