From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id rximL40g8moTVzsAWB0awg (envelope-from ) for ; Mon, 09 Nov 2026 14:35:41 -0500 Authentication-Results: simark.ca; dkim=pass (2048-bit key; secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=ailG8RUk; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 9E9381E06A; Mon, 09 Nov 2026 14:35:41 -0500 (EST) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on simark.ca X-Spam-Level: X-Spam-Status: No, score=-2.4 required=5.0 tests=ARC_SIGNED,ARC_VALID,BAYES_00, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,MAILING_LIST_MULTI, RCVD_IN_DNSWL_MED,RCVD_IN_VALIDITY_CERTIFIED_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED,RCVD_IN_VALIDITY_SAFE_BLOCKED autolearn=ham autolearn_force=no version=4.0.1 Received: from vm01.sourceware.org (vm01.sourceware.org [38.145.34.32]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange x25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPS id C74C91E023 for ; Mon, 09 Nov 2026 14:35:40 -0500 (EST) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E55FB4B920D4 for ; Mon, 9 Nov 2026 19:35:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E55FB4B920D4 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=ailG8RUk Received: from mail-vk1-xa29.google.com (mail-vk1-xa29.google.com [IPv6:2607:f8b0:4864:20::a29]) by sourceware.org (Postfix) with ESMTPS id DB4ED4B99F7D for ; Mon, 9 Nov 2026 19:35:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB4ED4B99F7D Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org DB4ED4B99F7D Authentication-Results: sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::a29 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794252909; cv=none; b=mCjJZ/467JCoCsB8WG7YPXTK6Z74su+HpkBbW2/KmDBSUm9KAkI+MJPu4yvzONdW7u7fR7/hHSgjxojM0BpCsSFEjFJIT/O4CL4MvSaLJ9HxGHTtgGGsBEw+wEfTIPWCiAtM2ZpxJPaLEtGyOJS0nCv/lgXQ0vzdkhC8TdlC8ls= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1794252909; c=relaxed/simple; bh=ut+rCg22p089Ezo73up4GSml4K7VhZdAm4KyE+sW97A=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Z7SDVmoaie1WDfqreYuNLzXK+hhKaG1o7HQ0KQpnS2v7MVd0qlwNgZ6+XMfvQDsVjwUz6nqFsODqjn9fyxUAJnmwZGpnTXzq09FaD/bXsRFqQmxXxxmrmP+a5Hc05OTcFwy/1YgUGGBHWOz15wpe+ThuGThR1Po6WMeGZ2IuNDo= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, secure) header.d=adacore.com header.i=@adacore.com header.a=rsa-sha256 header.s=google header.b=ailG8RUk DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org DB4ED4B99F7D Received: by mail-vk1-xa29.google.com with SMTP id 71dfb90a1353d-5eadf48fb2dso512092e0c.1 for ; Mon, 09 Nov 2026 11:35:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1794252908; x=1794857708; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=xihmmvT1kZdfxauc5/syO8Z1MMkXFBRARNFzdArmiz8=; b=ailG8RUkoEO3oFisXmh8eeFuJaYVkTnlNXjn0nJEVuarbxFVZJ5TqTjZOMc1SIt2cY PKdVzuwieEBDRsyrMiurihuas8fcQokmZsUOdadE6vOYKLkbhOT5tvt6i6qqeb6nmGnW 9t70vkZ086fw/AeFgNsc7AXeWBEtp9OTf/IkvTKEgpCXQ1tzcjWYk9itI0eoh+YQfCIu qJ0WC9SuX/SbMkM2hvMcFZfcyRtxNB2EkQlZ8ih4UkoDpCAwCuT+6dufWWBIIB+g7GoN nKdib30FZ56M08hWvqyMvMJWY3CA9cUGyibqPZPNgiFEz7DM3Yk54KRY1DHGZZP0ln/i czyw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1794252908; x=1794857708; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=xihmmvT1kZdfxauc5/syO8Z1MMkXFBRARNFzdArmiz8=; b=BgGw2BqxZROaAWAINg7/yhOxWlyuIgmPNjGO15jATmv6zv2px0z5grtweXjqM0XjAl FQh0BVhR13wplbDFB9LdeOrTsDp2rQA43E7xYaZdSVbg0KZzU+Psz4sEK53rb+s1PcsW 3hxGKfUTCCiPieCCoECbs4pn6V37tW14FrVboGAo6MA6ZeP2cUASSgi3uUAAleGm+BUL VOAVMr92E9o6m73BS4ilAXQqPpRzzmvp/WJvutRiAf45JC4bd4UJfEWrGWUUopU8FCiY ZusLg8taCh1XyktxBFqqBBpY/gf5cXt6+i0h/zPjfLdFHQ06Q+tne5xLgVXtQeIAMCCJ lHyA== X-Gm-Message-State: APuU4Z/zzPWKCLGSTGOE0I5z1LWjW12eECHDv6nM6IDkVYbK38SxJRMx 2oNRhKgRu+z2m7ZFWMbvAVkZEuvlUGf8+JZFv+Kc09+nPoGVXZ8iZEn18YyvIXPyGx9bJyNcKB6 iM5r0Tg== X-Gm-Gg: AYBFou2qhSr2yQR09i81ctoLI/55MH2Vzz6bIjhk+OubryMo5CufkwouG8kqaiSpZS9 ztL6iT8XzNBhZOHzkpLdtS/a3iGDxQzfB4qnXYJvz55A/S5sUJ9OY5H6frbET0DyWKV9S2e2QeO 7CCRT9Kk1GCZOSUUlCE83dlIz0UplUHdq1RITcO4qiriTwWeWZ8Bab3n/ySYACTPT+VrJj9qMPR H8qHVGy0AADKewqG93tvUOql2QFrNSRX3yl8ZGJVir770hO9S6AKYUIRGDnE10fsHkalrp/Gho7 2J9w9QoY4oZGlKvSLP+ZVRmSJT8ueBoWWKNO5VwMdRMAu1hqLs3DR5sVNtGjQ5j3oYewKJbKnVB R3IRpD1UmmX0ytaWSJABs8K0vpyXodNZGIwbKZEd8Hw24tFRigyYctEr/OOZW1PipkBvEkb4mRl 91E62wsYt/1+XIqp5ilxdc4TQPM+MoQHJnUpVJIS9MA3KM0fmzoJjWMyQzsPCtkb+1YBRKDO/0b jwj6Yad2FjUWcbmDg1G+lzjhvtLzU3xMSos87pNinSCWFSDXpzD1AIdss664uZSoip3Kbz1rH36 VBlfyaJGu7EuAMXq6n9Ia0X/gSTLmwDYJUVnB7oHOoMxk8OTvoCuUPJzOW2SXNTW12yTEHE2 X-Received: by 2002:a05:6123:b2:b0:5ec:d5e1:ee27 with SMTP id 71dfb90a1353d-5ee0d195596mr66331e0c.20.1794252908215; Mon, 09 Nov 2026 11:35:08 -0800 (PST) Received: from bapiya (97-122-110-129.hlrn.qwest.net. [97.122.110.129]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-5ee0c45a110sm402271e0c.2.2026.11.09.11.35.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 09 Nov 2026 11:35:07 -0800 (PST) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Avoid possible use-after-free in Ravenscar code Date: Mon, 9 Nov 2026 12:35:05 -0700 Message-ID: <20261109193505.1693202-1-tromey@adacore.com> X-Mailer: git-send-email 2.56.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~public-inbox=simark.ca@sourceware.org In the discussion for commit 45460a21309 ("gdb: fix assert after register write failure"), Stephan pointed out that a similar bug affects the Ravenscar thread layer. This patch fixes the problem. I don't have a new test for this -- I don't believe we have any Ravenscar thread tests in the tree, and I don't think they can be written without a certain runtime. However, I did test this using a Ravenscar-enabled target and the internal AdaCore test suite. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34973 --- gdb/ravenscar-thread.c | 67 ++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 29 deletions(-) diff --git a/gdb/ravenscar-thread.c b/gdb/ravenscar-thread.c index b5e0979a7f1..aa2da3a14a7 100644 --- a/gdb/ravenscar-thread.c +++ b/gdb/ravenscar-thread.c @@ -602,33 +602,33 @@ ravenscar_arch_ops::store_register (struct regcache *regcache, stack_address); } -/* Temporarily set the ptid of a regcache to some other value. When - this object is destroyed, the regcache's original ptid is - restored. */ +/* A wrapper for various target methods that temporarily sets the ptid + of a regcache to some other value. If the target does not exit, + the regcache ptid is then reset. */ -class temporarily_change_regcache_ptid +static void +temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid, + gdb::function_view callback) { -public: - - temporarily_change_regcache_ptid (struct regcache *regcache, ptid_t new_ptid) - : m_regcache (regcache), - m_save_ptid (regcache->ptid ()) - { - m_regcache->set_ptid (new_ptid); - } - - ~temporarily_change_regcache_ptid () - { - m_regcache->set_ptid (m_save_ptid); - } + ptid_t save = regcache->ptid (); + regcache->set_ptid (new_ptid); -private: + try + { + callback (); + } + catch (const gdb_exception &ex) + { + /* Note here we want to catch QUIT as well, just in case. If + the target has closed, then the regcache is destroyed and we + don't want to update it. */ + if (ex.error != TARGET_CLOSE_ERROR) + regcache->set_ptid (save); + throw; + } - /* The regcache. */ - struct regcache *m_regcache; - /* The saved ptid. */ - ptid_t m_save_ptid; -}; + regcache->set_ptid (save); +} ravenscar_thread_target::fpu_state ravenscar_thread_target::get_fpu_state (struct regcache *regcache, @@ -709,8 +709,11 @@ ravenscar_thread_target::fetch_registers (struct regcache *regcache, if (use_beneath) { - temporarily_change_regcache_ptid changer (regcache, base); - beneath ()->fetch_registers (regcache, i); + temporarily_change_regcache_ptid (regcache, base, + [=] () + { + beneath ()->fetch_registers (regcache, i); + }); } else arch_ops->fetch_register (regcache, i); @@ -755,8 +758,11 @@ ravenscar_thread_target::store_registers (struct regcache *regcache, if (use_beneath) { - temporarily_change_regcache_ptid changer (regcache, base); - beneath ()->store_registers (regcache, i); + temporarily_change_regcache_ptid (regcache, base, + [=] () + { + beneath ()->store_registers (regcache, i); + }); } else arch_ops->store_register (regcache, i); @@ -776,8 +782,11 @@ ravenscar_thread_target::prepare_to_store (struct regcache *regcache) if (task_is_currently_active (ptid)) { ptid_t base = get_base_thread_from_ravenscar_task (ptid); - temporarily_change_regcache_ptid changer (regcache, base); - beneath ()->prepare_to_store (regcache); + temporarily_change_regcache_ptid (regcache, base, + [=] () + { + beneath ()->prepare_to_store (regcache); + }); } else { base-commit: 96248aa4173886a47a7aa1798861267edde28801 -- 2.56.0