From patchwork Wed May 24 18:55:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 98660 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp114574vqr; Wed, 24 May 2023 11:57:07 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6BEUYiOWsGSGdKQ/IKfORXOxUjWf6nR/+4jf62+NzPn0p6gnXW81HRLzS7FDLkHWxSL1mS X-Received: by 2002:a17:907:94c1:b0:94f:7a8:a902 with SMTP id dn1-20020a17090794c100b0094f07a8a902mr17364198ejc.14.1684954627074; Wed, 24 May 2023 11:57:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1684954627; cv=none; d=google.com; s=arc-20160816; b=n26Rmt13dpt5TXqczyD1LK1iaB48NHKAYX8RlcxXXj4FKZ9yRhsWal/r5Rjdkcgu6j 5gGBDXRZowckSSaUcmtGRkchomHjimb+tHABmqciDks0EW4lLjU6K+wvtJclf5Oxk/lk YTOR+B/4jYsCW6zALWQeJguwCQ5rlMpMsiD4iC1ThUmFVO0NncLw7+jsoxthwBPTT+Db Ii1pLfmv5yquyY2AjJ2AopmCdT83VcFriueDHdNgq9QsfBCJWRhLGcIxk79LyLB9Nqir +1k41I9ekXAKho+xssgN7lMoELxpaKMBkw5HePQBB9J6aItfmYFTThX0Num+iIHtYl/K HE+w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=ZzIuuOm3uI+mJiuW+kd1lJtlukwjamN7y+4Zj5p/lkY=; b=R7zxkYQRKY5dE5zsdugWYOoZ8jbqJKtEZcx7n3AcVxaPBNvf4rvmWwfArnO0aLicx4 n9Ye3drPS0Ger+Ot6bGQZ4MKVPIu4VNs2gbqouTv4Mq3h4Hk24ZMOsPEYXn9gdyp0YPF K0cmSnY/+MGULhYdKovqj3g6mqZAKlbFgfe4r52aaMI4Jm+/n6tYQNoz3gFL7FyQvaQe T4ox/HGDr9HBhE1sYRS32yuC/GTRf9BRFzcyntCSYnaxm4UvAMxkB9p01gkDFLH4aeJ4 U30Vh39OrPvyJvAZkqEaZKjHAHBtzQsvhvpnkpXT0RVz6YyNSF1qc0bFTZmPX/FujB22 yQNQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Nr87MBdV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id a7-20020a17090682c700b0096f781f279asi6496367ejy.900.2023.05.24.11.57.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 11:57:07 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Nr87MBdV; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EC5D63857835 for ; Wed, 24 May 2023 18:56:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EC5D63857835 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1684954612; bh=ZzIuuOm3uI+mJiuW+kd1lJtlukwjamN7y+4Zj5p/lkY=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Nr87MBdV7P/1vHmkoaEsoCWaAzu1AFPvQ/5BD+c6idaWHTZg8r5x+j9hL2rNjiTLL da4QoQ1yNIeTV26BUZ0DZCKyRVVJpaC7vaJ/5XVeKzO0jdJ2XTXf71bHRh5WsADJZD QJrMAkPBtuBWgRb+cLiXGzvbGU7AD+y6xLpDzBHc= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 9223C3858D39 for ; Wed, 24 May 2023 18:56:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9223C3858D39 Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-250-vJR9CvJ1MbuOivLVq8go6w-1; Wed, 24 May 2023 14:56:03 -0400 X-MC-Unique: vJR9CvJ1MbuOivLVq8go6w-1 Received: by mail-qv1-f70.google.com with SMTP id 6a1803df08f44-6239c7547f7so796696d6.3 for ; Wed, 24 May 2023 11:56:03 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1684954563; x=1687546563; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=ZzIuuOm3uI+mJiuW+kd1lJtlukwjamN7y+4Zj5p/lkY=; b=JmAQP4YTOGsAyDsdM0RNn49m3YxS9leLhWX8DE4kKf6clF0mPIUNtciKSGTTD90cc0 qGH7BDMu9yX/9oXT2qSJDAdFmMM4C9wUZv+m26Tfz0PQNFbeD3eyI0906FBW6djVX9SG Nz2gCw2WYj3/5jdGTBaVtLYJhejdXu3M4ggPdSRbv/dFoxfkE8lNbxiMY1B/Qucr+qwb BeMZbnCp1XtNK4US5aFjhJGQKb5OnOlXteFSunMONwzwV4sVEpKGvaTj6GB+1gGzQNT5 AOfDoLNO0smfTfOydJbTuWm645sXl+KhgCf95JcbFwIhFvhd9lIWFWsP31qnRjgidc/x N94w== X-Gm-Message-State: AC+VfDzhh07OiYN5BR4DFED7+nTbfQ0B2bLtsnij5MrFItaCBrTYQw+t /5LC0Pur7uh2Onx19uSaRGhGChKnz8Za6K+pMiF7TJOAWeGetLNaEup7J/IpnRlHbpfpxjlc+TC zcv0bqGn3mwxRtaunJ/+npkkpZ8ECDwlO9hyeqGwJnwYBchIFhgfmc+bKvDXI0J5WCm3F38Pm5g == X-Received: by 2002:a05:6214:492:b0:623:42c5:6125 with SMTP id pt18-20020a056214049200b0062342c56125mr36961786qvb.7.1684954562830; Wed, 24 May 2023 11:56:02 -0700 (PDT) X-Received: by 2002:a05:6214:492:b0:623:42c5:6125 with SMTP id pt18-20020a056214049200b0062342c56125mr36961755qvb.7.1684954562313; Wed, 24 May 2023 11:56:02 -0700 (PDT) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id dz18-20020ad45892000000b00623819de804sm3759889qvb.127.2023.05.24.11.56.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 May 2023 11:56:01 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: libstdc++@gcc.gnu.org Subject: [PATCH RFC] c++: use __cxa_call_terminate for MUST_NOT_THROW [PR97720] Date: Wed, 24 May 2023 14:55:59 -0400 Message-Id: <20230524185559.1285583-1-jason@redhat.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1766802982811099858?= X-GMAIL-MSGID: =?utf-8?q?1766802982811099858?= Middle-end folks: any thoughts about how best to make the change described 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 properly for the case of not finding a handler (__cxa_throw calls __cxa_begin_catch before std::terminate) and the case of finding a callsite with no landing 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 noexcept 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_terminate instead. However, that requires exporting it from libstdc++, which we have not previously done. Despite the name, it isn't actually part of the ABI 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 necessary 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 say 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 base-commit: 2738955004256c2e9753364d78a7be340323b74b 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_TERMINATE_FN] #define call_unexpected_fn cp_global_trees[CPTI_CALL_UNEXPECTED_FN] +#define call_terminate_fn cp_global_trees[CPTI_CALL_TERMINATE_FN] #define get_exception_ptr_fn cp_global_trees[CPTI_GET_EXCEPTION_PTR_FN] #define begin_catch_fn cp_global_trees[CPTI_BEGIN_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 reachable from within the function, rather than implementable from the runtime. - The normal way for this to happen is for there to be a CLEANUP region + The normal way for this to happen is for there to be a TRY region contained within this MUST_NOT_THROW region. Note that if the runtime handles the MUST_NOT_THROW region, we have no control over 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 = gimple_build_eh_must_not_throw (terminate_fn); + mnt = gimple_build_eh_must_not_throw (call_terminate_fn); gimple_seq_add_stmt_without_update (&catch_, mnt); mnt = 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 = build_function_type_list (void_type_node, ptr_type_node, NULL_TREE); call_unexpected_fn = push_throw_library_fn (get_identifier ("__cxa_call_unexpected"), tmp); + call_terminate_fn + = 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++.dg/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 = gimple_block_label (new_bb); gsi2 = gsi_start_bb (new_bb); + /* Handle failure fns that expect either no arguments or the + exception pointer. */ fn = dst_r->u.must_not_throw.failure_decl; - x = gimple_build_call (fn, 0); + if (TYPE_ARG_TYPES (TREE_TYPE (fn)) != void_list_node) + { + tree epfn = builtin_decl_implicit (BUILT_IN_EH_POINTER); + src_nr = build_int_cst (integer_type_node, src_r->index); + x = gimple_build_call (epfn, 1, src_nr); + tree var = create_tmp_var (ptr_type_node); + var = make_ssa_name (var, x); + gimple_call_set_lhs (x, var); + gsi_insert_after (&gsi2, x, GSI_CONTINUE_LINKING); + x = gimple_build_call (fn, 1, var); + } + else + x = 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 + = 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 {