From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from simark.ca by simark.ca with LMTP id cWtcGEY1w2ouWDoAWB0awg (envelope-from ) for ; Mon, 05 Oct 2026 01:27:34 -0400 Authentication-Results: simark.ca; dkim=pass (2048-bit key; unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=gI/zDyzE; dkim-atps=neutral Received: by simark.ca (Postfix, from userid 112) id 581DF1E047; Mon, 05 Oct 2026 01:27:34 -0400 (EDT) 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 C87551E047 for ; Mon, 05 Oct 2026 01:27:32 -0400 (EDT) Received: from vm01.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 8B39C4BA23E9 for ; Mon, 5 Oct 2026 05:27:31 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 8B39C4BA23E9 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=gI/zDyzE Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id 574A34BA23D1 for ; Mon, 5 Oct 2026 05:26:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 574A34BA23D1 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 574A34BA23D1 Authentication-Results: sourceware.org; arc=none smtp.remote-ip=132.207.4.11 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791177974; cv=none; b=aj3QDY4t1w1CqtIwOZl3ge9JXSSYtn8xbvXd9mXluk7VB5/o3hShpk5rnCeEggUes5NkRF/wXFcpe6e733LxSx30d0/awv6xoFO56Jwg87i0T0Xd2q4N2xkkBPmsxSl9NiJhHVorwMFdAoUVE/fX2weufQ1MASuUb1ZsXW8YyaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1791177974; c=relaxed/simple; bh=/G/5qb4fDNGOLdPisnzkmcYuM1+2bytm2h6Qgg0WLU4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=Jhy/5F/4PdbA60qQUdTeTq06CbJ0bFSzbNtseDGl/LOXCmUXLd3KKVpVb9ZbuXPU/TBo1TY/6Rx1A4dGO2bLLiL9/z4KkpCVNHPukYeUeOoS6UlMBRj6y1wjkxw+oqANvUJU/Re8i1eMW7MPoqL2/3JcMxesvfKtxb7CaH9Yu+E= ARC-Authentication-Results: i=1; sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=polymtl.ca header.i=@polymtl.ca header.a=rsa-sha256 header.s=oct2025 header.b=gI/zDyzE DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 574A34BA23D1 Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 6955Q5bX046783 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 5 Oct 2026 01:26:10 -0400 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 6955Q5bX046783 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=oct2025; t=1791177971; bh=I3tvv0ymOGomFH65ZuVz4cob97B0jqbS35b3kjcJD/0=; h=From:To:Cc:Subject:Date:In-Reply-To:From; b=gI/zDyzE5grKPDyiuHGjgqpRcLB/y72YGmaDPwKtfuY8jSnn11gHz07g+EDgfQnrd JIlI4gPs1inhn03lM82LU0Q/3UDl+20ovdyQ3nviF80d15kEYG5kW/HsJhvUBlWSLE ZCtQbplVWL6khK86QCiWBGK7ky9jyXHG/U3tAKmCFI7LAzEPVuNsjDKGkOv+pqVJ/s HZ9nj6WQnt4npa8lMyW8dE66SS8FnyCGDP4SQ+89KtZiyom6vtN9szw1TO1IyqJxBD MAAYhonPeO0/ZWrvzjJxgsJPI8IJwxU5ZhnYpMUht1Py865fMqbKwD/gKWdorECvkz eJdS2RgPejJ+Q== Received: by simark.ca (Postfix) id 6E1CE1E050; Mon, 05 Oct 2026 01:26:05 -0400 (EDT) From: simon.marchi@polymtl.ca To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 3/3] gdb/dwarf: fix crash when .gdb_index contains Rust legacy mangled names Date: Mon, 5 Oct 2026 01:25:49 -0400 Message-ID: <20261005052602.1288766-3-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261005052602.1288766-1-simon.marchi@polymtl.ca> References: <20261005052602.1288766-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Mon, 5 Oct 2026 05:26:05 +0000 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 From: Simon Marchi PR 34707 reports that loading the separate debug file of /usr/lib/libglycin-2.so from Arch Linux crashes GDB (when built with -D_GLIBCXX_DEBUG): $ ./gdb -q -nx --data-directory data-directory $(DEBUGINFOD_URLS=https://debuginfod.archlinux.org debuginfod-find debuginfo /usr/lib/libglycin-2.so) /usr/include/c++/16/string_view:291: ... Assertion 'this->_M_len > 0' failed. This catches a case of calling string_view::back() when the string_view is empty. The file in question contains a .gdb_index with some Rust symbol names that are mangled with the legacy mangling style. The legacy mangling style was the default one until the Rust 1.97 release in July 2026, so it's still very much in use, even though it's called "legacy". Source: https://blog.rust-lang.org/2026/07/09/Rust-1.97.0/ An example of a problematic symbol is: _ZN4core3ptr271drop_in_place$LT$core..iter..adapters..filter..Filter$LT$alloc..vec..into_iter..IntoIter$LT$core..net..socket_addr..SocketAddr$GT$$C$zbus..address..transport..tcp..Tcp..connect..$u7b$$u7b$closure$u7d$$u7d$..$u7b$$u7b$closure$u7d$$u7d$..$u7b$$u7b$closure$u7d$$u7d$$GT$$GT$17h57c03bb201e55682E Method mapped_gdb_index::build_name_components attempts to split the demangled fully-qualified symbol names into parts (it does not know in advance whether a symbol name is a mangled (linkage) name or a demangled one). Since this name does not match the C++ and Ada special cases, it falls into the DOT_STYLE else: components = split_name (name, split_style::DOT_STYLE); Because of the consecutive dots in the symbol name, `components` (an std::vector) contains some empty components. Some code later calls .back() on one of those empty string_views, which is invalid. It seems like it's only caught when gdb is built with _GLIBCXX_DEBUG / _GLIBCXX_ASSERTIONS, which apparently is the case for the official Arch Linux packages. I can't think of a pretty way to fix this. Since we don't have a flag that tells us whether a name is a linkage name or not, we can only make some educated guesses. This patch fixes the problem by looking for empty components in the vector. If there are, we assume that we are probably looking at a Rust legacy mangled symbol name, or at the very least not a fully qualified demangled Go (or whatever) symbol. The name is then treated like other names without separators (a single component with an unknown language). This is a regression introduced by commit 486bc5ac812f ("Rewrite the .gdb_index reader"). Before that, the reader only split names on "::" and "__". This is new in the GDB 18.1 release, so this patch would be a candidate for the gdb-18-branch. Add a gdb.dwarf2 test that builds a Rust CU with such a linkage name, adds an index using gdb-add-index, and verifies that GDB can load it and find the function. The test is done with both .gdb_index and .debug_names, although only the .gdb_index reader was affected. Like the original crash, the test only fails without the fix if GDB is built with libstdc++ assertions. One last thing, I don't know if it's expected for Rust mangled names to appear in the index. For C++, index-write.c does not write them: else if (entry->lang == language_cplus && (entry->flags & IS_LINKAGE) != 0) { /* GDB never put C++ linkage names into .gdb_index. The theory here is that a linkage name will normally be in the minimal symbols anyway, so including it in the index is usually redundant -- and the cases where it would not be redundant are rare and not worth supporting. */ continue; } Perhaps this is the real root of the problem: maybe those names should not have been in the index in the first place, if the same rationale applies to Rust. We could make that change, but even if we do, there are already indexes out there with those mangled names, so we have to handle them. Change-Id: I2c346dfba25edecda970af0726f9d77216838cbd Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=34707 --- gdb/dwarf2/read-gdb-index.c | 21 ++++ .../gdb-index-rust-legacy-linkage-name.c | 31 ++++++ .../gdb-index-rust-legacy-linkage-name.exp | 98 +++++++++++++++++++ 3 files changed, 150 insertions(+) create mode 100644 gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.c create mode 100644 gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.exp diff --git a/gdb/dwarf2/read-gdb-index.c b/gdb/dwarf2/read-gdb-index.c index 324984c0de2d..cb2678d4ea22 100644 --- a/gdb/dwarf2/read-gdb-index.c +++ b/gdb/dwarf2/read-gdb-index.c @@ -203,6 +203,15 @@ struct mapped_gdb_index void build_name_components (dwarf2_per_objfile *per_objfile); }; +/* Return true if any of COMPONENTS is empty. */ + +static bool +has_empty_components (const std::vector &components) +{ + return std::any_of (components.begin (), components.end (), + [] (std::string_view c) { return c.empty (); }); +} + /* See declaration. */ void @@ -251,6 +260,18 @@ mapped_gdb_index::build_name_components (dwarf2_per_objfile *per_objfile) else { components = split_name (name, split_style::DOT_STYLE); + + /* Some names contain dots but are not qualified names, for + example Rust "legacy" mangled names such as + "_ZN4core3ptr80drop_in_place$LT$alloc..vec..drain..Drain...". + If splitting produced an empty component, assume it's not a + qualified name and don't split it. */ + if (has_empty_components (components)) + { + components.clear (); + components.emplace_back (name); + } + /* Mark ordinary names as having an unknown language. This is a hack to avoid problems with some Ada names. */ lang = (components.size () == 1) ? language_unknown : language_go; diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.c b/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.c new file mode 100644 index 000000000000..0d687040a800 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.c @@ -0,0 +1,31 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2026 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +void +func (void) +{ + asm ("func_label: .globl func_label"); +} + +int +main (void) +{ + asm ("main_label: .globl main_label"); + + func (); + return 0; +} diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.exp b/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.exp new file mode 100644 index 000000000000..b995e986355f --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/gdb-index-rust-legacy-linkage-name.exp @@ -0,0 +1,98 @@ +# Copyright 2026 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test reading an index containing a Rust "legacy" mangled linkage +# name. These names contain ".." sequences, which the .gdb_index +# reader used to split into empty components, leading to a crash. +# The .debug_names index is tested too, for completeness. +# +# Note that the crash only happened with libstdc++ assertions enabled +# (e.g. with -D_GLIBCXX_DEBUG). Otherwise, it was silent undefined +# behavior. + +load_lib dwarf.exp + +require dwarf2_support +require !readnow + +standard_testfile .c -dw.S + +set asm_file [standard_output_file $srcfile2] +Dwarf::assemble $asm_file { + cu {} { + DW_TAG_compile_unit { + DW_AT_language @DW_LANG_Rust + DW_AT_name lib.rs + } { + DW_TAG_namespace { + DW_AT_name foo + } { + # The linkage name demangles to foo::bar. + DW_TAG_subprogram { + DW_AT_name "bar" + DW_AT_linkage_name \ + "_ZN3foo19bar\$LT\$foo..Baz\$GT\$17h0123456789abcdefE" + DW_AT_external 1 DW_FORM_flag + MACRO_AT_range {func} + } + } + } + } +} + +if { [build_executable "failed to prepare" ${testfile} \ + [list $srcfile $asm_file] {nodebug}] == -1 } { + return +} + +# For simplicity, skip the test if the executable already has an index (e.g. +# with the cc-with-gdb-index board). The test will be covered when running +# with the unix board. +clean_restart $testfile +if { [get_index_type $binfile "check index type"] ne "cooked" } { + unsupported "executable already has an index" + return +} + +foreach_with_prefix index_type { gdb_index debug_names } { + set testfile_with_index ${testfile}-${index_type} + set binfile_with_index [standard_output_file $testfile_with_index] + remote_exec build "cp $binfile $binfile_with_index" + + if { $index_type eq "gdb_index" } { + set style "" + } else { + set style "-dwarf-5" + } + + if { [add_gdb_index $binfile_with_index $style] != 1 } { + untested "error adding index" + continue + } + + clean_restart $testfile_with_index + gdb_assert { [have_index $binfile_with_index] eq $index_type } \ + "index used" + + # Check that the function can be found. + set re "^foo::bar$" + gdb_test "info functions $re" \ + [multi_line \ + [string_to_regexp \ + "All functions matching regular expression \"$re\":"] \ + "" \ + "File lib.rs:" \ + "\[^\r\n\]*[string_to_regexp "fn foo::bar();"]"] +} -- 2.55.0