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: 188810 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:42cf:b0:101:a8e8:374 with SMTP id q15csp852868dye; Wed, 17 Jan 2024 03:53:16 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAgxYGTwliWFdJXIsIXAQf/7IZ913HjB8K0+FAxRdGCojPuxkedaNZeEufKrq0C04l4NPf X-Received: by 2002:a05:622a:44b:b0:429:bc57:d0a5 with SMTP id o11-20020a05622a044b00b00429bc57d0a5mr11562845qtx.93.1705492396713; Wed, 17 Jan 2024 03:53:16 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705492396; cv=pass; d=google.com; s=arc-20160816; b=riNNXcsY4XOD1XHEfQYiRopSe5gbNCwJvK6mKc1roTAI5mhQek/tPJKae+zxFPDdOA PEeJyfep09wxHAZzbhlIaGpDbKJUfI2vv8RmBkJWC21ThipCUnKmqZ1j5SA9y7T5z8cF qnMgCARDdFKPW0YCLqOFWvhcTfwKS65VvbeHIeMKI2fdITnhP/UBe1v1WZE+Y+xrzkF/ QT4Az8rFZLIAxnjW2bPawvLAzYemSluEsV5qv7J/Nibw/i471CuxQGtQRMWNTEN19iBz v1oQTNhsOuj8VMkOOblh5yzyS7+EPM+4KCsORWB0U3hS/UJekHzUxOGKFViVvK54lQgb CzKQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:cc:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=/d/Ea34BKF7wxZ8htxxKppMYSIOQ+PZhcGQ+4GXRZUs=; fh=YrIzwRxucaHOA7FZ0kt0wo7/wUyz4DZgXyhvMBUHICk=; b=jUL0QeUNuapuwTk7DaX5blQ/YaY/fzsWvREd4MxfCEa484egBwOY7xVVmMWH7opZUR GA/zwY+fYkueQ796vUqahLXveGOqHWfsnpQBGwXNQGUZVTzRwRdCDxDYeZbDMOvYclQk S6yvkUoRR1o80pjVkqcE/kkfuN1qa2I56FT37W4wHyeQ4LYDCS/TU0HXscQxAqqixWcg P11OGwjl0hF7+XFJWZdcABz4h/cdMZkjhNsaUQetHX4PjzCB2kEPCRnOYV/ou3PzkDa1 /6V/AlX8ds2YTc+4ip5S6lTyfHHMD0WVOEmivm0OINWJTzjbGo8Xxq0gUQFPJrlQQh3L mCYQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=fail header.i=@mitalia.net header.s=default header.b=t5xM68lv; arc=pass (i=1); 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" Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id dy33-20020a05620a60e100b0078320788f94si11388654qkb.179.2024.01.17.03.53.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Jan 2024 03:53:16 -0800 (PST) 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=fail header.i=@mitalia.net header.s=default header.b=t5xM68lv; arc=pass (i=1); 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6A33D3857733 for ; Wed, 17 Jan 2024 11:53:16 +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+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788338395243762380 X-GMAIL-MSGID: 1788338395243762380 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 (); }