From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pg1-x52d.google.com (mail-pg1-x52d.google.com [IPv6:2607:f8b0:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id E72C53858C42; Sun, 14 Jan 2024 01:17:39 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E72C53858C42 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org E72C53858C42 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::52d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705195072; cv=none; b=BspwU057LaGI76Upel7b38HwftKdxthE46O5kmv5slk5H39z5agDnhmsRkX6jfinNGmNh1bnf/q1f838iXuzQ2+0v0Ci/+WTsvf5+5AEoNgQ23YRR4nHhSYY3cjEdRTeRLJjB3zFby03eRwdpcZNo6Kec2hfand4u2ABekxQHGM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705195072; c=relaxed/simple; bh=4Ww8Qu0MwSpRBNTAbpY/FYVWJ1nho0qPgjcFDfhjjV4=; h=DKIM-Signature:MIME-Version:From:Date:Message-ID:Subject:To; b=sx+G1JdXILMxCQrrre+/C3PwHhsr/FRM+f4BoqbuB359j+Dil3xXGmUGtUMXqIQGD2wT9j+gSg+mZGSVZ4nAxKew/Fv0HBdEtZfdXy6Hnm3Tz8SZt43lva+ykhC9igln5VymYjsNMrZUBEP/nlglVd+fHrjovaGobxcebiJM8n0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pg1-x52d.google.com with SMTP id 41be03b00d2f7-5cf2d73a183so2099499a12.1; Sat, 13 Jan 2024 17:17:39 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705195059; x=1705799859; darn=gcc.gnu.org; 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=tVdqR4D0+TaCN6Dn+HPZQFu4r+EEUK25y4lIFT8BMB4=; b=H7GQiXavHNZHKnG7E0WpJQgl2eLSTU0Wu+qFOyWmNa33ozDFKxlORIO6VnqVAuBXTB vkXCeWdnsVayVytqcnJXRusRS+5W/vuZVkm8Wvzo+mbw68xb8Ng32ssqucdyl7KYE0Ym kI9W/jxu/J1+D6TPS1M3l0nEHu6YjvdWn6z5nDV4G2HJKqq46WWRAS6XT5S1HokbGzCQ mSfJjDO0Xyka7T8VbfAShyPP+6gsAR6ciSn4rc4Y7fuSbgRC3uDYg3rsTxolm9Q6P7re yQ0Sy05rJJuyLAesN9S6JIdvwJeAJf5iNZZerhhzdEc4adqMqIsuACC9IZlNeLI3Osf1 mvKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705195059; x=1705799859; 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=tVdqR4D0+TaCN6Dn+HPZQFu4r+EEUK25y4lIFT8BMB4=; b=ErYu0QeRpEjTx8RgUOiy5hvnmkaUXlmSxxGS5JbBcHrzF4ouyzN1R8nbbd/9wCzGFt ntsMq+2E86DMYQXtH0IVmaZeAJ/0j/T9XhZr/xSzlApk3t//WRWNFQYmyPGx+K/cpoSr XkjVcq6TlOFQF+SqfGcNvs/HOQXQ//bFRdDsVAM45dR9s1O5HweyhycepWMiHKVhQbMC a5ekT9IBV9mVaRKEDIYe6WAFg50K1CVP4t5WYaLxB/jGIJ++RHbBGtjbNxnOuvqdp4Kr 01xIQctyxXoY/7eNt0FLl6N5GRl2H/IKUHu4faPDtqEfkTAdpldSBTRU446kvWb2mYzn dSpw== X-Gm-Message-State: AOJu0YzDZD9t4EZINKPUDSzIu0CCh+OaIDWMjMvInJer5MrlcWzHwD+5 3qxIK/HQFpMHhzxQlHVy2QOPnAN5vRmLlsb4Xbw= X-Google-Smtp-Source: AGHT+IHHlEWxvjDKTjJq4hLWYYDqpYtl2rehxfSE44zNd1cfwnLP1H2Li7by5EYpdcX4+a3ml1gPNFFeAV0PRa9Ne2E= X-Received: by 2002:a17:90b:110f:b0:28c:b0a7:c1f9 with SMTP id gi15-20020a17090b110f00b0028cb0a7c1f9mr7337989pjb.16.1705195058524; Sat, 13 Jan 2024 17:17:38 -0800 (PST) MIME-Version: 1.0 References: <20240114000534.1775261-1-me@jdemille.com> In-Reply-To: <20240114000534.1775261-1-me@jdemille.com> From: Andrew Pinski Date: Sat, 13 Jan 2024 17:17:26 -0800 Message-ID: Subject: Re: [PATCH] libsupc++: Fix UB terminating on foreign exception To: Julia DeMille 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=-8.1 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 Sat, Jan 13, 2024 at 5:10=E2=80=AFPM Julia DeMille wro= te: > > Currently, when std::terminate() is called with a foreign exception > active, since nothing in the path checks whether the exception matches > the `GNUCC++\0` personality, a foreign exception can go into the verbose > terminate handler, and get treated as though it were a C++ exception. > > Reflection is attempted, and boom. UB. This patch should eliminate that > UB. 2 things, changelogs go into the email message rather than directly as part of the patch., Second I wonder if you could add a multiple language testcase using GNU objective-C exceptions as an example. If not directly adding a testcase there, at least a simple test that shows the issue outside of the testsuite? Thanks, Andrew Pinski > > Signed-off-by: Julia DeMille > --- > libstdc++-v3/ChangeLog | 9 +++++++++ > libstdc++-v3/libsupc++/eh_type.cc | 11 +++++++++++ > libstdc++-v3/libsupc++/vterminate.cc | 25 ++++++++++++++++++++----- > 3 files changed, 40 insertions(+), 5 deletions(-) > > diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog > index 36257cc4427..bfef0ed8ef1 100644 > --- a/libstdc++-v3/ChangeLog > +++ b/libstdc++-v3/ChangeLog > @@ -1,3 +1,12 @@ > +2024-01-13 Julia DeMille > + * libsupc++/eh_type.cc (__cxa_current_exception_type): > + Return NULL if the current exception is not the `GNUCC++\0` > + personality. > + * libsupc++/vterminate.cc: > + Check for both exception header and exception type. If there > + is an exception header, but no exception type, state in terminati= on > + message that a foreign exception was active. > + > 2024-01-13 Jonathan Wakely > > PR libstdc++/107466 > diff --git a/libstdc++-v3/libsupc++/eh_type.cc b/libstdc++-v3/libsupc++/e= h_type.cc > index 03c677b7e13..e0824eab4d4 100644 > --- a/libstdc++-v3/libsupc++/eh_type.cc > +++ b/libstdc++-v3/libsupc++/eh_type.cc > @@ -36,9 +36,20 @@ extern "C" > std::type_info *__cxa_current_exception_type () _GLIBCXX_NOTHROW > { > __cxa_eh_globals *globals =3D __cxa_get_globals (); > + > + if (!globals) > + return 0; > + > __cxa_exception *header =3D globals->caughtExceptions; > + > if (header) > { > + // It is UB to try and inspect an exception that isn't ours. > + // This extends to attempting to perform run-time reflection, as t= he ABI > + // is unknown. > + if (!__is_gxx_exception_class (header->unwindHeader.exception_clas= s)) > + return 0; > + > if (__is_dependent_exception (header->unwindHeader.exception_class= )) > { > __cxa_dependent_exception *de =3D > diff --git a/libstdc++-v3/libsupc++/vterminate.cc b/libstdc++-v3/libsupc+= +/vterminate.cc > index 23deeef5289..f931d951526 100644 > --- a/libstdc++-v3/libsupc++/vterminate.cc > +++ b/libstdc++-v3/libsupc++/vterminate.cc > @@ -25,11 +25,12 @@ > #include > > #if _GLIBCXX_HOSTED > -#include > -#include > +#include "unwind-cxx.h" > #include > +#include > +#include > #include > -# include > +#include > > using namespace std; > using namespace abi; > @@ -51,10 +52,19 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > } > terminating =3D true; > > + __cxa_eh_globals *globals =3D __cxa_get_globals (); > + if (!globals) > + { > + fputs ("terminate called", stderr); > + abort (); > + } > + > // Make sure there was an exception; terminate is also called for an > // attempt to rethrow when there is no suitable exception. > - type_info *t =3D __cxa_current_exception_type(); > - if (t) > + type_info *t =3D __cxa_current_exception_type (); > + __cxa_exception *header =3D globals->caughtExceptions; > + > + if (t && header) > { > // Note that "name" is the mangled name. > char const *name =3D t->name(); > @@ -89,6 +99,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION > #endif > __catch(...) { } > } > + else if (header) > + { > + fputs ("terminate called after a foreign exception was thrown\n"= , > + stderr); > + } > else > fputs("terminate called without an active exception\n", stderr); > > -- > 2.43.0 >