From patchwork Wed Jan 17 11:51:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matteo Italia X-Patchwork-Id: 84246 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 58CA73857437 for ; Wed, 17 Jan 2024 11:53:03 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from hostingweb35-23.netsons.net (hostingweb35-23.netsons.net [89.40.175.23]) by sourceware.org (Postfix) with ESMTPS id 4DCBF3858C2A for ; Wed, 17 Jan 2024 11:52:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4DCBF3858C2A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=mitalia.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mitalia.net ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4DCBF3858C2A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=89.40.175.23 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705492358; cv=none; b=imFtunAgnWIx1DkiV1s6BNK40QRbX9WzbA/d4MUqMsJjxHlikpdLKPBaGUcO6oOfcd5WGClIv7IfpAi6WRuvfoeZUicbDghfjLKLoCzh2N+cJJwaicWfEJgdQ2vE4sbH2IxyS89oy5r9EQcbG/HB7ZrsLNYKgdZTrZMNnvMD47Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705492358; c=relaxed/simple; bh=VUvHm//bAxbPhO+Rrn9Dzg/ouYLyD+D8TxAbugQzZck=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=GzpXiC/nWCxljGdCYlrInMFKEbjwm51EV0UBmrwLh/hTGTzFbHCxZZ4WIVmlWAYHR1sjVVAQNfpE+wZL2Ymgr5v8wZyVKg892bGNNCrX8UrECYgdOKDG+EXoqd1aKeKGxo6rGZdA+V2NqV3VqWF2SqAa9ObWkPi3/wG4kJAdjzY= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mitalia.net ; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject :Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=/d/Ea34BKF7wxZ8htxxKppMYSIOQ+PZhcGQ+4GXRZUs=; b=t5xM68lvlVcFqfaTlkx6tA2FJn P7xt+yZt57F4BAfRDLy44xZ8dMJtPkLJmrQ6LSLWOUlG2lrz2HNqQdH7TMzkjc2uzucMwPykyanvi Dn2Kq/Li7iYW6SSV7S2FpFltcKjMbjWNZxinYsHtPXJf6zWmXYhFXqP+zjZ4i/ZpKEdI=; Received: from [81.56.101.54] (port=31501 helo=localhost.localdomain) by hostingweb35.netsons.net with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96.2) (envelope-from ) id 1rQ4TC-001PEJ-2v; Wed, 17 Jan 2024 12:52:35 +0100 From: Matteo Italia To: gcc-patches@gcc.gnu.org Cc: Matteo Italia , 10walls@gmail.com, gingold@adacore.com Subject: [PATCH] libgcc: fix SEH C++ rethrow semantics [PR113337] Date: Wed, 17 Jan 2024 12:51:44 +0100 Message-Id: <20240117115144.50181-1-matteo@mitalia.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - hostingweb35.netsons.net X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - mitalia.net X-Get-Message-Sender-Via: hostingweb35.netsons.net: authenticated_id: matteo@mitalia.net X-Authenticated-Sender: hostingweb35.netsons.net: matteo@mitalia.net X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-14.0 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org SEH _Unwind_Resume_or_Rethrow invokes abort directly if _Unwind_RaiseException doesn't manage to find a handler for the rethrown exception; this is incorrect, as in this case std::terminate should be invoked, allowing an application-provided terminate handler to handle the situation instead of straight crashing the application through abort. The bug can be demonstrated with this simple test case: === static void custom_terminate_handler() { fprintf(stderr, "custom_terminate_handler invoked\n"); std::exit(1); } int main(int argc, char *argv[]) { std::set_terminate(&custom_terminate_handler); if (argc < 2) return 1; const char *mode = argv[1]; fprintf(stderr, "%s\n", mode); if (strcmp(mode, "throw") == 0) { throw std::exception(); } else if (strcmp(mode, "rethrow") == 0) { try { throw std::exception(); } catch (...) { throw; } } else { return 1; } return 0; } === On all gcc builds with non-SEH exceptions, this will print "custom_terminate_handler invoked" both if launched as ./a.out throw or as ./a.out rethrow, on SEH builds instead if will work as expected only with ./a.exe throw, but will crash with the "built-in" abort message with ./a.exe rethrow. This patch fixes the problem, forwarding back the error code to the caller (__cxa_rethrow), that calls std::terminate if _Unwind_Resume_or_Rethrow returns. The change makes the code path coherent with SEH _Unwind_RaiseException, and with the generic _Unwind_Resume_or_Rethrow from libgcc/unwind.inc (used for SjLj and Dw2 exception backend). libgcc/ChangeLog: * unwind-seh.c (_Unwind_Resume_or_Rethrow): forward _Unwind_RaiseException return code back to caller instead of calling abort, allowing __cxa_rethrow to invoke std::terminate in case of uncaught rethrown exception --- libgcc/unwind-seh.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libgcc/unwind-seh.c b/libgcc/unwind-seh.c index 8ef0257b616..f1b8f5a8519 100644 --- a/libgcc/unwind-seh.c +++ b/libgcc/unwind-seh.c @@ -395,9 +395,9 @@ _Unwind_Reason_Code _Unwind_Resume_or_Rethrow (struct _Unwind_Exception *exc) { if (exc->private_[0] == 0) - _Unwind_RaiseException (exc); - else - _Unwind_ForcedUnwind_Phase2 (exc); + return _Unwind_RaiseException (exc); + + _Unwind_ForcedUnwind_Phase2 (exc); abort (); }