From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 389EF3858D32 for ; Sun, 20 Aug 2023 11:21:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 389EF3858D32 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1692530509; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=YL+nzzBImDIhIs+hVzQGtemLmaMmKCVEiEzYtiDoJIw=; b=fqr1INSXHPjgLF1p5efjfsTF7vs4BN2MIxCIUZOkTAXqOUZMlnFb2do4EK8ewf+rrDRM9j Zm8i4ego4fY+BX9ujxQqU+sQlwk04xftd8XP/fZWT4J53OFcvUR/ifGAR4b+KpMizT4Vs9 g1F/aN2cN3qO8xH9zaABFmEQy0dnNS4= Received: from mail-lj1-f198.google.com (mail-lj1-f198.google.com [209.85.208.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-263--3mj9gTlOZC8ABqCHk7QHg-1; Sun, 20 Aug 2023 07:21:47 -0400 X-MC-Unique: -3mj9gTlOZC8ABqCHk7QHg-1 Received: by mail-lj1-f198.google.com with SMTP id 38308e7fff4ca-2bbc1d8011dso9065151fa.1 for ; Sun, 20 Aug 2023 04:21:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692530506; x=1693135306; h=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=YL+nzzBImDIhIs+hVzQGtemLmaMmKCVEiEzYtiDoJIw=; b=UsPpgYPGJP8wFXC7hnP7oOm+AO7mQ5pnZrBBqw852/AzTgSXxR9K37rDu/sp8HF5+a WwOVrM8tcEcw5Mr5WKhDgiIuRDE/iPU4oYTpcsf4QVr+rWklPuK9mg09Sqyrj6XX67MO XYWkNVfqgfKnyCYY4qWpqtyzonV9oZ4Z1HI0ZSfIFeaWeTExgCpx42qtJzkoUB5jZhSv TOB8w4wWdMdfa2UlnFU2GW60Vct5RBySMlYWAXiKb7fTyvOcN2QwwKpnqZhJjkFyO+9w g1OEwz9xNf4KLPDVOH8h/0JdKRYcBalvfvFwqB2/kIrDrBkUPr4EFHFxNwrmoyc/6F3p N3UA== X-Gm-Message-State: AOJu0YySwpk7zNchrljMRqzF3j0VetyOx/IjK17Bl8KDUw/nSec070ip TC95qNydMIQQa3J0/ldkkwv+fweKHMyyQ1ZLJpPzdmdhsxEnEcUpeZoVhYarpNJQG3IV7Z1D1rl XtmXV/zNP2ZiE8bNVj8gjkTCmGeM7XWk7+Qri6VwIOLnN X-Received: by 2002:a2e:330f:0:b0:2b8:4079:fd9d with SMTP id d15-20020a2e330f000000b002b84079fd9dmr2807233ljc.29.1692530505957; Sun, 20 Aug 2023 04:21:45 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGS2hhqTgosW0S0L4skMpujwgckey0LDWBYgl29G5eQbv4bxIp/+CYT+oUMJkCVAEpOinuI7vdHHCmZ70QJThk= X-Received: by 2002:a2e:330f:0:b0:2b8:4079:fd9d with SMTP id d15-20020a2e330f000000b002b84079fd9dmr2807227ljc.29.1692530505536; Sun, 20 Aug 2023 04:21:45 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Sun, 20 Aug 2023 12:21:35 +0100 Message-ID: Subject: Re: 18_support/nested_exception/rethrow_if_nested-term.cc To: Iain Sandoe Cc: libstdc++@gcc.gnu.org X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: multipart/mixed; boundary="0000000000008e14ad060358f77f" X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP 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: --0000000000008e14ad060358f77f Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Sun, 20 Aug 2023 at 11:08, Iain Sandoe wrote: > > Hi > > I=E2=80=99ve been trying to figure out why the test from $SUBJECT fails w= hen I use the c++abi library (either with clang or g++ and a modified libst= dc++ that uses c++abi for it=E2=80=99s runtime support). > > At the moment, I am not sure where exactly the issue lies - but I suspect= it might be in implementation divergence over the set_terminate call. > > The issue seems to center around interpretation of the following statemen= t in the upstream cxxabi: > > " =E2=80=A2 The fields unexpectedHandler and terminateHandler conta= in pointers to the unexpected and terminate handlers at the point where the= exception is thrown. The ISO C++ Final Draft International Standard [lib.u= nexpected] (18.6.2.4) states that the handlers to be used are those active = immediately after evaluating the throw argument. If destructors change the = active handlers during unwinding, the new values are not used until unwindi= ng is complete.=E2=80=9D > > - but I cannot find that statement in the current C++ draft, instead it s= eems to say: std::unexpected and the unexpected handler were removed in C++17, see [diff.cpp14.expect]. When std::terminate is called it's implementation-defined whether the stack is unwound. For GCC, it isn't, so destructors cannot change the handler during unwinding if there is no unwinding. > > "It is unspecified which terminate_handler function will be called if an = exception is active during a call to set_terminate. Otherwise calls the cur= rent terminate_handler function.=E2=80=9D (https://eel.is/c++draft/support.= exception#terminate). Interesting, I wasn't aware of (or had forgotten) this implementation variance. The libsupc++ std::terminate always loads the global terminate handler. I assume libc++abi uses the active exception's terminateHandler if there is an active exception, and the global one otherwise. C++14 seems to only allow the GCC behaviour, std::terminate just says "Calls the current terminate_handler function." That seems clearly the one that std::get_terminate() returns, which is the one that was set by std::set_terminate. It changed with https://cplusplus.github.io/LWG/issue2111 (reported by Howard) which allows the libc++abi behaviour. Libsupc++ has always just called __cxxabiv1::__terminate(get_terminate()) for an explicit call to std::terminate(), which is what happens in this test when rethrow_nested() can't throw anything. When the runtime wants to terminate using an exception's active handler it calls __cxxabiv1::__terminate(ex->terminateHandler), instead of using std::terminate(). > > =E2=80=94 > > If I set the terminate handler before the throw, then the c++abi implemen= tation works the same as the supc++ one. > > For both runtime support libraries, if I inspect the current exception fr= om the terminate handler it shows we have a _nested . Yes, that should be true after the throw_with_nested on line 17. The standard doesn't allow any variance here. > So, ISTM that the difference is not in the actual nested exception handli= ng (including the conditional rethrow) but lies, instead in a different int= erpretation of when the changes from set_terminate() take effect. I don't think it's when the changes take effect, that has to be instant (i.e. get_terminate() must return the new handler immediately). It's whether std::terminate() actually uses the result of std::get_terminate() or uses the active exception's stored handler. > =E2=80=94 it needs someone with state on the history of this to comment = =E2=80=A6 > > a) does that seem like a reasonable analysis? > b) would it be acceptable to move the set_terminate () call? I didn't intend this test to depend on unspecified behaviour. But just moving it before the throw would mean that if std::throw_with_nested terminates for some reason, we would exit cleanly and the test would pass, which we don't want. I would prefer the attached change instead. I assume this works with both runtimes? (This also gives a FAIL if throw_with_nested returns normally for some reason, which would also currently PASS.) > - I can also make changes conditional on the support runtime - but tryi= ng to minimise those. > > thanks > Iain > --0000000000008e14ad060358f77f Content-Type: text/plain; charset="US-ASCII"; name="patch.txt" Content-Disposition: attachment; filename="patch.txt" Content-Transfer-Encoding: base64 Content-ID: X-Attachment-Id: f_lljc75iu0 ZGlmZiAtLWdpdCBhL2xpYnN0ZGMrKy12My90ZXN0c3VpdGUvMThfc3VwcG9ydC9uZXN0ZWRfZXhj ZXB0aW9uL3JldGhyb3dfaWZfbmVzdGVkLXRlcm0uY2MgYi9saWJzdGRjKystdjMvdGV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